Krolyxon
Commit f37fa09

Branches

Refs →

nsxivoptions.c

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