1118613Snjl/****************************************************************************** 2118613Snjl * 3118613Snjl * Module Name: getopt 4118613Snjl * 5118613Snjl *****************************************************************************/ 6118613Snjl 7217365Sjkim/* 8281075Sdim * Copyright (C) 2000 - 2015, Intel Corp. 9118613Snjl * All rights reserved. 10118613Snjl * 11217365Sjkim * Redistribution and use in source and binary forms, with or without 12217365Sjkim * modification, are permitted provided that the following conditions 13217365Sjkim * are met: 14217365Sjkim * 1. Redistributions of source code must retain the above copyright 15217365Sjkim * notice, this list of conditions, and the following disclaimer, 16217365Sjkim * without modification. 17217365Sjkim * 2. Redistributions in binary form must reproduce at minimum a disclaimer 18217365Sjkim * substantially similar to the "NO WARRANTY" disclaimer below 19217365Sjkim * ("Disclaimer") and any redistribution must be conditioned upon 20217365Sjkim * including a substantially similar Disclaimer requirement for further 21217365Sjkim * binary redistribution. 22217365Sjkim * 3. Neither the names of the above-listed copyright holders nor the names 23217365Sjkim * of any contributors may be used to endorse or promote products derived 24217365Sjkim * from this software without specific prior written permission. 25118613Snjl * 26217365Sjkim * Alternatively, this software may be distributed under the terms of the 27217365Sjkim * GNU General Public License ("GPL") version 2 as published by the Free 28217365Sjkim * Software Foundation. 29118613Snjl * 30217365Sjkim * NO WARRANTY 31217365Sjkim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 32217365Sjkim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 33217365Sjkim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 34217365Sjkim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 35217365Sjkim * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 36217365Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 37217365Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 38217365Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 39217365Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 40217365Sjkim * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 41217365Sjkim * POSSIBILITY OF SUCH DAMAGES. 42217365Sjkim */ 43118613Snjl 44250838Sjkim/* 45250838Sjkim * ACPICA getopt() implementation 46250838Sjkim * 47250838Sjkim * Option strings: 48250838Sjkim * "f" - Option has no arguments 49250838Sjkim * "f:" - Option requires an argument 50250838Sjkim * "f^" - Option has optional single-char sub-options 51250838Sjkim * "f|" - Option has required single-char sub-options 52250838Sjkim */ 53118613Snjl 54193529Sjkim#include <contrib/dev/acpica/include/acpi.h> 55193529Sjkim#include <contrib/dev/acpica/include/accommon.h> 56193529Sjkim#include <contrib/dev/acpica/include/acapps.h> 57118613Snjl 58233250Sjkim#define ACPI_OPTION_ERROR(msg, badchar) \ 59281075Sdim if (AcpiGbl_Opterr) {AcpiLogError ("%s%c\n", msg, badchar);} 60118613Snjl 61118613Snjl 62250838Sjkimint AcpiGbl_Opterr = 1; 63250838Sjkimint AcpiGbl_Optind = 1; 64250838Sjkimint AcpiGbl_SubOptChar = 0; 65250838Sjkimchar *AcpiGbl_Optarg; 66118613Snjl 67250838Sjkimstatic int CurrentCharPtr = 1; 68118613Snjl 69250838Sjkim 70118613Snjl/******************************************************************************* 71118613Snjl * 72250838Sjkim * FUNCTION: AcpiGetoptArgument 73250838Sjkim * 74250838Sjkim * PARAMETERS: argc, argv - from main 75250838Sjkim * 76250838Sjkim * RETURN: 0 if an argument was found, -1 otherwise. Sets AcpiGbl_Optarg 77250838Sjkim * to point to the next argument. 78250838Sjkim * 79250838Sjkim * DESCRIPTION: Get the next argument. Used to obtain arguments for the 80250838Sjkim * two-character options after the original call to AcpiGetopt. 81250838Sjkim * Note: Either the argument starts at the next character after 82250838Sjkim * the option, or it is pointed to by the next argv entry. 83250838Sjkim * (After call to AcpiGetopt, we need to backup to the previous 84250838Sjkim * argv entry). 85250838Sjkim * 86250838Sjkim ******************************************************************************/ 87250838Sjkim 88250838Sjkimint 89250838SjkimAcpiGetoptArgument ( 90250838Sjkim int argc, 91250838Sjkim char **argv) 92250838Sjkim{ 93250838Sjkim AcpiGbl_Optind--; 94250838Sjkim CurrentCharPtr++; 95250838Sjkim 96250838Sjkim if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0') 97250838Sjkim { 98250838Sjkim AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][(int) (CurrentCharPtr+1)]; 99250838Sjkim } 100250838Sjkim else if (++AcpiGbl_Optind >= argc) 101250838Sjkim { 102250838Sjkim ACPI_OPTION_ERROR ("Option requires an argument: -", 'v'); 103250838Sjkim 104250838Sjkim CurrentCharPtr = 1; 105250838Sjkim return (-1); 106250838Sjkim } 107250838Sjkim else 108250838Sjkim { 109250838Sjkim AcpiGbl_Optarg = argv[AcpiGbl_Optind++]; 110250838Sjkim } 111250838Sjkim 112250838Sjkim CurrentCharPtr = 1; 113250838Sjkim return (0); 114250838Sjkim} 115250838Sjkim 116250838Sjkim 117250838Sjkim/******************************************************************************* 118250838Sjkim * 119118613Snjl * FUNCTION: AcpiGetopt 120118613Snjl * 121118613Snjl * PARAMETERS: argc, argv - from main 122118613Snjl * opts - options info list 123118613Snjl * 124281075Sdim * RETURN: Option character or ACPI_OPT_END 125118613Snjl * 126118613Snjl * DESCRIPTION: Get the next option 127118613Snjl * 128118613Snjl ******************************************************************************/ 129118613Snjl 130118613Snjlint 131118613SnjlAcpiGetopt( 132118613Snjl int argc, 133118613Snjl char **argv, 134118613Snjl char *opts) 135118613Snjl{ 136118613Snjl int CurrentChar; 137118613Snjl char *OptsPtr; 138118613Snjl 139118613Snjl 140118613Snjl if (CurrentCharPtr == 1) 141118613Snjl { 142118613Snjl if (AcpiGbl_Optind >= argc || 143118613Snjl argv[AcpiGbl_Optind][0] != '-' || 144118613Snjl argv[AcpiGbl_Optind][1] == '\0') 145118613Snjl { 146281075Sdim return (ACPI_OPT_END); 147118613Snjl } 148281075Sdim else if (ACPI_STRCMP (argv[AcpiGbl_Optind], "--") == 0) 149118613Snjl { 150118613Snjl AcpiGbl_Optind++; 151281075Sdim return (ACPI_OPT_END); 152118613Snjl } 153118613Snjl } 154118613Snjl 155118613Snjl /* Get the option */ 156118613Snjl 157212761Sjkim CurrentChar = argv[AcpiGbl_Optind][CurrentCharPtr]; 158118613Snjl 159118613Snjl /* Make sure that the option is legal */ 160118613Snjl 161118613Snjl if (CurrentChar == ':' || 162281075Sdim (OptsPtr = ACPI_STRCHR (opts, CurrentChar)) == NULL) 163118613Snjl { 164233250Sjkim ACPI_OPTION_ERROR ("Illegal option: -", CurrentChar); 165118613Snjl 166118613Snjl if (argv[AcpiGbl_Optind][++CurrentCharPtr] == '\0') 167118613Snjl { 168118613Snjl AcpiGbl_Optind++; 169118613Snjl CurrentCharPtr = 1; 170118613Snjl } 171118613Snjl 172118613Snjl return ('?'); 173118613Snjl } 174118613Snjl 175118613Snjl /* Option requires an argument? */ 176118613Snjl 177118613Snjl if (*++OptsPtr == ':') 178118613Snjl { 179198237Sjkim if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0') 180118613Snjl { 181198237Sjkim AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][(int) (CurrentCharPtr+1)]; 182118613Snjl } 183118613Snjl else if (++AcpiGbl_Optind >= argc) 184118613Snjl { 185233250Sjkim ACPI_OPTION_ERROR ("Option requires an argument: -", CurrentChar); 186118613Snjl 187118613Snjl CurrentCharPtr = 1; 188118613Snjl return ('?'); 189118613Snjl } 190118613Snjl else 191118613Snjl { 192118613Snjl AcpiGbl_Optarg = argv[AcpiGbl_Optind++]; 193118613Snjl } 194118613Snjl 195118613Snjl CurrentCharPtr = 1; 196118613Snjl } 197118613Snjl 198253690Sjkim /* Option has an optional argument? */ 199253690Sjkim 200253690Sjkim else if (*OptsPtr == '+') 201253690Sjkim { 202253690Sjkim if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0') 203253690Sjkim { 204253690Sjkim AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][(int) (CurrentCharPtr+1)]; 205253690Sjkim } 206253690Sjkim else if (++AcpiGbl_Optind >= argc) 207253690Sjkim { 208253690Sjkim AcpiGbl_Optarg = NULL; 209253690Sjkim } 210253690Sjkim else 211253690Sjkim { 212253690Sjkim AcpiGbl_Optarg = argv[AcpiGbl_Optind++]; 213253690Sjkim } 214253690Sjkim 215253690Sjkim CurrentCharPtr = 1; 216253690Sjkim } 217253690Sjkim 218118613Snjl /* Option has optional single-char arguments? */ 219118613Snjl 220118613Snjl else if (*OptsPtr == '^') 221118613Snjl { 222198237Sjkim if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0') 223118613Snjl { 224198237Sjkim AcpiGbl_Optarg = &argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)]; 225118613Snjl } 226118613Snjl else 227118613Snjl { 228118613Snjl AcpiGbl_Optarg = "^"; 229118613Snjl } 230118613Snjl 231250838Sjkim AcpiGbl_SubOptChar = AcpiGbl_Optarg[0]; 232118613Snjl AcpiGbl_Optind++; 233118613Snjl CurrentCharPtr = 1; 234118613Snjl } 235118613Snjl 236233250Sjkim /* Option has a required single-char argument? */ 237233250Sjkim 238233250Sjkim else if (*OptsPtr == '|') 239233250Sjkim { 240233250Sjkim if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0') 241233250Sjkim { 242233250Sjkim AcpiGbl_Optarg = &argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)]; 243233250Sjkim } 244233250Sjkim else 245233250Sjkim { 246233250Sjkim ACPI_OPTION_ERROR ("Option requires a single-character suboption: -", CurrentChar); 247233250Sjkim 248233250Sjkim CurrentCharPtr = 1; 249233250Sjkim return ('?'); 250233250Sjkim } 251233250Sjkim 252250838Sjkim AcpiGbl_SubOptChar = AcpiGbl_Optarg[0]; 253233250Sjkim AcpiGbl_Optind++; 254233250Sjkim CurrentCharPtr = 1; 255233250Sjkim } 256233250Sjkim 257118613Snjl /* Option with no arguments */ 258118613Snjl 259118613Snjl else 260118613Snjl { 261118613Snjl if (argv[AcpiGbl_Optind][++CurrentCharPtr] == '\0') 262118613Snjl { 263118613Snjl CurrentCharPtr = 1; 264118613Snjl AcpiGbl_Optind++; 265118613Snjl } 266118613Snjl 267118613Snjl AcpiGbl_Optarg = NULL; 268118613Snjl } 269118613Snjl 270118613Snjl return (CurrentChar); 271118613Snjl} 272