Krolyxon
Commit 9a7e97c

Branches

Refs →

nsxivoptions.c

170 lines · 4.0 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] 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 _options.from_stdin = false;
51 _options.to_stdout = false;
52 _options.recursive = false;
53 _options.startnum = 0;
54
55 _options.scalemode = SCALE_DOWN;
56 _options.zoom = 1.0;
57 _options.animate = false;
58 _options.gamma = 0;
59 _options.slideshow = 0;
60
61 _options.fullscreen = false;
62 _options.hide_bar = false;
63 _options.geometry = NULL;
64 _options.res_name = NULL;
65
66 _options.quiet = false;
67 _options.thumb_mode = false;
68 _options.clean_cache = false;
69
70 while ((opt = getopt(argc, argv, "abcfG:g:hin:N:oqrS:s:tvZz:")) != -1) {
71 switch (opt) {
72 case '?':
73 print_usage();
74 exit(EXIT_FAILURE);
75 case 'a':
76 _options.animate = true;
77 break;
78 case 'b':
79 _options.hide_bar = true;
80 break;
81 case 'c':
82 _options.clean_cache = true;
83 break;
84 case 'f':
85 _options.fullscreen = true;
86 break;
87 case 'G':
88 n = strtol(optarg, &end, 0);
89 if (*end != '\0') {
90 fprintf(stderr, "sxiv: invalid argument for option -G: %s\n", optarg);
91 exit(EXIT_FAILURE);
92 }
93 _options.gamma = n;
94 break;
95 case 'g':
96 _options.geometry = optarg;
97 break;
98 case 'h':
99 print_usage();
100 exit(EXIT_SUCCESS);
101 case 'i':
102 _options.from_stdin = true;
103 break;
104 case 'n':
105 n = strtol(optarg, &end, 0);
106 if (*end != '\0' || n <= 0) {
107 fprintf(stderr, "sxiv: invalid argument for option -n: %s\n", optarg);
108 exit(EXIT_FAILURE);
109 }
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 fprintf(stderr, "sxiv: invalid argument for option -S: %s\n", optarg);
128 exit(EXIT_FAILURE);
129 }
130 _options.slideshow = n;
131 break;
132 case 's':
133 s = strchr(scalemodes, optarg[0]);
134 if (s == NULL || *s == '\0' || strlen(optarg) != 1) {
135 fprintf(stderr, "sxiv: invalid argument for option -s: %s\n", optarg);
136 exit(EXIT_FAILURE);
137 }
138 _options.scalemode = s - scalemodes;
139 break;
140 case 't':
141 _options.thumb_mode = true;
142 break;
143 case 'v':
144 print_version();
145 exit(EXIT_SUCCESS);
146 case 'Z':
147 _options.scalemode = SCALE_ZOOM;
148 _options.zoom = 1.0;
149 break;
150 case 'z':
151 n = strtol(optarg, &end, 0);
152 if (*end != '\0' || n <= 0) {
153 fprintf(stderr, "sxiv: invalid argument for option -z: %s\n", optarg);
154 exit(EXIT_FAILURE);
155 }
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}