1118613Snjl
2118613Snjl/******************************************************************************
3118613Snjl *
4118613Snjl * Module Name: getopt
5118613Snjl *
6118613Snjl *****************************************************************************/
7118613Snjl
8217365Sjkim/*
9217365Sjkim * Copyright (C) 2000 - 2011, 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
52118613Snjl#define ERR(szz,czz) if(AcpiGbl_Opterr){fprintf(stderr,"%s%s%c\n",argv[0],szz,czz);}
53118613Snjl
54118613Snjl
55118613Snjlint   AcpiGbl_Opterr = 1;
56118613Snjlint   AcpiGbl_Optind = 1;
57118613Snjlchar  *AcpiGbl_Optarg;
58118613Snjl
59118613Snjl
60118613Snjl/*******************************************************************************
61118613Snjl *
62118613Snjl * FUNCTION:    AcpiGetopt
63118613Snjl *
64118613Snjl * PARAMETERS:  argc, argv          - from main
65118613Snjl *              opts                - options info list
66118613Snjl *
67118613Snjl * RETURN:      Option character or EOF
68118613Snjl *
69118613Snjl * DESCRIPTION: Get the next option
70118613Snjl *
71118613Snjl ******************************************************************************/
72118613Snjl
73118613Snjlint
74118613SnjlAcpiGetopt(
75118613Snjl    int                     argc,
76118613Snjl    char                    **argv,
77118613Snjl    char                    *opts)
78118613Snjl{
79118613Snjl    static int              CurrentCharPtr = 1;
80118613Snjl    int                     CurrentChar;
81118613Snjl    char                    *OptsPtr;
82118613Snjl
83118613Snjl
84118613Snjl    if (CurrentCharPtr == 1)
85118613Snjl    {
86118613Snjl        if (AcpiGbl_Optind >= argc ||
87118613Snjl            argv[AcpiGbl_Optind][0] != '-' ||
88118613Snjl            argv[AcpiGbl_Optind][1] == '\0')
89118613Snjl        {
90118613Snjl            return(EOF);
91118613Snjl        }
92118613Snjl        else if (strcmp (argv[AcpiGbl_Optind], "--") == 0)
93118613Snjl        {
94118613Snjl            AcpiGbl_Optind++;
95118613Snjl            return(EOF);
96118613Snjl        }
97118613Snjl    }
98118613Snjl
99118613Snjl    /* Get the option */
100118613Snjl
101212761Sjkim    CurrentChar = argv[AcpiGbl_Optind][CurrentCharPtr];
102118613Snjl
103118613Snjl    /* Make sure that the option is legal */
104118613Snjl
105118613Snjl    if (CurrentChar == ':' ||
106118613Snjl       (OptsPtr = strchr (opts, CurrentChar)) == NULL)
107118613Snjl    {
108118613Snjl        ERR (": illegal option -- ", CurrentChar);
109118613Snjl
110118613Snjl        if (argv[AcpiGbl_Optind][++CurrentCharPtr] == '\0')
111118613Snjl        {
112118613Snjl            AcpiGbl_Optind++;
113118613Snjl            CurrentCharPtr = 1;
114118613Snjl        }
115118613Snjl
116118613Snjl        return ('?');
117118613Snjl    }
118118613Snjl
119118613Snjl    /* Option requires an argument? */
120118613Snjl
121118613Snjl    if (*++OptsPtr == ':')
122118613Snjl    {
123198237Sjkim        if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
124118613Snjl        {
125198237Sjkim            AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][(int) (CurrentCharPtr+1)];
126118613Snjl        }
127118613Snjl        else if (++AcpiGbl_Optind >= argc)
128118613Snjl        {
129118613Snjl            ERR (": option requires an argument -- ", CurrentChar);
130118613Snjl
131118613Snjl            CurrentCharPtr = 1;
132118613Snjl            return ('?');
133118613Snjl        }
134118613Snjl        else
135118613Snjl        {
136118613Snjl            AcpiGbl_Optarg = argv[AcpiGbl_Optind++];
137118613Snjl        }
138118613Snjl
139118613Snjl        CurrentCharPtr = 1;
140118613Snjl    }
141118613Snjl
142118613Snjl    /* Option has optional single-char arguments? */
143118613Snjl
144118613Snjl    else if (*OptsPtr == '^')
145118613Snjl    {
146198237Sjkim        if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
147118613Snjl        {
148198237Sjkim            AcpiGbl_Optarg = &argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)];
149118613Snjl        }
150118613Snjl        else
151118613Snjl        {
152118613Snjl            AcpiGbl_Optarg = "^";
153118613Snjl        }
154118613Snjl
155118613Snjl        AcpiGbl_Optind++;
156118613Snjl        CurrentCharPtr = 1;
157118613Snjl    }
158118613Snjl
159118613Snjl    /* Option with no arguments */
160118613Snjl
161118613Snjl    else
162118613Snjl    {
163118613Snjl        if (argv[AcpiGbl_Optind][++CurrentCharPtr] == '\0')
164118613Snjl        {
165118613Snjl            CurrentCharPtr = 1;
166118613Snjl            AcpiGbl_Optind++;
167118613Snjl        }
168118613Snjl
169118613Snjl        AcpiGbl_Optarg = NULL;
170118613Snjl    }
171118613Snjl
172118613Snjl    return (CurrentChar);
173118613Snjl}
174