Krolyxon
Commit 4df97db

Branches

Refs →

nsxivoptions.c

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