No empty .Rs/.Re
[netbsd-mini2440.git] / usr.bin / sed / getopt1.c
blob781673c6f210f8e5eadd94542534dd60fd4b3bac
1 /* Getopt for GNU.
2 Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
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.
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.
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. */
18 #include "getopt.h"
20 #ifndef __STDC__
21 #define const
22 #endif
24 #if !defined (NULL)
25 #define NULL 0
26 #endif
28 int
29 getopt_long (argc, argv, options, long_options, opt_index)
30 int argc;
31 char **argv;
32 const char *options;
33 const struct option *long_options;
34 int *opt_index;
36 int val;
38 _getopt_long_options = long_options;
39 val = getopt (argc, argv, options);
40 if (val == 0 && opt_index != NULL)
41 *opt_index = option_index;
42 return val;
45 /* Like getopt_long, but '-' as well as '+' can indicate a long option.
46 If an option that starts with '-' doesn't match a long option,
47 but does match a short option, it is parsed as a short option
48 instead. */
50 int
51 getopt_long_only (argc, argv, options, long_options, opt_index)
52 int argc;
53 char **argv;
54 const char *options;
55 const struct option *long_options;
56 int *opt_index;
58 int val;
60 _getopt_long_options = long_options;
61 _getopt_long_only = 1;
62 val = getopt (argc, argv, options);
63 if (val == 0 && opt_index != NULL)
64 *opt_index = option_index;
65 return val;
69 #ifdef TEST
71 #include <stdio.h>
73 int
74 main (argc, argv)
75 int argc;
76 char **argv;
78 int c;
79 int digit_optind = 0;
81 while (1)
83 int this_option_optind = optind ? optind : 1;
84 char *name = '\0';
85 int option_index = 0;
86 static struct option long_options[] =
88 {"add", 1, 0, 0},
89 {"append", 0, 0, 0},
90 {"delete", 1, 0, 0},
91 {"verbose", 0, 0, 0},
92 {"create", 0, 0, 0},
93 {"file", 1, 0, 0},
94 {0, 0, 0, 0}
97 c = getopt_long (argc, argv, "abc:d:0123456789",
98 long_options, &option_index);
99 if (c == EOF)
100 break;
102 switch (c)
104 case 0:
105 printf ("option %s", (long_options[option_index]).name);
106 if (optarg)
107 printf (" with arg %s", optarg);
108 printf ("\n");
109 break;
111 case '0':
112 case '1':
113 case '2':
114 case '3':
115 case '4':
116 case '5':
117 case '6':
118 case '7':
119 case '8':
120 case '9':
121 if (digit_optind != 0 && digit_optind != this_option_optind)
122 printf ("digits occur in two different argv-elements.\n");
123 digit_optind = this_option_optind;
124 printf ("option %c\n", c);
125 break;
127 case 'a':
128 printf ("option a\n");
129 break;
131 case 'b':
132 printf ("option b\n");
133 break;
135 case 'c':
136 printf ("option c with value `%s'\n", optarg);
137 break;
139 case '?':
140 break;
142 default:
143 printf ("?? getopt returned character code 0%o ??\n", c);
147 if (optind < argc)
149 printf ("non-option ARGV-elements: ");
150 while (optind < argc)
151 printf ("%s ", argv[optind++]);
152 printf ("\n");
155 exit (0);
158 #endif /* TEST */