Krolyxon
Commit ab28c9a

Branches

Refs →

nsxivoptions.c

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