home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / DIFFPT.ZIP / GETOPT1.C < prev    next >
C/C++ Source or Header  |  1991-07-01  |  4KB  |  162 lines

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