Home | History | Annotate | Line # | Download | only in common
getopt.c revision 1.1.1.4.2.3
      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.4.2.3     skrll  * Copyright (C) 2000 - 2016, 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.3  christos /*
     45      1.1.1.3  christos  * ACPICA getopt() implementation
     46      1.1.1.3  christos  *
     47      1.1.1.3  christos  * Option strings:
     48      1.1.1.3  christos  *    "f"       - Option has no arguments
     49      1.1.1.3  christos  *    "f:"      - Option requires an argument
     50  1.1.1.4.2.3     skrll  *    "f+"      - Option has an optional argument
     51      1.1.1.3  christos  *    "f^"      - Option has optional single-char sub-options
     52      1.1.1.3  christos  *    "f|"      - Option has required single-char sub-options
     53      1.1.1.3  christos  */
     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.3  christos #define ACPI_OPTION_ERROR(msg, badchar) \
     60      1.1.1.4  christos     if (AcpiGbl_Opterr) {AcpiLogError ("%s%c\n", msg, badchar);}
     61      1.1.1.3  christos 
     62          1.1    jruoho 
     63      1.1.1.3  christos int                 AcpiGbl_Opterr = 1;
     64      1.1.1.3  christos int                 AcpiGbl_Optind = 1;
     65      1.1.1.3  christos int                 AcpiGbl_SubOptChar = 0;
     66      1.1.1.3  christos char                *AcpiGbl_Optarg;
     67          1.1    jruoho 
     68      1.1.1.3  christos static int          CurrentCharPtr = 1;
     69      1.1.1.3  christos 
     70      1.1.1.3  christos 
     71      1.1.1.3  christos /*******************************************************************************
     72      1.1.1.3  christos  *
     73      1.1.1.3  christos  * FUNCTION:    AcpiGetoptArgument
     74      1.1.1.3  christos  *
     75      1.1.1.3  christos  * PARAMETERS:  argc, argv          - from main
     76      1.1.1.3  christos  *
     77      1.1.1.3  christos  * RETURN:      0 if an argument was found, -1 otherwise. Sets AcpiGbl_Optarg
     78      1.1.1.3  christos  *              to point to the next argument.
     79      1.1.1.3  christos  *
     80      1.1.1.3  christos  * DESCRIPTION: Get the next argument. Used to obtain arguments for the
     81      1.1.1.3  christos  *              two-character options after the original call to AcpiGetopt.
     82      1.1.1.3  christos  *              Note: Either the argument starts at the next character after
     83      1.1.1.3  christos  *              the option, or it is pointed to by the next argv entry.
     84      1.1.1.3  christos  *              (After call to AcpiGetopt, we need to backup to the previous
     85      1.1.1.3  christos  *              argv entry).
     86      1.1.1.3  christos  *
     87      1.1.1.3  christos  ******************************************************************************/
     88      1.1.1.3  christos 
     89      1.1.1.3  christos int
     90      1.1.1.3  christos AcpiGetoptArgument (
     91      1.1.1.3  christos     int                     argc,
     92      1.1.1.3  christos     char                    **argv)
     93      1.1.1.3  christos {
     94  1.1.1.4.2.3     skrll 
     95      1.1.1.3  christos     AcpiGbl_Optind--;
     96      1.1.1.3  christos     CurrentCharPtr++;
     97      1.1.1.3  christos 
     98      1.1.1.3  christos     if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
     99      1.1.1.3  christos     {
    100      1.1.1.3  christos         AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][(int) (CurrentCharPtr+1)];
    101      1.1.1.3  christos     }
    102      1.1.1.3  christos     else if (++AcpiGbl_Optind >= argc)
    103      1.1.1.3  christos     {
    104      1.1.1.3  christos         ACPI_OPTION_ERROR ("Option requires an argument: -", 'v');
    105      1.1.1.3  christos 
    106      1.1.1.3  christos         CurrentCharPtr = 1;
    107      1.1.1.3  christos         return (-1);
    108      1.1.1.3  christos     }
    109      1.1.1.3  christos     else
    110      1.1.1.3  christos     {
    111      1.1.1.3  christos         AcpiGbl_Optarg = argv[AcpiGbl_Optind++];
    112      1.1.1.3  christos     }
    113      1.1.1.3  christos 
    114      1.1.1.3  christos     CurrentCharPtr = 1;
    115      1.1.1.3  christos     return (0);
    116      1.1.1.3  christos }
    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.4  christos  * 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.4  christos             return (ACPI_OPT_END);
    149          1.1    jruoho         }
    150  1.1.1.4.2.2     skrll         else if (strcmp (argv[AcpiGbl_Optind], "--") == 0)
    151          1.1    jruoho         {
    152          1.1    jruoho             AcpiGbl_Optind++;
    153      1.1.1.4  christos             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.1.4.2.2     skrll        (OptsPtr = strchr (opts, CurrentChar)) == NULL)
    165          1.1    jruoho     {
    166      1.1.1.3  christos         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.4.2.3     skrll             ACPI_OPTION_ERROR (
    188  1.1.1.4.2.3     skrll                 "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.3  christos     /* Option has an optional argument? */
    202      1.1.1.3  christos 
    203      1.1.1.3  christos     else if (*OptsPtr == '+')
    204      1.1.1.3  christos     {
    205      1.1.1.3  christos         if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
    206      1.1.1.3  christos         {
    207      1.1.1.3  christos             AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][(int) (CurrentCharPtr+1)];
    208      1.1.1.3  christos         }
    209      1.1.1.3  christos         else if (++AcpiGbl_Optind >= argc)
    210      1.1.1.3  christos         {
    211      1.1.1.3  christos             AcpiGbl_Optarg = NULL;
    212      1.1.1.3  christos         }
    213      1.1.1.3  christos         else
    214      1.1.1.3  christos         {
    215      1.1.1.3  christos             AcpiGbl_Optarg = argv[AcpiGbl_Optind++];
    216      1.1.1.3  christos         }
    217      1.1.1.3  christos 
    218      1.1.1.3  christos         CurrentCharPtr = 1;
    219      1.1.1.3  christos     }
    220      1.1.1.3  christos 
    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.3  christos         AcpiGbl_SubOptChar = AcpiGbl_Optarg[0];
    235      1.1.1.3  christos         AcpiGbl_Optind++;
    236      1.1.1.3  christos         CurrentCharPtr = 1;
    237      1.1.1.3  christos     }
    238      1.1.1.3  christos 
    239      1.1.1.3  christos     /* Option has a required single-char argument? */
    240      1.1.1.3  christos 
    241      1.1.1.3  christos     else if (*OptsPtr == '|')
    242      1.1.1.3  christos     {
    243      1.1.1.3  christos         if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
    244      1.1.1.3  christos         {
    245      1.1.1.3  christos             AcpiGbl_Optarg = &argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)];
    246      1.1.1.3  christos         }
    247      1.1.1.3  christos         else
    248      1.1.1.3  christos         {
    249  1.1.1.4.2.3     skrll             ACPI_OPTION_ERROR (
    250  1.1.1.4.2.3     skrll                 "Option requires a single-character suboption: -",
    251  1.1.1.4.2.3     skrll                 CurrentChar);
    252      1.1.1.3  christos 
    253      1.1.1.3  christos             CurrentCharPtr = 1;
    254      1.1.1.3  christos             return ('?');
    255      1.1.1.3  christos         }
    256      1.1.1.3  christos 
    257      1.1.1.3  christos         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