1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
|
.TH SH-STD 1
.SH NAME
std, if, while, ~, no, !, apply, getlines, status, pctl, fn, and, or, raise, rescue, hd, tl, index, split, join, pid, parse, pctl, env \- standard shell builtins module.
.SH SYNOPSIS
.B load std
.B !
.I command
.br
.B ~
.I value
[
.IR pattern ...
]
.br
.B no
[
.IR arg ...
]
.br
.B and
.IR command ...
.br
.B apply
.I command
[
.IR arg ...
]
.br
.B getlines
[
.I separators
]
.I command
.br
.B flag
.I f
[
.B +-
]
.br
.B for
.I var
.B in
[
.IR arg ...
]
.I command
.br
.B fn
.I name command
.br
.B if
.I condition action
[
.I condition action
]... [
.I elseaction
]
.br
.B or
.IR command ...
.br
.B pctl
.IR flag...
.br
.B raise
.I name
.br
.B rescue
.I pattern rescueblock command
.br
.B status
.I value
.br
.B subfn
.I name command
.br
.B if
[
.I condition action
]... [
.I elseaction
]
.br
.B while
.I condition command
.br
.B ${hd
.IB list }
.br
.B ${index
.I number
.IB list }
.br
.B ${pid}
.br
.B ${split
[
.I separators
]
.IB arg }
.br
.B ${join
.I separator
.IB list }
.br
.B ${tl
.IB list }
.br
.B ${parse
.IB arg ]
.br
.B ${pipe
(
.B from
|
.B to
|
.I fdnum
)
.IB command }
.br
.B ${env}
.SH DESCRIPTION
.B Std
is a loadable module for
.IR sh (1)
that provides the equivalent of a
``standard library'' for the shell, including
a set of control-flow constructs and some
other miscellaneous commands.
In the following descriptions, if an argument is
executed, then it should be a braced block
suitable for executing by
.IR sh .
A true exit status is defined to be nil;
any non-nil exit status is false.
Unless otherwise stated, the return value
of a command is that of the last command that
it executed.
If invalid arguments are passed to any command,
a
.B usage
exception is raised, and a message printed to stderr.
.PP
Each of the looping commands
.BR for ,
.BR apply ,
.BR while ,
and
.B getlines
installs an exception handler for the duration of the
loop to catch the exceptions
.B break
and
.BR continue .
If a
.B break
exception is caught, the loop is terminated; if a
.B continue
exception is caught, the loop will continue executing as
usual.
The commands are as follows:
.TP 10
.B !
.B !
inverts the exit status of a command (non-null is changed to null,
null is changed to non-null).
.TP
.B ~
.B ~
matches
.I value
against each
.I pattern
in turn, returning true if any of them match and false otherwise.
The patterns are of the same form as those accepted by
the shell for filename pattern matching except that / is
not treated specially. (see
.IR filepat (2)).
Patterns
must be quoted to stop the shell from interpreting them.
.TP
.B no
True if there are no arguments. Useful for testing if there are any items in a list
without counting the items with
.BR $# .
.TP
.BI and
.B And
evaluates each
.I command
in turn until one returns false.
.TP
.B apply
.B Apply
evaluates
.I command
once for each
.IR arg ,
passing it in the variable
.BR $1 .
.TP
.B getlines
.B Getlines
reads lines from the standard input,
executing
.I command
for each line, setting the environment variable
.B $line
to the line read, with any terminating character
removed. If
.I separators
is given, a line is terminated when any character
in
.I separators
is found; the default separator string
is a single newline character.
.TP
.B flag
Either set
.RB ( + ),
clear
.RB ( - ),
or test (neither
.B +
or
.BR - )
the flag
.IR f ,
where
.I f
is a single character, one of the
command line flags to
.I sh
(see
.IR sh (1)).
.TP
.B fn
.B Fn
defines a new builtin command named
.IR name ;
when run, this command evaluates
.IR command .
The command is stored in the environment
variable
.BI fn- name\f1;\fP
any variables of this form found when
when
.B std
is loaded will be defined in this way.
If
.I command
is not given, then the builtin will be removed.
.TP
.B subfn
.B Subfn
is similar to
.B fn
except that it defines a new substitution builtin
.IR name .
When
.I name
is invoked, it creates a new local variable
.B result
and executes
.IR command .
The value of
.B $result
when
.I command
has terminated is the value yielded by the substitution builtin
.IR name .
.I Command
is stored in and restored from the environment in a similar
way to
.BR fn ,
except that
.BI sfn- name
is used as the name of the environment variable.
.TP
.B if
.B If
executes
.IR condition ;
if it returns true, then
.I action
is executed, otherwise each of the next
.IR condition - action
pairs is evaluated in the same way; if no
.I condition
is satisfied, then
.I elseaction
will be executed, if present.
.TP
.B for
.B For
is similar to
.BR apply ;
it runs
.I command
once for each
.IR arg ,
but it performs a local assignment of
.I arg
to
.I var
each time.
.TP
.B or
.B Or
evaluates each
.I command
in turn until one returns true.
.TP
.B pctl
.B Pctl
is an interface to the Inferno system call
.IR sys-pctl (2);
each argument specifies one bit in the bitmask
passed to that function. The possible flags are
.BR newfd ,
.BR forkfd ,
.BR newns ,
.BR forkns ,
.BR newpgrp
and
.BR nodevs .
See
.IR sys-pctl (2)
for details of the meaning of these flags.
.B Pctl
returns true.
.TP
.B raise
.B Raise
raises the exception
.IR name ;
.I name
will be truncated if it is longer than
that allowed by
.I raise
(see
.IR sys-exception (2)).
Control will be transferred to the innermost
rescue block in the same process that
matches
.IR name .
If there is no rescue block in place,
the current process will exit, yielding
.I name
as its exit status.
If no
.I name
is given, the exception named in
.B $exception
is raised; if this is null, a
.B bad raise context
exception is raised.
The default command prompt catches all
exceptions.
.TP
.B rescue
.B Rescue
executes
.I command
with an exception handler installed for the duration
of the call. It will catch all exceptions with a name
matching
.IR pattern ,
where
.I pattern
is of the same form accepted by
.I rescue
(see
.IR sys-exception (2));
i.e. an exact match, or a prefix
followed by an asterisk
.RB ( * )
which will match an exception starting with the prefix.
If an exception is caught,
.B rescue
executes
.IR rescueblock ,
setting
.B $exception
to the name of the exception raised.
.TP
.B status
returns its first argument word as its exit status,
or nil if none is given.
.TP
.B while
.B While
repeatedly executes
.I condition
and then
.I action
until
.I condition
does not return true.
.TP
.B ${env}
.B Env
yields a list of the names of all
currently set non-nil environment variables.
.TP
.B ${hd}
.B Hd
yields the first of its arguments, or nil if there
are no arguments.
.TP
.B ${index}
.B Index
yields the
.IR n 'th
element in its argument list, indexed from 1.
.I N
must be a decimal integer.
.TP
.B ${join}
.B Join
yields a single element which is the concatenation of all
the elements in
.I list
separated by
.IR separator .
If there are no elements in
.IR list ,
it yields an empty string.
The shell operator
\f5$"\f2var\f1
is exactly equivalent to
.BI "${join ' ' $" var }\f1.\fP
.TP
.B ${parse}
.B Parse
parses
.I arg
according to the usual syntax rules, raising a
.B parse error
exception if it fails.
.I Arg
must be a well-formed command block
surrounded by braces.
.B Parse
yields a functionally equivalent version
of
.IR arg .
.TP
.B ${pid}
.B Pid
yields the process id of the current process.
.TP
.B ${pipe}
.B Pipe
runs
.I command
asynchronously, with one of its file descriptors connected
to a bidirectional pipe. The first argument to
.B pipe
determines which file descriptor is connected: if
the argument is
.BR from ,
its standard output is connected; if the argument is
.BR to ,
its standard input is connected; otherwise file descriptor
.I fdnum
is connected.
.B Pipe
yields the name of a file that can be opened to access
the other end of the pipe. This allows the construction
of non-linear pipelines. For example, the following runs two commands
.B old
and
.B new
and uses
.B cmp
to compare their outputs
.EX
cmp ${pipe from {old}} ${pipe from {new}}
.EE
.TP
.B ${split}
.B Split
splits
.I arg
into list elements at every point where one or
more characters in
.I separators
appear. If
.I separators
is not given, the value of
.B $ifs
is used.
.TP
.B ${tl}
.B Tl
yields all but the first of its arguments,
or nil if there are no arguments.
.SS Syntactic considerations
It is worth being aware of a few pitfalls that await the user of
some of these commands. Unlike other shells, the syntax of
.I sh
does not include the syntax of the control flow commands,
so it is important to be aware of the rules that govern
the gathering of the arguments for a command.
In particular, the following code, written to print a message
a filename ends in
.B .b
will not work: it will always print ``file is Limbo source''.
.EX
and
{~ $filename '*.b'}
{echo file is Limbo source}
.EE
This is because newlines separate shell commands, so
the above code first invokes
.B and
with no arguments, and then each of the braced block
commands on each subsequent line.
It is usual to use round brackets in order to group together arguments
on separate lines, e.g.
.EX
and (
{~ $filename '*.b'}
{echo file is Limbo source}
)
.EE
This has the originally intended meaning.
.SH FILES
.TF /tmp/pipes/*
.TP
.B /tmp/pipe.*d
Temporary placeholder directory for named pipes.
.TP
.B /tmp/pipes/*
Mount point for named pipes.
.SH SOURCE
.B /appl/cmd/sh/std.b
.SH SEE ALSO
.IR sh (1),
.IR sh-expr (1),
.IR sh-tk (1)
.SH BUGS
As the shell does not know about functions,
the
.B whatis
command cannot return information about
their value. Use
.BI "whatis fn-" name
to find out the current definition of a function,
or
.BI "whatis sfn-" name
to find out the current definition of a substitution function.
|