getopt.c revision 233250
1118613Snjl 2118613Snjl/****************************************************************************** 3118613Snjl * 4118613Snjl * Module Name: getopt 5118613Snjl * 6118613Snjl *****************************************************************************/ 7118613Snjl 8217365Sjkim/* 9229989Sjkim * Copyright (C) 2000 - 2012, Intel Corp. 10118613Snjl * All rights reserved. 11118613Snjl * 12217365Sjkim * Redistribution and use in source and binary forms, with or without 13217365Sjkim * modification, are permitted provided that the following conditions 14217365Sjkim * are met: 15217365Sjkim * 1. Redistributions of source code must retain the above copyright 16217365Sjkim * notice, this list of conditions, and the following disclaimer, 17217365Sjkim * without modification. 18217365Sjkim * 2. Redistributions in binary form must reproduce at minimum a disclaimer 19217365Sjkim * substantially similar to the "NO WARRANTY" disclaimer below 20217365Sjkim * ("Disclaimer") and any redistribution must be conditioned upon 21217365Sjkim * including a substantially similar Disclaimer requirement for further 22217365Sjkim * binary redistribution. 23217365Sjkim * 3. Neither the names of the above-listed copyright holders nor the names 24217365Sjkim * of any contributors may be used to endorse or promote products derived 25217365Sjkim * from this software without specific prior written permission. 26118613Snjl * 27217365Sjkim * Alternatively, this software may be distributed under the terms of the 28217365Sjkim * GNU General Public License ("GPL") version 2 as published by the Free 29217365Sjkim * Software Foundation. 30118613Snjl * 31217365Sjkim * NO WARRANTY 32217365Sjkim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 33217365Sjkim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 34217365Sjkim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 35217365Sjkim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 36217365Sjkim * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 37217365Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 38217365Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 39217365Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 40217365Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 41217365Sjkim * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 42217365Sjkim * POSSIBILITY OF SUCH DAMAGES. 43217365Sjkim */ 44118613Snjl 45118613Snjl 46118613Snjl#include <stdio.h> 47118613Snjl#include <string.h> 48193529Sjkim#include <contrib/dev/acpica/include/acpi.h> 49193529Sjkim#include <contrib/dev/acpica/include/accommon.h> 50193529Sjkim#include <contrib/dev/acpica/include/acapps.h> 51118613Snjl 52233250Sjkim#define ACPI_OPTION_ERROR(msg, badchar) \ 53233250Sjkim if (AcpiGbl_Opterr) {fprintf (stderr, "%s%c\n", msg, badchar);} 54118613Snjl 55118613Snjl 56118613Snjlint AcpiGbl_Opterr = 1; 57118613Snjlint AcpiGbl_Optind = 1; 58118613Snjlchar *AcpiGbl_Optarg; 59118613Snjl 60118613Snjl 61118613Snjl/******************************************************************************* 62118613Snjl * 63118613Snjl * FUNCTION: AcpiGetopt 64118613Snjl * 65118613Snjl * PARAMETERS: argc, argv - from main 66118613Snjl * opts - options info list 67118613Snjl * 68118613Snjl * RETURN: Option character or EOF 69118613Snjl * 70118613Snjl * DESCRIPTION: Get the next option 71118613Snjl * 72118613Snjl ******************************************************************************/ 73118613Snjl 74118613Snjlint 75118613SnjlAcpiGetopt( 76118613Snjl int argc, 77118613Snjl char **argv, 78118613Snjl char *opts) 79118613Snjl{ 80118613Snjl static int CurrentCharPtr = 1; 81118613Snjl int CurrentChar; 82118613Snjl char *OptsPtr; 83118613Snjl 84118613Snjl 85118613Snjl if (CurrentCharPtr == 1) 86118613Snjl { 87118613Snjl if (AcpiGbl_Optind >= argc || 88118613Snjl argv[AcpiGbl_Optind][0] != '-' || 89118613Snjl argv[AcpiGbl_Optind][1] == '\0') 90118613Snjl { 91233250Sjkim return (EOF); 92118613Snjl } 93118613Snjl else if (strcmp (argv[AcpiGbl_Optind], "--") == 0) 94118613Snjl { 95118613Snjl AcpiGbl_Optind++; 96233250Sjkim return (EOF); 97118613Snjl } 98118613Snjl } 99118613Snjl 100118613Snjl /* Get the option */ 101118613Snjl 102212761Sjkim CurrentChar = argv[AcpiGbl_Optind][CurrentCharPtr]; 103118613Snjl 104118613Snjl /* Make sure that the option is legal */ 105118613Snjl 106118613Snjl if (CurrentChar == ':' || 107118613Snjl (OptsPtr = strchr (opts, CurrentChar)) == NULL) 108118613Snjl { 109233250Sjkim ACPI_OPTION_ERROR ("Illegal option: -", CurrentChar); 110118613Snjl 111118613Snjl if (argv[AcpiGbl_Optind][++CurrentCharPtr] == '\0') 112118613Snjl { 113118613Snjl AcpiGbl_Optind++; 114118613Snjl CurrentCharPtr = 1; 115118613Snjl } 116118613Snjl 117118613Snjl return ('?'); 118118613Snjl } 119118613Snjl 120118613Snjl /* Option requires an argument? */ 121118613Snjl 122118613Snjl if (*++OptsPtr == ':') 123118613Snjl { 124198237Sjkim if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0') 125118613Snjl { 126198237Sjkim AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][(int) (CurrentCharPtr+1)]; 127118613Snjl } 128118613Snjl else if (++AcpiGbl_Optind >= argc) 129118613Snjl { 130233250Sjkim ACPI_OPTION_ERROR ("Option requires an argument: -", CurrentChar); 131118613Snjl 132118613Snjl CurrentCharPtr = 1; 133118613Snjl return ('?'); 134118613Snjl } 135118613Snjl else 136118613Snjl { 137118613Snjl AcpiGbl_Optarg = argv[AcpiGbl_Optind++]; 138118613Snjl } 139118613Snjl 140118613Snjl CurrentCharPtr = 1; 141118613Snjl } 142118613Snjl 143118613Snjl /* Option has optional single-char arguments? */ 144118613Snjl 145118613Snjl else if (*OptsPtr == '^') 146118613Snjl { 147198237Sjkim if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0') 148118613Snjl { 149198237Sjkim AcpiGbl_Optarg = &argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)]; 150118613Snjl } 151118613Snjl else 152118613Snjl { 153118613Snjl AcpiGbl_Optarg = "^"; 154118613Snjl } 155118613Snjl 156118613Snjl AcpiGbl_Optind++; 157118613Snjl CurrentCharPtr = 1; 158118613Snjl } 159118613Snjl 160233250Sjkim /* Option has a required single-char argument? */ 161233250Sjkim 162233250Sjkim else if (*OptsPtr == '|') 163233250Sjkim { 164233250Sjkim if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0') 165233250Sjkim { 166233250Sjkim AcpiGbl_Optarg = &argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)]; 167233250Sjkim } 168233250Sjkim else 169233250Sjkim { 170233250Sjkim ACPI_OPTION_ERROR ("Option requires a single-character suboption: -", CurrentChar); 171233250Sjkim 172233250Sjkim CurrentCharPtr = 1; 173233250Sjkim return ('?'); 174233250Sjkim } 175233250Sjkim 176233250Sjkim AcpiGbl_Optind++; 177233250Sjkim CurrentCharPtr = 1; 178233250Sjkim } 179233250Sjkim 180118613Snjl /* Option with no arguments */ 181118613Snjl 182118613Snjl else 183118613Snjl { 184118613Snjl if (argv[AcpiGbl_Optind][++CurrentCharPtr] == '\0') 185118613Snjl { 186118613Snjl CurrentCharPtr = 1; 187118613Snjl AcpiGbl_Optind++; 188118613Snjl } 189118613Snjl 190118613Snjl AcpiGbl_Optarg = NULL; 191118613Snjl } 192118613Snjl 193118613Snjl return (CurrentChar); 194118613Snjl} 195