Collapse gd_astpending and gd_reqpri together into gd_reqflags. gd_reqflags
[dragonfly.git] / lib / libstand / libstand.3
1 .\" Copyright (c) Michael Smith
2 .\" All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\"
13 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 .\" SUCH DAMAGE.
24 .\"
25 .\" $FreeBSD: src/lib/libstand/libstand.3,v 1.5.2.11 2002/06/26 19:14:43 schweikh Exp $
26 .\" $DragonFly: src/lib/libstand/libstand.3,v 1.2 2003/06/17 04:26:51 dillon Exp $
27 .\"
28 .Dd June 22, 1998
29 .Dt LIBSTAND 3
30 .Os
31 .Sh NAME
32 .Nm libstand
33 .Nd support library for standalone executables
34 .Sh SYNOPSIS
35 .In stand.h
36 .Sh DESCRIPTION
37 .Nm
38 provides a set of supporting functions for standalone
39 applications, mimicking where possible the standard
40 .Bx
41 programming
42 environment.  The following sections group these functions by kind.
43 Unless specifically described here, see the corresponding section 3
44 manpages for the given functions.
45 .Sh STRING FUNCTIONS
46 String functions are available as documented in
47 .Xr string 3
48 and
49 .Xr bstring 3 .
50 .Sh MEMORY ALLOCATION
51 .Bl -hang -width 10n
52 .It Xo
53 .Ft "void *"
54 .Fn malloc "size_t size"
55 .Xc
56 .Pp
57 Allocate
58 .Fa size
59 bytes of memory from the heap using a best-fit algorithm.
60 .It Xo
61 .Ft void
62 .Fn free "void *ptr"
63 .Xc
64 .Pp
65 Free the allocated object at
66 .Fa ptr .
67 .It Xo
68 .Ft void
69 .Fn setheap "void *start" "void *limit"
70 .Xc
71 .Pp
72 Initialise the heap.  This function must be called before calling
73 .Fn alloc
74 for the first time.  The region between
75 .Fa start
76 and
77 .Fa limit
78 will be used for the heap; attempting to allocate beyond this will result
79 in a panic.
80 .It Xo
81 .Ft "char *"
82 .Fn sbrk "int junk"
83 .Xc
84 .Pp
85 Provides the behaviour of
86 .Fn sbrk 0 ,
87 ie. returns the highest point that the heap has reached.  This value can
88 be used during testing to determine the actual heap usage.  The
89 .Fa junk
90 argument is ignored.
91 .El
92 .Sh ENVIRONMENT
93 A set of functions are provided for manipulating a flat variable space similar
94 to the traditional shell-supported environment.  Major enhancements are support
95 for set/unset hook functions.
96 .Bl -hang -width 10n
97 .It Xo
98 .Ft "char *"
99 .Fn getenv "const char *name"
100 .Xc
101 .It Xo
102 .Ft int
103 .Fn setenv "const char *name" "char *value" "int overwrite"
104 .Xc
105 .It Xo
106 .Ft int
107 .Fn putenv "const char *string"
108 .Xc
109 .It Xo
110 .Ft int
111 .Fn unsetenv "const char *name"
112 .Xc
113 .Pp
114 These functions behave similarly to their standard library counterparts.
115 .It Xo
116 .Ft "struct env_var *"
117 .Fn env_getenv "const char *name"
118 .Xc
119 .Pp
120 Looks up a variable in the environment and returns its entire
121 data structure.
122 .It Xo
123 .Ft int
124 .Fn env_setenv "const char *name" "int flags" "char *value" "ev_sethook_t sethook" "ev_unsethook_t unsethook"
125 .Xc
126 .Pp
127 Creates a new or sets an existing environment variable called
128 .Fa name .
129 If creating a new variable, the
130 .Fa sethook
131 and
132 .Fa unsethook
133 arguments may be specified.
134 .Pp
135 The set hook is invoked whenever an attempt
136 is made to set the variable, unless the EV_NOHOOK flag is set.  Typically
137 a set hook will validate the
138 .Fa value
139 argument, and then call
140 .Fn env_setenv
141 again with EV_NOHOOK set to actually save the value.  The predefined function
142 .Fn env_noset
143 may be specified to refuse all attempts to set a variable.
144 .Pp
145 The unset hook is invoked when an attempt is made to unset a variable.
146 If it
147 returns zero, the  variable will be unset.  The predefined function
148 .Fa env_nounset
149 may be used to prevent a variable being unset.
150 .El
151 .Sh STANDARD LIBRARY SUPPORT
152 .Bl -hang -width 10n
153 .It Xo
154 .Ft int
155 .Fn getopt "int argc" "char * const *argv" "const char *optstring"
156 .Xc
157 .It Xo
158 .Ft long
159 .Fn strtol "const char *nptr" "char **endptr" "int base"
160 .Xc
161 .It Xo
162 .Ft void
163 .Fn srandom "unsigned long seed"
164 .Xc
165 .It Xo
166 .Ft "unsigned long"
167 .Fn random void
168 .Xc
169 .It Xo
170 .Ft "char *"
171 .Fn strerror "int error"
172 .Xc
173 .Pp
174 Returns error messages for the subset of errno values supported by
175 .Nm .
176 .It Fn assert expression
177 .Pp
178 Requires
179 .In assert.h
180 .It Xo
181 .Ft int
182 .Fn setjmp "jmp_buf env"
183 .Xc
184 .It Xo
185 .Ft void
186 .Fn longjmp "jmp_buf env" "int val"
187 .Xc
188 .Pp
189 Defined as
190 .Fn _setjmp
191 and
192 .Fn _longjmp
193 respectively as there is no signal state to manipulate.  Requires
194 .In setjmp.h
195 .El
196 .Sh CHARACTER I/O
197 .Bl -hang -width 10n
198 .It Xo
199 .Ft void
200 .Fn gets "char *buf"
201 .Xc
202 .Pp
203 Read characters from the console into
204 .Fa buf .
205 All of the standard cautions apply to this function.
206 .It Xo
207 .Ft void
208 .Fn ngets "char *buf" "size_t size"
209 .Xc
210 .Pp
211 Read at most
212 .Fa size
213 - 1 characters from the console into
214 .Fa buf .
215 If
216 .Fa size
217 is less than 1, the function's behaviour is as for
218 .Fn gets .
219 .It Xo
220 .Ft int
221 .Fn fgetstr "char *buf" "int size" "int fd"
222 .Xc
223 .Pp
224 Read a line of at most
225 .Fa size
226 characters into
227 .Fa buf .
228 Line terminating characters are stripped, and the buffer is always nul
229 terminated.  Returns the number of characters in
230 .Fa buf
231 if successful, or -1 if a read error occurs.
232 .It Xo
233 .Ft int
234 .Fn printf "const char *fmt" "..."
235 .Xc
236 .It Xo
237 .Ft void
238 .Fn vprintf "const char *fmt" "va_list ap"
239 .Xc
240 .It Xo
241 .Ft int
242 .Fn sprintf "char *buf" "const char *fmt" "..."
243 .Xc
244 .It Xo
245 .Ft void
246 .Fn vsprintf "char *buf" "const char *fmt" "va_list ap"
247 .Xc
248 .Pp
249 The *printf functions implement a subset of the standard
250 .Fn printf
251 family functionality and some extensions.  The following standard conversions
252 are supported: c,d,n,o,p,s,u,x.  The following modifiers are supported:
253 +,-,#,*,0,field width,precision,l.
254 .Pp
255 The
256 .Li b
257 conversion is provided to decode error registers.  Its usage is:
258 .Pp
259 .Bd -ragged -offset indent
260 printf(
261 .Qq reg=%b\en ,
262 regval,
263 .Qq <base><arg>*
264 );
265 .Ed
266 .Pp
267 where <base> is the output expressed as a control character, eg. \e10 gives
268 octal, \e20 gives hex.  Each <arg> is a sequence of characters, the first of
269 which gives the bit number to be inspected (origin 1) and the next characters
270 (up to a character less than 32) give the text to be displayed if the bit is set.
271 Thus
272 .Pp
273 .Bd -ragged -offset indent
274 printf(
275 .Qq reg=%b\en
276 3
277 .Qq \e10\e2BITTWO\e1BITONE\en
278 );
279 .Ed
280 .Pp
281 would give the output
282 .Pp
283 .Bd -ragged -offset indent
284 reg=3<BITTWO,BITONE>
285 .Ed
286 .Pp
287 The
288 .Li D
289 conversion provides a hexdump facility, eg.
290 .Pp
291 .Bd -ragged -offset indent
292 printf(
293 .Qq %6D ,
294 ptr,
295 .Qq \&:
296 );  gives
297 .Qq XX:XX:XX:XX:XX:XX
298 .Ed
299 .Bd -ragged -offset indent
300 printf(
301 .Qq %*D ,
302 len,
303 ptr,
304 .Qq "\ "
305 );  gives
306 .Qq XX XX XX ...
307 .Ed
308 .El
309 .Sh CHARACTER TESTS AND CONVERSIONS
310 .Bl -hang -width 10n
311 .It Xo
312 .Ft int
313 .Fn isupper "int c"
314 .Xc
315 .It Xo
316 .Ft int
317 .Fn islower "int c"
318 .Xc
319 .It Xo
320 .Ft int
321 .Fn isspace "int c"
322 .Xc
323 .It Xo
324 .Ft int
325 .Fn isdigit "int c"
326 .Xc
327 .It Xo
328 .Ft int
329 .Fn isxdigit "int c"
330 .Xc
331 .It Xo
332 .Ft int
333 .Fn isascii "int c"
334 .Xc
335 .It Xo
336 .Ft int
337 .Fn isalpha "int c"
338 .Xc
339 .It Xo
340 .Ft int
341 .Fn toupper "int c"
342 .Xc
343 .It Xo
344 .Ft int
345 .Fn tolower "int c"
346 .Xc
347 .El
348 .Sh FILE I/O
349 .Bl -hang -width 10n
350 .It Xo
351 .Ft int
352 .Fn open "const char *path" "int flags"
353 .Xc
354 .Pp
355 Similar to the behaviour as specified in
356 .Xr open 2 ,
357 except that file creation is not supported, so the mode parameter is not
358 required.  The
359 .Fa flags
360 argument may be one of O_RDONLY, O_WRONLY and O_RDWR (although no filesystems
361 currently support writing).
362 .It Xo
363 .Ft int
364 .Fn close "int fd"
365 .Xc
366 .It Xo
367 .Ft void
368 .Fn closeall void
369 .Xc
370 .Pp
371 Close all open files.
372 .It Xo
373 .Ft ssize_t
374 .Fn read "int fd" "void *buf" "size_t len"
375 .Xc
376 .It Xo
377 .Ft ssize_t
378 .Fn write "int fd" "void *buf" "size_t len"
379 .Xc
380 .Pp
381 (No filesystems currently support writing.)
382 .It Xo
383 .Ft off_t
384 .Fn lseek "int fd" "off_t offset" "int whence"
385 .Xc
386 .Pp
387 Files being automatically uncompressed during reading cannot seek backwards
388 from the current point.
389 .It Xo
390 .Ft int
391 .Fn stat "const char *path" "struct stat *sb"
392 .Xc
393 .It Xo
394 .Ft int
395 .Fn fstat "int fd" "struct stat *sb"
396 .Xc
397 .Pp
398 The
399 .Fn stat
400 and
401 .Fn fstat
402 functions only fill out the following fields in the
403 .Fa sb
404 structure: st_mode,st_nlink,st_uid,st_gid,st_size.  The
405 .Nm tftp
406 filesystem cannot provide meaningful values for this call, and the
407 .Nm cd9660
408 filesystem always reports files having uid/gid of zero.
409 .El
410 .Sh PAGER
411 .Nm
412 supplies a simple internal pager to ease reading the output of large commands.
413 .Bl -hang -width 10n
414 .It Xo
415 .Ft void
416 .Fn pager_open
417 .Xc
418 .Pp
419 Initialises the pager and tells it that the next line output will be the top of the
420 display.  The environment variable LINES is consulted to determine the number of
421 lines to be displayed before pausing.
422 .It Xo
423 .Ft void
424 .Fn pager_close void
425 .Xc
426 .Pp
427 Closes the pager.
428 .It Xo
429 .Ft int
430 .Fn pager_output "char *lines"
431 .Xc
432 .Pp
433 Sends the lines in the nul-terminated buffer at
434 .Fa lines
435 to the pager.  Newline characters are counted in order to determine the number
436 of lines being output (wrapped lines are not accounted for).
437 .Fn pager_output
438 will return zero when all of the lines have been output, or nonzero if the
439 display was paused and the user elected to quit.
440 .It Xo
441 .Ft int
442 .Fn pager_file "char *fname"
443 .Xc
444 .Pp
445 Attempts to open and display the file
446 .Fa fname .
447 Returns -1 on error, 0 at EOF, or 1 if the user elects to quit while reading.
448 .El
449 .Sh MISC
450 .Bl -hang -width 10n
451 .It Xo
452 .Ft void
453 .Fn twiddle void
454 .Xc
455 .Pp
456 Successive calls emit the characters in the sequence |,/,-,\\ followed by a
457 backspace in order to provide reassurance to the user.
458 .El
459 .Sh REQUIRED LOW-LEVEL SUPPORT
460 The following resources are consumed by
461 .Nm
462 - stack, heap, console and devices.
463 .Pp
464 The stack must be established before
465 .Nm
466 functions can be invoked.  Stack requirements vary depending on the functions
467 and filesystems used by the consumer and the support layer functions detailed
468 below.
469 .Pp
470 The heap must be established before calling
471 .Fn alloc
472 or
473 .Fn open
474 by calling
475 .Fn setheap .
476 Heap usage will vary depending on the number of simultaneously open files,
477 as well as client behaviour.  Automatic decompression will allocate more
478 than 64K of data per open file.
479 .Pp
480 Console access is performed via the
481 .Fn getchar ,
482 .Fn putchar
483 and
484 .Fn ischar
485 functions detailed below.
486 .Pp
487 Device access is initiated via
488 .Fn devopen
489 and is performed through the
490 .Fn dv_strategy ,
491 .Fn dv_ioctl
492 and
493 .Fn dv_close
494 functions in the device switch structure that
495 .Fn devopen
496 returns.
497 .Pp
498 The consumer must provide the following support functions:
499 .Bl -hang -width 10n
500 .It Xo
501 .Ft int
502 .Fn getchar void
503 .Xc
504 .Pp
505 Return a character from the console, used by
506 .Fn gets ,
507 .Fn ngets
508 and pager functions.
509 .It Xo
510 .Ft int
511 .Fn ischar void
512 .Xc
513 .Pp
514 Returns nonzero if a character is waiting from the console.
515 .It Xo
516 .Ft void
517 .Fn putchar int
518 .Xc
519 .Pp
520 Write a character to the console, used by
521 .Fn gets ,
522 .Fn ngets ,
523 .Fn *printf ,
524 .Fn panic
525 and
526 .Fn twiddle
527 and thus by many other functions for debugging and informational output.
528 .It Xo
529 .Ft int
530 .Fn devopen "struct open_file *of" "const char *name" "char **file"
531 .Xc
532 .Pp
533 Open the appropriate device for the file named in
534 .Fa name ,
535 returning in
536 .Fa file
537 a pointer to the remaining body of
538 .Fa name
539 which does not refer to the device.  The
540 .Va f_dev
541 field in
542 .Fa of
543 will be set to point to the
544 .Vt devsw
545 structure for the opened device if successful.  Device identifiers must
546 always precede the path component, but may otherwise be arbitrarily formatted.
547 Used by
548 .Fn open
549 and thus for all device-related I/O.
550 .It Xo
551 .Ft int
552 .Fn devclose "struct open_file *of"
553 .Xc
554 Close the device allocated for
555 .Fa of .
556 The device driver itself will already have been called for the close; this call
557 should clean up any allocation made by devopen only.
558 .It Xo
559 .Ft void
560 .Fn panic "const char *msg" "..."
561 .Xc
562 .Pp
563 Signal a fatal and unrecoverable error condition.  The
564 .Fa msg ...
565 arguments are as for
566 .Fn printf .
567 .El
568 .Sh INTERNAL FILESYSTEMS
569 Internal filesystems are enabled by the consumer exporting the array
570 .Vt struct fs_ops *file_system[] ,
571 which should be initialised with pointers
572 to
573 .Vt struct fs_ops
574 structures.  The following filesystem handlers are supplied by
575 .Nm ,
576 the consumer may supply other filesystems of their own:
577 .Bl -hang -width ".Va cd9660_fsops"
578 .It Va ufs_fsops
579 The
580 .Bx
581 UFS.
582 .It Va ext2fs_fsops
583 Linux ext2fs filesystem.
584 .It Va tftp_fsops
585 File access via TFTP.
586 .It Va nfs_fsops
587 File access via NFS.
588 .It Va cd9660_fsops
589 ISO 9660 (CD-ROM) filesystem.
590 .It Va zipfs_fsops
591 Stacked filesystem supporting gzipped files.
592 When trying the zipfs filesystem,
593 .Nm
594 appends
595 .Li .gz
596 to the end of the filename, and then tries to locate the file using the other
597 filesystems.  Placement of this filesystem in the
598 .Va file_system[]
599 array determines whether gzipped files will be opened in preference to non-gzipped
600 files.  It is only possible to seek a gzipped file forwards, and
601 .Fn stat
602 and
603 .Fn fstat
604 on gzipped files will report an invalid length.
605 .It Va bzipfs_fsops
606 The same as
607 .Va zipfs_fsops ,
608 but for
609 .Xr bzip2 1 Ns -compressed
610 files.
611 .El
612 .Pp
613 The array of
614 .Vt struct fs_ops
615 pointers should be terminated with a NULL.
616 .Sh DEVICES
617 Devices are exported by the supporting code via the array
618 .Vt struct devsw *devsw[]
619 which is a NULL terminated array of pointers to device switch structures.
620 .Sh BUGS
621 The lack of detailed memory usage data is unhelpful.
622 .Sh HISTORY
623 .Nm
624 contains contributions from many sources, including:
625 .Bl -bullet -compact
626 .It
627 .Nm libsa
628 from
629 .Nx
630 .It
631 .Nm libc
632 and
633 .Nm libkern
634 from
635 .Fx 3.0 .
636 .It
637 .Nm zalloc
638 from
639 .An Matthew Dillon Aq dillon@backplane.com
640 .El
641 .Pp
642 The reorganisation and port to
643 .Fx 3.0 ,
644 the environment functions and this manpage were written by
645 .An Mike Smith Aq msmith@FreeBSD.org .