| 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 [-bcFfhioqrstvZ] [-G GAMMA] [-g GEOMETRY] [-n NUM] " |
| 37 | "[-N NAME] [-S DELAY] [-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 n, opt; |
| 48 | char *end; |
| 49 | |
| 50 | _options.from_stdin = false; |
| 51 | _options.to_stdout = false; |
| 52 | _options.recursive = false; |
| 53 | _options.startnum = 0; |
| 54 | |
| 55 | _options.scalemode = SCALE_DOWN; |
| 56 | _options.zoom = 1.0; |
| 57 | _options.gamma = 0; |
| 58 | _options.slideshow = 0; |
| 59 | |
| 60 | _options.fixed_win = false; |
| 61 | _options.fullscreen = false; |
| 62 | _options.hide_bar = false; |
| 63 | _options.geometry = NULL; |
| 64 | _options.res_name = NULL; |
| 65 | |
| 66 | _options.quiet = false; |
| 67 | _options.thumb_mode = false; |
| 68 | _options.clean_cache = false; |
| 69 | |
| 70 | while ((opt = getopt(argc, argv, "bcFfG:g:hin:N:oqrS:stvZz:")) != -1) { |
| 71 | switch (opt) { |
| 72 | case '?': |
| 73 | print_usage(); |
| 74 | exit(EXIT_FAILURE); |
| 75 | case 'b': |
| 76 | _options.hide_bar = true; |
| 77 | break; |
| 78 | case 'c': |
| 79 | _options.clean_cache = true; |
| 80 | break; |
| 81 | case 'F': |
| 82 | _options.fixed_win = true; |
| 83 | break; |
| 84 | case 'f': |
| 85 | _options.fullscreen = true; |
| 86 | break; |
| 87 | case 'G': |
| 88 | n = strtol(optarg, &end, 0); |
| 89 | if (*end != '\0') { |
| 90 | fprintf(stderr, "sxiv: invalid argument for option -G: %s\n", optarg); |
| 91 | exit(EXIT_FAILURE); |
| 92 | } |
| 93 | _options.gamma = n; |
| 94 | break; |
| 95 | case 'g': |
| 96 | _options.geometry = optarg; |
| 97 | break; |
| 98 | case 'h': |
| 99 | print_usage(); |
| 100 | exit(EXIT_SUCCESS); |
| 101 | case 'i': |
| 102 | _options.from_stdin = true; |
| 103 | break; |
| 104 | case 'n': |
| 105 | n = strtol(optarg, &end, 0); |
| 106 | if (*end != '\0' || n <= 0) { |
| 107 | fprintf(stderr, "sxiv: invalid argument for option -n: %s\n", optarg); |
| 108 | exit(EXIT_FAILURE); |
| 109 | } |
| 110 | _options.startnum = n - 1; |
| 111 | break; |
| 112 | case 'N': |
| 113 | _options.res_name = optarg; |
| 114 | break; |
| 115 | case 'o': |
| 116 | _options.to_stdout = true; |
| 117 | break; |
| 118 | case 'q': |
| 119 | _options.quiet = true; |
| 120 | break; |
| 121 | case 'r': |
| 122 | _options.recursive = true; |
| 123 | break; |
| 124 | case 'S': |
| 125 | n = strtol(optarg, &end, 0); |
| 126 | if (*end != '\0' || n <= 0) { |
| 127 | fprintf(stderr, "sxiv: invalid argument for option -S: %s\n", optarg); |
| 128 | exit(EXIT_FAILURE); |
| 129 | } |
| 130 | _options.slideshow = n; |
| 131 | break; |
| 132 | case 's': |
| 133 | _options.scalemode = SCALE_FIT; |
| 134 | break; |
| 135 | case 't': |
| 136 | _options.thumb_mode = true; |
| 137 | break; |
| 138 | case 'v': |
| 139 | print_version(); |
| 140 | exit(EXIT_SUCCESS); |
| 141 | case 'Z': |
| 142 | _options.scalemode = SCALE_ZOOM; |
| 143 | _options.zoom = 1.0; |
| 144 | break; |
| 145 | case 'z': |
| 146 | n = strtol(optarg, &end, 0); |
| 147 | if (*end != '\0' || n <= 0) { |
| 148 | fprintf(stderr, "sxiv: invalid argument for option -z: %s\n", optarg); |
| 149 | exit(EXIT_FAILURE); |
| 150 | } |
| 151 | _options.scalemode = SCALE_ZOOM; |
| 152 | _options.zoom = (float) n / 100.0; |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | _options.filenames = argv + optind; |
| 158 | _options.filecnt = argc - optind; |
| 159 | |
| 160 | if (_options.filecnt == 1 && STREQ(_options.filenames[0], "-")) { |
| 161 | _options.filenames++; |
| 162 | _options.filecnt--; |
| 163 | _options.from_stdin = true; |
| 164 | } |
| 165 | } |