Krolyxon
Commit d5c5708

Branches

Refs →

nsxivoptions.c

179 lines · 4.4 KB · C

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