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