Krolyxon
Commit c3c95ab

Branches

Refs →

nsxivoptions.c

144 lines · 3.4 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 _FEATURE_CONFIG
21#define _IMAGE_CONFIG
22
23#include <stdlib.h>
24#include <string.h>
25#include <stdio.h>
26#include <unistd.h>
27
28#include "options.h"
29#include "util.h"
30#include "config.h"
31
32options_t _options;
33const options_t *options = (const options_t*) &_options;
34
35void print_usage(void) {
36 printf("usage: sxiv [-cdFfhpqrstvZ] [-g GEOMETRY] [-n NUM] "
37 "[-z ZOOM] FILES...\n");
38}
39
40void print_version(void) {
41 printf("sxiv %s - Simple X Image Viewer\n", VERSION);
42 printf("Additional features included (+) or not (-): %s, %s\n",
43#if EXIF_SUPPORT
44 "+exif",
45#else
46 "-exif",
47#endif
48#if GIF_SUPPORT
49 "+gif"
50#else
51 "-gif"
52#endif
53 );
54}
55
56void parse_options(int argc, char **argv) {
57 int opt, t;
58
59 _options.recursive = false;
60 _options.startnum = 0;
61
62 _options.scalemode = SCALE_MODE;
63 _options.zoom = 1.0;
64 _options.aa = true;
65
66 _options.fixed_win = false;
67 _options.fullscreen = false;
68 _options.geometry = NULL;
69
70 _options.quiet = false;
71 _options.thumb_mode = false;
72 _options.clean_cache = false;
73
74 while ((opt = getopt(argc, argv, "cdFfg:hn:pqrstvZz:")) != -1) {
75 switch (opt) {
76 case '?':
77 print_usage();
78 exit(EXIT_FAILURE);
79 case 'c':
80 _options.clean_cache = true;
81 break;
82 case 'd':
83 _options.scalemode = SCALE_DOWN;
84 break;
85 case 'F':
86 _options.fixed_win = true;
87 break;
88 case 'f':
89 _options.fullscreen = true;
90 break;
91 case 'g':
92 _options.geometry = optarg;
93 break;
94 case 'h':
95 print_usage();
96 exit(EXIT_SUCCESS);
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 'p':
107 _options.aa = false;
108 break;
109 case 'q':
110 _options.quiet = true;
111 break;
112 case 'r':
113 _options.recursive = true;
114 break;
115 case 's':
116 _options.scalemode = SCALE_FIT;
117 break;
118 case 't':
119 _options.thumb_mode = true;
120 break;
121 case 'v':
122 print_version();
123 exit(EXIT_SUCCESS);
124 case 'Z':
125 _options.scalemode = SCALE_ZOOM;
126 _options.zoom = 1.0;
127 break;
128 case 'z':
129 _options.scalemode = SCALE_ZOOM;
130 if (sscanf(optarg, "%d", &t) <= 0 || t <= 0) {
131 fprintf(stderr, "sxiv: invalid argument for option -z: %s\n",
132 optarg);
133 exit(EXIT_FAILURE);
134 }
135 _options.zoom = (float) t / 100.0;
136 break;
137 }
138 }
139
140 _options.filenames = argv + optind;
141 _options.filecnt = argc - optind;
142 _options.from_stdin = _options.filecnt == 1 &&
143 STREQ(_options.filenames[0], "-");
144}