1 /* $NetBSD: h_getopt.c,v 1.1 2011/01/01 23:56:49 pgoyette Exp $ */ 2 3 /*- 4 * Copyright (c) 2002 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 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. 18 * 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. 30 */ 31 32 #include <stdio.h> 33 #include <string.h> 34 #include <stdlib.h> 35 #include <unistd.h> 36 #include <err.h> 37 38 #define WS "\t\n " 39 #define debug 0 40 41 int 42 main(int argc, char *argv[]) 43 { 44 size_t len, lineno = 0; 45 char *line, *ptr, *optstring = NULL, *result = NULL; 46 char buf[1024]; 47 char *args[100]; 48 char arg[100]; 49 int nargs = -1; 50 int c; 51 52 while ((line = fparseln(stdin, &len, &lineno, NULL, 0)) != NULL) { 53 if (strncmp(line, "load:", 5) == 0) { 54 if (optstring) 55 free(optstring); 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); 61 if (debug) 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) 66 free(args[nargs]); 67 } 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); 72 73 args[nargs] = strdup(args[nargs]); 74 while ((args[++nargs] = strtok(NULL, WS)) != NULL) 75 args[nargs] = strdup(args[nargs]); 76 if (debug) { 77 int i = 0; 78 for (i = 0; i < nargs; i++) 79 fprintf(stderr, "argv[%d] = %s\n", i, 80 args[i]); 81 } 82 } else if (strncmp(line, "result:", 7) == 0) { 83 buf[0] = '\0'; 84 optind = optreset = 1; 85 if (result) 86 free(result); 87 result = strtok(&line[8], WS); 88 if (result == NULL) 89 errx(1, "missing result at line %ld", 90 (unsigned long)lineno); 91 result = strdup(result); 92 if (nargs == -1) 93 errx(1, "result: without args:"); 94 if (debug) 95 fprintf(stderr, "result = %s\n", result); 96 while ((c = getopt(nargs, args, optstring)) != -1) { 97 if (c == ':') 98 err(1, "`:' found as argument char"); 99 if ((ptr = strchr(optstring, c)) == NULL) { 100 snprintf(arg, sizeof(arg), "!%c,", c); 101 strcat(buf, arg); 102 continue; 103 } 104 if (ptr[1] != ':') 105 snprintf(arg, sizeof(arg), "%c,", c); 106 else 107 snprintf(arg, sizeof(arg), "%c=%s,", 108 c, optarg); 109 strcat(buf, arg); 110 } 111 len = strlen(buf); 112 if (len > 0) { 113 buf[len - 1] = '|'; 114 buf[len] = '\0'; 115 } else { 116 buf[0] = '|'; 117 buf[1] = '\0'; 118 } 119 snprintf(arg, sizeof(arg), "%d", nargs - optind); 120 strcat(buf, arg); 121 if (strcmp(buf, result) != 0) 122 errx(1, "`%s' != `%s'", buf, result); 123 } 124 free(line); 125 } 126 return 0; 127 } 128