Krolyxon
Commit 68ddbe2

Branches

Refs →

nsxivoptions.c

198 lines · 5.0 KB · C

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