1 /* getopt.c provide access to the C getopt library.
2
3 Copyright (C) 2017-2023 Free Software Foundation, Inc.
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"
29 #include <getopt.h>
30 // #include "ansi-decl.h"
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,
62 int *longindex)
63 {
64 int r = getopt_long (argc, argv, optstring, longopts, longindex);
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,
77 const struct option *longopts, int *longindex)
78 {
79 int r = getopt_long_only (argc, argv, optstring, longopts, longindex);
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,
124 char *name, bool has_arg,
125 int *flag, int val)
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;
134 o->cinfo[index].flag = flag;
135 o->cinfo[index].val = val;
136 }
137
138
139 /* GetLongOptionArray returns a pointer to the C array containing all
140 long options. */
141
142 struct option *
143 cgetopt_GetLongOptionArray (cgetopt_Options *o)
144 {
145 return o->cinfo;
146 }
147
148
149 /* GNU Modula-2 linking fodder. */
150
151 void
152 _M2_cgetopt_init (void)
153 {
154 }
155
156
157 void
158 _M2_cgetopt_finish (void)
159 {
160 }
161
162 # ifdef __cplusplus
163 }
164 # endif