Krolyxon
Commit 12d635b

Branches

Refs →

nsxivoptions.c

256 lines · 7.2 KB · C

1/* Copyright 2011-2020 Bert Muennich
2 * Copyright 2021-2023 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 print_version(void)
50{
51 printf("%s %s\n", progname, VERSION);
52}
53
54void parse_options(int argc, char **argv)
55{
56 enum {
57 /* ensure these can't be represented in a single byte in order
58 * to avoid conflicts with short opts
59 */
60 OPT_START = UCHAR_MAX,
61 OPT_AA,
62 OPT_AL
63 };
64 static const struct optparse_long longopts[] = {
65 { "framerate", 'A', OPTPARSE_REQUIRED },
66 { "animate", 'a', OPTPARSE_NONE },
67 { "no-bar", 'b', OPTPARSE_NONE },
68 { "clean-cache", 'c', OPTPARSE_NONE },
69 { "embed", 'e', OPTPARSE_REQUIRED },
70 { "fullscreen", 'f', OPTPARSE_NONE },
71 { "gamma", 'G', OPTPARSE_REQUIRED },
72 { "geometry", 'g', OPTPARSE_REQUIRED },
73 { "help", 'h', OPTPARSE_NONE },
74 { "stdin", 'i', OPTPARSE_NONE },
75 { "class", 'N', OPTPARSE_REQUIRED },
76 { "start-at", 'n', OPTPARSE_REQUIRED },
77 { "stdout", 'o', OPTPARSE_NONE },
78 { "private", 'p', OPTPARSE_NONE },
79 { "quiet", 'q', OPTPARSE_NONE },
80 { "recursive", 'r', OPTPARSE_NONE },
81 { "ss-delay", 'S', OPTPARSE_REQUIRED },
82 { "scale-mode", 's', OPTPARSE_REQUIRED },
83 { "thumbnail", 't', OPTPARSE_NONE },
84 { "version", 'v', OPTPARSE_NONE },
85 { "zoom-100", 'Z', OPTPARSE_NONE },
86 { "zoom", 'z', OPTPARSE_REQUIRED },
87 { "null", '0', OPTPARSE_NONE },
88 { "anti-alias", OPT_AA, OPTPARSE_OPTIONAL },
89 { "alpha-layer", OPT_AL, OPTPARSE_OPTIONAL },
90 { 0 }, /* end */
91 };
92
93 long n, opt;
94 float f;
95 char *end, *s;
96 struct optparse op;
97 const char scalemodes[] = "dfFwh"; /* must be sorted according to scalemode_t */
98 static opt_t _options;
99
100 options = &_options;
101 _options.from_stdin = false;
102 _options.to_stdout = false;
103 _options.using_null = false;
104 _options.recursive = false;
105 _options.startnum = 0;
106
107 _options.scalemode = SCALE_DOWN;
108 _options.zoom = 1.0;
109 _options.anti_alias = ANTI_ALIAS;
110 _options.alpha_layer = ALPHA_LAYER;
111 _options.animate = false;
112 _options.gamma = 0;
113 _options.slideshow = 0;
114 _options.framerate = 0;
115
116 _options.fullscreen = false;
117 _options.embed = 0;
118 _options.hide_bar = false;
119 _options.geometry = NULL;
120 _options.res_name = NULL;
121
122 _options.quiet = false;
123 _options.thumb_mode = false;
124 _options.clean_cache = false;
125 _options.private_mode = false;
126
127 if (argc > 0) {
128 s = strrchr(argv[0], '/');
129 progname = s != NULL && s[1] != '\0' ? s + 1 : argv[0];
130 }
131
132 optparse_init(&op, argv);
133 while ((opt = optparse_long(&op, longopts, NULL)) != -1) {
134 for (n = 0; n < (int)ARRLEN(longopts); ++n) { /* clang-tidy finds some non-sensical branch and thinks optarg == NULL is possible */
135 if (opt == longopts[n].shortname && longopts[n].argtype == OPTPARSE_REQUIRED)
136 assert(op.optarg != NULL);
137 }
138 switch (opt) {
139 case '?':
140 fprintf(stderr, "%s\n", op.errmsg);
141 print_usage();
142 exit(EXIT_FAILURE);
143 case 'A':
144 n = strtol(op.optarg, &end, 0);
145 if (*end != '\0' || n <= 0 || n > INT_MAX)
146 error(EXIT_FAILURE, 0, "Invalid framerate: %s", op.optarg);
147 _options.framerate = n;
148 /* fall through */
149 case 'a':
150 _options.animate = true;
151 break;
152 case 'b':
153 _options.hide_bar = true;
154 break;
155 case 'c':
156 _options.clean_cache = true;
157 break;
158 case 'e':
159 n = strtol(op.optarg, &end, 0);
160 if (*end != '\0')
161 error(EXIT_FAILURE, 0, "Invalid window id: %s", op.optarg);
162 _options.embed = n;
163 break;
164 case 'f':
165 _options.fullscreen = true;
166 break;
167 case 'G':
168 n = strtol(op.optarg, &end, 0);
169 if (*end != '\0' || n < INT_MIN || n > INT_MAX)
170 error(EXIT_FAILURE, 0, "Invalid gamma: %s", op.optarg);
171 _options.gamma = n;
172 break;
173 case 'g':
174 _options.geometry = op.optarg;
175 break;
176 case 'h':
177 print_usage();
178 exit(EXIT_SUCCESS);
179 case 'i':
180 _options.from_stdin = true;
181 break;
182 case 'n':
183 n = strtol(op.optarg, &end, 0);
184 if (*end != '\0' || n <= 0 || n > INT_MAX)
185 error(EXIT_FAILURE, 0, "Invalid starting number: %s", op.optarg);
186 _options.startnum = n - 1;
187 break;
188 case 'N':
189 _options.res_name = op.optarg;
190 break;
191 case 'o':
192 _options.to_stdout = true;
193 break;
194 case 'p':
195 _options.private_mode = true;
196 break;
197 case 'q':
198 _options.quiet = true;
199 break;
200 case 'r':
201 _options.recursive = true;
202 break;
203 case 'S':
204 f = strtof(op.optarg, &end) * 10.0f;
205 if (*end != '\0' || f <= 0 || f >= (float)UINT_MAX)
206 error(EXIT_FAILURE, 0, "Invalid slideshow delay: %s", op.optarg);
207 _options.slideshow = (unsigned int)f;
208 break;
209 case 's':
210 s = strchr(scalemodes, op.optarg[0]);
211 if (s == NULL || *s == '\0' || strlen(op.optarg) != 1)
212 error(EXIT_FAILURE, 0, "Invalid scale mode: %s", op.optarg);
213 _options.scalemode = s - scalemodes;
214 break;
215 case 't':
216 _options.thumb_mode = true;
217 break;
218 case 'v':
219 print_version();
220 exit(EXIT_SUCCESS);
221 case 'Z':
222 _options.scalemode = SCALE_ZOOM;
223 _options.zoom = 1.0f;
224 break;
225 case 'z':
226 n = strtol(op.optarg, &end, 0);
227 if (*end != '\0' || n <= 0)
228 error(EXIT_FAILURE, 0, "Invalid zoom level: %s", op.optarg);
229 _options.scalemode = SCALE_ZOOM;
230 _options.zoom = (float) n / 100.0f;
231 break;
232 case '0':
233 _options.using_null = true;
234 break;
235 case OPT_AA:
236 if (op.optarg != NULL && !STREQ(op.optarg, "no"))
237 error(EXIT_FAILURE, 0, "Invalid argument for option --anti-alias: %s", op.optarg);
238 _options.anti_alias = op.optarg == NULL;
239 break;
240 case OPT_AL:
241 if (op.optarg != NULL && !STREQ(op.optarg, "no"))
242 error(EXIT_FAILURE, 0, "Invalid argument for option --alpha-layer: %s", op.optarg);
243 _options.alpha_layer = op.optarg == NULL;
244 break;
245 }
246 }
247
248 _options.filenames = argv + op.optind;
249 _options.filecnt = argc - op.optind;
250
251 if (_options.filecnt == 1 && STREQ(_options.filenames[0], "-")) {
252 _options.filenames++;
253 _options.filecnt--;
254 _options.from_stdin = true;
255 }
256}