1/***************************************************************************** 2* getopt.c - competent and free getopt library. 3* $Header: /cvsroot/freegetopt/freegetopt/getopt.c,v 1.2 2003/10/26 03:10:20 vindaci Exp $ 4 * 5* Copyright (c)2002-2003 Mark K. Kim 6* All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12* * Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15* * Redistributions in binary form must reproduce the above copyright 16* notice, this list of conditions and the following disclaimer in 17* the documentation and/or other materials provided with the 18* distribution. 19* 20* * Neither the original author of this software nor the names of its 21* contributors may be used to endorse or promote products derived 22* from this software without specific prior written permission. 23* 24* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 25* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 26* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 27* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 28* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 29* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 30* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 31* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 32* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 33* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 34* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 35* DAMAGE. 36*/ 37#include <stdio.h> 38#include <stdlib.h> 39#include <string.h> 40#include "getopt.h" 41 42 43static const char* ID = "$Id: getopt.c,v 1.2 2003/10/26 03:10:20 vindaci Exp $"; 44 45 46char* optarg = NULL; 47int optind = 0; 48int opterr = 1; 49int optopt = '?'; 50 51 52static char** prev_argv = NULL; /* Keep a copy of argv and argc to */ 53static int prev_argc = 0; /* tell if getopt params change */ 54static int argv_index = 0; /* Option we're checking */ 55static int argv_index2 = 0; /* Option argument we're checking */ 56static int opt_offset = 0; /* Index into compounded "-option" */ 57static int dashdash = 0; /* True if "--" option reached */ 58static int nonopt = 0; /* How many nonopts we've found */ 59 60static void increment_index() 61{ 62 /* Move onto the next option */ 63 if(argv_index < argv_index2) 64 { 65 while(prev_argv[++argv_index] && prev_argv[argv_index][0] != '-' 66 && argv_index < argv_index2+1); 67 } 68 else argv_index++; 69 opt_offset = 1; 70} 71 72 73/* 74* Permutes argv[] so that the argument currently being processed is moved 75* to the end. 76*/ 77static int permute_argv_once() 78{ 79 /* Movability check */ 80 if(argv_index + nonopt >= prev_argc) return 1; 81 /* Move the current option to the end, bring the others to front */ 82 else 83 { 84 char* tmp = prev_argv[argv_index]; 85 86 /* Move the data */ 87 memmove(&prev_argv[argv_index], &prev_argv[argv_index+1], 88 sizeof(char**) * (prev_argc - argv_index - 1)); 89 prev_argv[prev_argc - 1] = tmp; 90 91 nonopt++; 92 return 0; 93 } 94} 95 96 97int getopt(int argc, char* const argv[], const char* optstr) 98{ 99 int c = 0; 100 101 /* If we have new argv, reinitialize */ 102 if(prev_argv != argv || prev_argc != argc) 103 { 104 /* Initialize variables */ 105 prev_argv = argv; 106 prev_argc = argc; 107 argv_index = 1; 108 argv_index2 = 1; 109 opt_offset = 1; 110 dashdash = 0; 111 nonopt = 0; 112 } 113 114 /* Jump point in case we want to ignore the current argv_index */ 115 getopt_top: 116 117 /* Misc. initializations */ 118 optarg = NULL; 119 120 /* Dash-dash check */ 121 if(argv[argv_index] && !strcmp(argv[argv_index], "--")) 122 { 123 dashdash = 1; 124 increment_index(); 125 } 126 127 /* If we're at the end of argv, that's it. */ 128 if(argv[argv_index] == NULL) 129 { 130 c = -1; 131 } 132 /* Are we looking at a string? Single dash is also a string */ 133 else if(dashdash || argv[argv_index][0] != '-' || !strcmp(argv[argv_index], "-")) 134 { 135 /* If we want a string... */ 136 if(optstr[0] == '-') 137 { 138 c = 1; 139 optarg = argv[argv_index]; 140 increment_index(); 141 } 142 /* If we really don't want it (we're in POSIX mode), we're done */ 143 else if(optstr[0] == '+' || getenv("POSIXLY_CORRECT")) 144 { 145 c = -1; 146 147 /* Everything else is a non-opt argument */ 148 nonopt = argc - argv_index; 149 } 150 /* If we mildly don't want it, then move it back */ 151 else 152 { 153 if(!permute_argv_once()) goto getopt_top; 154 else c = -1; 155 } 156 } 157 /* Otherwise we're looking at an option */ 158 else 159 { 160 char* opt_ptr = NULL; 161 162 /* Grab the option */ 163 c = argv[argv_index][opt_offset++]; 164 165 /* Is the option in the optstr? */ 166 if(optstr[0] == '-') opt_ptr = strchr(optstr+1, c); 167 else opt_ptr = strchr(optstr, c); 168 /* Invalid argument */ 169 if(!opt_ptr) 170 { 171 if(opterr) 172 { 173 fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c); 174 } 175 176 optopt = c; 177 c = '?'; 178 179 /* Move onto the next option */ 180 increment_index(); 181 } 182 /* Option takes argument */ 183 else if(opt_ptr[1] == ':') 184 { 185 /* ie, -oARGUMENT, -xxxoARGUMENT, etc. */ 186 if(argv[argv_index][opt_offset] != '\0') 187 { 188 optarg = &argv[argv_index][opt_offset]; 189 increment_index(); 190 } 191 /* ie, -o ARGUMENT (only if it's a required argument) */ 192 else if(opt_ptr[2] != ':') 193 { 194 /* One of those "you're not expected to understand this" moment */ 195 if(argv_index2 < argv_index) argv_index2 = argv_index; 196 while(argv[++argv_index2] && argv[argv_index2][0] == '-'); 197 optarg = argv[argv_index2]; 198 199 /* Don't cross into the non-option argument list */ 200 if(argv_index2 + nonopt >= prev_argc) optarg = NULL; 201 202 /* Move onto the next option */ 203 increment_index(); 204 } 205 else 206 { 207 /* Move onto the next option */ 208 increment_index(); 209 } 210 211 /* In case we got no argument for an option with required argument */ 212 if(optarg == NULL && opt_ptr[2] != ':') 213 { 214 optopt = c; 215 c = '?'; 216 217 if(opterr) 218 { 219 fprintf(stderr,"%s: option requires an argument -- %c\n", 220 argv[0], optopt); 221 } 222 } 223 } 224 /* Option does not take argument */ 225 else 226 { 227 /* Next argv_index */ 228 if(argv[argv_index][opt_offset] == '\0') 229 { 230 increment_index(); 231 } 232 } 233 } 234 235 /* Calculate optind */ 236 if(c == -1) 237 { 238 optind = argc - nonopt; 239 } 240 else 241 { 242 optind = argv_index; 243 } 244 245 return c; 246} 247 248 249/* vim:ts=3 250*/ 251