f31c42f8a8d2fa6c2e16c4762eb9741187465188
[platform/upstream/groff.git] / src / libs / libgroff / getopt1.c
1 /* getopt_long and getopt_long_only entry points for GNU getopt.
2    Copyright (C) 1987-2014  Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any 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, see <http://www.gnu.org/licenses/>.  */
17 \f
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21
22 #ifdef _LIBC
23 # include <getopt.h>
24 #else
25 # include "getopt.h"
26 #endif
27 #include "getopt_int.h"
28
29 #include <stdio.h>
30
31 /* This needs to come after some library #include
32    to get __GNU_LIBRARY__ defined.  */
33 #ifdef __GNU_LIBRARY__
34 #include <stdlib.h>
35 #endif
36
37 #ifndef NULL
38 #define NULL 0
39 #endif
40
41 int
42 getopt_long (int argc, char *__getopt_argv_const *argv, const char *options,
43              const struct option *long_options, int *opt_index)
44 {
45   return _getopt_internal (argc, (char **) argv, options, long_options,
46                            opt_index, 0, 0);
47 }
48
49 int
50 _getopt_long_r (int argc, char **argv, const char *options,
51                 const struct option *long_options, int *opt_index,
52                 struct _getopt_data *d)
53 {
54   return _getopt_internal_r (argc, argv, options, long_options, opt_index,
55                              0, 0, d);
56 }
57
58 /* Like getopt_long, but '-' as well as '--' can indicate a long option.
59    If an option that starts with '-' (not '--') doesn't match a long option,
60    but does match a short option, it is parsed as a short option
61    instead.  */
62
63 int
64 getopt_long_only (int argc, char *__getopt_argv_const *argv,
65                   const char *options,
66                   const struct option *long_options, int *opt_index)
67 {
68   return _getopt_internal (argc, (char **) argv, options, long_options,
69                            opt_index, 1, 0);
70 }
71
72 int
73 _getopt_long_only_r (int argc, char **argv, const char *options,
74                      const struct option *long_options, int *opt_index,
75                      struct _getopt_data *d)
76 {
77   return _getopt_internal_r (argc, argv, options, long_options, opt_index,
78                              1, 0, d);
79 }
80
81 \f
82 #ifdef TEST
83
84 #include <stdio.h>
85
86 int
87 main (int argc, char **argv)
88 {
89   int c;
90   int digit_optind = 0;
91
92   while (1)
93     {
94       int this_option_optind = optind ? optind : 1;
95       int option_index = 0;
96       static struct option long_options[] =
97       {
98         {"add", 1, 0, 0},
99         {"append", 0, 0, 0},
100         {"delete", 1, 0, 0},
101         {"verbose", 0, 0, 0},
102         {"create", 0, 0, 0},
103         {"file", 1, 0, 0},
104         {0, 0, 0, 0}
105       };
106
107       c = getopt_long (argc, argv, "abc:d:0123456789",
108                        long_options, &option_index);
109       if (c == -1)
110         break;
111
112       switch (c)
113         {
114         case 0:
115           printf ("option %s", long_options[option_index].name);
116           if (optarg)
117             printf (" with arg %s", optarg);
118           printf ("\n");
119           break;
120
121         case '0':
122         case '1':
123         case '2':
124         case '3':
125         case '4':
126         case '5':
127         case '6':
128         case '7':
129         case '8':
130         case '9':
131           if (digit_optind != 0 && digit_optind != this_option_optind)
132             printf ("digits occur in two different argv-elements.\n");
133           digit_optind = this_option_optind;
134           printf ("option %c\n", c);
135           break;
136
137         case 'a':
138           printf ("option a\n");
139           break;
140
141         case 'b':
142           printf ("option b\n");
143           break;
144
145         case 'c':
146           printf ("option c with value `%s'\n", optarg);
147           break;
148
149         case 'd':
150           printf ("option d with value `%s'\n", optarg);
151           break;
152
153         case '?':
154           break;
155
156         default:
157           printf ("?? getopt returned character code 0%o ??\n", c);
158         }
159     }
160
161   if (optind < argc)
162     {
163       printf ("non-option ARGV-elements: ");
164       while (optind < argc)
165         printf ("%s ", argv[optind++]);
166       printf ("\n");
167     }
168
169   exit (0);
170 }
171
172 #endif /* TEST */