1 /* $NetBSD: h_getopt_long.c,v 1.1 2011/01/01 23:56:49 pgoyette Exp $ */
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
43 #define SKIPWS(p) while (isspace((int)(*p))) p++
47 main(int argc, char *argv[])
49 size_t len, lineno = 0;
50 char *line, *eptr, *longopt, *ptr, *optstring = NULL, *result = NULL;
58 int *longopt_flags = NULL;
59 struct option *longopts = NULL;
61 while ((line = fparseln(stdin, &len, &lineno, NULL, 0)) != NULL) {
62 if (strncmp(line, "optstring:", 10) == 0) {
65 optstring = strtok(&line[11], WS);
66 if (optstring == NULL)
67 errx(1, "missing optstring at line %ld",
68 (unsigned long)lineno);
69 optstring = strdup(optstring);
70 } else if (strncmp(line, "longopts:", 9) == 0) {
73 for (i = 0; i < nlongopts; i++)
74 if (longopts[i].name != NULL)
75 free(__UNCONST(longopts[i].name));
81 ptr = strtok(&line[10], WS);
83 errx(1, "missing longopts at line %ld",
84 (unsigned long)lineno);
85 maxnlongopts = strtoul(ptr, &eptr, 10);
87 warnx("garbage in longopts at line %ld",
88 (unsigned long)lineno);
89 maxnlongopts++; /* space for trailer */
91 (struct option *)calloc(sizeof(struct option),
95 longopt_flags = (int *)calloc(sizeof(int), maxnlongopts);
96 if (longopt_flags == NULL)
98 } else if (strncmp(line, "longopt:", 8) == 0) {
100 errx(1, "longopt: without longopts at line %ld",
101 (unsigned long)lineno);
102 if (nlongopts >= maxnlongopts)
103 errx(1, "longopt: too many options at line %ld",
104 (unsigned long)lineno);
108 longopt = strsep(&ptr, ",");
110 errx(1, "missing longopt at line %ld",
111 (unsigned long)lineno);
112 longopts[nlongopts].name = strdup(longopt);
115 longopt = strsep(&ptr, ",");
116 if (*longopt != '\0') {
117 if (strncmp(longopt, "0", 1) == 0 ||
118 strncmp(longopt, "no_argument", 2) == 0)
119 longopts[nlongopts].has_arg = no_argument;
120 else if (strncmp(longopt, "1", 1) == 0 ||
121 strncmp(longopt, "required_argument", 8) == 0)
122 longopts[nlongopts].has_arg = required_argument;
123 else if (strncmp(longopt, "2", 1) == 0 ||
124 strncmp(longopt, "optional_argument", 8) == 0)
125 longopts[nlongopts].has_arg = optional_argument;
127 errx(1, "unknown has_arg %s at line %ld",
128 longopt, (unsigned long)lineno);
132 longopt = strsep(&ptr, ",");
133 if (*longopt != '\0' &&
134 strncmp(longopt, "NULL", 4) != 0)
135 longopts[nlongopts].flag = &longopt_flags[nlongopts];
138 longopt = strsep(&ptr, ",");
139 if (*longopt == '\0')
140 errx(1, "missing val at line %ld",
141 (unsigned long)lineno);
142 if (*longopt != '\'') {
143 longopts[nlongopts].val =
144 (int)strtoul(longopt, &eptr, 10);
146 errx(1, "invalid val at line %ld",
147 (unsigned long)lineno);
149 longopts[nlongopts].val = (int)longopt[1];
151 } else if (strncmp(line, "args:", 5) == 0) {
152 for (; nargs >= 0; nargs--) {
153 if (args[nargs] != NULL)
156 args[nargs = 0] = strtok(&line[6], WS);
157 if (args[nargs] == NULL)
158 errx(1, "Missing args");
160 args[nargs] = strdup(args[nargs]);
161 while ((args[++nargs] = strtok(NULL, WS)) != NULL)
162 args[nargs] = strdup(args[nargs]);
163 } else if (strncmp(line, "result:", 7) == 0) {
166 optind = optreset = 1;
169 result = strtok(&line[8], WS);
171 errx(1, "missing result at line %ld",
172 (unsigned long)lineno);
173 if (optstring == NULL)
174 errx(1, "result: without optstring");
175 if (longopts == NULL || nlongopts == 0)
176 errx(1, "result: without longopts");
177 result = strdup(result);
179 errx(1, "result: without args");
181 while ((c = getopt_long(nargs, args, optstring,
182 longopts, &li)) != -1) {
184 errx(1, "`:' found as argument char");
186 ptr = strchr(optstring, c);
188 snprintf(arg, sizeof(arg),
194 snprintf(arg, sizeof(arg),
197 snprintf(arg, sizeof(arg),
198 "%c=%s,", c, optarg);
200 switch (longopts[li].has_arg) {
202 snprintf(arg, sizeof(arg), "-%s,",
205 case required_argument:
206 snprintf(arg, sizeof(arg),
208 longopts[li].name, optarg);
210 case optional_argument:
211 snprintf(arg, sizeof(arg),
215 (optarg)? optarg : "");
218 errx(1, "internal error");
232 snprintf(arg, sizeof(arg), "%d", nargs - optind);
234 if (strcmp(buf, result) != 0)
235 errx(1, "`%s' != `%s'", buf, result);
237 errx(1, "unknown directive at line %ld",
238 (unsigned long)lineno);