Krolyxon
Commit 63972db

Branches

Refs →

nsxivoptions.c

198 lines · 5.0 KB · C

1/* Copyright 2011 Bert Muennich
2 *
3 * This file is a part of nsxiv.
4 *
5 * nsxiv is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published
7 * by the Free Software Foundation; either version 2 of the License,
8 * or (at your option) any later version.
9 *
10 * nsxiv is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with nsxiv. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "nsxiv.h"
20#define _IMAGE_CONFIG
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 [-abcfhiopqrtvZ] [-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.stdout_separator = '\n';
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:0opqrS:s:T:tvZz:")) != -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 '0':
133 _options.stdout_separator = '\0';
134 /* -0 implies -o */
135 /* fall through */
136 case 'o':
137 _options.to_stdout = true;
138 break;
139 case 'p':
140 _options.private_mode = true;
141 break;
142 case 'q':
143 _options.quiet = true;
144 break;
145 case 'r':
146 _options.recursive = true;
147 break;
148 case 'S':
149 n = strtof(optarg, &end) * 10;
150 if (*end != '\0' || n <= 0)
151 error(EXIT_FAILURE, 0, "Invalid argument for option -S: %s", optarg);
152 _options.slideshow = n;
153 break;
154 case 's':
155 s = strchr(scalemodes, optarg[0]);
156 if (s == NULL || *s == '\0' || strlen(optarg) != 1)
157 error(EXIT_FAILURE, 0, "Invalid argument for option -s: %s", optarg);
158 _options.scalemode = s - scalemodes;
159 break;
160 case 'T':
161 if ((s = strrchr(optarg, ':')) != NULL) {
162 *s = '\0';
163 n = strtol(++s, &end, 0);
164 if (*end != '\0' || n < SUFFIX_EMPTY || n > SUFFIX_FULLPATH)
165 error(EXIT_FAILURE, 0, "Invalid argument for option -T suffixmode: %s", s);
166 _options.title_suffixmode = n;
167 }
168 _options.title_prefix = optarg;
169 break;
170 case 't':
171 _options.thumb_mode = true;
172 break;
173 case 'v':
174 print_version();
175 exit(EXIT_SUCCESS);
176 case 'Z':
177 _options.scalemode = SCALE_ZOOM;
178 _options.zoom = 1.0;
179 break;
180 case 'z':
181 n = strtol(optarg, &end, 0);
182 if (*end != '\0' || n <= 0)
183 error(EXIT_FAILURE, 0, "Invalid argument for option -z: %s", optarg);
184 _options.scalemode = SCALE_ZOOM;
185 _options.zoom = (float) n / 100.0;
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}