factor/basis/command-line/command-line.factor

100 lines
2.5 KiB
Factor
Raw Normal View History

! Copyright (C) 2003, 2009 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
2011-11-30 19:02:37 -05:00
USING: accessors alien.strings assocs continuations fry init
io.encodings.utf8 io.files io.pathnames kernel kernel.private
namespaces parser parser.notes sequences source-files
source-files.errors splitting system vocabs.loader ;
2008-04-26 12:28:08 -04:00
IN: command-line
2007-09-20 18:09:08 -04:00
SYMBOL: user-init-errors
SYMBOL: +user-init-error+
TUPLE: user-init-error error file line# asset ;
: <user-init-error> ( error -- error' )
[ ] [ error-file ] [ error-line ] tri
f user-init-error boa ; inline
M: user-init-error error-file file>> ;
M: user-init-error error-line line#>> ;
M: user-init-error error-type drop +user-init-error+ ;
2008-12-01 17:10:48 -05:00
SYMBOL: script
SYMBOL: command-line
: (command-line) ( -- args )
OBJ-ARGS special-object sift [ alien>native-string ] map ;
2008-12-01 17:10:48 -05:00
: rc-path ( name -- path )
home prepend-path ;
: try-user-init ( file -- )
"user-init" get swap '[
_ [ ?run-file ] [
<user-init-error>
swap user-init-errors get set-at
notify-error-observers
] recover
2007-09-20 18:09:08 -04:00
] when ;
: run-bootstrap-init ( -- )
".factor-boot-rc" rc-path try-user-init ;
2007-09-20 18:09:08 -04:00
: run-user-init ( -- )
".factor-rc" rc-path try-user-init ;
2007-09-20 18:09:08 -04:00
2008-12-01 17:10:48 -05:00
: load-vocab-roots ( -- )
"user-init" get [
".factor-roots" rc-path dup exists? [
2010-06-04 15:17:07 -04:00
utf8 file-lines harvest [ add-vocab-root ] each
2008-12-01 17:10:48 -05:00
] [ drop ] if
] when ;
: var-param ( name value -- ) swap set-global ;
: bool-param ( name -- ) "no-" ?head not var-param ;
2007-09-20 18:09:08 -04:00
2008-12-01 17:10:48 -05:00
: param ( param -- )
"=" split1 [ var-param ] [ bool-param ] if* ;
2007-09-20 18:09:08 -04:00
2008-12-01 17:10:48 -05:00
: run-script ( file -- )
t parser-quiet? [
[ run-file ]
[ source-file main>> [ execute( -- ) ] when* ] bi
] with-variable ;
2007-09-20 18:09:08 -04:00
: run-script? ( rest first -- rest first ? )
over empty? not "run" get-global and ;
2008-12-01 17:10:48 -05:00
: parse-command-line ( args -- )
[ command-line off script off ] [
unclip "-" ?head
[ param parse-command-line ]
[
run-script? [ prefix f ] when
script set command-line set
] if
2008-12-01 17:10:48 -05:00
] if-empty ;
2007-09-20 18:09:08 -04:00
SYMBOL: main-vocab-hook
: main-vocab ( -- vocab )
embedded? [
"alien.remote-control"
] [
2009-03-17 03:19:50 -04:00
main-vocab-hook get [ call( -- vocab ) ] [ "listener" ] if*
2007-09-20 18:09:08 -04:00
] if ;
2008-06-08 16:32:55 -04:00
: default-cli-args ( -- )
[
2007-09-20 18:09:08 -04:00
"e" off
"user-init" on
main-vocab "run" set
] with-global ;
2007-09-20 18:09:08 -04:00
[
H{ } user-init-errors set-global
default-cli-args
] "command-line" add-startup-hook
2011-11-27 14:45:28 -05:00
{ "debugger" "command-line" } "command-line.debugger" require-when