Krolyxon
Commit 27bbaab

Branches

Refs →

nsxivoptions.c

171 lines · 4.2 KB · C

1/* Copyright 2011 Bert Muennich
2 *
3 * This file is part of sxiv.
4 *
5 * sxiv is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published
7 * by the Free Software Foundation; either version 2 of the License,
8 * or (at your option) any later version.
9 *
10 * sxiv is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with sxiv. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <stdlib.h>
20#include <string.h>
21#include <stdio.h>
22#include <unistd.h>
23
24#include "options.h"
25#include "util.h"
26
27#define _IMAGE_CONFIG
28#include "config.h"
29
30options_t _options;
31const options_t *options = (const options_t*) &_options;
32
33void print_usage(void)
34{
35 printf("usage: sxiv [-abcfhioqrtvZ] [-e WID] [-G GAMMA] [-g GEOMETRY] "
36 "[-N NAME] [-n NUM] [-S DELAY] [-s MODE] [-z ZOOM] FILES...\n");
37}
38
39void print_version(void)
40{
41 printf("sxiv %s - Simple X Image Viewer\n", VERSION);
42}
43
44void parse_options(int argc, char **argv)
45{
46 int n, opt;
47 float f;
48 char *end, *s;
49 const char *scalemodes = "dfwh";
50
51 progname = strrchr(argv[0], '/');
52 progname = progname ? progname + 1 : argv[0];
53
54 _options.from_stdin = false;
55 _options.to_stdout = false;
56 _options.recursive = false;
57 _options.startnum = 0;
58
59 _options.scalemode = SCALE_DOWN;
60 _options.zoom = 1.0;
61 _options.animate = false;
62 _options.gamma = 0;
63 _options.slideshow = 0.0;
64
65 _options.fullscreen = false;
66 _options.embed = 0;
67 _options.hide_bar = false;
68 _options.geometry = NULL;
69 _options.res_name = NULL;
70
71 _options.quiet = false;
72 _options.thumb_mode = false;
73 _options.clean_cache = false;
74
75 while ((opt = getopt(argc, argv, "abce:fG:g:hin:N:oqrS:s:tvZz:")) != -1) {
76 switch (opt) {
77 case '?':
78 print_usage();
79 exit(EXIT_FAILURE);
80 case 'a':
81 _options.animate = true;
82 break;
83 case 'b':
84 _options.hide_bar = true;
85 break;
86 case 'c':
87 _options.clean_cache = true;
88 break;
89 case 'e':
90 n = strtol(optarg, &end, 0);
91 if (*end != '\0')
92 error(EXIT_FAILURE, 0, "Invalid argument for option -e: %s", optarg);
93 _options.embed = n;
94 break;
95 case 'f':
96 _options.fullscreen = true;
97 break;
98 case 'G':
99 n = strtol(optarg, &end, 0);
100 if (*end != '\0')
101 error(EXIT_FAILURE, 0, "Invalid argument for option -G: %s", optarg);
102 _options.gamma = n;
103 break;
104 case 'g':
105 _options.geometry = optarg;
106 break;
107 case 'h':
108 print_usage();
109 exit(EXIT_SUCCESS);
110 case 'i':
111 _options.from_stdin = true;
112 break;
113 case 'n':
114 n = strtol(optarg, &end, 0);
115 if (*end != '\0' || n <= 0)
116 error(EXIT_FAILURE, 0, "Invalid argument for option -n: %s", optarg);
117 _options.startnum = n - 1;
118 break;
119 case 'N':
120 _options.res_name = optarg;
121 break;
122 case 'o':
123 _options.to_stdout = true;
124 break;
125 case 'q':
126 _options.quiet = true;
127 break;
128 case 'r':
129 _options.recursive = true;
130 break;
131 case 'S':
132 f = (float) strtof(optarg, &end);
133 if (*end != '\0' || f <= 0.0)
134 error(EXIT_FAILURE, 0, "Invalid argument for option -S: %s", optarg);
135 _options.slideshow = (float) f;
136 break;
137 case 's':
138 s = strchr(scalemodes, optarg[0]);
139 if (s == NULL || *s == '\0' || strlen(optarg) != 1)
140 error(EXIT_FAILURE, 0, "Invalid argument for option -s: %s", optarg);
141 _options.scalemode = s - scalemodes;
142 break;
143 case 't':
144 _options.thumb_mode = true;
145 break;
146 case 'v':
147 print_version();
148 exit(EXIT_SUCCESS);
149 case 'Z':
150 _options.scalemode = SCALE_ZOOM;
151 _options.zoom = 1.0;
152 break;
153 case 'z':
154 n = strtol(optarg, &end, 0);
155 if (*end != '\0' || n <= 0)
156 error(EXIT_FAILURE, 0, "Invalid argument for option -z: %s", optarg);
157 _options.scalemode = SCALE_ZOOM;
158 _options.zoom = (float) n / 100.0;
159 break;
160 }
161 }
162
163 _options.filenames = argv + optind;
164 _options.filecnt = argc - optind;
165
166 if (_options.filecnt == 1 && STREQ(_options.filenames[0], "-")) {
167 _options.filenames++;
168 _options.filecnt--;
169 _options.from_stdin = true;
170 }
171}