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