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 "system.bi"
29 /* This file is part of Mailfromd.             -*- c -*-
30    Copyright (C) 2006-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 <sys/utsname.h>
48 #include <mflib/status.h>
49 
50 void
51 #line 22
bi_system(eval_environ_t env)52 bi_system(eval_environ_t env)
53 #line 22
54 
55 #line 22
56 
57 #line 22 "system.bi"
58 {
59 #line 22
60 
61 #line 22
62 
63 #line 22
64 
65 #line 22
66 char *  str;
67 #line 22
68 
69 #line 22
70 get_string_arg(env, 0, &str);
71 #line 22
72 
73 #line 22
74 
75 #line 22
76         adjust_stack(env, 1);
77 #line 22
78 
79 #line 22
80 
81 #line 22
82 	if (builtin_module_trace(BUILTIN_IDX_system))
83 #line 22
84 		prog_trace(env, "system %s",str);;
85 #line 22
86 
87 {
88 
89 #line 24
90 do {
91 #line 24
92   push(env, (STKVAL)(mft_number)(system(str)));
93 #line 24
94   goto endlab;
95 #line 24
96 } while (0);
97 }
98 endlab:
99 #line 26
100         env_function_cleanup_flush(env, NULL);
101 #line 26
102 	return;
103 #line 26
104 }
105 
106 void
107 #line 28
bi_time(eval_environ_t env)108 bi_time(eval_environ_t env)
109 #line 28
110 
111 #line 28
112 
113 #line 28 "system.bi"
114 {
115 #line 28
116 
117 #line 28
118 
119 #line 28
120 
121 #line 28
122 
123 #line 28
124 
125 #line 28
126 
127 #line 28
128         adjust_stack(env, 0);
129 #line 28
130 
131 #line 28
132 
133 #line 28
134 	if (builtin_module_trace(BUILTIN_IDX_system))
135 #line 28
136 		prog_trace(env, "time");;
137 #line 28
138 
139 {
140 	time_t t = time(NULL);
141 
142 #line 31
143 do {
144 #line 31
145   push(env, (STKVAL)(mft_number)(t));
146 #line 31
147   goto endlab;
148 #line 31
149 } while (0);
150 }
151 endlab:
152 #line 33
153         env_function_cleanup_flush(env, NULL);
154 #line 33
155 	return;
156 #line 33
157 }
158 
159 void
160 #line 35
bi_sleep(eval_environ_t env)161 bi_sleep(eval_environ_t env)
162 #line 35
163 
164 #line 35
165 
166 #line 35 "system.bi"
167 {
168 #line 35
169 
170 #line 35
171 
172 #line 35
173 long __bi_argcnt;
174 #line 35
175 long  seconds;
176 #line 35
177         long  useconds;
178 #line 35
179 
180 #line 35
181 get_numeric_arg(env, 1, &seconds);
182 #line 35
183         get_numeric_arg(env, 2, &useconds);
184 #line 35
185 
186 #line 35
187 get_numeric_arg(env, 0, &__bi_argcnt);
188 #line 35
189         adjust_stack(env, __bi_argcnt + 1);
190 #line 35
191 
192 #line 35
193 
194 #line 35
195 	if (builtin_module_trace(BUILTIN_IDX_system))
196 #line 35
197 		prog_trace(env, "sleep %lu %lu",seconds, ((__bi_argcnt > 1) ? useconds : 0));;
198 #line 35
199 
200 {
201 	struct timeval tv;
202 
203 	tv.tv_sec = seconds;
204 	tv.tv_usec = ((__bi_argcnt > 1) ? useconds : 0);
205 	/* FIXME: signals? */
206 	select(0, NULL, NULL, NULL, &tv);
207 }
208 
209 #line 44
210         env_function_cleanup_flush(env, NULL);
211 #line 44
212 	return;
213 #line 44
214 }
215 
216 void
217 #line 46
bi_strftime(eval_environ_t env)218 bi_strftime(eval_environ_t env)
219 #line 46
220 
221 #line 46
222 
223 #line 46 "system.bi"
224 {
225 #line 46
226 
227 #line 46
228 
229 #line 46
230 long __bi_argcnt;
231 #line 46
232 char * MFL_DATASEG fmt;
233 #line 46
234         long  timestamp;
235 #line 46
236         long  gmt;
237 #line 46
238 
239 #line 46
240 get_string_arg(env, 1, &fmt);
241 #line 46
242         get_numeric_arg(env, 2, &timestamp);
243 #line 46
244         get_numeric_arg(env, 3, &gmt);
245 #line 46
246 
247 #line 46
248 get_numeric_arg(env, 0, &__bi_argcnt);
249 #line 46
250         adjust_stack(env, __bi_argcnt + 1);
251 #line 46
252 
253 #line 46
254 
255 #line 46
256 	if (builtin_module_trace(BUILTIN_IDX_system))
257 #line 46
258 		prog_trace(env, "strftime %s %lu %lu",fmt, timestamp, ((__bi_argcnt > 2) ? gmt : 0));;
259 #line 46
260 
261 {
262 	char buf[1024];
263 	time_t t = timestamp;
264 	struct tm *tm = ((__bi_argcnt > 2) ? gmt : 0) ? gmtime(&t) : localtime(&t);
265 	size_t size = strftime(buf, sizeof(buf), fmt, tm);
266 	size_t off;
267 	char *s = (char*) env_data_ref(env, (off = heap_reserve(env, size + 1)));
268 	strcpy(s, buf);
269 
270 #line 55
271 do {
272 #line 55
273   push(env, (STKVAL) (mft_size) (off));
274 #line 55
275   goto endlab;
276 #line 55
277 } while (0);
278 }
279 endlab:
280 #line 57
281         env_function_cleanup_flush(env, NULL);
282 #line 57
283 	return;
284 #line 57
285 }
286 
287 void
288 #line 59
bi_umask(eval_environ_t env)289 bi_umask(eval_environ_t env)
290 #line 59
291 
292 #line 59
293 
294 #line 59 "system.bi"
295 {
296 #line 59
297 
298 #line 59
299 
300 #line 59
301 
302 #line 59
303 long  mask;
304 #line 59
305 
306 #line 59
307 get_numeric_arg(env, 0, &mask);
308 #line 59
309 
310 #line 59
311 
312 #line 59
313         adjust_stack(env, 1);
314 #line 59
315 
316 #line 59
317 
318 #line 59
319 	if (builtin_module_trace(BUILTIN_IDX_system))
320 #line 59
321 		prog_trace(env, "umask %lu",mask);;
322 #line 59
323 
324 {
325 	mode_t mode = umask(mask);
326 
327 #line 62
328 do {
329 #line 62
330   push(env, (STKVAL)(mft_number)(mode));
331 #line 62
332   goto endlab;
333 #line 62
334 } while (0);
335 }
336 endlab:
337 #line 64
338         env_function_cleanup_flush(env, NULL);
339 #line 64
340 	return;
341 #line 64
342 }
343 
344 /* Interface to the system uname call.
345 
346    Format sequences are:
347    %s   sysname
348    %n   nodename
349    %r   release
350    %v   version
351    %m   machine
352 */
353 void
354 #line 75
bi_uname(eval_environ_t env)355 bi_uname(eval_environ_t env)
356 #line 75
357 
358 #line 75
359 
360 #line 75 "system.bi"
361 {
362 #line 75
363 
364 #line 75
365 
366 #line 75
367 
368 #line 75
369 char * MFL_DATASEG fmt;
370 #line 75
371 
372 #line 75
373 get_string_arg(env, 0, &fmt);
374 #line 75
375 
376 #line 75
377 
378 #line 75
379         adjust_stack(env, 1);
380 #line 75
381 
382 #line 75
383 
384 #line 75
385 	if (builtin_module_trace(BUILTIN_IDX_system))
386 #line 75
387 		prog_trace(env, "uname %s",fmt);;
388 #line 75
389 
390 {
391 	struct utsname ubuf;
392 
393 	uname(&ubuf);
394 	heap_obstack_begin(env);
395 	while (*fmt) {
396 		if (*fmt == '%') {
397 			switch (*++fmt) {
398 			case 's':
399 				heap_obstack_grow(env, ubuf.sysname, strlen(ubuf.sysname));
400 #line 87
401 				break;
402 			case 'n':
403 				heap_obstack_grow(env, ubuf.nodename, strlen(ubuf.nodename));
404 #line 91
405 				break;
406 			case 'r':
407 				heap_obstack_grow(env, ubuf.release, strlen(ubuf.release));
408 #line 95
409 				break;
410 			case 'v':
411 				heap_obstack_grow(env, ubuf.version, strlen(ubuf.version));
412 #line 99
413 				break;
414 			case 'm':
415 				heap_obstack_grow(env, ubuf.machine, strlen(ubuf.machine));
416 #line 103
417 				break;
418 			case '%':
419 				do { char __c = '%'; heap_obstack_grow(env, &__c, 1); } while(0);
420 				break;
421 			default:
422 				do { char __c = '%'; heap_obstack_grow(env, &__c, 1); } while(0);
423 				do { char __c = *fmt; heap_obstack_grow(env, &__c, 1); } while(0);
424 			}
425 			fmt++;
426 		} else {
427 			do { char __c = *fmt; heap_obstack_grow(env, &__c, 1); } while(0);
428 			fmt++;
429 		}
430 	}
431 	do { char __c = 0; heap_obstack_grow(env, &__c, 1); } while(0);
432 
433 #line 118
434 do {
435 #line 118
436   push(env, (STKVAL) (heap_obstack_finish(env)));
437 #line 118
438   goto endlab;
439 #line 118
440 } while (0);
441 }
442 endlab:
443 #line 120
444         env_function_cleanup_flush(env, NULL);
445 #line 120
446 	return;
447 #line 120
448 }
449 
450 static struct builtin_const_trans access_modes[] = {
451 	{ _MFL_R_OK, R_OK },
452 	{ _MFL_W_OK, W_OK },
453 	{ _MFL_X_OK, X_OK },
454 	{ _MFL_F_OK, F_OK }
455 };
456 
457 void
458 #line 129
bi_access(eval_environ_t env)459 bi_access(eval_environ_t env)
460 #line 129
461 
462 #line 129
463 
464 #line 129 "system.bi"
465 {
466 #line 129
467 
468 #line 129
469 
470 #line 129
471 
472 #line 129
473 char *  pathname;
474 #line 129
475         long  mode;
476 #line 129
477 
478 #line 129
479 get_string_arg(env, 0, &pathname);
480 #line 129
481         get_numeric_arg(env, 1, &mode);
482 #line 129
483 
484 #line 129
485 
486 #line 129
487         adjust_stack(env, 2);
488 #line 129
489 
490 #line 129
491 
492 #line 129
493 	if (builtin_module_trace(BUILTIN_IDX_system))
494 #line 129
495 		prog_trace(env, "access %s %lu",pathname, mode);;
496 #line 129
497 
498 {
499 	int c_mode;
500 
501 		if (!(_builtin_const_to_c(access_modes,
502 #line 133
503 				      MU_ARRAY_SIZE(access_modes),
504 #line 133
505 				      mode,
506 #line 133
507 				      &c_mode) == 0))
508 #line 133
509 		(
510 #line 133
511 	env_throw_bi(env, mfe_failure, "access", "bad access mode")
512 #line 133
513 )
514 #line 138
515 ;
516 
517 #line 139
518 do {
519 #line 139
520   push(env, (STKVAL)(mft_number)(access(pathname, c_mode) == 0));
521 #line 139
522   goto endlab;
523 #line 139
524 } while (0);
525 }
526 endlab:
527 #line 141
528         env_function_cleanup_flush(env, NULL);
529 #line 141
530 	return;
531 #line 141
532 }
533 
534 void
535 #line 143
bi_getenv(eval_environ_t env)536 bi_getenv(eval_environ_t env)
537 #line 143
538 
539 #line 143
540 
541 #line 143 "system.bi"
542 {
543 #line 143
544 
545 #line 143
546 
547 #line 143
548 
549 #line 143
550 char * MFL_DATASEG name;
551 #line 143
552 
553 #line 143
554 get_string_arg(env, 0, &name);
555 #line 143
556 
557 #line 143
558 
559 #line 143
560         adjust_stack(env, 1);
561 #line 143
562 
563 #line 143
564 
565 #line 143
566 	if (builtin_module_trace(BUILTIN_IDX_system))
567 #line 143
568 		prog_trace(env, "getenv %s",name);;
569 #line 143
570 
571 {
572 	char *p = getenv(name);
573 		if (!(p != NULL))
574 #line 146
575 		(
576 #line 146
577 	env_throw_bi(env, mfe_not_found, "getenv", "%s: environment variable not defined",name)
578 #line 146
579 )
580 #line 149
581 ;
582 
583 #line 150
584 do {
585 #line 150
586   pushs(env, p);
587 #line 150
588   goto endlab;
589 #line 150
590 } while (0);
591 }
592 endlab:
593 #line 152
594         env_function_cleanup_flush(env, NULL);
595 #line 152
596 	return;
597 #line 152
598 }
599 
600 void
601 #line 154
bi_unlink(eval_environ_t env)602 bi_unlink(eval_environ_t env)
603 #line 154
604 
605 #line 154
606 
607 #line 154 "system.bi"
608 {
609 #line 154
610 
611 #line 154
612 
613 #line 154
614 
615 #line 154
616 char *  name;
617 #line 154
618 
619 #line 154
620 get_string_arg(env, 0, &name);
621 #line 154
622 
623 #line 154
624 
625 #line 154
626         adjust_stack(env, 1);
627 #line 154
628 
629 #line 154
630 
631 #line 154
632 	if (builtin_module_trace(BUILTIN_IDX_system))
633 #line 154
634 		prog_trace(env, "unlink %s",name);;
635 #line 154
636 
637 {
638 	int rc = unlink(name);
639 		if (!(rc == 0))
640 #line 157
641 		(
642 #line 157
643 	env_throw_bi(env, mfe_failure, "unlink", "cannot unlink %s: %s",name,mu_strerror(errno))
644 #line 157
645 )
646 #line 159
647 ;
648 }
649 
650 #line 161
651         env_function_cleanup_flush(env, NULL);
652 #line 161
653 	return;
654 #line 161
655 }
656 
657 #line 982 "../../src/builtin/snarf.m4"
658 
659 #line 982
660 
661 #line 982
662 
663 #line 982
664 void
665 #line 982
system_init_builtin(void)666 system_init_builtin(void)
667 #line 982
668 {
669 #line 982
670 
671 #line 982
672 	#line 22 "system.bi"
673 va_builtin_install_ex("system", bi_system, 0, dtype_number, 1, 0, 0|0, dtype_string);
674 #line 28 "system.bi"
675 va_builtin_install_ex("time", bi_time, 0, dtype_number, 0, 0, 0|0, dtype_unspecified);
676 #line 35 "system.bi"
677 va_builtin_install_ex("sleep", bi_sleep, 0, dtype_unspecified, 2, 1, 0|0, dtype_number, dtype_number);
678 #line 46 "system.bi"
679 va_builtin_install_ex("strftime", bi_strftime, 0, dtype_string, 3, 1, 0|0, dtype_string, dtype_number, dtype_number);
680 #line 59 "system.bi"
681 va_builtin_install_ex("umask", bi_umask, 0, dtype_number, 1, 0, 0|0, dtype_number);
682 #line 75 "system.bi"
683 va_builtin_install_ex("uname", bi_uname, 0, dtype_string, 1, 0, 0|0, dtype_string);
684 #line 129 "system.bi"
685 va_builtin_install_ex("access", bi_access, 0, dtype_number, 2, 0, 0|0, dtype_string, dtype_number);
686 #line 143 "system.bi"
687 va_builtin_install_ex("getenv", bi_getenv, 0, dtype_string, 1, 0, 0|0, dtype_string);
688 #line 154 "system.bi"
689 va_builtin_install_ex("unlink", bi_unlink, 0, dtype_unspecified, 1, 0, 0|0, dtype_string);
690 
691 #line 982 "../../src/builtin/snarf.m4"
692 
693 #line 982
694 }
695 #line 982 "../../src/builtin/snarf.m4"
696 
697