factor/extra/shell/shell.factor

126 lines
3.0 KiB
Factor
Raw Normal View History

2008-04-17 17:05:49 -04:00
USING: kernel words continuations namespaces debugger sequences combinators
2008-04-17 20:46:28 -04:00
system io io.files io.launcher sequences.deep
2008-04-17 17:05:49 -04:00
accessors multi-methods newfx shell.parser ;
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 "*" =
[ drop current-directory get directory [ first ] map ]
[ ]
if ;
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
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2008-04-18 20:23:30 -04:00
: run-sword ( basic-expr -- ) command>> unclip "shell" lookup execute ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: 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 ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: pipeline-chant ( pipeline-chant -- )
drop "ix: pipelines not supported" print ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: 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 ] }
{ [ dup expr ] [ expr ast>> chant shell ] }
{ [ 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 ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
MAIN: ix