echo as a value command

- echo gives its words as one string, and shadows the program.
- With -n, -e or -E it runs the program echo.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Linus Björnstam 2026-12-01 07:29:10 +01:00
parent acd84a2a4c
commit 8ded734477
5 changed files with 36 additions and 10 deletions

View file

@ -288,9 +288,10 @@ the text above:*
state, command and id; and of number and line. Alone they write text. state, command and id; and of number and line. Alone they write text.
- ~2>&1~ is done (phase 3): ~>FILE 2>&1~ works, ~2>&1 >FILE~ is an error. - ~2>&1~ is done (phase 3): ~>FILE 2>&1~ works, ~2>&1 >FILE~ is an error.
~>&2~ has no form in ~(std run)~ yet. ~>&2~ has no form in ~(std run)~ yet.
- Not done: ~echo~. A value ~echo~ would take the name from ~/bin/echo~, - ~echo~ is a value command, and shadows the program: the one exception to
which decision 4 does not allow; to decide. decision 4. With ~-n~, ~-e~ or ~-E~ the program runs, since a value has
- Tests: ~tests/values.bjo~ (51); ~tests/run.bjo~ (34); no way to leave out the newline.
- Tests: ~tests/values.bjo~ (56); ~tests/run.bjo~ (34);
~tests/interactive.bjo~ (12). ~tests/interactive.bjo~ (12).
** Phase 3: more POSIX, and the rest of the edges ** Phase 3: more POSIX, and the rest of the edges

View file

