Home | History | Annotate | Line # | Download | only in common
getopt.c revision 1.1.1.2.4.2
      1  1.1.1.2.4.2  rmind 
      2  1.1.1.2.4.2  rmind /******************************************************************************
      3  1.1.1.2.4.2  rmind  *
      4  1.1.1.2.4.2  rmind  * Module Name: getopt
      5  1.1.1.2.4.2  rmind  *
      6  1.1.1.2.4.2  rmind  *****************************************************************************/
      7  1.1.1.2.4.2  rmind 
      8  1.1.1.2.4.2  rmind /*
      9  1.1.1.2.4.2  rmind  * Copyright (C) 2000 - 2011, Intel Corp.
     10  1.1.1.2.4.2  rmind  * All rights reserved.
     11  1.1.1.2.4.2  rmind  *
     12  1.1.1.2.4.2  rmind  * Redistribution and use in source and binary forms, with or without
     13  1.1.1.2.4.2  rmind  * modification, are permitted provided that the following conditions
     14  1.1.1.2.4.2  rmind  * are met:
     15  1.1.1.2.4.2  rmind  * 1. Redistributions of source code must retain the above copyright
     16  1.1.1.2.4.2  rmind  *    notice, this list of conditions, and the following disclaimer,
     17  1.1.1.2.4.2  rmind  *    without modification.
     18  1.1.1.2.4.2  rmind  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     19  1.1.1.2.4.2  rmind  *    substantially similar to the "NO WARRANTY" disclaimer below
     20  1.1.1.2.4.2  rmind  *    ("Disclaimer") and any redistribution must be conditioned upon
     21  1.1.1.2.4.2  rmind  *    including a substantially similar Disclaimer requirement for further
     22  1.1.1.2.4.2  rmind  *    binary redistribution.
     23  1.1.1.2.4.2  rmind  * 3. Neither the names of the above-listed copyright holders nor the names
     24  1.1.1.2.4.2  rmind  *    of any contributors may be used to endorse or promote products derived
     25  1.1.1.2.4.2  rmind  *    from this software without specific prior written permission.
     26  1.1.1.2.4.2  rmind  *
     27  1.1.1.2.4.2  rmind  * Alternatively, this software may be distributed under the terms of the
     28  1.1.1.2.4.2  rmind  * GNU General Public License ("GPL") version 2 as published by the Free
     29  1.1.1.2.4.2  rmind  * Software Foundation.
     30  1.1.1.2.4.2  rmind  *
     31  1.1.1.2.4.2  rmind  * NO WARRANTY
     32  1.1.1.2.4.2  rmind  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     33  1.1.1.2.4.2  rmind  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     34  1.1.1.2.4.2  rmind  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     35  1.1.1.2.4.2  rmind  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     36  1.1.1.2.4.2  rmind  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37  1.1.1.2.4.2  rmind  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38  1.1.1.2.4.2  rmind  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39  1.1.1.2.4.2  rmind  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     40  1.1.1.2.4.2  rmind  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     41  1.1.1.2.4.2  rmind  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     42  1.1.1.2.4.2  rmind  * POSSIBILITY OF SUCH DAMAGES.
     43  1.1.1.2.4.2  rmind  */
     44  1.1.1.2.4.2  rmind 
     45  1.1.1.2.4.2  rmind 
     46  1.1.1.2.4.2  rmind #include <stdio.h>
     47  1.1.1.2.4.2  rmind #include <string.h>
     48  1.1.1.2.4.2  rmind #include "acpi.h"
     49  1.1.1.2.4.2  rmind #include "accommon.h"
     50  1.1.1.2.4.2  rmind #include "acapps.h"
     51  1.1.1.2.4.2  rmind 
     52  1.1.1.2.4.2  rmind #define ERR(szz,czz) if(AcpiGbl_Opterr){fprintf(stderr,"%s%s%c\n",argv[0],szz,czz);}
     53  1.1.1.2.4.2  rmind 
     54  1.1.1.2.4.2  rmind 
     55  1.1.1.2.4.2  rmind int   AcpiGbl_Opterr = 1;
     56  1.1.1.2.4.2  rmind int   AcpiGbl_Optind = 1;
     57  1.1.1.2.4.2  rmind char  *AcpiGbl_Optarg;
     58  1.1.1.2.4.2  rmind 
     59  1.1.1.2.4.2  rmind 
     60  1.1.1.2.4.2  rmind /*******************************************************************************
     61  1.1.1.2.4.2  rmind  *
     62  1.1.1.2.4.2  rmind  * FUNCTION:    AcpiGetopt
     63  1.1.1.2.4.2  rmind  *
     64  1.1.1.2.4.2  rmind  * PARAMETERS:  argc, argv          - from main
     65  1.1.1.2.4.2  rmind  *              opts                - options info list
     66  1.1.1.2.4.2  rmind  *
     67  1.1.1.2.4.2  rmind  * RETURN:      Option character or EOF
     68  1.1.1.2.4.2  rmind  *
     69  1.1.1.2.4.2  rmind  * DESCRIPTION: Get the next option
     70  1.1.1.2.4.2  rmind  *
     71  1.1.1.2.4.2  rmind  ******************************************************************************/
     72  1.1.1.2.4.2  rmind 
     73  1.1.1.2.4.2  rmind int
     74  1.1.1.2.4.2  rmind AcpiGetopt(
     75  1.1.1.2.4.2  rmind     int                     argc,
     76  1.1.1.2.4.2  rmind     char                    **argv,
     77  1.1.1.2.4.2  rmind     char                    *opts)
     78  1.1.1.2.4.2  rmind {
     79  1.1.1.2.4.2  rmind     static int              CurrentCharPtr = 1;
     80  1.1.1.2.4.2  rmind     int                     CurrentChar;
     81  1.1.1.2.4.2  rmind     char                    *OptsPtr;
     82  1.1.1.2.4.2  rmind 
     83  1.1.1.2.4.2  rmind 
     84  1.1.1.2.4.2  rmind     if (CurrentCharPtr == 1)
     85  1.1.1.2.4.2  rmind     {
     86  1.1.1.2.4.2  rmind         if (AcpiGbl_Optind >= argc ||
     87  1.1.1.2.4.2  rmind             argv[AcpiGbl_Optind][0] != '-' ||
     88  1.1.1.2.4.2  rmind             argv[AcpiGbl_Optind][1] == '\0')
     89  1.1.1.2.4.2  rmind         {
     90  1.1.1.2.4.2  rmind             return(EOF);
     91  1.1.1.2.4.2  rmind         }
     92  1.1.1.2.4.2  rmind         else if (strcmp (argv[AcpiGbl_Optind], "--") == 0)
     93  1.1.1.2.4.2  rmind         {
     94  1.1.1.2.4.2  rmind             AcpiGbl_Optind++;
     95  1.1.1.2.4.2  rmind             return(EOF);
     96  1.1.1.2.4.2  rmind         }
     97  1.1.1.2.4.2  rmind     }
     98  1.1.1.2.4.2  rmind 
     99  1.1.1.2.4.2  rmind     /* Get the option */
    100  1.1.1.2.4.2  rmind 
    101  1.1.1.2.4.2  rmind     CurrentChar = argv[AcpiGbl_Optind][CurrentCharPtr];
    102  1.1.1.2.4.2  rmind 
    103  1.1.1.2.4.2  rmind     /* Make sure that the option is legal */
    104  1.1.1.2.4.2  rmind 
    105  1.1.1.2.4.2  rmind     if (CurrentChar == ':' ||
    106  1.1.1.2.4.2  rmind        (OptsPtr = strchr (opts, CurrentChar)) == NULL)
    107  1.1.1.2.4.2  rmind     {
    108  1.1.1.2.4.2  rmind         ERR (": illegal option -- ", CurrentChar);
    109  1.1.1.2.4.2  rmind 
    110  1.1.1.2.4.2  rmind         if (argv[AcpiGbl_Optind][++CurrentCharPtr] == '\0')
    111  1.1.1.2.4.2  rmind         {
    112  1.1.1.2.4.2  rmind             AcpiGbl_Optind++;
    113  1.1.1.2.4.2  rmind             CurrentCharPtr = 1;
    114  1.1.1.2.4.2  rmind         }
    115  1.1.1.2.4.2  rmind 
    116  1.1.1.2.4.2  rmind         return ('?');
    117  1.1.1.2.4.2  rmind     }
    118  1.1.1.2.4.2  rmind 
    119  1.1.1.2.4.2  rmind     /* Option requires an argument? */
    120  1.1.1.2.4.2  rmind 
    121  1.1.1.2.4.2  rmind     if (*++OptsPtr == ':')
    122  1.1.1.2.4.2  rmind     {
    123  1.1.1.2.4.2  rmind         if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
    124  1.1.1.2.4.2  rmind         {
    125  1.1.1.2.4.2  rmind             AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][(int) (CurrentCharPtr+1)];
    126  1.1.1.2.4.2  rmind         }
    127  1.1.1.2.4.2  rmind         else if (++AcpiGbl_Optind >= argc)
    128  1.1.1.2.4.2  rmind         {
    129  1.1.1.2.4.2  rmind             ERR (": option requires an argument -- ", CurrentChar);
    130  1.1.1.2.4.2  rmind 
    131  1.1.1.2.4.2  rmind             CurrentCharPtr = 1;
    132  1.1.1.2.4.2  rmind             return ('?');
    133  1.1.1.2.4.2  rmind         }
    134  1.1.1.2.4.2  rmind         else
    135  1.1.1.2.4.2  rmind         {
    136  1.1.1.2.4.2  rmind             AcpiGbl_Optarg = argv[AcpiGbl_Optind++];
    137  1.1.1.2.4.2  rmind         }
    138  1.1.1.2.4.2  rmind 
    139  1.1.1.2.4.2  rmind         CurrentCharPtr = 1;
    140  1.1.1.2.4.2  rmind     }
    141  1.1.1.2.4.2  rmind 
    142  1.1.1.2.4.2  rmind     /* Option has optional single-char arguments? */
    143  1.1.1.2.4.2  rmind 
    144  1.1.1.2.4.2  rmind     else if (*OptsPtr == '^')
    145  1.1.1.2.4.2  rmind     {
    146  1.1.1.2.4.2  rmind         if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
    147  1.1.1.2.4.2  rmind         {
    148  1.1.1.2.4.2  rmind             AcpiGbl_Optarg = &argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)];
    149  1.1.1.2.4.2  rmind         }
    150  1.1.1.2.4.2  rmind         else
    151  1.1.1.2.4.2  rmind         {
    152  1.1.1.2.4.2  rmind             AcpiGbl_Optarg = "^";
    153  1.1.1.2.4.2  rmind         }
    154  1.1.1.2.4.2  rmind 
    155  1.1.1.2.4.2  rmind         AcpiGbl_Optind++;
    156  1.1.1.2.4.2  rmind         CurrentCharPtr = 1;
    157  1.1.1.2.4.2  rmind     }
    158  1.1.1.2.4.2  rmind 
    159  1.1.1.2.4.2  rmind     /* Option with no arguments */
    160  1.1.1.2.4.2  rmind 
    161  1.1.1.2.4.2  rmind     else
    162  1.1.1.2.4.2  rmind     {
    163  1.1.1.2.4.2  rmind         if (argv[AcpiGbl_Optind][++CurrentCharPtr] == '\0')
    164  1.1.1.2.4.2  rmind         {
    165  1.1.1.2.4.2  rmind             CurrentCharPtr = 1;
    166  1.1.1.2.4.2  rmind             AcpiGbl_Optind++;
    167  1.1.1.2.4.2  rmind         }
    168  1.1.1.2.4.2  rmind 
    169  1.1.1.2.4.2  rmind         AcpiGbl_Optarg = NULL;
    170  1.1.1.2.4.2  rmind     }
    171  1.1.1.2.4.2  rmind 
    172  1.1.1.2.4.2  rmind     return (CurrentChar);
    173  1.1.1.2.4.2  rmind }
    174