xref: /freebsd/contrib/netbsd-tests/lib/libc/stdlib/h_getopt_long.c (revision 4436b51dff5736e74da464946049ea6899a88938)
1 /*	$NetBSD: h_getopt_long.c,v 1.1 2011/01/01 23:56:49 pgoyette Exp $	*/
2 
3 /*-
4  * Copyright (c) 2007 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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.
30  */
31 
32 #include <ctype.h>
33 #include <err.h>
34 #include <getopt.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #ifdef __FreeBSD__
40 #include <libutil.h>
41 #endif
42 
43 #define SKIPWS(p)	while (isspace((int)(*p))) p++
44 #define	WS	"\t\n "
45 
46 int
47 main(int argc, char *argv[])
48 {
49 	size_t len, lineno = 0;
50 	char *line, *eptr, *longopt, *ptr, *optstring = NULL, *result = NULL;
51 	char buf[1024];
52 	char *args[128];
53 	char arg[256];
54 	int nargs = -1;
55 	int c;
56 	int nlongopts = 0;
57 	int maxnlongopts = 0;
58 	int *longopt_flags = NULL;
59 	struct option *longopts = NULL;
60 
61 	while ((line = fparseln(stdin, &len, &lineno, NULL, 0)) != NULL) {
62 		if (strncmp(line, "optstring:", 10) == 0) {
63 			if (optstring)
64 				free(optstring);
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) {
71 			if (longopts) {
72 				int i;
73 				for (i = 0; i < nlongopts; i++)
74 					if (longopts[i].name != NULL)
75 						free(__UNCONST(longopts[i].name));
76 				free(longopts);
77 			}
78 			if (longopt_flags)
79 				free(longopt_flags);
80 			nlongopts = 0;
81 			ptr = strtok(&line[10], WS);
82 			if (ptr == NULL)
83 				errx(1, "missing longopts at line %ld",
84 				    (unsigned long)lineno);
85 			maxnlongopts = strtoul(ptr, &eptr, 10);
86 			if (*eptr != '\0')
87 				warnx("garbage in longopts at line %ld",
88 				    (unsigned long)lineno);
89 			maxnlongopts++;		/* space for trailer */
90 			longopts =
91 			    (struct option *)calloc(sizeof(struct option),
92 						    maxnlongopts);
93 			if (longopts == NULL)
94 				err(1, "calloc");
95 			longopt_flags = (int *)calloc(sizeof(int), maxnlongopts);
96 			if (longopt_flags == NULL)
97 				err(1, "calloc");
98 		} else if (strncmp(line, "longopt:", 8) == 0) {
99 			if (longopts == NULL)
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);
105 			/* name */
106 			ptr = &line[9];
107 			SKIPWS(ptr);
108 			longopt = strsep(&ptr, ",");
109 			if (longopt == NULL)
110 				errx(1, "missing longopt at line %ld",
111 				    (unsigned long)lineno);
112 			longopts[nlongopts].name = strdup(longopt);
113 			/* has_arg */
114 			SKIPWS(ptr);
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;
126 				else
127 					errx(1, "unknown has_arg %s at line %ld",
128 					    longopt, (unsigned long)lineno);
129 			}
130 			/* flag */
131 			SKIPWS(ptr);
132 			longopt = strsep(&ptr, ",");
133 			if (*longopt != '\0' &&
134 			    strncmp(longopt, "NULL", 4) != 0)
135 				longopts[nlongopts].flag = &longopt_flags[nlongopts];
136 			/* val */
137 			SKIPWS(ptr);
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);
145 				if (*eptr != '\0')
146 					errx(1, "invalid val at line %ld",
147 					    (unsigned long)lineno);
148 			} else
149 				longopts[nlongopts].val = (int)longopt[1];
150 			nlongopts++;
151 		} else if (strncmp(line, "args:", 5) == 0) {
152 			for (; nargs >= 0; nargs--) {
153 				if (args[nargs] != NULL)
154 					free(args[nargs]);
155 			}
156 			args[nargs = 0] = strtok(&line[6], WS);
157 			if (args[nargs] == NULL)
158 				errx(1, "Missing args");
159 
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) {
164 			int li;
165 			buf[0] = '\0';
166 			optind = optreset = 1;
167 			if (result)
168 				free(result);
169 			result = strtok(&line[8], WS);
170 			if (result == NULL)
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);
178 			if (nargs == -1)
179 				errx(1, "result: without args");
180 			li = -2;
181 			while ((c = getopt_long(nargs, args, optstring,
182 				       	longopts, &li)) != -1)  {
183 				if (c == ':')
184 					errx(1, "`:' found as argument char");
185 				if (li == -2) {
186 					ptr = strchr(optstring, c);
187 					if (ptr == NULL) {
188 						snprintf(arg, sizeof(arg),
189 						    "!%c,", c);
190 						strcat(buf, arg);
191 						continue;
192 					}
193 					if (ptr[1] != ':')
194 						snprintf(arg, sizeof(arg),
195 						    "%c,", c);
196 					else
197 						snprintf(arg, sizeof(arg),
198 						    "%c=%s,", c, optarg);
199 				} else {
200 					switch (longopts[li].has_arg) {
201 					case no_argument:
202 						snprintf(arg, sizeof(arg), "-%s,",
203 						    longopts[li].name);
204 						break;
205 					case required_argument:
206 						snprintf(arg, sizeof(arg),
207 						    "-%s=%s,",
208 						    longopts[li].name, optarg);
209 						break;
210 					case optional_argument:
211 						snprintf(arg, sizeof(arg),
212 						    "-%s%s%s,",
213 						    longopts[li].name,
214 						    (optarg)? "=" : "",
215 						    (optarg)? optarg : "");
216 						break;
217 					default:
218 						errx(1, "internal error");
219 					}
220 				}
221 				strcat(buf, arg);
222 				li = -2;
223 			}
224 			len = strlen(buf);
225 			if (len > 0) {
226 				buf[len - 1] = '|';
227 				buf[len] = '\0';
228 			} else {
229 				buf[0] = '|';
230 				buf[1] = '\0';
231 			}
232 			snprintf(arg, sizeof(arg), "%d", nargs - optind);
233 			strcat(buf, arg);
234 			if (strcmp(buf, result) != 0)
235 				errx(1, "`%s' != `%s'", buf, result);
236 		} else
237 			errx(1, "unknown directive at line %ld",
238 			    (unsigned long)lineno);
239 		free(line);
240 	}
241 	return 0;
242 }
243