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