Krolyxon
Commit 6f9b4f5

Branches

Refs →

nsxivoptions.c

139 lines · 3.3 KB · C

1/* sxiv: options.c
2 * Copyright (c) 2012 Bert Muennich <be.muennich at googlemail.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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 printf("usage: sxiv [-bcdFfhpqrstvZ] [-g GEOMETRY] [-n NUM] "
36 "[-N name] [-z ZOOM] FILES...\n");
37}
38
39void print_version(void) {
40 printf("sxiv %s - Simple X Image Viewer\n", VERSION);
41}
42
43void parse_options(int argc, char **argv) {
44 int opt, t;
45
46 _options.recursive = false;
47 _options.startnum = 0;
48
49 _options.scalemode = SCALE_MODE;
50 _options.zoom = 1.0;
51 _options.aa = true;
52
53 _options.fixed_win = false;
54 _options.fullscreen = false;
55 _options.hide_bar = false;
56 _options.geometry = NULL;
57 _options.res_name = NULL;
58
59 _options.quiet = false;
60 _options.thumb_mode = false;
61 _options.clean_cache = false;
62
63 while ((opt = getopt(argc, argv, "bcdFfg:hn:N:pqrstvZz:")) != -1) {
64 switch (opt) {
65 case '?':
66 print_usage();
67 exit(EXIT_FAILURE);
68 case 'b':
69 _options.hide_bar = true;
70 break;
71 case 'c':
72 _options.clean_cache = true;
73 break;
74 case 'd':
75 _options.scalemode = SCALE_DOWN;
76 break;
77 case 'F':
78 _options.fixed_win = true;
79 break;
80 case 'f':
81 _options.fullscreen = true;
82 break;
83 case 'g':
84 _options.geometry = optarg;
85 break;
86 case 'h':
87 print_usage();
88 exit(EXIT_SUCCESS);
89 case 'n':
90 if (sscanf(optarg, "%d", &t) <= 0 || t < 1) {
91 fprintf(stderr, "sxiv: invalid argument for option -n: %s\n",
92 optarg);
93 exit(EXIT_FAILURE);
94 } else {
95 _options.startnum = t - 1;
96 }
97 break;
98 case 'N':
99 _options.res_name = optarg;
100 break;
101 case 'p':
102 _options.aa = false;
103 break;
104 case 'q':
105 _options.quiet = true;
106 break;
107 case 'r':
108 _options.recursive = true;
109 break;
110 case 's':
111 _options.scalemode = SCALE_FIT;
112 break;
113 case 't':
114 _options.thumb_mode = true;
115 break;
116 case 'v':
117 print_version();
118 exit(EXIT_SUCCESS);
119 case 'Z':
120 _options.scalemode = SCALE_ZOOM;
121 _options.zoom = 1.0;
122 break;
123 case 'z':
124 _options.scalemode = SCALE_ZOOM;
125 if (sscanf(optarg, "%d", &t) <= 0 || t <= 0) {
126 fprintf(stderr, "sxiv: invalid argument for option -z: %s\n",
127 optarg);
128 exit(EXIT_FAILURE);
129 }
130 _options.zoom = (float) t / 100.0;
131 break;
132 }
133 }
134
135 _options.filenames = argv + optind;
136 _options.filecnt = argc - optind;
137 _options.from_stdin = _options.filecnt == 1 &&
138 STREQ(_options.filenames[0], "-");
139}