| 1 | /* Copyright 2011-2020 Bert Muennich |
| 2 | * Copyright 2021-2022 nsxiv contributors |
| 3 | * |
| 4 | * This file is a part of nsxiv. |
| 5 | * |
| 6 | * nsxiv is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published |
| 8 | * by the Free Software Foundation; either version 2 of the License, |
| 9 | * or (at your option) any later version. |
| 10 | * |
| 11 | * nsxiv is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with nsxiv. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "nsxiv.h" |
| 21 | #include "version.h" |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | const opt_t *options; |
| 29 | |
| 30 | void print_usage(void) |
| 31 | { |
| 32 | printf("usage: nsxiv [-abcfhiopqrtvZ0] [-A FRAMERATE] [-e WID] [-G GAMMA] " |
| 33 | "[-g GEOMETRY] [-N NAME] [-n NUM] [-S DELAY] [-s MODE] " |
| 34 | "[-z ZOOM] FILES...\n"); |
| 35 | } |
| 36 | |
| 37 | static void title_deprecation_notice(void) |
| 38 | { |
| 39 | error(EXIT_FAILURE, 0, "\n" |
| 40 | "################################################################\n" |
| 41 | "# DEPRECATION NOTICE #\n" |
| 42 | "################################################################\n" |
| 43 | "# `-T` option has been deprecated in favour of `win-title`. #\n" |
| 44 | "# Please read the `WINDOW TITLE` section of the manpage for #\n" |
| 45 | "# more info. #\n" |
| 46 | "################################################################" |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | static void print_version(void) |
| 51 | { |
| 52 | puts("nsxiv " VERSION); |
| 53 | } |
| 54 | |
| 55 | void parse_options(int argc, char **argv) |
| 56 | { |
| 57 | int n, opt; |
| 58 | char *end, *s; |
| 59 | const char *scalemodes = "dfFwh"; |
| 60 | static opt_t _options; |
| 61 | options = &_options; |
| 62 | |
| 63 | progname = strrchr(argv[0], '/'); |
| 64 | progname = progname ? progname + 1 : argv[0]; |
| 65 | |
| 66 | _options.from_stdin = false; |
| 67 | _options.to_stdout = false; |
| 68 | _options.using_null = false; |
| 69 | _options.recursive = false; |
| 70 | _options.startnum = 0; |
| 71 | |
| 72 | _options.scalemode = SCALE_DOWN; |
| 73 | _options.zoom = 1.0; |
| 74 | _options.animate = false; |
| 75 | _options.gamma = 0; |
| 76 | _options.slideshow = 0; |
| 77 | _options.framerate = 0; |
| 78 | |
| 79 | _options.fullscreen = false; |
| 80 | _options.embed = 0; |
| 81 | _options.hide_bar = false; |
| 82 | _options.geometry = NULL; |
| 83 | _options.res_name = NULL; |
| 84 | |
| 85 | _options.quiet = false; |
| 86 | _options.thumb_mode = false; |
| 87 | _options.clean_cache = false; |
| 88 | _options.private_mode = false; |
| 89 | |
| 90 | while ((opt = getopt(argc, argv, "A:abce:fG:g:hin:N:opqrS:s:T:tvZz:0")) != -1) { |
| 91 | switch (opt) { |
| 92 | case '?': |
| 93 | print_usage(); |
| 94 | exit(EXIT_FAILURE); |
| 95 | case 'A': |
| 96 | n = strtol(optarg, &end, 0); |
| 97 | if (*end != '\0' || n <= 0) |
| 98 | error(EXIT_FAILURE, 0, "Invalid argument for option -A: %s", optarg); |
| 99 | _options.framerate = n; |
| 100 | /* fall through */ |
| 101 | case 'a': |
| 102 | _options.animate = true; |
| 103 | break; |
| 104 | case 'b': |
| 105 | _options.hide_bar = true; |
| 106 | break; |
| 107 | case 'c': |
| 108 | _options.clean_cache = true; |
| 109 | break; |
| 110 | case 'e': |
| 111 | n = strtol(optarg, &end, 0); |
| 112 | if (*end != '\0') |
| 113 | error(EXIT_FAILURE, 0, "Invalid argument for option -e: %s", optarg); |
| 114 | _options.embed = n; |
| 115 | break; |
| 116 | case 'f': |
| 117 | _options.fullscreen = true; |
| 118 | break; |
| 119 | case 'G': |
| 120 | n = strtol(optarg, &end, 0); |
| 121 | if (*end != '\0') |
| 122 | error(EXIT_FAILURE, 0, "Invalid argument for option -G: %s", optarg); |
| 123 | _options.gamma = n; |
| 124 | break; |
| 125 | case 'g': |
| 126 | _options.geometry = optarg; |
| 127 | break; |
| 128 | case 'h': |
| 129 | print_usage(); |
| 130 | exit(EXIT_SUCCESS); |
| 131 | case 'i': |
| 132 | _options.from_stdin = true; |
| 133 | break; |
| 134 | case 'n': |
| 135 | n = strtol(optarg, &end, 0); |
| 136 | if (*end != '\0' || n <= 0) |
| 137 | error(EXIT_FAILURE, 0, "Invalid argument for option -n: %s", optarg); |
| 138 | _options.startnum = n - 1; |
| 139 | break; |
| 140 | case 'N': |
| 141 | _options.res_name = optarg; |
| 142 | break; |
| 143 | case 'o': |
| 144 | _options.to_stdout = true; |
| 145 | break; |
| 146 | case 'p': |
| 147 | _options.private_mode = true; |
| 148 | break; |
| 149 | case 'q': |
| 150 | _options.quiet = true; |
| 151 | break; |
| 152 | case 'r': |
| 153 | _options.recursive = true; |
| 154 | break; |
| 155 | case 'S': |
| 156 | n = strtof(optarg, &end) * 10; |
| 157 | if (*end != '\0' || n <= 0) |
| 158 | error(EXIT_FAILURE, 0, "Invalid argument for option -S: %s", optarg); |
| 159 | _options.slideshow = n; |
| 160 | break; |
| 161 | case 's': |
| 162 | s = strchr(scalemodes, optarg[0]); |
| 163 | if (s == NULL || *s == '\0' || strlen(optarg) != 1) |
| 164 | error(EXIT_FAILURE, 0, "Invalid argument for option -s: %s", optarg); |
| 165 | _options.scalemode = s - scalemodes; |
| 166 | break; |
| 167 | case 'T': |
| 168 | title_deprecation_notice(); /* TODO(v31): remove this option */ |
| 169 | break; |
| 170 | case 't': |
| 171 | _options.thumb_mode = true; |
| 172 | break; |
| 173 | case 'v': |
| 174 | print_version(); |
| 175 | exit(EXIT_SUCCESS); |
| 176 | case 'Z': |
| 177 | _options.scalemode = SCALE_ZOOM; |
| 178 | _options.zoom = 1.0; |
| 179 | break; |
| 180 | case 'z': |
| 181 | n = strtol(optarg, &end, 0); |
| 182 | if (*end != '\0' || n <= 0) |
| 183 | error(EXIT_FAILURE, 0, "Invalid argument for option -z: %s", optarg); |
| 184 | _options.scalemode = SCALE_ZOOM; |
| 185 | _options.zoom = (float) n / 100.0; |
| 186 | break; |
| 187 | case '0': |
| 188 | _options.using_null = true; |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | _options.filenames = argv + optind; |
| 194 | _options.filecnt = argc - optind; |
| 195 | |
| 196 | if (_options.filecnt == 1 && STREQ(_options.filenames[0], "-")) { |
| 197 | _options.filenames++; |
| 198 | _options.filecnt--; |
| 199 | _options.from_stdin = true; |
| 200 | } |
| 201 | } |