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

1894 lines, 158 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-replace ${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 line
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 [Modules] runproc Run a proc; use as main entry point
307 module guard against duplicate 'source'
308 is-main false when sourcing a file
309 use change first word lookup
310 [I/O] ysh-read flags --all, -0
311 ysh-echo no -e -n with simple_echo
312 write Like echo, with --, --sep, --end
313 fork forkwait Replace & and (), and takes a block
314 fopen Open multiple streams, takes a block
315 X dbg Only thing that can be used in funcs
316 [Hay Config] hay haynode For DSLs and config files
317 [Completion] compadjust compexport
318 [Data Formats] json read write
319 json8 read write
320)zZXx");
321
322GLOBAL_STR(gStr19, R"zZXx(
323 Builtin Functions <a class="group-link" href="chap-builtin-func.html">builtin-func</a>
324
325
326 [Values] len() func/type()
327 [Conversions] bool() int() float()
328 str() list() dict()
329 X runes() X encodeRunes()
330 X bytes() X encodeBytes()
331 [Str] X strcmp() X split() shSplit()
332 [List] join()
333 [Float] floatsEqual() X isinf() X isnan()
334 [Collections] X copy() X deepCopy()
335 [Word] glob() maybe()
336 [Serialize] toJson() fromJson()
337 toJson8() fromJson8()
338X [J8 Decode] J8.Bool() J8.Int() ...
339 [Pattern] _group() _start() _end()
340 [Introspection] shvarGet() getVar() evalExpr()
341 [Hay Config] parseHay() evalHay()
342X [Hashing] sha1dc() sha256()
343)zZXx");
344
345GLOBAL_STR(gStr20, R"zZXx(The reference is divided in to "chapters", each of which has its own table of
346contents. Type:
347
348 help ysh-$CHAPTER
349
350Where $CHAPTER is one of:
351
352 front-end
353 command-lang
354 expr-lang
355 word-lang
356 builtin-cmd
357 option
358 special-var
359 type-method
360 builtin-func
361
362Example:
363
364 help ysh-expr-lang
365)zZXx");
366
367GLOBAL_STR(gStr21, R"zZXx(
368 Command Language <a class="group-link" href="chap-cmd-lang.html">cmd-lang</a>
369
370
371 [YSH Simple] typed-arg json write (x)
372 lazy-expr-arg assert [42 === x]
373 block-arg cd /tmp { echo $PWD }; cd /tmp (; ; blockexpr)
374 [YSH Cond] ysh-case case (x) { *.py { echo 'python' } }
375 ysh-if if (x > 0) { echo }
376 [YSH Iter] ysh-while while (x > 0) { echo }
377 ysh-for for i, item in (mylist) { echo }
378)zZXx");
379
380GLOBAL_STR(gStr22, R"zZXx(
381 Expression Language and Assignments <a class="group-link" href="chap-expr-lang.html">expr-lang</a>
382
383
384 [Assignment] assign =
385 aug-assign += -= *= /= **= //= %=
386 &= |= ^= <<= >>=
387 [Literals] atom-literal true false null
388 int-literal 42 65_536 0xFF 0o755 0b10
389 float-lit 3.14 1.5e-10
390 char-literal \\ \t \" \y00 \u{3bc}
391 X num-suffix 42 K Ki M Mi G Gi T Ti / ms us
392 ysh-string "x is $x" $"x is $x" r'[a-z]\n'
393 u'line\n' b'byte \yff'
394 triple-quoted """ $""" r''' u''' b'''
395 str-template ^"$a and $b" for Str::replace()
396 list-literal ['one', 'two', 3] :| unquoted words |
397 dict-literal {name: 'bob'} {a, b}
398 range 1 .. n+1
399 block-expr ^(echo $PWD)
400 expr-literal ^[1 + 2*3]
401 X expr-sub $[myobj]
402 X expr-splice @[myobj]
403 [Operators] op-precedence Like Python
404 concat s1 ++ s2, L1 ++ L2
405 ysh-equals === !== ~== is, is not
406 ysh-in in, not in
407 ysh-compare < <= > >= (numbers only)
408 ysh-logical not and or
409 ysh-arith + - * / // % **
410 ysh-bitwise ~ & | ^ << >>
411 ysh-ternary '+' if x >= 0 else '-'
412 ysh-index s[0] mylist[3] mydict['key']
413 ysh-attr mydict.key
414 ysh-slice a[1:-1] s[1:-1]
415 func-call f(x, y; ...named)
416 thin-arrow mylist->pop()
417 fat-arrow mystr => startsWith('prefix')
418 match-ops ~ !~ ~~ !~~
419 [Eggex] re-literal / d+ ; re-flags ; ERE /
420 re-primitive %zero 'sq'
421 class-literal [c a-z 'abc' @str_var \\ \xFF \u{3bc}]
422 named-class dot digit space word d s w
423 re-repeat d? d* d+ d{3} d{2,4}
424 re-compound seq1 seq2 alt1|alt2 (expr1 expr2)
425 re-capture <capture d+ as name: int>
426 re-splice Subpattern @subpattern
427 re-flags reg_icase reg_newline
428 X re-multiline ///
429)zZXx");
430
431GLOBAL_STR(gStr23, R"zZXx(
432 Front End <a class="group-link" href="chap-front-end.html">front-end</a>
433
434
435 [Usage] oils-usage ysh-usage
436 [Lexing] ascii-whitespace [ \t\r\n]
437 doc-comment ### multiline-command ...
438 [Tools] cat-em
439)zZXx");
440
441GLOBAL_STR(gStr24, R"zZXx(
442 Other Mini Languages <a class="group-link" href="chap-mini-lang.html">mini-lang</a>
443
444
445 [Patterns] glob-pat *.py
446 [Other Sublang] braces {alice,bob}@example.com
447)zZXx");
448
449GLOBAL_STR(gStr25, R"zZXx(
450 Global Shell Options <a class="group-link" href="chap-option.html">option</a>
451
452
453 [Groups] strict:all ysh:upgrade ysh:all
454 [YSH Details] opts-redefine opts-internal
455)zZXx");
456
457GLOBAL_STR(gStr26, R"zZXx(
458 Plugins and Hooks <a class="group-link" href="chap-plugin.html">plugin</a>
459
460
461 [YSH] renderPrompt()
462)zZXx");
463
464GLOBAL_STR(gStr27, R"zZXx(
465 Special Variables <a class="group-link" href="chap-special-var.html">special-var</a>
466
467
468 [YSH Vars] ARGV X ENV X _ESCAPE
469 _this_dir
470 [YSH Status] _error
471 _pipeline_status _process_sub_status
472 [YSH Tracing] SHX_indent SHX_punct SHX_pid_str
473 [YSH read] _reply
474 [History] YSH_HISTFILE
475 [Oils VM] OILS_VERSION
476 OILS_GC_THRESHOLD OILS_GC_ON_EXIT
477 OILS_GC_STATS OILS_GC_STATS_FD
478 LIB_YSH
479 [Float] NAN INFINITY
480)zZXx");
481
482GLOBAL_STR(gStr28, R"zZXx(
483 Standard Library<a class="group-link" href="chap-stdlib.html">stdlib</a>
484
485
486 [math] abs()
487 max() min()
488 X round()
489 sum()
490 [list] all() any()
491 repeat()
492 [args] parser Parse command line arguments
493 flag
494 arg
495 rest
496 parseArgs()
497 [yblocks] yb-capture
498 yb-capture-2
499X [Lines] slurp-by combine adjacent lines into cells
500X [Awk] each-line --j8 --max-jobs (Str, Template, Block) - xargs
501 each-row --max-jobs (Str, Template, Block) - xargs
502 each-word xargs-like splitting, similar to IFS too
503 split-by (str=\n, ifs=':', pattern=/s+/)
504 if-split-by
505 chop alias for split-by (pattern=/s+/)
506 must-match (/ <capture d+> </capture w+> /)
507 if-match
508X [Table Create] table --by-row --by-col (&place); construct/parse a table
509 table/cols cols name age - cols name:Str age:Int
510 types type Str Int
511 attr attr units - secs
512 row emit row
513 table cat concatenate TSV8
514 table align to ssv8
515 table tabify to tsv8
516 table header (cols = :|name age|, types = :|Str Int|, units = :|- secs|)
517 table slice e.g. slice (1, -1) slice (5, 7)
518 table to-tsv lose type info, and error on \t in cells
519X [Table Ops] where subset of rows; dplyr filter()
520 pick subset of columns ('select' taken by shell)
521 mutate transmute [average = count / sum] - drop the ones that are used?
522 rename (bytes='bytes', path='filename')
523 group-by add a column with a group ID [ext]
524 sort-by sort by columns; dplyr arrange() [ext]
525 summary count, sum, histogram, any, all, reduce(), etc. [ext]
526)zZXx");
527
528GLOBAL_STR(gStr29, R"zZXx(
529 Types and Methods <a class="group-link" href="chap-type-method.html">type-method</a>
530
531
532 [Atom Types] Null Bool
533 [Number Types] Int Float
534 [Str] X find() replace()
535 trim() trimStart() trimEnd()
536 startsWith() endsWith()
537 upper() lower()
538 search() leftMatch()
539 [List] List/append() pop() extend() indexOf()
540 X insert() X remove() reverse()
541 [Dict] keys() values() X get() erase()
542 X inc() X accum()
543 [Range]
544 [Eggex]
545 [Match] group() start() end()
546 X groups() X groupDict()
547 [Place] setValue()
548 [Code Types] Expr Command
549 BuiltinFunc BoundFunc
550X [Func] name() location() toJson()
551X [Proc] name() location() toJson()
552X [Module] name() filename()
553 [IO] eval() captureStdout()
554 promptVal()
555 X time() X strftime()
556 X glob()
557X [Guts] heapId()
558)zZXx");
559
560GLOBAL_STR(gStr30, R"zZXx(bin/ysh is the shell with data tYpes, influenced by pYthon, JavaScript, ...
561
562Usage: ysh FLAG* SCRIPT ARG*
563 ysh FLAG* -c COMMAND ARG*
564 ysh FLAG*
565
566`bin/ysh` is the same as `bin/osh` with a the `ysh:all` option group set. So
567`bin/ysh` also accepts shell flags.
568
569 ysh -c 'echo hi'
570 ysh myscript.ysh
571 echo 'echo hi' | ysh
572)zZXx");
573
574GLOBAL_STR(gStr31, R"zZXx(
575 Word Language <a class="group-link" href="chap-word-lang.html">word-lang</a>
576
577
578 [Quotes] ysh-string "x is $x" $"x is $x" r'[a-z]\n'
579 u'line\n' b'byte \yff'
580 triple-quoted """ $""" r''' u''' b'''
581 X tagged-str "<span id=$x>"html
582 [Substitutions] expr-sub echo $[42 + a[i]]
583 expr-splice echo @[split(x)]
584 var-splice @myarray @ARGV
585 command-sub @(split command)
586 [Formatting] X ysh-printf ${x %.3f}
587 X ysh-format ${x|html}
588)zZXx");
589
590GLOBAL_STR(gStr32, R"zZXx(
591 YSH Command Language Keywords <a class="group-link" href="chap-ysh-cmd.html">ysh-cmd</a>
592
593
594 [Assignment] const var Declare variables
595 setvar setvar a[i] = 42
596 setglobal setglobal d.key = 'foo'
597 [Expression] equal = = 1 + 2*3
598 call call mylist->append(42)
599 [Definitions] proc proc p (s, ...rest) {
600 typed proc p (; typed, ...rest; n=0; b) {
601 func func f(x; opt1, opt2) { return (x + 1) }
602 ysh-return return (myexpr)
603)zZXx");
604
605GLOBAL_STR(gStr33, R"zZXx(func identity(x) {
606 ## The identity function. Returns its argument.
607
608 return (x)
609}
610)zZXx");
611
612GLOBAL_STR(gStr34, R"zZXx(# Can we define methods in pure YSH?
613#
614# (mylist->find(42) !== -1)
615#
616# instead of
617#
618# ('42' in mylist)
619#
620# Because 'in' is for Dict
621
622func find (haystack List, needle) {
623 for i, x in (haystack) {
624 if (x === needle) {
625 return (i)
626 }
627 }
628 return (-1)
629}
630)zZXx");
631
632GLOBAL_STR(gStr35, R"zZXx(# Bash strict mode, updated for 2024
633
634set -o nounset
635set -o pipefail
636set -o errexit
637shopt -s inherit_errexit
638shopt -s strict:all 2>/dev/null || true # dogfood for OSH
639
640)zZXx");
641
642GLOBAL_STR(gStr36, R"zZXx(# Library to turn a shell file into a "BYO test server"
643#
644# Usage:
645#
646# # from both bash and OSH
647# if test -z "$LIB_OSH"; then LIB_OSH=stdlib/osh; fi
648# source $LIB_OSH/byo-server-lib.sh
649#
650# The client creates a clean process state and directory state for each tests.
651#
652# (This file requires compgen -A, and maybe declare -f, so it's not POSIX
653# shell.)
654
655: ${LIB_OSH:-stdlib/osh}
656source $LIB_OSH/two.sh
657
658byo-maybe-run() {
659 local command=${BYO_COMMAND:-}
660
661 case $command in
662 '')
663 # Do nothing if it's not specified
664 return
665 ;;
666
667 detect)
668 # all the commands supported, except 'detect'
669 echo list-tests
670 echo run-test
671
672 exit 66 # ASCII code for 'B' - what the protocol specifies
673 ;;
674
675 list-tests)
676 # bash extension that OSH also implements
677 compgen -A function | grep '^test-'
678 exit 0
679 ;;
680
681 run-test)
682 local test_name=${BYO_ARG:-}
683 if test -z "$test_name"; then
684 die "BYO run-test: Expected BYO_ARG"
685 fi
686
687 # Shell convention: we name functions test-*
688 $test_name
689
690 # Only run if not set -e. Either way it's equivalent
691 exit $?
692 ;;
693
694 *)
695 die "Invalid BYO command '$command'"
696 ;;
697 esac
698
699 # Do nothing if BYO_COMMAND is not set.
700 # The program continues to its "main".
701}
702
703byo-must-run() {
704 local command=${BYO_COMMAND:-}
705 if test -z "$command"; then
706 die "Expected BYO_COMMAND= in environment"
707 fi
708
709 byo-maybe-run
710}
711)zZXx");
712
713GLOBAL_STR(gStr37, R"zZXx(#!/usr/bin/env bash
714#
715# Testing library for bash and OSH.
716#
717# Capture status/stdout/stderr, and nq-assert those values.
718
719: ${LIB_OSH=stdlib/osh}
720source $LIB_OSH/two.sh
721
722nq-assert() {
723 ### Assertion with same syntax as shell 'test'
724
725 if ! test "$@"; then
726 die "line ${BASH_LINENO[0]}: nq-assert $(printf '%q ' "$@") failed"
727 fi
728}
729
730# Problem: we want to capture status and stdout at the same time
731#
732# We use:
733#
734# __stdout=$(set -o errexit; "$@")
735# __status=$?
736#
737# However, we lose the trailing \n, since that's how command subs work.
738
739# Here is another possibility:
740#
741# shopt -s lastpipe # need this too
742# ( set -o errexit; "$@" ) | read -r -d __stdout
743# __status=${PIPESTATUS[0]}
744# shopt -u lastpipe
745#
746# But this feels complex for just the \n issue, which can be easily worked
747# around.
748
749nq-run() {
750 ### capture status only
751
752 local -n out_status=$1
753 shift
754
755 local __status
756
757 # Tricky: turn errexit off so we can capture it, but turn it on against
758 set +o errexit
759 ( set -o errexit; "$@" )
760 __status=$?
761 set -o errexit
762
763 out_status=$__status
764}
765
766nq-capture() {
767 ### capture status and stdout
768
769 local -n out_status=$1
770 local -n out_stdout=$2
771 shift 2
772
773 local __status
774 local __stdout
775
776 # Tricky: turn errexit off so we can capture it, but turn it on against
777 set +o errexit
778 __stdout=$(set -o errexit; "$@")
779 __status=$?
780 set -o errexit
781
782 out_status=$__status
783 out_stdout=$__stdout
784}
785
786nq-capture-2() {
787 ### capture status and stderr
788
789 # This is almost identical to the above
790
791 local -n out_status=$1
792 local -n out_stderr=$2
793 shift 2
794
795 local __status
796 local __stderr
797
798 # Tricky: turn errexit off so we can capture it, but turn it on against
799 set +o errexit
800 __stderr=$(set -o errexit; "$@" 2>&1)
801 __status=$?
802 set -o errexit
803
804 out_status=$__status
805 out_stderr=$__stderr
806}
807
808# 'byo test' can set this?
809: ${NQ_TEST_TEMP=/tmp}
810
811nq-redir() {
812 ### capture status and stdout
813
814 local -n out_status=$1
815 local -n out_stdout_file=$2
816 shift 2
817
818 local __status
819 local __stdout_file=$NQ_TEST_TEMP/nq-redir-$$.txt
820
821 # Tricky: turn errexit off so we can capture it, but turn it on against
822 set +o errexit
823 ( set -o errexit; "$@" ) > $__stdout_file
824 __status=$?
825 set -o errexit
826
827 out_status=$__status
828 out_stdout_file=$__stdout_file
829}
830
831nq-redir-2() {
832 ### capture status and stdout
833
834 local -n out_status=$1
835 local -n out_stderr_file=$2
836 shift 2
837
838 local __status
839 local __stderr_file=$NQ_TEST_TEMP/nq-redir-$$.txt
840
841 # Tricky: turn errexit off so we can capture it, but turn it on against
842 set +o errexit
843 ( set -o errexit; "$@" ) 2> $__stderr_file
844 __status=$?
845 set -o errexit
846
847 out_status=$__status
848 out_stderr_file=$__stderr_file
849}
850)zZXx");
851
852GLOBAL_STR(gStr38, R"zZXx(#!/usr/bin/env bash
853#
854# Common shell functions for task scripts.
855#
856# Usage:
857# source $LIB_OSH/task-five.sh
858#
859# test-foo() { # define task functions
860# echo foo
861# }
862# task-five "$@"
863
864# Definition of a "task"
865#
866# - File invokes task-five "$@"
867# - or maybe you can look at its source
868# - It's a shell function
869# - Has ### docstring
870# - Doesn't start with _
871
872: ${LIB_OSH=stdlib/osh}
873source $LIB_OSH/byo-server.sh
874
875
876# List all functions defined in this file (and not in sourced files).
877_bash-print-funcs() {
878 ### Print shell functions in this file that don't start with _ (bash reflection)
879
880 local funcs
881 funcs=($(compgen -A function))
882 # extdebug makes `declare -F` print the file path, but, annoyingly, only
883 # if you pass the function names as arguments.
884 shopt -s extdebug
885 declare -F "${funcs[@]}" | grep --fixed-strings " $0" | awk '{print $1}'
886 shopt -u extdebug
887}
888
889_gawk-print-funcs() {
890 ### Print shell functions in this file that don't start with _ (awk parsing)
891
892 # Using gawk because it has match()
893 # - doesn't start with _
894
895 # space = / ' '* /
896 # shfunc = / %begin
897 # <capture !['_' ' '] ![' ']*>
898 # '()' space '{' space
899 # %end /
900 # docstring = / %begin
901 # space '###' ' '+
902 # <capture dot*>
903 # %end /
904 gawk '
905 match($0, /^([^_ ][^ ]*)\(\)[ ]*{[ ]*$/, m) {
906 #print NR " shfunc " m[1]
907 print m[1]
908 #print m[0]
909 }
910
911 match($0, /^[ ]*###[ ]+(.*)$/, m) {
912 print NR " docstring " m[1]
913 }
914' $0
915}
916
917_print-funcs() {
918 if command -v gawk > /dev/null; then
919 _gawk-print-funcs
920 else
921 _bash-print-funcs
922 fi
923}
924
925_show-help() {
926 # TODO:
927 # - Use awk to find comments at the top of the file?
928 # - Use OSH to extract docstrings
929 # - BYO_COMMAND=list-tasks will reuse that logic? It only applies to the
930 # current file, not anything in a different file?
931
932 echo "Usage: $0 TASK_NAME ARGS..."
933 echo
934 echo "To complete tasks, run:"
935 echo " source devtools/completion.bash"
936 echo
937 echo "Tasks:"
938
939 if command -v column >/dev/null; then
940 _print-funcs | column
941 else
942 _print-funcs
943 fi
944}
945
946task-five() {
947 # Respond to BYO_COMMAND=list-tasks, etc. All task files need this.
948 byo-maybe-run
949
950 case ${1:-} in
951 ''|--help|-h)
952 _show-help
953 exit 0
954 ;;
955 esac
956
957 if ! declare -f "$1" >/dev/null; then
958 echo "$0: '$1' isn't an action in this task file. Try '$0 --help'"
959 exit 1
960 fi
961
962 "$@"
963}
964)zZXx");
965
966GLOBAL_STR(gStr39, R"zZXx(# Two functions I actually use, all the time.
967#
968# To keep depenedencies small, this library will NEVER grow other functions
969# (and is named to imply that.)
970#
971# Usage:
972# source --builtin two.sh
973#
974# Examples:
975# log 'hi'
976# die 'expected a number'
977
978if command -v source-guard >/dev/null; then # include guard for YSH
979 source-guard two || return 0
980fi
981
982log() {
983 ### Write a message to stderr.
984 echo "$@" >&2
985}
986
987die() {
988 ### Write an error message with the script name, and exit with status 1.
989 log "$0: fatal: $@"
990 exit 1
991}
992
993)zZXx");
994
995GLOBAL_STR(gStr40, R"zZXx(# These were helpful while implementing args.ysh
996# Maybe we will want to export them in a prelude so that others can use them too?
997#
998# Prior art: Rust has `todo!()` which is quite nice. Other languages allow
999# users to `raise NotImplmentedError()`.
1000
1001# Andy comments:
1002# - 'pass' can be : or true in shell. It's a little obscure / confusing, but
1003# there is an argument for minimalism. Although I prefer words like 'true',
1004# and that already means something.
1005# - UPDATE: we once took 'pass' as a keyword, but users complained because
1006# there is a command 'pass'. So we probably can't have this by default.
1007# Need to discuss source --builtin.
1008
1009# - todo could be more static? Rust presumably does it at compile time
1010
1011proc todo () {
1012 ## Raises a not implemented error when run.
1013 error ("TODO: not implemented") # TODO: is error code 1 ok?
1014}
1015
1016proc pass () {
1017 ## Use when you want to temporarily leave a block empty.
1018 _ null
1019}
1020)zZXx");
1021
1022GLOBAL_STR(gStr41, R"zZXx(# testing.ysh
1023#
1024# Usage:
1025# source --builtin testing.sh
1026#
1027# func f(x) { return (x + 1) }
1028#
1029# describe foo {
1030# assert (43 === f(42))
1031# }
1032#
1033# if is-main {
1034# run-tests @ARGV # --filter
1035# }
1036
1037module stdlib/testing || return 0
1038
1039source --builtin args.ysh
1040
1041proc assert ( ; cond ; fail_message='default fail message') {
1042 echo 'hi from assert'
1043
1044 = cond
1045
1046 # I think this might be ready now?
1047
1048 var val = evalExpr(cond)
1049
1050 echo
1051 echo 'value'
1052 = val
1053 pp line (val)
1054
1055 = fail_message
1056
1057 if (val) {
1058 echo 'OK'
1059 } else {
1060 var m = evalExpr(fail_message)
1061 echo "FAIL - this is where we extract the string - $m"
1062 }
1063}
1064
1065proc test-assert {
1066 var x = 42
1067 assert [42 === x]
1068}
1069
1070proc test-expr ( ; expr ) {
1071 echo 'expr'
1072 pp line (expr)
1073}
1074
1075proc test-named ( ; ; n=^[99] ) {
1076 echo 'n'
1077 pp line (n)
1078}
1079
1080# What happens when there are duplicate test IDs?
1081#
1082# Also I think filter by "$test_id/$case_id"
1083
1084proc __it (case_id ; ; ; block) {
1085 # This uses a clean directory
1086 echo TODO
1087}
1088
1089# is this accessible to users?
1090# It can contain a global list of things to run
1091
1092# Naming convention: a proc named 'describe' mutates a global named _describe?
1093# Or maybe _describe_list ?
1094
1095var _describe_list = []
1096
1097proc describe (test_id ; ; ; block) {
1098 echo describe
1099 #= desc
1100
1101 # TODO:
1102 # - need append
1103 # - need ::
1104 # _ _describe->append(cmd)
1105 #
1106 # Need to clean this up
1107 # append (_describe, cmd) # does NOT work!
1108
1109 call _describe_list->append(block)
1110}
1111
1112proc Args {
1113 echo TODO
1114}
1115
1116# Problem: this creates a global variable?
1117Args (&spec) {
1118 flag --filter 'Regex of test descriptions'
1119}
1120
1121proc run-tests {
1122 var opt, i = parseArgs(spec, ARGV)
1123
1124 # TODO:
1125 # - parse --filter foo, which you can use eggex for!
1126
1127 for cmd in (_describe) {
1128 # TODO: print filename and 'describe' name?
1129 try {
1130 eval (cmd)
1131 }
1132 if (_status !== 0) {
1133 echo 'failed'
1134 }
1135 }
1136}
1137)zZXx");
1138
1139GLOBAL_STR(gStr42, R"zZXx(# args.ysh
1140#
1141# Usage:
1142# source --builtin args.sh
1143#
1144# parser (&spec) {
1145# flag -v --verbose (help="Verbosely") # default is Bool, false
1146#
1147# flag -P --max-procs ('int', default=-1, doc='''
1148# Run at most P processes at a time
1149# ''')
1150#
1151# flag -i --invert ('bool', default=true, doc='''
1152# Long multiline
1153# Description
1154# ''')
1155#
1156# arg src (help='Source')
1157# arg dest (help='Dest')
1158# arg times (help='Foo')
1159#
1160# rest files
1161# }
1162#
1163# var args = parseArgs(spec, ARGV)
1164#
1165# echo "Verbose $[args.verbose]"
1166
1167# TODO: See list
1168# - It would be nice to keep `flag` and `arg` private, injecting them into the
1169# proc namespace only within `Args`
1170# - We need "type object" to replace the strings 'int', 'bool', etc.
1171# - flag builtin:
1172# - handle only long flag or only short flag
1173# - flag aliases
1174
1175proc parser (; place ; ; block_def) {
1176 ## Create an args spec which can be passed to parseArgs.
1177 ##
1178 ## Example:
1179 ##
1180 ## # NOTE: &spec will create a variable named spec
1181 ## parser (&spec) {
1182 ## flag -v --verbose ('bool')
1183 ## }
1184 ##
1185 ## var args = parseArgs(spec, ARGV)
1186
1187 var p = {flags: [], args: []}
1188 ctx push (p; ; block_def)
1189
1190 # Validate that p.rest = [name] or null and reduce p.rest into name or null.
1191 if ('rest' in p) {
1192 if (len(p.rest) > 1) {
1193 error '`rest` was called more than once' (code=3)
1194 } else {
1195 setvar p.rest = p.rest[0]
1196 }
1197 } else {
1198 setvar p.rest = null
1199 }
1200
1201 var names = {}
1202 for items in ([p.flags, p.args]) {
1203 for x in (items) {
1204 if (x.name in names) {
1205 error "Duplicate flag/arg name $[x.name] in spec" (code=3)
1206 }
1207
1208 setvar names[x.name] = null
1209 }
1210 }
1211
1212 # TODO: what about `flag --name` and then `arg name`?
1213
1214 call place->setValue(p)
1215}
1216
1217proc flag (short, long ; type='bool' ; default=null, help=null) {
1218 ## Declare a flag within an `arg-parse`.
1219 ##
1220 ## Examples:
1221 ##
1222 ## arg-parse (&spec) {
1223 ## flag -v --verbose
1224 ## flag -n --count ('int', default=1)
1225 ## flag -f --file ('str', help="File to process")
1226 ## }
1227
1228 # bool has a default of false, not null
1229 if (type === 'bool' and default === null) {
1230 setvar default = false
1231 }
1232
1233 # TODO: validate `type`
1234
1235 # TODO: Should use "trimPrefix"
1236 var name = long[2:]
1237
1238 ctx emit flags ({short, long, name, type, default, help})
1239}
1240
1241proc arg (name ; ; help=null) {
1242 ## Declare a positional argument within an `arg-parse`.
1243 ##
1244 ## Examples:
1245 ##
1246 ## arg-parse (&spec) {
1247 ## arg name
1248 ## arg config (help="config file path")
1249 ## }
1250
1251 ctx emit args ({name, help})
1252}
1253
1254proc rest (name) {
1255 ## Take the remaining positional arguments within an `arg-parse`.
1256 ##
1257 ## Examples:
1258 ##
1259 ## arg-parse (&grepSpec) {
1260 ## arg query
1261 ## rest files
1262 ## }
1263
1264 # We emit instead of set to detect multiple invocations of "rest"
1265 ctx emit rest (name)
1266}
1267
1268func parseArgs(spec, argv) {
1269 ## Given a spec created by `parser`. Parse an array of strings `argv` per
1270 ## that spec.
1271 ##
1272 ## See `parser` for examples of use.
1273
1274 var i = 0
1275 var positionalPos = 0
1276 var argc = len(argv)
1277 var args = {}
1278 var rest = []
1279
1280 var value
1281 var found
1282 while (i < argc) {
1283 var arg = argv[i]
1284 if (arg->startsWith('-')) {
1285 setvar found = false
1286
1287 for flag in (spec.flags) {
1288 if ( (flag.short and flag.short === arg) or
1289 (flag.long and flag.long === arg) ) {
1290 case (flag.type) {
1291 ('bool') | (null) { setvar value = true }
1292 int {
1293 setvar i += 1
1294 if (i >= len(argv)) {
1295 error "Expected integer after '$arg'" (code=2)
1296 }
1297
1298 try { setvar value = int(argv[i]) }
1299 if (_status !== 0) {
1300 error "Expected integer after '$arg', got '$[argv[i]]'" (code=2)
1301 }
1302 }
1303 }
1304
1305 setvar args[flag.name] = value
1306 setvar found = true
1307 break
1308 }
1309 }
1310
1311 if (not found) {
1312 error "Unknown flag '$arg'" (code=2)
1313 }
1314 } elif (positionalPos >= len(spec.args)) {
1315 if (not spec.rest) {
1316 error "Too many arguments, unexpected '$arg'" (code=2)
1317 }
1318
1319 call rest->append(arg)
1320 } else {
1321 var pos = spec.args[positionalPos]
1322 setvar positionalPos += 1
1323 setvar value = arg
1324 setvar args[pos.name] = value
1325 }
1326
1327 setvar i += 1
1328 }
1329
1330 if (spec.rest) {
1331 setvar args[spec.rest] = rest
1332 }
1333
1334 # Set defaults for flags
1335 for flag in (spec.flags) {
1336 if (flag.name not in args) {
1337 setvar args[flag.name] = flag.default
1338 }
1339 }
1340
1341 # Raise error on missing args
1342 for arg in (spec.args) {
1343 if (arg.name not in args) {
1344 error "Usage Error: Missing required argument $[arg.name]" (code=2)
1345 }
1346 }
1347
1348 return (args)
1349}
1350)zZXx");
1351
1352GLOBAL_STR(gStr43, R"zZXx(func any(list) {
1353 ### Returns true if any value in the list is truthy.
1354 # Empty list: returns false
1355
1356 for item in (list) {
1357 if (item) {
1358 return (true)
1359 }
1360 }
1361 return (false)
1362}
1363
1364func all(list) {
1365 ## Returns true if all values in the list are truthy.
1366 # Empty list: returns true
1367
1368 for item in (list) {
1369 if (not item) {
1370 return (false)
1371 }
1372 }
1373 return (true)
1374}
1375
1376func sum(list; start=0) {
1377 ### Returns the sum of all elements in the list.
1378 # Empty list: returns 0
1379
1380 var sum = start
1381 for item in (list) {
1382 setvar sum += item
1383 }
1384 return (sum)
1385}
1386
1387func repeat(x, n) {
1388 ### Returns a list with the given string or list repeated
1389
1390 # Like Python's 'foo'*3 or ['foo', 'bar']*3
1391 # negative numbers are like 0 in Python
1392
1393 var t = type(x)
1394 case (t) {
1395 Str {
1396 var parts = []
1397 for i in (0 .. n) {
1398 call parts->append(x)
1399 }
1400 return (join(parts))
1401 }
1402 List {
1403 var result = []
1404 for i in (0 .. n) {
1405 call result->extend(x)
1406 }
1407 return (result)
1408 }
1409 (else) {
1410 error "Expected Str or List, got $t"
1411 }
1412 }
1413}
1414)zZXx");
1415
1416GLOBAL_STR(gStr44, R"zZXx(func __math_select(list, cmp) {
1417 ## Internal helper for `max` and `min`.
1418 ##
1419 ## NOTE: If `list` is empty, then an error is thrown.
1420
1421 if (len(list) === 0) {
1422 error "Unexpected empty list" (code=3)
1423 }
1424
1425 if (len(list) === 1) {
1426 return (list[0])
1427 }
1428
1429 var match = list[0]
1430 for i in (1 .. len(list)) {
1431 setvar match = cmp(list[i], match)
1432 }
1433 return (match)
1434}
1435
1436func max(...args) {
1437 ## Compute the maximum of 2 or more values.
1438 ##
1439 ## `max` takes two different signatures:
1440 ## - `max(a, b)` to return the maximum of `a`, `b`
1441 ## - `max(list)` to return the greatest item in the `list`
1442 ##
1443 ## So, for example:
1444 ##
1445 ## max(1, 2) # => 2
1446 ## max([1, 2, 3]) # => 3
1447
1448 case (len(args)) {
1449 (1) { return (__math_select(args[0], max)) }
1450 (2) {
1451 if (args[0] > args[1]) {
1452 return (args[0])
1453 } else {
1454 return (args[1])
1455 }
1456 }
1457 (else) { error "max expects 1 or 2 args" (code=3) }
1458 }
1459}
1460
1461func min(...args) {
1462 ## Compute the minimum of 2 or more values.
1463 ##
1464 ## `min` takes two different signatures:
1465 ## - `min(a, b)` to return the minimum of `a`, `b`
1466 ## - `min(list)` to return the least item in the `list`
1467 ##
1468 ## So, for example:
1469 ##
1470 ## min(2, 3) # => 2
1471 ## max([1, 2, 3]) # => 1
1472
1473 case (len(args)) {
1474 (1) { return (__math_select(args[0], min)) }
1475 (2) {
1476 if (args[0] < args[1]) {
1477 return (args[0])
1478 } else {
1479 return (args[1])
1480 }
1481 }
1482 (else) { error "min expects 1 or 2 args" (code=3) }
1483 }
1484}
1485
1486func abs(x) {
1487 ## Compute the absolute (positive) value of a number (float or int).
1488
1489 if (x < 0) {
1490 return (-x)
1491 } else {
1492 return (x)
1493 }
1494}
1495)zZXx");
1496
1497GLOBAL_STR(gStr45, R"zZXx(# stream.ysh
1498#
1499# Usage:
1500# source --builtin stream.ysh
1501#
1502# For reading lines, decoding, extracting, splitting
1503
1504# make this file a test server
1505source $LIB_OSH/byo-server.sh
1506
1507source $LIB_YSH/args.ysh
1508
1509proc slurp-by (; num_lines) {
1510 # TODO: (stdin)
1511 for line in (stdin) {
1512 echo TODO
1513 }
1514}
1515
1516# Note:
1517# - these are all the same algorithm
1518# - also word, block, etc. are all optional
1519
1520proc each-line (...words; template=null; ; block=null) {
1521 # TODO:
1522 # parse --j8 --max-jobs flag
1523
1524 # parse template_str as string
1525 # TODO: this is dangerous though ... because you can execute code
1526 # I think you need a SAFE version
1527
1528 # evaluate template string expression - I guess that allows $(echo hi) and so
1529 # forth
1530
1531 # evaluate block with _line binding
1532 # block: execute in parallel with --max-jobs
1533
1534 for line in (stdin) {
1535 echo TODO
1536 }
1537}
1538
1539proc test-each-line {
1540 echo 'TODO: need basic test runner'
1541
1542 # ysh-tool test stream.ysh
1543 #
1544 # Col
1545
1546
1547}
1548
1549proc each-row (; ; block) {
1550 echo TODO
1551}
1552
1553proc split-by (; ifs=null; block) {
1554 echo TODO
1555}
1556
1557proc if-split-by (; ifs=null; block) {
1558 echo TODO
1559}
1560
1561proc chop () {
1562 ### alias for if-split-by
1563 echo TODO
1564}
1565
1566proc must-match (; pattern; block) {
1567 echo TODO
1568}
1569
1570proc if-match (; pattern; block) {
1571 echo TODO
1572}
1573
1574# Protocol:
1575#
1576# - The file lists its tests the "actions"
1577# - Then the test harness runs them
1578# - But should it be ENV vars
1579#
1580# - BYO_LIST_TESTS=1
1581# - BYO_RUN_TEST=foo
1582# - $PWD is a CLEAN temp dir, the process doesn't have to do anything
1583
1584# - silent on success, but prints file on output
1585# - OK this makes sense
1586#
1587# The trivial test in Python:
1588#
1589# from test import byo
1590# byo.maybe_main()
1591#
1592# bash library:
1593# source --builtin byo-server.sh
1594#
1595# byo-maybe-main # reads env variables, and then exits
1596#
1597# source --builtin assertions.ysh
1598#
1599# assert-ok 'echo hi'
1600# assert-stdout 'hi' 'echo -n hi'
1601#
1602# "$@"
1603#
1604# Run all tests
1605# util/byo-client.sh run-tests $YSH stdlib/table.ysh
1606# util/byo-client.sh run-tests -f x $YSH stdlib/table.ysh
1607
1608# Clean process
1609# Clean working dir
1610
1611#
1612# Stream Protocol:
1613# #.byo - is this she-dot, that's for a file
1614# Do we need metadata?
1615#
1616
1617# The harness
1618#
1619# It's process based testing.
1620#
1621# Test runner process: bash or OSH (unlike sharness!)
1622# Tested process: any language - bash,
1623#
1624# Key point: you don't have to quote shell code?
1625
1626list-byo-tests() {
1627 echo TODO
1628}
1629
1630run-byo-tests() {
1631 # source it
1632 echo TODO
1633}
1634
1635byo-maybe-run
1636)zZXx");
1637
1638GLOBAL_STR(gStr46, R"zZXx(# table.ysh - Library for tables.
1639#
1640# Usage:
1641# source --builtin table.ysh
1642
1643# make this file a test server
1644source --builtin osh/byo-server.sh
1645
1646proc table (...words; place; ; block) {
1647 var n = len(words)
1648
1649 # TODO: parse flags
1650 #
1651 # --by-row
1652 # --by-col
1653 #
1654 # Place is optional
1655
1656 if (n === 0) {
1657 echo TODO
1658 return
1659 }
1660
1661 var action = words[0]
1662
1663 # textual operations
1664 case (action) {
1665 cat {
1666 echo todo
1667 }
1668 align {
1669 echo todo
1670 }
1671 tabify {
1672 echo todo
1673 }
1674 tabify {
1675 echo todo
1676 }
1677 header {
1678 echo todo
1679 }
1680 slice {
1681 # this has typed args
1682 # do we do some sort of splat?
1683 echo todo
1684 }
1685 to-tsv {
1686 echo todo
1687 }
1688 }
1689
1690 echo TODO
1691}
1692
1693proc test-table {
1694 return
1695
1696 table (&files1) {
1697 cols num_bytes path
1698 type Int Str
1699
1700 row 10 README.md
1701 row 12 main.py
1702
1703 row (12, 'lib.py')
1704 row (num_bytes=12, path='util.py')
1705 }
1706
1707 # 2 columns - The default is by column?
1708 assert ['Dict' === type(files1)]
1709 assert [2 === len(files1)]
1710
1711 # Same table
1712 table --by-row (&files2) {
1713 cols num_bytes path
1714 type Int Str
1715
1716 row 10 README.md
1717 row 12 main.py
1718
1719 row (12, 'lib.py')
1720 row (num_bytes=12, path='util.py')
1721 }
1722
1723 # 4 rows
1724 assert ['List' === type(files2)]
1725 assert [4 === len(files2)]
1726}
1727
1728# "subcommands" of the dialect
1729
1730proc cols (...names) {
1731 # cols name age
1732 echo TODO
1733}
1734
1735proc types (...types) {
1736 # types - Int? Str?
1737 echo TODO
1738}
1739
1740proc attr (name; ...values) {
1741 # attr units ('-', 'secs')
1742 echo TODO
1743}
1744
1745# is this allowed outside table {} blocks too?
1746proc row {
1747 echo TODO
1748}
1749
1750#
1751# dplyr names
1752#
1753
1754# TODO: can we parse select?
1755
1756proc where {
1757 echo
1758}
1759
1760# TODO: should be able to test argv[0] or something
1761# Or pass to _mutate-transmute
1762
1763proc mutate {
1764 echo TODO
1765}
1766
1767proc transmute {
1768 echo TODO
1769}
1770
1771proc rename {
1772 echo TODO
1773}
1774
1775proc group-by {
1776 echo TODO
1777}
1778
1779proc sort-by {
1780 echo TODO
1781}
1782
1783proc summary {
1784 echo TODO
1785}
1786
1787byo-maybe-run
1788)zZXx");
1789
1790GLOBAL_STR(gStr47, R"zZXx(#!/usr/bin/env bash
1791#
1792# Testing library for bash and OSH.
1793#
1794# Capture status/stdout/stderr, and nq-assert those values.
1795
1796#module yblocks || return 0
1797
1798: ${LIB_OSH=stdlib/osh}
1799source $LIB_OSH/two.sh
1800
1801# There is no yb-run, because you can just use try { } and inspect _error.code
1802# There is no yb-redir, because you can just use try >$tmp { } and inspect _error.code
1803
1804proc yb-capture(; out; ; block) {
1805 ### capture status and stderr
1806
1807 var stdout = ''
1808 try {
1809 eval (block) | read --all (&stdout)
1810 }
1811 # TODO: if 'block' contains a pipeline, we lose this magic var
1812 var result = {status: _pipeline_status[0], stdout}
1813
1814 #echo 'result-1'
1815 #pp line (result)
1816
1817 call out->setValue(result)
1818}
1819
1820proc yb-capture-2(; out; ; block) {
1821 ### capture status and stderr
1822
1823 var stderr = ''
1824 try {
1825 eval (block) 2>&1 | read --all (&stderr)
1826 }
1827 #pp line (_pipeline_status)
1828
1829 var result = {status: _pipeline_status[0], stderr}
1830 #echo 'result-2'
1831 #pp line (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/funcs.ysh", .contents = gStr33},
1874 {.rel_path = "stdlib/methods.ysh", .contents = gStr34},
1875 {.rel_path = "stdlib/osh/bash-strict.sh", .contents = gStr35},
1876 {.rel_path = "stdlib/osh/byo-server.sh", .contents = gStr36},
1877 {.rel_path = "stdlib/osh/no-quotes.sh", .contents = gStr37},
1878 {.rel_path = "stdlib/osh/task-five.sh", .contents = gStr38},
1879 {.rel_path = "stdlib/osh/two.sh", .contents = gStr39},
1880 {.rel_path = "stdlib/prelude.ysh", .contents = gStr40},
1881 {.rel_path = "stdlib/testing.ysh", .contents = gStr41},
1882 {.rel_path = "stdlib/ysh/args.ysh", .contents = gStr42},
1883 {.rel_path = "stdlib/ysh/list.ysh", .contents = gStr43},
1884 {.rel_path = "stdlib/ysh/math.ysh", .contents = gStr44},
1885 {.rel_path = "stdlib/ysh/stream.ysh", .contents = gStr45},
1886 {.rel_path = "stdlib/ysh/table.ysh", .contents = gStr46},
1887 {.rel_path = "stdlib/ysh/yblocks.ysh", .contents = gStr47},
1888
1889 {.rel_path = nullptr, .contents = nullptr},
1890};
1891
1892} // namespace embedded_file
1893
1894TextFile* gEmbeddedFiles = embedded_file::array; // turn array into pointer