Krolyxon
Commit ba39006

Branches

Refs →

nsxivoptions.c

245 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 progname = strrchr(argv[0], '/');
102 progname = progname ? progname + 1 : argv[0];
103
104 _options.from_stdin = false;
105 _options.to_stdout = false;
106 _options.using_null = false;
107 _options.recursive = false;
108 _options.startnum = 0;
109
110 _options.scalemode = SCALE_DOWN;
111 _options.zoom = 1.0;
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 optparse_init(&op, argv);
129 while ((opt = optparse_long(&op, longopts, NULL)) != -1) {
130 for (n = 0; n < (int)ARRLEN(longopts); ++n) { /* clang-tidy finds some non-sensical branch and thinks optarg == NULL is possible */
131 if (opt == longopts[n].shortname && longopts[n].argtype == OPTPARSE_REQUIRED)
132 assert(op.optarg != NULL);
133 }
134 switch (opt) {
135 case '?':
136 fprintf(stderr, "%s\n", op.errmsg);
137 print_usage();
138 exit(EXIT_FAILURE);
139 case 'A':
140 n = strtol(op.optarg, &end, 0);
141 if (*end != '\0' || n <= 0)
142 error(EXIT_FAILURE, 0, "Invalid argument for option -A: %s", op.optarg);
143 _options.framerate = n;
144 /* fall through */
145 case 'a':
146 _options.animate = true;
147 break;
148 case 'b':
149 _options.hide_bar = true;
150 break;
151 case 'c':
152 _options.clean_cache = true;
153 break;
154 case 'e':
155 n = strtol(op.optarg, &end, 0);
156 if (*end != '\0')
157 error(EXIT_FAILURE, 0, "Invalid argument for option -e: %s", op.optarg);
158 _options.embed = n;
159 break;
160 case 'f':
161 _options.fullscreen = true;
162 break;
163 case 'G':
164 n = strtol(op.optarg, &end, 0);
165 if (*end != '\0')
166 error(EXIT_FAILURE, 0, "Invalid argument for option -G: %s", op.optarg);
167 _options.gamma = n;
168 break;
169 case 'g':
170 _options.geometry = op.optarg;
171 break;
172 case 'h':
173 print_usage();
174 exit(EXIT_SUCCESS);
175 case 'i':
176 _options.from_stdin = true;
177 break;
178 case 'n':
179 n = strtol(op.optarg, &end, 0);
180 if (*end != '\0' || n <= 0)
181 error(EXIT_FAILURE, 0, "Invalid argument for option -n: %s", op.optarg);
182 _options.startnum = n - 1;
183 break;
184 case 'N':
185 _options.res_name = op.optarg;
186 break;
187 case 'o':
188 _options.to_stdout = true;
189 break;
190 case 'p':
191 _options.private_mode = true;
192 break;
193 case 'q':
194 _options.quiet = true;
195 break;
196 case 'r':
197 _options.recursive = true;
198 break;
199 case 'S':
200 n = strtof(op.optarg, &end) * 10;
201 if (*end != '\0' || n <= 0)
202 error(EXIT_FAILURE, 0, "Invalid argument for option -S: %s", op.optarg);
203 _options.slideshow = n;
204 break;
205 case 's':
206 s = strchr(scalemodes, op.optarg[0]);
207 if (s == NULL || *s == '\0' || strlen(op.optarg) != 1)
208 error(EXIT_FAILURE, 0, "Invalid argument for option -s: %s", op.optarg);
209 _options.scalemode = s - scalemodes;
210 break;
211 case 'T':
212 title_deprecation_notice(); /* TODO(v31): remove this option */
213 break;
214 case 't':
215 _options.thumb_mode = true;
216 break;
217 case 'v':
218 print_version();
219 exit(EXIT_SUCCESS);
220 case 'Z':
221 _options.scalemode = SCALE_ZOOM;
222 _options.zoom = 1.0f;
223 break;
224 case 'z':
225 n = strtol(op.optarg, &end, 0);
226 if (*end != '\0' || n <= 0)
227 error(EXIT_FAILURE, 0, "Invalid argument for option -z: %s", op.optarg);
228 _options.scalemode = SCALE_ZOOM;
229 _options.zoom = (float) n / 100.0f;
230 break;
231 case '0':
232 _options.using_null = true;
233 break;
234 }
235 }
236
237 _options.filenames = argv + op.optind;
238 _options.filecnt = argc - op.optind;
239
240 if (_options.filecnt == 1 && STREQ(_options.filenames[0], "-")) {
241 _options.filenames++;
242 _options.filecnt--;
243 _options.from_stdin = true;
244 }
245}