Krolyxon
Commit 590d9fe

Branches

Refs →

nsxivoptions.c

200 lines · 5.2 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 <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26
27const opt_t *options;
28
29void print_usage(void)
30{
31 printf("usage: nsxiv [-abcfhiopqrtvZ0] [-A FRAMERATE] [-e WID] [-G GAMMA] "
32 "[-g GEOMETRY] [-N NAME] [-n NUM] [-S DELAY] [-s MODE] "
33 "[-z ZOOM] FILES...\n");
34}
35
36static void title_deprecation_notice(void)
37{
38 error(EXIT_FAILURE, 0, "\n"
39 "################################################################\n"
40 "# DEPRECATION NOTICE #\n"
41 "################################################################\n"
42 "# `-T` option has been deprecated in favour of `win-title`. #\n"
43 "# Please read the `WINDOW TITLE` section of the manpage for #\n"
44 "# more info. #\n"
45 "################################################################"
46 );
47}
48
49static void print_version(void)
50{
51 puts("nsxiv " VERSION);
52}
53
54void parse_options(int argc, char **argv)
55{
56 int n, opt;
57 char *end, *s;
58 const char *scalemodes = "dfFwh";
59 static opt_t _options;
60 options = &_options;
61
62 progname = strrchr(argv[0], '/');
63 progname = progname ? progname + 1 : argv[0];
64
65 _options.from_stdin = false;
66 _options.to_stdout = false;
67 _options.using_null = false;
68 _options.recursive = false;
69 _options.startnum = 0;
70
71 _options.scalemode = SCALE_DOWN;
72 _options.zoom = 1.0;
73 _options.animate = false;
74 _options.gamma = 0;
75 _options.slideshow = 0;
76 _options.framerate = 0;
77
78 _options.fullscreen = false;
79 _options.embed = 0;
80 _options.hide_bar = false;
81 _options.geometry = NULL;
82 _options.res_name = NULL;
83
84 _options.quiet = false;
85 _options.thumb_mode = false;
86 _options.clean_cache = false;
87 _options.private_mode = false;
88
89 while ((opt = getopt(argc, argv, "A:abce:fG:g:hin:N:opqrS:s:T:tvZz:0")) != -1) {
90 switch (opt) {
91 case '?':
92 print_usage();
93 exit(EXIT_FAILURE);
94 case 'A':
95 n = strtol(optarg, &end, 0);
96 if (*end != '\0' || n <= 0)
97 error(EXIT_FAILURE, 0, "Invalid argument for option -A: %s", optarg);
98 _options.framerate = n;
99 /* fall through */
100 case 'a':
101 _options.animate = true;
102 break;
103 case 'b':
104 _options.hide_bar = true;
105 break;
106 case 'c':
107 _options.clean_cache = true;
108 break;
109 case 'e':
110 n = strtol(optarg, &end, 0);
111 if (*end != '\0')
112 error(EXIT_FAILURE, 0, "Invalid argument for option -e: %s", optarg);
113 _options.embed = n;
114 break;
115 case 'f':
116 _options.fullscreen = true;
117 break;
118 case 'G':
119 n = strtol(optarg, &end, 0);
120 if (*end != '\0')
121 error(EXIT_FAILURE, 0, "Invalid argument for option -G: %s", optarg);
122 _options.gamma = n;
123 break;
124 case 'g':
125 _options.geometry = optarg;
126 break;
127 case 'h':
128 print_usage();
129 exit(EXIT_SUCCESS);
130 case 'i':
131 _options.from_stdin = true;
132 break;
133 case 'n':
134 n = strtol(optarg, &end, 0);
135 if (*end != '\0' || n <= 0)
136 error(EXIT_FAILURE, 0, "Invalid argument for option -n: %s", optarg);
137 _options.startnum = n - 1;
138 break;
139 case 'N':
140 _options.res_name = optarg;
141 break;
142 case 'o':
143 _options.to_stdout = true;
144 break;
145 case 'p':
146 _options.private_mode = true;
147 break;
148 case 'q':
149 _options.quiet = true;
150 break;
151 case 'r':
152 _options.recursive = true;
153 break;
154 case 'S':
155 n = strtof(optarg, &end) * 10;
156 if (*end != '\0' || n <= 0)
157 error(EXIT_FAILURE, 0, "Invalid argument for option -S: %s", optarg);
158 _options.slideshow = n;
159 break;
160 case 's':
161 s = strchr(scalemodes, optarg[0]);
162 if (s == NULL || *s == '\0' || strlen(optarg) != 1)
163 error(EXIT_FAILURE, 0, "Invalid argument for option -s: %s", optarg);
164 _options.scalemode = s - scalemodes;
165 break;
166 case 'T':
167 title_deprecation_notice(); /* TODO(v30): remove this option */
168 break;
169 case 't':
170 _options.thumb_mode = true;
171 break;
172 case 'v':
173 print_version();
174 exit(EXIT_SUCCESS);
175 case 'Z':
176 _options.scalemode = SCALE_ZOOM;
177 _options.zoom = 1.0;
178 break;
179 case 'z':
180 n = strtol(optarg, &end, 0);
181 if (*end != '\0' || n <= 0)
182 error(EXIT_FAILURE, 0, "Invalid argument for option -z: %s", optarg);
183 _options.scalemode = SCALE_ZOOM;
184 _options.zoom = (float) n / 100.0;
185 break;
186 case '0':
187 _options.using_null = true;
188 break;
189 }
190 }
191
192 _options.filenames = argv + optind;
193 _options.filecnt = argc - optind;
194
195 if (_options.filecnt == 1 && STREQ(_options.filenames[0], "-")) {
196 _options.filenames++;
197 _options.filecnt--;
198 _options.from_stdin = true;
199 }
200}