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