| 1 | /* |
|---|
| 2 | * neercs console-based window manager |
|---|
| 3 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
|---|
| 4 | * 2008 Jean-Yves Lamoureux <jylam@lnxscene.org> |
|---|
| 5 | * All Rights Reserved |
|---|
| 6 | * |
|---|
| 7 | * |
|---|
| 8 | * $Id: mygetopt.c 271 2007-02-02 11:58:06Z sam $ |
|---|
| 9 | * |
|---|
| 10 | * This program is free software. It comes without any warranty, to |
|---|
| 11 | * the extent permitted by applicable law. You can redistribute it |
|---|
| 12 | * and/or modify it under the terms of the Do What The Fuck You Want |
|---|
| 13 | * To Public License, Version 2, as published by Sam Hocevar. See |
|---|
| 14 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | /* |
|---|
| 18 | * mygetopt.c: getopt_long reimplementation |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | #include "config.h" |
|---|
| 22 | |
|---|
| 23 | #include <stdio.h> |
|---|
| 24 | #include <string.h> |
|---|
| 25 | |
|---|
| 26 | #include "cucul_types.h" |
|---|
| 27 | |
|---|
| 28 | #include "mygetopt.h" |
|---|
| 29 | |
|---|
| 30 | int myoptind = 1; |
|---|
| 31 | char *myoptarg = NULL; |
|---|
| 32 | |
|---|
| 33 | /* XXX: this getopt_long implementation should not be trusted for other |
|---|
| 34 | * applications without any serious peer reviewing. It “just works” with |
|---|
| 35 | * zzuf but may fail miserably in other programs. */ |
|---|
| 36 | int mygetopt(int argc, char * const _argv[], const char *optstring, |
|---|
| 37 | const struct myoption *longopts, int *longindex) |
|---|
| 38 | { |
|---|
| 39 | char **argv = (char **)(uintptr_t)_argv; |
|---|
| 40 | char *flag; |
|---|
| 41 | int i; |
|---|
| 42 | |
|---|
| 43 | if(myoptind >= argc) |
|---|
| 44 | return -1; |
|---|
| 45 | |
|---|
| 46 | flag = argv[myoptind]; |
|---|
| 47 | |
|---|
| 48 | if(flag[0] == '-' && flag[1] != '-') |
|---|
| 49 | { |
|---|
| 50 | char *tmp; |
|---|
| 51 | int ret = flag[1]; |
|---|
| 52 | |
|---|
| 53 | if(ret == '\0') |
|---|
| 54 | return -1; |
|---|
| 55 | |
|---|
| 56 | tmp = strchr(optstring, ret); |
|---|
| 57 | if(!tmp || ret == ':') |
|---|
| 58 | return '?'; |
|---|
| 59 | |
|---|
| 60 | myoptind++; |
|---|
| 61 | if(tmp[1] == ':') |
|---|
| 62 | { |
|---|
| 63 | if(flag[2] != '\0') |
|---|
| 64 | myoptarg = flag + 2; |
|---|
| 65 | else |
|---|
| 66 | myoptarg = argv[myoptind++]; |
|---|
| 67 | return ret; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | if(flag[2] != '\0') |
|---|
| 71 | { |
|---|
| 72 | flag[1] = '-'; |
|---|
| 73 | myoptind--; |
|---|
| 74 | argv[myoptind]++; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | return ret; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | if(flag[0] == '-' && flag[1] == '-') |
|---|
| 81 | { |
|---|
| 82 | if(flag[2] == '\0') |
|---|
| 83 | return -1; |
|---|
| 84 | |
|---|
| 85 | for(i = 0; longopts[i].name; i++) |
|---|
| 86 | { |
|---|
| 87 | size_t l = strlen(longopts[i].name); |
|---|
| 88 | |
|---|
| 89 | if(strncmp(flag + 2, longopts[i].name, l)) |
|---|
| 90 | continue; |
|---|
| 91 | |
|---|
| 92 | switch(flag[2 + l]) |
|---|
| 93 | { |
|---|
| 94 | case '=': |
|---|
| 95 | if(!longopts[i].has_arg) |
|---|
| 96 | goto bad_opt; |
|---|
| 97 | if(longindex) |
|---|
| 98 | *longindex = i; |
|---|
| 99 | myoptind++; |
|---|
| 100 | myoptarg = flag + 2 + l + 1; |
|---|
| 101 | return longopts[i].val; |
|---|
| 102 | case '\0': |
|---|
| 103 | if(longindex) |
|---|
| 104 | *longindex = i; |
|---|
| 105 | myoptind++; |
|---|
| 106 | if(longopts[i].has_arg) |
|---|
| 107 | myoptarg = argv[myoptind++]; |
|---|
| 108 | return longopts[i].val; |
|---|
| 109 | default: |
|---|
| 110 | break; |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | bad_opt: |
|---|
| 114 | fprintf(stderr, "%s: unrecognized option `%s'\n", argv[0], flag); |
|---|
| 115 | return '?'; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | return -1; |
|---|
| 119 | } |
|---|
| 120 | |
|---|