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