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 /*
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
43  */
44 #include <libutil.h>
45 #endif
46 
47 #define SKIPWS(p)	while (isspace((int)(*p))) p++
48 #define	WS	"\t\n "
49 
50 int
51 main(int argc, char *argv[])
52 {
53 	size_t len, lineno = 0;
54 	char *line, *eptr, *longopt, *ptr, *optstring = NULL, *result = NULL;
55 	char buf[1024];
56 	char *args[128];
57 	char arg[256];
58 	int nargs = -1;
59 	int c;
60 	int nlongopts = 0;
61 	int maxnlongopts = 0;
62 	int *longopt_flags = NULL;
63 	struct option *longopts = NULL;
64 
65 	while ((line = fparseln(stdin, &len, &lineno, NULL, 0)) != NULL) {
66 		if (strncmp(line, "optstring:", 10) == 0) {
67 			if (optstring)
68 				free(optstring);
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) {
75 			if (longopts) {
76 				int i;
77 				for (i = 0; i < nlongopts; i++)
78 					if (longopts[i].name != NULL)
79 						free(__UNCONST(longopts[i].name));
80 				free(longopts);
81 			}
82 			if (longopt_flags)
83 				free(longopt_flags);
84 			nlongopts = 0;
85 			ptr = strtok(&line[10], WS);
86 			if (ptr == NULL)
87 				errx(1, "missing longopts at line %ld",
88 				    (unsigned long)lineno);
89 			maxnlongopts = strtoul(ptr, &eptr, 10);
90 			if (*eptr != '\0')
91 				warnx("garbage in longopts at line %ld",
92 				    (unsigned long)lineno);
93 			maxnlongopts++;		/* space for trailer */
94 			longopts =
95 			    (struct option *)calloc(sizeof(struct option),
96 						    maxnlongopts);
97 			if (longopts == NULL)
98 				err(1, "calloc");
99 			longopt_flags = (int *)calloc(sizeof(int), maxnlongopts);
100 			if (longopt_flags == NULL)
101 				err(1, "calloc");
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);
109 			/* name */
110 			ptr = &line[9];
111 			SKIPWS(ptr);
112 			longopt = strsep(&ptr, ",");
113 			if (longopt == NULL)
114 				errx(1, "missing longopt at line %ld",
115 				    (unsigned long)lineno);
116 			longopts[nlongopts].name = strdup(longopt);
117 			/* has_arg */
118 			SKIPWS(ptr);
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;
130 				else
131 					errx(1, "unknown has_arg %s at line %ld",
132 					    longopt, (unsigned long)lineno);
133 			}
134 			/* flag */
135 			SKIPWS(ptr);
136 			longopt = strsep(&ptr, ",");
137 			if (*longopt != '\0' &&
138 			    strncmp(longopt, "NULL", 4) != 0)
139 				longopts[nlongopts].flag = &longopt_flags[nlongopts];
140 			/* val */
141 			SKIPWS(ptr);
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);
149 				if (*eptr != '\0')
150 					errx(1, "invalid val at line %ld",
151 					    (unsigned long)lineno);
152 			} else
153 				longopts[nlongopts].val = (int)longopt[1];
154 			nlongopts++;
155 		} else if (strncmp(line, "args:", 5) == 0) {
156 			for (; nargs >= 0; nargs--) {
157 				if (args[nargs] != NULL)
158 					free(args[nargs]);
159 			}
160 			args[nargs = 0] = strtok(&line[6], WS);
161 			if (args[nargs] == NULL)
162 				errx(1, "Missing args");
163 
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) {
168 			int li;
169 			buf[0] = '\0';
170 			optind = optreset = 1;
171 			if (result)
172 				free(result);
173 			result = strtok(&line[8], WS);
174 			if (result == NULL)
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);
182 			if (nargs == -1)
183 				errx(1, "result: without args");
184 			li = -2;
185 			while ((c = getopt_long(nargs, args, optstring,
186 				       	longopts, &li)) != -1)  {
187 				if (c == ':')
188 					errx(1, "`:' found as argument char");
189 				if (li == -2) {
190 					ptr = strchr(optstring, c);
191 					if (ptr == NULL) {
192 						snprintf(arg, sizeof(arg),
193 						    "!%c,", c);
194 						strcat(buf, arg);
195 						continue;
196 					}
197 					if (ptr[1] != ':')
198 						snprintf(arg, sizeof(arg),
199 						    "%c,", c);
200 					else
201 						snprintf(arg, sizeof(arg),
202 						    "%c=%s,", c, optarg);
203 				} else {
204 					switch (longopts[li].has_arg) {
205 					case no_argument:
206 						snprintf(arg, sizeof(arg), "-%s,",
207 						    longopts[li].name);
208 						break;
209 					case required_argument:
210 						snprintf(arg, sizeof(arg),
211 						    "-%s=%s,",
212 						    longopts[li].name, optarg);
213 						break;
214 					case optional_argument:
215 						snprintf(arg, sizeof(arg),
216 						    "-%s%s%s,",
217 						    longopts[li].name,
218 						    (optarg)? "=" : "",
219 						    (optarg)? optarg : "");
220 						break;
221 					default:
222 						errx(1, "internal error");
223 					}
224 				}
225 				strcat(buf, arg);
226 				li = -2;
227 			}
228 			len = strlen(buf);
229 			if (len > 0) {
230 				buf[len - 1] = '|';
231 				buf[len] = '\0';
232 			} else {
233 				buf[0] = '|';
234 				buf[1] = '\0';
235 			}
236 			snprintf(arg, sizeof(arg), "%d", nargs - optind);
237 			strcat(buf, arg);
238 			if (strcmp(buf, result) != 0)
239 				errx(1, "`%s' != `%s'", buf, result);
240 		} else
241 			errx(1, "unknown directive at line %ld",
242 			    (unsigned long)lineno);
243 		free(line);
244 	}
245 	return 0;
246 }
247