Krolyxon
Commit ed69b48

Branches

Refs →

nsxivoptions.c

168 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#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 [-bcdFfhioqrstvZ] [-G GAMMA] [-g GEOMETRY] [-n NUM] "
37 "[-N NAME] [-S DELAY] [-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;
49
50 _options.from_stdin = false;
51 _options.to_stdout = false;
52 _options.recursive = false;
53 _options.startnum = 0;
54
55 _options.scalemode = SCALE_MODE;
56 _options.zoom = 1.0;
57 _options.gamma = 0;
58 _options.slideshow = 0;
59
60 _options.fixed_win = false;
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, "bcdFfG:g:hin:N:oqrS:stvZz:")) != -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 'd':
82 _options.scalemode = SCALE_DOWN;
83 break;
84 case 'F':
85 _options.fixed_win = 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 fprintf(stderr, "sxiv: invalid argument for option -G: %s\n", optarg);
94 exit(EXIT_FAILURE);
95 }
96 _options.gamma = n;
97 break;
98 case 'g':
99 _options.geometry = optarg;
100 break;
101 case 'h':
102 print_usage();
103 exit(EXIT_SUCCESS);
104 case 'i':
105 _options.from_stdin = true;
106 break;
107 case 'n':
108 n = strtol(optarg, &end, 0);
109 if (*end != '\0' || n <= 0) {
110 fprintf(stderr, "sxiv: invalid argument for option -n: %s\n", optarg);
111 exit(EXIT_FAILURE);
112 }
113 _options.startnum = n - 1;
114 break;
115 case 'N':
116 _options.res_name = optarg;
117 break;
118 case 'o':
119 _options.to_stdout = true;
120 break;
121 case 'q':
122 _options.quiet = true;
123 break;
124 case 'r':
125 _options.recursive = true;
126 break;
127 case 'S':
128 n = strtol(optarg, &end, 0);
129 if (*end != '\0' || n <= 0) {
130 fprintf(stderr, "sxiv: invalid argument for option -S: %s\n", optarg);
131 exit(EXIT_FAILURE);
132 }
133 _options.slideshow = n;
134 break;
135 case 's':
136 _options.scalemode = SCALE_FIT;
137 break;
138 case 't':
139 _options.thumb_mode = true;
140 break;
141 case 'v':
142 print_version();
143 exit(EXIT_SUCCESS);
144 case 'Z':
145 _options.scalemode = SCALE_ZOOM;
146 _options.zoom = 1.0;
147 break;
148 case 'z':
149 n = strtol(optarg, &end, 0);
150 if (*end != '\n' || n <= 0) {
151 fprintf(stderr, "sxiv: invalid argument for option -z: %s\n", optarg);
152 exit(EXIT_FAILURE);
153 }
154 _options.scalemode = SCALE_ZOOM;
155 _options.zoom = (float) n / 100.0;
156 break;
157 }
158 }
159
160 _options.filenames = argv + optind;
161 _options.filecnt = argc - optind;
162
163 if (_options.filecnt == 1 && STREQ(_options.filenames[0], "-")) {
164 _options.filenames++;
165 _options.filecnt--;
166 _options.from_stdin = true;
167 }
168}