157718be8SEnji Cooper /* $NetBSD: h_getopt.c,v 1.1 2011/01/01 23:56:49 pgoyette Exp $ */ 257718be8SEnji Cooper 357718be8SEnji Cooper /*- 457718be8SEnji Cooper * Copyright (c) 2002 The NetBSD Foundation, Inc. 557718be8SEnji Cooper * All rights reserved. 657718be8SEnji Cooper * 757718be8SEnji Cooper * This code is derived from software contributed to The NetBSD Foundation 857718be8SEnji Cooper * by Christos Zoulas. 957718be8SEnji Cooper * 1057718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without 1157718be8SEnji Cooper * modification, are permitted provided that the following conditions 1257718be8SEnji Cooper * are met: 1357718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright 1457718be8SEnji Cooper * notice, this list of conditions and the following disclaimer. 1557718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright 1657718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the 1757718be8SEnji Cooper * documentation and/or other materials provided with the distribution. 1857718be8SEnji Cooper * 1957718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 2057718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2157718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2257718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 2357718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2457718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2557718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2657718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2757718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2857718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2957718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE. 3057718be8SEnji Cooper */ 3157718be8SEnji Cooper 3257718be8SEnji Cooper #include <stdio.h> 3357718be8SEnji Cooper #include <string.h> 3457718be8SEnji Cooper #include <stdlib.h> 3557718be8SEnji Cooper #include <unistd.h> 3657718be8SEnji Cooper #include <err.h> 37*6fa64778SEnji Cooper #ifdef __FreeBSD__ 38*6fa64778SEnji Cooper /* 39*6fa64778SEnji Cooper * Needed to avoid libutil.h pollution in stdio.h, which causes grief with 40*6fa64778SEnji Cooper * with hexdump(3) in lib/libc/db/h_hash.c 41*6fa64778SEnji Cooper */ 42*6fa64778SEnji Cooper #include <libutil.h> 43*6fa64778SEnji Cooper #endif 4457718be8SEnji Cooper 4557718be8SEnji Cooper #define WS "\t\n " 4657718be8SEnji Cooper #define debug 0 4757718be8SEnji Cooper 4857718be8SEnji Cooper int 4957718be8SEnji Cooper main(int argc, char *argv[]) 5057718be8SEnji Cooper { 5157718be8SEnji Cooper size_t len, lineno = 0; 5257718be8SEnji Cooper char *line, *ptr, *optstring = NULL, *result = NULL; 5357718be8SEnji Cooper char buf[1024]; 5457718be8SEnji Cooper char *args[100]; 5557718be8SEnji Cooper char arg[100]; 5657718be8SEnji Cooper int nargs = -1; 5757718be8SEnji Cooper int c; 5857718be8SEnji Cooper 5957718be8SEnji Cooper while ((line = fparseln(stdin, &len, &lineno, NULL, 0)) != NULL) { 6057718be8SEnji Cooper if (strncmp(line, "load:", 5) == 0) { 6157718be8SEnji Cooper if (optstring) 6257718be8SEnji Cooper free(optstring); 6357718be8SEnji Cooper optstring = strtok(&line[6], WS); 6457718be8SEnji Cooper if (optstring == NULL) 6557718be8SEnji Cooper errx(1, "missing optstring at line %ld", 6657718be8SEnji Cooper (unsigned long)lineno); 6757718be8SEnji Cooper optstring = strdup(optstring); 6857718be8SEnji Cooper if (debug) 6957718be8SEnji Cooper fprintf(stderr, "optstring = %s\n", optstring); 7057718be8SEnji Cooper } else if (strncmp(line, "args:", 5) == 0) { 7157718be8SEnji Cooper for (; nargs >= 0; nargs--) { 7257718be8SEnji Cooper if (args[nargs] != NULL) 7357718be8SEnji Cooper free(args[nargs]); 7457718be8SEnji Cooper } 7557718be8SEnji Cooper args[nargs = 0] = strtok(&line[6], WS); 7657718be8SEnji Cooper if (args[nargs] == NULL) 7757718be8SEnji Cooper errx(1, "missing args at line %ld", 7857718be8SEnji Cooper (unsigned long)lineno); 7957718be8SEnji Cooper 8057718be8SEnji Cooper args[nargs] = strdup(args[nargs]); 8157718be8SEnji Cooper while ((args[++nargs] = strtok(NULL, WS)) != NULL) 8257718be8SEnji Cooper args[nargs] = strdup(args[nargs]); 8357718be8SEnji Cooper if (debug) { 8457718be8SEnji Cooper int i = 0; 8557718be8SEnji Cooper for (i = 0; i < nargs; i++) 8657718be8SEnji Cooper fprintf(stderr, "argv[%d] = %s\n", i, 8757718be8SEnji Cooper args[i]); 8857718be8SEnji Cooper } 8957718be8SEnji Cooper } else if (strncmp(line, "result:", 7) == 0) { 9057718be8SEnji Cooper buf[0] = '\0'; 9157718be8SEnji Cooper optind = optreset = 1; 9257718be8SEnji Cooper if (result) 9357718be8SEnji Cooper free(result); 9457718be8SEnji Cooper result = strtok(&line[8], WS); 9557718be8SEnji Cooper if (result == NULL) 9657718be8SEnji Cooper errx(1, "missing result at line %ld", 9757718be8SEnji Cooper (unsigned long)lineno); 9857718be8SEnji Cooper result = strdup(result); 9957718be8SEnji Cooper if (nargs == -1) 10057718be8SEnji Cooper errx(1, "result: without args:"); 10157718be8SEnji Cooper if (debug) 10257718be8SEnji Cooper fprintf(stderr, "result = %s\n", result); 10357718be8SEnji Cooper while ((c = getopt(nargs, args, optstring)) != -1) { 10457718be8SEnji Cooper if (c == ':') 10557718be8SEnji Cooper err(1, "`:' found as argument char"); 10657718be8SEnji Cooper if ((ptr = strchr(optstring, c)) == NULL) { 10757718be8SEnji Cooper snprintf(arg, sizeof(arg), "!%c,", c); 10857718be8SEnji Cooper strcat(buf, arg); 10957718be8SEnji Cooper continue; 11057718be8SEnji Cooper } 11157718be8SEnji Cooper if (ptr[1] != ':') 11257718be8SEnji Cooper snprintf(arg, sizeof(arg), "%c,", c); 11357718be8SEnji Cooper else 11457718be8SEnji Cooper snprintf(arg, sizeof(arg), "%c=%s,", 11557718be8SEnji Cooper c, optarg); 11657718be8SEnji Cooper strcat(buf, arg); 11757718be8SEnji Cooper } 11857718be8SEnji Cooper len = strlen(buf); 11957718be8SEnji Cooper if (len > 0) { 12057718be8SEnji Cooper buf[len - 1] = '|'; 12157718be8SEnji Cooper buf[len] = '\0'; 12257718be8SEnji Cooper } else { 12357718be8SEnji Cooper buf[0] = '|'; 12457718be8SEnji Cooper buf[1] = '\0'; 12557718be8SEnji Cooper } 12657718be8SEnji Cooper snprintf(arg, sizeof(arg), "%d", nargs - optind); 12757718be8SEnji Cooper strcat(buf, arg); 12857718be8SEnji Cooper if (strcmp(buf, result) != 0) 12957718be8SEnji Cooper errx(1, "`%s' != `%s'", buf, result); 13057718be8SEnji Cooper } 13157718be8SEnji Cooper free(line); 13257718be8SEnji Cooper } 13357718be8SEnji Cooper return 0; 13457718be8SEnji Cooper } 135