home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / os2 / hpgl312.zip / GETOPT1.C < prev    next >
C/C++ Source or Header  |  1993-04-18  |  4KB  |  160 lines

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