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