Krolyxon
Commit 6ffc64a

Branches

Refs →

nsxivoptions.c

273 lines · 8.0 KB · C

1/* Copyright 2011-2020 Bert Muennich
2 * Copyright 2021-2022 nsxiv contributors
3 *
4 * This file is a part of nsxiv.
5 *
6 * nsxiv is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; either version 2 of the License,
9 * or (at your option) any later version.
10 *
11 * nsxiv is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with nsxiv. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "nsxiv.h"
21#include "version.h"
22#define INCLUDE_OPTIONS_CONFIG
23#include "config.h"
24
25#include <assert.h>
26#include <limits.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <unistd.h>
31
32#define OPTPARSE_IMPLEMENTATION
33#define OPTPARSE_API static
34#pragma GCC diagnostic push /* also works on clang */
35#pragma GCC diagnostic ignored "-Wshadow"
36#pragma GCC diagnostic ignored "-Wunused-function"
37#include "optparse.h"
38#pragma GCC diagnostic pop
39
40const opt_t *options;
41
42void print_usage(void)
43{
44 printf("usage: %s [-abcfhiopqrtvZ0] [-A FRAMERATE] [-e WID] [-G GAMMA] "
45 "[-g GEOMETRY] [-N NAME] [-n NUM] [-S DELAY] [-s MODE] "
46 "[-z ZOOM] FILES...\n", progname);
47}
48
49static void title_deprecation_notice(void)
50{
51 error(EXIT_FAILURE, 0, "\n"
52 "################################################################\n"
53 "# DEPRECATION NOTICE #\n"
54 "################################################################\n"
55 "# `-T` option has been deprecated in favour of `win-title`. #\n"
56 "# Please read the `WINDOW TITLE` section of the manpage for #\n"
57 "# more info. #\n"
58 "################################################################"
59 );
60}
61
62static void print_version(void)
63{
64 printf("%s %s\n", progname, VERSION);
65}
66
67void parse_options(int argc, char **argv)
68{
69 enum {
70 /* ensure these can't be represented in a single byte in order
71 * to avoid conflicts with short opts
72 */
73 OPT_START = UCHAR_MAX,
74 OPT_AA,
75 OPT_AL
76 };
77 static const struct optparse_long longopts[] = {
78 { "framerate", 'A', OPTPARSE_REQUIRED },
79 { "animate", 'a', OPTPARSE_NONE },
80 { "no-bar", 'b', OPTPARSE_NONE },
81 { "clean-cache", 'c', OPTPARSE_NONE },
82 { "embed", 'e', OPTPARSE_REQUIRED },
83 { "fullscreen", 'f', OPTPARSE_NONE },
84 { "gamma", 'G', OPTPARSE_REQUIRED },
85 { "geometry", 'g', OPTPARSE_REQUIRED },
86 { "help", 'h', OPTPARSE_NONE },
87 { "stdin", 'i', OPTPARSE_NONE },
88 { "class", 'N', OPTPARSE_REQUIRED },
89 { "start-at", 'n', OPTPARSE_REQUIRED },
90 { "stdout", 'o', OPTPARSE_NONE },
91 { "private", 'p', OPTPARSE_NONE },
92 { "quiet", 'q', OPTPARSE_NONE },
93 { "recursive", 'r', OPTPARSE_NONE },
94 { "ss-delay", 'S', OPTPARSE_REQUIRED },
95 { "scale-mode", 's', OPTPARSE_REQUIRED },
96 { NULL, 'T', OPTPARSE_REQUIRED },
97 { "thumbnail", 't', OPTPARSE_NONE },
98 { "version", 'v', OPTPARSE_NONE },
99 { "zoom-100", 'Z', OPTPARSE_NONE },
100 { "zoom", 'z', OPTPARSE_REQUIRED },
101 { "null", '0', OPTPARSE_NONE },
102 { "anti-alias", OPT_AA, OPTPARSE_OPTIONAL },
103 { "alpha-layer", OPT_AL, OPTPARSE_OPTIONAL },
104 { 0 }, /* end */
105 };
106
107 long n, opt;
108 float f;
109 char *end, *s;
110 struct optparse op;
111 const char scalemodes[] = "dfFwh"; /* must be sorted according to scalemode_t */
112 static opt_t _options;
113
114 options = &_options;
115 _options.from_stdin = false;
116 _options.to_stdout = false;
117 _options.using_null = false;
118 _options.recursive = false;
119 _options.startnum = 0;
120
121 _options.scalemode = SCALE_DOWN;
122 _options.zoom = 1.0;
123 _options.anti_alias = ANTI_ALIAS;
124 _options.alpha_layer = ALPHA_LAYER;
125 _options.animate = false;
126 _options.gamma = 0;
127 _options.slideshow = 0;
128 _options.framerate = 0;
129
130 _options.fullscreen = false;
131 _options.embed = 0;
132 _options.hide_bar = false;
133 _options.geometry = NULL;
134 _options.res_name = NULL;
135
136 _options.quiet = false;
137 _options.thumb_mode = false;
138 _options.clean_cache = false;
139 _options.private_mode = false;
140
141 if (argc > 0) {
142 s = strrchr(argv[0], '/');
143 progname = s != NULL && s[1] != '\0' ? s + 1 : argv[0];
144 }
145
146 optparse_init(&op, argv);
147 while ((opt = optparse_long(&op, longopts, NULL)) != -1) {
148 for (n = 0; n < (int)ARRLEN(longopts); ++n) { /* clang-tidy finds some non-sensical branch and thinks optarg == NULL is possible */
149 if (opt == longopts[n].shortname && longopts[n].argtype == OPTPARSE_REQUIRED)
150 assert(op.optarg != NULL);
151 }
152 switch (opt) {
153 case '?':
154 fprintf(stderr, "%s\n", op.errmsg);
155 print_usage();
156 exit(EXIT_FAILURE);
157 case 'A':
158 n = strtol(op.optarg, &end, 0);
159 if (*end != '\0' || n <= 0 || n > INT_MAX)
160 error(EXIT_FAILURE, 0, "Invalid framerate: %s", op.optarg);
161 _options.framerate = n;
162 /* fall through */
163 case 'a':
164 _options.animate = true;
165 break;
166 case 'b':
167 _options.hide_bar = true;
168 break;
169 case 'c':
170 _options.clean_cache = true;
171 break;
172 case 'e':
173 n = strtol(op.optarg, &end, 0);
174 if (*end != '\0')
175 error(EXIT_FAILURE, 0, "Invalid window id: %s", op.optarg);
176 _options.embed = n;
177 break;
178 case 'f':
179 _options.fullscreen = true;
180 break;
181 case 'G':
182 n = strtol(op.optarg, &end, 0);
183 if (*end != '\0' || n < INT_MIN || n > INT_MAX)
184 error(EXIT_FAILURE, 0, "Invalid gamma: %s", op.optarg);
185 _options.gamma = n;
186 break;
187 case 'g':
188 _options.geometry = op.optarg;
189 break;
190 case 'h':
191 print_usage();
192 exit(EXIT_SUCCESS);
193 case 'i':
194 _options.from_stdin = true;
195 break;
196 case 'n':
197 n = strtol(op.optarg, &end, 0);
198 if (*end != '\0' || n <= 0 || n > INT_MAX)
199 error(EXIT_FAILURE, 0, "Invalid starting number: %s", op.optarg);
200 _options.startnum = n - 1;
201 break;
202 case 'N':
203 _options.res_name = op.optarg;
204 break;
205 case 'o':
206 _options.to_stdout = true;
207 break;
208 case 'p':
209 _options.private_mode = true;
210 break;
211 case 'q':
212 _options.quiet = true;
213 break;
214 case 'r':
215 _options.recursive = true;
216 break;
217 case 'S':
218 f = strtof(op.optarg, &end) * 10.0f;
219 if (*end != '\0' || f <= 0 || f >= (float)UINT_MAX)
220 error(EXIT_FAILURE, 0, "Invalid slideshow delay: %s", op.optarg);
221 _options.slideshow = (unsigned int)f;
222 break;
223 case 's':
224 s = strchr(scalemodes, op.optarg[0]);
225 if (s == NULL || *s == '\0' || strlen(op.optarg) != 1)
226 error(EXIT_FAILURE, 0, "Invalid scale mode: %s", op.optarg);
227 _options.scalemode = s - scalemodes;
228 break;
229 case 'T':
230 title_deprecation_notice(); /* TODO(v31): remove this option */
231 break;
232 case 't':
233 _options.thumb_mode = true;
234 break;
235 case 'v':
236 print_version();
237 exit(EXIT_SUCCESS);
238 case 'Z':
239 _options.scalemode = SCALE_ZOOM;
240 _options.zoom = 1.0f;
241 break;
242 case 'z':
243 n = strtol(op.optarg, &end, 0);
244 if (*end != '\0' || n <= 0)
245 error(EXIT_FAILURE, 0, "Invalid zoom level: %s", op.optarg);
246 _options.scalemode = SCALE_ZOOM;
247 _options.zoom = (float) n / 100.0f;
248 break;
249 case '0':
250 _options.using_null = true;
251 break;
252 case OPT_AA:
253 if (op.optarg != NULL && !STREQ(op.optarg, "no"))
254 error(EXIT_FAILURE, 0, "Invalid argument for option --anti-alias: %s", op.optarg);
255 _options.anti_alias = op.optarg == NULL;
256 break;
257 case OPT_AL:
258 if (op.optarg != NULL && !STREQ(op.optarg, "no"))
259 error(EXIT_FAILURE, 0, "Invalid argument for option --alpha-layer: %s", op.optarg);
260 _options.alpha_layer = op.optarg == NULL;
261 break;
262 }
263 }
264
265 _options.filenames = argv + op.optind;
266 _options.filecnt = argc - op.optind;
267
268 if (_options.filecnt == 1 && STREQ(_options.filenames[0], "-")) {
269 _options.filenames++;
270 _options.filecnt--;
271 _options.from_stdin = true;
272 }
273}