-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.fs
110 lines (107 loc) · 1.88 KB
/
run.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
require builtins.fs
require cstrarrays.fs
require exec-helpers.fs
require fd.fs
require lib.fs
\ This must not throw ever, catch all errors within.
: run-program ( c-addr1 u1 c-addr2 u2 ... u c-addrN uN list f -- n )
SIGCHLD block-signal
['] fork catch if
2drop
rot 1+ consume-argv
SIGCHLD unblock-signal
errno>
exit
endif
dup 0> if
\ parent
>r >r
drop
rot 1+ consume-argv
r> if
r> drop EXIT_SUCCESS
else
r> wait-for-child
endif
SIGCHLD unblock-signal
else
\ child
2drop
SIGINT default-signal if
errno> strerror type-err cr-err
EXIT_FAILURE terminate
endif
SIGCHLD unblock-signal
\ The stack is not cleaned up here since we terminate the process anyway.
['] stack-args-exec catch if
errno> strerror type-err cr-err
errno> ENOENT = if
EXIT_NOENT
else
EXIT_FAILURE
endif terminate
endif
terminate
endif ;
\ This must not throw ever, catch all errors within.
: run-builtin ( c-addr1 u1 c-addr2 u2 ... u c-addrN uN list f xt -- n )
swap if
\ run in bg
['] fork catch if
errno>
else
0= if
execute terminate
\ child
endif
EXIT_SUCCESS
endif
>r
2drop
rot 1+ consume-argv
r>
else
\ run in fg
execute
endif ;
\ This must not throw ever, catch all errors within and make sure that errno remains 0.
: run ( c-addr1 u1 c-addr2 u2 ... u c-addrN uN list a a a f -- n )
errno> >r 0 >errno
['] copy-std-fds catch if
drop
2drop drop
drop
rot 1+ consume-argv
errno>
r> >errno
exit
endif >r >r >r
>r
['] replace-std-fds catch if
2drop drop
drop
rot 1+ consume-argv
r> drop
r> r> r> ['] restore-std-fds catch if
2drop drop
endif
errno>
r> >errno
exit
endif
>r 2dup builtins search-wordlist if
\ builtin
r>
r> rot
run-builtin
else
\ no builtin
r>
r>
run-program
endif
r> r> r> ['] restore-std-fds catch if
2drop drop
drop errno>
endif
r> >errno ;