xref: /freebsd/contrib/netbsd-tests/lib/libc/stdlib/h_getopt.c (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1 /*	$NetBSD: h_getopt.c,v 1.1 2011/01/01 23:56:49 pgoyette Exp $	*/
2 
3 /*-
4  * Copyright (c) 2002 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 <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <err.h>
37 #ifdef __FreeBSD__
38 /*
39  * Needed to avoid libutil.h pollution in stdio.h, which causes grief with
40  * with hexdump(3) in lib/libc/db/h_hash.c
41  */
42 #include <libutil.h>
43 #endif
44 
45 #define	WS	"\t\n "
46 #define	debug	0
47 
48 int
49 main(int argc, char *argv[])
50 {
51 	size_t len, lineno = 0;
52 	char *line, *ptr, *optstring = NULL, *result = NULL;
53 	char buf[1024];
54 	char *args[100];
55 	char arg[100];
56 	int nargs = -1;
57 	int c;
58 
59 	while ((line = fparseln(stdin, &len, &lineno, NULL, 0)) != NULL) {
60 		if (strncmp(line, "load:", 5) == 0) {
61 			if (optstring)
62 				free(optstring);
63 			optstring = strtok(&line[6], WS);
64 			if (optstring == NULL)
65 			    errx(1, "missing optstring at line %ld",
66 				(unsigned long)lineno);
67 			optstring = strdup(optstring);
68 			if (debug)
69 				fprintf(stderr, "optstring = %s\n", optstring);
70 		} else if (strncmp(line, "args:", 5) == 0) {
71 			for (; nargs >= 0; nargs--) {
72 				if (args[nargs] != NULL)
73 					free(args[nargs]);
74 			}
75 			args[nargs = 0] = strtok(&line[6], WS);
76 			if (args[nargs] == NULL)
77 				errx(1, "missing args at line %ld",
78 				    (unsigned long)lineno);
79 
80 			args[nargs] = strdup(args[nargs]);
81 			while ((args[++nargs] = strtok(NULL, WS)) != NULL)
82 				args[nargs] = strdup(args[nargs]);
83 			if (debug) {
84 				int i = 0;
85 				for (i = 0; i < nargs; i++)
86 					fprintf(stderr, "argv[%d] = %s\n", i,
87 					    args[i]);
88 			}
89 		} else if (strncmp(line, "result:", 7) == 0) {
90 			buf[0] = '\0';
91 			optind = optreset = 1;
92 			if (result)
93 				free(result);
94 			result = strtok(&line[8], WS);
95 			if (result == NULL)
96 				errx(1, "missing result at line %ld",
97 				    (unsigned long)lineno);
98 			result = strdup(result);
99 			if (nargs == -1)
100 				errx(1, "result: without args:");
101 			if (debug)
102 				fprintf(stderr, "result = %s\n", result);
103 			while ((c = getopt(nargs, args, optstring)) != -1)  {
104 				if (c == ':')
105 					err(1, "`:' found as argument char");
106 				if ((ptr = strchr(optstring, c)) == NULL) {
107 					snprintf(arg, sizeof(arg), "!%c,", c);
108 					strcat(buf, arg);
109 					continue;
110 				}
111 				if (ptr[1] != ':')
112 					snprintf(arg, sizeof(arg), "%c,", c);
113 				else
114 					snprintf(arg, sizeof(arg), "%c=%s,",
115 					    c, optarg);
116 				strcat(buf, arg);
117 			}
118 			len = strlen(buf);
119 			if (len > 0) {
120 				buf[len - 1] = '|';
121 				buf[len] = '\0';
122 			} else {
123 				buf[0] = '|';
124 				buf[1] = '\0';
125 			}
126 			snprintf(arg, sizeof(arg), "%d", nargs - optind);
127 			strcat(buf, arg);
128 			if (strcmp(buf, result) != 0)
129 				errx(1, "`%s' != `%s'", buf, result);
130 		}
131 		free(line);
132 	}
133 	return 0;
134 }
135