xref: /freebsd/sys/contrib/dev/acpica/common/getopt.c (revision ab6f3bf9cb8ad195aecd9165d8d4b1a93aed3c8d)
159db4265SNate Lawson 
259db4265SNate Lawson /******************************************************************************
359db4265SNate Lawson  *
459db4265SNate Lawson  * Module Name: getopt
559db4265SNate Lawson  *
659db4265SNate Lawson  *****************************************************************************/
759db4265SNate Lawson 
859db4265SNate Lawson /******************************************************************************
959db4265SNate Lawson  *
1059db4265SNate Lawson  * 1. Copyright Notice
1159db4265SNate Lawson  *
12a9f12690SJung-uk Kim  * Some or all of this work - Copyright (c) 1999 - 2009, Intel Corp.
1359db4265SNate Lawson  * All rights reserved.
1459db4265SNate Lawson  *
1559db4265SNate Lawson  * 2. License
1659db4265SNate Lawson  *
1759db4265SNate Lawson  * 2.1. This is your license from Intel Corp. under its intellectual property
1859db4265SNate Lawson  * rights.  You may have additional license terms from the party that provided
1959db4265SNate Lawson  * you this software, covering your right to use that party's intellectual
2059db4265SNate Lawson  * property rights.
2159db4265SNate Lawson  *
2259db4265SNate Lawson  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
2359db4265SNate Lawson  * copy of the source code appearing in this file ("Covered Code") an
2459db4265SNate Lawson  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
2559db4265SNate Lawson  * base code distributed originally by Intel ("Original Intel Code") to copy,
2659db4265SNate Lawson  * make derivatives, distribute, use and display any portion of the Covered
2759db4265SNate Lawson  * Code in any form, with the right to sublicense such rights; and
2859db4265SNate Lawson  *
2959db4265SNate Lawson  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
3059db4265SNate Lawson  * license (with the right to sublicense), under only those claims of Intel
3159db4265SNate Lawson  * patents that are infringed by the Original Intel Code, to make, use, sell,
3259db4265SNate Lawson  * offer to sell, and import the Covered Code and derivative works thereof
3359db4265SNate Lawson  * solely to the minimum extent necessary to exercise the above copyright
3459db4265SNate Lawson  * license, and in no event shall the patent license extend to any additions
3559db4265SNate Lawson  * to or modifications of the Original Intel Code.  No other license or right
3659db4265SNate Lawson  * is granted directly or by implication, estoppel or otherwise;
3759db4265SNate Lawson  *
3859db4265SNate Lawson  * The above copyright and patent license is granted only if the following
3959db4265SNate Lawson  * conditions are met:
4059db4265SNate Lawson  *
4159db4265SNate Lawson  * 3. Conditions
4259db4265SNate Lawson  *
4359db4265SNate Lawson  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
4459db4265SNate Lawson  * Redistribution of source code of any substantial portion of the Covered
4559db4265SNate Lawson  * Code or modification with rights to further distribute source must include
4659db4265SNate Lawson  * the above Copyright Notice, the above License, this list of Conditions,
4759db4265SNate Lawson  * and the following Disclaimer and Export Compliance provision.  In addition,
4859db4265SNate Lawson  * Licensee must cause all Covered Code to which Licensee contributes to
4959db4265SNate Lawson  * contain a file documenting the changes Licensee made to create that Covered
5059db4265SNate Lawson  * Code and the date of any change.  Licensee must include in that file the
5159db4265SNate Lawson  * documentation of any changes made by any predecessor Licensee.  Licensee
5259db4265SNate Lawson  * must include a prominent statement that the modification is derived,
5359db4265SNate Lawson  * directly or indirectly, from Original Intel Code.
5459db4265SNate Lawson  *
5559db4265SNate Lawson  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
5659db4265SNate Lawson  * Redistribution of source code of any substantial portion of the Covered
5759db4265SNate Lawson  * Code or modification without rights to further distribute source must
5859db4265SNate Lawson  * include the following Disclaimer and Export Compliance provision in the
5959db4265SNate Lawson  * documentation and/or other materials provided with distribution.  In
6059db4265SNate Lawson  * addition, Licensee may not authorize further sublicense of source of any
6159db4265SNate Lawson  * portion of the Covered Code, and must include terms to the effect that the
6259db4265SNate Lawson  * license from Licensee to its licensee is limited to the intellectual
6359db4265SNate Lawson  * property embodied in the software Licensee provides to its licensee, and
6459db4265SNate Lawson  * not to intellectual property embodied in modifications its licensee may
6559db4265SNate Lawson  * make.
6659db4265SNate Lawson  *
6759db4265SNate Lawson  * 3.3. Redistribution of Executable. Redistribution in executable form of any
6859db4265SNate Lawson  * substantial portion of the Covered Code or modification must reproduce the
6959db4265SNate Lawson  * above Copyright Notice, and the following Disclaimer and Export Compliance
7059db4265SNate Lawson  * provision in the documentation and/or other materials provided with the
7159db4265SNate Lawson  * distribution.
7259db4265SNate Lawson  *
7359db4265SNate Lawson  * 3.4. Intel retains all right, title, and interest in and to the Original
7459db4265SNate Lawson  * Intel Code.
7559db4265SNate Lawson  *
7659db4265SNate Lawson  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
7759db4265SNate Lawson  * Intel shall be used in advertising or otherwise to promote the sale, use or
7859db4265SNate Lawson  * other dealings in products derived from or relating to the Covered Code
7959db4265SNate Lawson  * without prior written authorization from Intel.
8059db4265SNate Lawson  *
8159db4265SNate Lawson  * 4. Disclaimer and Export Compliance
8259db4265SNate Lawson  *
8359db4265SNate Lawson  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
8459db4265SNate Lawson  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
8559db4265SNate Lawson  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
8659db4265SNate Lawson  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
8759db4265SNate Lawson  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
8859db4265SNate Lawson  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
8959db4265SNate Lawson  * PARTICULAR PURPOSE.
9059db4265SNate Lawson  *
9159db4265SNate Lawson  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
9259db4265SNate Lawson  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
9359db4265SNate Lawson  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
9459db4265SNate Lawson  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
9559db4265SNate Lawson  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
9659db4265SNate Lawson  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
9759db4265SNate Lawson  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
9859db4265SNate Lawson  * LIMITED REMEDY.
9959db4265SNate Lawson  *
10059db4265SNate Lawson  * 4.3. Licensee shall not export, either directly or indirectly, any of this
10159db4265SNate Lawson  * software or system incorporating such software without first obtaining any
10259db4265SNate Lawson  * required license or other approval from the U. S. Department of Commerce or
10359db4265SNate Lawson  * any other agency or department of the United States Government.  In the
10459db4265SNate Lawson  * event Licensee exports any such software from the United States or
10559db4265SNate Lawson  * re-exports any such software from a foreign destination, Licensee shall
10659db4265SNate Lawson  * ensure that the distribution and export/re-export of the software is in
10759db4265SNate Lawson  * compliance with all laws, regulations, orders, or other restrictions of the
10859db4265SNate Lawson  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
10959db4265SNate Lawson  * any of its subsidiaries will export/re-export any technical data, process,
11059db4265SNate Lawson  * software, or service, directly or indirectly, to any country for which the
11159db4265SNate Lawson  * United States government or any agency thereof requires an export license,
11259db4265SNate Lawson  * other governmental approval, or letter of assurance, without first obtaining
11359db4265SNate Lawson  * such license, approval or letter.
11459db4265SNate Lawson  *
11559db4265SNate Lawson  *****************************************************************************/
11659db4265SNate Lawson 
11759db4265SNate Lawson 
11859db4265SNate Lawson #include <stdio.h>
11959db4265SNate Lawson #include <string.h>
120ab6f3bf9SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h>
121ab6f3bf9SJung-uk Kim #include <contrib/dev/acpica/include/accommon.h>
122ab6f3bf9SJung-uk Kim #include <contrib/dev/acpica/include/acapps.h>
12359db4265SNate Lawson 
12459db4265SNate Lawson #define ERR(szz,czz) if(AcpiGbl_Opterr){fprintf(stderr,"%s%s%c\n",argv[0],szz,czz);}
12559db4265SNate Lawson 
12659db4265SNate Lawson 
12759db4265SNate Lawson int   AcpiGbl_Opterr = 1;
12859db4265SNate Lawson int   AcpiGbl_Optind = 1;
12959db4265SNate Lawson int   AcpiGbl_Optopt;
13059db4265SNate Lawson char  *AcpiGbl_Optarg;
13159db4265SNate Lawson 
13259db4265SNate Lawson 
13359db4265SNate Lawson /*******************************************************************************
13459db4265SNate Lawson  *
13559db4265SNate Lawson  * FUNCTION:    AcpiGetopt
13659db4265SNate Lawson  *
13759db4265SNate Lawson  * PARAMETERS:  argc, argv          - from main
13859db4265SNate Lawson  *              opts                - options info list
13959db4265SNate Lawson  *
14059db4265SNate Lawson  * RETURN:      Option character or EOF
14159db4265SNate Lawson  *
14259db4265SNate Lawson  * DESCRIPTION: Get the next option
14359db4265SNate Lawson  *
14459db4265SNate Lawson  ******************************************************************************/
14559db4265SNate Lawson 
14659db4265SNate Lawson int
14759db4265SNate Lawson AcpiGetopt(
14859db4265SNate Lawson     int                     argc,
14959db4265SNate Lawson     char                    **argv,
15059db4265SNate Lawson     char                    *opts)
15159db4265SNate Lawson {
15259db4265SNate Lawson     static int              CurrentCharPtr = 1;
15359db4265SNate Lawson     int                     CurrentChar;
15459db4265SNate Lawson     char                    *OptsPtr;
15559db4265SNate Lawson 
15659db4265SNate Lawson 
15759db4265SNate Lawson     if (CurrentCharPtr == 1)
15859db4265SNate Lawson     {
15959db4265SNate Lawson         if (AcpiGbl_Optind >= argc ||
16059db4265SNate Lawson             argv[AcpiGbl_Optind][0] != '-' ||
16159db4265SNate Lawson             argv[AcpiGbl_Optind][1] == '\0')
16259db4265SNate Lawson         {
16359db4265SNate Lawson             return(EOF);
16459db4265SNate Lawson         }
16559db4265SNate Lawson         else if (strcmp (argv[AcpiGbl_Optind], "--") == 0)
16659db4265SNate Lawson         {
16759db4265SNate Lawson             AcpiGbl_Optind++;
16859db4265SNate Lawson             return(EOF);
16959db4265SNate Lawson         }
17059db4265SNate Lawson     }
17159db4265SNate Lawson 
17259db4265SNate Lawson     /* Get the option */
17359db4265SNate Lawson 
17459db4265SNate Lawson     CurrentChar =
17559db4265SNate Lawson     AcpiGbl_Optopt =
17659db4265SNate Lawson     argv[AcpiGbl_Optind][CurrentCharPtr];
17759db4265SNate Lawson 
17859db4265SNate Lawson     /* Make sure that the option is legal */
17959db4265SNate Lawson 
18059db4265SNate Lawson     if (CurrentChar == ':' ||
18159db4265SNate Lawson        (OptsPtr = strchr (opts, CurrentChar)) == NULL)
18259db4265SNate Lawson     {
18359db4265SNate Lawson         ERR (": illegal option -- ", CurrentChar);
18459db4265SNate Lawson 
18559db4265SNate Lawson         if (argv[AcpiGbl_Optind][++CurrentCharPtr] == '\0')
18659db4265SNate Lawson         {
18759db4265SNate Lawson             AcpiGbl_Optind++;
18859db4265SNate Lawson             CurrentCharPtr = 1;
18959db4265SNate Lawson         }
19059db4265SNate Lawson 
19159db4265SNate Lawson         return ('?');
19259db4265SNate Lawson     }
19359db4265SNate Lawson 
19459db4265SNate Lawson     /* Option requires an argument? */
19559db4265SNate Lawson 
19659db4265SNate Lawson     if (*++OptsPtr == ':')
19759db4265SNate Lawson     {
19859db4265SNate Lawson         if (argv[AcpiGbl_Optind][CurrentCharPtr+1] != '\0')
19959db4265SNate Lawson         {
20059db4265SNate Lawson             AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][CurrentCharPtr+1];
20159db4265SNate Lawson         }
20259db4265SNate Lawson         else if (++AcpiGbl_Optind >= argc)
20359db4265SNate Lawson         {
20459db4265SNate Lawson             ERR (": option requires an argument -- ", CurrentChar);
20559db4265SNate Lawson 
20659db4265SNate Lawson             CurrentCharPtr = 1;
20759db4265SNate Lawson             return ('?');
20859db4265SNate Lawson         }
20959db4265SNate Lawson         else
21059db4265SNate Lawson         {
21159db4265SNate Lawson             AcpiGbl_Optarg = argv[AcpiGbl_Optind++];
21259db4265SNate Lawson         }
21359db4265SNate Lawson 
21459db4265SNate Lawson         CurrentCharPtr = 1;
21559db4265SNate Lawson     }
21659db4265SNate Lawson 
21759db4265SNate Lawson     /* Option has optional single-char arguments? */
21859db4265SNate Lawson 
21959db4265SNate Lawson     else if (*OptsPtr == '^')
22059db4265SNate Lawson     {
22159db4265SNate Lawson         if (argv[AcpiGbl_Optind][CurrentCharPtr+1] != '\0')
22259db4265SNate Lawson         {
22359db4265SNate Lawson             AcpiGbl_Optarg = &argv[AcpiGbl_Optind][CurrentCharPtr+1];
22459db4265SNate Lawson         }
22559db4265SNate Lawson         else
22659db4265SNate Lawson         {
22759db4265SNate Lawson             AcpiGbl_Optarg = "^";
22859db4265SNate Lawson         }
22959db4265SNate Lawson 
23059db4265SNate Lawson         AcpiGbl_Optind++;
23159db4265SNate Lawson         CurrentCharPtr = 1;
23259db4265SNate Lawson     }
23359db4265SNate Lawson 
23459db4265SNate Lawson     /* Option with no arguments */
23559db4265SNate Lawson 
23659db4265SNate Lawson     else
23759db4265SNate Lawson     {
23859db4265SNate Lawson         if (argv[AcpiGbl_Optind][++CurrentCharPtr] == '\0')
23959db4265SNate Lawson         {
24059db4265SNate Lawson             CurrentCharPtr = 1;
24159db4265SNate Lawson             AcpiGbl_Optind++;
24259db4265SNate Lawson         }
24359db4265SNate Lawson 
24459db4265SNate Lawson         AcpiGbl_Optarg = NULL;
24559db4265SNate Lawson     }
24659db4265SNate Lawson 
24759db4265SNate Lawson     return (CurrentChar);
24859db4265SNate Lawson }
249