factor/extra/shell/shell.factor

143 lines
3.3 KiB
Factor
Raw Normal View History

2008-04-17 17:05:49 -04:00
USING: kernel parser words continuations namespaces debugger
2008-04-20 18:17:48 -04:00
sequences combinators splitting prettyprint
2008-05-16 19:14:36 -04:00
system io io.files io.launcher io.encodings.utf8 io.pipes sequences.deep
accessors multi-methods newfx shell.parser
2008-10-18 23:50:10 -04:00
combinators.short-circuit eval environment ;
2008-04-17 17:05:49 -04:00
IN: shell
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: cd ( args -- )
dup empty?
[ drop home set-current-directory ]
[ first set-current-directory ]
if ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: pwd ( args -- )
drop
current-directory get
print ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: swords ( -- seq ) { "cd" "pwd" } ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
GENERIC: expand ( expr -- expr )
METHOD: expand { single-quoted-expr } expr>> ;
METHOD: expand { double-quoted-expr } expr>> ;
2008-04-17 20:46:28 -04:00
METHOD: expand { variable-expr } expr>> os-env ;
METHOD: expand { glob-expr }
expr>>
dup "*" =
2008-10-19 14:09:48 -04:00
[ drop current-directory get directory-files ]
2008-04-17 20:46:28 -04:00
[ ]
if ;
METHOD: expand { factor-expr } expr>> eval unparse ;
2008-04-20 18:17:48 -04:00
DEFER: expansion
METHOD: expand { back-quoted-expr }
expr>>
expr
command>>
expansion
utf8 <process-stream>
contents
" \n" split
"" remove ;
2008-04-17 17:05:49 -04:00
METHOD: expand { object } ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2008-04-17 20:46:28 -04:00
: expansion ( command -- command ) [ expand ] map flatten ;
2008-04-17 17:05:49 -04:00
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: run-sword ( basic-expr -- )
command>> expansion unclip "shell" lookup execute ;
2008-04-18 20:23:30 -04:00
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: run-foreground ( process -- )
[ try-process ] [ print-error drop ] recover ;
: run-background ( process -- ) run-detached drop ;
: run-basic-expr ( basic-expr -- )
2008-04-17 17:05:49 -04:00
<process>
over command>> expansion >>command
over stdin>> >>stdin
over stdout>> >>stdout
swap background>>
2008-04-18 20:23:30 -04:00
[ run-background ]
[ run-foreground ]
2008-04-17 17:05:49 -04:00
if ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2008-04-18 20:23:30 -04:00
: basic-chant ( basic-expr -- )
2008-04-17 20:46:28 -04:00
dup command>> first swords member-of?
2008-04-18 20:23:30 -04:00
[ run-sword ]
[ run-basic-expr ]
if ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2008-05-16 19:14:36 -04:00
: pipeline-chant ( pipeline-chant -- ) commands>> run-pipeline drop ;
2008-04-18 20:23:30 -04:00
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: chant ( obj -- )
dup basic-expr?
[ basic-chant ]
[ pipeline-chant ]
2008-04-17 20:46:28 -04:00
if ;
2008-04-17 17:05:49 -04:00
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: prompt ( -- )
current-directory get write
" $ " write
flush ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2008-04-17 20:46:28 -04:00
DEFER: shell
: handle ( input -- )
{
{ [ dup f = ] [ drop ] }
{ [ dup "exit" = ] [ drop ] }
{ [ dup "" = ] [ drop shell ] }
2008-07-24 19:13:27 -04:00
{ [ dup expr ] [ expr chant shell ] }
2008-04-17 20:46:28 -04:00
{ [ t ] [ drop "ix: ignoring input" print shell ] }
}
cond ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2008-04-17 17:05:49 -04:00
: shell ( -- )
prompt
readln
2008-04-17 20:46:28 -04:00
handle ;
2008-04-17 17:05:49 -04:00
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: ix ( -- ) shell ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2008-10-18 23:50:10 -04:00
MAIN: ix