Krolyxon
Commit b8fd923

Branches

Refs →

nsxivoptions.c

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