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.
42 main(int argc
, char *argv
[])
44 size_t len
, lineno
= 0;
45 char *line
, *ptr
, *optstring
= NULL
, *result
= NULL
;
52 while ((line
= fparseln(stdin
, &len
, &lineno
, NULL
, 0)) != NULL
) {
53 if (strncmp(line
, "load:", 5) == 0) {
56 optstring
= strtok(&line
[6], WS
);
57 if (optstring
== NULL
)
58 errx(1, "missing optstring at line %ld",
59 (unsigned long)lineno
);
60 optstring
= strdup(optstring
);
62 fprintf(stderr
, "optstring = %s\n", optstring
);
63 } else if (strncmp(line
, "args:", 5) == 0) {
64 for (; nargs
>= 0; nargs
--) {
65 if (args
[nargs
] != NULL
)
68 args
[nargs
= 0] = strtok(&line
[6], WS
);
69 if (args
[nargs
] == NULL
)
70 errx(1, "missing args at line %ld",
71 (unsigned long)lineno
);
73 args
[nargs
] = strdup(args
[nargs
]);
74 while ((args
[++nargs
] = strtok(NULL
, WS
)) != NULL
)
75 args
[nargs
] = strdup(args
[nargs
]);
78 for (i
= 0; i
< nargs
; i
++)
79 fprintf(stderr
, "argv[%d] = %s\n", i
,
82 } else if (strncmp(line
, "result:", 7) == 0) {
84 optind
= optreset
= 1;
87 result
= strtok(&line
[8], WS
);
89 errx(1, "missing result at line %ld",
90 (unsigned long)lineno
);
91 result
= strdup(result
);
93 errx(1, "result: without args:");
95 fprintf(stderr
, "result = %s\n", result
);
96 while ((c
= getopt(nargs
, args
, optstring
)) != -1) {
98 err(1, "`:' found as argument char");
99 if ((ptr
= strchr(optstring
, c
)) == NULL
) {
100 snprintf(arg
, sizeof(arg
), "!%c,", c
);
105 snprintf(arg
, sizeof(arg
), "%c,", c
);
107 snprintf(arg
, sizeof(arg
), "%c=%s,",
119 snprintf(arg
, sizeof(arg
), "%d", nargs
- optind
);
121 if (strcmp(buf
, result
) != 0)
122 errx(1, "`%s' != `%s'", buf
, result
);