Krolyxon
Commit f2a3d73

Branches

Refs →

nsxivoptions.c

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