home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / syslog.zip / getopt1.c < prev    next >
Text File  |  1994-10-16  |  3KB  |  142 lines

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