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 3551 2009-07-10 14:46:54Z pterjan $ |
---|
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 "caca_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 | if(myoptind >= argc) |
---|
67 | { |
---|
68 | if(tmp[2] != ':') |
---|
69 | { |
---|
70 | fprintf(stderr, "%s: `%s' needs an argument\n", argv[0], flag); |
---|
71 | return -2; |
---|
72 | } |
---|
73 | } |
---|
74 | else |
---|
75 | myoptarg = argv[myoptind++]; |
---|
76 | return ret; |
---|
77 | } |
---|
78 | |
---|
79 | if(flag[2] != '\0') |
---|
80 | { |
---|
81 | flag[1] = '-'; |
---|
82 | myoptind--; |
---|
83 | argv[myoptind]++; |
---|
84 | } |
---|
85 | |
---|
86 | return ret; |
---|
87 | } |
---|
88 | |
---|
89 | if(flag[0] == '-' && flag[1] == '-') |
---|
90 | { |
---|
91 | if(flag[2] == '\0') |
---|
92 | return -1; |
---|
93 | |
---|
94 | for(i = 0; longopts[i].name; i++) |
---|
95 | { |
---|
96 | size_t l = strlen(longopts[i].name); |
---|
97 | |
---|
98 | if(strncmp(flag + 2, longopts[i].name, l)) |
---|
99 | continue; |
---|
100 | |
---|
101 | switch(flag[2 + l]) |
---|
102 | { |
---|
103 | case '=': |
---|
104 | if(!longopts[i].has_arg) |
---|
105 | goto bad_opt; |
---|
106 | if(longindex) |
---|
107 | *longindex = i; |
---|
108 | myoptind++; |
---|
109 | myoptarg = flag + 2 + l + 1; |
---|
110 | return longopts[i].val; |
---|
111 | case '\0': |
---|
112 | if(longindex) |
---|
113 | *longindex = i; |
---|
114 | myoptind++; |
---|
115 | if(longopts[i].has_arg) |
---|
116 | myoptarg = argv[myoptind++]; |
---|
117 | return longopts[i].val; |
---|
118 | default: |
---|
119 | break; |
---|
120 | } |
---|
121 | } |
---|
122 | bad_opt: |
---|
123 | fprintf(stderr, "%s: unrecognized option `%s'\n", argv[0], flag); |
---|
124 | return '?'; |
---|
125 | } |
---|
126 | |
---|
127 | return -1; |
---|
128 | } |
---|
129 | |
---|