2019-01-06 08:23:03 +00:00
|
|
|
# Copyright 2017-2019 (C) Calvin Rose
|
2018-08-04 01:51:35 +00:00
|
|
|
|
2018-03-28 17:50:06 +00:00
|
|
|
(do
|
|
|
|
|
2019-01-10 22:10:12 +00:00
|
|
|
(var *should-repl* false)
|
|
|
|
(var *no-file* true)
|
|
|
|
(var *quiet* false)
|
|
|
|
(var *raw-stdin* false)
|
|
|
|
(var *handleopts* true)
|
|
|
|
(var *exit-on-error* true)
|
2019-03-24 19:00:22 +00:00
|
|
|
(var *colorize* true)
|
2019-05-19 17:26:53 +00:00
|
|
|
(var *compile-only* false)
|
2018-02-07 18:19:34 +00:00
|
|
|
|
2019-02-18 20:31:15 +00:00
|
|
|
(if-let [jp (os/getenv "JANET_PATH")] (set module/*syspath* jp))
|
|
|
|
|
2018-07-02 01:12:46 +00:00
|
|
|
# Flag handlers
|
2018-08-16 02:48:35 +00:00
|
|
|
(def handlers :private
|
2018-11-25 19:03:00 +00:00
|
|
|
{"h" (fn [&]
|
2019-01-20 15:05:51 +00:00
|
|
|
(print "usage: " (get process/args 0) " [options] script args...")
|
2018-08-16 02:48:35 +00:00
|
|
|
(print
|
2018-07-02 01:12:46 +00:00
|
|
|
`Options are:
|
2019-02-17 02:55:53 +00:00
|
|
|
-h : Show this help
|
|
|
|
-v : Print the version string
|
|
|
|
-s : Use raw stdin instead of getline like functionality
|
|
|
|
-e code : Execute a string of janet
|
|
|
|
-r : Enter the repl after running all scripts
|
|
|
|
-p : Keep on executing if there is a top level error (persistent)
|
|
|
|
-q : Hide prompt, logo, and repl output (quiet)
|
2019-05-19 17:26:53 +00:00
|
|
|
-k : Compile scripts but do not execute
|
2019-02-18 20:06:17 +00:00
|
|
|
-m syspath : Set system path for loading global modules
|
2019-02-17 02:55:53 +00:00
|
|
|
-c source output : Compile janet source code into an image
|
2019-03-24 19:00:22 +00:00
|
|
|
-n : Disable ANSI color output in the repl
|
2019-02-17 02:55:53 +00:00
|
|
|
-l path : Execute code in a file before running the main script
|
|
|
|
-- : Stop handling options`)
|
2018-12-01 03:49:21 +00:00
|
|
|
(os/exit 0)
|
2018-07-02 01:12:46 +00:00
|
|
|
1)
|
2018-12-08 19:17:03 +00:00
|
|
|
"v" (fn [&] (print janet/version "-" janet/build) (os/exit 0) 1)
|
2018-12-17 02:57:32 +00:00
|
|
|
"s" (fn [&] (set *raw-stdin* true) (set *should-repl* true) 1)
|
|
|
|
"r" (fn [&] (set *should-repl* true) 1)
|
|
|
|
"p" (fn [&] (set *exit-on-error* false) 1)
|
2019-01-10 22:10:12 +00:00
|
|
|
"q" (fn [&] (set *quiet* true) 1)
|
2019-05-19 17:26:53 +00:00
|
|
|
"k" (fn [&] (set *compile-only* true) 1)
|
2019-03-24 19:00:22 +00:00
|
|
|
"n" (fn [&] (set *colorize* false) 1)
|
2019-02-18 20:06:17 +00:00
|
|
|
"m" (fn [i &] (set module/*syspath* (get process/args (+ i 1))) 2)
|
2019-02-17 02:55:53 +00:00
|
|
|
"c" (fn [i &]
|
|
|
|
(def e (require (get process/args (+ i 1))))
|
|
|
|
(spit (get process/args (+ i 2)) (make-image e))
|
|
|
|
(set *no-file* false)
|
|
|
|
3)
|
2018-12-17 02:57:32 +00:00
|
|
|
"-" (fn [&] (set *handleopts* false) 1)
|
2019-01-20 15:05:51 +00:00
|
|
|
"l" (fn [i &]
|
2019-04-16 19:41:45 +00:00
|
|
|
(import* (get process/args (+ i 1))
|
2019-01-20 15:05:51 +00:00
|
|
|
:prefix "" :exit *exit-on-error*)
|
|
|
|
2)
|
2018-11-25 19:03:00 +00:00
|
|
|
"e" (fn [i &]
|
2018-12-17 02:57:32 +00:00
|
|
|
(set *no-file* false)
|
2018-12-30 22:51:15 +00:00
|
|
|
(eval-string (get process/args (+ i 1)))
|
2018-07-02 01:12:46 +00:00
|
|
|
2)})
|
2018-02-07 18:19:34 +00:00
|
|
|
|
2018-11-25 19:03:00 +00:00
|
|
|
(defn- dohandler [n i &]
|
2018-07-02 01:12:46 +00:00
|
|
|
(def h (get handlers n))
|
2018-08-16 02:48:35 +00:00
|
|
|
(if h (h i) (do (print "unknown flag -" n) ((get handlers "h")))))
|
2018-02-07 05:44:51 +00:00
|
|
|
|
2018-07-02 01:12:46 +00:00
|
|
|
# Process arguments
|
|
|
|
(var i 1)
|
2018-12-01 03:49:21 +00:00
|
|
|
(def lenargs (length process/args))
|
2018-07-02 01:12:46 +00:00
|
|
|
(while (< i lenargs)
|
2018-12-01 03:49:21 +00:00
|
|
|
(def arg (get process/args i))
|
|
|
|
(if (and *handleopts* (= "-" (string/slice arg 0 1)))
|
|
|
|
(+= i (dohandler (string/slice arg 1 2) i))
|
2018-07-02 01:12:46 +00:00
|
|
|
(do
|
2018-12-17 02:57:32 +00:00
|
|
|
(set *no-file* false)
|
2019-05-19 17:26:53 +00:00
|
|
|
(import* arg :prefix "" :exit *exit-on-error* :compile-only *compile-only*)
|
2019-01-20 15:05:51 +00:00
|
|
|
(set i lenargs))))
|
2018-02-07 05:44:51 +00:00
|
|
|
|
2019-05-19 17:26:53 +00:00
|
|
|
(when (and (not *compile-only*) (or *should-repl* *no-file*))
|
2019-01-10 22:10:12 +00:00
|
|
|
(if-not *quiet*
|
2019-01-20 15:05:51 +00:00
|
|
|
(print "Janet " janet/version "-" janet/build " Copyright (C) 2017-2019 Calvin Rose"))
|
2019-01-10 22:10:12 +00:00
|
|
|
(defn noprompt [_] "")
|
|
|
|
(defn getprompt [p]
|
|
|
|
(def offset (parser/where p))
|
|
|
|
(string "janet:" offset ":" (parser/state p) "> "))
|
|
|
|
(def prompter (if *quiet* noprompt getprompt))
|
|
|
|
(defn getstdin [prompt buf]
|
|
|
|
(file/write stdout prompt)
|
|
|
|
(file/flush stdout)
|
|
|
|
(file/read stdin :line buf))
|
|
|
|
(def getter (if *raw-stdin* getstdin getline))
|
|
|
|
(defn getchunk [buf p]
|
|
|
|
(getter (prompter p) buf))
|
|
|
|
(def onsig (if *quiet* (fn [x &] x) nil))
|
2019-04-16 19:41:45 +00:00
|
|
|
(setdyn :pretty-format (if *colorize* "%.20P" "%.20p"))
|
|
|
|
(repl getchunk onsig)))
|