@ -5,7 +5,8 @@
;; (bjosh commands): the value commands. See decision 4 of plan.org. ;; (bjosh commands): the value commands. See decision 4 of plan.org.
;; ;;
;; - Each command has a long name and a short one. The short name always ;; - Each command has a long name and a short one. The short name always
;; runs the command. The long name runs it only when no program of that ;; runs the command. `echo` is both, so it shadows the program, except
;; with the program's flags (`echo-flags?`). The long name runs it only when no program of that
;; name is on PATH: `uniq` is the program, `u` the command. ;; name is on PATH: `uniq` is the program, `u` the command.
;; - A command reads bytes (`OnBytes`), values (`OnValues`), or nothing ;; - A command reads bytes (`OnBytes`), values (`OnValues`), or nothing
;; (`OnNothing`), and writes values. The caller connects them: see ;; (`OnNothing`), and writes values. The caller connects them: see
@ -19,7 +20,7 @@
(import (text csv)) (import (text csv))
(import (bjosh values)) (import (bjosh values))
(export ValueCommand Body value-command value-commands value-names lines-body) (export ValueCommand Body value-command value-commands value-names lines-body echo-flags?)
(import/extern (import/extern
(regex-match? (: System.Text.RegularExpressions.Regex.IsMatch (-> string string bool) (regex-match? (: System.Text.RegularExpressions.Regex.IsMatch (-> string string bool)
@ -172,6 +173,16 @@
unit) unit)
;; A file by its extension: .json as from-json, .csv as from-csv, else lines. ;; A file by its extension: .json as from-json, .csv as from-csv, else lines.
(: (-> (Vec string) Values Unit))
(defbjo (echo-body args out)
(emit! out (JsonStr (string-join args " "))))
;; The flags of the program echo. With one, the program runs.
(: (-> (Vec string) bool))
(defun (echo-flags? argv)
(and (> (vec-length argv) 1)
(vec-contains? ["-n" "-e" "-E" "-ne" "-en" "-nE" "-En"] (vec-ref argv 1))))
(: (-> (Vec string) Values Unit)) (: (-> (Vec string) Values Unit))
(defbjo (open-body args out) (defbjo (open-body args out)
(unless (= (vec-length args) 1) (unless (= (vec-length args) 1)
@ -441,7 +452,8 @@
(: (-> (Vec ValueCommand))) (: (-> (Vec ValueCommand)))
(defun (value-commands) (defun (value-commands)
[(vc "files" "l" "files [DIR...]" "the files of directories, as records: name, type, size, modified" (OnNothing files-body)) [(vc "echo" "echo" "echo WORD..." "its words as one string; with -n, -e or -E, the program echo" (OnNothing echo-body))
(vc "files" "l" "files [DIR...]" "the files of directories, as records: name, type, size, modified" (OnNothing files-body))
(vc "procs" "p" "procs" "the processes, from /proc: pid, ppid, name, state, mem, command" (OnNothing procs-body)) (vc "procs" "p" "procs" "the processes, from /proc: pid, ppid, name, state, mem, command" (OnNothing procs-body))
(vc "open" "o" "open FILE" "a file as values: .json as from-json, .csv as from-csv, else lines" (OnNothing open-body)) (vc "open" "o" "open FILE" "a file as values: .json as from-json, .csv as from-csv, else lines" (OnNothing open-body))
(vc "where" "wh" "where FIELD OP VALUE" "the values that match: OP is == != < <= > >= (or eq ne lt le gt ge), =~ or contains; FIELD it is the value itself" (OnValues where-body)) (vc "where" "wh" "where FIELD OP VALUE" "the values that match: OP is == != < <= > >= (or eq ne lt le gt ge), =~ or contains; FIELD it is the value itself" (OnValues where-body))
@ -462,7 +474,9 @@
;; The short and long names, for completion. ;; The short and long names, for completion.
(: (-> (Vec string))) (: (-> (Vec string)))
(defun (value-names) (defun (value-names)
(vec-fold (fun (c acc) (vec-add (vec-add acc (record-ref c short)) (record-ref c long))) (vec-fold (fun (c acc)
(def with-short (vec-add acc (record-ref c short)))
(if (= (record-ref c short) (record-ref c long)) with-short (vec-add with-short (record-ref c long))))
[] []
(value-commands))) (value-commands)))

View file

@ -170,7 +170,7 @@
(match builtin-in-pipe (match builtin-in-pipe
((Some e) (shell-error (str (vec-ref (record-ref e argv) 0) ": a shell command cannot be in a pipe")) 2) ((Some e) (shell-error (str (vec-ref (record-ref e argv) 0) ": a shell command cannot be in a pipe")) 2)
(None (None
(if (any? (fun (e) (some? (stage-command sh (vec-ref (record-ref e argv) 0)))) expanded) (if (any? (fun (e) (some? (stage-command-of sh (record-ref e argv)))) expanded)
(run-mixed sh expanded text background?) (run-mixed sh expanded text background?)
(run-form sh expanded text background?))))))) (run-form sh expanded text background?)))))))
@ -187,6 +187,10 @@
;; Value commands next to each other, with the file the first reads (<) ;; Value commands next to each other, with the file the first reads (<)
;; and the file the last writes (> or >>, with append?). ;; and the file the last writes (> or >>, with append?).
(: (-> Shell (Vec string) (Option ValueCommand)))
(defun (stage-command-of sh argv)
(if (echo-flags? argv) None (stage-command sh (vec-ref argv 0))))
(type (: Segment (Record (: stages (Vec ValueStage)) (type (: Segment (Record (: stages (Vec ValueStage))
(: from (Option string)) (: from (Option string))
(: into (Option (Tuple string bool)))))) (: into (Option (Tuple string bool))))))
@ -238,7 +242,7 @@
(let* ((e (vec-ref expanded i)) (let* ((e (vec-ref expanded i))
(argv (record-ref e argv)) (argv (record-ref e argv))
(name (vec-ref argv 0))) (name (vec-ref argv 0)))
(match (stage-command sh name) (match (stage-command-of sh argv)
((Some c) ((Some c)
(def stage (ValueStage (command c) (args (vec-drop argv 1)))) (def stage (ValueStage (command c) (args (vec-drop argv 1))))
(def seg (match current (def seg (match current

View file

@ -70,7 +70,7 @@
(expect ">FILE 2>&1" (c (str "ls /no/such/dir >" file " 2>&1; grep -c such " file)) "1") (expect ">FILE 2>&1" (c (str "ls /no/such/dir >" file " 2>&1; grep -c such " file)) "1")
(expect "2>&1 before >" (c (str "ls 2>&1 >" file)) (expect "2>&1 before >" (c (str "ls 2>&1 >" file))
"err: bjosh: 2>&1 before > or >> is not supported yet: write >FILE 2>&1\ncode: 2") "err: bjosh: 2>&1 before > or >> is not supported yet: write >FILE 2>&1\ncode: 2")
(expect ">&2 is not supported yet" (c "echo x >&2") (expect ">&2 is not supported yet" (c "printf x >&2")
"err: bjosh: >&2 is not supported yet\ncode: 2") "err: bjosh: >&2 is not supported yet\ncode: 2")
;; Shell commands. ;; Shell commands.

View file

@ -117,6 +117,13 @@
(expect "jobs in a pipe, without jobs" (c "jobs | n") "0") (expect "jobs in a pipe, without jobs" (c "jobs | n") "0")
(expect "history in a pipe, without an editor" (c "history | n") "0") (expect "history in a pipe, without an editor" (c "history | n") "0")
;; echo.
(expect "echo is a value command" (c "type echo") "echo is a value command: echo, echo")
(expect "echo: its words as one string" (c "echo a b | n") "1")
(expect "echo with no words" (c "echo | wc -c") "1")
(expect "echo -n is the program" (c "echo -n x | wc -c") "1")
(expect "echo > a file" (c "echo hi > e.txt; cat e.txt") "hi")
;; Names (decision 4). ;; Names (decision 4).
(expect "a long name that is a program runs the program" (c "printf 'a\\na\\n' | uniq -c") " 2 a") (expect "a long name that is a program runs the program" (c "printf 'a\\na\\n' | uniq -c") " 2 a")
(expect "a short name is always the value command" (c "type u") "u is a value command: uniq, u") (expect "a short name is always the value command" (c "type u") "u is a value command: uniq, u")