]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/m2/gm2-libs-ch/cgetopt.c
Update copyright years.
[thirdparty/gcc.git] / gcc / m2 / gm2-libs-ch / cgetopt.c
CommitLineData
1eee94d3
GM
1/* getopt.c provide access to the C getopt library.
2
a945c346 3Copyright (C) 2017-2024 Free Software Foundation, Inc.
1eee94d3
GM
4Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
5
6This file is part of GNU Modula-2.
7
8GNU Modula-2 is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 3, or (at your option)
11any later version.
12
13GNU Modula-2 is distributed in the hope that it will be useful, but
14WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16General Public License for more details.
17
18Under Section 7 of GPL version 3, you are granted additional
19permissions described in the GCC Runtime Library Exception, version
203.1, as published by the Free Software Foundation.
21
22You should have received a copy of the GNU General Public License and
23a copy of the GCC Runtime Library Exception along with this program;
24see 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
34extern "C" {
35#endif
36
37
38char *cgetopt_optarg;
39int cgetopt_optind;
40int cgetopt_opterr;
41int cgetopt_optopt;
42
43
44char
45cgetopt_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
60int
61cgetopt_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
75int
76cgetopt_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
90typedef struct cgetopt_Options_s {
91 struct option *cinfo;
92 unsigned int high;
93} cgetopt_Options;
94
95
96/* InitOptions a constructor for Options. */
97
98cgetopt_Options *
99cgetopt_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
111cgetopt_Options *
112cgetopt_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
122void
123cgetopt_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
144struct option *
145cgetopt_GetLongOptionArray (cgetopt_Options *o)
146{
147 return o->cinfo;
148}
149
150
151/* GNU Modula-2 linking fodder. */
152
153void
154_M2_cgetopt_init (void)
155{
156}
157
158
159void
160_M2_cgetopt_finish (void)
161{
162}
163
164# ifdef __cplusplus
165}
166# endif