Home | History | Annotate | Line # | Download | only in common
getopt.c revision 1.1.1.2.20.2
      1           1.1    jruoho /******************************************************************************
      2           1.1    jruoho  *
      3           1.1    jruoho  * Module Name: getopt
      4           1.1    jruoho  *
      5           1.1    jruoho  *****************************************************************************/
      6           1.1    jruoho 
      7       1.1.1.2    jruoho /*
      8  1.1.1.2.20.2  jdolecek  * Copyright (C) 2000 - 2017, Intel Corp.
      9           1.1    jruoho  * All rights reserved.
     10           1.1    jruoho  *
     11       1.1.1.2    jruoho  * Redistribution and use in source and binary forms, with or without
     12       1.1.1.2    jruoho  * modification, are permitted provided that the following conditions
     13       1.1.1.2    jruoho  * are met:
     14       1.1.1.2    jruoho  * 1. Redistributions of source code must retain the above copyright
     15       1.1.1.2    jruoho  *    notice, this list of conditions, and the following disclaimer,
     16       1.1.1.2    jruoho  *    without modification.
     17       1.1.1.2    jruoho  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18       1.1.1.2    jruoho  *    substantially similar to the "NO WARRANTY" disclaimer below
     19       1.1.1.2    jruoho  *    ("Disclaimer") and any redistribution must be conditioned upon
     20       1.1.1.2    jruoho  *    including a substantially similar Disclaimer requirement for further
     21       1.1.1.2    jruoho  *    binary redistribution.
     22       1.1.1.2    jruoho  * 3. Neither the names of the above-listed copyright holders nor the names
     23       1.1.1.2    jruoho  *    of any contributors may be used to endorse or promote products derived
     24       1.1.1.2    jruoho  *    from this software without specific prior written permission.
     25       1.1.1.2    jruoho  *
     26       1.1.1.2    jruoho  * Alternatively, this software may be distributed under the terms of the
     27       1.1.1.2    jruoho  * GNU General Public License ("GPL") version 2 as published by the Free
     28       1.1.1.2    jruoho  * Software Foundation.
     29       1.1.1.2    jruoho  *
     30       1.1.1.2    jruoho  * NO WARRANTY
     31       1.1.1.2    jruoho  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32       1.1.1.2    jruoho  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33       1.1.1.2    jruoho  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     34       1.1.1.2    jruoho  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35       1.1.1.2    jruoho  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36       1.1.1.2    jruoho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37       1.1.1.2    jruoho  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38       1.1.1.2    jruoho  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39       1.1.1.2    jruoho  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40       1.1.1.2    jruoho  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41       1.1.1.2    jruoho  * POSSIBILITY OF SUCH DAMAGES.
     42       1.1.1.2    jruoho  */
     43           1.1    jruoho 
     44  1.1.1.2.20.1       tls /*
     45  1.1.1.2.20.1       tls  * ACPICA getopt() implementation
     46  1.1.1.2.20.1       tls  *
     47  1.1.1.2.20.1       tls  * Option strings:
     48  1.1.1.2.20.1       tls  *    "f"       - Option has no arguments
     49  1.1.1.2.20.1       tls  *    "f:"      - Option requires an argument
     50  1.1.1.2.20.2  jdolecek  *    "f+"      - Option has an optional argument
     51  1.1.1.2.20.1       tls  *    "f^"      - Option has optional single-char sub-options
     52  1.1.1.2.20.1       tls  *    "f|"      - Option has required single-char sub-options
     53  1.1.1.2.20.1       tls  */
     54           1.1    jruoho 
     55           1.1    jruoho #include "acpi.h"
     56           1.1    jruoho #include "accommon.h"
     57           1.1    jruoho #include "acapps.h"
     58           1.1    jruoho 
     59  1.1.1.2.20.1       tls #define ACPI_OPTION_ERROR(msg, badchar) \
     60  1.1.1.2.20.1       tls     if (AcpiGbl_Opterr) {fprintf (stderr, "%s%c\n", msg, badchar);}
     61  1.1.1.2.20.1       tls 
     62           1.1    jruoho 
     63  1.1.1.2.20.1       tls int                 AcpiGbl_Opterr = 1;
     64  1.1.1.2.20.1       tls int                 AcpiGbl_Optind = 1;
     65  1.1.1.2.20.1       tls int                 AcpiGbl_SubOptChar = 0;
     66  1.1.1.2.20.1       tls char                *AcpiGbl_Optarg;
     67           1.1    jruoho 
     68  1.1.1.2.20.1       tls static int          CurrentCharPtr = 1;
     69  1.1.1.2.20.1       tls 
     70  1.1.1.2.20.1       tls 
     71  1.1.1.2.20.1       tls /*******************************************************************************
     72  1.1.1.2.20.1       tls  *
     73  1.1.1.2.20.1       tls  * FUNCTION:    AcpiGetoptArgument
     74  1.1.1.2.20.1       tls  *
     75  1.1.1.2.20.1       tls  * PARAMETERS:  argc, argv          - from main
     76  1.1.1.2.20.1       tls  *
     77  1.1.1.2.20.1       tls  * RETURN:      0 if an argument was found, -1 otherwise. Sets AcpiGbl_Optarg
     78  1.1.1.2.20.1       tls  *              to point to the next argument.
     79  1.1.1.2.20.1       tls  *
     80  1.1.1.2.20.1       tls  * DESCRIPTION: Get the next argument. Used to obtain arguments for the
     81  1.1.1.2.20.1       tls  *              two-character options after the original call to AcpiGetopt.
     82  1.1.1.2.20.1       tls  *              Note: Either the argument starts at the next character after
     83  1.1.1.2.20.1       tls  *              the option, or it is pointed to by the next argv entry.
     84  1.1.1.2.20.1       tls  *              (After call to AcpiGetopt, we need to backup to the previous
     85  1.1.1.2.20.1       tls  *              argv entry).
     86  1.1.1.2.20.1       tls  *
     87  1.1.1.2.20.1       tls  ******************************************************************************/
     88  1.1.1.2.20.1       tls 
     89  1.1.1.2.20.1       tls int
     90  1.1.1.2.20.1       tls AcpiGetoptArgument (
     91  1.1.1.2.20.1       tls     int                     argc,
     92  1.1.1.2.20.1       tls     char                    **argv)
     93  1.1.1.2.20.1       tls {
     94  1.1.1.2.20.2  jdolecek 
     95  1.1.1.2.20.1       tls     AcpiGbl_Optind--;
     96  1.1.1.2.20.1       tls     CurrentCharPtr++;
     97  1.1.1.2.20.1       tls 
     98  1.1.1.2.20.1       tls     if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
     99  1.1.1.2.20.1       tls     {
    100  1.1.1.2.20.1       tls         AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][(int) (CurrentCharPtr+1)];
    101  1.1.1.2.20.1       tls     }
    102  1.1.1.2.20.1       tls     else if (++AcpiGbl_Optind >= argc)
    103  1.1.1.2.20.1       tls     {
    104  1.1.1.2.20.2  jdolecek         ACPI_OPTION_ERROR ("\nOption requires an argument", 0);
    105  1.1.1.2.20.1       tls 
    106  1.1.1.2.20.1       tls         CurrentCharPtr = 1;
    107  1.1.1.2.20.1       tls         return (-1);
    108  1.1.1.2.20.1       tls     }
    109  1.1.1.2.20.1       tls     else
    110  1.1.1.2.20.1       tls     {
    111  1.1.1.2.20.1       tls         AcpiGbl_Optarg = argv[AcpiGbl_Optind++];
    112  1.1.1.2.20.1       tls     }
    113  1.1.1.2.20.1       tls 
    114  1.1.1.2.20.1       tls     CurrentCharPtr = 1;
    115  1.1.1.2.20.1       tls     return (0);
    116  1.1.1.2.20.1       tls }
    117           1.1    jruoho 
    118           1.1    jruoho 
    119           1.1    jruoho /*******************************************************************************
    120           1.1    jruoho  *
    121           1.1    jruoho  * FUNCTION:    AcpiGetopt
    122           1.1    jruoho  *
    123           1.1    jruoho  * PARAMETERS:  argc, argv          - from main
    124           1.1    jruoho  *              opts                - options info list
    125           1.1    jruoho  *
    126  1.1.1.2.20.2  jdolecek  * RETURN:      Option character or ACPI_OPT_END
    127           1.1    jruoho  *
    128           1.1    jruoho  * DESCRIPTION: Get the next option
    129           1.1    jruoho  *
    130           1.1    jruoho  ******************************************************************************/
    131           1.1    jruoho 
    132           1.1    jruoho int
    133           1.1    jruoho AcpiGetopt(
    134           1.1    jruoho     int                     argc,
    135           1.1    jruoho     char                    **argv,
    136           1.1    jruoho     char                    *opts)
    137           1.1    jruoho {
    138           1.1    jruoho     int                     CurrentChar;
    139           1.1    jruoho     char                    *OptsPtr;
    140           1.1    jruoho 
    141           1.1    jruoho 
    142           1.1    jruoho     if (CurrentCharPtr == 1)
    143           1.1    jruoho     {
    144           1.1    jruoho         if (AcpiGbl_Optind >= argc ||
    145           1.1    jruoho             argv[AcpiGbl_Optind][0] != '-' ||
    146           1.1    jruoho             argv[AcpiGbl_Optind][1] == '\0')
    147           1.1    jruoho         {
    148  1.1.1.2.20.2  jdolecek             return (ACPI_OPT_END);
    149           1.1    jruoho         }
    150           1.1    jruoho         else if (strcmp (argv[AcpiGbl_Optind], "--") == 0)
    151           1.1    jruoho         {
    152           1.1    jruoho             AcpiGbl_Optind++;
    153  1.1.1.2.20.2  jdolecek             return (ACPI_OPT_END);
    154           1.1    jruoho         }
    155           1.1    jruoho     }
    156           1.1    jruoho 
    157           1.1    jruoho     /* Get the option */
    158           1.1    jruoho 
    159       1.1.1.2    jruoho     CurrentChar = argv[AcpiGbl_Optind][CurrentCharPtr];
    160           1.1    jruoho 
    161           1.1    jruoho     /* Make sure that the option is legal */
    162           1.1    jruoho 
    163           1.1    jruoho     if (CurrentChar == ':' ||
    164           1.1    jruoho        (OptsPtr = strchr (opts, CurrentChar)) == NULL)
    165           1.1    jruoho     {
    166  1.1.1.2.20.1       tls         ACPI_OPTION_ERROR ("Illegal option: -", CurrentChar);
    167           1.1    jruoho 
    168           1.1    jruoho         if (argv[AcpiGbl_Optind][++CurrentCharPtr] == '\0')
    169           1.1    jruoho         {
    170           1.1    jruoho             AcpiGbl_Optind++;
    171           1.1    jruoho             CurrentCharPtr = 1;
    172           1.1    jruoho         }
    173           1.1    jruoho 
    174           1.1    jruoho         return ('?');
    175           1.1    jruoho     }
    176           1.1    jruoho 
    177           1.1    jruoho     /* Option requires an argument? */
    178           1.1    jruoho 
    179           1.1    jruoho     if (*++OptsPtr == ':')
    180           1.1    jruoho     {
    181           1.1    jruoho         if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
    182           1.1    jruoho         {
    183           1.1    jruoho             AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][(int) (CurrentCharPtr+1)];
    184           1.1    jruoho         }
    185           1.1    jruoho         else if (++AcpiGbl_Optind >= argc)
    186           1.1    jruoho         {
    187  1.1.1.2.20.2  jdolecek             ACPI_OPTION_ERROR (
    188  1.1.1.2.20.2  jdolecek                 "Option requires an argument: -", CurrentChar);
    189           1.1    jruoho 
    190           1.1    jruoho             CurrentCharPtr = 1;
    191           1.1    jruoho             return ('?');
    192           1.1    jruoho         }
    193           1.1    jruoho         else
    194           1.1    jruoho         {
    195           1.1    jruoho             AcpiGbl_Optarg = argv[AcpiGbl_Optind++];
    196           1.1    jruoho         }
    197           1.1    jruoho 
    198           1.1    jruoho         CurrentCharPtr = 1;
    199           1.1    jruoho     }
    200           1.1    jruoho 
    201  1.1.1.2.20.1       tls     /* Option has an optional argument? */
    202  1.1.1.2.20.1       tls 
    203  1.1.1.2.20.1       tls     else if (*OptsPtr == '+')
    204  1.1.1.2.20.1       tls     {
    205  1.1.1.2.20.1       tls         if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
    206  1.1.1.2.20.1       tls         {
    207  1.1.1.2.20.1       tls             AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][(int) (CurrentCharPtr+1)];
    208  1.1.1.2.20.1       tls         }
    209  1.1.1.2.20.1       tls         else if (++AcpiGbl_Optind >= argc)
    210  1.1.1.2.20.1       tls         {
    211  1.1.1.2.20.1       tls             AcpiGbl_Optarg = NULL;
    212  1.1.1.2.20.1       tls         }
    213  1.1.1.2.20.1       tls         else
    214  1.1.1.2.20.1       tls         {
    215  1.1.1.2.20.1       tls             AcpiGbl_Optarg = argv[AcpiGbl_Optind++];
    216  1.1.1.2.20.1       tls         }
    217  1.1.1.2.20.1       tls 
    218  1.1.1.2.20.1       tls         CurrentCharPtr = 1;
    219  1.1.1.2.20.1       tls     }
    220  1.1.1.2.20.1       tls 
    221           1.1    jruoho     /* Option has optional single-char arguments? */
    222           1.1    jruoho 
    223           1.1    jruoho     else if (*OptsPtr == '^')
    224           1.1    jruoho     {
    225           1.1    jruoho         if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
    226           1.1    jruoho         {
    227           1.1    jruoho             AcpiGbl_Optarg = &argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)];
    228           1.1    jruoho         }
    229           1.1    jruoho         else
    230           1.1    jruoho         {
    231           1.1    jruoho             AcpiGbl_Optarg = "^";
    232           1.1    jruoho         }
    233           1.1    jruoho 
    234  1.1.1.2.20.1       tls         AcpiGbl_SubOptChar = AcpiGbl_Optarg[0];
    235  1.1.1.2.20.1       tls         AcpiGbl_Optind++;
    236  1.1.1.2.20.1       tls         CurrentCharPtr = 1;
    237  1.1.1.2.20.1       tls     }
    238  1.1.1.2.20.1       tls 
    239  1.1.1.2.20.1       tls     /* Option has a required single-char argument? */
    240  1.1.1.2.20.1       tls 
    241  1.1.1.2.20.1       tls     else if (*OptsPtr == '|')
    242  1.1.1.2.20.1       tls     {
    243  1.1.1.2.20.1       tls         if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
    244  1.1.1.2.20.1       tls         {
    245  1.1.1.2.20.1       tls             AcpiGbl_Optarg = &argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)];
    246  1.1.1.2.20.1       tls         }
    247  1.1.1.2.20.1       tls         else
    248  1.1.1.2.20.1       tls         {
    249  1.1.1.2.20.2  jdolecek             ACPI_OPTION_ERROR (
    250  1.1.1.2.20.2  jdolecek                 "Option requires a single-character suboption: -",
    251  1.1.1.2.20.2  jdolecek                 CurrentChar);
    252  1.1.1.2.20.1       tls 
    253  1.1.1.2.20.1       tls             CurrentCharPtr = 1;
    254  1.1.1.2.20.1       tls             return ('?');
    255  1.1.1.2.20.1       tls         }
    256  1.1.1.2.20.1       tls 
    257  1.1.1.2.20.1       tls         AcpiGbl_SubOptChar = AcpiGbl_Optarg[0];
    258           1.1    jruoho         AcpiGbl_Optind++;
    259           1.1    jruoho         CurrentCharPtr = 1;
    260           1.1    jruoho     }
    261           1.1    jruoho 
    262           1.1    jruoho     /* Option with no arguments */
    263           1.1    jruoho 
    264           1.1    jruoho     else
    265           1.1    jruoho     {
    266           1.1    jruoho         if (argv[AcpiGbl_Optind][++CurrentCharPtr] == '\0')
    267           1.1    jruoho         {
    268           1.1    jruoho             CurrentCharPtr = 1;
    269           1.1    jruoho             AcpiGbl_Optind++;
    270           1.1    jruoho         }
    271           1.1    jruoho 
    272           1.1    jruoho         AcpiGbl_Optarg = NULL;
    273           1.1    jruoho     }
    274           1.1    jruoho 
    275           1.1    jruoho     return (CurrentChar);
    276           1.1    jruoho }
    277