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