1 1.1 pgoyette /* $NetBSD: h_getopt.c,v 1.1 2011/01/01 23:56:49 pgoyette Exp $ */ 2 1.1 pgoyette 3 1.1 pgoyette /*- 4 1.1 pgoyette * Copyright (c) 2002 The NetBSD Foundation, Inc. 5 1.1 pgoyette * All rights reserved. 6 1.1 pgoyette * 7 1.1 pgoyette * This code is derived from software contributed to The NetBSD Foundation 8 1.1 pgoyette * by Christos Zoulas. 9 1.1 pgoyette * 10 1.1 pgoyette * Redistribution and use in source and binary forms, with or without 11 1.1 pgoyette * modification, are permitted provided that the following conditions 12 1.1 pgoyette * are met: 13 1.1 pgoyette * 1. Redistributions of source code must retain the above copyright 14 1.1 pgoyette * notice, this list of conditions and the following disclaimer. 15 1.1 pgoyette * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 pgoyette * notice, this list of conditions and the following disclaimer in the 17 1.1 pgoyette * documentation and/or other materials provided with the distribution. 18 1.1 pgoyette * 19 1.1 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 pgoyette * POSSIBILITY OF SUCH DAMAGE. 30 1.1 pgoyette */ 31 1.1 pgoyette 32 1.1 pgoyette #include <stdio.h> 33 1.1 pgoyette #include <string.h> 34 1.1 pgoyette #include <stdlib.h> 35 1.1 pgoyette #include <unistd.h> 36 1.1 pgoyette #include <err.h> 37 1.1 pgoyette 38 1.1 pgoyette #define WS "\t\n " 39 1.1 pgoyette #define debug 0 40 1.1 pgoyette 41 1.1 pgoyette int 42 1.1 pgoyette main(int argc, char *argv[]) 43 1.1 pgoyette { 44 1.1 pgoyette size_t len, lineno = 0; 45 1.1 pgoyette char *line, *ptr, *optstring = NULL, *result = NULL; 46 1.1 pgoyette char buf[1024]; 47 1.1 pgoyette char *args[100]; 48 1.1 pgoyette char arg[100]; 49 1.1 pgoyette int nargs = -1; 50 1.1 pgoyette int c; 51 1.1 pgoyette 52 1.1 pgoyette while ((line = fparseln(stdin, &len, &lineno, NULL, 0)) != NULL) { 53 1.1 pgoyette if (strncmp(line, "load:", 5) == 0) { 54 1.1 pgoyette if (optstring) 55 1.1 pgoyette free(optstring); 56 1.1 pgoyette optstring = strtok(&line[6], WS); 57 1.1 pgoyette if (optstring == NULL) 58 1.1 pgoyette errx(1, "missing optstring at line %ld", 59 1.1 pgoyette (unsigned long)lineno); 60 1.1 pgoyette optstring = strdup(optstring); 61 1.1 pgoyette if (debug) 62 1.1 pgoyette fprintf(stderr, "optstring = %s\n", optstring); 63 1.1 pgoyette } else if (strncmp(line, "args:", 5) == 0) { 64 1.1 pgoyette for (; nargs >= 0; nargs--) { 65 1.1 pgoyette if (args[nargs] != NULL) 66 1.1 pgoyette free(args[nargs]); 67 1.1 pgoyette } 68 1.1 pgoyette args[nargs = 0] = strtok(&line[6], WS); 69 1.1 pgoyette if (args[nargs] == NULL) 70 1.1 pgoyette errx(1, "missing args at line %ld", 71 1.1 pgoyette (unsigned long)lineno); 72 1.1 pgoyette 73 1.1 pgoyette args[nargs] = strdup(args[nargs]); 74 1.1 pgoyette while ((args[++nargs] = strtok(NULL, WS)) != NULL) 75 1.1 pgoyette args[nargs] = strdup(args[nargs]); 76 1.1 pgoyette if (debug) { 77 1.1 pgoyette int i = 0; 78 1.1 pgoyette for (i = 0; i < nargs; i++) 79 1.1 pgoyette fprintf(stderr, "argv[%d] = %s\n", i, 80 1.1 pgoyette args[i]); 81 1.1 pgoyette } 82 1.1 pgoyette } else if (strncmp(line, "result:", 7) == 0) { 83 1.1 pgoyette buf[0] = '\0'; 84 1.1 pgoyette optind = optreset = 1; 85 1.1 pgoyette if (result) 86 1.1 pgoyette free(result); 87 1.1 pgoyette result = strtok(&line[8], WS); 88 1.1 pgoyette if (result == NULL) 89 1.1 pgoyette errx(1, "missing result at line %ld", 90 1.1 pgoyette (unsigned long)lineno); 91 1.1 pgoyette result = strdup(result); 92 1.1 pgoyette if (nargs == -1) 93 1.1 pgoyette errx(1, "result: without args:"); 94 1.1 pgoyette if (debug) 95 1.1 pgoyette fprintf(stderr, "result = %s\n", result); 96 1.1 pgoyette while ((c = getopt(nargs, args, optstring)) != -1) { 97 1.1 pgoyette if (c == ':') 98 1.1 pgoyette err(1, "`:' found as argument char"); 99 1.1 pgoyette if ((ptr = strchr(optstring, c)) == NULL) { 100 1.1 pgoyette snprintf(arg, sizeof(arg), "!%c,", c); 101 1.1 pgoyette strcat(buf, arg); 102 1.1 pgoyette continue; 103 1.1 pgoyette } 104 1.1 pgoyette if (ptr[1] != ':') 105 1.1 pgoyette snprintf(arg, sizeof(arg), "%c,", c); 106 1.1 pgoyette else 107 1.1 pgoyette snprintf(arg, sizeof(arg), "%c=%s,", 108 1.1 pgoyette c, optarg); 109 1.1 pgoyette strcat(buf, arg); 110 1.1 pgoyette } 111 1.1 pgoyette len = strlen(buf); 112 1.1 pgoyette if (len > 0) { 113 1.1 pgoyette buf[len - 1] = '|'; 114 1.1 pgoyette buf[len] = '\0'; 115 1.1 pgoyette } else { 116 1.1 pgoyette buf[0] = '|'; 117 1.1 pgoyette buf[1] = '\0'; 118 1.1 pgoyette } 119 1.1 pgoyette snprintf(arg, sizeof(arg), "%d", nargs - optind); 120 1.1 pgoyette strcat(buf, arg); 121 1.1 pgoyette if (strcmp(buf, result) != 0) 122 1.1 pgoyette errx(1, "`%s' != `%s'", buf, result); 123 1.1 pgoyette } 124 1.1 pgoyette free(line); 125 1.1 pgoyette } 126 1.1 pgoyette return 0; 127 1.1 pgoyette } 128