1 /* $NetBSD: h_getopt.c,v 1.1 2011/01/01 23:56:49 pgoyette Exp $ */
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
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
49 main(int argc, char *argv[])
51 size_t len, lineno = 0;
52 char *line, *ptr, *optstring = NULL, *result = NULL;
59 while ((line = fparseln(stdin, &len, &lineno, NULL, 0)) != NULL) {
60 if (strncmp(line, "load:", 5) == 0) {
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);
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)
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);
80 args[nargs] = strdup(args[nargs]);
81 while ((args[++nargs] = strtok(NULL, WS)) != NULL)
82 args[nargs] = strdup(args[nargs]);
85 for (i = 0; i < nargs; i++)
86 fprintf(stderr, "argv[%d] = %s\n", i,
89 } else if (strncmp(line, "result:", 7) == 0) {
91 optind = optreset = 1;
94 result = strtok(&line[8], WS);
96 errx(1, "missing result at line %ld",
97 (unsigned long)lineno);
98 result = strdup(result);
100 errx(1, "result: without args:");
102 fprintf(stderr, "result = %s\n", result);
103 while ((c = getopt(nargs, args, optstring)) != -1) {
105 err(1, "`:' found as argument char");
106 if ((ptr = strchr(optstring, c)) == NULL) {
107 snprintf(arg, sizeof(arg), "!%c,", c);
112 snprintf(arg, sizeof(arg), "%c,", c);
114 snprintf(arg, sizeof(arg), "%c=%s,",
126 snprintf(arg, sizeof(arg), "%d", nargs - optind);
128 if (strcmp(buf, result) != 0)
129 errx(1, "`%s' != `%s'", buf, result);