Krolyxon
Commit daee344

Branches

Refs →

nsxivoptions.c

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