<commit_before>! Copyright (C) 2003, 2009 <NAME>.
! See http://factorcode.org/license.txt for BSD license.
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 ;
IN: command-line

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+ ;

SYMBOL: script
SYMBOL: command-line

: (command-line) ( -- args )
    OBJ-ARGS special-object sift [ alien>native-string ] map ;

: 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
    ] when ;

: run-bootstrap-init ( -- )
    ".factor-boot-rc" rc-path try-user-init ;

: run-user-init ( -- )
    ".factor-rc" rc-path try-user-init ;

: load-vocab-roots ( -- )
    "user-init" get [
        ".factor-roots" rc-path dup exists? [
            utf8 file-lines harvest [ add-vocab-root ] each
        ] [ drop ] if
    ] when ;

: var-param ( name value -- ) swap set-global ;

: bool-param ( name -- ) "no-" ?head not var-param ;

: param ( param -- )
    "=" split1 [ var-param ] [ bool-param ] if* ;

: run-script ( file -- )
    t parser-quiet? [
        [ run-file ]
        [ source-file main>> [ execute( -- ) ] when* ] bi
    ] with-variable ;

: run-script? ( rest first -- rest first ? )
    over empty? not "run" get-global and ;

: 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
    ] if-empty ;

SYMBOL: main-vocab-hook

: main-vocab ( -- vocab )
    embedded? [
        "alien.remote-control"
    ] [
        main-vocab-hook get [ call( -- vocab ) ] [ "listener" ] if*
    ] if ;

: default-cli-args ( -- )
    [
        "e" off
        "user-init" on
        main-vocab "run" set
    ] with-global ;

[
    H{ } user-init-errors set-global
    default-cli-args
] "command-line" add-startup-hook

{ "debugger" "command-line" } "command-line.debugger" require-when
<commit_msg>command-line: need to check for run differently.<commit_after>! Copyright (C) 2003, 2009 <NAME>.
! See http://factorcode.org/license.txt for BSD license.
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 ;
IN: command-line

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+ ;

SYMBOL: script
SYMBOL: command-line

: (command-line) ( -- args )
    OBJ-ARGS special-object sift [ alien>native-string ] map ;

: 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
    ] when ;

: run-bootstrap-init ( -- )
    ".factor-boot-rc" rc-path try-user-init ;

: run-user-init ( -- )
    ".factor-rc" rc-path try-user-init ;

: load-vocab-roots ( -- )
    "user-init" get [
        ".factor-roots" rc-path dup exists? [
            utf8 file-lines harvest [ add-vocab-root ] each
        ] [ drop ] if
    ] when ;

: var-param ( name value -- ) swap set-global ;

: bool-param ( name -- ) "no-" ?head not var-param ;

: param ( param -- )
    "=" split1 [ var-param ] [ bool-param ] if* ;

: run-script ( file -- )
    t parser-quiet? [
        [ run-file ]
        [ source-file main>> [ execute( -- ) ] when* ] bi
    ] with-variable ;

: (parse-command-line) ( run? args -- )
    [ command-line off script off drop ] [
        unclip "-" ?head
        [ param (parse-command-line) ]
        [
            rot [ prefix f ] when
            script set command-line set
        ] if
    ] if-empty ;

: parse-command-line ( args -- )
    [ [ "-run=" head? ] any? ] keep (parse-command-line) ;

SYMBOL: main-vocab-hook

: main-vocab ( -- vocab )
    embedded? [
        "alien.remote-control"
    ] [
        main-vocab-hook get [ call( -- vocab ) ] [ "listener" ] if*
    ] if ;

: default-cli-args ( -- )
    [
        "e" off
        "user-init" on
        main-vocab "run" set
    ] with-global ;

[
    H{ } user-init-errors set-global
    default-cli-args
] "command-line" add-startup-hook

{ "debugger" "command-line" } "command-line.debugger" require-when

{'fraction_non_alphanumeric': 0.15077393464551883, 'fraction_numerical': 0.004204089432447927, 'mean_word_length': 2.134131736526946, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1067130', 'n_tokens_mistral': 1805, 'n_tokens_neox': 1776, 'n_words': 426}