]> gitweb.factorcode.org Git - factor.git/blob - basis/command-line/command-line.factor
4bc899b2f16187ec9ee49a3d473ec394a0335110
[factor.git] / basis / command-line / command-line.factor
1 ! Copyright (C) 2003, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: init continuations hashtables io io.encodings.utf8
4 io.files io.pathnames kernel kernel.private namespaces parser
5 sequences source-files strings system splitting vocabs.loader
6 alien.strings accessors parser.notes ;
7 IN: command-line
8
9 SYMBOL: script
10 SYMBOL: command-line
11
12 : (command-line) ( -- args )
13     10 special-object sift [ alien>native-string ] map ;
14
15 : rc-path ( name -- path )
16     home prepend-path ;
17
18 : run-bootstrap-init ( -- )
19     "user-init" get [
20         ".factor-boot-rc" rc-path ?run-file
21     ] when ;
22
23 : run-user-init ( -- )
24     "user-init" get [
25         ".factor-rc" rc-path ?run-file
26     ] when ;
27
28 : load-vocab-roots ( -- )
29     "user-init" get [
30         ".factor-roots" rc-path dup exists? [
31             utf8 file-lines harvest [ add-vocab-root ] each
32         ] [ drop ] if
33     ] when ;
34
35 : var-param ( name value -- ) swap set-global ;
36
37 : bool-param ( name -- ) "no-" ?head not var-param ;
38
39 : param ( param -- )
40     "=" split1 [ var-param ] [ bool-param ] if* ;
41
42 : run-script ( file -- )
43     t parser-quiet? [
44         [ run-file ]
45         [ source-file main>> [ execute( -- ) ] when* ] bi
46     ] with-variable ;
47
48 : parse-command-line ( args -- )
49     [ command-line off script off ] [
50         unclip "-" ?head
51         [ param parse-command-line ]
52         [ script set command-line set ] if
53     ] if-empty ;
54
55 SYMBOL: main-vocab-hook
56
57 : main-vocab ( -- vocab )
58     embedded? [
59         "alien.remote-control"
60     ] [
61         main-vocab-hook get [ call( -- vocab ) ] [ "listener" ] if*
62     ] if ;
63
64 : default-cli-args ( -- )
65     global [
66         "e" off
67         "user-init" on
68         main-vocab "run" set
69     ] bind ;
70
71 [ default-cli-args ] "command-line" add-startup-hook