getopt.c revision 233250
131198Sahasty
29313Ssos/******************************************************************************
39313Ssos *
49313Ssos * Module Name: getopt
59313Ssos *
69313Ssos *****************************************************************************/
79313Ssos
89313Ssos/*
99313Ssos * Copyright (C) 2000 - 2012, Intel Corp.
109313Ssos * All rights reserved.
119313Ssos *
129313Ssos * Redistribution and use in source and binary forms, with or without
139313Ssos * modification, are permitted provided that the following conditions
149313Ssos * are met:
1597748Sschweikh * 1. Redistributions of source code must retain the above copyright
169313Ssos *    notice, this list of conditions, and the following disclaimer,
179313Ssos *    without modification.
189313Ssos * 2. Redistributions in binary form must reproduce at minimum a disclaimer
199313Ssos *    substantially similar to the "NO WARRANTY" disclaimer below
209313Ssos *    ("Disclaimer") and any redistribution must be conditioned upon
219313Ssos *    including a substantially similar Disclaimer requirement for further
229313Ssos *    binary redistribution.
239313Ssos * 3. Neither the names of the above-listed copyright holders nor the names
249313Ssos *    of any contributors may be used to endorse or promote products derived
259313Ssos *    from this software without specific prior written permission.
269313Ssos *
279313Ssos * Alternatively, this software may be distributed under the terms of the
289313Ssos * GNU General Public License ("GPL") version 2 as published by the Free
29116173Sobrien * Software Foundation.
30116173Sobrien *
31116173Sobrien * NO WARRANTY
329313Ssos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
339313Ssos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3412458Sbde * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
3546163Sluoqi * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3686555Smarcel * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37112938Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38112740Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3966834Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4085012Sdes * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
4124131Sbde * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
429313Ssos * POSSIBILITY OF SUCH DAMAGES.
439313Ssos */
4424205Sbde
4566834Sphk
4654122Smarcel#include <stdio.h>
4754122Smarcel#include <string.h>
4885012Sdes#include <contrib/dev/acpica/include/acpi.h>
4985012Sdes#include <contrib/dev/acpica/include/accommon.h>
5085012Sdes#include <contrib/dev/acpica/include/acapps.h>
5185012Sdes
52130453Sphk#define ACPI_OPTION_ERROR(msg, badchar) \
539313Ssos    if (AcpiGbl_Opterr) {fprintf (stderr, "%s%c\n", msg, badchar);}
5485012Sdes
5514331Speter
5626364Smsmithint   AcpiGbl_Opterr = 1;
5726364Smsmithint   AcpiGbl_Optind = 1;
5812458Sbdechar  *AcpiGbl_Optarg;
59133816Stjr
60133816Stjr
61133816Stjr/*******************************************************************************
6264907Smarcel *
6368583Smarcel * FUNCTION:    AcpiGetopt
64133816Stjr *
65133816Stjr * PARAMETERS:  argc, argv          - from main
66133816Stjr *              opts                - options info list
67133816Stjr *
6868201Sobrien * RETURN:      Option character or EOF
6964907Smarcel *
7064907Smarcel * DESCRIPTION: Get the next option
7164907Smarcel *
729313Ssos ******************************************************************************/
7354122Smarcel
74104893Ssobomaxint
7554122SmarcelAcpiGetopt(
76130453Sphk    int                     argc,
7757998Snsayer    char                    **argv,
7854122Smarcel    char                    *opts)
7954122Smarcel{
8054122Smarcel    static int              CurrentCharPtr = 1;
8185127Sdes    int                     CurrentChar;
82113991Sanholt    char                    *OptsPtr;
8386607Siedowse
8438672Sjkh
8554122Smarcel    if (CurrentCharPtr == 1)
8654122Smarcel    {
87104893Ssobomax        if (AcpiGbl_Optind >= argc ||
88104893Ssobomax            argv[AcpiGbl_Optind][0] != '-' ||
8954122Smarcel            argv[AcpiGbl_Optind][1] == '\0')
9054122Smarcel        {
91130453Sphk            return (EOF);
92130453Sphk        }
9357998Snsayer        else if (strcmp (argv[AcpiGbl_Optind], "--") == 0)
9457998Snsayer        {
9554122Smarcel            AcpiGbl_Optind++;
9654122Smarcel            return (EOF);
9754122Smarcel        }
9854122Smarcel    }
9954122Smarcel
10054122Smarcel    /* Get the option */
10185127Sdes
10285127Sdes    CurrentChar = argv[AcpiGbl_Optind][CurrentCharPtr];
103113991Sanholt
104113991Sanholt    /* Make sure that the option is legal */
10554122Smarcel
10654122Smarcel    if (CurrentChar == ':' ||
107104893Ssobomax       (OptsPtr = strchr (opts, CurrentChar)) == NULL)
10854122Smarcel    {
109130453Sphk        ACPI_OPTION_ERROR ("Illegal option: -", CurrentChar);
11057998Snsayer
11154122Smarcel        if (argv[AcpiGbl_Optind][++CurrentCharPtr] == '\0')
11254122Smarcel        {
11354122Smarcel            AcpiGbl_Optind++;
11485127Sdes            CurrentCharPtr = 1;
115113991Sanholt        }
11654122Smarcel
117111742Sdes        return ('?');
11854122Smarcel    }
11960938Sjake
12083366Sjulian    /* Option requires an argument? */
12154122Smarcel
12254122Smarcel    if (*++OptsPtr == ':')
12354122Smarcel    {
12460938Sjake        if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
12554122Smarcel        {
12654122Smarcel            AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][(int) (CurrentCharPtr+1)];
127130453Sphk        }
128130453Sphk        else if (++AcpiGbl_Optind >= argc)
129130453Sphk        {
130130453Sphk            ACPI_OPTION_ERROR ("Option requires an argument: -", CurrentChar);
131130453Sphk
132130453Sphk            CurrentCharPtr = 1;
133130453Sphk            return ('?');
134130453Sphk        }
135130453Sphk        else
136130453Sphk        {
137130453Sphk            AcpiGbl_Optarg = argv[AcpiGbl_Optind++];
138130453Sphk        }
139130453Sphk
140130453Sphk        CurrentCharPtr = 1;
141130453Sphk    }
142130453Sphk
143130453Sphk    /* Option has optional single-char arguments? */
144130453Sphk
14557858Snsayer    else if (*OptsPtr == '^')
146130453Sphk    {
147130453Sphk        if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
148130453Sphk        {
149130453Sphk            AcpiGbl_Optarg = &argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)];
150130453Sphk        }
151130453Sphk        else
152130453Sphk        {
153130453Sphk            AcpiGbl_Optarg = "^";
154130453Sphk        }
155130453Sphk
156130453Sphk        AcpiGbl_Optind++;
157130453Sphk        CurrentCharPtr = 1;
158130453Sphk    }
159130453Sphk
160130453Sphk    /* Option has a required single-char argument? */
161130453Sphk
162130453Sphk    else if (*OptsPtr == '|')
163130453Sphk    {
164130453Sphk        if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
165130453Sphk        {
166130453Sphk            AcpiGbl_Optarg = &argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)];
167130453Sphk        }
168130453Sphk        else
169130453Sphk        {
170130453Sphk            ACPI_OPTION_ERROR ("Option requires a single-character suboption: -", CurrentChar);
171130453Sphk
172130453Sphk            CurrentCharPtr = 1;
173130453Sphk            return ('?');
174130453Sphk        }
175130453Sphk
176130453Sphk        AcpiGbl_Optind++;
177130453Sphk        CurrentCharPtr = 1;
178130453Sphk    }
179130453Sphk
180130453Sphk    /* Option with no arguments */
181130453Sphk
182130453Sphk    else
183130453Sphk    {
184130453Sphk        if (argv[AcpiGbl_Optind][++CurrentCharPtr] == '\0')
185130453Sphk        {
186130453Sphk            CurrentCharPtr = 1;
187130453Sphk            AcpiGbl_Optind++;
188130453Sphk        }
189130453Sphk
190130453Sphk        AcpiGbl_Optarg = NULL;
191130453Sphk    }
192130453Sphk
193130453Sphk    return (CurrentChar);
194130453Sphk}
195130453Sphk