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

1188 lines, 134 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 [Debugging] xtrace X verbose X extdebug
174 [Interactive] emacs vi
175 [Other POSIX] X noclobber
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 asdl cell X gc-stats line proc
296 [Handle Errors] error error 'failed' (status=2)
297 try Run with errexit, set _error
298 failed Test if _error.code !== 0
299 boolstatus Enforce 0 or 1 exit status
300 [Shell State] ysh-cd ysh-shopt compatible, and takes a block
301 shvar Temporary modify global settings
302 ctx Share and update a temporary "context"
303 push-registers Save registers like $?, PIPESTATUS
304 [Modules] runproc Run a proc; use as main entry point
305 module guard against duplicate 'source'
306 is-main false when sourcing a file
307 use change first word lookup
308 [I/O] ysh-read flags --all, -0
309 ysh-echo no -e -n with simple_echo
310 write Like echo, with --, --sep, --end
311 fork forkwait Replace & and (), and takes a block
312 fopen Open multiple streams, takes a block
313 X dbg Only thing that can be used in funcs
314 [Hay Config] hay haynode For DSLs and config files
315 [Completion] compadjust compexport
316 [Data Formats] json read write
317 json8 read write
318X [Testing] assert takes an expression
319)zZXx");
320
321GLOBAL_STR(gStr19, R"zZXx(
322 Builtin Functions <a class="group-link" href="chap-builtin-func.html">builtin-func</a>
323
324
325 [Values] len() func/type()
326 [Conversions] bool() int() float()
327 str() list() dict()
328 X runes() X encodeRunes()
329 X bytes() X encodeBytes()
330 [Str] X strcmp() X split() shSplit()
331 [List] join()
332 [Float] floatsEqual() X isinf() X isnan()
333 [Collections] X copy() X deepCopy()
334 [Word] glob() maybe()
335 [Serialize] toJson() fromJson()
336 toJson8() fromJson8()
337X [J8 Decode] J8.Bool() J8.Int() ...
338 [Pattern] _group() _start() _end()
339 [Introspection] shvarGet() getVar() evalExpr()
340 [Hay Config] parseHay() evalHay()
341X [Hashing] sha1dc() sha256()
342)zZXx");
343
344GLOBAL_STR(gStr20, R"zZXx(The reference is divided in to "chapters", each of which has its own table of
345contents. Type:
346
347 help ysh-$CHAPTER
348
349Where $CHAPTER is one of:
350
351 front-end
352 command-lang
353 expr-lang
354 word-lang
355 builtin-cmd
356 option
357 special-var
358 type-method
359 builtin-func
360
361Example:
362
363 help ysh-expr-lang
364)zZXx");
365
366GLOBAL_STR(gStr21, R"zZXx(
367 Command Language <a class="group-link" href="chap-cmd-lang.html">cmd-lang</a>
368
369
370 [YSH Simple] typed-arg json write (x)
371 lazy-expr-arg assert [42 === x]
372 block-arg cd /tmp { echo $PWD }; cd /tmp (; ; blockexpr)
373 [YSH Cond] ysh-case case (x) { *.py { echo 'python' } }
374 ysh-if if (x > 0) { echo }
375 [YSH Iter] ysh-while while (x > 0) { echo }
376 ysh-for for i, item in (mylist) { echo }
377)zZXx");
378
379GLOBAL_STR(gStr22, R"zZXx(
380 Expression Language and Assignments <a class="group-link" href="chap-expr-lang.html">expr-lang</a>
381
382
383 [Assignment] assign =
384 aug-assign += -= *= /= **= //= %=
385 &= |= ^= <<= >>=
386 [Literals] atom-literal true false null
387 int-literal 42 65_536 0xFF 0o755 0b10
388 float-lit 3.14 1.5e-10
389 X num-suffix 42 K Ki M Mi G Gi T Ti / ms us
390 ysh-string "x is $x" $"x is $x" r'[a-z]\n'
391 u'line\n' b'byte \yff'
392 triple-quoted """ $""" r''' u''' b'''
393 str-template ^"$a and $b" for Str::replace()
394 list-literal ['one', 'two', 3] :| unquoted words |
395 dict-literal {name: 'bob'} {a, b}
396 range 1 .. n+1
397 block-expr ^(echo $PWD)
398 expr-literal ^[1 + 2*3]
399 X expr-sub $[myobj]
400 X expr-splice @[myobj]
401 [Operators] op-precedence Like Python
402 concat s1 ++ s2, L1 ++ L2
403 ysh-equals === !== ~== is, is not
404 ysh-in in, not in
405 ysh-compare < <= > >= (numbers only)
406 ysh-logical not and or
407 ysh-arith + - * / // % **
408 ysh-bitwise ~ & | ^ << >>
409 ysh-ternary '+' if x >= 0 else '-'
410 ysh-index s[0] mylist[3] mydict['key']
411 ysh-attr mydict.key
412 ysh-slice a[1:-1] s[1:-1]
413 func-call f(x, y; ...named)
414 thin-arrow mylist->pop()
415 fat-arrow mystr => startsWith('prefix')
416 match-ops ~ !~ ~~ !~~
417 [Eggex] re-literal / d+ ; re-flags ; ERE /
418 re-primitive %zero 'sq'
419 class-literal [c a-z 'abc' @str_var \\ \xFF \u0100]
420 named-class dot digit space word d s w
421 re-repeat d? d* d+ d{3} d{2,4}
422 re-compound seq1 seq2 alt1|alt2 (expr1 expr2)
423 re-capture <capture d+ as name: int>
424 re-splice Subpattern @subpattern
425 re-flags reg_icase reg_newline
426 X re-multiline ///
427)zZXx");
428
429GLOBAL_STR(gStr23, R"zZXx(
430 Front End <a class="group-link" href="chap-front-end.html">front-end</a>
431
432
433 [Usage] oils-usage ysh-usage
434 [Lexing] ascii-whitespace [ \t\r\n]
435 doc-comment ### multiline-command ...
436 [Tools] cat-em
437)zZXx");
438
439GLOBAL_STR(gStr24, R"zZXx(
440 Other Mini Languages <a class="group-link" href="chap-mini-lang.html">mini-lang</a>
441
442
443 [Patterns] glob-pat *.py
444 [Other Sublang] braces {alice,bob}@example.com
445)zZXx");
446
447GLOBAL_STR(gStr25, R"zZXx(
448 Global Shell Options <a class="group-link" href="chap-option.html">option</a>
449
450
451 [Groups] strict:all ysh:upgrade ysh:all
452 [YSH Details] opts-redefine opts-internal
453)zZXx");
454
455GLOBAL_STR(gStr26, R"zZXx(
456 Plugins and Hooks <a class="group-link" href="chap-plugin.html">plugin</a>
457
458
459 [YSH] renderPrompt()
460)zZXx");
461
462GLOBAL_STR(gStr27, R"zZXx(
463 Special Variables <a class="group-link" href="chap-special-var.html">special-var</a>
464
465
466 [YSH Vars] ARGV X ENV X _ESCAPE
467 _this_dir
468 [YSH Status] _error
469 _pipeline_status _process_sub_status
470 [YSH Tracing] SHX_indent SHX_punct SHX_pid_str
471 [YSH read] _reply
472 [History] YSH_HISTFILE
473 [Oils VM] OILS_VERSION
474 OILS_GC_THRESHOLD OILS_GC_ON_EXIT
475 OILS_GC_STATS OILS_GC_STATS_FD
476 LIB_YSH
477 [Float] NAN INFINITY
478)zZXx");
479
480GLOBAL_STR(gStr28, R"zZXx(
481 Standard Library<a class="group-link" href="chap-stdlib.html">stdlib</a>
482
483
484 [math] abs()
485 max() min()
486 X round()
487 sum()
488 [list] all() any()
489 repeat()
490 [args] parser Parse command line arguments
491 flag
492 arg
493 rest
494 parseArgs()
495 [yblocks] yb-capture
496 yb-capture-2
497X [Lines] slurp-by combine adjacent lines into cells
498X [Awk] each-line --j8 --max-jobs (Str, Template, Block) - xargs
499 each-row --max-jobs (Str, Template, Block) - xargs
500 each-word xargs-like splitting, similar to IFS too
501 split-by (str=\n, ifs=':', pattern=/s+/)
502 if-split-by
503 chop alias for split-by (pattern=/s+/)
504 must-match (/ <capture d+> </capture w+> /)
505 if-match
506X [Table Create] table --by-row --by-col (&place); construct/parse a table
507 table/cols cols name age - cols name:Str age:Int
508 types type Str Int
509 attr attr units - secs
510 row emit row
511 table cat concatenate TSV8
512 table align to ssv8
513 table tabify to tsv8
514 table header (cols = :|name age|, types = :|Str Int|, units = :|- secs|)
515 table slice e.g. slice (1, -1) slice (5, 7)
516 table to-tsv lose type info, and error on \t in cells
517X [Table Ops] where subset of rows; dplyr filter()
518 pick subset of columns ('select' taken by shell)
519 mutate transmute [average = count / sum] - drop the ones that are used?
520 rename (bytes='bytes', path='filename')
521 group-by add a column with a group ID [ext]
522 sort-by sort by columns; dplyr arrange() [ext]
523 summary count, sum, histogram, any, all, reduce(), etc. [ext]
524)zZXx");
525
526GLOBAL_STR(gStr29, R"zZXx(
527 Types and Methods <a class="group-link" href="chap-type-method.html">type-method</a>
528
529
530 [Atom Types] Null Bool
531 [Number Types] Int Float
532 [Str] X find() replace()
533 trim() trimStart() trimEnd()
534 startsWith() endsWith()
535 upper() lower()
536 search() leftMatch()
537 [List] List/append() pop() extend() indexOf()
538 X insert() X remove() reverse()
539 [Dict] keys() values() X get() X erase()
540 X inc() X accum()
541 [Range]
542 [Eggex]
543 [Match] group() start() end()
544 X groups() X groupDict()
545 [Place] setValue()
546 [Code Types] Expr Command
547 BuiltinFunc BoundFunc
548X [Func] name() location() toJson()
549X [Proc] name() location() toJson()
550X [Module] name() filename()
551 [IO] X eval() X captureStdout()
552 promptVal()
553 X time() X strftime()
554 X glob()
555X [Guts] heapId()
556)zZXx");
557
558GLOBAL_STR(gStr30, R"zZXx(bin/ysh is the shell with data tYpes, influenced by pYthon, JavaScript, ...
559
560Usage: ysh FLAG* SCRIPT ARG*
561 ysh FLAG* -c COMMAND ARG*
562 ysh FLAG*
563
564`bin/ysh` is the same as `bin/osh` with a the `ysh:all` option group set. So
565`bin/ysh` also accepts shell flags.
566
567 ysh -c 'echo hi'
568 ysh myscript.ysh
569 echo 'echo hi' | ysh
570)zZXx");
571
572GLOBAL_STR(gStr31, R"zZXx(
573 Word Language <a class="group-link" href="chap-word-lang.html">word-lang</a>
574
575
576 [Quotes] ysh-string "x is $x" $"x is $x" r'[a-z]\n'
577 u'line\n' b'byte \yff'
578 triple-quoted """ $""" r''' u''' b'''
579 X tagged-str "<span id=$x>"html
580 [Substitutions] expr-sub echo $[42 + a[i]]
581 expr-splice echo @[split(x)]
582 var-splice @myarray @ARGV
583 command-sub @(split command)
584 [Formatting] X ysh-printf ${x %.3f}
585 X ysh-format ${x|html}
586)zZXx");
587
588GLOBAL_STR(gStr32, R"zZXx(
589 YSH Command Language Keywords <a class="group-link" href="chap-ysh-cmd.html">ysh-cmd</a>
590
591
592 [Assignment] const var Declare variables
593 setvar setvar a[i] = 42
594 setglobal setglobal d.key = 'foo'
595 [Expression] equal = = 1 + 2*3
596 call call mylist->append(42)
597 [Definitions] proc proc p (s, ...rest) {
598 typed proc p (; typed, ...rest; n=0; b) {
599 func func f(x; opt1, opt2) { return (x + 1) }
600 ysh-return return (myexpr)
601)zZXx");
602
603GLOBAL_STR(gStr33, R"zZXx(func identity(x) {
604 ## The identity function. Returns its argument.
605
606 return (x)
607}
608)zZXx");
609
610GLOBAL_STR(gStr34, R"zZXx(# Can we define methods in pure YSH?
611#
612# (mylist->find(42) !== -1)
613#
614# instead of
615#
616# ('42' in mylist)
617#
618# Because 'in' is for Dict
619
620func find (haystack List, needle) {
621 for i, x in (haystack) {
622 if (x === needle) {
623 return (i)
624 }
625 }
626 return (-1)
627}
628)zZXx");
629
630GLOBAL_STR(gStr35, R"zZXx(# Bash strict mode, updated for 2024
631
632set -o nounset
633set -o pipefail
634set -o errexit
635shopt -s inherit_errexit
636shopt -s strict:all 2>/dev/null || true # dogfood for OSH
637
638)zZXx");
639
640GLOBAL_STR(gStr36, R"zZXx(# Library to turn a shell file into a "BYO test server"
641#
642# Usage:
643#
644# # from both bash and OSH
645# if test -z "$LIB_OSH"; then LIB_OSH=stdlib/osh; fi
646# source $LIB_OSH/byo-server-lib.sh
647#
648# The client creates a clean process state and directory state for each tests.
649#
650# (This file requires compgen -A, and maybe declare -f, so it's not POSIX
651# shell.)
652
653: ${LIB_OSH:-stdlib/osh}
654source $LIB_OSH/two.sh
655
656byo-maybe-run() {
657 local command=${BYO_COMMAND:-}
658
659 case $command in
660 '')
661 # Do nothing if it's not specified
662 return
663 ;;
664
665 detect)
666 # all the commands supported, except 'detect'
667 echo list-tests
668 echo run-test
669
670 exit 66 # ASCII code for 'B' - what the protocol specifies
671 ;;
672
673 list-tests)
674 # bash extension that OSH also implements
675 compgen -A function | grep '^test-'
676 exit 0
677 ;;
678
679 run-test)
680 local test_name=${BYO_ARG:-}
681 if test -z "$test_name"; then
682 die "BYO run-test: Expected BYO_ARG"
683 fi
684
685 # Shell convention: we name functions test-*
686 $test_name
687
688 # Only run if not set -e. Either way it's equivalent
689 exit $?
690 ;;
691
692 *)
693 die "Invalid BYO command '$command'"
694 ;;
695 esac
696
697 # Do nothing if BYO_COMMAND is not set.
698 # The program continues to its "main".
699}
700
701byo-must-run() {
702 local command=${BYO_COMMAND:-}
703 if test -z "$command"; then
704 die "Expected BYO_COMMAND= in environment"
705 fi
706
707 byo-maybe-run
708}
709)zZXx");
710
711GLOBAL_STR(gStr37, R"zZXx(#!/usr/bin/env bash
712#
713# Testing library for bash and OSH.
714#
715# Capture status/stdout/stderr, and nq-assert those values.
716
717: ${LIB_OSH=stdlib/osh}
718source $LIB_OSH/two.sh
719
720nq-assert() {
721 ### Assertion with same syntax as shell 'test'
722
723 if ! test "$@"; then
724 die "line ${BASH_LINENO[0]}: nq-assert $(printf '%q ' "$@") failed"
725 fi
726}
727
728# Problem: we want to capture status and stdout at the same time
729#
730# We use:
731#
732# __stdout=$(set -o errexit; "$@")
733# __status=$?
734#
735# However, we lose the trailing \n, since that's how command subs work.
736
737# Here is another possibility:
738#
739# shopt -s lastpipe # need this too
740# ( set -o errexit; "$@" ) | read -r -d __stdout
741# __status=${PIPESTATUS[0]}
742# shopt -u lastpipe
743#
744# But this feels complex for just the \n issue, which can be easily worked
745# around.
746
747nq-run() {
748 ### capture status only
749
750 local -n out_status=$1
751 shift
752
753 local __status
754
755 # Tricky: turn errexit off so we can capture it, but turn it on against
756 set +o errexit
757 ( set -o errexit; "$@" )
758 __status=$?
759 set -o errexit
760
761 out_status=$__status
762}
763
764nq-capture() {
765 ### capture status and stdout
766
767 local -n out_status=$1
768 local -n out_stdout=$2
769 shift 2
770
771 local __status
772 local __stdout
773
774 # Tricky: turn errexit off so we can capture it, but turn it on against
775 set +o errexit
776 __stdout=$(set -o errexit; "$@")
777 __status=$?
778 set -o errexit
779
780 out_status=$__status
781 out_stdout=$__stdout
782}
783
784nq-capture-2() {
785 ### capture status and stderr
786
787 # This is almost identical to the above
788
789 local -n out_status=$1
790 local -n out_stderr=$2
791 shift 2
792
793 local __status
794 local __stderr
795
796 # Tricky: turn errexit off so we can capture it, but turn it on against
797 set +o errexit
798 __stderr=$(set -o errexit; "$@" 2>&1)
799 __status=$?
800 set -o errexit
801
802 out_status=$__status
803 out_stderr=$__stderr
804}
805
806# 'byo test' can set this?
807: ${NQ_TEST_TEMP=/tmp}
808
809nq-redir() {
810 ### capture status and stdout
811
812 local -n out_status=$1
813 local -n out_stdout_file=$2
814 shift 2
815
816 local __status
817 local __stdout_file=$NQ_TEST_TEMP/nq-redir-$$.txt
818
819 # Tricky: turn errexit off so we can capture it, but turn it on against
820 set +o errexit
821 ( set -o errexit; "$@" ) > $__stdout_file
822 __status=$?
823 set -o errexit
824
825 out_status=$__status
826 out_stdout_file=$__stdout_file
827}
828
829nq-redir-2() {
830 ### capture status and stdout
831
832 local -n out_status=$1
833 local -n out_stderr_file=$2
834 shift 2
835
836 local __status
837 local __stderr_file=$NQ_TEST_TEMP/nq-redir-$$.txt
838
839 # Tricky: turn errexit off so we can capture it, but turn it on against
840 set +o errexit
841 ( set -o errexit; "$@" ) 2> $__stderr_file
842 __status=$?
843 set -o errexit
844
845 out_status=$__status
846 out_stderr_file=$__stderr_file
847}
848)zZXx");
849
850GLOBAL_STR(gStr38, R"zZXx(#!/usr/bin/env bash
851#
852# Common shell functions for task scripts.
853#
854# Usage:
855# source $LIB_OSH/task-five.sh
856#
857# test-foo() { # define task functions
858# echo foo
859# }
860# task-five "$@"
861
862# Definition of a "task"
863#
864# - File invokes task-five "$@"
865# - or maybe you can look at its source
866# - It's a shell function
867# - Has ### docstring
868# - Doesn't start with _
869
870: ${LIB_OSH=stdlib/osh}
871source $LIB_OSH/byo-server.sh
872
873
874# List all functions defined in this file (and not in sourced files).
875_bash-print-funcs() {
876 ### Print shell functions in this file that don't start with _ (bash reflection)
877
878 local funcs
879 funcs=($(compgen -A function))
880 # extdebug makes `declare -F` print the file path, but, annoyingly, only
881 # if you pass the function names as arguments.
882 shopt -s extdebug
883 declare -F "${funcs[@]}" | grep --fixed-strings " $0" | awk '{print $1}'
884 shopt -u extdebug
885}
886
887_gawk-print-funcs() {
888 ### Print shell functions in this file that don't start with _ (awk parsing)
889
890 # Using gawk because it has match()
891 # - doesn't start with _
892
893 # space = / ' '* /
894 # shfunc = / %begin
895 # <capture !['_' ' '] ![' ']*>
896 # '()' space '{' space
897 # %end /
898 # docstring = / %begin
899 # space '###' ' '+
900 # <capture dot*>
901 # %end /
902 gawk '
903 match($0, /^([^_ ][^ ]*)\(\)[ ]*{[ ]*$/, m) {
904 #print NR " shfunc " m[1]
905 print m[1]
906 #print m[0]
907 }
908
909 match($0, /^[ ]*###[ ]+(.*)$/, m) {
910 print NR " docstring " m[1]
911 }
912' $0
913}
914
915_print-funcs() {
916 if command -v gawk > /dev/null; then
917 _gawk-print-funcs
918 else
919 _bash-print-funcs
920 fi
921}
922
923_show-help() {
924 # TODO:
925 # - Use awk to find comments at the top of the file?
926 # - Use OSH to extract docstrings
927 # - BYO_COMMAND=list-tasks will reuse that logic? It only applies to the
928 # current file, not anything in a different file?
929
930 echo "Usage: $0 TASK_NAME ARGS..."
931 echo
932 echo "To complete tasks, run:"
933 echo " source devtools/completion.bash"
934 echo
935 echo "Tasks:"
936
937 if command -v column >/dev/null; then
938 _print-funcs | column
939 else
940 _print-funcs
941 fi
942}
943
944task-five() {
945 # Respond to BYO_COMMAND=list-tasks, etc. All task files need this.
946 byo-maybe-run
947
948 case ${1:-} in
949 ''|--help|-h)
950 _show-help
951 exit 0
952 ;;
953 esac
954
955 if ! declare -f "$1" >/dev/null; then
956 echo "$0: '$1' isn't an action in this task file. Try '$0 --help'"
957 exit 1
958 fi
959
960 "$@"
961}
962)zZXx");
963
964GLOBAL_STR(gStr39, R"zZXx(# Two functions I actually use, all the time.
965#
966# To keep depenedencies small, this library will NEVER grow other functions
967# (and is named to imply that.)
968#
969# Usage:
970# source --builtin two.sh
971#
972# Examples:
973# log 'hi'
974# die 'expected a number'
975
976if command -v source-guard >/dev/null; then # include guard for YSH
977 source-guard two || return 0
978fi
979
980log() {
981 ### Write a message to stderr.
982 echo "$@" >&2
983}
984
985die() {
986 ### Write an error message with the script name, and exit with status 1.
987 log "$0: fatal: $@"
988 exit 1
989}
990
991)zZXx");
992
993GLOBAL_STR(gStr40, R"zZXx(# These were helpful while implementing args.ysh
994# Maybe we will want to export them in a prelude so that others can use them too?
995#
996# Prior art: Rust has `todo!()` which is quite nice. Other languages allow
997# users to `raise NotImplmentedError()`.
998
999# Andy comments:
1000# - 'pass' can be : or true in shell. It's a little obscure / confusing, but
1001# there is an argument for minimalism. Although I prefer words like 'true',
1002# and that already means something.
1003# - UPDATE: we once took 'pass' as a keyword, but users complained because
1004# there is a command 'pass'. So we probably can't have this by default.
1005# Need to discuss source --builtin.
1006
1007# - todo could be more static? Rust presumably does it at compile time
1008
1009proc todo () {
1010 ## Raises a not implemented error when run.
1011 error ("TODO: not implemented") # TODO: is error code 1 ok?
1012}
1013
1014proc pass () {
1015 ## Use when you want to temporarily leave a block empty.
1016 _ null
1017}
1018)zZXx");
1019
1020GLOBAL_STR(gStr41, R"zZXx(# testing.ysh
1021#
1022# Usage:
1023# source --builtin testing.sh
1024#
1025# func f(x) { return (x + 1) }
1026#
1027# describe foo {
1028# assert (43 === f(42))
1029# }
1030#
1031# if is-main {
1032# run-tests @ARGV # --filter
1033# }
1034
1035module stdlib/testing || return 0
1036
1037source --builtin args.ysh
1038
1039proc assert ( ; cond ; fail_message='default fail message') {
1040 echo 'hi from assert'
1041
1042 = cond
1043
1044 # I think this might be ready now?
1045
1046 var val = evalExpr(cond)
1047
1048 echo
1049 echo 'value'
1050 = val
1051 pp line (val)
1052
1053 = fail_message
1054
1055 if (val) {
1056 echo 'OK'
1057 } else {
1058 var m = evalExpr(fail_message)
1059 echo "FAIL - this is where we extract the string - $m"
1060 }
1061}
1062
1063proc test-assert {
1064 var x = 42
1065 assert [42 === x]
1066}
1067
1068proc test-expr ( ; expr ) {
1069 echo 'expr'
1070 pp line (expr)
1071}
1072
1073proc test-named ( ; ; n=^[99] ) {
1074 echo 'n'
1075 pp line (n)
1076}
1077
1078# What happens when there are duplicate test IDs?
1079#
1080# Also I think filter by "$test_id/$case_id"
1081
1082proc __it (case_id ; ; ; block) {
1083 # This uses a clean directory
1084 echo TODO
1085}
1086
1087# is this accessible to users?
1088# It can contain a global list of things to run
1089
1090# Naming convention: a proc named 'describe' mutates a global named _describe?
1091# Or maybe _describe_list ?
1092
1093var _describe_list = []
1094
1095proc describe (test_id ; ; ; block) {
1096 echo describe
1097 #= desc
1098
1099 # TODO:
1100 # - need append
1101 # - need ::
1102 # _ _describe->append(cmd)
1103 #
1104 # Need to clean this up
1105 # append (_describe, cmd) # does NOT work!
1106
1107 call _describe_list->append(block)
1108}
1109
1110proc Args {
1111 echo TODO
1112}
1113
1114# Problem: this creates a global variable?
1115Args (&spec) {
1116 flag --filter 'Regex of test descriptions'
1117}
1118
1119proc run-tests {
1120 var opt, i = parseArgs(spec, ARGV)
1121
1122 # TODO:
1123 # - parse --filter foo, which you can use eggex for!
1124
1125 for cmd in (_describe) {
1126 # TODO: print filename and 'describe' name?
1127 try {
1128 eval (cmd)
1129 }
1130 if (_status !== 0) {
1131 echo 'failed'
1132 }
1133 }
1134}
1135)zZXx");
1136
1137
1138
1139TextFile array[] = {
1140 {.rel_path = "_devbuild/help/data-errors", .contents = gStr0},
1141 {.rel_path = "_devbuild/help/data-front-end", .contents = gStr1},
1142 {.rel_path = "_devbuild/help/data-j8-notation", .contents = gStr2},
1143 {.rel_path = "_devbuild/help/help", .contents = gStr3},
1144 {.rel_path = "_devbuild/help/oils-usage", .contents = gStr4},
1145 {.rel_path = "_devbuild/help/osh-builtin-cmd", .contents = gStr5},
1146 {.rel_path = "_devbuild/help/osh-chapters", .contents = gStr6},
1147 {.rel_path = "_devbuild/help/osh-cmd-lang", .contents = gStr7},
1148 {.rel_path = "_devbuild/help/osh-front-end", .contents = gStr8},
1149 {.rel_path = "_devbuild/help/osh-mini-lang", .contents = gStr9},
1150 {.rel_path = "_devbuild/help/osh-option", .contents = gStr10},
1151 {.rel_path = "_devbuild/help/osh-osh-assign", .contents = gStr11},
1152 {.rel_path = "_devbuild/help/osh-plugin", .contents = gStr12},
1153 {.rel_path = "_devbuild/help/osh-special-var", .contents = gStr13},
1154 {.rel_path = "_devbuild/help/osh-stdlib", .contents = gStr14},
1155 {.rel_path = "_devbuild/help/osh-type-method", .contents = gStr15},
1156 {.rel_path = "_devbuild/help/osh-usage", .contents = gStr16},
1157 {.rel_path = "_devbuild/help/osh-word-lang", .contents = gStr17},
1158 {.rel_path = "_devbuild/help/ysh-builtin-cmd", .contents = gStr18},
1159 {.rel_path = "_devbuild/help/ysh-builtin-func", .contents = gStr19},
1160 {.rel_path = "_devbuild/help/ysh-chapters", .contents = gStr20},
1161 {.rel_path = "_devbuild/help/ysh-cmd-lang", .contents = gStr21},
1162 {.rel_path = "_devbuild/help/ysh-expr-lang", .contents = gStr22},
1163 {.rel_path = "_devbuild/help/ysh-front-end", .contents = gStr23},
1164 {.rel_path = "_devbuild/help/ysh-mini-lang", .contents = gStr24},
1165 {.rel_path = "_devbuild/help/ysh-option", .contents = gStr25},
1166 {.rel_path = "_devbuild/help/ysh-plugin", .contents = gStr26},
1167 {.rel_path = "_devbuild/help/ysh-special-var", .contents = gStr27},
1168 {.rel_path = "_devbuild/help/ysh-stdlib", .contents = gStr28},
1169 {.rel_path = "_devbuild/help/ysh-type-method", .contents = gStr29},
1170 {.rel_path = "_devbuild/help/ysh-usage", .contents = gStr30},
1171 {.rel_path = "_devbuild/help/ysh-word-lang", .contents = gStr31},
1172 {.rel_path = "_devbuild/help/ysh-ysh-cmd", .contents = gStr32},
1173 {.rel_path = "stdlib/funcs.ysh", .contents = gStr33},
1174 {.rel_path = "stdlib/methods.ysh", .contents = gStr34},
1175 {.rel_path = "stdlib/osh/bash-strict.sh", .contents = gStr35},
1176 {.rel_path = "stdlib/osh/byo-server.sh", .contents = gStr36},
1177 {.rel_path = "stdlib/osh/no-quotes.sh", .contents = gStr37},
1178 {.rel_path = "stdlib/osh/task-five.sh", .contents = gStr38},
1179 {.rel_path = "stdlib/osh/two.sh", .contents = gStr39},
1180 {.rel_path = "stdlib/prelude.ysh", .contents = gStr40},
1181 {.rel_path = "stdlib/testing.ysh", .contents = gStr41},
1182
1183 {.rel_path = nullptr, .contents = nullptr},
1184};
1185
1186} // namespace embedded_file
1187
1188TextFile* gEmbeddedFiles = embedded_file::array; // turn array into pointer