| 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 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | #include <stdio.h> |
| 22 | #include <unistd.h> |
| 23 | |
| 24 | #include "options.h" |
| 25 | #include "util.h" |
| 26 | |
| 27 | #define _IMAGE_CONFIG |
| 28 | #include "config.h" |
| 29 | |
| 30 | options_t _options; |
| 31 | const options_t *options = (const options_t*) &_options; |
| 32 | |
| 33 | void print_usage(void) |
| 34 | { |
| 35 | printf("usage: sxiv [-abcfhioqrtvZ] [-e WID] [-G GAMMA] [-g GEOMETRY] " |
| 36 | "[-N NAME] [-n NUM] [-S DELAY] [-s MODE] [-z ZOOM] FILES...\n"); |
| 37 | } |
| 38 | |
| 39 | void print_version(void) |
| 40 | { |
| 41 | printf("sxiv %s - Simple X Image Viewer\n", VERSION); |
| 42 | } |
| 43 | |
| 44 | void parse_options(int argc, char **argv) |
| 45 | { |
| 46 | int n, opt; |
| 47 | char *end, *s; |
| 48 | const char *scalemodes = "dfwh"; |
| 49 | |
| 50 | progname = strrchr(argv[0], '/'); |
| 51 | progname = progname ? progname + 1 : argv[0]; |
| 52 | |
| 53 | _options.from_stdin = false; |
| 54 | _options.to_stdout = false; |
| 55 | _options.recursive = false; |
| 56 | _options.startnum = 0; |
| 57 | |
| 58 | _options.scalemode = SCALE_DOWN; |
| 59 | _options.zoom = 1.0; |
| 60 | _options.animate = false; |
| 61 | _options.gamma = 0; |
| 62 | _options.slideshow = 0; |
| 63 | |
| 64 | _options.fullscreen = false; |
| 65 | _options.embed = 0; |
| 66 | _options.hide_bar = false; |
| 67 | _options.geometry = NULL; |
| 68 | _options.res_name = NULL; |
| 69 | |
| 70 | _options.quiet = false; |
| 71 | _options.thumb_mode = false; |
| 72 | _options.clean_cache = false; |
| 73 | |
| 74 | while ((opt = getopt(argc, argv, "abce:fG:g:hin:N:oqrS:s:tvZz:")) != -1) { |
| 75 | switch (opt) { |
| 76 | case '?': |
| 77 | print_usage(); |
| 78 | exit(EXIT_FAILURE); |
| 79 | case 'a': |
| 80 | _options.animate = true; |
| 81 | break; |
| 82 | case 'b': |
| 83 | _options.hide_bar = true; |
| 84 | break; |
| 85 | case 'c': |
| 86 | _options.clean_cache = true; |
| 87 | break; |
| 88 | case 'e': |
| 89 | n = strtol(optarg, &end, 0); |
| 90 | if (*end != '\0') |
| 91 | error(EXIT_FAILURE, 0, "Invalid argument for option -e: %s", optarg); |
| 92 | _options.embed = n; |
| 93 | break; |
| 94 | case 'f': |
| 95 | _options.fullscreen = true; |
| 96 | break; |
| 97 | case 'G': |
| 98 | n = strtol(optarg, &end, 0); |
| 99 | if (*end != '\0') |
| 100 | error(EXIT_FAILURE, 0, "Invalid argument for option -G: %s", optarg); |
| 101 | _options.gamma = n; |
| 102 | break; |
| 103 | case 'g': |
| 104 | _options.geometry = optarg; |
| 105 | break; |
| 106 | case 'h': |
| 107 | print_usage(); |
| 108 | exit(EXIT_SUCCESS); |
| 109 | case 'i': |
| 110 | _options.from_stdin = true; |
| 111 | break; |
| 112 | case 'n': |
| 113 | n = strtol(optarg, &end, 0); |
| 114 | if (*end != '\0' || n <= 0) |
| 115 | error(EXIT_FAILURE, 0, "Invalid argument for option -n: %s", optarg); |
| 116 | _options.startnum = n - 1; |
| 117 | break; |
| 118 | case 'N': |
| 119 | _options.res_name = optarg; |
| 120 | break; |
| 121 | case 'o': |
| 122 | _options.to_stdout = true; |
| 123 | break; |
| 124 | case 'q': |
| 125 | _options.quiet = true; |
| 126 | break; |
| 127 | case 'r': |
| 128 | _options.recursive = true; |
| 129 | break; |
| 130 | case 'S': |
| 131 | n = strtol(optarg, &end, 0); |
| 132 | if (*end != '\0' || n <= 0) |
| 133 | error(EXIT_FAILURE, 0, "Invalid argument for option -S: %s", optarg); |
| 134 | _options.slideshow = n; |
| 135 | break; |
| 136 | case 's': |
| 137 | s = strchr(scalemodes, optarg[0]); |
| 138 | if (s == NULL || *s == '\0' || strlen(optarg) != 1) |
| 139 | error(EXIT_FAILURE, 0, "Invalid argument for option -s: %s", optarg); |
| 140 | _options.scalemode = s - scalemodes; |
| 141 | break; |
| 142 | case 't': |
| 143 | _options.thumb_mode = true; |
| 144 | break; |
| 145 | case 'v': |
| 146 | print_version(); |
| 147 | exit(EXIT_SUCCESS); |
| 148 | case 'Z': |
| 149 | _options.scalemode = SCALE_ZOOM; |
| 150 | _options.zoom = 1.0; |
| 151 | break; |
| 152 | case 'z': |
| 153 | n = strtol(optarg, &end, 0); |
| 154 | if (*end != '\0' || n <= 0) |
| 155 | error(EXIT_FAILURE, 0, "Invalid argument for option -z: %s", optarg); |
| 156 | _options.scalemode = SCALE_ZOOM; |
| 157 | _options.zoom = (float) n / 100.0; |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | _options.filenames = argv + optind; |
| 163 | _options.filecnt = argc - optind; |
| 164 | |
| 165 | if (_options.filecnt == 1 && STREQ(_options.filenames[0], "-")) { |
| 166 | _options.filenames++; |
| 167 | _options.filecnt--; |
| 168 | _options.from_stdin = true; |
| 169 | } |
| 170 | } |