Krolyxon
Commit 4a282da

Branches

Refs →

nsxivoptions.c

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