Krolyxon
Commit 68ff9d7

Branches

Refs →

nsxivoptions.c

154 lines · 3.4 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 [-bcdFfhiopqrstvZ] [-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;
48
49 _options.from_stdin = false;
50 _options.to_stdout = false;
51 _options.recursive = false;
52 _options.startnum = 0;
53
54 _options.scalemode = SCALE_MODE;
55 _options.zoom = 1.0;
56 _options.aa = true;
57
58 _options.fixed_win = false;
59 _options.fullscreen = false;
60 _options.hide_bar = false;
61 _options.geometry = NULL;
62 _options.res_name = NULL;
63
64 _options.quiet = false;
65 _options.thumb_mode = false;
66 _options.clean_cache = false;
67
68 while ((opt = getopt(argc, argv, "bcdFfg:hin:N:opqrstvZz:")) != -1) {
69 switch (opt) {
70 case '?':
71 print_usage();
72 exit(EXIT_FAILURE);
73 case 'b':
74 _options.hide_bar = true;
75 break;
76 case 'c':
77 _options.clean_cache = true;
78 break;
79 case 'd':
80 _options.scalemode = SCALE_DOWN;
81 break;
82 case 'F':
83 _options.fixed_win = true;
84 break;
85 case 'f':
86 _options.fullscreen = true;
87 break;
88 case 'g':
89 _options.geometry = optarg;
90 break;
91 case 'h':
92 print_usage();
93 exit(EXIT_SUCCESS);
94 case 'i':
95 _options.from_stdin = true;
96 break;
97 case 'n':
98 if (sscanf(optarg, "%d", &t) <= 0 || t < 1) {
99 fprintf(stderr, "sxiv: invalid argument for option -n: %s\n",
100 optarg);
101 exit(EXIT_FAILURE);
102 } else {
103 _options.startnum = t - 1;
104 }
105 break;
106 case 'N':
107 _options.res_name = optarg;
108 break;
109 case 'o':
110 _options.to_stdout = true;
111 break;
112 case 'p':
113 _options.aa = false;
114 break;
115 case 'q':
116 _options.quiet = true;
117 break;
118 case 'r':
119 _options.recursive = true;
120 break;
121 case 's':
122 _options.scalemode = SCALE_FIT;
123 break;
124 case 't':
125 _options.thumb_mode = true;
126 break;
127 case 'v':
128 print_version();
129 exit(EXIT_SUCCESS);
130 case 'Z':
131 _options.scalemode = SCALE_ZOOM;
132 _options.zoom = 1.0;
133 break;
134 case 'z':
135 _options.scalemode = SCALE_ZOOM;
136 if (sscanf(optarg, "%d", &t) <= 0 || t <= 0) {
137 fprintf(stderr, "sxiv: invalid argument for option -z: %s\n",
138 optarg);
139 exit(EXIT_FAILURE);
140 }
141 _options.zoom = (float) t / 100.0;
142 break;
143 }
144 }
145
146 _options.filenames = argv + optind;
147 _options.filecnt = argc - optind;
148
149 if (_options.filecnt == 1 && STREQ(_options.filenames[0], "-")) {
150 _options.filenames++;
151 _options.filecnt--;
152 _options.from_stdin = true;
153 }
154}