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

72 lines
1.8 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.
2008-12-08 15:58:00 -05:00
USING: init continuations hashtables io io.encodings.utf8
io.files io.pathnames kernel kernel.private namespaces parser
sequences source-files strings system splitting vocabs.loader
alien.strings accessors parser.notes ;
2008-04-26 12:28:08 -04:00
IN: command-line
2007-09-20 18:09:08 -04:00
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 ;
2007-09-20 18:09:08 -04:00
: run-bootstrap-init ( -- )
"user-init" get [
".factor-boot-rc" rc-path ?run-file
2007-09-20 18:09:08 -04:00
] when ;
: run-user-init ( -- )
"user-init" get [
".factor-rc" rc-path ?run-file
2007-09-20 18:09:08 -04:00
] when ;
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
2008-12-01 17:10:48 -05:00
: parse-command-line ( args -- )
[ command-line off script off ] [
unclip "-" ?head
[ param parse-command-line ]
[ script set command-line set ] if
] 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
[ default-cli-args ] "command-line" add-startup-hook