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.
45 main(int argc, char *argv[])
47 size_t len, lineno = 0;
48 char *line, *ptr, *optstring = NULL, *result = NULL;
55 while ((line = fparseln(stdin, &len, &lineno, NULL, 0)) != NULL) {
56 if (strncmp(line, "load:", 5) == 0) {
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);
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)
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);
76 args[nargs] = strdup(args[nargs]);
77 while ((args[++nargs] = strtok(NULL, WS)) != NULL)
78 args[nargs] = strdup(args[nargs]);
81 for (i = 0; i < nargs; i++)
82 fprintf(stderr, "argv[%d] = %s\n", i,
85 } else if (strncmp(line, "result:", 7) == 0) {
87 optind = optreset = 1;
90 result = strtok(&line[8], WS);
92 errx(1, "missing result at line %ld",
93 (unsigned long)lineno);
94 result = strdup(result);
96 errx(1, "result: without args:");
98 fprintf(stderr, "result = %s\n", result);
99 while ((c = getopt(nargs, args, optstring)) != -1) {
101 err(1, "`:' found as argument char");
102 if ((ptr = strchr(optstring, c)) == NULL) {
103 snprintf(arg, sizeof(arg), "!%c,", c);
108 snprintf(arg, sizeof(arg), "%c,", c);
110 snprintf(arg, sizeof(arg), "%c=%s,",
122 snprintf(arg, sizeof(arg), "%d", nargs - optind);
124 if (strcmp(buf, result) != 0)
125 errx(1, "`%s' != `%s'", buf, result);