Krolyxon
Commit f55d9f4

Branches

Refs →

nsxivoptions.c

171 lines · 4.1 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#define _POSIX_C_SOURCE 200112L
20#define _IMAGE_CONFIG
21
22#include <stdlib.h>
23#include <string.h>
24#include <stdio.h>
25#include <unistd.h>
26
27#include "options.h"
28#include "util.h"
29#include "config.h"
30
31options_t _options;
32const options_t *options = (const options_t*) &_options;
33
34void print_usage(void)
35{
36 printf("usage: sxiv [-abcfhioqrtvZ] [-G GAMMA] [-g GEOMETRY] [-n NUM] "
37 "[-N NAME] [-S DELAY] [-s MODE] [-z ZOOM] 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 _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
62 _options.fullscreen = false;
63 _options.hide_bar = false;
64 _options.geometry = NULL;
65 _options.res_name = NULL;
66
67 _options.quiet = false;
68 _options.thumb_mode = false;
69 _options.clean_cache = false;
70
71 while ((opt = getopt(argc, argv, "abcfG:g:hin:N:oqrS:s:tvZz:")) != -1) {
72 switch (opt) {
73 case '?':
74 print_usage();
75 exit(EXIT_FAILURE);
76 case 'a':
77 _options.animate = true;
78 break;
79 case 'b':
80 _options.hide_bar = true;
81 break;
82 case 'c':
83 _options.clean_cache = true;
84 break;
85 case 'f':
86 _options.fullscreen = true;
87 break;
88 case 'G':
89 n = strtol(optarg, &end, 0);
90 if (*end != '\0') {
91 fprintf(stderr, "sxiv: invalid argument for option -G: %s\n", optarg);
92 exit(EXIT_FAILURE);
93 }
94 _options.gamma = n;
95 break;
96 case 'g':
97 _options.geometry = optarg;
98 break;
99 case 'h':
100 print_usage();
101 exit(EXIT_SUCCESS);
102 case 'i':
103 _options.from_stdin = true;
104 break;
105 case 'n':
106 n = strtol(optarg, &end, 0);
107 if (*end != '\0' || n <= 0) {
108 fprintf(stderr, "sxiv: invalid argument for option -n: %s\n", optarg);
109 exit(EXIT_FAILURE);
110 }
111 _options.startnum = n - 1;
112 break;
113 case 'N':
114 _options.res_name = optarg;
115 break;
116 case 'o':
117 _options.to_stdout = true;
118 break;
119 case 'q':
120 _options.quiet = true;
121 break;
122 case 'r':
123 _options.recursive = true;
124 break;
125 case 'S':
126 n = strtol(optarg, &end, 0);
127 if (*end != '\0' || n <= 0) {
128 fprintf(stderr, "sxiv: invalid argument for option -S: %s\n", optarg);
129 exit(EXIT_FAILURE);
130 }
131 _options.slideshow = n;
132 break;
133 case 's':
134 s = strchr(scalemodes, optarg[0]);
135 if (s == NULL || *s == '\0' || strlen(optarg) != 1) {
136 fprintf(stderr, "sxiv: invalid argument for option -s: %s\n", optarg);
137 exit(EXIT_FAILURE);
138 }
139 _options.scalemode = s - scalemodes;
140 break;
141 case 't':
142 _options.thumb_mode = true;
143 break;
144 case 'v':
145 print_version();
146 exit(EXIT_SUCCESS);
147 case 'Z':
148 _options.scalemode = SCALE_ZOOM;
149 _options.zoom = 1.0;
150 break;
151 case 'z':
152 n = strtol(optarg, &end, 0);
153 if (*end != '\0' || n <= 0) {
154 fprintf(stderr, "sxiv: invalid argument for option -z: %s\n", optarg);
155 exit(EXIT_FAILURE);
156 }
157 _options.scalemode = SCALE_ZOOM;
158 _options.zoom = (float) n / 100.0;
159 break;
160 }
161 }
162
163 _options.filenames = argv + optind;
164 _options.filecnt = argc - optind;
165
166 if (_options.filecnt == 1 && STREQ(_options.filenames[0], "-")) {
167 _options.filenames++;
168 _options.filecnt--;
169 _options.from_stdin = true;
170 }
171}