Skip to content

Commit a09143f

Browse files
ALSchwalmandrewchambers
authored andcommitted
Support for not showing status code from shell commands
1 parent 2411c1a commit a09143f

File tree

1 file changed

+78
-14
lines changed

1 file changed

+78
-14
lines changed

src/janetsh

Lines changed: 78 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,18 @@
9393
(when (want-implicit-parens buf p)
9494
(let [line (string buf)]
9595
(buffer/clear buf)
96-
(buffer/format buf "(sh/$? %s)\n" line)))
97-
buf))
96+
(buffer/format buf "(sh/$? %s)\n" line)
97+
true))))
98+
99+
(var *show-exit-code* true)
100+
101+
(defn- show-status [f res added-parens]
102+
(case (fiber/status f)
103+
:dead (do
104+
(if (or *show-exit-code* (not added-parens))
105+
(pp res))
106+
(put user-env '_ @{:value res}))
107+
(debug/stacktrace f res)))
98108

99109
(setdyn :pretty-format "%.40p")
100110

@@ -109,18 +119,72 @@
109119
(def do-lines sh/do-lines)
110120
(def out-lines sh/out-lines)
111121

112-
(var *janetsh-repl*
113-
(fn *janetsh-repl*
114-
[]
115-
(when *hist-file*
116-
(try
117-
(shlib/input/history-load *hist-file*)
118-
([e] nil)))
119-
120-
(repl getchunk nil user-env)
121-
122-
(when *hist-file*
123-
(shlib/input/history-save *hist-file*))))
122+
(defn- janetsh-default-repl []
123+
(var show-status show-status)
124+
(var on-compile-error bad-compile)
125+
(var on-parse-error bad-parse)
126+
(var where "<janetsh>")
127+
128+
(when *hist-file*
129+
(try
130+
(shlib/input/history-load *hist-file*)
131+
([e] nil)))
132+
133+
# Are we done yet?
134+
(var going true)
135+
136+
# The parser object
137+
(def p (parser/new))
138+
139+
# Evaluate 1 source form in a protected manner
140+
(defn eval1 [source added-parens]
141+
(var good true)
142+
(def f
143+
(fiber/new
144+
(fn []
145+
(def res (compile source user-env ))
146+
(if (= (type res) :function)
147+
(res)
148+
(do
149+
(set good false)
150+
(def {:error err :start start :end end :fiber errf} res)
151+
(def msg
152+
(if (<= 0 start)
153+
(string err " at (" start ":" end ")")
154+
err))
155+
(on-compile-error msg errf where))))
156+
:a))
157+
(fiber/setenv f user-env)
158+
(def res (resume f nil))
159+
(when good (if going (show-status f res added-parens))))
160+
161+
# Loop
162+
(def buf @"")
163+
(while going
164+
(buffer/clear buf)
165+
(let [added-parens (getchunk buf p)]
166+
(var pindex 0)
167+
(var pstatus nil)
168+
(def len (length buf))
169+
(when (= len 0)
170+
(parser/eof p)
171+
(set going false))
172+
(while (> len pindex)
173+
(+= pindex (parser/consume p buf pindex))
174+
(while (parser/has-more p)
175+
(eval1 (parser/produce p) added-parens))
176+
(when (= (parser/status p) :error)
177+
(on-parse-error p where)))))
178+
# Check final parser state
179+
(while (parser/has-more p)
180+
(eval1 (parser/produce p)))
181+
(when (= (parser/status p) :error)
182+
(on-parse-error p where))
183+
184+
(when *hist-file*
185+
(shlib/input/history-save *hist-file*)))
186+
187+
(var *janetsh-repl* janetsh-default-repl)
124188

125189
(defn- run-interactive
126190
[]

0 commit comments

Comments
 (0)