]> gitweb.factorcode.org Git - factor.git/blob - basis/command-line/command-line.factor
44ff0c0c1206fe7417858b2095c5c03ad6cdf33a
[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: accessors alien.strings assocs continuations fry init
4 io.encodings.utf8 io.files io.pathnames kernel kernel.private
5 namespaces parser parser.notes sequences source-files
6 source-files.errors splitting system vocabs.loader ;
7 IN: command-line
8
9 SYMBOL: user-init-errors
10 SYMBOL: +user-init-error+
11
12 TUPLE: user-init-error error path line# asset ;
13
14 : <user-init-error> ( error -- error' )
15     [ ] [ error-file ] [ error-line ] tri
16     f user-init-error boa ; inline
17 M: user-init-error error-file path>> ;
18 M: user-init-error error-line line#>> ;
19 M: user-init-error error-type drop +user-init-error+ ;
20
21 SYMBOL: executable
22 SYMBOL: script
23 SYMBOL: command-line
24
25 : (command-line) ( -- args )
26     OBJ-ARGS special-object sift [ alien>native-string ] map ;
27
28 : rc-path ( name -- path )
29     home prepend-path ;
30
31 : try-user-init ( file -- )
32     "user-init" get swap '[
33         _ [ ?run-file ] [
34             <user-init-error>
35             swap user-init-errors get set-at
36             notify-error-observers
37         ] recover
38     ] when ;
39
40 : run-bootstrap-init ( -- )
41     ".factor-boot-rc" rc-path try-user-init ;
42
43 : run-user-init ( -- )
44     ".factor-rc" rc-path try-user-init ;
45
46 : load-vocab-roots ( -- )
47     "user-init" get [
48         ".factor-roots" rc-path dup exists? [
49             utf8 file-lines harvest [ add-vocab-root ] each
50         ] [ drop ] if
51     ] when ;
52
53 : var-param ( name value -- ) swap set-global ;
54
55 : bool-param ( name -- ) "no-" ?head not var-param ;
56
57 : param ( param -- )
58     "=" split1 [ var-param ] [ bool-param ] if* ;
59
60 : run-script ( file -- )
61     t parser-quiet? [
62         [ run-file ]
63         [ path>source-file main>> [ execute( -- ) ] when* ] bi
64     ] with-variable ;
65
66 : (parse-command-line) ( args -- )
67     [
68         unclip "-" ?head [
69             [ param ] [ "run=" head? ] bi
70             [ command-line set ]
71             [ (parse-command-line) ] if
72         ] [
73             script set command-line set
74         ] if
75     ] unless-empty ;
76
77 : parse-command-line ( args -- )
78     command-line off
79     script off
80     unclip executable set
81     (parse-command-line) ;
82
83 SYMBOL: main-vocab-hook
84
85 : main-vocab ( -- vocab )
86     embedded? [
87         "alien.remote-control"
88     ] [
89         main-vocab-hook get [ call( -- vocab ) ] [ "listener" ] if*
90     ] if ;
91
92 : default-cli-args ( -- )
93     [
94         "e" off
95         "user-init" on
96         main-vocab "run" set
97     ] with-global ;
98
99 [
100     H{ } user-init-errors set-global
101     default-cli-args
102 ] "command-line" add-startup-hook
103
104 { "debugger" "command-line" } "command-line.debugger" require-when