]>
Commit | Line | Data |
---|---|---|
1eee94d3 GM |
1 | /* getopt.c provide access to the C getopt library. |
2 | ||
a945c346 | 3 | Copyright (C) 2017-2024 Free Software Foundation, Inc. |
1eee94d3 GM |
4 | Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>. |
5 | ||
6 | This file is part of GNU Modula-2. | |
7 | ||
8 | GNU Modula-2 is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 3, or (at your option) | |
11 | any later version. | |
12 | ||
13 | GNU Modula-2 is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | General Public License for more details. | |
17 | ||
18 | Under Section 7 of GPL version 3, you are granted additional | |
19 | permissions described in the GCC Runtime Library Exception, version | |
20 | 3.1, as published by the Free Software Foundation. | |
21 | ||
22 | You should have received a copy of the GNU General Public License and | |
23 | a copy of the GCC Runtime Library Exception along with this program; | |
24 | see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
25 | <http://www.gnu.org/licenses/>. */ | |
26 | ||
27 | #include "config.h" | |
28 | #include "system.h" | |
5bb27a1b GM |
29 | #include <getopt.h> |
30 | // #include "ansi-decl.h" | |
1eee94d3 GM |
31 | |
32 | ||
33 | #ifdef __cplusplus | |
34 | extern "C" { | |
35 | #endif | |
36 | ||
37 | ||
38 | char *cgetopt_optarg; | |
39 | int cgetopt_optind; | |
40 | int cgetopt_opterr; | |
41 | int cgetopt_optopt; | |
42 | ||
43 | ||
44 | char | |
45 | cgetopt_getopt (int argc, char *argv[], char *optstring) | |
46 | { | |
47 | char r = getopt (argc, argv, optstring); | |
48 | ||
49 | cgetopt_optarg = optarg; | |
50 | cgetopt_optind = optind; | |
51 | cgetopt_opterr = opterr; | |
52 | cgetopt_optopt = optopt; | |
53 | ||
54 | if (r == (char)-1) | |
55 | return (char)0; | |
56 | return r; | |
57 | } | |
58 | ||
59 | ||
60 | int | |
61 | cgetopt_cgetopt_long (int argc, char *argv[], char *optstring, const struct option *longopts, | |
e5f6a5ad | 62 | int *longindex) |
1eee94d3 | 63 | { |
5bb27a1b | 64 | int r = getopt_long (argc, argv, optstring, longopts, longindex); |
1eee94d3 GM |
65 | |
66 | cgetopt_optarg = optarg; | |
67 | cgetopt_optind = optind; | |
68 | cgetopt_opterr = opterr; | |
69 | cgetopt_optopt = optopt; | |
70 | ||
71 | return r; | |
72 | } | |
73 | ||
74 | ||
75 | int | |
76 | cgetopt_cgetopt_long_only (int argc, char *argv[], char *optstring, | |
e5f6a5ad | 77 | const struct option *longopts, int *longindex) |
1eee94d3 | 78 | { |
5bb27a1b | 79 | int r = getopt_long_only (argc, argv, optstring, longopts, longindex); |
1eee94d3 GM |
80 | |
81 | cgetopt_optarg = optarg; | |
82 | cgetopt_optind = optind; | |
83 | cgetopt_opterr = opterr; | |
84 | cgetopt_optopt = optopt; | |
85 | ||
86 | return r; | |
87 | } | |
88 | ||
89 | ||
90 | typedef struct cgetopt_Options_s { | |
91 | struct option *cinfo; | |
92 | unsigned int high; | |
93 | } cgetopt_Options; | |
94 | ||
95 | ||
96 | /* InitOptions a constructor for Options. */ | |
97 | ||
98 | cgetopt_Options * | |
99 | cgetopt_InitOptions (void) | |
100 | { | |
101 | cgetopt_Options *o = (cgetopt_Options *) malloc (sizeof (cgetopt_Options)); | |
102 | o->cinfo = (struct option *) malloc (sizeof (struct option)); | |
103 | o->high = 0; | |
104 | return o; | |
105 | } | |
106 | ||
107 | ||
108 | /* KillOptions a deconstructor for Options. Returns NULL after freeing | |
109 | up all allocated memory associated with o. */ | |
110 | ||
111 | cgetopt_Options * | |
112 | cgetopt_KillOptions (cgetopt_Options *o) | |
113 | { | |
114 | free (o->cinfo); | |
115 | free (o); | |
116 | return NULL; | |
117 | } | |
118 | ||
119 | ||
120 | /* SetOption set option[index] with {name, has_arg, flag, val}. */ | |
121 | ||
122 | void | |
123 | cgetopt_SetOption (cgetopt_Options *o, unsigned int index, | |
e5f6a5ad GM |
124 | char *name, int has_arg, |
125 | int *flag, int val) | |
1eee94d3 GM |
126 | { |
127 | if (index > o->high) | |
128 | { | |
129 | o->cinfo = (struct option *) malloc (sizeof (struct option) * (index + 1)); | |
130 | o->high = index + 1; | |
131 | } | |
132 | o->cinfo[index].name = name; | |
133 | o->cinfo[index].has_arg = has_arg; | |
e5f6a5ad GM |
134 | if (name == NULL) |
135 | flag = NULL; | |
1eee94d3 GM |
136 | o->cinfo[index].flag = flag; |
137 | o->cinfo[index].val = val; | |
138 | } | |
139 | ||
140 | ||
141 | /* GetLongOptionArray returns a pointer to the C array containing all | |
142 | long options. */ | |
143 | ||
144 | struct option * | |
145 | cgetopt_GetLongOptionArray (cgetopt_Options *o) | |
146 | { | |
147 | return o->cinfo; | |
148 | } | |
149 | ||
150 | ||
151 | /* GNU Modula-2 linking fodder. */ | |
152 | ||
153 | void | |
154 | _M2_cgetopt_init (void) | |
155 | { | |
156 | } | |
157 | ||
158 | ||
159 | void | |
160 | _M2_cgetopt_finish (void) | |
161 | { | |
162 | } | |
163 | ||
164 | # ifdef __cplusplus | |
165 | } | |
166 | # endif |