home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / tinfo210.lzh / TINFO210 / C / GETOPT1.C < prev    next >
C/C++ Source or Header  |  1991-08-30  |  4KB  |  167 lines

  1. /* Getopt for GNU.
  2.    Copyright (C) 1987, 88, 89, 90, 91 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 2, 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. #ifndef __STDC__
  21. #define const
  22. #endif
  23.  
  24. #if defined(STDC_HEADERS) || defined(__GNU_LIBRARY__)
  25. #include <stdlib.h>
  26. #else /* STDC_HEADERS or __GNU_LIBRARY__ */
  27. char *getenv ();
  28. #endif /* STDC_HEADERS or __GNU_LIBRARY__ */
  29.  
  30. #if !defined (NULL)
  31. #define NULL 0
  32. #endif
  33.  
  34. int
  35. getopt_long (argc, argv, options, long_options, opt_index)
  36.      int argc;
  37.      char **argv;
  38.      const char *options;
  39.      const struct option *long_options;
  40.      int *opt_index;
  41. {
  42.   int val;
  43.  
  44.   /* For strict POSIX compatibility, we must turn off long options.  */
  45.   if (getenv ("POSIX_ME_HARDER") == 0)
  46.     _getopt_long_options = long_options;
  47.   val = getopt (argc, argv, options);
  48.   if (val == 0 && opt_index != NULL)
  49.     *opt_index = option_index;
  50.   return val;
  51. }
  52.  
  53. /* Like getopt_long, but '-' as well as '+' can indicate a long option.
  54.    If an option that starts with '-' doesn't match a long option,
  55.    but does match a short option, it is parsed as a short option
  56.    instead. */
  57.  
  58. int 
  59. getopt_long_only (argc, argv, options, long_options, opt_index)
  60.      int argc;
  61.      char **argv;
  62.      const char *options;
  63.      const struct option *long_options;
  64.      int *opt_index;
  65. {
  66.   int val;
  67.  
  68.   _getopt_long_options = long_options;
  69.   _getopt_long_only = 1;
  70.   val = getopt (argc, argv, options);
  71.   if (val == 0 && opt_index != NULL)
  72.     *opt_index = option_index;
  73.   return val;
  74. }
  75.  
  76.  
  77. #ifdef TEST
  78.  
  79. #include <stdio.h>
  80.  
  81. int
  82. main (argc, argv)
  83.      int argc;
  84.      char **argv;
  85. {
  86.   int c;
  87.   int digit_optind = 0;
  88.  
  89.   while (1)
  90.     {
  91.       int this_option_optind = optind ? optind : 1;
  92.       char *name = '\0';
  93.       int option_index = 0;
  94.       static struct option long_options[] =
  95.       {
  96.     {"add", 1, 0, 0},
  97.     {"append", 0, 0, 0},
  98.     {"delete", 1, 0, 0},
  99.     {"verbose", 0, 0, 0},
  100.     {"create", 0, 0, 0},
  101.     {"file", 1, 0, 0},
  102.     {0, 0, 0, 0}
  103.       };
  104.  
  105.       c = getopt_long (argc, argv, "abc:d:0123456789",
  106.                long_options, &option_index);
  107.       if (c == EOF)
  108.     break;
  109.  
  110.       switch (c)
  111.     {
  112.     case 0:
  113.       printf ("option %s", (long_options[option_index]).name);
  114.       if (optarg)
  115.         printf (" with arg %s", optarg);
  116.       printf ("\n");
  117.       break;
  118.  
  119.     case '0':
  120.     case '1':
  121.     case '2':
  122.     case '3':
  123.     case '4':
  124.     case '5':
  125.     case '6':
  126.     case '7':
  127.     case '8':
  128.     case '9':
  129.       if (digit_optind != 0 && digit_optind != this_option_optind)
  130.         printf ("digits occur in two different argv-elements.\n");
  131.       digit_optind = this_option_optind;
  132.       printf ("option %c\n", c);
  133.       break;
  134.  
  135.     case 'a':
  136.       printf ("option a\n");
  137.       break;
  138.  
  139.     case 'b':
  140.       printf ("option b\n");
  141.       break;
  142.  
  143.     case 'c':
  144.       printf ("option c with value `%s'\n", optarg);
  145.       break;
  146.  
  147.     case '?':
  148.       break;
  149.  
  150.     default:
  151.       printf ("?? getopt returned character code 0%o ??\n", c);
  152.     }
  153.     }
  154.  
  155.   if (optind < argc)
  156.     {
  157.       printf ("non-option ARGV-elements: ");
  158.       while (optind < argc)
  159.     printf ("%s ", argv[optind++]);
  160.       printf ("\n");
  161.     }
  162.  
  163.   exit (0);
  164. }
  165.  
  166. #endif /* TEST */
  167.