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