Krolyxon
Commit 0b758e0

Branches

Refs →

nsxivoptions.c

170 lines · 4.2 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] [-G GAMMA] [-g GEOMETRY] [-n NUM] "
36 "[-N NAME] [-S DELAY] [-s MODE] [-z ZOOM] [-w WID] FILES...\n");
37}
38
39void print_version(void)
40{
41 printf("sxiv %s - Simple X Image Viewer\n", VERSION);
42}
43
44void parse_options(int argc, char **argv)
45{
46 int n, opt;
47 char *end, *s;
48 const char *scalemodes = "dfwh";
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
64 _options.fullscreen = false;
65 _options.embed = 0;
66 _options.hide_bar = false;
67 _options.geometry = NULL;
68 _options.res_name = NULL;
69
70 _options.quiet = false;
71 _options.thumb_mode = false;
72 _options.clean_cache = false;
73
74 while ((opt = getopt(argc, argv, "abcfG:g:hin:N:oqrS:s:tvw:Zz:")) != -1) {
75 switch (opt) {
76 case '?':
77 print_usage();
78 exit(EXIT_FAILURE);
79 case 'a':
80 _options.animate = true;
81 break;
82 case 'b':
83 _options.hide_bar = true;
84 break;
85 case 'c':
86 _options.clean_cache = true;
87 break;
88 case 'f':
89 _options.fullscreen = true;
90 break;
91 case 'G':
92 n = strtol(optarg, &end, 0);
93 if (*end != '\0')
94 error(EXIT_FAILURE, 0, "Invalid argument for option -G: %s", optarg);
95 _options.gamma = n;
96 break;
97 case 'g':
98 _options.geometry = optarg;
99 break;
100 case 'h':
101 print_usage();
102 exit(EXIT_SUCCESS);
103 case 'i':
104 _options.from_stdin = true;
105 break;
106 case 'n':
107 n = strtol(optarg, &end, 0);
108 if (*end != '\0' || n <= 0)
109 error(EXIT_FAILURE, 0, "Invalid argument for option -n: %s", optarg);
110 _options.startnum = n - 1;
111 break;
112 case 'N':
113 _options.res_name = optarg;
114 break;
115 case 'o':
116 _options.to_stdout = true;
117 break;
118 case 'q':
119 _options.quiet = true;
120 break;
121 case 'r':
122 _options.recursive = true;
123 break;
124 case 'S':
125 n = strtol(optarg, &end, 0);
126 if (*end != '\0' || n <= 0)
127 error(EXIT_FAILURE, 0, "Invalid argument for option -S: %s", optarg);
128 _options.slideshow = n;
129 break;
130 case 's':
131 s = strchr(scalemodes, optarg[0]);
132 if (s == NULL || *s == '\0' || strlen(optarg) != 1)
133 error(EXIT_FAILURE, 0, "Invalid argument for option -s: %s", optarg);
134 _options.scalemode = s - scalemodes;
135 break;
136 case 't':
137 _options.thumb_mode = true;
138 break;
139 case 'v':
140 print_version();
141 exit(EXIT_SUCCESS);
142 case 'w':
143 n = strtol(optarg, &end, 0);
144 if (*end != '\0')
145 error(EXIT_FAILURE, 0, "Invalid argument for option -w: %s", optarg);
146 _options.embed = n;
147 break;
148 case 'Z':
149 _options.scalemode = SCALE_ZOOM;
150 _options.zoom = 1.0;
151 break;
152 case 'z':
153 n = strtol(optarg, &end, 0);
154 if (*end != '\0' || n <= 0)
155 error(EXIT_FAILURE, 0, "Invalid argument for option -z: %s", optarg);
156 _options.scalemode = SCALE_ZOOM;
157 _options.zoom = (float) n / 100.0;
158 break;
159 }
160 }
161
162 _options.filenames = argv + optind;
163 _options.filecnt = argc - optind;
164
165 if (_options.filecnt == 1 && STREQ(_options.filenames[0], "-")) {
166 _options.filenames++;
167 _options.filecnt--;
168 _options.from_stdin = true;
169 }
170}