Krolyxon
Commit 26a624a

Branches

Refs →

nsxivoptions.c

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