Home | History | Annotate | Line # | Download | only in common
getopt.c revision 1.1.1.2.20.1
      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.1     tls  * Copyright (C) 2000 - 2013, 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.1     tls  *    "f^"      - Option has optional single-char sub-options
     51  1.1.1.2.20.1     tls  *    "f|"      - Option has required single-char sub-options
     52  1.1.1.2.20.1     tls  */
     53           1.1  jruoho 
     54           1.1  jruoho #include <stdio.h>
     55           1.1  jruoho #include <string.h>
     56           1.1  jruoho #include "acpi.h"
     57           1.1  jruoho #include "accommon.h"
     58           1.1  jruoho #include "acapps.h"
     59           1.1  jruoho 
     60  1.1.1.2.20.1     tls #define ACPI_OPTION_ERROR(msg, badchar) \
     61  1.1.1.2.20.1     tls     if (AcpiGbl_Opterr) {fprintf (stderr, "%s%c\n", msg, badchar);}
     62  1.1.1.2.20.1     tls 
     63           1.1  jruoho 
     64  1.1.1.2.20.1     tls int                 AcpiGbl_Opterr = 1;
     65  1.1.1.2.20.1     tls int                 AcpiGbl_Optind = 1;
     66  1.1.1.2.20.1     tls int                 AcpiGbl_SubOptChar = 0;
     67  1.1.1.2.20.1     tls char                *AcpiGbl_Optarg;
     68           1.1  jruoho 
     69  1.1.1.2.20.1     tls static int          CurrentCharPtr = 1;
     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  *
     74  1.1.1.2.20.1     tls  * FUNCTION:    AcpiGetoptArgument
     75  1.1.1.2.20.1     tls  *
     76  1.1.1.2.20.1     tls  * PARAMETERS:  argc, argv          - from main
     77  1.1.1.2.20.1     tls  *
     78  1.1.1.2.20.1     tls  * RETURN:      0 if an argument was found, -1 otherwise. Sets AcpiGbl_Optarg
     79  1.1.1.2.20.1     tls  *              to point to the next argument.
     80  1.1.1.2.20.1     tls  *
     81  1.1.1.2.20.1     tls  * DESCRIPTION: Get the next argument. Used to obtain arguments for the
     82  1.1.1.2.20.1     tls  *              two-character options after the original call to AcpiGetopt.
     83  1.1.1.2.20.1     tls  *              Note: Either the argument starts at the next character after
     84  1.1.1.2.20.1     tls  *              the option, or it is pointed to by the next argv entry.
     85  1.1.1.2.20.1     tls  *              (After call to AcpiGetopt, we need to backup to the previous
     86  1.1.1.2.20.1     tls  *              argv entry).
     87  1.1.1.2.20.1     tls  *
     88  1.1.1.2.20.1     tls  ******************************************************************************/
     89  1.1.1.2.20.1     tls 
     90  1.1.1.2.20.1     tls int
     91  1.1.1.2.20.1     tls AcpiGetoptArgument (
     92  1.1.1.2.20.1     tls     int                     argc,
     93  1.1.1.2.20.1     tls     char                    **argv)
     94  1.1.1.2.20.1     tls {
     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.1     tls         ACPI_OPTION_ERROR ("Option requires an argument: -", 'v');
    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  jruoho  * RETURN:      Option character or EOF
    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.1     tls             return (EOF);
    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.1     tls             return (EOF);
    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.1     tls             ACPI_OPTION_ERROR ("Option requires an argument: -", CurrentChar);
    188           1.1  jruoho 
    189           1.1  jruoho             CurrentCharPtr = 1;
    190           1.1  jruoho             return ('?');
    191           1.1  jruoho         }
    192           1.1  jruoho         else
    193           1.1  jruoho         {
    194           1.1  jruoho             AcpiGbl_Optarg = argv[AcpiGbl_Optind++];
    195           1.1  jruoho         }
    196           1.1  jruoho 
    197           1.1  jruoho         CurrentCharPtr = 1;
    198           1.1  jruoho     }
    199           1.1  jruoho 
    200  1.1.1.2.20.1     tls     /* Option has an optional argument? */
    201  1.1.1.2.20.1     tls 
    202  1.1.1.2.20.1     tls     else if (*OptsPtr == '+')
    203  1.1.1.2.20.1     tls     {
    204  1.1.1.2.20.1     tls         if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
    205  1.1.1.2.20.1     tls         {
    206  1.1.1.2.20.1     tls             AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][(int) (CurrentCharPtr+1)];
    207  1.1.1.2.20.1     tls         }
    208  1.1.1.2.20.1     tls         else if (++AcpiGbl_Optind >= argc)
    209  1.1.1.2.20.1     tls         {
    210  1.1.1.2.20.1     tls             AcpiGbl_Optarg = NULL;
    211  1.1.1.2.20.1     tls         }
    212  1.1.1.2.20.1     tls         else
    213  1.1.1.2.20.1     tls         {
    214  1.1.1.2.20.1     tls             AcpiGbl_Optarg = argv[AcpiGbl_Optind++];
    215  1.1.1.2.20.1     tls         }
    216  1.1.1.2.20.1     tls 
    217  1.1.1.2.20.1     tls         CurrentCharPtr = 1;
    218  1.1.1.2.20.1     tls     }
    219  1.1.1.2.20.1     tls 
    220           1.1  jruoho     /* Option has optional single-char arguments? */
    221           1.1  jruoho 
    222           1.1  jruoho     else if (*OptsPtr == '^')
    223           1.1  jruoho     {
    224           1.1  jruoho         if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
    225           1.1  jruoho         {
    226           1.1  jruoho             AcpiGbl_Optarg = &argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)];
    227           1.1  jruoho         }
    228           1.1  jruoho         else
    229           1.1  jruoho         {
    230           1.1  jruoho             AcpiGbl_Optarg = "^";
    231           1.1  jruoho         }
    232           1.1  jruoho 
    233  1.1.1.2.20.1     tls         AcpiGbl_SubOptChar = AcpiGbl_Optarg[0];
    234  1.1.1.2.20.1     tls         AcpiGbl_Optind++;
    235  1.1.1.2.20.1     tls         CurrentCharPtr = 1;
    236  1.1.1.2.20.1     tls     }
    237  1.1.1.2.20.1     tls 
    238  1.1.1.2.20.1     tls     /* Option has a required single-char argument? */
    239  1.1.1.2.20.1     tls 
    240  1.1.1.2.20.1     tls     else if (*OptsPtr == '|')
    241  1.1.1.2.20.1     tls     {
    242  1.1.1.2.20.1     tls         if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
    243  1.1.1.2.20.1     tls         {
    244  1.1.1.2.20.1     tls             AcpiGbl_Optarg = &argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)];
    245  1.1.1.2.20.1     tls         }
    246  1.1.1.2.20.1     tls         else
    247  1.1.1.2.20.1     tls         {
    248  1.1.1.2.20.1     tls             ACPI_OPTION_ERROR ("Option requires a single-character suboption: -", CurrentChar);
    249  1.1.1.2.20.1     tls 
    250  1.1.1.2.20.1     tls             CurrentCharPtr = 1;
    251  1.1.1.2.20.1     tls             return ('?');
    252  1.1.1.2.20.1     tls         }
    253  1.1.1.2.20.1     tls 
    254  1.1.1.2.20.1     tls         AcpiGbl_SubOptChar = AcpiGbl_Optarg[0];
    255           1.1  jruoho         AcpiGbl_Optind++;
    256           1.1  jruoho         CurrentCharPtr = 1;
    257           1.1  jruoho     }
    258           1.1  jruoho 
    259           1.1  jruoho     /* Option with no arguments */
    260           1.1  jruoho 
    261           1.1  jruoho     else
    262           1.1  jruoho     {
    263           1.1  jruoho         if (argv[AcpiGbl_Optind][++CurrentCharPtr] == '\0')
    264           1.1  jruoho         {
    265           1.1  jruoho             CurrentCharPtr = 1;
    266           1.1  jruoho             AcpiGbl_Optind++;
    267           1.1  jruoho         }
    268           1.1  jruoho 
    269           1.1  jruoho         AcpiGbl_Optarg = NULL;
    270           1.1  jruoho     }
    271           1.1  jruoho 
    272           1.1  jruoho     return (CurrentChar);
    273           1.1  jruoho }
    274