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