Krolyxon
Commit 7a74fae

Branches

Refs →

nsxivoptions.c

167 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#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 [-bcfhioqrtvZ] [-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.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, "bcfG:g:hin:N:oqrS:s:tvZz:")) != -1) {
71 switch (opt) {
72 case '?':
73 print_usage();
74 exit(EXIT_FAILURE);
75 case 'b':
76 _options.hide_bar = true;
77 break;
78 case 'c':
79 _options.clean_cache = true;
80 break;
81 case 'f':
82 _options.fullscreen = true;
83 break;
84 case 'G':
85 n = strtol(optarg, &end, 0);
86 if (*end != '\0') {
87 fprintf(stderr, "sxiv: invalid argument for option -G: %s\n", optarg);
88 exit(EXIT_FAILURE);
89 }
90 _options.gamma = n;
91 break;
92 case 'g':
93 _options.geometry = optarg;
94 break;
95 case 'h':
96 print_usage();
97 exit(EXIT_SUCCESS);
98 case 'i':
99 _options.from_stdin = true;
100 break;
101 case 'n':
102 n = strtol(optarg, &end, 0);
103 if (*end != '\0' || n <= 0) {
104 fprintf(stderr, "sxiv: invalid argument for option -n: %s\n", optarg);
105 exit(EXIT_FAILURE);
106 }
107 _options.startnum = n - 1;
108 break;
109 case 'N':
110 _options.res_name = optarg;
111 break;
112 case 'o':
113 _options.to_stdout = true;
114 break;
115 case 'q':
116 _options.quiet = true;
117 break;
118 case 'r':
119 _options.recursive = true;
120 break;
121 case 'S':
122 n = strtol(optarg, &end, 0);
123 if (*end != '\0' || n <= 0) {
124 fprintf(stderr, "sxiv: invalid argument for option -S: %s\n", optarg);
125 exit(EXIT_FAILURE);
126 }
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 fprintf(stderr, "sxiv: invalid argument for option -s: %s\n", optarg);
133 exit(EXIT_FAILURE);
134 }
135 _options.scalemode = s - scalemodes;
136 break;
137 case 't':
138 _options.thumb_mode = true;
139 break;
140 case 'v':
141 print_version();
142 exit(EXIT_SUCCESS);
143 case 'Z':
144 _options.scalemode = SCALE_ZOOM;
145 _options.zoom = 1.0;
146 break;
147 case 'z':
148 n = strtol(optarg, &end, 0);
149 if (*end != '\0' || n <= 0) {
150 fprintf(stderr, "sxiv: invalid argument for option -z: %s\n", optarg);
151 exit(EXIT_FAILURE);
152 }
153 _options.scalemode = SCALE_ZOOM;
154 _options.zoom = (float) n / 100.0;
155 break;
156 }
157 }
158
159 _options.filenames = argv + optind;
160 _options.filecnt = argc - optind;
161
162 if (_options.filecnt == 1 && STREQ(_options.filenames[0], "-")) {
163 _options.filenames++;
164 _options.filecnt--;
165 _options.from_stdin = true;
166 }
167}