| 1 | /* sxiv: options.c |
| 2 | * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License as published by the |
| 6 | * Free Software Foundation; either version 2 of the License, or (at your |
| 7 | * option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along |
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | */ |
| 18 | |
| 19 | #define _POSIX_C_SOURCE 200112L |
| 20 | |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <stdio.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #include "options.h" |
| 27 | #include "util.h" |
| 28 | |
| 29 | #define _IMAGE_CONFIG |
| 30 | #include "config.h" |
| 31 | |
| 32 | options_t _options; |
| 33 | const options_t *options = (const options_t*) &_options; |
| 34 | |
| 35 | void print_usage() { |
| 36 | printf("usage: sxiv [-cdFfhpqrstvZ] [-g GEOMETRY] [-n NUM] [-z ZOOM] FILES...\n"); |
| 37 | } |
| 38 | |
| 39 | void print_version() { |
| 40 | printf("sxiv %s - simple x image viewer\n", VERSION); |
| 41 | } |
| 42 | |
| 43 | void parse_options(int argc, char **argv) { |
| 44 | float z; |
| 45 | int n, opt; |
| 46 | |
| 47 | _options.startnum = 0; |
| 48 | |
| 49 | _options.scalemode = SCALE_MODE; |
| 50 | _options.zoom = 1.0; |
| 51 | _options.aa = 1; |
| 52 | _options.thumbnails = 0; |
| 53 | |
| 54 | _options.fixed = 0; |
| 55 | _options.fullscreen = 0; |
| 56 | _options.geometry = NULL; |
| 57 | |
| 58 | _options.quiet = 0; |
| 59 | _options.clean_cache = 0; |
| 60 | _options.recursive = 0; |
| 61 | |
| 62 | while ((opt = getopt(argc, argv, "cdFfg:hn:pqrstvZz:")) != -1) { |
| 63 | switch (opt) { |
| 64 | case '?': |
| 65 | print_usage(); |
| 66 | exit(1); |
| 67 | case 'c': |
| 68 | _options.clean_cache = 1; |
| 69 | break; |
| 70 | case 'd': |
| 71 | _options.scalemode = SCALE_DOWN; |
| 72 | break; |
| 73 | case 'F': |
| 74 | _options.fixed = 1; |
| 75 | break; |
| 76 | case 'f': |
| 77 | _options.fullscreen = 1; |
| 78 | break; |
| 79 | case 'g': |
| 80 | _options.geometry = optarg; |
| 81 | break; |
| 82 | case 'h': |
| 83 | print_usage(); |
| 84 | exit(0); |
| 85 | case 'n': |
| 86 | if (!sscanf(optarg, "%d", &n) || n < 1) { |
| 87 | fprintf(stderr, "sxiv: invalid argument for option -n: %s\n", |
| 88 | optarg); |
| 89 | exit(1); |
| 90 | } else { |
| 91 | _options.startnum = n - 1; |
| 92 | } |
| 93 | break; |
| 94 | case 'p': |
| 95 | _options.aa = 0; |
| 96 | break; |
| 97 | case 'q': |
| 98 | _options.quiet = 1; |
| 99 | break; |
| 100 | case 'r': |
| 101 | _options.recursive = 1; |
| 102 | break; |
| 103 | case 's': |
| 104 | _options.scalemode = SCALE_FIT; |
| 105 | break; |
| 106 | case 't': |
| 107 | _options.thumbnails = 1; |
| 108 | break; |
| 109 | case 'v': |
| 110 | print_version(); |
| 111 | exit(0); |
| 112 | case 'Z': |
| 113 | _options.scalemode = SCALE_ZOOM; |
| 114 | _options.zoom = 1.0; |
| 115 | break; |
| 116 | case 'z': |
| 117 | _options.scalemode = SCALE_ZOOM; |
| 118 | if (!sscanf(optarg, "%f", &z) || z < 0) { |
| 119 | fprintf(stderr, "sxiv: invalid argument for option -z: %s\n", |
| 120 | optarg); |
| 121 | exit(1); |
| 122 | } else { |
| 123 | _options.zoom = z / 100.0; |
| 124 | } |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | _options.filenames = argv + optind; |
| 130 | _options.filecnt = argc - optind; |
| 131 | _options.from_stdin = _options.filecnt == 1 && |
| 132 | strcmp(_options.filenames[0], "-") == 0; |
| 133 | } |