1 /****************************************************************************** 2 * 3 * Module Name: getopt 4 * 5 *****************************************************************************/ 6 7 /* 8 * Copyright (C) 2000 - 2015, Intel Corp. 9 * All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions, and the following disclaimer, 16 * without modification. 17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 18 * substantially similar to the "NO WARRANTY" disclaimer below 19 * ("Disclaimer") and any redistribution must be conditioned upon 20 * including a substantially similar Disclaimer requirement for further 21 * binary redistribution. 22 * 3. Neither the names of the above-listed copyright holders nor the names 23 * of any contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * Alternatively, this software may be distributed under the terms of the 27 * GNU General Public License ("GPL") version 2 as published by the Free 28 * Software Foundation. 29 * 30 * NO WARRANTY 31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 41 * POSSIBILITY OF SUCH DAMAGES. 42 */ 43 44 /* 45 * ACPICA getopt() implementation 46 * 47 * Option strings: 48 * "f" - Option has no arguments 49 * "f:" - Option requires an argument 50 * "f^" - Option has optional single-char sub-options 51 * "f|" - Option has required single-char sub-options 52 */ 53 54 #include "acpi.h" 55 #include "accommon.h" 56 #include "acapps.h" 57 58 #define ACPI_OPTION_ERROR(msg, badchar) \ 59 if (AcpiGbl_Opterr) {AcpiLogError ("%s%c\n", msg, badchar);} 60 61 62 int AcpiGbl_Opterr = 1; 63 int AcpiGbl_Optind = 1; 64 int AcpiGbl_SubOptChar = 0; 65 char *AcpiGbl_Optarg; 66 67 static int CurrentCharPtr = 1; 68 69 70 /******************************************************************************* 71 * 72 * FUNCTION: AcpiGetoptArgument 73 * 74 * PARAMETERS: argc, argv - from main 75 * 76 * RETURN: 0 if an argument was found, -1 otherwise. Sets AcpiGbl_Optarg 77 * to point to the next argument. 78 * 79 * DESCRIPTION: Get the next argument. Used to obtain arguments for the 80 * two-character options after the original call to AcpiGetopt. 81 * Note: Either the argument starts at the next character after 82 * the option, or it is pointed to by the next argv entry. 83 * (After call to AcpiGetopt, we need to backup to the previous 84 * argv entry). 85 * 86 ******************************************************************************/ 87 88 int 89 AcpiGetoptArgument ( 90 int argc, 91 char **argv) 92 { 93 94 AcpiGbl_Optind--; 95 CurrentCharPtr++; 96 97 if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0') 98 { 99 AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][(int) (CurrentCharPtr+1)]; 100 } 101 else if (++AcpiGbl_Optind >= argc) 102 { 103 ACPI_OPTION_ERROR ("Option requires an argument: -", 'v'); 104 105 CurrentCharPtr = 1; 106 return (-1); 107 } 108 else 109 { 110 AcpiGbl_Optarg = argv[AcpiGbl_Optind++]; 111 } 112 113 CurrentCharPtr = 1; 114 return (0); 115 } 116 117 118 /******************************************************************************* 119 * 120 * FUNCTION: AcpiGetopt 121 * 122 * PARAMETERS: argc, argv - from main 123 * opts - options info list 124 * 125 * RETURN: Option character or ACPI_OPT_END 126 * 127 * DESCRIPTION: Get the next option 128 * 129 ******************************************************************************/ 130 131 int 132 AcpiGetopt( 133 int argc, 134 char **argv, 135 char *opts) 136 { 137 int CurrentChar; 138 char *OptsPtr; 139 140 141 if (CurrentCharPtr == 1) 142 { 143 if (AcpiGbl_Optind >= argc || 144 argv[AcpiGbl_Optind][0] != '-' || 145 argv[AcpiGbl_Optind][1] == '\0') 146 { 147 return (ACPI_OPT_END); 148 } 149 else if (strcmp (argv[AcpiGbl_Optind], "--") == 0) 150 { 151 AcpiGbl_Optind++; 152 return (ACPI_OPT_END); 153 } 154 } 155 156 /* Get the option */ 157 158 CurrentChar = argv[AcpiGbl_Optind][CurrentCharPtr]; 159 160 /* Make sure that the option is legal */ 161 162 if (CurrentChar == ':' || 163 (OptsPtr = strchr (opts, CurrentChar)) == NULL) 164 { 165 ACPI_OPTION_ERROR ("Illegal option: -", CurrentChar); 166 167 if (argv[AcpiGbl_Optind][++CurrentCharPtr] == '\0') 168 { 169 AcpiGbl_Optind++; 170 CurrentCharPtr = 1; 171 } 172 173 return ('?'); 174 } 175 176 /* Option requires an argument? */ 177 178 if (*++OptsPtr == ':') 179 { 180 if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0') 181 { 182 AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][(int) (CurrentCharPtr+1)]; 183 } 184 else if (++AcpiGbl_Optind >= argc) 185 { 186 ACPI_OPTION_ERROR ( 187 "Option requires an argument: -", CurrentChar); 188 189 CurrentCharPtr = 1; 190 return ('?'); 191 } 192 else 193 { 194 AcpiGbl_Optarg = argv[AcpiGbl_Optind++]; 195 } 196 197 CurrentCharPtr = 1; 198 } 199 200 /* Option has an optional argument? */ 201 202 else if (*OptsPtr == '+') 203 { 204 if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0') 205 { 206 AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][(int) (CurrentCharPtr+1)]; 207 } 208 else if (++AcpiGbl_Optind >= argc) 209 { 210 AcpiGbl_Optarg = NULL; 211 } 212 else 213 { 214 AcpiGbl_Optarg = argv[AcpiGbl_Optind++]; 215 } 216 217 CurrentCharPtr = 1; 218 } 219 220 /* Option has optional single-char arguments? */ 221 222 else if (*OptsPtr == '^') 223 { 224 if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0') 225 { 226 AcpiGbl_Optarg = &argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)]; 227 } 228 else 229 { 230 AcpiGbl_Optarg = "^"; 231 } 232 233 AcpiGbl_SubOptChar = AcpiGbl_Optarg[0]; 234 AcpiGbl_Optind++; 235 CurrentCharPtr = 1; 236 } 237 238 /* Option has a required single-char argument? */ 239 240 else if (*OptsPtr == '|') 241 { 242 if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0') 243 { 244 AcpiGbl_Optarg = &argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)]; 245 } 246 else 247 { 248 ACPI_OPTION_ERROR ( 249 "Option requires a single-character suboption: -", 250 CurrentChar); 251 252 CurrentCharPtr = 1; 253 return ('?'); 254 } 255 256 AcpiGbl_SubOptChar = AcpiGbl_Optarg[0]; 257 AcpiGbl_Optind++; 258 CurrentCharPtr = 1; 259 } 260 261 /* Option with no arguments */ 262 263 else 264 { 265 if (argv[AcpiGbl_Optind][++CurrentCharPtr] == '\0') 266 { 267 CurrentCharPtr = 1; 268 AcpiGbl_Optind++; 269 } 270 271 AcpiGbl_Optarg = NULL; 272 } 273 274 return (CurrentChar); 275 } 276