Home | History | Annotate | Line # | Download | only in lib
getopt1.c revision 1.2
      1  1.1  christos /* getopt_long and getopt_long_only entry points for GNU getopt.
      2  1.1  christos    Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98,2004
      3  1.1  christos      Free Software Foundation, Inc.
      4  1.1  christos    This file is part of the GNU C Library.
      5  1.1  christos 
      6  1.1  christos    This program is free software; you can redistribute it and/or modify
      7  1.1  christos    it under the terms of the GNU General Public License as published by
      8  1.1  christos    the Free Software Foundation; either version 2, or (at your option)
      9  1.1  christos    any later version.
     10  1.1  christos 
     11  1.1  christos    This program is distributed in the hope that it will be useful,
     12  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14  1.1  christos    GNU General Public License for more details.
     15  1.1  christos 
     16  1.1  christos    You should have received a copy of the GNU General Public License along
     17  1.1  christos    with this program; if not, write to the Free Software Foundation,
     18  1.1  christos    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
     19  1.2  christos #include <sys/cdefs.h>
     20  1.2  christos __RCSID("$NetBSD: getopt1.c,v 1.2 2016/05/17 14:00:09 christos Exp $");
     21  1.2  christos 
     22  1.1  christos 
     23  1.1  christos #ifdef HAVE_CONFIG_H
     25  1.1  christos # include <config.h>
     26  1.1  christos #endif
     27  1.1  christos 
     28  1.1  christos #ifdef _LIBC
     29  1.1  christos # include <getopt.h>
     30  1.1  christos #else
     31  1.1  christos # include "getopt.h"
     32  1.1  christos #endif
     33  1.1  christos #include "getopt_int.h"
     34  1.1  christos 
     35  1.1  christos #include <stdio.h>
     36  1.1  christos 
     37  1.1  christos /* This needs to come after some library #include
     38  1.1  christos    to get __GNU_LIBRARY__ defined.  */
     39  1.1  christos #ifdef __GNU_LIBRARY__
     40  1.1  christos #include <stdlib.h>
     41  1.1  christos #endif
     42  1.1  christos 
     43  1.1  christos #ifndef	NULL
     44  1.1  christos #define NULL 0
     45  1.1  christos #endif
     46  1.1  christos 
     47  1.1  christos int
     48  1.1  christos getopt_long (int argc, char *__getopt_argv_const *argv, const char *options,
     49  1.1  christos 	     const struct option *long_options, int *opt_index)
     50  1.1  christos {
     51  1.1  christos   return _getopt_internal (argc, (char **) argv, options, long_options,
     52  1.1  christos 			   opt_index, 0, 0);
     53  1.1  christos }
     54  1.1  christos 
     55  1.1  christos int
     56  1.1  christos _getopt_long_r (int argc, char **argv, const char *options,
     57  1.1  christos 		const struct option *long_options, int *opt_index,
     58  1.1  christos 		struct _getopt_data *d)
     59  1.1  christos {
     60  1.1  christos   return _getopt_internal_r (argc, argv, options, long_options, opt_index,
     61  1.1  christos 			     0, 0, d);
     62  1.1  christos }
     63  1.1  christos 
     64  1.1  christos /* Like getopt_long, but '-' as well as '--' can indicate a long option.
     65  1.1  christos    If an option that starts with '-' (not '--') doesn't match a long option,
     66  1.1  christos    but does match a short option, it is parsed as a short option
     67  1.1  christos    instead.  */
     68  1.1  christos 
     69  1.1  christos int
     70  1.1  christos getopt_long_only (int argc, char *__getopt_argv_const *argv,
     71  1.1  christos 		  const char *options,
     72  1.1  christos 		  const struct option *long_options, int *opt_index)
     73  1.1  christos {
     74  1.1  christos   return _getopt_internal (argc, (char **) argv, options, long_options,
     75  1.1  christos 			   opt_index, 1, 0);
     76  1.1  christos }
     77  1.1  christos 
     78  1.1  christos int
     79  1.1  christos _getopt_long_only_r (int argc, char **argv, const char *options,
     80  1.1  christos 		     const struct option *long_options, int *opt_index,
     81  1.1  christos 		     struct _getopt_data *d)
     82  1.1  christos {
     83  1.1  christos   return _getopt_internal_r (argc, argv, options, long_options, opt_index,
     84  1.1  christos 			     1, 0, d);
     85  1.1  christos }
     86  1.1  christos 
     87  1.1  christos 
     88  1.1  christos #ifdef TEST
     90  1.1  christos 
     91  1.1  christos #include <stdio.h>
     92  1.1  christos 
     93  1.1  christos int
     94  1.1  christos main (int argc, char **argv)
     95  1.1  christos {
     96  1.1  christos   int c;
     97  1.1  christos   int digit_optind = 0;
     98  1.1  christos 
     99  1.1  christos   while (1)
    100  1.1  christos     {
    101  1.1  christos       int this_option_optind = optind ? optind : 1;
    102  1.1  christos       int option_index = 0;
    103  1.1  christos       static struct option long_options[] =
    104  1.1  christos       {
    105  1.1  christos 	{"add", 1, 0, 0},
    106  1.1  christos 	{"append", 0, 0, 0},
    107  1.1  christos 	{"delete", 1, 0, 0},
    108  1.1  christos 	{"verbose", 0, 0, 0},
    109  1.1  christos 	{"create", 0, 0, 0},
    110  1.1  christos 	{"file", 1, 0, 0},
    111  1.1  christos 	{0, 0, 0, 0}
    112  1.1  christos       };
    113  1.1  christos 
    114  1.1  christos       c = getopt_long (argc, argv, "abc:d:0123456789",
    115  1.1  christos 		       long_options, &option_index);
    116  1.1  christos       if (c == -1)
    117  1.1  christos 	break;
    118  1.1  christos 
    119  1.1  christos       switch (c)
    120  1.1  christos 	{
    121  1.1  christos 	case 0:
    122  1.1  christos 	  printf ("option %s", long_options[option_index].name);
    123  1.1  christos 	  if (optarg)
    124  1.1  christos 	    printf (" with arg %s", optarg);
    125  1.1  christos 	  printf ("\n");
    126  1.1  christos 	  break;
    127  1.1  christos 
    128  1.1  christos 	case '0':
    129  1.1  christos 	case '1':
    130  1.1  christos 	case '2':
    131  1.1  christos 	case '3':
    132  1.1  christos 	case '4':
    133  1.1  christos 	case '5':
    134  1.1  christos 	case '6':
    135  1.1  christos 	case '7':
    136  1.1  christos 	case '8':
    137  1.1  christos 	case '9':
    138  1.1  christos 	  if (digit_optind != 0 && digit_optind != this_option_optind)
    139  1.1  christos 	    printf ("digits occur in two different argv-elements.\n");
    140  1.1  christos 	  digit_optind = this_option_optind;
    141  1.1  christos 	  printf ("option %c\n", c);
    142  1.1  christos 	  break;
    143  1.1  christos 
    144  1.1  christos 	case 'a':
    145  1.1  christos 	  printf ("option a\n");
    146  1.1  christos 	  break;
    147  1.1  christos 
    148  1.1  christos 	case 'b':
    149  1.1  christos 	  printf ("option b\n");
    150  1.1  christos 	  break;
    151  1.1  christos 
    152  1.1  christos 	case 'c':
    153  1.1  christos 	  printf ("option c with value `%s'\n", optarg);
    154  1.1  christos 	  break;
    155  1.1  christos 
    156  1.1  christos 	case 'd':
    157  1.1  christos 	  printf ("option d with value `%s'\n", optarg);
    158  1.1  christos 	  break;
    159  1.1  christos 
    160  1.1  christos 	case '?':
    161  1.1  christos 	  break;
    162  1.1  christos 
    163  1.1  christos 	default:
    164  1.1  christos 	  printf ("?? getopt returned character code 0%o ??\n", c);
    165  1.1  christos 	}
    166  1.1  christos     }
    167  1.1  christos 
    168  1.1  christos   if (optind < argc)
    169  1.1  christos     {
    170  1.1  christos       printf ("non-option ARGV-elements: ");
    171  1.1  christos       while (optind < argc)
    172  1.1  christos 	printf ("%s ", argv[optind++]);
    173  1.1  christos       printf ("\n");
    174  1.1  christos     }
    175  1.1  christos 
    176  1.1  christos   exit (0);
    177  1.1  christos }
    178                
    179                #endif /* TEST */
    180