Krolyxon
Commit b32aefc

Branches

Refs →

nsxivoptions.c

163 lines · 3.9 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 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.hide_bar = false;
66 _options.geometry = NULL;
67 _options.res_name = NULL;
68
69 _options.quiet = false;
70 _options.thumb_mode = false;
71 _options.clean_cache = false;
72
73 while ((opt = getopt(argc, argv, "abcfG:g:hin:N:oqrS:s:tvZz:")) != -1) {
74 switch (opt) {
75 case '?':
76 print_usage();
77 exit(EXIT_FAILURE);
78 case 'a':
79 _options.animate = true;
80 break;
81 case 'b':
82 _options.hide_bar = true;
83 break;
84 case 'c':
85 _options.clean_cache = true;
86 break;
87 case 'f':
88 _options.fullscreen = true;
89 break;
90 case 'G':
91 n = strtol(optarg, &end, 0);
92 if (*end != '\0')
93 error(EXIT_FAILURE, 0, "Invalid argument for option -G: %s", optarg);
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 error(EXIT_FAILURE, 0, "Invalid argument for option -n: %s", optarg);
109 _options.startnum = n - 1;
110 break;
111 case 'N':
112 _options.res_name = optarg;
113 break;
114 case 'o':
115 _options.to_stdout = true;
116 break;
117 case 'q':
118 _options.quiet = true;
119 break;
120 case 'r':
121 _options.recursive = true;
122 break;
123 case 'S':
124 n = strtol(optarg, &end, 0);
125 if (*end != '\0' || n <= 0)
126 error(EXIT_FAILURE, 0, "Invalid argument for option -S: %s", optarg);
127 _options.slideshow = n;
128 break;
129 case 's':
130 s = strchr(scalemodes, optarg[0]);
131 if (s == NULL || *s == '\0' || strlen(optarg) != 1)
132 error(EXIT_FAILURE, 0, "Invalid argument for option -s: %s", optarg);
133 _options.scalemode = s - scalemodes;
134 break;
135 case 't':
136 _options.thumb_mode = true;
137 break;
138 case 'v':
139 print_version();
140 exit(EXIT_SUCCESS);
141 case 'Z':
142 _options.scalemode = SCALE_ZOOM;
143 _options.zoom = 1.0;
144 break;
145 case 'z':
146 n = strtol(optarg, &end, 0);
147 if (*end != '\0' || n <= 0)
148 error(EXIT_FAILURE, 0, "Invalid argument for option -z: %s", optarg);
149 _options.scalemode = SCALE_ZOOM;
150 _options.zoom = (float) n / 100.0;
151 break;
152 }
153 }
154
155 _options.filenames = argv + optind;
156 _options.filecnt = argc - optind;
157
158 if (_options.filecnt == 1 && STREQ(_options.filenames[0], "-")) {
159 _options.filenames++;
160 _options.filecnt--;
161 _options.from_stdin = true;
162 }
163}