1272343Sngie/*	$NetBSD: h_getopt.c,v 1.1 2011/01/01 23:56:49 pgoyette Exp $	*/
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2002 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * This code is derived from software contributed to The NetBSD Foundation
8272343Sngie * by Christos Zoulas.
9272343Sngie *
10272343Sngie * Redistribution and use in source and binary forms, with or without
11272343Sngie * modification, are permitted provided that the following conditions
12272343Sngie * are met:
13272343Sngie * 1. Redistributions of source code must retain the above copyright
14272343Sngie *    notice, this list of conditions and the following disclaimer.
15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
16272343Sngie *    notice, this list of conditions and the following disclaimer in the
17272343Sngie *    documentation and/or other materials provided with the distribution.
18272343Sngie *
19272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29272343Sngie * POSSIBILITY OF SUCH DAMAGE.
30272343Sngie */
31272343Sngie
32272343Sngie#include <stdio.h>
33272343Sngie#include <string.h>
34272343Sngie#include <stdlib.h>
35272343Sngie#include <unistd.h>
36272343Sngie#include <err.h>
37276478Sngie#ifdef __FreeBSD__
38276478Sngie#include <libutil.h>
39276478Sngie#endif
40272343Sngie
41272343Sngie#define	WS	"\t\n "
42272343Sngie#define	debug	0
43272343Sngie
44272343Sngieint
45272343Sngiemain(int argc, char *argv[])
46272343Sngie{
47272343Sngie	size_t len, lineno = 0;
48272343Sngie	char *line, *ptr, *optstring = NULL, *result = NULL;
49272343Sngie	char buf[1024];
50272343Sngie	char *args[100];
51272343Sngie	char arg[100];
52272343Sngie	int nargs = -1;
53272343Sngie	int c;
54272343Sngie
55272343Sngie	while ((line = fparseln(stdin, &len, &lineno, NULL, 0)) != NULL) {
56272343Sngie		if (strncmp(line, "load:", 5) == 0) {
57272343Sngie			if (optstring)
58272343Sngie				free(optstring);
59272343Sngie			optstring = strtok(&line[6], WS);
60272343Sngie			if (optstring == NULL)
61272343Sngie			    errx(1, "missing optstring at line %ld",
62272343Sngie				(unsigned long)lineno);
63272343Sngie			optstring = strdup(optstring);
64272343Sngie			if (debug)
65272343Sngie				fprintf(stderr, "optstring = %s\n", optstring);
66272343Sngie		} else if (strncmp(line, "args:", 5) == 0) {
67272343Sngie			for (; nargs >= 0; nargs--) {
68272343Sngie				if (args[nargs] != NULL)
69272343Sngie					free(args[nargs]);
70272343Sngie			}
71272343Sngie			args[nargs = 0] = strtok(&line[6], WS);
72272343Sngie			if (args[nargs] == NULL)
73272343Sngie				errx(1, "missing args at line %ld",
74272343Sngie				    (unsigned long)lineno);
75272343Sngie
76272343Sngie			args[nargs] = strdup(args[nargs]);
77272343Sngie			while ((args[++nargs] = strtok(NULL, WS)) != NULL)
78272343Sngie				args[nargs] = strdup(args[nargs]);
79272343Sngie			if (debug) {
80272343Sngie				int i = 0;
81272343Sngie				for (i = 0; i < nargs; i++)
82272343Sngie					fprintf(stderr, "argv[%d] = %s\n", i,
83272343Sngie					    args[i]);
84272343Sngie			}
85272343Sngie		} else if (strncmp(line, "result:", 7) == 0) {
86272343Sngie			buf[0] = '\0';
87272343Sngie			optind = optreset = 1;
88272343Sngie			if (result)
89272343Sngie				free(result);
90272343Sngie			result = strtok(&line[8], WS);
91272343Sngie			if (result == NULL)
92272343Sngie				errx(1, "missing result at line %ld",
93272343Sngie				    (unsigned long)lineno);
94272343Sngie			result = strdup(result);
95272343Sngie			if (nargs == -1)
96272343Sngie				errx(1, "result: without args:");
97272343Sngie			if (debug)
98272343Sngie				fprintf(stderr, "result = %s\n", result);
99272343Sngie			while ((c = getopt(nargs, args, optstring)) != -1)  {
100272343Sngie				if (c == ':')
101272343Sngie					err(1, "`:' found as argument char");
102272343Sngie				if ((ptr = strchr(optstring, c)) == NULL) {
103272343Sngie					snprintf(arg, sizeof(arg), "!%c,", c);
104272343Sngie					strcat(buf, arg);
105272343Sngie					continue;
106272343Sngie				}
107272343Sngie				if (ptr[1] != ':')
108272343Sngie					snprintf(arg, sizeof(arg), "%c,", c);
109272343Sngie				else
110272343Sngie					snprintf(arg, sizeof(arg), "%c=%s,",
111272343Sngie					    c, optarg);
112272343Sngie				strcat(buf, arg);
113272343Sngie			}
114272343Sngie			len = strlen(buf);
115272343Sngie			if (len > 0) {
116272343Sngie				buf[len - 1] = '|';
117272343Sngie				buf[len] = '\0';
118272343Sngie			} else {
119272343Sngie				buf[0] = '|';
120272343Sngie				buf[1] = '\0';
121272343Sngie			}
122272343Sngie			snprintf(arg, sizeof(arg), "%d", nargs - optind);
123272343Sngie			strcat(buf, arg);
124272343Sngie			if (strcmp(buf, result) != 0)
125272343Sngie				errx(1, "`%s' != `%s'", buf, result);
126272343Sngie		}
127272343Sngie		free(line);
128272343Sngie	}
129272343Sngie	return 0;
130272343Sngie}
131