libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / m2 / gm2-libs-ch / cgetopt.c
blob184f29005b64760f344a2588acd56024b19eb3e1
1 /* getopt.c provide access to the C getopt library.
3 Copyright (C) 2017-2024 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
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.
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.
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.
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/>. */
27 #include "config.h"
28 #include "system.h"
29 #include <getopt.h>
30 // #include "ansi-decl.h"
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
38 char *cgetopt_optarg;
39 int cgetopt_optind;
40 int cgetopt_opterr;
41 int cgetopt_optopt;
44 char
45 cgetopt_getopt (int argc, char *argv[], char *optstring)
47 char r = getopt (argc, argv, optstring);
49 cgetopt_optarg = optarg;
50 cgetopt_optind = optind;
51 cgetopt_opterr = opterr;
52 cgetopt_optopt = optopt;
54 if (r == (char)-1)
55 return (char)0;
56 return r;
60 int
61 cgetopt_cgetopt_long (int argc, char *argv[], char *optstring, const struct option *longopts,
62 int *longindex)
64 int r = getopt_long (argc, argv, optstring, longopts, longindex);
66 cgetopt_optarg = optarg;
67 cgetopt_optind = optind;
68 cgetopt_opterr = opterr;
69 cgetopt_optopt = optopt;
71 return r;
75 int
76 cgetopt_cgetopt_long_only (int argc, char *argv[], char *optstring,
77 const struct option *longopts, int *longindex)
79 int r = getopt_long_only (argc, argv, optstring, longopts, longindex);
81 cgetopt_optarg = optarg;
82 cgetopt_optind = optind;
83 cgetopt_opterr = opterr;
84 cgetopt_optopt = optopt;
86 return r;
90 typedef struct cgetopt_Options_s {
91 struct option *cinfo;
92 unsigned int high;
93 } cgetopt_Options;
96 /* InitOptions a constructor for Options. */
98 cgetopt_Options *
99 cgetopt_InitOptions (void)
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;
108 /* KillOptions a deconstructor for Options. Returns NULL after freeing
109 up all allocated memory associated with o. */
111 cgetopt_Options *
112 cgetopt_KillOptions (cgetopt_Options *o)
114 free (o->cinfo);
115 free (o);
116 return NULL;
120 /* SetOption set option[index] with {name, has_arg, flag, val}. */
122 void
123 cgetopt_SetOption (cgetopt_Options *o, unsigned int index,
124 char *name, int has_arg,
125 int *flag, int val)
127 if (index > o->high)
129 o->cinfo = (struct option *) malloc (sizeof (struct option) * (index + 1));
130 o->high = index + 1;
132 o->cinfo[index].name = name;
133 o->cinfo[index].has_arg = has_arg;
134 if (name == NULL)
135 flag = NULL;
136 o->cinfo[index].flag = flag;
137 o->cinfo[index].val = val;
141 /* GetLongOptionArray returns a pointer to the C array containing all
142 long options. */
144 struct option *
145 cgetopt_GetLongOptionArray (cgetopt_Options *o)
147 return o->cinfo;
151 /* GNU Modula-2 linking fodder. */
153 void
154 _M2_cgetopt_init (void)
159 void
160 _M2_cgetopt_finish (void)
164 # ifdef __cplusplus
166 # endif