rs6000: build constant via lis;rotldi
[official-gcc.git] / libgm2 / libm2pim / cgetopt.cc
bloba3954db5517e6b1d9e62a87554c7de772e2cad90
1 /* cgetopt.cc provide access to the C getopt library.
3 Copyright (C) 2009-2022 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 <unistd.h>
28 #include <stdlib.h>
29 #include <getopt.h>
30 #include <m2rts.h>
32 #define EXPORT(FUNC) m2pim ## _cgetopt_ ## FUNC
33 #define M2EXPORT(FUNC) m2pim ## _M2_cgetopt_ ## FUNC
34 #define M2LIBNAME "m2pim"
36 extern "C" {char *EXPORT(optarg);}
37 extern "C" {int EXPORT(optind);}
38 extern "C" {int EXPORT(opterr);}
39 extern "C" {int EXPORT(optopt);}
41 extern "C" char
42 EXPORT(getopt) (int argc, char *argv[], char *optstring)
44 char r = getopt (argc, argv, optstring);
46 EXPORT(optarg) = optarg;
47 EXPORT(optind) = optind;
48 EXPORT(opterr) = opterr;
49 EXPORT(optopt) = optopt;
51 if (r == (char)-1)
52 return (char)0;
53 return r;
56 extern "C" int
57 EXPORT(getopt_long) (int argc, char *argv[], char *optstring,
58 const struct option *longopts, int *longindex)
60 int r = getopt_long (argc, argv, optstring, longopts, longindex);
62 EXPORT(optarg) = optarg;
63 EXPORT(optind) = optind;
64 EXPORT(opterr) = opterr;
65 EXPORT(optopt) = optopt;
67 return r;
70 extern "C" int
71 EXPORT(getopt_long_only) (int argc, char *argv[], char *optstring,
72 const struct option *longopts, int *longindex)
74 int r = getopt_long_only (argc, argv, optstring, longopts, longindex);
76 EXPORT(optarg) = optarg;
77 EXPORT(optind) = optind;
78 EXPORT(opterr) = opterr;
79 EXPORT(optopt) = optopt;
81 return r;
84 typedef struct cgetopt_Options_s
86 struct option *cinfo;
87 unsigned int high;
88 } cgetopt_Options;
90 /* InitOptions a constructor for Options. */
92 extern "C" cgetopt_Options *
93 EXPORT(InitOptions) (void)
95 cgetopt_Options *o = (cgetopt_Options *)malloc (sizeof (cgetopt_Options));
96 o->cinfo = (struct option *)malloc (sizeof (struct option));
97 o->high = 0;
98 return o;
101 /* KillOptions a deconstructor for Options. Returns NULL after freeing
102 up all allocated memory associated with o. */
104 extern "C" cgetopt_Options *
105 EXPORT(KillOptions) (cgetopt_Options *o)
107 free (o->cinfo);
108 free (o);
109 return NULL;
112 /* SetOption set option[index] with {name, has_arg, flag, val}. */
114 extern "C" void
115 EXPORT(SetOption) (cgetopt_Options *o, unsigned int index, char *name,
116 bool has_arg, int *flag, int val)
118 if (index > o->high)
120 o->cinfo
121 = (struct option *)malloc (sizeof (struct option) * (index + 1));
122 o->high = index + 1;
124 o->cinfo[index].name = name;
125 o->cinfo[index].has_arg = has_arg;
126 o->cinfo[index].flag = flag;
127 o->cinfo[index].val = val;
130 /* GetLongOptionArray returns a pointer to the C array containing all
131 long options. */
133 extern "C" struct option *
134 EXPORT(GetLongOptionArray) (cgetopt_Options *o)
136 return o->cinfo;
139 /* GNU Modula-2 linking fodder. */
141 extern "C" void
142 M2EXPORT(init) (int, char *argv[], char *env[])
146 extern "C" void
147 M2EXPORT(fini) (int, char *argv[], char *env[])
151 extern "C" void
152 M2EXPORT(dep) (void)
156 extern "C" void __attribute__((__constructor__))
157 M2EXPORT(ctor) (void)
159 m2pim_M2RTS_RegisterModule ("cgetopt", M2LIBNAME,
160 M2EXPORT(init), M2EXPORT(fini),
161 M2EXPORT(dep));