Krolyxon
Commit 5fb5d44

Branches

Refs →

nsxivoptions.c

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