]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/m2/gm2-libs-ch/cgetopt.c
Update copyright years.
[thirdparty/gcc.git] / gcc / m2 / gm2-libs-ch / cgetopt.c
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 "ansi-decl.h"
30
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36
37 char *cgetopt_optarg;
38 int cgetopt_optind;
39 int cgetopt_opterr;
40 int cgetopt_optopt;
41
42
43 char
44 cgetopt_getopt (int argc, char *argv[], char *optstring)
45 {
46 char r = getopt (argc, argv, optstring);
47
48 cgetopt_optarg = optarg;
49 cgetopt_optind = optind;
50 cgetopt_opterr = opterr;
51 cgetopt_optopt = optopt;
52
53 if (r == (char)-1)
54 return (char)0;
55 return r;
56 }
57
58
59 int
60 cgetopt_cgetopt_long (int argc, char *argv[], char *optstring, const struct option *longopts,
61 int *longindex)
62 {
63 int r = cgetopt_long (argc, argv, optstring, longopts, longindex);
64
65 cgetopt_optarg = optarg;
66 cgetopt_optind = optind;
67 cgetopt_opterr = opterr;
68 cgetopt_optopt = optopt;
69
70 return r;
71 }
72
73
74 int
75 cgetopt_cgetopt_long_only (int argc, char *argv[], char *optstring,
76 const struct option *longopts, int *longindex)
77 {
78 int r = cgetopt_long_only (argc, argv, optstring, longopts, longindex);
79
80 cgetopt_optarg = optarg;
81 cgetopt_optind = optind;
82 cgetopt_opterr = opterr;
83 cgetopt_optopt = optopt;
84
85 return r;
86 }
87
88
89 typedef struct cgetopt_Options_s {
90 struct option *cinfo;
91 unsigned int high;
92 } cgetopt_Options;
93
94
95 /* InitOptions a constructor for Options. */
96
97 cgetopt_Options *
98 cgetopt_InitOptions (void)
99 {
100 cgetopt_Options *o = (cgetopt_Options *) malloc (sizeof (cgetopt_Options));
101 o->cinfo = (struct option *) malloc (sizeof (struct option));
102 o->high = 0;
103 return o;
104 }
105
106
107 /* KillOptions a deconstructor for Options. Returns NULL after freeing
108 up all allocated memory associated with o. */
109
110 cgetopt_Options *
111 cgetopt_KillOptions (cgetopt_Options *o)
112 {
113 free (o->cinfo);
114 free (o);
115 return NULL;
116 }
117
118
119 /* SetOption set option[index] with {name, has_arg, flag, val}. */
120
121 void
122 cgetopt_SetOption (cgetopt_Options *o, unsigned int index,
123 char *name, unsigned int has_arg,
124 int *flag, int val)
125 {
126 if (index > o->high)
127 {
128 o->cinfo = (struct option *) malloc (sizeof (struct option) * (index + 1));
129 o->high = index + 1;
130 }
131 o->cinfo[index].name = name;
132 o->cinfo[index].has_arg = has_arg;
133 o->cinfo[index].flag = flag;
134 o->cinfo[index].val = val;
135 }
136
137
138 /* GetLongOptionArray returns a pointer to the C array containing all
139 long options. */
140
141 struct option *
142 cgetopt_GetLongOptionArray (cgetopt_Options *o)
143 {
144 return o->cinfo;
145 }
146
147
148 /* GNU Modula-2 linking fodder. */
149
150 void
151 _M2_cgetopt_init (void)
152 {
153 }
154
155
156 void
157 _M2_cgetopt_finish (void)
158 {
159 }
160
161 # ifdef __cplusplus
162 }
163 # endif