home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / online / source / c / compilers / Bison.sit.hqx / Bison / Source / getopt1.c < prev    next >
Text File  |  1992-08-21  |  4KB  |  170 lines

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