]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - lib/libstand/libstand.3
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.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 .Bd -ragged -offset indent
274 printf(
275 .Qq reg=%b\en ,
276 regval,
277 .Qq <base><arg>*
278 );
279 .Ed
280 .Pp
281 where <base> is the output expressed as a control character, e.g.\& \e10 gives
282 octal, \e20 gives hex.
283 Each <arg> is a sequence of characters, the first of
284 which gives the bit number to be inspected (origin 1) and the next characters
285 (up to a character less than 32) give the text to be displayed if the bit is set.
286 Thus
287 .Bd -ragged -offset indent
288 printf(
289 .Qq reg=%b\en ,
290 3,
291 .Qq \e10\e2BITTWO\e1BITONE\en
292 );
293 .Ed
294 .Pp
295 would give the output
296 .Bd -ragged -offset indent
297 reg=3<BITTWO,BITONE>
298 .Ed
299 .Pp
300 The
301 .Li D
302 conversion provides a hexdump facility, e.g.
303 .Bd -ragged -offset indent
304 printf(
305 .Qq %6D ,
306 ptr,
307 .Qq \&:
308 ); gives
309 .Qq XX:XX:XX:XX:XX:XX
310 .Ed
311 .Bd -ragged -offset indent
312 printf(
313 .Qq %*D ,
314 len,
315 ptr,
316 .Qq "\ "
317 ); gives
318 .Qq XX XX XX ...
319 .Ed
320 .El
321 .Sh CHARACTER TESTS AND CONVERSIONS
322 .Bl -hang -width 10n
323 .It Xo
324 .Ft int
325 .Fn isupper "int c"
326 .Xc
327 .It Xo
328 .Ft int
329 .Fn islower "int c"
330 .Xc
331 .It Xo
332 .Ft int
333 .Fn isspace "int c"
334 .Xc
335 .It Xo
336 .Ft int
337 .Fn isdigit "int c"
338 .Xc
339 .It Xo
340 .Ft int
341 .Fn isxdigit "int c"
342 .Xc
343 .It Xo
344 .Ft int
345 .Fn isascii "int c"
346 .Xc
347 .It Xo
348 .Ft int
349 .Fn isalpha "int c"
350 .Xc
351 .It Xo
352 .Ft int
353 .Fn toupper "int c"
354 .Xc
355 .It Xo
356 .Ft int
357 .Fn tolower "int c"
358 .Xc
359 .El
360 .Sh FILE I/O
361 .Bl -hang -width 10n
362 .It Xo
363 .Ft int
364 .Fn open "const char *path" "int flags"
365 .Xc
366 .Pp
367 Similar to the behaviour as specified in
368 .Xr open 2 ,
369 except that file creation is not supported, so the mode parameter is not
370 required.
371 The
372 .Fa flags
373 argument may be one of O_RDONLY, O_WRONLY and O_RDWR (although no file systems
374 currently support writing).
375 .It Xo
376 .Ft int
377 .Fn close "int fd"
378 .Xc
379 .It Xo
380 .Ft void
381 .Fn closeall void
382 .Xc
383 .Pp
384 Close all open files.
385 .It Xo
386 .Ft ssize_t
387 .Fn read "int fd" "void *buf" "size_t len"
388 .Xc
389 .It Xo
390 .Ft ssize_t
391 .Fn write "int fd" "void *buf" "size_t len"
392 .Xc
393 .Pp
394 (No file systems currently support writing.)
395 .It Xo
396 .Ft off_t
397 .Fn lseek "int fd" "off_t offset" "int whence"
398 .Xc
399 .Pp
400 Files being automatically uncompressed during reading cannot seek backwards
401 from the current point.
402 .It Xo
403 .Ft int
404 .Fn stat "const char *path" "struct stat *sb"
405 .Xc
406 .It Xo
407 .Ft int
408 .Fn fstat "int fd" "struct stat *sb"
409 .Xc
410 .Pp
411 The
412 .Fn stat
413 and
414 .Fn fstat
415 functions only fill out the following fields in the
416 .Fa sb
417 structure: st_mode,st_nlink,st_uid,st_gid,st_size.
418 The
419 .Nm tftp
420 file system cannot provide meaningful values for this call, and the
421 .Nm cd9660
422 file system always reports files having uid/gid of zero.
423 .El
424 .Sh PAGER
425 The
426 .Nm
427 library supplies a simple internal pager to ease reading the output of large
428 commands.
429 .Bl -hang -width 10n
430 .It Xo
431 .Ft void
432 .Fn pager_open
433 .Xc
434 .Pp
435 Initialises the pager and tells it that the next line output will be the top of the
436 display.
437 The environment variable LINES is consulted to determine the number of
438 lines to be displayed before pausing.
439 .It Xo
440 .Ft void
441 .Fn pager_close void
442 .Xc
443 .Pp
444 Closes the pager.
445 .It Xo
446 .Ft int
447 .Fn pager_output "const char *lines"
448 .Xc
449 .Pp
450 Sends the lines in the
451 .Dv NUL Ns
452 -terminated buffer at
453 .Fa lines
454 to the pager.
455 Newline characters are counted in order to determine the number
456 of lines being output (wrapped lines are not accounted for).
457 The
458 .Fn pager_output
459 function will return zero when all of the lines have been output, or nonzero
460 if the display was paused and the user elected to quit.
461 .It Xo
462 .Ft int
463 .Fn pager_file "const char *fname"
464 .Xc
465 .Pp
466 Attempts to open and display the file
467 .Fa fname .
468 Returns -1 on error, 0 at EOF, or 1 if the user elects to quit while reading.
469 .El
470 .Sh MISC
471 .Bl -hang -width 10n
472 .It Xo
473 .Ft void
474 .Fn twiddle void
475 .Xc
476 .Pp
477 Successive calls emit the characters in the sequence |,/,-,\\ followed by a
478 backspace in order to provide reassurance to the user.
479 .El
480 .Sh REQUIRED LOW-LEVEL SUPPORT
481 The following resources are consumed by
482 .Nm
483 - stack, heap, console and devices.
484 .Pp
485 The stack must be established before
486 .Nm
487 functions can be invoked.
488 Stack requirements vary depending on the functions
489 and file systems used by the consumer and the support layer functions detailed
490 below.
491 .Pp
492 The heap must be established before calling
493 .Fn alloc
494 or
495 .Fn open
496 by calling
497 .Fn setheap .
498 Heap usage will vary depending on the number of simultaneously open files,
499 as well as client behaviour.
500 Automatic decompression will allocate more
501 than 64K of data per open file.
502 .Pp
503 Console access is performed via the
504 .Fn getchar ,
505 .Fn putchar
506 and
507 .Fn ischar
508 functions detailed below.
509 .Pp
510 Device access is initiated via
511 .Fn devopen
512 and is performed through the
513 .Fn dv_strategy ,
514 .Fn dv_ioctl
515 and
516 .Fn dv_close
517 functions in the device switch structure that
518 .Fn devopen
519 returns.
520 .Pp
521 The consumer must provide the following support functions:
522 .Bl -hang -width 10n
523 .It Xo
524 .Ft int
525 .Fn getchar void
526 .Xc
527 .Pp
528 Return a character from the console, used by
529 .Fn gets ,
530 .Fn ngets
531 and pager functions.
532 .It Xo
533 .Ft int
534 .Fn ischar void
535 .Xc
536 .Pp
537 Returns nonzero if a character is waiting from the console.
538 .It Xo
539 .Ft void
540 .Fn putchar int
541 .Xc
542 .Pp
543 Write a character to the console, used by
544 .Fn gets ,
545 .Fn ngets ,
546 .Fn *printf ,
547 .Fn panic
548 and
549 .Fn twiddle
550 and thus by many other functions for debugging and informational output.
551 .It Xo
552 .Ft int
553 .Fn devopen "struct open_file *of" "const char *name" "const char **file"
554 .Xc
555 .Pp
556 Open the appropriate device for the file named in
557 .Fa name ,
558 returning in
559 .Fa file
560 a pointer to the remaining body of
561 .Fa name
562 which does not refer to the device.
563 The
564 .Va f_dev
565 field in
566 .Fa of
567 will be set to point to the
568 .Vt devsw
569 structure for the opened device if successful.
570 Device identifiers must
571 always precede the path component, but may otherwise be arbitrarily formatted.
572 Used by
573 .Fn open
574 and thus for all device-related I/O.
575 .It Xo
576 .Ft int
577 .Fn devclose "struct open_file *of"
578 .Xc
579 .Pp
580 Close the device allocated for
581 .Fa of .
582 The device driver itself will already have been called for the close; this call
583 should clean up any allocation made by devopen only.
584 .It Xo
585 .Ft void
586 .Fn panic "const char *msg" "..."
587 .Xc
588 .Pp
589 Signal a fatal and unrecoverable error condition.
590 The
591 .Fa msg ...
592 arguments are as for
593 .Fn printf .
594 .El
595 .Sh INTERNAL FILE SYSTEMS
596 Internal file systems are enabled by the consumer exporting the array
597 .Vt struct fs_ops *file_system[] ,
598 which should be initialised with pointers
599 to
600 .Vt struct fs_ops
601 structures.
602 The following file system handlers are supplied by
603 .Nm ,
604 the consumer may supply other file systems of their own:
605 .Bl -hang -width ".Va cd9660_fsops"
606 .It Va ufs_fsops
607 The
608 .Bx
609 UFS.
610 .It Va ext2fs_fsops
611 Linux ext2fs file system.
612 .It Va tftp_fsops
613 File access via TFTP.
614 .It Va nfs_fsops
615 File access via NFS.
616 .It Va cd9660_fsops
617 ISO 9660 (CD-ROM) file system.
618 .It Va gzipfs_fsops
619 Stacked file system supporting gzipped files.
620 When trying the gzipfs file system,
621 .Nm
622 appends
623 .Li .gz
624 to the end of the filename, and then tries to locate the file using the other
625 file systems.
626 Placement of this file system in the
627 .Va file_system[]
628 array determines whether gzipped files will be opened in preference to non-gzipped
629 files.
630 It is only possible to seek a gzipped file forwards, and
631 .Fn stat
632 and
633 .Fn fstat
634 on gzipped files will report an invalid length.
635 .It Va bzipfs_fsops
636 The same as
637 .Va gzipfs_fsops ,
638 but for
639 .Xr bzip2 1 Ns -compressed
640 files.
641 .El
642 .Pp
643 The array of
644 .Vt struct fs_ops
645 pointers should be terminated with a NULL.
646 .Sh DEVICES
647 Devices are exported by the supporting code via the array
648 .Vt struct devsw *devsw[]
649 which is a NULL terminated array of pointers to device switch structures.
650 .Sh HISTORY
651 The
652 .Nm
653 library contains contributions from many sources, including:
654 .Bl -bullet -compact
655 .It
656 .Nm libsa
657 from
658 .Nx
659 .It
660 .Nm libc
661 and
662 .Nm libkern
663 from
664 .Fx 3.0 .
665 .It
666 .Nm zalloc
667 from
668 .An Matthew Dillon Aq dillon@backplane.com
669 .El
670 .Pp
671 The reorganisation and port to
672 .Fx 3.0 ,
673 the environment functions and this manpage were written by
674 .An Mike Smith Aq msmith@FreeBSD.org .
675 .Sh BUGS
676 The lack of detailed memory usage data is unhelpful.