Krolyxon
Commit b845827

Branches

Refs →

nsxivoptions.c

131 lines · 3.2 KB · C

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