-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactor-shell.factor
36 lines (27 loc) · 1.26 KB
/
factor-shell.factor
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
USING: fry io kernel sequences sets splitting unix.process system io.launcher formatting io.pathnames io.files.private combinators math.parser continuations sequences.extras accessors peg.ebnf ;
QUALIFIED: unix.ffi
IN: factor-shell
: make-prompt ( -- str )
{ } cwd suffix " $" suffix "" join ;
: print-prompt ( -- )
make-prompt write flush ;
: tokenize-line ( str -- seq )
" " split ;
: exit-maybe ( x -- x )
dup "exit" = [ 0 exit ] [ ] if ;
: segment-seq-for-piping ( x -- x )
{ { } } [ dup "|" =
[ drop { } suffix ]
! hide the string, dup the outer sequence, get the sequence we are appending to
[ [ dup last ] dip suffix [ 1 head* ] dip suffix ]
if ] reduce ;
: cd ( path -- ) dup unix.ffi:chdir 0 = not [ "cd: no such file or directory: " swap "\n" 3append ] when write flush ;
: factor-shell ( -- )
[ t ] [ print-prompt readln exit-maybe tokenize-line
{
{ [ dup { "|" } contains? ] [ segment-seq-for-piping input-stream [ <process> swap " " join >>command swap >>stdin run-process stdout>> ] reduce drop ] }
{ [ dup first "cd" = ] [ 1 tail " " join cd ] }
{ [ dup last "&" = ] [ 1 head* run-detached drop ] }
[ run-process wait-for-process drop ]
} cond ] while ;
MAIN: factor-shell