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

1893 lines, 156 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(# testing.ysh
1005#
1006# Usage:
1007# source --builtin testing.sh
1008#
1009# func f(x) { return (x + 1) }
1010#
1011# describe foo {
1012# assert (43 === f(42))
1013# }
1014#
1015# if is-main {
1016# run-tests @ARGV # --filter
1017# }
1018
1019module stdlib/testing || return 0
1020
1021source --builtin args.ysh
1022
1023proc assert ( ; cond ; fail_message='default fail message') {
1024 echo 'hi from assert'
1025
1026 = cond
1027
1028 # I think this might be ready now?
1029
1030 var val = evalExpr(cond)
1031
1032 echo
1033 echo 'value'
1034 = val
1035 pp line (val)
1036
1037 = fail_message
1038
1039 if (val) {
1040 echo 'OK'
1041 } else {
1042 var m = evalExpr(fail_message)
1043 echo "FAIL - this is where we extract the string - $m"
1044 }
1045}
1046
1047proc test-assert {
1048 var x = 42
1049 assert [42 === x]
1050}
1051
1052proc test-expr ( ; expr ) {
1053 echo 'expr'
1054 pp line (expr)
1055}
1056
1057proc test-named ( ; ; n=^[99] ) {
1058 echo 'n'
1059 pp line (n)
1060}
1061
1062# What happens when there are duplicate test IDs?
1063#
1064# Also I think filter by "$test_id/$case_id"
1065
1066proc __it (case_id ; ; ; block) {
1067 # This uses a clean directory
1068 echo TODO
1069}
1070
1071# is this accessible to users?
1072# It can contain a global list of things to run
1073
1074# Naming convention: a proc named 'describe' mutates a global named _describe?
1075# Or maybe _describe_list ?
1076
1077var _describe_list = []
1078
1079proc describe (test_id ; ; ; block) {
1080 echo describe
1081 #= desc
1082
1083 # TODO:
1084 # - need append
1085 # - need ::
1086 # _ _describe->append(cmd)
1087 #
1088 # Need to clean this up
1089 # append (_describe, cmd) # does NOT work!
1090
1091 call _describe_list->append(block)
1092}
1093
1094proc Args {
1095 echo TODO
1096}
1097
1098# Problem: this creates a global variable?
1099Args (&spec) {
1100 flag --filter 'Regex of test descriptions'
1101}
1102
1103proc run-tests {
1104 var opt, i = parseArgs(spec, ARGV)
1105
1106 # TODO:
1107 # - parse --filter foo, which you can use eggex for!
1108
1109 for cmd in (_describe) {
1110 # TODO: print filename and 'describe' name?
1111 try {
1112 call io->eval(cmd)
1113 }
1114 if (_status !== 0) {
1115 echo 'failed'
1116 }
1117 }
1118}
1119)zZXx");
1120
1121GLOBAL_STR(gStr41, R"zZXx(# args.ysh
1122#
1123# Usage:
1124# source --builtin args.sh
1125#
1126# parser (&spec) {
1127# flag -v --verbose (help="Verbosely") # default is Bool, false
1128#
1129# flag -P --max-procs ('int', default=-1, doc='''
1130# Run at most P processes at a time
1131# ''')
1132#
1133# flag -i --invert ('bool', default=true, doc='''
1134# Long multiline
1135# Description
1136# ''')
1137#
1138# arg src (help='Source')
1139# arg dest (help='Dest')
1140# arg times (help='Foo')
1141#
1142# rest files
1143# }
1144#
1145# var args = parseArgs(spec, ARGV)
1146#
1147# echo "Verbose $[args.verbose]"
1148
1149# TODO: See list
1150# - It would be nice to keep `flag` and `arg` private, injecting them into the
1151# proc namespace only within `Args`
1152# - We need "type object" to replace the strings 'int', 'bool', etc.
1153# - flag builtin:
1154# - handle only long flag or only short flag
1155# - flag aliases
1156
1157proc parser (; place ; ; block_def) {
1158 ## Create an args spec which can be passed to parseArgs.
1159 ##
1160 ## Example:
1161 ##
1162 ## # NOTE: &spec will create a variable named spec
1163 ## parser (&spec) {
1164 ## flag -v --verbose ('bool')
1165 ## }
1166 ##
1167 ## var args = parseArgs(spec, ARGV)
1168
1169 var p = {flags: [], args: []}
1170 ctx push (p; ; block_def)
1171
1172 # Validate that p.rest = [name] or null and reduce p.rest into name or null.
1173 if ('rest' in p) {
1174 if (len(p.rest) > 1) {
1175 error '`rest` was called more than once' (code=3)
1176 } else {
1177 setvar p.rest = p.rest[0]
1178 }
1179 } else {
1180 setvar p.rest = null
1181 }
1182
1183 var names = {}
1184 for items in ([p.flags, p.args]) {
1185 for x in (items) {
1186 if (x.name in names) {
1187 error "Duplicate flag/arg name $[x.name] in spec" (code=3)
1188 }
1189
1190 setvar names[x.name] = null
1191 }
1192 }
1193
1194 # TODO: what about `flag --name` and then `arg name`?
1195
1196 call place->setValue(p)
1197}
1198
1199proc flag (short, long ; type='bool' ; default=null, help=null) {
1200 ## Declare a flag within an `arg-parse`.
1201 ##
1202 ## Examples:
1203 ##
1204 ## arg-parse (&spec) {
1205 ## flag -v --verbose
1206 ## flag -n --count ('int', default=1)
1207 ## flag -f --file ('str', help="File to process")
1208 ## }
1209
1210 # bool has a default of false, not null
1211 if (type === 'bool' and default === null) {
1212 setvar default = false
1213 }
1214
1215 # TODO: validate `type`
1216
1217 # TODO: Should use "trimPrefix"
1218 var name = long[2:]
1219
1220 ctx emit flags ({short, long, name, type, default, help})
1221}
1222
1223proc arg (name ; ; help=null) {
1224 ## Declare a positional argument within an `arg-parse`.
1225 ##
1226 ## Examples:
1227 ##
1228 ## arg-parse (&spec) {
1229 ## arg name
1230 ## arg config (help="config file path")
1231 ## }
1232
1233 ctx emit args ({name, help})
1234}
1235
1236proc rest (name) {
1237 ## Take the remaining positional arguments within an `arg-parse`.
1238 ##
1239 ## Examples:
1240 ##
1241 ## arg-parse (&grepSpec) {
1242 ## arg query
1243 ## rest files
1244 ## }
1245
1246 # We emit instead of set to detect multiple invocations of "rest"
1247 ctx emit rest (name)
1248}
1249
1250func parseArgs(spec, argv) {
1251 ## Given a spec created by `parser`. Parse an array of strings `argv` per
1252 ## that spec.
1253 ##
1254 ## See `parser` for examples of use.
1255
1256 var i = 0
1257 var positionalPos = 0
1258 var argc = len(argv)
1259 var args = {}
1260 var rest = []
1261
1262 var value
1263 var found
1264 while (i < argc) {
1265 var arg = argv[i]
1266 if (arg.startsWith('-')) {
1267 setvar found = false
1268
1269 for flag in (spec.flags) {
1270 if ( (flag.short and flag.short === arg) or
1271 (flag.long and flag.long === arg) ) {
1272 case (flag.type) {
1273 ('bool') | (null) { setvar value = true }
1274 int {
1275 setvar i += 1
1276 if (i >= len(argv)) {
1277 error "Expected integer after '$arg'" (code=2)
1278 }
1279
1280 try { setvar value = int(argv[i]) }
1281 if (_status !== 0) {
1282 error "Expected integer after '$arg', got '$[argv[i]]'" (code=2)
1283 }
1284 }
1285 }
1286
1287 setvar args[flag.name] = value
1288 setvar found = true
1289 break
1290 }
1291 }
1292
1293 if (not found) {
1294 error "Unknown flag '$arg'" (code=2)
1295 }
1296 } elif (positionalPos >= len(spec.args)) {
1297 if (not spec.rest) {
1298 error "Too many arguments, unexpected '$arg'" (code=2)
1299 }
1300
1301 call rest->append(arg)
1302 } else {
1303 var pos = spec.args[positionalPos]
1304 setvar positionalPos += 1
1305 setvar value = arg
1306 setvar args[pos.name] = value
1307 }
1308
1309 setvar i += 1
1310 }
1311
1312 if (spec.rest) {
1313 setvar args[spec.rest] = rest
1314 }
1315
1316 # Set defaults for flags
1317 for flag in (spec.flags) {
1318 if (flag.name not in args) {
1319 setvar args[flag.name] = flag.default
1320 }
1321 }
1322
1323 # Raise error on missing args
1324 for arg in (spec.args) {
1325 if (arg.name not in args) {
1326 error "Usage Error: Missing required argument $[arg.name]" (code=2)
1327 }
1328 }
1329
1330 return (args)
1331}
1332)zZXx");
1333
1334GLOBAL_STR(gStr42, R"zZXx(const __provide__ = :| any all sum repeat |
1335
1336func any(list) {
1337 ### Returns true if any value in the list is truthy.
1338 # Empty list: returns false
1339
1340 for item in (list) {
1341 if (item) {
1342 return (true)
1343 }
1344 }
1345 return (false)
1346}
1347
1348func all(list) {
1349 ### Returns true if all values in the list are truthy.
1350 # Empty list: returns true
1351
1352 for item in (list) {
1353 if (not item) {
1354 return (false)
1355 }
1356 }
1357 return (true)
1358}
1359
1360func sum(list; start=0) {
1361 ### Returns the sum of all elements in the list.
1362 # Empty list: returns 0
1363
1364 var sum = start
1365 for item in (list) {
1366 setvar sum += item
1367 }
1368 return (sum)
1369}
1370
1371func repeat(x, n) {
1372 ### Returns a list with the given string or list repeated
1373
1374 # Like Python's 'foo'*3 or ['foo', 'bar']*3
1375 # negative numbers are like 0 in Python
1376
1377 var t = type(x)
1378 case (t) {
1379 Str {
1380 var parts = []
1381 for i in (0 .. n) {
1382 call parts->append(x)
1383 }
1384 return (join(parts))
1385 }
1386 List {
1387 var result = []
1388 for i in (0 .. n) {
1389 call result->extend(x)
1390 }
1391 return (result)
1392 }
1393 (else) {
1394 error "Expected Str or List, got $t"
1395 }
1396 }
1397}
1398)zZXx");
1399
1400GLOBAL_STR(gStr43, R"zZXx(const __provide__ = :| identity max min abs |
1401
1402func identity(x) {
1403 ### The identity function. Returns its argument.
1404
1405 return (x)
1406}
1407
1408func __math_select(list, cmp) {
1409 ## Internal helper for `max` and `min`.
1410 ##
1411 ## NOTE: If `list` is empty, then an error is thrown.
1412
1413 if (len(list) === 0) {
1414 error "Unexpected empty list" (code=3)
1415 }
1416
1417 if (len(list) === 1) {
1418 return (list[0])
1419 }
1420
1421 var match = list[0]
1422 for i in (1 .. len(list)) {
1423 setvar match = cmp(list[i], match)
1424 }
1425 return (match)
1426}
1427
1428func max(...args) {
1429 ## Compute the maximum of 2 or more values.
1430 ##
1431 ## `max` takes two different signatures:
1432 ## - `max(a, b)` to return the maximum of `a`, `b`
1433 ## - `max(list)` to return the greatest item in the `list`
1434 ##
1435 ## So, for example:
1436 ##
1437 ## max(1, 2) # => 2
1438 ## max([1, 2, 3]) # => 3
1439
1440 case (len(args)) {
1441 (1) { return (__math_select(args[0], max)) }
1442 (2) {
1443 if (args[0] > args[1]) {
1444 return (args[0])
1445 } else {
1446 return (args[1])
1447 }
1448 }
1449 (else) { error "max expects 1 or 2 args" (code=3) }
1450 }
1451}
1452
1453func min(...args) {
1454 ## Compute the minimum of 2 or more values.
1455 ##
1456 ## `min` takes two different signatures:
1457 ## - `min(a, b)` to return the minimum of `a`, `b`
1458 ## - `min(list)` to return the least item in the `list`
1459 ##
1460 ## So, for example:
1461 ##
1462 ## min(2, 3) # => 2
1463 ## max([1, 2, 3]) # => 1
1464
1465 case (len(args)) {
1466 (1) { return (__math_select(args[0], min)) }
1467 (2) {
1468 if (args[0] < args[1]) {
1469 return (args[0])
1470 } else {
1471 return (args[1])
1472 }
1473 }
1474 (else) { error "min expects 1 or 2 args" (code=3) }
1475 }
1476}
1477
1478func abs(x) {
1479 ## Compute the absolute (positive) value of a number (float or int).
1480
1481 if (x < 0) {
1482 return (-x)
1483 } else {
1484 return (x)
1485 }
1486}
1487)zZXx");
1488
1489GLOBAL_STR(gStr44, R"zZXx(# stream.ysh
1490#
1491# Usage:
1492# source --builtin stream.ysh
1493#
1494# For reading lines, decoding, extracting, splitting
1495
1496# make this file a test server
1497source $LIB_OSH/byo-server.sh
1498
1499source $LIB_YSH/args.ysh
1500
1501proc slurp-by (; num_lines) {
1502 # TODO: (stdin)
1503 for line in (stdin) {
1504 echo TODO
1505 }
1506}
1507
1508# Note:
1509# - these are all the same algorithm
1510# - also word, block, etc. are all optional
1511
1512proc each-line (...words; template=null; ; block=null) {
1513 # TODO:
1514 # parse --j8 --max-jobs flag
1515
1516 # parse template_str as string
1517 # TODO: this is dangerous though ... because you can execute code
1518 # I think you need a SAFE version
1519
1520 # evaluate template string expression - I guess that allows $(echo hi) and so
1521 # forth
1522
1523 # evaluate block with _line binding
1524 # block: execute in parallel with --max-jobs
1525
1526 for line in (stdin) {
1527 echo TODO
1528 }
1529}
1530
1531proc test-each-line {
1532 echo 'TODO: need basic test runner'
1533
1534 # ysh-tool test stream.ysh
1535 #
1536 # Col
1537
1538
1539}
1540
1541proc each-row (; ; block) {
1542 echo TODO
1543}
1544
1545proc split-by (; ifs=null; block) {
1546 echo TODO
1547}
1548
1549proc if-split-by (; ifs=null; block) {
1550 echo TODO
1551}
1552
1553proc chop () {
1554 ### alias for if-split-by
1555 echo TODO
1556}
1557
1558proc must-match (; pattern; block) {
1559 echo TODO
1560}
1561
1562proc if-match (; pattern; block) {
1563 echo TODO
1564}
1565
1566# Protocol:
1567#
1568# - The file lists its tests the "actions"
1569# - Then the test harness runs them
1570# - But should it be ENV vars
1571#
1572# - BYO_LIST_TESTS=1
1573# - BYO_RUN_TEST=foo
1574# - $PWD is a CLEAN temp dir, the process doesn't have to do anything
1575
1576# - silent on success, but prints file on output
1577# - OK this makes sense
1578#
1579# The trivial test in Python:
1580#
1581# from test import byo
1582# byo.maybe_main()
1583#
1584# bash library:
1585# source --builtin byo-server.sh
1586#
1587# byo-maybe-main # reads env variables, and then exits
1588#
1589# source --builtin assertions.ysh
1590#
1591# assert-ok 'echo hi'
1592# assert-stdout 'hi' 'echo -n hi'
1593#
1594# "$@"
1595#
1596# Run all tests
1597# util/byo-client.sh run-tests $YSH stdlib/table.ysh
1598# util/byo-client.sh run-tests -f x $YSH stdlib/table.ysh
1599
1600# Clean process
1601# Clean working dir
1602
1603#
1604# Stream Protocol:
1605# #.byo - is this she-dot, that's for a file
1606# Do we need metadata?
1607#
1608
1609# The harness
1610#
1611# It's process based testing.
1612#
1613# Test runner process: bash or OSH (unlike sharness!)
1614# Tested process: any language - bash,
1615#
1616# Key point: you don't have to quote shell code?
1617
1618list-byo-tests() {
1619 echo TODO
1620}
1621
1622run-byo-tests() {
1623 # source it
1624 echo TODO
1625}
1626
1627byo-maybe-run
1628)zZXx");
1629
1630GLOBAL_STR(gStr45, R"zZXx(# table.ysh - Library for tables.
1631#
1632# Usage:
1633# source --builtin table.ysh
1634
1635# make this file a test server
1636source --builtin osh/byo-server.sh
1637
1638proc table (...words; place; ; block) {
1639 var n = len(words)
1640
1641 # TODO: parse flags
1642 #
1643 # --by-row
1644 # --by-col
1645 #
1646 # Place is optional
1647
1648 if (n === 0) {
1649 echo TODO
1650 return
1651 }
1652
1653 var action = words[0]
1654
1655 # textual operations
1656 case (action) {
1657 cat {
1658 echo todo
1659 }
1660 align {
1661 echo todo
1662 }
1663 tabify {
1664 echo todo
1665 }
1666 tabify {
1667 echo todo
1668 }
1669 header {
1670 echo todo
1671 }
1672 slice {
1673 # this has typed args
1674 # do we do some sort of splat?
1675 echo todo
1676 }
1677 to-tsv {
1678 echo todo
1679 }
1680 }
1681
1682 echo TODO
1683}
1684
1685proc test-table {
1686 return
1687
1688 table (&files1) {
1689 cols num_bytes path
1690 type Int Str
1691
1692 row 10 README.md
1693 row 12 main.py
1694
1695 row (12, 'lib.py')
1696 row (num_bytes=12, path='util.py')
1697 }
1698
1699 # 2 columns - The default is by column?
1700 assert ['Dict' === type(files1)]
1701 assert [2 === len(files1)]
1702
1703 # Same table
1704 table --by-row (&files2) {
1705 cols num_bytes path
1706 type Int Str
1707
1708 row 10 README.md
1709 row 12 main.py
1710
1711 row (12, 'lib.py')
1712 row (num_bytes=12, path='util.py')
1713 }
1714
1715 # 4 rows
1716 assert ['List' === type(files2)]
1717 assert [4 === len(files2)]
1718}
1719
1720# "subcommands" of the dialect
1721
1722proc cols (...names) {
1723 # cols name age
1724 echo TODO
1725}
1726
1727proc types (...types) {
1728 # types - Int? Str?
1729 echo TODO
1730}
1731
1732proc attr (name; ...values) {
1733 # attr units ('-', 'secs')
1734 echo TODO
1735}
1736
1737# is this allowed outside table {} blocks too?
1738proc row {
1739 echo TODO
1740}
1741
1742#
1743# dplyr names
1744#
1745
1746# TODO: can we parse select?
1747
1748proc where {
1749 echo
1750}
1751
1752# TODO: should be able to test argv[0] or something
1753# Or pass to _mutate-transmute
1754
1755proc mutate {
1756 echo TODO
1757}
1758
1759proc transmute {
1760 echo TODO
1761}
1762
1763proc rename {
1764 echo TODO
1765}
1766
1767proc group-by {
1768 echo TODO
1769}
1770
1771proc sort-by {
1772 echo TODO
1773}
1774
1775proc summary {
1776 echo TODO
1777}
1778
1779byo-maybe-run
1780)zZXx");
1781
1782GLOBAL_STR(gStr46, R"zZXx(#!/usr/bin/env bash
1783#
1784# Testing library for bash and OSH.
1785#
1786# Capture status/stdout/stderr, and nq-assert those values.
1787
1788#module yblocks || return 0
1789
1790: ${LIB_OSH=stdlib/osh}
1791source $LIB_OSH/two.sh
1792
1793# There is no yb-run, because you can just use try { } and inspect _error.code
1794# There is no yb-redir, because you can just use try >$tmp { } and inspect _error.code
1795
1796proc yb-capture(; out; ; block) {
1797 ### capture status and stderr
1798
1799 var stdout = ''
1800 try {
1801 { call io->eval(block) } | read --all (&stdout)
1802
1803 # Note that this doesn't parse because of expression issue:
1804 # call io->eval(block) | read --all (&stdout)
1805 # used to be eval (block)
1806 }
1807 # TODO: if 'block' contains a pipeline, we lose this magic var
1808 var result = {status: _pipeline_status[0], stdout}
1809
1810 #echo 'result-1'
1811 #pp test_ (result)
1812
1813 call out->setValue(result)
1814}
1815
1816proc yb-capture-2(; out; ; block) {
1817 ### capture status and stderr
1818
1819 var stderr = ''
1820 try {
1821 fopen 2>&1 { call io->eval(block); } | read --all (&stderr)
1822
1823 # Note that this doesn't parse because of expression issue:
1824 # call io->eval(block) 2>&1 | read --all (&stderr)
1825 # used to be eval (block) 2>&1
1826 }
1827 #pp test_ (_pipeline_status)
1828
1829 var result = {status: _pipeline_status[0], stderr}
1830 #echo 'result-2'
1831 #pp test_ (result)
1832
1833 call out->setValue(result)
1834}
1835)zZXx");
1836
1837
1838
1839TextFile array[] = {
1840 {.rel_path = "_devbuild/help/data-errors", .contents = gStr0},
1841 {.rel_path = "_devbuild/help/data-front-end", .contents = gStr1},
1842 {.rel_path = "_devbuild/help/data-j8-notation", .contents = gStr2},
1843 {.rel_path = "_devbuild/help/help", .contents = gStr3},
1844 {.rel_path = "_devbuild/help/oils-usage", .contents = gStr4},
1845 {.rel_path = "_devbuild/help/osh-builtin-cmd", .contents = gStr5},
1846 {.rel_path = "_devbuild/help/osh-chapters", .contents = gStr6},
1847 {.rel_path = "_devbuild/help/osh-cmd-lang", .contents = gStr7},
1848 {.rel_path = "_devbuild/help/osh-front-end", .contents = gStr8},
1849 {.rel_path = "_devbuild/help/osh-mini-lang", .contents = gStr9},
1850 {.rel_path = "_devbuild/help/osh-option", .contents = gStr10},
1851 {.rel_path = "_devbuild/help/osh-osh-assign", .contents = gStr11},
1852 {.rel_path = "_devbuild/help/osh-plugin", .contents = gStr12},
1853 {.rel_path = "_devbuild/help/osh-special-var", .contents = gStr13},
1854 {.rel_path = "_devbuild/help/osh-stdlib", .contents = gStr14},
1855 {.rel_path = "_devbuild/help/osh-type-method", .contents = gStr15},
1856 {.rel_path = "_devbuild/help/osh-usage", .contents = gStr16},
1857 {.rel_path = "_devbuild/help/osh-word-lang", .contents = gStr17},
1858 {.rel_path = "_devbuild/help/ysh-builtin-cmd", .contents = gStr18},
1859 {.rel_path = "_devbuild/help/ysh-builtin-func", .contents = gStr19},
1860 {.rel_path = "_devbuild/help/ysh-chapters", .contents = gStr20},
1861 {.rel_path = "_devbuild/help/ysh-cmd-lang", .contents = gStr21},
1862 {.rel_path = "_devbuild/help/ysh-expr-lang", .contents = gStr22},
1863 {.rel_path = "_devbuild/help/ysh-front-end", .contents = gStr23},
1864 {.rel_path = "_devbuild/help/ysh-mini-lang", .contents = gStr24},
1865 {.rel_path = "_devbuild/help/ysh-option", .contents = gStr25},
1866 {.rel_path = "_devbuild/help/ysh-plugin", .contents = gStr26},
1867 {.rel_path = "_devbuild/help/ysh-special-var", .contents = gStr27},
1868 {.rel_path = "_devbuild/help/ysh-stdlib", .contents = gStr28},
1869 {.rel_path = "_devbuild/help/ysh-type-method", .contents = gStr29},
1870 {.rel_path = "_devbuild/help/ysh-usage", .contents = gStr30},
1871 {.rel_path = "_devbuild/help/ysh-word-lang", .contents = gStr31},
1872 {.rel_path = "_devbuild/help/ysh-ysh-cmd", .contents = gStr32},
1873 {.rel_path = "stdlib/methods.ysh", .contents = gStr33},
1874 {.rel_path = "stdlib/osh/bash-strict.sh", .contents = gStr34},
1875 {.rel_path = "stdlib/osh/byo-server.sh", .contents = gStr35},
1876 {.rel_path = "stdlib/osh/no-quotes.sh", .contents = gStr36},
1877 {.rel_path = "stdlib/osh/task-five.sh", .contents = gStr37},
1878 {.rel_path = "stdlib/osh/two.sh", .contents = gStr38},
1879 {.rel_path = "stdlib/prelude.ysh", .contents = gStr39},
1880 {.rel_path = "stdlib/testing.ysh", .contents = gStr40},
1881 {.rel_path = "stdlib/ysh/args.ysh", .contents = gStr41},
1882 {.rel_path = "stdlib/ysh/list.ysh", .contents = gStr42},
1883 {.rel_path = "stdlib/ysh/math.ysh", .contents = gStr43},
1884 {.rel_path = "stdlib/ysh/stream.ysh", .contents = gStr44},
1885 {.rel_path = "stdlib/ysh/table.ysh", .contents = gStr45},
1886 {.rel_path = "stdlib/ysh/yblocks.ysh", .contents = gStr46},
1887
1888 {.rel_path = nullptr, .contents = nullptr},
1889};
1890
1891} // namespace embedded_file
1892
1893TextFile* gEmbeddedFiles = embedded_file::array; // turn array into pointer