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.
41 * Needed to avoid libutil.h pollution in stdio.h, which causes grief with
42 * with hexdump(3) in lib/libc/db/h_hash.c
47 #define SKIPWS(p) while (isspace((int)(*p))) p++
51 main(int argc, char *argv[])
53 size_t len, lineno = 0;
54 char *line, *eptr, *longopt, *ptr, *optstring = NULL, *result = NULL;
62 int *longopt_flags = NULL;
63 struct option *longopts = NULL;
65 while ((line = fparseln(stdin, &len, &lineno, NULL, 0)) != NULL) {
66 if (strncmp(line, "optstring:", 10) == 0) {
69 optstring = strtok(&line[11], WS);
70 if (optstring == NULL)
71 errx(1, "missing optstring at line %ld",
72 (unsigned long)lineno);
73 optstring = strdup(optstring);
74 } else if (strncmp(line, "longopts:", 9) == 0) {
77 for (i = 0; i < nlongopts; i++)
78 if (longopts[i].name != NULL)
79 free(__UNCONST(longopts[i].name));
85 ptr = strtok(&line[10], WS);
87 errx(1, "missing longopts at line %ld",
88 (unsigned long)lineno);
89 maxnlongopts = strtoul(ptr, &eptr, 10);
91 warnx("garbage in longopts at line %ld",
92 (unsigned long)lineno);
93 maxnlongopts++; /* space for trailer */
95 (struct option *)calloc(sizeof(struct option),
99 longopt_flags = (int *)calloc(sizeof(int), maxnlongopts);
100 if (longopt_flags == NULL)
102 } else if (strncmp(line, "longopt:", 8) == 0) {
103 if (longopts == NULL)
104 errx(1, "longopt: without longopts at line %ld",
105 (unsigned long)lineno);
106 if (nlongopts >= maxnlongopts)
107 errx(1, "longopt: too many options at line %ld",
108 (unsigned long)lineno);
112 longopt = strsep(&ptr, ",");
114 errx(1, "missing longopt at line %ld",
115 (unsigned long)lineno);
116 longopts[nlongopts].name = strdup(longopt);
119 longopt = strsep(&ptr, ",");
120 if (*longopt != '\0') {
121 if (strncmp(longopt, "0", 1) == 0 ||
122 strncmp(longopt, "no_argument", 2) == 0)
123 longopts[nlongopts].has_arg = no_argument;
124 else if (strncmp(longopt, "1", 1) == 0 ||
125 strncmp(longopt, "required_argument", 8) == 0)
126 longopts[nlongopts].has_arg = required_argument;
127 else if (strncmp(longopt, "2", 1) == 0 ||
128 strncmp(longopt, "optional_argument", 8) == 0)
129 longopts[nlongopts].has_arg = optional_argument;
131 errx(1, "unknown has_arg %s at line %ld",
132 longopt, (unsigned long)lineno);
136 longopt = strsep(&ptr, ",");
137 if (*longopt != '\0' &&
138 strncmp(longopt, "NULL", 4) != 0)
139 longopts[nlongopts].flag = &longopt_flags[nlongopts];
142 longopt = strsep(&ptr, ",");
143 if (*longopt == '\0')
144 errx(1, "missing val at line %ld",
145 (unsigned long)lineno);
146 if (*longopt != '\'') {
147 longopts[nlongopts].val =
148 (int)strtoul(longopt, &eptr, 10);
150 errx(1, "invalid val at line %ld",
151 (unsigned long)lineno);
153 longopts[nlongopts].val = (int)longopt[1];
155 } else if (strncmp(line, "args:", 5) == 0) {
156 for (; nargs >= 0; nargs--) {
157 if (args[nargs] != NULL)
160 args[nargs = 0] = strtok(&line[6], WS);
161 if (args[nargs] == NULL)
162 errx(1, "Missing args");
164 args[nargs] = strdup(args[nargs]);
165 while ((args[++nargs] = strtok(NULL, WS)) != NULL)
166 args[nargs] = strdup(args[nargs]);
167 } else if (strncmp(line, "result:", 7) == 0) {
170 optind = optreset = 1;
173 result = strtok(&line[8], WS);
175 errx(1, "missing result at line %ld",
176 (unsigned long)lineno);
177 if (optstring == NULL)
178 errx(1, "result: without optstring");
179 if (longopts == NULL || nlongopts == 0)
180 errx(1, "result: without longopts");
181 result = strdup(result);
183 errx(1, "result: without args");
185 while ((c = getopt_long(nargs, args, optstring,
186 longopts, &li)) != -1) {
188 errx(1, "`:' found as argument char");
190 ptr = strchr(optstring, c);
192 snprintf(arg, sizeof(arg),
198 snprintf(arg, sizeof(arg),
201 snprintf(arg, sizeof(arg),
202 "%c=%s,", c, optarg);
204 switch (longopts[li].has_arg) {
206 snprintf(arg, sizeof(arg), "-%s,",
209 case required_argument:
210 snprintf(arg, sizeof(arg),
212 longopts[li].name, optarg);
214 case optional_argument:
215 snprintf(arg, sizeof(arg),
219 (optarg)? optarg : "");
222 errx(1, "internal error");
236 snprintf(arg, sizeof(arg), "%d", nargs - optind);
238 if (strcmp(buf, result) != 0)
239 errx(1, "`%s' != `%s'", buf, result);
241 errx(1, "unknown directive at line %ld",
242 (unsigned long)lineno);