| 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 <stdio.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include "sxiv.h" |
| 26 | #include "options.h" |
| 27 | |
| 28 | options_t _options; |
| 29 | const options_t *options = (const options_t*) &_options; |
| 30 | |
| 31 | void print_usage() { |
| 32 | printf("usage: sxiv [-hv] [-w WIDTH[xHEIGHT]] FILES...\n"); |
| 33 | } |
| 34 | |
| 35 | void print_version() { |
| 36 | printf("sxiv - simple x image viewer\n"); |
| 37 | printf("Version %s, written by Bert Muennich\n", VERSION); |
| 38 | } |
| 39 | |
| 40 | void parse_options(int argc, char **argv) { |
| 41 | unsigned short w, h; |
| 42 | int opt; |
| 43 | |
| 44 | _options.winw = w = 0; |
| 45 | _options.winh = h = 0; |
| 46 | |
| 47 | while ((opt = getopt(argc, argv, "hvw:")) != -1) { |
| 48 | switch (opt) { |
| 49 | case '?': |
| 50 | print_usage(); |
| 51 | exit(1); |
| 52 | case 'h': |
| 53 | print_usage(); |
| 54 | exit(0); |
| 55 | case 'v': |
| 56 | print_version(); |
| 57 | exit(0); |
| 58 | case 'w': |
| 59 | if (!sscanf(optarg, "%hux%hu", &w, &h)) { |
| 60 | fprintf(stderr, "sxiv: invalid argument for option -w: %s\n", |
| 61 | optarg); |
| 62 | exit(1); |
| 63 | } else { |
| 64 | _options.winw = (int) w; |
| 65 | _options.winh = (int) h; |
| 66 | } |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | if (!_options.winw) { |
| 72 | _options.winw = WIN_WIDTH; |
| 73 | _options.winh = WIN_HEIGHT; |
| 74 | } else if (!_options.winh) { |
| 75 | _options.winh = _options.winw; |
| 76 | } |
| 77 | |
| 78 | _options.filenames = (const char**) argv + optind; |
| 79 | _options.filecnt = argc - optind; |
| 80 | } |