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