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