From 8ded734477c848bbdc8e3760484d1f3b4d2bff511a9971af7ac633769ad7f16b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Bj=C3=B6rnstam?= Date: Tue, 1 Dec 2026 07:29:10 +0100 Subject: [PATCH] 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 --- plan.org | 7 ++++--- src/commands.bjo | 22 ++++++++++++++++++---- src/exec.bjo | 8 ++++++-- tests/run.bjo | 2 +- tests/values.bjo | 7 +++++++ 5 files changed, 36 insertions(+), 10 deletions(-) diff --git a/plan.org b/plan.org index d993896..4363573 100644 --- a/plan.org +++ b/plan.org @@ -288,9 +288,10 @@ the text above:* 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~ has no form in ~(std run)~ yet. -- Not done: ~echo~. A value ~echo~ would take the name from ~/bin/echo~, - which decision 4 does not allow; to decide. -- Tests: ~tests/values.bjo~ (51); ~tests/run.bjo~ (34); +- ~echo~ is a value command, and shadows the program: the one exception to + decision 4. With ~-n~, ~-e~ or ~-E~ the program runs, since a value has + no way to leave out the newline. +- Tests: ~tests/values.bjo~ (56); ~tests/run.bjo~ (34); ~tests/interactive.bjo~ (12). ** Phase 3: more POSIX, and the rest of the edges diff --git a/src/commands.bjo b/src/commands.bjo index b048eee..2ad5ce9 100644 --- a/src/commands.bjo +++ b/src/commands.bjo @@ -5,7 +5,8 @@ ;; (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 -;; 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. ;; - A command reads bytes (`OnBytes`), values (`OnValues`), or nothing ;; (`OnNothing`), and writes values. The caller connects them: see @@ -19,7 +20,7 @@ (import (text csv)) (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 (regex-match? (: System.Text.RegularExpressions.Regex.IsMatch (-> string string bool) @@ -172,6 +173,16 @@ unit) ;; 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)) (defbjo (open-body args out) (unless (= (vec-length args) 1) @@ -441,7 +452,8 @@ (: (-> (Vec ValueCommand))) (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 "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)) @@ -462,7 +474,9 @@ ;; The short and long names, for completion. (: (-> (Vec string))) (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))) diff --git a/src/exec.bjo b/src/exec.bjo index 6b999f5..de41e15 100644 --- a/src/exec.bjo +++ b/src/exec.bjo @@ -170,7 +170,7 @@ (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) (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-form sh expanded text background?))))))) @@ -187,6 +187,10 @@ ;; Value commands next to each other, with the file the first reads (<) ;; 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)) (: from (Option string)) (: into (Option (Tuple string bool)))))) @@ -238,7 +242,7 @@ (let* ((e (vec-ref expanded i)) (argv (record-ref e argv)) (name (vec-ref argv 0))) - (match (stage-command sh name) + (match (stage-command-of sh argv) ((Some c) (def stage (ValueStage (command c) (args (vec-drop argv 1)))) (def seg (match current diff --git a/tests/run.bjo b/tests/run.bjo index 847589c..0a7ba78 100644 --- a/tests/run.bjo +++ b/tests/run.bjo @@ -70,7 +70,7 @@ (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)) "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") ;; Shell commands. diff --git a/tests/values.bjo b/tests/values.bjo index bd16b1c..24f4b86 100644 --- a/tests/values.bjo +++ b/tests/values.bjo @@ -117,6 +117,13 @@ (expect "jobs in a pipe, without jobs" (c "jobs | 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). (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")