1 #line 982 "../../src/builtin/snarf.m4"
2 /* -*- buffer-read-only: t -*- vi: set ro:
3    THIS FILE IS GENERATED AUTOMATICALLY.  PLEASE DO NOT EDIT.
4 */
5 #line 982
6 #ifdef HAVE_CONFIG_H
7 #line 982
8 # include <config.h>
9 #line 982
10 #endif
11 #line 982
12 #include <sys/types.h>
13 #line 982
14 
15 #line 982
16 #include "mailfromd.h"
17 #line 982
18 #include "prog.h"
19 #line 982
20 #include "builtin.h"
21 #line 982
22 
23 #line 982
24 
25 #line 1022 "../../src/builtin/snarf.m4"
26 
27 /* End of snarf.m4 */
28 #line 1 "getopt.bi"
29 /* This file is part of Mailfromd.             -*- c -*-
30    Copyright (C) 2008-2021 Sergey Poznyakoff
31 
32    This program is free software; you can redistribute it and/or modify
33    it under the terms of the GNU General Public License as published by
34    the Free Software Foundation; either version 3, or (at your option)
35    any later version.
36 
37    This program is distributed in the hope that it will be useful,
38    but WITHOUT ANY WARRANTY; without even the implied warranty of
39    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
40    GNU General Public License for more details.
41 
42    You should have received a copy of the GNU General Public License
43    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
44 
45 
46 
47 #include <unistd.h>
48 #include <stdlib.h>
49 #include <getopt.h>
50 
51 static size_t optarg_loc
52 #line 23 "getopt.bi"
53 ;
54 static size_t optind_loc
55 #line 24 "getopt.bi"
56 ;
57 static size_t opterr_loc
58 #line 25 "getopt.bi"
59 ;
60 static size_t optopt_loc
61 #line 26 "getopt.bi"
62 ;
63 
64 void
65 #line 28
bi_getopt(eval_environ_t env)66 bi_getopt(eval_environ_t env)
67 #line 28
68 
69 #line 28
70 
71 #line 28 "getopt.bi"
72 {
73 #line 28
74 
75 #line 28
76 
77 #line 28
78 long __bi_argcnt;
79 #line 28
80 long  argc;
81 #line 28
82         long  argoff;
83 #line 28
84 
85 #line 28
86 get_numeric_arg(env, 1, &argc);
87 #line 28
88         get_numeric_arg(env, 2, &argoff);
89 #line 28
90 
91 #line 28
92 get_numeric_arg(env, 0, &__bi_argcnt);
93 #line 28
94         adjust_stack(env, __bi_argcnt + 1);
95 #line 28
96 
97 #line 28
98 
99 #line 28
100 	if (builtin_module_trace(BUILTIN_IDX_getopt))
101 #line 28
102 		prog_trace(env, "getopt %lu %lu",argc, argoff);;
103 #line 28
104 
105 {
106 	int rc;
107 	int long_idx;
108 	char s[2] = { 0, 0 };
109 	static char xargc;
110 	static char **xargv;
111 	static struct option *option;
112 	static char *optstr;
113 	static char *loptstr;
114 
115 	if (argc) {
116 		size_t i, n;
117 		size_t serial = 256;
118 
119 		xargc = argc + 1;
120 		xargv = mu_calloc(xargc+1, sizeof(xargv[0]));
121 		xargv[0] = script_file;
122 		for (i = 0; i < argc; i++)
123 			xargv[i+1] = env_vaptr(env, argoff + i);
124 		xargv[i+1] = NULL;
125 
126 		n = (__bi_argcnt - 2);
127 		if (n) {
128 			size_t i, j;
129 			size_t len;
130 			char *str;
131 			size_t size, lsize, lcnt;
132 
133 
134 #line 57
135 unroll_stack(env, __bi_argcnt + 1);
136 
137 			size = lsize = lcnt = 0;
138 			for (i = 0; i < n; i++) {
139 
140 #line 61
141  ((__bi_argcnt > (i+2)) ?   get_string_arg(env, (i+2) + 1, &str) :   ((
142 #line 61
143 	env_throw_bi(env, mfe_range, "getopt", "Argument %u is not supplied",(unsigned) (i+2))
144 #line 61
145 ),(char * MFL_DATASEG) 0));
146 				len = strcspn(str, "|");
147 				size += len;
148 				str += len;
149 				if (*str) {
150 					lcnt++;
151 					lsize += strlen(str);
152 				}
153 			}
154 
155 			optstr = mu_realloc(optstr, size + 1);
156 			loptstr = mu_realloc(loptstr, lsize + 1);
157 			option = mu_realloc(option,
158 					  (lcnt + 1) * sizeof(option[0]));
159 
160 			size = 0;
161 			lsize = 0;
162 			for (i = j = 0; i < n; i++) {
163 				size_t len;
164 				int val;
165 				char *flags;
166 
167 
168 #line 83
169  ((__bi_argcnt > (i+2)) ?   get_string_arg(env, (i+2) + 1, &str) :   ((
170 #line 83
171 	env_throw_bi(env, mfe_range, "getopt", "Argument %u is not supplied",(unsigned) (i+2))
172 #line 83
173 ),(char * MFL_DATASEG) 0));
174 				len = strcspn(str, "|");
175 
176 				if (len > 0) {
177 					flags = str + 1;
178 					if (i == 0 || str[0] != '-') {
179 						memcpy(optstr + size, str,
180 						       len);
181 						size += len;
182 						val = str[0];
183 					} else
184 						val = serial++;
185 				} else {
186 					flags = "";
187 					val = serial++;
188 				}
189 
190 				str += len;
191 
192 				if (*str) {
193 					option[j].name = loptstr + lsize;
194 					strcpy(loptstr + lsize, str + 1);
195 					lsize += strlen(str) + 1;
196 					if (flags[0] == ':')
197 						option[j].has_arg =
198 							(flags[1] == ':') ?
199 							  optional_argument :
200 							  required_argument;
201 					else
202 						option[j].has_arg =
203 							no_argument;
204 					option[j].flag = NULL;
205 					option[j].val = val;
206 					j++;
207 				}
208 			}
209 			adjust_stack(env, __bi_argcnt + 1);
210 			optstr[size] = 0;
211 			loptstr[lsize] = 0;
212 			memset(&option[j], 0, sizeof option[0]);
213 		} else
214 			option = NULL;
215 	}
216 
217 	if (xargc == 0 || optstr == NULL || option == 0)
218 
219 #line 128
220 do {
221 #line 128
222   pushs(env, "");
223 #line 128
224   goto endlab;
225 #line 128
226 } while (0);
227 
228 	optind = mf_c_val(*env_data_ref(env, optind_loc),int) ;
229 	opterr = mf_c_val(*env_data_ref(env, opterr_loc),int) ;
230 
231 	rc = getopt_long(xargc, xargv, optstr, option, &long_idx);
232 	mf_c_val(*env_data_ref(env, optind_loc),int) = (optind);
233 	mf_c_val(*env_data_ref(env, opterr_loc),int) = (opterr);
234 	s[0] = optopt;
235 
236 #line 137
237 { size_t __off;
238 #line 137
239   const char *__s = s;
240 #line 137
241   if (__s)
242 #line 137
243      strcpy((char*)env_data_ref(env, __off = heap_reserve(env, strlen(__s) + 1)), __s);
244 #line 137
245   else
246 #line 137
247      __off = 0;
248 #line 137
249   mf_c_val(*env_data_ref(env, optopt_loc),size) = (__off); }
250 #line 137
251 ;
252 
253 #line 138
254 { size_t __off;
255 #line 138
256   const char *__s = optarg;
257 #line 138
258   if (__s)
259 #line 138
260      strcpy((char*)env_data_ref(env, __off = heap_reserve(env, strlen(__s) + 1)), __s);
261 #line 138
262   else
263 #line 138
264      __off = 0;
265 #line 138
266   mf_c_val(*env_data_ref(env, optarg_loc),size) = (__off); }
267 #line 138
268 ;
269 	if (rc == EOF)
270 
271 #line 140
272 do {
273 #line 140
274   pushs(env, "");
275 #line 140
276   goto endlab;
277 #line 140
278 } while (0);
279 	if (rc < 256) {
280 		s[0] = rc;
281 
282 #line 143
283 do {
284 #line 143
285   pushs(env, s);
286 #line 143
287   goto endlab;
288 #line 143
289 } while (0);
290 	}
291 
292 #line 145
293 do {
294 #line 145
295   pushs(env, option[long_idx].name);
296 #line 145
297   goto endlab;
298 #line 145
299 } while (0);
300 }
301 endlab:
302 #line 147
303         env_function_cleanup_flush(env, NULL);
304 #line 147
305 	return;
306 #line 147
307 }
308 
309 
310 #line 982 "../../src/builtin/snarf.m4"
311 
312 #line 982
313 
314 #line 982
315 
316 #line 982
317 void
318 #line 982
getopt_init_builtin(void)319 getopt_init_builtin(void)
320 #line 982
321 {
322 #line 982
323 
324 #line 982
325 	#line 23 "getopt.bi"
326 	builtin_variable_install("optarg", dtype_string, SYM_VOLATILE, &optarg_loc);
327 #line 24 "getopt.bi"
328 	builtin_variable_install("optind", dtype_number, SYM_VOLATILE, &optind_loc);
329 #line 25 "getopt.bi"
330 	builtin_variable_install("opterr", dtype_number, SYM_VOLATILE, &opterr_loc);
331 #line 26 "getopt.bi"
332 	builtin_variable_install("optopt", dtype_string, SYM_VOLATILE, &optopt_loc);
333 #line 28 "getopt.bi"
334 va_builtin_install_ex("getopt", bi_getopt, 0, dtype_string, 2, 0, 0|MFD_BUILTIN_VARIADIC, dtype_number, dtype_number);
335 
336 #line 982 "../../src/builtin/snarf.m4"
337 	struct value val;
338 #line 982
339 	val.type = dtype_number;
340 #line 982
341 	val.v.number = 1;
342 #line 982
343 	ensure_initialized_variable("opterr", &val);
344 #line 982
345 
346 #line 982
347 }
348 #line 982 "../../src/builtin/snarf.m4"
349 
350