Krolyxon
Commit fbe186e

Branches

Refs →

nsxivoptions.c

247 lines · 7.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
23#include <assert.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <unistd.h>
28
29#define OPTPARSE_IMPLEMENTATION
30#define OPTPARSE_API static
31#pragma GCC diagnostic push /* also works on clang */
32#pragma GCC diagnostic ignored "-Wshadow"
33#pragma GCC diagnostic ignored "-Wunused-function"
34#include "optparse.h"
35#pragma GCC diagnostic pop
36
37const opt_t *options;
38
39void print_usage(void)
40{
41 printf("usage: nsxiv [-abcfhiopqrtvZ0] [-A FRAMERATE] [-e WID] [-G GAMMA] "
42 "[-g GEOMETRY] [-N NAME] [-n NUM] [-S DELAY] [-s MODE] "
43 "[-z ZOOM] FILES...\n");
44}
45
46static void title_deprecation_notice(void)
47{
48 error(EXIT_FAILURE, 0, "\n"
49 "################################################################\n"
50 "# DEPRECATION NOTICE #\n"
51 "################################################################\n"
52 "# `-T` option has been deprecated in favour of `win-title`. #\n"
53 "# Please read the `WINDOW TITLE` section of the manpage for #\n"
54 "# more info. #\n"
55 "################################################################"
56 );
57}
58
59static void print_version(void)
60{
61 puts("nsxiv " VERSION);
62}
63
64void parse_options(int argc, char **argv)
65{
66 static const struct optparse_long longopts[] = {
67 { "framerate", 'A', OPTPARSE_REQUIRED },
68 { "animate", 'a', OPTPARSE_NONE },
69 { "no-bar", 'b', OPTPARSE_NONE },
70 { "clean-cache", 'c', OPTPARSE_NONE },
71 { "embed", 'e', OPTPARSE_REQUIRED },
72 { "fullscreen", 'f', OPTPARSE_NONE },
73 { "gamma", 'G', OPTPARSE_REQUIRED },
74 { "geometry", 'g', OPTPARSE_REQUIRED },
75 { "help", 'h', OPTPARSE_NONE },
76 { "stdin", 'i', OPTPARSE_NONE },
77 { "class", 'N', OPTPARSE_REQUIRED },
78 { "start-at", 'n', OPTPARSE_REQUIRED },
79 { "stdout", 'o', OPTPARSE_NONE },
80 { "private", 'p', OPTPARSE_NONE },
81 { "quiet", 'q', OPTPARSE_NONE },
82 { "recursive", 'r', OPTPARSE_NONE },
83 { "ss-delay", 'S', OPTPARSE_REQUIRED },
84 { "scale-mode", 's', OPTPARSE_REQUIRED },
85 { NULL, 'T', OPTPARSE_REQUIRED },
86 { "thumbnail", 't', OPTPARSE_NONE },
87 { "version", 'v', OPTPARSE_NONE },
88 { "zoom-100", 'Z', OPTPARSE_NONE },
89 { "zoom", 'z', OPTPARSE_REQUIRED },
90 { "null", '0', OPTPARSE_NONE },
91 { 0 }, /* end */
92 };
93
94 int n, opt;
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.animate = false;
110 _options.gamma = 0;
111 _options.slideshow = 0;
112 _options.framerate = 0;
113
114 _options.fullscreen = false;
115 _options.embed = 0;
116 _options.hide_bar = false;
117 _options.geometry = NULL;
118 _options.res_name = NULL;
119
120 _options.quiet = false;
121 _options.thumb_mode = false;
122 _options.clean_cache = false;
123 _options.private_mode = false;
124
125 if (argc > 0) {
126 s = strrchr(argv[0], '/');
127 progname = s != NULL && s[1] != '\0' ? s + 1 : argv[0];
128 }
129
130 optparse_init(&op, argv);
131 while ((opt = optparse_long(&op, longopts, NULL)) != -1) {
132 for (n = 0; n < (int)ARRLEN(longopts); ++n) { /* clang-tidy finds some non-sensical branch and thinks optarg == NULL is possible */
133 if (opt == longopts[n].shortname && longopts[n].argtype == OPTPARSE_REQUIRED)
134 assert(op.optarg != NULL);
135 }
136 switch (opt) {
137 case '?':
138 fprintf(stderr, "%s\n", op.errmsg);
139 print_usage();
140 exit(EXIT_FAILURE);
141 case 'A':
142 n = strtol(op.optarg, &end, 0);
143 if (*end != '\0' || n <= 0)
144 error(EXIT_FAILURE, 0, "Invalid argument for option -A: %s", op.optarg);
145 _options.framerate = n;
146 /* fall through */
147 case 'a':
148 _options.animate = true;
149 break;
150 case 'b':
151 _options.hide_bar = true;
152 break;
153 case 'c':
154 _options.clean_cache = true;
155 break;
156 case 'e':
157 n = strtol(op.optarg, &end, 0);
158 if (*end != '\0')
159 error(EXIT_FAILURE, 0, "Invalid argument for option -e: %s", op.optarg);
160 _options.embed = n;
161 break;
162 case 'f':
163 _options.fullscreen = true;
164 break;
165 case 'G':
166 n = strtol(op.optarg, &end, 0);
167 if (*end != '\0')
168 error(EXIT_FAILURE, 0, "Invalid argument for option -G: %s", op.optarg);
169 _options.gamma = n;
170 break;
171 case 'g':
172 _options.geometry = op.optarg;
173 break;
174 case 'h':
175 print_usage();
176 exit(EXIT_SUCCESS);
177 case 'i':
178 _options.from_stdin = true;
179 break;
180 case 'n':
181 n = strtol(op.optarg, &end, 0);
182 if (*end != '\0' || n <= 0)
183 error(EXIT_FAILURE, 0, "Invalid argument for option -n: %s", op.optarg);
184 _options.startnum = n - 1;
185 break;
186 case 'N':
187 _options.res_name = op.optarg;
188 break;
189 case 'o':
190 _options.to_stdout = true;
191 break;
192 case 'p':
193 _options.private_mode = true;
194 break;
195 case 'q':
196 _options.quiet = true;
197 break;
198 case 'r':
199 _options.recursive = true;
200 break;
201 case 'S':
202 n = strtof(op.optarg, &end) * 10;
203 if (*end != '\0' || n <= 0)
204 error(EXIT_FAILURE, 0, "Invalid argument for option -S: %s", op.optarg);
205 _options.slideshow = n;
206 break;
207 case 's':
208 s = strchr(scalemodes, op.optarg[0]);
209 if (s == NULL || *s == '\0' || strlen(op.optarg) != 1)
210 error(EXIT_FAILURE, 0, "Invalid argument for option -s: %s", op.optarg);
211 _options.scalemode = s - scalemodes;
212 break;
213 case 'T':
214 title_deprecation_notice(); /* TODO(v31): remove this option */
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 argument for option -z: %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 }
237 }
238
239 _options.filenames = argv + op.optind;
240 _options.filecnt = argc - op.optind;
241
242 if (_options.filecnt == 1 && STREQ(_options.filenames[0], "-")) {
243 _options.filenames++;
244 _options.filecnt--;
245 _options.from_stdin = true;
246 }
247}