home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 2 / DATAFILE_PDCD2.iso / utilities3 / gnu_sed_rx / c / getopt1 < prev    next >
Text File  |  1994-02-25  |  4KB  |  175 lines

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