home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / rcs567s.zip / diff16 / getopt1.c < prev    next >
C/C++ Source or Header  |  1994-06-25  |  4KB  |  164 lines

  1. /* Getopt for GNU.
  2.    Copyright (C) 1987, 1989 Free Software Foundation, Inc.
  3.    Modified for DOS and OS/2 on 1991/09/14 by Kai Uwe Rommel
  4.     <rommel@ars.muc.de>.
  5.  
  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 1, or (at your option)
  9.    any later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "getopt.h"
  21.  
  22. #if defined(__STDC__) || defined(MSDOS)
  23. #define CONST const
  24. #else
  25. #define CONST
  26. #endif
  27.  
  28. #if !defined (NULL)
  29. #define NULL 0
  30. #endif
  31.  
  32. int
  33. getopt_long (argc, argv, options, long_options, opt_index)
  34.      int argc;
  35.      char **argv;
  36.      CONST char *options;
  37.      CONST struct option *long_options;
  38.      int *opt_index;
  39. {
  40.   int val;
  41.  
  42.   _getopt_long_options = long_options;
  43.   _getopt_long_only = 0;
  44.   val = getopt (argc, argv, options);
  45.   if (val == 0 && opt_index != NULL)
  46.     *opt_index = option_index;
  47.   return val;
  48. }
  49.  
  50. /* Like getopt_long, but '-' as well as '+' can indicate a long option.
  51.    If an option that starts with '-' doesn't match a long option,
  52.    but does match a short option, it is parsed as a short option
  53.    instead. */
  54.  
  55. int 
  56. getopt_long_only (argc, argv, options, long_options, opt_index)
  57.      int argc;
  58.      char **argv;
  59.      CONST char *options;
  60.      CONST struct option *long_options;
  61.      int *opt_index;
  62. {
  63.   int val;
  64.  
  65.   _getopt_long_options = long_options;
  66.   _getopt_long_only = 1;
  67.   val = getopt (argc, argv, options);
  68.   if (val == 0 && opt_index != NULL)
  69.     *opt_index = option_index;
  70.   return val;
  71. }
  72.  
  73.  
  74. #ifdef TEST
  75.  
  76. #include <stdio.h>
  77.  
  78. int
  79. main (argc, argv)
  80.      int argc;
  81.      char **argv;
  82. {
  83.   int c;
  84.   int digit_optind = 0;
  85.  
  86.   while (1)
  87.     {
  88.       int this_option_optind = optind ? optind : 1;
  89.       char *name = '\0';
  90.       int option_index = 0;
  91.       static struct option long_options[] =
  92.       {
  93.     {"add", 1, 0, 0},
  94.     {"append", 0, 0, 0},
  95.     {"delete", 1, 0, 0},
  96.     {"verbose", 0, 0, 0},
  97.     {"create", 0, 0, 0},
  98.     {"file", 1, 0, 0},
  99.     {0, 0, 0, 0}
  100.       };
  101.  
  102.       c = getopt_long (argc, argv, "abc:d:0123456789",
  103.                long_options, &option_index);
  104.       if (c == EOF)
  105.     break;
  106.  
  107.       switch (c)
  108.     {
  109.     case 0:
  110.       printf ("option %s", (long_options[option_index]).name);
  111.       if (optarg)
  112.         printf (" with arg %s", optarg);
  113.       printf ("\n");
  114.       break;
  115.  
  116.     case '0':
  117.     case '1':
  118.     case '2':
  119.     case '3':
  120.     case '4':
  121.     case '5':
  122.     case '6':
  123.     case '7':
  124.     case '8':
  125.     case '9':
  126.       if (digit_optind != 0 && digit_optind != this_option_optind)
  127.         printf ("digits occur in two different argv-elements.\n");
  128.       digit_optind = this_option_optind;
  129.       printf ("option %c\n", c);
  130.       break;
  131.  
  132.     case 'a':
  133.       printf ("option a\n");
  134.       break;
  135.  
  136.     case 'b':
  137.       printf ("option b\n");
  138.       break;
  139.  
  140.     case 'c':
  141.       printf ("option c with value `%s'\n", optarg);
  142.       break;
  143.  
  144.     case '?':
  145.       break;
  146.  
  147.     default:
  148.       printf ("?? getopt returned character code 0%o ??\n", c);
  149.     }
  150.     }
  151.  
  152.   if (optind < argc)
  153.     {
  154.       printf ("non-option ARGV-elements: ");
  155.       while (optind < argc)
  156.     printf ("%s ", argv[optind++]);
  157.       printf ("\n");
  158.     }
  159.  
  160.   exit (0);
  161. }
  162.  
  163. #endif /* TEST */
  164.