| 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 [-adFfhpqrstvZ] [-g GEOMETRY] [-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 opt; |
| 44 | |
| 45 | _options.scalemode = SCALE_MODE; |
| 46 | _options.zoom = 1.0; |
| 47 | _options.aa = 1; |
| 48 | _options.thumbnails = 0; |
| 49 | |
| 50 | _options.fixed = 0; |
| 51 | _options.fullscreen = 0; |
| 52 | _options.geometry = NULL; |
| 53 | |
| 54 | _options.all = 0; |
| 55 | _options.quiet = 0; |
| 56 | _options.recursive = 0; |
| 57 | |
| 58 | while ((opt = getopt(argc, argv, "adFfg:hpqrstvZz:")) != -1) { |
| 59 | switch (opt) { |
| 60 | case '?': |
| 61 | print_usage(); |
| 62 | exit(1); |
| 63 | case 'a': |
| 64 | _options.all = 1; |
| 65 | break; |
| 66 | case 'd': |
| 67 | _options.scalemode = SCALE_DOWN; |
| 68 | break; |
| 69 | case 'F': |
| 70 | _options.fixed = 1; |
| 71 | break; |
| 72 | case 'f': |
| 73 | _options.fullscreen = 1; |
| 74 | break; |
| 75 | case 'g': |
| 76 | _options.geometry = optarg; |
| 77 | break; |
| 78 | case 'h': |
| 79 | print_usage(); |
| 80 | exit(0); |
| 81 | case 'p': |
| 82 | _options.aa = 0; |
| 83 | break; |
| 84 | case 'q': |
| 85 | _options.quiet = 1; |
| 86 | break; |
| 87 | case 'r': |
| 88 | _options.recursive = 1; |
| 89 | break; |
| 90 | case 's': |
| 91 | _options.scalemode = SCALE_FIT; |
| 92 | break; |
| 93 | case 't': |
| 94 | _options.thumbnails = 1; |
| 95 | break; |
| 96 | case 'v': |
| 97 | print_version(); |
| 98 | exit(0); |
| 99 | case 'Z': |
| 100 | _options.scalemode = SCALE_ZOOM; |
| 101 | _options.zoom = 1.0; |
| 102 | break; |
| 103 | case 'z': |
| 104 | _options.scalemode = SCALE_ZOOM; |
| 105 | if (!sscanf(optarg, "%f", &z) || z < 0) { |
| 106 | fprintf(stderr, "sxiv: invalid argument for option -z: %s\n", |
| 107 | optarg); |
| 108 | exit(1); |
| 109 | } else { |
| 110 | _options.zoom = z / 100.0; |
| 111 | } |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | _options.filenames = (const char**) argv + optind; |
| 117 | _options.filecnt = argc - optind; |
| 118 | _options.from_stdin = _options.filecnt == 1 && |
| 119 | strcmp(_options.filenames[0], "-") == 0; |
| 120 | } |