getopt.c revision 193529
1118613Snjl
2118613Snjl/******************************************************************************
3118613Snjl *
4118613Snjl * Module Name: getopt
5118613Snjl *
6118613Snjl *****************************************************************************/
7118613Snjl
8118613Snjl/******************************************************************************
9118613Snjl *
10118613Snjl * 1. Copyright Notice
11118613Snjl *
12193529Sjkim * Some or all of this work - Copyright (c) 1999 - 2009, Intel Corp.
13118613Snjl * All rights reserved.
14118613Snjl *
15118613Snjl * 2. License
16118613Snjl *
17118613Snjl * 2.1. This is your license from Intel Corp. under its intellectual property
18118613Snjl * rights.  You may have additional license terms from the party that provided
19118613Snjl * you this software, covering your right to use that party's intellectual
20118613Snjl * property rights.
21118613Snjl *
22118613Snjl * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23118613Snjl * copy of the source code appearing in this file ("Covered Code") an
24118613Snjl * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25118613Snjl * base code distributed originally by Intel ("Original Intel Code") to copy,
26118613Snjl * make derivatives, distribute, use and display any portion of the Covered
27118613Snjl * Code in any form, with the right to sublicense such rights; and
28118613Snjl *
29118613Snjl * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30118613Snjl * license (with the right to sublicense), under only those claims of Intel
31118613Snjl * patents that are infringed by the Original Intel Code, to make, use, sell,
32118613Snjl * offer to sell, and import the Covered Code and derivative works thereof
33118613Snjl * solely to the minimum extent necessary to exercise the above copyright
34118613Snjl * license, and in no event shall the patent license extend to any additions
35118613Snjl * to or modifications of the Original Intel Code.  No other license or right
36118613Snjl * is granted directly or by implication, estoppel or otherwise;
37118613Snjl *
38118613Snjl * The above copyright and patent license is granted only if the following
39118613Snjl * conditions are met:
40118613Snjl *
41118613Snjl * 3. Conditions
42118613Snjl *
43118613Snjl * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44118613Snjl * Redistribution of source code of any substantial portion of the Covered
45118613Snjl * Code or modification with rights to further distribute source must include
46118613Snjl * the above Copyright Notice, the above License, this list of Conditions,
47118613Snjl * and the following Disclaimer and Export Compliance provision.  In addition,
48118613Snjl * Licensee must cause all Covered Code to which Licensee contributes to
49118613Snjl * contain a file documenting the changes Licensee made to create that Covered
50118613Snjl * Code and the date of any change.  Licensee must include in that file the
51118613Snjl * documentation of any changes made by any predecessor Licensee.  Licensee
52118613Snjl * must include a prominent statement that the modification is derived,
53118613Snjl * directly or indirectly, from Original Intel Code.
54118613Snjl *
55118613Snjl * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56118613Snjl * Redistribution of source code of any substantial portion of the Covered
57118613Snjl * Code or modification without rights to further distribute source must
58118613Snjl * include the following Disclaimer and Export Compliance provision in the
59118613Snjl * documentation and/or other materials provided with distribution.  In
60118613Snjl * addition, Licensee may not authorize further sublicense of source of any
61118613Snjl * portion of the Covered Code, and must include terms to the effect that the
62118613Snjl * license from Licensee to its licensee is limited to the intellectual
63118613Snjl * property embodied in the software Licensee provides to its licensee, and
64118613Snjl * not to intellectual property embodied in modifications its licensee may
65118613Snjl * make.
66118613Snjl *
67118613Snjl * 3.3. Redistribution of Executable. Redistribution in executable form of any
68118613Snjl * substantial portion of the Covered Code or modification must reproduce the
69118613Snjl * above Copyright Notice, and the following Disclaimer and Export Compliance
70118613Snjl * provision in the documentation and/or other materials provided with the
71118613Snjl * distribution.
72118613Snjl *
73118613Snjl * 3.4. Intel retains all right, title, and interest in and to the Original
74118613Snjl * Intel Code.
75118613Snjl *
76118613Snjl * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77118613Snjl * Intel shall be used in advertising or otherwise to promote the sale, use or
78118613Snjl * other dealings in products derived from or relating to the Covered Code
79118613Snjl * without prior written authorization from Intel.
80118613Snjl *
81118613Snjl * 4. Disclaimer and Export Compliance
82118613Snjl *
83118613Snjl * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84118613Snjl * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85118613Snjl * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
86118613Snjl * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
87118613Snjl * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
88118613Snjl * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89118613Snjl * PARTICULAR PURPOSE.
90118613Snjl *
91118613Snjl * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92118613Snjl * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93118613Snjl * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94118613Snjl * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95118613Snjl * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96118613Snjl * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
97118613Snjl * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98118613Snjl * LIMITED REMEDY.
99118613Snjl *
100118613Snjl * 4.3. Licensee shall not export, either directly or indirectly, any of this
101118613Snjl * software or system incorporating such software without first obtaining any
102118613Snjl * required license or other approval from the U. S. Department of Commerce or
103118613Snjl * any other agency or department of the United States Government.  In the
104118613Snjl * event Licensee exports any such software from the United States or
105118613Snjl * re-exports any such software from a foreign destination, Licensee shall
106118613Snjl * ensure that the distribution and export/re-export of the software is in
107118613Snjl * compliance with all laws, regulations, orders, or other restrictions of the
108118613Snjl * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109118613Snjl * any of its subsidiaries will export/re-export any technical data, process,
110118613Snjl * software, or service, directly or indirectly, to any country for which the
111118613Snjl * United States government or any agency thereof requires an export license,
112118613Snjl * other governmental approval, or letter of assurance, without first obtaining
113118613Snjl * such license, approval or letter.
114118613Snjl *
115118613Snjl *****************************************************************************/
116118613Snjl
117118613Snjl
118118613Snjl#include <stdio.h>
119118613Snjl#include <string.h>
120193529Sjkim#include <contrib/dev/acpica/include/acpi.h>
121193529Sjkim#include <contrib/dev/acpica/include/accommon.h>
122193529Sjkim#include <contrib/dev/acpica/include/acapps.h>
123118613Snjl
124118613Snjl#define ERR(szz,czz) if(AcpiGbl_Opterr){fprintf(stderr,"%s%s%c\n",argv[0],szz,czz);}
125118613Snjl
126118613Snjl
127118613Snjlint   AcpiGbl_Opterr = 1;
128118613Snjlint   AcpiGbl_Optind = 1;
129118613Snjlint   AcpiGbl_Optopt;
130118613Snjlchar  *AcpiGbl_Optarg;
131118613Snjl
132118613Snjl
133118613Snjl/*******************************************************************************
134118613Snjl *
135118613Snjl * FUNCTION:    AcpiGetopt
136118613Snjl *
137118613Snjl * PARAMETERS:  argc, argv          - from main
138118613Snjl *              opts                - options info list
139118613Snjl *
140118613Snjl * RETURN:      Option character or EOF
141118613Snjl *
142118613Snjl * DESCRIPTION: Get the next option
143118613Snjl *
144118613Snjl ******************************************************************************/
145118613Snjl
146118613Snjlint
147118613SnjlAcpiGetopt(
148118613Snjl    int                     argc,
149118613Snjl    char                    **argv,
150118613Snjl    char                    *opts)
151118613Snjl{
152118613Snjl    static int              CurrentCharPtr = 1;
153118613Snjl    int                     CurrentChar;
154118613Snjl    char                    *OptsPtr;
155118613Snjl
156118613Snjl
157118613Snjl    if (CurrentCharPtr == 1)
158118613Snjl    {
159118613Snjl        if (AcpiGbl_Optind >= argc ||
160118613Snjl            argv[AcpiGbl_Optind][0] != '-' ||
161118613Snjl            argv[AcpiGbl_Optind][1] == '\0')
162118613Snjl        {
163118613Snjl            return(EOF);
164118613Snjl        }
165118613Snjl        else if (strcmp (argv[AcpiGbl_Optind], "--") == 0)
166118613Snjl        {
167118613Snjl            AcpiGbl_Optind++;
168118613Snjl            return(EOF);
169118613Snjl        }
170118613Snjl    }
171118613Snjl
172118613Snjl    /* Get the option */
173118613Snjl
174118613Snjl    CurrentChar =
175118613Snjl    AcpiGbl_Optopt =
176118613Snjl    argv[AcpiGbl_Optind][CurrentCharPtr];
177118613Snjl
178118613Snjl    /* Make sure that the option is legal */
179118613Snjl
180118613Snjl    if (CurrentChar == ':' ||
181118613Snjl       (OptsPtr = strchr (opts, CurrentChar)) == NULL)
182118613Snjl    {
183118613Snjl        ERR (": illegal option -- ", CurrentChar);
184118613Snjl
185118613Snjl        if (argv[AcpiGbl_Optind][++CurrentCharPtr] == '\0')
186118613Snjl        {
187118613Snjl            AcpiGbl_Optind++;
188118613Snjl            CurrentCharPtr = 1;
189118613Snjl        }
190118613Snjl
191118613Snjl        return ('?');
192118613Snjl    }
193118613Snjl
194118613Snjl    /* Option requires an argument? */
195118613Snjl
196118613Snjl    if (*++OptsPtr == ':')
197118613Snjl    {
198118613Snjl        if (argv[AcpiGbl_Optind][CurrentCharPtr+1] != '\0')
199118613Snjl        {
200118613Snjl            AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][CurrentCharPtr+1];
201118613Snjl        }
202118613Snjl        else if (++AcpiGbl_Optind >= argc)
203118613Snjl        {
204118613Snjl            ERR (": option requires an argument -- ", CurrentChar);
205118613Snjl
206118613Snjl            CurrentCharPtr = 1;
207118613Snjl            return ('?');
208118613Snjl        }
209118613Snjl        else
210118613Snjl        {
211118613Snjl            AcpiGbl_Optarg = argv[AcpiGbl_Optind++];
212118613Snjl        }
213118613Snjl
214118613Snjl        CurrentCharPtr = 1;
215118613Snjl    }
216118613Snjl
217118613Snjl    /* Option has optional single-char arguments? */
218118613Snjl
219118613Snjl    else if (*OptsPtr == '^')
220118613Snjl    {
221118613Snjl        if (argv[AcpiGbl_Optind][CurrentCharPtr+1] != '\0')
222118613Snjl        {
223118613Snjl            AcpiGbl_Optarg = &argv[AcpiGbl_Optind][CurrentCharPtr+1];
224118613Snjl        }
225118613Snjl        else
226118613Snjl        {
227118613Snjl            AcpiGbl_Optarg = "^";
228118613Snjl        }
229118613Snjl
230118613Snjl        AcpiGbl_Optind++;
231118613Snjl        CurrentCharPtr = 1;
232118613Snjl    }
233118613Snjl
234118613Snjl    /* Option with no arguments */
235118613Snjl
236118613Snjl    else
237118613Snjl    {
238118613Snjl        if (argv[AcpiGbl_Optind][++CurrentCharPtr] == '\0')
239118613Snjl        {
240118613Snjl            CurrentCharPtr = 1;
241118613Snjl            AcpiGbl_Optind++;
242118613Snjl        }
243118613Snjl
244118613Snjl        AcpiGbl_Optarg = NULL;
245118613Snjl    }
246118613Snjl
247118613Snjl    return (CurrentChar);
248118613Snjl}
249