OILS / _gen / bin / text_files.cc View on Github | oilshell.org

1779 lines, 152 significant
1
2#include "cpp/embedded_file.h"
3
4namespace embedded_file {
5GLOBAL_STR(gStr0, R"zZXx(
6 Errors
7 <a class="group-link" href="chap-errors.html">errors</a>
8
9
10 [UTF8] err-utf8-encode err-utf8-decode
11 [J8 String] err-j8-str-encode err-j8-str-decode
12 [J8 Lines] err-j8-lines-encode err-j8-lines-decode
13 [JSON] err-json-encode err-json-decode
14 [JSON8] err-json8-encode err-json8-decode
15)zZXx");
16
17GLOBAL_STR(gStr1, R"zZXx(
18
19 Front End <a class="group-link" href="chap-front-end.html">front-end</a>
20
21
22 [Lexing] ascii-whitespace [ \t\r\n]
23 ascii-control-chars
24)zZXx");
25
26GLOBAL_STR(gStr2, R"zZXx(
27 J8 Notation
28 <a class="group-link" href="chap-j8.html">j8</a>
29
30
31 [J8 Strings] json-string "hi"
32 json-escape \" \\ \u1234
33 surrogate-pair \ud83e\udd26
34 j8-escape \' \u{1f926} \yff
35 u-prefix u'hi'
36 b-prefix b'hi'
37 no-prefix 'hi'
38 [J8 Lines] unquoted-line
39 [JSON8] json8-num json8-str
40 X json8-list X json8-dict
41 json8-comment
42 [TSV8] column-attrs column-types
43)zZXx");
44
45GLOBAL_STR(gStr3, R"zZXx(Usage: help TOPIC?
46
47Examples:
48
49 help # this help
50 help echo # help on the 'echo' builtin
51 help command-sub # help on command sub $(date)
52
53 help oils-usage # identical to oils-for-unix --help
54 help osh-usage # osh --help
55 help ysh-usage # ysh --help
56)zZXx");
57
58GLOBAL_STR(gStr4, R"zZXx(bin/oils-for-unix is an executable that contains OSH, YSH, and more.
59
60Usage: oils-for-unix MAIN_NAME ARG*
61 MAIN_NAME ARG*
62
63It behaves like busybox. The command name can be passed as the first argument:
64
65 oils-for-unix ysh -c 'echo hi'
66
67More commonly, it's invoked through a symlink like 'ysh', which causes it to
68behave like that command:
69
70 ysh -c 'echo hi'
71)zZXx");
72
73GLOBAL_STR(gStr5, R"zZXx(
74 Builtin Commands <a class="group-link" href="chap-builtin-cmd.html">builtin-cmd</a>
75
76
77 [I/O] read echo printf
78 readarray mapfile
79 [Run Code] source . eval trap
80 [Set Options] set shopt
81 [Working Dir] cd pwd pushd popd dirs
82 [Completion] complete compgen compopt compadjust compexport
83 [Shell Process] exec X logout
84 umask ulimit times
85 [Child Process] jobs wait
86 fg X bg X kill X disown
87 [External] test [ getopts
88 [Introspection] help hash cmd/type X caller
89 [Word Lookup] command builtin
90 [Interactive] alias unalias history X fc X bind
91X [Unsupported] enable
92)zZXx");
93
94GLOBAL_STR(gStr6, R"zZXx(The reference is divided in to "chapters", each of which has its own table of
95contents. Type:
96
97 help osh-$CHAPTER
98
99Where $CHAPTER is one of:
100
101 front-end
102 command-lang
103 osh-assign
104 word-lang
105 mini-lang
106 builtin-cmd
107 option
108 special-var
109 plugin
110
111Example:
112
113 help osh-word-lang
114)zZXx");
115
116GLOBAL_STR(gStr7, R"zZXx(
117 Command Language <a class="group-link" href="chap-cmd-lang.html">cmd-lang</a>
118
119
120 [Commands] simple-command semicolon ;
121 [Conditional] case if dbracket [[
122 true false colon :
123 bang ! and && or ||
124 [Iteration] while until for for-expr-sh ((
125 [Control Flow] break continue return exit
126 [Grouping] sh-func sh-block { subshell (
127 [Concurrency] pipe | X pipe-amp |& ampersand &
128 [Redirects] redir-file > >> >| < <> not impl: &>
129 redir-desc >& <&
130 here-doc << <<- <<<
131 [Other Command] dparen (( time X coproc X select
132)zZXx");
133
134GLOBAL_STR(gStr8, R"zZXx(
135 Front End <a class="group-link" href="chap-front-end.html">front-end</a>
136
137
138 [Usage] oils-usage osh-usage config
139 startup line-editing exit-codes
140 [Lexing] comment # line-continuation \ ascii-whitespace [ \t\r\n]
141)zZXx");
142
143GLOBAL_STR(gStr9, R"zZXx(
144 Other Mini Languages <a class="group-link" href="chap-mini-lang.html">mini-lang</a>
145
146
147 [Arithmetic] arith-context Where legacy arithmetic is allowed
148 sh-numbers 0xFF 0755 etc.
149 sh-arith 1 + 2*3 a *= 2
150 sh-logical !a && b
151 sh-bitwise ~a ^ b
152 [Boolean] bool-expr [[ ! $x && $y || $z ]]
153 test ! $x -a $y -o $z
154 bool-infix $a -nt $b $x == $y
155 bool-path -d /etc
156 bool-str -n foo -z ''
157 bool-other -o errexit -v name[index]
158 [Patterns] glob-pat *.py
159 extglob ,(*.py|*.sh)
160 regex [[ foo =~ [a-z]+ ]]
161 [Other Sublang] braces {alice,bob}@example.com
162 histsub !$ !! !n
163 char-escapes \t \c \x00 \u03bc
164)zZXx");
165
166GLOBAL_STR(gStr10, R"zZXx(
167 Global Shell Options <a class="group-link" href="chap-option.html">option</a>
168
169
170 [Errors] nounset -u errexit -e inherit_errexit pipefail
171 [Globbing] noglob -f nullglob failglob X dotglob
172 dashglob (true)
173 [Other Option] noclobber -C errtrace -E
174 [Debugging] xtrace X verbose X extdebug
175 [Interactive] emacs vi
176 [Compat] eval_unsafe_arith ignore_flags_not_impl
177)zZXx");
178
179GLOBAL_STR(gStr11, R"zZXx(
180 Assignments and Expressions <a class="group-link" href="chap-osh-assign.html">osh-assign</a>
181
182
183 [Literals] sh-array array=(a b c) array[1]=B "${a[@]}"
184 sh-assoc assoc=(['a']=1 ['b']=2) assoc['x']=b
185 [Operators] sh-assign str='xyz'
186 sh-append str+='abc'
187 [Builtins] local readonly export unset shift
188 declare typeset X let
189)zZXx");
190
191GLOBAL_STR(gStr12, R"zZXx(
192 Plugins and Hooks <a class="group-link" href="chap-plugin.html">plugin</a>
193
194
195 [Signals] SIGTERM SIGINT SIGQUIT
196 SIGTTIN SIGTTOU SIGWINCH
197 [Traps] DEBUG ERR EXIT X RETURN
198 [Words] PS1 X PS2 X PS3 PS4
199 [Completion] complete
200 [Other Plugin] PROMPT_COMMAND X command_not_found
201)zZXx");
202
203GLOBAL_STR(gStr13, R"zZXx(
204 Special Variables <a class="group-link" href="chap-special-var.html">special-var</a>
205
206
207 [POSIX Special] $@ $* $# $? $- $$ $! $0 $9
208 [Shell Vars] IFS X LANG X GLOBIGNORE
209 [Shell Options] SHELLOPTS X BASHOPTS
210 [Other Env] HOME PATH
211 [Other Special] BASH_REMATCH @PIPESTATUS
212 [Platform] HOSTNAME OSTYPE
213 [Call Stack] @BASH_SOURCE @FUNCNAME @BASH_LINENO
214 X @BASH_ARGV X @BASH_ARGC
215 [Tracing] LINENO
216 [Process State] UID EUID PPID X BASHPID
217X [Process Stack] BASH_SUBSHELL SHLVL
218X [Shell State] BASH_CMDS @DIRSTACK
219 [Completion] @COMP_WORDS COMP_CWORD COMP_LINE COMP_POINT
220 COMP_WORDBREAKS @COMPREPLY X COMP_KEY
221 X COMP_TYPE COMP_ARGV
222 [History] HISTFILE
223 [cd] PWD OLDPWD X CDPATH
224 [getopts] OPTIND OPTARG X OPTERR
225 [read] REPLY
226 [Functions] X RANDOM SECONDS
227 [Oils VM] OILS_VERSION LIB_OSH
228)zZXx");
229
230GLOBAL_STR(gStr14, R"zZXx(
231 Standard Library <a class="group-link" href="chap-stdlib.html">stdlib</a>
232
233
234 [two] log die
235 [no-quotes] nq-assert nq-run
236 nq-capture nq-capture-2
237 nq-redir nq-redir-2
238 [bash-strict]
239 [task-five]
240)zZXx");
241
242GLOBAL_STR(gStr15, R"zZXx(
243 OSH Types <a class="group-link" href="chap-type-method.html">type-method</a>
244
245
246 [OSH] BashArray BashAssoc
247)zZXx");
248
249GLOBAL_STR(gStr16, R"zZXx(bin/osh is compatible with POSIX shell, bash, and other shells.
250
251Usage: osh FLAG* SCRIPT ARG*
252 osh FLAG* -c COMMAND ARG*
253 osh FLAG*
254
255The command line accepted by `bin/osh` is compatible with /bin/sh and bash.
256
257 osh -c 'echo hi'
258 osh myscript.sh
259 echo 'echo hi' | osh
260
261It also has a few enhancements:
262
263 osh -n -c 'hello' # pretty-print the AST
264 osh --ast-format text -n -c 'hello' # print it full
265
266osh accepts POSIX sh flags, with these additions:
267
268 -n parse the program but don't execute it. Print the AST.
269 --ast-format what format the AST should be in
270)zZXx");
271
272GLOBAL_STR(gStr17, R"zZXx(
273 Word Language <a class="group-link" href="chap-word-lang.html">word-lang</a>
274
275
276 [Quotes] osh-string 'abc' $'line\n' "$var"
277 [Substitutions] command-sub $(command) `command`
278 var-sub ${var} $0 $9
279 arith-sub $((1 + 2))
280 tilde-sub ~/src
281 proc-sub diff <(sort L.txt) <(sort R.txt)
282 [Var Ops] op-test ${x:-default}
283 op-strip ${x%%suffix} etc.
284 op-patsub ${x//y/z}
285 op-index ${a[i+1}
286 op-slice ${a[@]:0:1}
287 op-format ${x@P}
288)zZXx");
289
290GLOBAL_STR(gStr18, R"zZXx(
291 Builtin Commands <a class="group-link" href="chap-builtin-cmd.html">builtin-cmd</a>
292
293
294 [Memory] cmd/append Add elements to end of array
295 pp value proc test_
296 asdl_ cell_ X gc-stats_
297 [Handle Errors] error error 'failed' (status=2)
298 try Run with errexit, set _error
299 failed Test if _error.code !== 0
300 boolstatus Enforce 0 or 1 exit status
301 assert assert [42 === f(x)]
302 [Shell State] ysh-cd ysh-shopt compatible, and takes a block
303 shvar Temporary modify global settings
304 ctx Share and update a temporary "context"
305 push-registers Save registers like $?, PIPESTATUS
306 [Introspection] runproc Run a proc; use as main entry point
307 X extern Run an external command, with an ENV
308 X invoke Control which "invokables" are run
309 [Modules]
310 source-guard guard against duplicate 'source'
311 is-main false when sourcing a file
312 use create a module Obj from a source file
313 [I/O] ysh-read flags --all, -0
314 ysh-echo no -e -n with simple_echo
315 write Like echo, with --, --sep, --end
316 fork forkwait Replace & and (), and takes a block
317 fopen Open multiple streams, takes a block
318 [Hay Config] hay haynode For DSLs and config files
319 [Completion] compadjust compexport
320 [Data Formats] json read write
321 json8 read write
322)zZXx");
323
324GLOBAL_STR(gStr19, R"zZXx(
325 Builtin Functions <a class="group-link" href="chap-builtin-func.html">builtin-func</a>
326
327
328 [Values] len() func/type()
329 [Conversions] bool() int() float()
330 str() list() dict()
331 X runes() X encodeRunes()
332 X bytes() X encodeBytes()
333 [Str] X strcmp() X split() shSplit()
334 [List] join()
335 [Dict] keys() values() get()
336 [Float] floatsEqual() X isinf() X isnan()
337 [Obj] Object() prototype() propView()
338 get()
339 [Word] glob() maybe()
340 [Serialize] toJson() fromJson()
341 toJson8() fromJson8()
342X [J8 Decode] J8.Bool() J8.Int() ...
343 [Pattern] _group() _start() _end()
344 [Introspection] id()
345 shvarGet() getVar() setVar()
346 parseCommand() X parseExpr() evalExpr()
347 [Hay Config] parseHay() evalHay()
348X [Hashing] sha1dc() sha256()
349)zZXx");
350
351GLOBAL_STR(gStr20, R"zZXx(The reference is divided in to "chapters", each of which has its own table of
352contents. Type:
353
354 help ysh-$CHAPTER
355
356Where $CHAPTER is one of:
357
358 front-end
359 command-lang
360 expr-lang
361 word-lang
362 builtin-cmd
363 option
364 special-var
365 type-method
366 builtin-func
367
368Example:
369
370 help ysh-expr-lang
371)zZXx");
372
373GLOBAL_STR(gStr21, R"zZXx(
374 Command Language <a class="group-link" href="chap-cmd-lang.html">cmd-lang</a>
375
376
377 [YSH Simple] typed-arg json write (x)
378 lazy-expr-arg assert [42 === x]
379 block-arg cd /tmp { echo $PWD }; cd /tmp (; ; blockexpr)
380 [YSH Cond] ysh-case case (x) { *.py { echo 'python' } }
381 ysh-if if (x > 0) { echo }
382 [YSH Iter] ysh-for for i, item in (mylist) { echo }
383 ysh-while while (x > 0) { echo }
384)zZXx");
385
386GLOBAL_STR(gStr22, R"zZXx(
387 Expression Language and Assignments <a class="group-link" href="chap-expr-lang.html">expr-lang</a>
388
389
390 [Assignment] assign =
391 aug-assign += -= *= /= **= //= %=
392 &= |= ^= <<= >>=
393 [Literals] atom-literal true false null
394 int-literal 42 65_536 0xFF 0o755 0b10
395 float-lit 3.14 1.5e-10
396 char-literal \\ \t \" \y00 \u{3bc}
397 X num-suffix 42 K Ki M Mi G Gi T Ti / ms us
398 ysh-string "x is $x" $"x is $x" r'[a-z]\n'
399 u'line\n' b'byte \yff'
400 triple-quoted """ $""" r''' u''' b'''
401 str-template ^"$a and $b" for Str::replace()
402 list-literal ['one', 'two', 3] :| unquoted words |
403 dict-literal {name: 'bob'} {a, b}
404 range 1 .. n+1
405 block-expr ^(echo $PWD)
406 expr-literal ^[1 + 2*3]
407 X expr-sub $[myobj]
408 X expr-splice @[myobj]
409 [Operators] op-precedence Like Python
410 concat s1 ++ s2, L1 ++ L2
411 ysh-equals === !== ~== is, is not
412 ysh-in in, not in
413 ysh-compare < <= > >= (numbers only)
414 ysh-logical not and or
415 ysh-arith + - * / // % **
416 ysh-bitwise ~ & | ^ << >>
417 ysh-ternary '+' if x >= 0 else '-'
418 ysh-index s[0] mylist[3] mydict['key']
419 ysh-attr mydict.key mystr.startsWith('x')
420 ysh-slice a[1:-1] s[1:-1]
421 func-call f(x, y; ...named)
422 thin-arrow mylist->pop()
423 fat-arrow mylist => join() => upper()
424 match-ops ~ !~ ~~ !~~
425 [Eggex] re-literal / d+ ; re-flags ; ERE /
426 re-primitive %zero 'sq'
427 class-literal [c a-z 'abc' @str_var \\ \xFF \u{3bc}]
428 named-class dot digit space word d s w
429 re-repeat d? d* d+ d{3} d{2,4}
430 re-compound seq1 seq2 alt1|alt2 (expr1 expr2)
431 re-capture <capture d+ as name: int>
432 re-splice Subpattern @subpattern
433 re-flags reg_icase reg_newline
434 X re-multiline ///
435)zZXx");
436
437GLOBAL_STR(gStr23, R"zZXx(
438 Front End <a class="group-link" href="chap-front-end.html">front-end</a>
439
440
441 [Usage] oils-usage ysh-usage
442 [Lexing] ascii-whitespace [ \t\r\n]
443 doc-comment ### multiline-command ...
444 [Tools] cat-em
445)zZXx");
446
447GLOBAL_STR(gStr24, R"zZXx(
448 Other Mini Languages <a class="group-link" href="chap-mini-lang.html">mini-lang</a>
449
450
451 [Patterns] glob-pat *.py
452 [Other Sublang] braces {alice,bob}@example.com
453)zZXx");
454
455GLOBAL_STR(gStr25, R"zZXx(
456 Global Shell Options <a class="group-link" href="chap-option.html">option</a>
457
458
459 [Groups] strict:all ysh:upgrade ysh:all
460 [YSH Details] opts-redefine opts-internal
461)zZXx");
462
463GLOBAL_STR(gStr26, R"zZXx(
464 Plugins and Hooks <a class="group-link" href="chap-plugin.html">plugin</a>
465
466
467 [YSH] renderPrompt()
468)zZXx");
469
470GLOBAL_STR(gStr27, R"zZXx(
471 Special Variables <a class="group-link" href="chap-special-var.html">special-var</a>
472
473
474 [YSH Vars] ARGV X ENV X _ESCAPE
475 _this_dir
476 [YSH Status] _error
477 _pipeline_status _process_sub_status
478 [YSH Tracing] SHX_indent SHX_punct SHX_pid_str
479 [YSH read] _reply
480 [History] YSH_HISTFILE
481 [Oils VM] OILS_VERSION
482 OILS_GC_THRESHOLD OILS_GC_ON_EXIT
483 OILS_GC_STATS OILS_GC_STATS_FD
484 LIB_YSH
485 [Float] NAN INFINITY
486 [Module] __provide__
487)zZXx");
488
489GLOBAL_STR(gStr28, R"zZXx(
490 Standard Library<a class="group-link" href="chap-stdlib.html">stdlib</a>
491
492
493 [math] abs() max() min() X round()
494 sum()
495 [list] all() any() repeat()
496 [yblocks] yb-capture yb-capture-2
497 [args] parser flag arg rest
498 parseArgs()
499)zZXx");
500
501GLOBAL_STR(gStr29, R"zZXx(
502 Types and Methods <a class="group-link" href="chap-type-method.html">type-method</a>
503
504
505 [Atom Types] Null Bool
506 [Number Types] Int Float
507 [Str] X find() replace()
508 trim() trimStart() trimEnd()
509 startsWith() endsWith()
510 upper() lower()
511 search() leftMatch()
512 [List] List/append() pop() extend() indexOf()
513 X insert() X remove() reverse()
514 [Dict] erase() X inc() X accum()
515 [Range]
516 [Eggex]
517 [Match] group() start() end()
518 X groups() X groupDict()
519 [Place] setValue()
520 [Code Types] Expr Command
521 BuiltinFunc BoundFunc
522X [Func] name() location() toJson()
523X [Proc] name() location() toJson()
524 [IO] eval() evalToDict() captureStdout()
525 promptVal()
526 X time() X strftime() X glob()
527 [Obj] __invoke__ X __call__
528)zZXx");
529
530GLOBAL_STR(gStr30, R"zZXx(bin/ysh is the shell with data tYpes, influenced by pYthon, JavaScript, ...
531
532Usage: ysh FLAG* SCRIPT ARG*
533 ysh FLAG* -c COMMAND ARG*
534 ysh FLAG*
535
536`bin/ysh` is the same as `bin/osh` with a the `ysh:all` option group set. So
537`bin/ysh` also accepts shell flags.
538
539 ysh -c 'echo hi'
540 ysh myscript.ysh
541 echo 'echo hi' | ysh
542)zZXx");
543
544GLOBAL_STR(gStr31, R"zZXx(
545 Word Language <a class="group-link" href="chap-word-lang.html">word-lang</a>
546
547
548 [Quotes] ysh-string "x is $x" $"x is $x" r'[a-z]\n'
549 u'line\n' b'byte \yff'
550 triple-quoted """ $""" r''' u''' b'''
551 X tagged-str "<span id=$x>"html
552 [Substitutions] expr-sub echo $[42 + a[i]]
553 expr-splice echo @[split(x)]
554 var-splice @myarray @ARGV
555 command-sub @(cat my-j8-lines.txt)
556 [Formatting] X ysh-printf ${x %.3f}
557 X ysh-format ${x|html}
558)zZXx");
559
560GLOBAL_STR(gStr32, R"zZXx(
561 YSH Command Language Keywords <a class="group-link" href="chap-ysh-cmd.html">ysh-cmd</a>
562
563
564 [Assignment] const var Declare variables
565 setvar setvar a[i] = 42
566 setglobal setglobal d.key = 'foo'
567 [Expression] equal = = 1 + 2*3
568 call call mylist->append(42)
569 [Definitions] proc proc p (s, ...rest) {
570 typed proc p (; typed, ...rest; n=0; b) {
571 func func f(x; opt1, opt2) { return (x + 1) }
572 ysh-return return (myexpr)
573)zZXx");
574
575GLOBAL_STR(gStr33, R"zZXx(# Can we define methods in pure YSH?
576#
577# (mylist->find(42) !== -1)
578#
579# instead of
580#
581# ('42' in mylist)
582#
583# Because 'in' is for Dict
584
585func find (haystack List, needle) {
586 for i, x in (haystack) {
587 if (x === needle) {
588 return (i)
589 }
590 }
591 return (-1)
592}
593)zZXx");
594
595GLOBAL_STR(gStr34, R"zZXx(# Bash strict mode, updated for 2024
596
597set -o nounset
598set -o pipefail
599set -o errexit
600shopt -s inherit_errexit
601shopt -s strict:all 2>/dev/null || true # dogfood for OSH
602
603)zZXx");
604
605GLOBAL_STR(gStr35, R"zZXx(# Library to turn a shell file into a "BYO test server"
606#
607# Usage:
608#
609# # from both bash and OSH
610# if test -z "$LIB_OSH"; then LIB_OSH=stdlib/osh; fi
611# source $LIB_OSH/byo-server-lib.sh
612#
613# The client creates a clean process state and directory state for each tests.
614#
615# (This file requires compgen -A, and maybe declare -f, so it's not POSIX
616# shell.)
617
618: ${LIB_OSH:-stdlib/osh}
619source $LIB_OSH/two.sh
620
621# List all functions defined in this file (and not in sourced files).
622_bash-print-funcs() {
623 ### Print shell functions in this file that don't start with _ (bash reflection)
624
625 local funcs
626 funcs=($(compgen -A function))
627
628 # extdebug makes `declare -F` print the file path, but, annoyingly, only
629 # if you pass the function names as arguments.
630 shopt -s extdebug
631
632 # bash format:
633 # func1 1 path1
634 # func2 2 path2 # where 2 is the linen umber
635
636 #declare -F "${funcs[@]}"
637
638 # TODO: do we need to normalize the LHS and RHS of $3 == path?
639 declare -F "${funcs[@]}" | awk -v "path=$0" '$3 == path { print $1 }'
640
641 shopt -u extdebug
642}
643
644_gawk-print-funcs() {
645 ### Print shell functions in this file that don't start with _ (awk parsing)
646
647 # Using gawk because it has match()
648 # - doesn't start with _
649
650 # space = / ' '* /
651 # shfunc = / %begin
652 # <capture !['_' ' '] ![' ']*>
653 # '()' space '{' space
654 # %end /
655 # docstring = / %begin
656 # space '###' ' '+
657 # <capture dot*>
658 # %end /
659 gawk '
660 match($0, /^([^_ ][^ ]*)\(\)[ ]*{[ ]*$/, m) {
661 #print NR " shfunc " m[1]
662 print m[1]
663 #print m[0]
664 }
665
666 match($0, /^[ ]*###[ ]+(.*)$/, m) {
667 print NR " docstring " m[1]
668 }
669' $0
670}
671
672_print-funcs() {
673 _bash-print-funcs
674 return
675
676 # TODO: make gawk work, with docstrings
677 if command -v gawk > /dev/null; then
678 _gawk-print-funcs
679 else
680 _bash-print-funcs
681 fi
682}
683
684
685byo-maybe-run() {
686 local command=${BYO_COMMAND:-}
687
688 case $command in
689 '')
690 # Do nothing if it's not specified
691 return
692 ;;
693
694 detect)
695 # all the commands supported, except 'detect'
696 echo list-tests
697 echo run-test
698
699 exit 66 # ASCII code for 'B' - what the protocol specifies
700 ;;
701
702 list-tests)
703 # TODO: use _bash-print-funcs? This fixes the transitive test problem,
704 # which happened in soil/web-remote-test.sh
705 # But it should work with OSH, not just bash! We need shopt -s extdebug
706 compgen -A function | grep '^test-'
707 exit 0
708 ;;
709
710 run-test)
711 local test_name=${BYO_ARG:-}
712 if test -z "$test_name"; then
713 die "BYO run-test: Expected BYO_ARG"
714 fi
715
716 # Avoid issues polluting recursive calls!
717 unset BYO_COMMAND BYO_ARG
718
719 # Shell convention: we name functions test-*
720 "$test_name"
721
722 # Only run if not set -e. Either way it's equivalent
723 exit $?
724 ;;
725
726 *)
727 die "Invalid BYO command '$command'"
728 ;;
729 esac
730
731 # Do nothing if BYO_COMMAND is not set.
732 # The program continues to its "main".
733}
734
735byo-must-run() {
736 local command=${BYO_COMMAND:-}
737 if test -z "$command"; then
738 die "Expected BYO_COMMAND= in environment"
739 fi
740
741 byo-maybe-run
742}
743)zZXx");
744
745GLOBAL_STR(gStr36, R"zZXx(#!/usr/bin/env bash
746#
747# Testing library for bash and OSH.
748#
749# Capture status/stdout/stderr, and nq-assert those values.
750
751: ${LIB_OSH=stdlib/osh}
752source $LIB_OSH/two.sh
753
754nq-assert() {
755 ### Assertion with same syntax as shell 'test'
756
757 if ! test "$@"; then
758 die "line ${BASH_LINENO[0]}: nq-assert $(printf '%q ' "$@") failed"
759 fi
760}
761
762# Problem: we want to capture status and stdout at the same time
763#
764# We use:
765#
766# __stdout=$(set -o errexit; "$@")
767# __status=$?
768#
769# However, we lose the trailing \n, since that's how command subs work.
770
771# Here is another possibility:
772#
773# shopt -s lastpipe # need this too
774# ( set -o errexit; "$@" ) | read -r -d __stdout
775# __status=${PIPESTATUS[0]}
776# shopt -u lastpipe
777#
778# But this feels complex for just the \n issue, which can be easily worked
779# around.
780
781nq-run() {
782 ### capture status only
783
784 local -n out_status=$1
785 shift
786
787 local __status
788
789 # Tricky: turn errexit off so we can capture it, but turn it on against
790 set +o errexit
791 ( set -o errexit; "$@" )
792 __status=$?
793 set -o errexit
794
795 out_status=$__status
796}
797
798nq-capture() {
799 ### capture status and stdout
800
801 local -n out_status=$1
802 local -n out_stdout=$2
803 shift 2
804
805 local __status
806 local __stdout
807
808 # Tricky: turn errexit off so we can capture it, but turn it on against
809 set +o errexit
810 __stdout=$(set -o errexit; "$@")
811 __status=$?
812 set -o errexit
813
814 out_status=$__status
815 out_stdout=$__stdout
816}
817
818nq-capture-2() {
819 ### capture status and stderr
820
821 # This is almost identical to the above
822
823 local -n out_status=$1
824 local -n out_stderr=$2
825 shift 2
826
827 local __status
828 local __stderr
829
830 # Tricky: turn errexit off so we can capture it, but turn it on against
831 set +o errexit
832 __stderr=$(set -o errexit; "$@" 2>&1)
833 __status=$?
834 set -o errexit
835
836 out_status=$__status
837 out_stderr=$__stderr
838}
839
840# 'byo test' can set this?
841: ${NQ_TEST_TEMP=/tmp}
842
843nq-redir() {
844 ### capture status and stdout
845
846 local -n out_status=$1
847 local -n out_stdout_file=$2
848 shift 2
849
850 local __status
851 local __stdout_file=$NQ_TEST_TEMP/nq-redir-$$.txt
852
853 # Tricky: turn errexit off so we can capture it, but turn it on against
854 set +o errexit
855 ( set -o errexit; "$@" ) > $__stdout_file
856 __status=$?
857 set -o errexit
858
859 out_status=$__status
860 out_stdout_file=$__stdout_file
861}
862
863nq-redir-2() {
864 ### capture status and stdout
865
866 local -n out_status=$1
867 local -n out_stderr_file=$2
868 shift 2
869
870 local __status
871 local __stderr_file=$NQ_TEST_TEMP/nq-redir-$$.txt
872
873 # Tricky: turn errexit off so we can capture it, but turn it on against
874 set +o errexit
875 ( set -o errexit; "$@" ) 2> $__stderr_file
876 __status=$?
877 set -o errexit
878
879 out_status=$__status
880 out_stderr_file=$__stderr_file
881}
882)zZXx");
883
884GLOBAL_STR(gStr37, R"zZXx(#!/usr/bin/env bash
885#
886# Common shell functions for task scripts.
887#
888# Usage:
889# source $LIB_OSH/task-five.sh
890#
891# test-foo() { # define task functions
892# echo foo
893# }
894# task-five "$@"
895
896# Definition of a "task"
897#
898# - File invokes task-five "$@"
899# - or maybe you can look at its source
900# - It's a shell function
901# - Has ### docstring
902# - Doesn't start with _
903
904: ${LIB_OSH=stdlib/osh}
905source $LIB_OSH/byo-server.sh
906
907_show-help() {
908 # TODO:
909 # - Use awk to find comments at the top of the file?
910 # - Use OSH to extract docstrings
911 # - BYO_COMMAND=list-tasks will reuse that logic? It only applies to the
912 # current file, not anything in a different file?
913
914 echo "Usage: $0 TASK_NAME ARGS..."
915 echo
916 echo "To complete tasks, run:"
917 echo " source devtools/completion.bash"
918 echo
919 echo "Tasks:"
920
921 if command -v column >/dev/null; then
922 _print-funcs | column
923 else
924 _print-funcs
925 fi
926}
927
928task-five() {
929 # Respond to BYO_COMMAND=list-tasks, etc. All task files need this.
930 byo-maybe-run
931
932 case ${1:-} in
933 ''|--help|-h)
934 _show-help
935 exit 0
936 ;;
937 esac
938
939 if ! declare -f "$1" >/dev/null; then
940 echo "$0: '$1' isn't an action in this task file. Try '$0 --help'"
941 exit 1
942 fi
943
944 "$@"
945}
946)zZXx");
947
948GLOBAL_STR(gStr38, R"zZXx(# Two functions I actually use, all the time.
949#
950# To keep depenedencies small, this library will NEVER grow other functions
951# (and is named to imply that.)
952#
953# Usage:
954# source --builtin two.sh
955#
956# Examples:
957# log 'hi'
958# die 'expected a number'
959
960if command -v source-guard >/dev/null; then # include guard for YSH
961 source-guard two || return 0
962fi
963
964log() {
965 ### Write a message to stderr.
966 echo "$@" >&2
967}
968
969die() {
970 ### Write an error message with the script name, and exit with status 1.
971 log "$0: fatal: $@"
972 exit 1
973}
974
975)zZXx");
976
977GLOBAL_STR(gStr39, R"zZXx(# These were helpful while implementing args.ysh
978# Maybe we will want to export them in a prelude so that others can use them too?
979#
980# Prior art: Rust has `todo!()` which is quite nice. Other languages allow
981# users to `raise NotImplmentedError()`.
982
983# Andy comments:
984# - 'pass' can be : or true in shell. It's a little obscure / confusing, but
985# there is an argument for minimalism. Although I prefer words like 'true',
986# and that already means something.
987# - UPDATE: we once took 'pass' as a keyword, but users complained because
988# there is a command 'pass'. So we probably can't have this by default.
989# Need to discuss source --builtin.
990
991# - todo could be more static? Rust presumably does it at compile time
992
993proc todo () {
994 ## Raises a not implemented error when run.
995 error ("TODO: not implemented") # TODO: is error code 1 ok?
996}
997
998proc pass () {
999 ## Use when you want to temporarily leave a block empty.
1000 _ null
1001}
1002)zZXx");
1003
1004GLOBAL_STR(gStr40, R"zZXx(# args.ysh
1005#
1006# Usage:
1007# source --builtin args.sh
1008
1009const __provide__ = :| parser flag arg rest parseArgs |
1010
1011#
1012#
1013# parser (&spec) {
1014# flag -v --verbose (help="Verbosely") # default is Bool, false
1015#
1016# flag -P --max-procs ('int', default=-1, doc='''
1017# Run at most P processes at a time
1018# ''')
1019#
1020# flag -i --invert ('bool', default=true, doc='''
1021# Long multiline
1022# Description
1023# ''')
1024#
1025# arg src (help='Source')
1026# arg dest (help='Dest')
1027# arg times (help='Foo')
1028#
1029# rest files
1030# }
1031#
1032# var args = parseArgs(spec, ARGV)
1033#
1034# echo "Verbose $[args.verbose]"
1035
1036# TODO: See list
1037# - It would be nice to keep `flag` and `arg` private, injecting them into the
1038# proc namespace only within `Args`
1039# - We need "type object" to replace the strings 'int', 'bool', etc.
1040# - flag builtin:
1041# - handle only long flag or only short flag
1042# - flag aliases
1043
1044proc parser (; place ; ; block_def) {
1045 ## Create an args spec which can be passed to parseArgs.
1046 ##
1047 ## Example:
1048 ##
1049 ## # NOTE: &spec will create a variable named spec
1050 ## parser (&spec) {
1051 ## flag -v --verbose ('bool')
1052 ## }
1053 ##
1054 ## var args = parseArgs(spec, ARGV)
1055
1056 var p = {flags: [], args: []}
1057 ctx push (p; ; block_def)
1058
1059 # Validate that p.rest = [name] or null and reduce p.rest into name or null.
1060 if ('rest' in p) {
1061 if (len(p.rest) > 1) {
1062 error '`rest` was called more than once' (code=3)
1063 } else {
1064 setvar p.rest = p.rest[0]
1065 }
1066 } else {
1067 setvar p.rest = null
1068 }
1069
1070 var names = {}
1071 for items in ([p.flags, p.args]) {
1072 for x in (items) {
1073 if (x.name in names) {
1074 error "Duplicate flag/arg name $[x.name] in spec" (code=3)
1075 }
1076
1077 setvar names[x.name] = null
1078 }
1079 }
1080
1081 # TODO: what about `flag --name` and then `arg name`?
1082
1083 call place->setValue(p)
1084}
1085
1086proc flag (short, long ; type='bool' ; default=null, help=null) {
1087 ## Declare a flag within an `arg-parse`.
1088 ##
1089 ## Examples:
1090 ##
1091 ## arg-parse (&spec) {
1092 ## flag -v --verbose
1093 ## flag -n --count ('int', default=1)
1094 ## flag -f --file ('str', help="File to process")
1095 ## }
1096
1097 # bool has a default of false, not null
1098 if (type === 'bool' and default === null) {
1099 setvar default = false
1100 }
1101
1102 # TODO: validate `type`
1103
1104 # TODO: Should use "trimPrefix"
1105 var name = long[2:]
1106
1107 ctx emit flags ({short, long, name, type, default, help})
1108}
1109
1110proc arg (name ; ; help=null) {
1111 ## Declare a positional argument within an `arg-parse`.
1112 ##
1113 ## Examples:
1114 ##
1115 ## arg-parse (&spec) {
1116 ## arg name
1117 ## arg config (help="config file path")
1118 ## }
1119
1120 ctx emit args ({name, help})
1121}
1122
1123proc rest (name) {
1124 ## Take the remaining positional arguments within an `arg-parse`.
1125 ##
1126 ## Examples:
1127 ##
1128 ## arg-parse (&grepSpec) {
1129 ## arg query
1130 ## rest files
1131 ## }
1132
1133 # We emit instead of set to detect multiple invocations of "rest"
1134 ctx emit rest (name)
1135}
1136
1137func parseArgs(spec, argv) {
1138 ## Given a spec created by `parser`. Parse an array of strings `argv` per
1139 ## that spec.
1140 ##
1141 ## See `parser` for examples of use.
1142
1143 var i = 0
1144 var positionalPos = 0
1145 var argc = len(argv)
1146 var args = {}
1147 var rest = []
1148
1149 var value
1150 var found
1151 while (i < argc) {
1152 var arg = argv[i]
1153 if (arg.startsWith('-')) {
1154 setvar found = false
1155
1156 for flag in (spec.flags) {
1157 if ( (flag.short and flag.short === arg) or
1158 (flag.long and flag.long === arg) ) {
1159 case (flag.type) {
1160 ('bool') | (null) { setvar value = true }
1161 int {
1162 setvar i += 1
1163 if (i >= len(argv)) {
1164 error "Expected integer after '$arg'" (code=2)
1165 }
1166
1167 try { setvar value = int(argv[i]) }
1168 if (_status !== 0) {
1169 error "Expected integer after '$arg', got '$[argv[i]]'" (code=2)
1170 }
1171 }
1172 }
1173
1174 setvar args[flag.name] = value
1175 setvar found = true
1176 break
1177 }
1178 }
1179
1180 if (not found) {
1181 error "Unknown flag '$arg'" (code=2)
1182 }
1183 } elif (positionalPos >= len(spec.args)) {
1184 if (not spec.rest) {
1185 error "Too many arguments, unexpected '$arg'" (code=2)
1186 }
1187
1188 call rest->append(arg)
1189 } else {
1190 var pos = spec.args[positionalPos]
1191 setvar positionalPos += 1
1192 setvar value = arg
1193 setvar args[pos.name] = value
1194 }
1195
1196 setvar i += 1
1197 }
1198
1199 if (spec.rest) {
1200 setvar args[spec.rest] = rest
1201 }
1202
1203 # Set defaults for flags
1204 for flag in (spec.flags) {
1205 if (flag.name not in args) {
1206 setvar args[flag.name] = flag.default
1207 }
1208 }
1209
1210 # Raise error on missing args
1211 for arg in (spec.args) {
1212 if (arg.name not in args) {
1213 error "Usage Error: Missing required argument $[arg.name]" (code=2)
1214 }
1215 }
1216
1217 return (args)
1218}
1219)zZXx");
1220
1221GLOBAL_STR(gStr41, R"zZXx(const __provide__ = :| any all sum repeat |
1222
1223func any(list) {
1224 ### Returns true if any value in the list is truthy.
1225 # Empty list: returns false
1226
1227 for item in (list) {
1228 if (item) {
1229 return (true)
1230 }
1231 }
1232 return (false)
1233}
1234
1235func all(list) {
1236 ### Returns true if all values in the list are truthy.
1237 # Empty list: returns true
1238
1239 for item in (list) {
1240 if (not item) {
1241 return (false)
1242 }
1243 }
1244 return (true)
1245}
1246
1247func sum(list; start=0) {
1248 ### Returns the sum of all elements in the list.
1249 # Empty list: returns 0
1250
1251 var sum = start
1252 for item in (list) {
1253 setvar sum += item
1254 }
1255 return (sum)
1256}
1257
1258func repeat(x, n) {
1259 ### Returns a list with the given string or list repeated
1260
1261 # Like Python's 'foo'*3 or ['foo', 'bar']*3
1262 # negative numbers are like 0 in Python
1263
1264 var t = type(x)
1265 case (t) {
1266 Str {
1267 var parts = []
1268 for i in (0 .. n) {
1269 call parts->append(x)
1270 }
1271 return (join(parts))
1272 }
1273 List {
1274 var result = []
1275 for i in (0 .. n) {
1276 call result->extend(x)
1277 }
1278 return (result)
1279 }
1280 (else) {
1281 error "Expected Str or List, got $t"
1282 }
1283 }
1284}
1285)zZXx");
1286
1287GLOBAL_STR(gStr42, R"zZXx(const __provide__ = :| identity max min abs |
1288
1289func identity(x) {
1290 ### The identity function. Returns its argument.
1291
1292 return (x)
1293}
1294
1295func __math_select(list, cmp) {
1296 ## Internal helper for `max` and `min`.
1297 ##
1298 ## NOTE: If `list` is empty, then an error is thrown.
1299
1300 if (len(list) === 0) {
1301 error "Unexpected empty list" (code=3)
1302 }
1303
1304 if (len(list) === 1) {
1305 return (list[0])
1306 }
1307
1308 var match = list[0]
1309 for i in (1 .. len(list)) {
1310 setvar match = cmp(list[i], match)
1311 }
1312 return (match)
1313}
1314
1315func max(...args) {
1316 ## Compute the maximum of 2 or more values.
1317 ##
1318 ## `max` takes two different signatures:
1319 ## - `max(a, b)` to return the maximum of `a`, `b`
1320 ## - `max(list)` to return the greatest item in the `list`
1321 ##
1322 ## So, for example:
1323 ##
1324 ## max(1, 2) # => 2
1325 ## max([1, 2, 3]) # => 3
1326
1327 case (len(args)) {
1328 (1) { return (__math_select(args[0], max)) }
1329 (2) {
1330 if (args[0] > args[1]) {
1331 return (args[0])
1332 } else {
1333 return (args[1])
1334 }
1335 }
1336 (else) { error "max expects 1 or 2 args" (code=3) }
1337 }
1338}
1339
1340func min(...args) {
1341 ## Compute the minimum of 2 or more values.
1342 ##
1343 ## `min` takes two different signatures:
1344 ## - `min(a, b)` to return the minimum of `a`, `b`
1345 ## - `min(list)` to return the least item in the `list`
1346 ##
1347 ## So, for example:
1348 ##
1349 ## min(2, 3) # => 2
1350 ## max([1, 2, 3]) # => 1
1351
1352 case (len(args)) {
1353 (1) { return (__math_select(args[0], min)) }
1354 (2) {
1355 if (args[0] < args[1]) {
1356 return (args[0])
1357 } else {
1358 return (args[1])
1359 }
1360 }
1361 (else) { error "min expects 1 or 2 args" (code=3) }
1362 }
1363}
1364
1365func abs(x) {
1366 ## Compute the absolute (positive) value of a number (float or int).
1367
1368 if (x < 0) {
1369 return (-x)
1370 } else {
1371 return (x)
1372 }
1373}
1374)zZXx");
1375
1376GLOBAL_STR(gStr43, R"zZXx(# stream.ysh
1377#
1378# Usage:
1379# source --builtin stream.ysh
1380#
1381# For reading lines, decoding, extracting, splitting
1382
1383# make this file a test server
1384source $LIB_OSH/byo-server.sh
1385
1386source $LIB_YSH/args.ysh
1387
1388proc slurp-by (; num_lines) {
1389 # TODO: (stdin)
1390 for line in (stdin) {
1391 echo TODO
1392 }
1393}
1394
1395# Note:
1396# - these are all the same algorithm
1397# - also word, block, etc. are all optional
1398
1399proc each-line (...words; template=null; ; block=null) {
1400 # TODO:
1401 # parse --j8 --max-jobs flag
1402
1403 # parse template_str as string
1404 # TODO: this is dangerous though ... because you can execute code
1405 # I think you need a SAFE version
1406
1407 # evaluate template string expression - I guess that allows $(echo hi) and so
1408 # forth
1409
1410 # evaluate block with _line binding
1411 # block: execute in parallel with --max-jobs
1412
1413 for line in (stdin) {
1414 echo TODO
1415 }
1416}
1417
1418proc test-each-line {
1419 echo 'TODO: need basic test runner'
1420
1421 # ysh-tool test stream.ysh
1422 #
1423 # Col
1424
1425
1426}
1427
1428proc each-row (; ; block) {
1429 echo TODO
1430}
1431
1432proc split-by (; ifs=null; block) {
1433 echo TODO
1434}
1435
1436proc if-split-by (; ifs=null; block) {
1437 echo TODO
1438}
1439
1440proc chop () {
1441 ### alias for if-split-by
1442 echo TODO
1443}
1444
1445proc must-match (; pattern; block) {
1446 echo TODO
1447}
1448
1449proc if-match (; pattern; block) {
1450 echo TODO
1451}
1452
1453# Protocol:
1454#
1455# - The file lists its tests the "actions"
1456# - Then the test harness runs them
1457# - But should it be ENV vars
1458#
1459# - BYO_LIST_TESTS=1
1460# - BYO_RUN_TEST=foo
1461# - $PWD is a CLEAN temp dir, the process doesn't have to do anything
1462
1463# - silent on success, but prints file on output
1464# - OK this makes sense
1465#
1466# The trivial test in Python:
1467#
1468# from test import byo
1469# byo.maybe_main()
1470#
1471# bash library:
1472# source --builtin byo-server.sh
1473#
1474# byo-maybe-main # reads env variables, and then exits
1475#
1476# source --builtin assertions.ysh
1477#
1478# assert-ok 'echo hi'
1479# assert-stdout 'hi' 'echo -n hi'
1480#
1481# "$@"
1482#
1483# Run all tests
1484# util/byo-client.sh run-tests $YSH stdlib/table.ysh
1485# util/byo-client.sh run-tests -f x $YSH stdlib/table.ysh
1486
1487# Clean process
1488# Clean working dir
1489
1490#
1491# Stream Protocol:
1492# #.byo - is this she-dot, that's for a file
1493# Do we need metadata?
1494#
1495
1496# The harness
1497#
1498# It's process based testing.
1499#
1500# Test runner process: bash or OSH (unlike sharness!)
1501# Tested process: any language - bash,
1502#
1503# Key point: you don't have to quote shell code?
1504
1505list-byo-tests() {
1506 echo TODO
1507}
1508
1509run-byo-tests() {
1510 # source it
1511 echo TODO
1512}
1513
1514byo-maybe-run
1515)zZXx");
1516
1517GLOBAL_STR(gStr44, R"zZXx(# table.ysh - Library for tables.
1518#
1519# Usage:
1520# source --builtin table.ysh
1521
1522# make this file a test server
1523source --builtin osh/byo-server.sh
1524
1525proc table (...words; place; ; block) {
1526 var n = len(words)
1527
1528 # TODO: parse flags
1529 #
1530 # --by-row
1531 # --by-col
1532 #
1533 # Place is optional
1534
1535 if (n === 0) {
1536 echo TODO
1537 return
1538 }
1539
1540 var action = words[0]
1541
1542 # textual operations
1543 case (action) {
1544 cat {
1545 echo todo
1546 }
1547 align {
1548 echo todo
1549 }
1550 tabify {
1551 echo todo
1552 }
1553 tabify {
1554 echo todo
1555 }
1556 header {
1557 echo todo
1558 }
1559 slice {
1560 # this has typed args
1561 # do we do some sort of splat?
1562 echo todo
1563 }
1564 to-tsv {
1565 echo todo
1566 }
1567 }
1568
1569 echo TODO
1570}
1571
1572proc test-table {
1573 return
1574
1575 table (&files1) {
1576 cols num_bytes path
1577 type Int Str
1578
1579 row 10 README.md
1580 row 12 main.py
1581
1582 row (12, 'lib.py')
1583 row (num_bytes=12, path='util.py')
1584 }
1585
1586 # 2 columns - The default is by column?
1587 assert ['Dict' === type(files1)]
1588 assert [2 === len(files1)]
1589
1590 # Same table
1591 table --by-row (&files2) {
1592 cols num_bytes path
1593 type Int Str
1594
1595 row 10 README.md
1596 row 12 main.py
1597
1598 row (12, 'lib.py')
1599 row (num_bytes=12, path='util.py')
1600 }
1601
1602 # 4 rows
1603 assert ['List' === type(files2)]
1604 assert [4 === len(files2)]
1605}
1606
1607# "subcommands" of the dialect
1608
1609proc cols (...names) {
1610 # cols name age
1611 echo TODO
1612}
1613
1614proc types (...types) {
1615 # types - Int? Str?
1616 echo TODO
1617}
1618
1619proc attr (name; ...values) {
1620 # attr units ('-', 'secs')
1621 echo TODO
1622}
1623
1624# is this allowed outside table {} blocks too?
1625proc row {
1626 echo TODO
1627}
1628
1629#
1630# dplyr names
1631#
1632
1633# TODO: can we parse select?
1634
1635proc where {
1636 echo
1637}
1638
1639# TODO: should be able to test argv[0] or something
1640# Or pass to _mutate-transmute
1641
1642proc mutate {
1643 echo TODO
1644}
1645
1646proc transmute {
1647 echo TODO
1648}
1649
1650proc rename {
1651 echo TODO
1652}
1653
1654proc group-by {
1655 echo TODO
1656}
1657
1658proc sort-by {
1659 echo TODO
1660}
1661
1662proc summary {
1663 echo TODO
1664}
1665
1666byo-maybe-run
1667)zZXx");
1668
1669GLOBAL_STR(gStr45, R"zZXx(#!/usr/bin/env bash
1670#
1671# Testing library for bash and OSH.
1672#
1673# Capture status/stdout/stderr, and nq-assert those values.
1674
1675const __provide__ = :| yb-capture yb-capture-2 |
1676
1677: ${LIB_OSH=stdlib/osh}
1678source $LIB_OSH/two.sh
1679
1680# There is no yb-run, because you can just use try { } and inspect _error.code
1681# There is no yb-redir, because you can just use try >$tmp { } and inspect _error.code
1682
1683proc yb-capture(; out; ; block) {
1684 ### capture status and stdout
1685
1686 var stdout = ''
1687 try {
1688 { call io->eval(block) } | read --all (&stdout)
1689
1690 # Note that this doesn't parse because of expression issue:
1691 # call io->eval(block) | read --all (&stdout)
1692 # used to be eval (block)
1693 }
1694 # TODO: if 'block' contains a pipeline, we lose this magic var
1695 var result = {status: _pipeline_status[0], stdout}
1696
1697 #echo 'result-1'
1698 #pp test_ (result)
1699
1700 call out->setValue(result)
1701}
1702
1703proc yb-capture-2(; out; ; block) {
1704 ### capture status and stderr
1705
1706 var stderr = ''
1707 try {
1708 redir 2>&1 { call io->eval(block); } | read --all (&stderr)
1709
1710 # Note that this doesn't parse because of expression issue:
1711 # call io->eval(block) 2>&1 | read --all (&stderr)
1712 # used to be eval (block) 2>&1
1713 }
1714 #pp test_ (_pipeline_status)
1715
1716 var result = {status: _pipeline_status[0], stderr}
1717 #echo 'result-2'
1718 #pp test_ (result)
1719
1720 call out->setValue(result)
1721}
1722)zZXx");
1723
1724
1725
1726TextFile array[] = {
1727 {.rel_path = "_devbuild/help/data-errors", .contents = gStr0},
1728 {.rel_path = "_devbuild/help/data-front-end", .contents = gStr1},
1729 {.rel_path = "_devbuild/help/data-j8-notation", .contents = gStr2},
1730 {.rel_path = "_devbuild/help/help", .contents = gStr3},
1731 {.rel_path = "_devbuild/help/oils-usage", .contents = gStr4},
1732 {.rel_path = "_devbuild/help/osh-builtin-cmd", .contents = gStr5},
1733 {.rel_path = "_devbuild/help/osh-chapters", .contents = gStr6},
1734 {.rel_path = "_devbuild/help/osh-cmd-lang", .contents = gStr7},
1735 {.rel_path = "_devbuild/help/osh-front-end", .contents = gStr8},
1736 {.rel_path = "_devbuild/help/osh-mini-lang", .contents = gStr9},
1737 {.rel_path = "_devbuild/help/osh-option", .contents = gStr10},
1738 {.rel_path = "_devbuild/help/osh-osh-assign", .contents = gStr11},
1739 {.rel_path = "_devbuild/help/osh-plugin", .contents = gStr12},
1740 {.rel_path = "_devbuild/help/osh-special-var", .contents = gStr13},
1741 {.rel_path = "_devbuild/help/osh-stdlib", .contents = gStr14},
1742 {.rel_path = "_devbuild/help/osh-type-method", .contents = gStr15},
1743 {.rel_path = "_devbuild/help/osh-usage", .contents = gStr16},
1744 {.rel_path = "_devbuild/help/osh-word-lang", .contents = gStr17},
1745 {.rel_path = "_devbuild/help/ysh-builtin-cmd", .contents = gStr18},
1746 {.rel_path = "_devbuild/help/ysh-builtin-func", .contents = gStr19},
1747 {.rel_path = "_devbuild/help/ysh-chapters", .contents = gStr20},
1748 {.rel_path = "_devbuild/help/ysh-cmd-lang", .contents = gStr21},
1749 {.rel_path = "_devbuild/help/ysh-expr-lang", .contents = gStr22},
1750 {.rel_path = "_devbuild/help/ysh-front-end", .contents = gStr23},
1751 {.rel_path = "_devbuild/help/ysh-mini-lang", .contents = gStr24},
1752 {.rel_path = "_devbuild/help/ysh-option", .contents = gStr25},
1753 {.rel_path = "_devbuild/help/ysh-plugin", .contents = gStr26},
1754 {.rel_path = "_devbuild/help/ysh-special-var", .contents = gStr27},
1755 {.rel_path = "_devbuild/help/ysh-stdlib", .contents = gStr28},
1756 {.rel_path = "_devbuild/help/ysh-type-method", .contents = gStr29},
1757 {.rel_path = "_devbuild/help/ysh-usage", .contents = gStr30},
1758 {.rel_path = "_devbuild/help/ysh-word-lang", .contents = gStr31},
1759 {.rel_path = "_devbuild/help/ysh-ysh-cmd", .contents = gStr32},
1760 {.rel_path = "stdlib/methods.ysh", .contents = gStr33},
1761 {.rel_path = "stdlib/osh/bash-strict.sh", .contents = gStr34},
1762 {.rel_path = "stdlib/osh/byo-server.sh", .contents = gStr35},
1763 {.rel_path = "stdlib/osh/no-quotes.sh", .contents = gStr36},
1764 {.rel_path = "stdlib/osh/task-five.sh", .contents = gStr37},
1765 {.rel_path = "stdlib/osh/two.sh", .contents = gStr38},
1766 {.rel_path = "stdlib/prelude.ysh", .contents = gStr39},
1767 {.rel_path = "stdlib/ysh/args.ysh", .contents = gStr40},
1768 {.rel_path = "stdlib/ysh/list.ysh", .contents = gStr41},
1769 {.rel_path = "stdlib/ysh/math.ysh", .contents = gStr42},
1770 {.rel_path = "stdlib/ysh/stream.ysh", .contents = gStr43},
1771 {.rel_path = "stdlib/ysh/table.ysh", .contents = gStr44},
1772 {.rel_path = "stdlib/ysh/yblocks.ysh", .contents = gStr45},
1773
1774 {.rel_path = nullptr, .contents = nullptr},
1775};
1776
1777} // namespace embedded_file
1778
1779TextFile* gEmbeddedFiles = embedded_file::array; // turn array into pointer