xref: /netbsd-src/tests/lib/libc/stdlib/h_getopt_long.c (revision 9a073313c77bab83bb297f5b4ae4fc5e27ee3cdf)
1*9a073313Spgoyette /*	$NetBSD: h_getopt_long.c,v 1.1 2011/01/01 23:56:49 pgoyette Exp $	*/
2*9a073313Spgoyette 
3*9a073313Spgoyette /*-
4*9a073313Spgoyette  * Copyright (c) 2007 The NetBSD Foundation, Inc.
5*9a073313Spgoyette  * All rights reserved.
6*9a073313Spgoyette  *
7*9a073313Spgoyette  * This code is derived from software contributed to The NetBSD Foundation
8*9a073313Spgoyette  * by Christos Zoulas.
9*9a073313Spgoyette  *
10*9a073313Spgoyette  * Redistribution and use in source and binary forms, with or without
11*9a073313Spgoyette  * modification, are permitted provided that the following conditions
12*9a073313Spgoyette  * are met:
13*9a073313Spgoyette  * 1. Redistributions of source code must retain the above copyright
14*9a073313Spgoyette  *    notice, this list of conditions and the following disclaimer.
15*9a073313Spgoyette  * 2. Redistributions in binary form must reproduce the above copyright
16*9a073313Spgoyette  *    notice, this list of conditions and the following disclaimer in the
17*9a073313Spgoyette  *    documentation and/or other materials provided with the distribution.
18*9a073313Spgoyette  *
19*9a073313Spgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*9a073313Spgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*9a073313Spgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*9a073313Spgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*9a073313Spgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*9a073313Spgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*9a073313Spgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*9a073313Spgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*9a073313Spgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*9a073313Spgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*9a073313Spgoyette  * POSSIBILITY OF SUCH DAMAGE.
30*9a073313Spgoyette  */
31*9a073313Spgoyette 
32*9a073313Spgoyette #include <ctype.h>
33*9a073313Spgoyette #include <err.h>
34*9a073313Spgoyette #include <getopt.h>
35*9a073313Spgoyette #include <stdio.h>
36*9a073313Spgoyette #include <string.h>
37*9a073313Spgoyette #include <stdlib.h>
38*9a073313Spgoyette #include <unistd.h>
39*9a073313Spgoyette 
40*9a073313Spgoyette #define SKIPWS(p)	while (isspace((int)(*p))) p++
41*9a073313Spgoyette #define	WS	"\t\n "
42*9a073313Spgoyette 
43*9a073313Spgoyette int
main(int argc,char * argv[])44*9a073313Spgoyette main(int argc, char *argv[])
45*9a073313Spgoyette {
46*9a073313Spgoyette 	size_t len, lineno = 0;
47*9a073313Spgoyette 	char *line, *eptr, *longopt, *ptr, *optstring = NULL, *result = NULL;
48*9a073313Spgoyette 	char buf[1024];
49*9a073313Spgoyette 	char *args[128];
50*9a073313Spgoyette 	char arg[256];
51*9a073313Spgoyette 	int nargs = -1;
52*9a073313Spgoyette 	int c;
53*9a073313Spgoyette 	int nlongopts = 0;
54*9a073313Spgoyette 	int maxnlongopts = 0;
55*9a073313Spgoyette 	int *longopt_flags = NULL;
56*9a073313Spgoyette 	struct option *longopts = NULL;
57*9a073313Spgoyette 
58*9a073313Spgoyette 	while ((line = fparseln(stdin, &len, &lineno, NULL, 0)) != NULL) {
59*9a073313Spgoyette 		if (strncmp(line, "optstring:", 10) == 0) {
60*9a073313Spgoyette 			if (optstring)
61*9a073313Spgoyette 				free(optstring);
62*9a073313Spgoyette 			optstring = strtok(&line[11], WS);
63*9a073313Spgoyette 			if (optstring == NULL)
64*9a073313Spgoyette 			    errx(1, "missing optstring at line %ld",
65*9a073313Spgoyette 				(unsigned long)lineno);
66*9a073313Spgoyette 			optstring = strdup(optstring);
67*9a073313Spgoyette 		} else if (strncmp(line, "longopts:", 9) == 0) {
68*9a073313Spgoyette 			if (longopts) {
69*9a073313Spgoyette 				int i;
70*9a073313Spgoyette 				for (i = 0; i < nlongopts; i++)
71*9a073313Spgoyette 					if (longopts[i].name != NULL)
72*9a073313Spgoyette 						free(__UNCONST(longopts[i].name));
73*9a073313Spgoyette 				free(longopts);
74*9a073313Spgoyette 			}
75*9a073313Spgoyette 			if (longopt_flags)
76*9a073313Spgoyette 				free(longopt_flags);
77*9a073313Spgoyette 			nlongopts = 0;
78*9a073313Spgoyette 			ptr = strtok(&line[10], WS);
79*9a073313Spgoyette 			if (ptr == NULL)
80*9a073313Spgoyette 				errx(1, "missing longopts at line %ld",
81*9a073313Spgoyette 				    (unsigned long)lineno);
82*9a073313Spgoyette 			maxnlongopts = strtoul(ptr, &eptr, 10);
83*9a073313Spgoyette 			if (*eptr != '\0')
84*9a073313Spgoyette 				warnx("garbage in longopts at line %ld",
85*9a073313Spgoyette 				    (unsigned long)lineno);
86*9a073313Spgoyette 			maxnlongopts++;		/* space for trailer */
87*9a073313Spgoyette 			longopts =
88*9a073313Spgoyette 			    (struct option *)calloc(sizeof(struct option),
89*9a073313Spgoyette 						    maxnlongopts);
90*9a073313Spgoyette 			if (longopts == NULL)
91*9a073313Spgoyette 				err(1, "calloc");
92*9a073313Spgoyette 			longopt_flags = (int *)calloc(sizeof(int), maxnlongopts);
93*9a073313Spgoyette 			if (longopt_flags == NULL)
94*9a073313Spgoyette 				err(1, "calloc");
95*9a073313Spgoyette 		} else if (strncmp(line, "longopt:", 8) == 0) {
96*9a073313Spgoyette 			if (longopts == NULL)
97*9a073313Spgoyette 				errx(1, "longopt: without longopts at line %ld",
98*9a073313Spgoyette 				    (unsigned long)lineno);
99*9a073313Spgoyette 			if (nlongopts >= maxnlongopts)
100*9a073313Spgoyette 				errx(1, "longopt: too many options at line %ld",
101*9a073313Spgoyette 				    (unsigned long)lineno);
102*9a073313Spgoyette 			/* name */
103*9a073313Spgoyette 			ptr = &line[9];
104*9a073313Spgoyette 			SKIPWS(ptr);
105*9a073313Spgoyette 			longopt = strsep(&ptr, ",");
106*9a073313Spgoyette 			if (longopt == NULL)
107*9a073313Spgoyette 				errx(1, "missing longopt at line %ld",
108*9a073313Spgoyette 				    (unsigned long)lineno);
109*9a073313Spgoyette 			longopts[nlongopts].name = strdup(longopt);
110*9a073313Spgoyette 			/* has_arg */
111*9a073313Spgoyette 			SKIPWS(ptr);
112*9a073313Spgoyette 			longopt = strsep(&ptr, ",");
113*9a073313Spgoyette 			if (*longopt != '\0') {
114*9a073313Spgoyette 				if (strncmp(longopt, "0", 1) == 0 ||
115*9a073313Spgoyette 				    strncmp(longopt, "no_argument", 2) == 0)
116*9a073313Spgoyette 					longopts[nlongopts].has_arg = no_argument;
117*9a073313Spgoyette 				else if (strncmp(longopt, "1", 1) == 0 ||
118*9a073313Spgoyette 					 strncmp(longopt, "required_argument", 8) == 0)
119*9a073313Spgoyette 					longopts[nlongopts].has_arg = required_argument;
120*9a073313Spgoyette 				else if (strncmp(longopt, "2", 1) == 0 ||
121*9a073313Spgoyette 					 strncmp(longopt, "optional_argument", 8) == 0)
122*9a073313Spgoyette 					longopts[nlongopts].has_arg = optional_argument;
123*9a073313Spgoyette 				else
124*9a073313Spgoyette 					errx(1, "unknown has_arg %s at line %ld",
125*9a073313Spgoyette 					    longopt, (unsigned long)lineno);
126*9a073313Spgoyette 			}
127*9a073313Spgoyette 			/* flag */
128*9a073313Spgoyette 			SKIPWS(ptr);
129*9a073313Spgoyette 			longopt = strsep(&ptr, ",");
130*9a073313Spgoyette 			if (*longopt != '\0' &&
131*9a073313Spgoyette 			    strncmp(longopt, "NULL", 4) != 0)
132*9a073313Spgoyette 				longopts[nlongopts].flag = &longopt_flags[nlongopts];
133*9a073313Spgoyette 			/* val */
134*9a073313Spgoyette 			SKIPWS(ptr);
135*9a073313Spgoyette 			longopt = strsep(&ptr, ",");
136*9a073313Spgoyette 			if (*longopt == '\0')
137*9a073313Spgoyette 				errx(1, "missing val at line %ld",
138*9a073313Spgoyette 				    (unsigned long)lineno);
139*9a073313Spgoyette 			if (*longopt != '\'') {
140*9a073313Spgoyette 				longopts[nlongopts].val =
141*9a073313Spgoyette 			            (int)strtoul(longopt, &eptr, 10);
142*9a073313Spgoyette 				if (*eptr != '\0')
143*9a073313Spgoyette 					errx(1, "invalid val at line %ld",
144*9a073313Spgoyette 					    (unsigned long)lineno);
145*9a073313Spgoyette 			} else
146*9a073313Spgoyette 				longopts[nlongopts].val = (int)longopt[1];
147*9a073313Spgoyette 			nlongopts++;
148*9a073313Spgoyette 		} else if (strncmp(line, "args:", 5) == 0) {
149*9a073313Spgoyette 			for (; nargs >= 0; nargs--) {
150*9a073313Spgoyette 				if (args[nargs] != NULL)
151*9a073313Spgoyette 					free(args[nargs]);
152*9a073313Spgoyette 			}
153*9a073313Spgoyette 			args[nargs = 0] = strtok(&line[6], WS);
154*9a073313Spgoyette 			if (args[nargs] == NULL)
155*9a073313Spgoyette 				errx(1, "Missing args");
156*9a073313Spgoyette 
157*9a073313Spgoyette 			args[nargs] = strdup(args[nargs]);
158*9a073313Spgoyette 			while ((args[++nargs] = strtok(NULL, WS)) != NULL)
159*9a073313Spgoyette 				args[nargs] = strdup(args[nargs]);
160*9a073313Spgoyette 		} else if (strncmp(line, "result:", 7) == 0) {
161*9a073313Spgoyette 			int li;
162*9a073313Spgoyette 			buf[0] = '\0';
163*9a073313Spgoyette 			optind = optreset = 1;
164*9a073313Spgoyette 			if (result)
165*9a073313Spgoyette 				free(result);
166*9a073313Spgoyette 			result = strtok(&line[8], WS);
167*9a073313Spgoyette 			if (result == NULL)
168*9a073313Spgoyette 				errx(1, "missing result at line %ld",
169*9a073313Spgoyette 				    (unsigned long)lineno);
170*9a073313Spgoyette 			if (optstring == NULL)
171*9a073313Spgoyette 				errx(1, "result: without optstring");
172*9a073313Spgoyette 			if (longopts == NULL || nlongopts == 0)
173*9a073313Spgoyette 				errx(1, "result: without longopts");
174*9a073313Spgoyette 			result = strdup(result);
175*9a073313Spgoyette 			if (nargs == -1)
176*9a073313Spgoyette 				errx(1, "result: without args");
177*9a073313Spgoyette 			li = -2;
178*9a073313Spgoyette 			while ((c = getopt_long(nargs, args, optstring,
179*9a073313Spgoyette 				       	longopts, &li)) != -1)  {
180*9a073313Spgoyette 				if (c == ':')
181*9a073313Spgoyette 					errx(1, "`:' found as argument char");
182*9a073313Spgoyette 				if (li == -2) {
183*9a073313Spgoyette 					ptr = strchr(optstring, c);
184*9a073313Spgoyette 					if (ptr == NULL) {
185*9a073313Spgoyette 						snprintf(arg, sizeof(arg),
186*9a073313Spgoyette 						    "!%c,", c);
187*9a073313Spgoyette 						strcat(buf, arg);
188*9a073313Spgoyette 						continue;
189*9a073313Spgoyette 					}
190*9a073313Spgoyette 					if (ptr[1] != ':')
191*9a073313Spgoyette 						snprintf(arg, sizeof(arg),
192*9a073313Spgoyette 						    "%c,", c);
193*9a073313Spgoyette 					else
194*9a073313Spgoyette 						snprintf(arg, sizeof(arg),
195*9a073313Spgoyette 						    "%c=%s,", c, optarg);
196*9a073313Spgoyette 				} else {
197*9a073313Spgoyette 					switch (longopts[li].has_arg) {
198*9a073313Spgoyette 					case no_argument:
199*9a073313Spgoyette 						snprintf(arg, sizeof(arg), "-%s,",
200*9a073313Spgoyette 						    longopts[li].name);
201*9a073313Spgoyette 						break;
202*9a073313Spgoyette 					case required_argument:
203*9a073313Spgoyette 						snprintf(arg, sizeof(arg),
204*9a073313Spgoyette 						    "-%s=%s,",
205*9a073313Spgoyette 						    longopts[li].name, optarg);
206*9a073313Spgoyette 						break;
207*9a073313Spgoyette 					case optional_argument:
208*9a073313Spgoyette 						snprintf(arg, sizeof(arg),
209*9a073313Spgoyette 						    "-%s%s%s,",
210*9a073313Spgoyette 						    longopts[li].name,
211*9a073313Spgoyette 						    (optarg)? "=" : "",
212*9a073313Spgoyette 						    (optarg)? optarg : "");
213*9a073313Spgoyette 						break;
214*9a073313Spgoyette 					default:
215*9a073313Spgoyette 						errx(1, "internal error");
216*9a073313Spgoyette 					}
217*9a073313Spgoyette 				}
218*9a073313Spgoyette 				strcat(buf, arg);
219*9a073313Spgoyette 				li = -2;
220*9a073313Spgoyette 			}
221*9a073313Spgoyette 			len = strlen(buf);
222*9a073313Spgoyette 			if (len > 0) {
223*9a073313Spgoyette 				buf[len - 1] = '|';
224*9a073313Spgoyette 				buf[len] = '\0';
225*9a073313Spgoyette 			} else {
226*9a073313Spgoyette 				buf[0] = '|';
227*9a073313Spgoyette 				buf[1] = '\0';
228*9a073313Spgoyette 			}
229*9a073313Spgoyette 			snprintf(arg, sizeof(arg), "%d", nargs - optind);
230*9a073313Spgoyette 			strcat(buf, arg);
231*9a073313Spgoyette 			if (strcmp(buf, result) != 0)
232*9a073313Spgoyette 				errx(1, "`%s' != `%s'", buf, result);
233*9a073313Spgoyette 		} else
234*9a073313Spgoyette 			errx(1, "unknown directive at line %ld",
235*9a073313Spgoyette 			    (unsigned long)lineno);
236*9a073313Spgoyette 		free(line);
237*9a073313Spgoyette 	}
238*9a073313Spgoyette 	return 0;
239*9a073313Spgoyette }
240