xref: /freebsd/stand/libsa/stand.h (revision bf020787d5f1b4a1274135b49309f7e9ef3955e1)
1ca987d46SWarner Losh /*
2ca987d46SWarner Losh  * Copyright (c) 1998 Michael Smith.
3ca987d46SWarner Losh  * All rights reserved.
4ca987d46SWarner Losh  *
5ca987d46SWarner Losh  * Redistribution and use in source and binary forms, with or without
6ca987d46SWarner Losh  * modification, are permitted provided that the following conditions
7ca987d46SWarner Losh  * are met:
8ca987d46SWarner Losh  * 1. Redistributions of source code must retain the above copyright
9ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer.
10ca987d46SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
11ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
12ca987d46SWarner Losh  *    documentation and/or other materials provided with the distribution.
13ca987d46SWarner Losh  *
14ca987d46SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15ca987d46SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16ca987d46SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17ca987d46SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18ca987d46SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19ca987d46SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20ca987d46SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21ca987d46SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22ca987d46SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23ca987d46SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24ca987d46SWarner Losh  * SUCH DAMAGE.
25ca987d46SWarner Losh  *
26ca987d46SWarner Losh  * $FreeBSD$
27ca987d46SWarner Losh  * From	$NetBSD: stand.h,v 1.22 1997/06/26 19:17:40 drochner Exp $
28ca987d46SWarner Losh  */
29ca987d46SWarner Losh 
30ca987d46SWarner Losh /*-
31ca987d46SWarner Losh  * Copyright (c) 1993
32ca987d46SWarner Losh  *	The Regents of the University of California.  All rights reserved.
33ca987d46SWarner Losh  *
34ca987d46SWarner Losh  * Redistribution and use in source and binary forms, with or without
35ca987d46SWarner Losh  * modification, are permitted provided that the following conditions
36ca987d46SWarner Losh  * are met:
37ca987d46SWarner Losh  * 1. Redistributions of source code must retain the above copyright
38ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer.
39ca987d46SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
40ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
41ca987d46SWarner Losh  *    documentation and/or other materials provided with the distribution.
42ca987d46SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
43ca987d46SWarner Losh  *    may be used to endorse or promote products derived from this software
44ca987d46SWarner Losh  *    without specific prior written permission.
45ca987d46SWarner Losh  *
46ca987d46SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47ca987d46SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48ca987d46SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49ca987d46SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50ca987d46SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51ca987d46SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52ca987d46SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53ca987d46SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54ca987d46SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55ca987d46SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56ca987d46SWarner Losh  * SUCH DAMAGE.
57ca987d46SWarner Losh  *
58ca987d46SWarner Losh  *	@(#)stand.h	8.1 (Berkeley) 6/11/93
59ca987d46SWarner Losh  */
60ca987d46SWarner Losh 
61ca987d46SWarner Losh #ifndef	STAND_H
62ca987d46SWarner Losh #define	STAND_H
63ca987d46SWarner Losh 
64ca987d46SWarner Losh #include <sys/cdefs.h>
655cf20707SWarner Losh 
665cf20707SWarner Losh #include <sys/types.h>
67ca987d46SWarner Losh #include <sys/stat.h>
68ca987d46SWarner Losh #include <sys/dirent.h>
6997cbd5e7SToomas Soome #include <sys/queue.h>
70ca987d46SWarner Losh 
71ca987d46SWarner Losh /* this header intentionally exports NULL from <string.h> */
72ca987d46SWarner Losh #include <string.h>
73c7b46ba4SWarner Losh #define strcoll(a, b)	strcmp((a), (b))
74ca987d46SWarner Losh 
75ca987d46SWarner Losh #define CHK(fmt, args...)	printf("%s(%d): " fmt "\n", __func__, __LINE__ , ##args)
76ca987d46SWarner Losh #define PCHK(fmt, args...)	{printf("%s(%d): " fmt "\n", __func__, __LINE__ , ##args); getchar();}
77ca987d46SWarner Losh 
78ca987d46SWarner Losh #include <sys/errno.h>
79ca987d46SWarner Losh 
80ca987d46SWarner Losh /* special stand error codes */
81ca987d46SWarner Losh #define	EADAPT	(ELAST+1)	/* bad adaptor */
82ca987d46SWarner Losh #define	ECTLR	(ELAST+2)	/* bad controller */
83ca987d46SWarner Losh #define	EUNIT	(ELAST+3)	/* bad unit */
84ca987d46SWarner Losh #define ESLICE	(ELAST+4)	/* bad slice */
85ca987d46SWarner Losh #define	EPART	(ELAST+5)	/* bad partition */
86ca987d46SWarner Losh #define	ERDLAB	(ELAST+6)	/* can't read disk label */
87ca987d46SWarner Losh #define	EUNLAB	(ELAST+7)	/* unlabeled disk */
88ca987d46SWarner Losh #define	EOFFSET	(ELAST+8)	/* relative seek not supported */
89ca987d46SWarner Losh #define	ESALAST	(ELAST+8)	/* */
90ca987d46SWarner Losh 
9149cb0130SWarner Losh /* Partial signal emulation for sig_atomic_t */
9249cb0130SWarner Losh #include <machine/signal.h>
9349cb0130SWarner Losh 
945cf20707SWarner Losh __BEGIN_DECLS
955cf20707SWarner Losh 
96ca987d46SWarner Losh struct open_file;
97ca987d46SWarner Losh 
98ca987d46SWarner Losh /*
99ca987d46SWarner Losh  * This structure is used to define file system operations in a file system
100ca987d46SWarner Losh  * independent way.
101ca987d46SWarner Losh  *
102ca987d46SWarner Losh  * XXX note that filesystem providers should export a pointer to their fs_ops
103ca987d46SWarner Losh  *     struct, so that consumers can reference this and thus include the
104ca987d46SWarner Losh  *     filesystems that they require.
105ca987d46SWarner Losh  */
106ca987d46SWarner Losh struct fs_ops {
107ca987d46SWarner Losh     const char	*fs_name;
108ca987d46SWarner Losh     int		(*fo_open)(const char *path, struct open_file *f);
109ca987d46SWarner Losh     int		(*fo_close)(struct open_file *f);
110ca987d46SWarner Losh     int		(*fo_read)(struct open_file *f, void *buf,
111ca987d46SWarner Losh 			   size_t size, size_t *resid);
1122e7e6fbcSConrad Meyer     int		(*fo_write)(struct open_file *f, const void *buf,
113ca987d46SWarner Losh 			    size_t size, size_t *resid);
114ca987d46SWarner Losh     off_t	(*fo_seek)(struct open_file *f, off_t offset, int where);
115ca987d46SWarner Losh     int		(*fo_stat)(struct open_file *f, struct stat *sb);
116ca987d46SWarner Losh     int		(*fo_readdir)(struct open_file *f, struct dirent *d);
117c25d9affSEmmanuel Vadot     int		(*fo_preload)(struct open_file *f);
118b4cb3fe0SToomas Soome     int		(*fo_mount)(const char *, const char *, void **);
119b4cb3fe0SToomas Soome     int		(*fo_unmount)(const char *, void *);
120ca987d46SWarner Losh };
121ca987d46SWarner Losh 
122ca987d46SWarner Losh /*
123bd001d86SWarner Losh  * libsa-supplied filesystems
124ca987d46SWarner Losh  */
125ca987d46SWarner Losh extern struct fs_ops ufs_fsops;
126ca987d46SWarner Losh extern struct fs_ops tftp_fsops;
127ca987d46SWarner Losh extern struct fs_ops nfs_fsops;
128ca987d46SWarner Losh extern struct fs_ops cd9660_fsops;
129ca987d46SWarner Losh extern struct fs_ops gzipfs_fsops;
130ca987d46SWarner Losh extern struct fs_ops bzipfs_fsops;
131ca987d46SWarner Losh extern struct fs_ops dosfs_fsops;
132ca987d46SWarner Losh extern struct fs_ops ext2fs_fsops;
133ca987d46SWarner Losh extern struct fs_ops splitfs_fsops;
134ca987d46SWarner Losh extern struct fs_ops pkgfs_fsops;
135da4961c7SRebecca Cran extern struct fs_ops efihttp_fsops;
136ca987d46SWarner Losh 
137ca987d46SWarner Losh /* where values for lseek(2) */
138ca987d46SWarner Losh #define	SEEK_SET	0	/* set file offset to offset */
139ca987d46SWarner Losh #define	SEEK_CUR	1	/* set file offset to current plus offset */
140ca987d46SWarner Losh #define	SEEK_END	2	/* set file offset to EOF plus offset */
141ca987d46SWarner Losh 
142ca987d46SWarner Losh /*
143ca987d46SWarner Losh  * Device switch
144ca987d46SWarner Losh  */
1454932a6e4SWarner Losh #define DEV_NAMLEN	8		/* Length of name of device class */
1464d4b1a29SWarner Losh #define DEV_DEVLEN	128		/* Length of longest device instance name */
1474d4b1a29SWarner Losh struct devdesc;
148ca987d46SWarner Losh struct devsw {
1494932a6e4SWarner Losh     const char	dv_name[DEV_NAMLEN];
150079f02e8SWarner Losh     int		dv_type;		/* opaque type constant */
151b3a2aad1SWarner Losh #define DEVT_NONE	0
152b3a2aad1SWarner Losh #define DEVT_DISK	1
153b3a2aad1SWarner Losh #define DEVT_NET	2
154b3a2aad1SWarner Losh #define DEVT_CD		3
155b3a2aad1SWarner Losh #define DEVT_ZFS	4
156b3a2aad1SWarner Losh #define DEVT_FD		5
157ca987d46SWarner Losh     int		(*dv_init)(void);	/* early probe call */
158ca987d46SWarner Losh     int		(*dv_strategy)(void *devdata, int rw, daddr_t blk,
159ca987d46SWarner Losh 			size_t size, char *buf, size_t *rsize);
160ca987d46SWarner Losh     int		(*dv_open)(struct open_file *f, ...);
161ca987d46SWarner Losh     int		(*dv_close)(struct open_file *f);
162ca987d46SWarner Losh     int		(*dv_ioctl)(struct open_file *f, u_long cmd, void *data);
163ca987d46SWarner Losh     int		(*dv_print)(int verbose);	/* print device information */
164ca987d46SWarner Losh     void	(*dv_cleanup)(void);
1654d4b1a29SWarner Losh     char *	(*dv_fmtdev)(struct devdesc *);
166a0aad69fSWarner Losh     int		(*dv_parsedev)(struct devdesc **, const char *, const char **);
167a07cef5aSWarner Losh     bool	(*dv_match)(struct devsw *, const char *);
168ca987d46SWarner Losh };
169ca987d46SWarner Losh 
170ca987d46SWarner Losh /*
171bd001d86SWarner Losh  * libsa-supplied device switch
172ca987d46SWarner Losh  */
173ca987d46SWarner Losh extern struct devsw netdev;
174ca987d46SWarner Losh 
175ca987d46SWarner Losh extern int errno;
176ca987d46SWarner Losh 
177ca987d46SWarner Losh /*
178269865a8SWarner Losh  * Generic device specifier; architecture-dependent versions may be larger, but
179269865a8SWarner Losh  * should be allowed to overlap. The larger device specifiers store more data
180269865a8SWarner Losh  * than can fit in the generic one that's gleaned after parsing the device
181269865a8SWarner Losh  * string, or used in some cases to indicate wildcards that match a variety of
182269865a8SWarner Losh  * situations based on what's on the drive itself rather than what the progammer
183269865a8SWarner Losh  * might know in advance. Information about open files is stored in d_opendata,
184269865a8SWarner Losh  * though what's passed into the open routine may differ from what's present
185269865a8SWarner Losh  * after the open on some configurations.
186ca987d46SWarner Losh  */
187b3a2aad1SWarner Losh struct devdesc {
188ca987d46SWarner Losh     struct devsw	*d_dev;
189ca987d46SWarner Losh     int			d_unit;
190ca987d46SWarner Losh     void		*d_opendata;
191ca987d46SWarner Losh };
192ca987d46SWarner Losh 
1934d4b1a29SWarner Losh char *devformat(struct devdesc *d);
194781ca0afSWarner Losh int devparse(struct devdesc **, const char *, const char **);
19566012c8fSWarner Losh int devinit(void);
196*bf020787SWarner Losh void	dev_cleanup(void);
1974d4b1a29SWarner Losh 
198ca987d46SWarner Losh struct open_file {
199ca987d46SWarner Losh     int			f_flags;	/* see F_* below */
200ca987d46SWarner Losh     struct devsw	*f_dev;		/* pointer to device operations */
201ca987d46SWarner Losh     void		*f_devdata;	/* device specific data */
202ca987d46SWarner Losh     struct fs_ops	*f_ops;		/* pointer to file system operations */
203ca987d46SWarner Losh     void		*f_fsdata;	/* file system specific data */
204ca987d46SWarner Losh     off_t		f_offset;	/* current file offset */
205ca987d46SWarner Losh     char		*f_rabuf;	/* readahead buffer pointer */
206ca987d46SWarner Losh     size_t		f_ralen;	/* valid data in readahead buffer */
207ca987d46SWarner Losh     off_t		f_raoffset;	/* consumer offset in readahead buffer */
20897cbd5e7SToomas Soome     int			f_id;		/* file number */
20997cbd5e7SToomas Soome     TAILQ_ENTRY(open_file) f_link;	/* next entry */
210ca987d46SWarner Losh #define SOPEN_RASIZE	512
211ca987d46SWarner Losh };
212ca987d46SWarner Losh 
21397cbd5e7SToomas Soome typedef TAILQ_HEAD(file_list, open_file) file_list_t;
21497cbd5e7SToomas Soome extern file_list_t files;
21597cbd5e7SToomas Soome extern struct open_file *fd2open_file(int);
216ca987d46SWarner Losh 
217ca987d46SWarner Losh /* f_flags values */
218ca987d46SWarner Losh #define	F_READ		0x0001	/* file opened for reading */
219ca987d46SWarner Losh #define	F_WRITE		0x0002	/* file opened for writing */
220ca987d46SWarner Losh #define	F_RAW		0x0004	/* raw device open - no file system */
221ca987d46SWarner Losh #define F_NODEV		0x0008	/* network open - no device */
222ca987d46SWarner Losh #define	F_MASK		0xFFFF
223ca987d46SWarner Losh /* Mode modifier for strategy() */
224ca987d46SWarner Losh #define	F_NORA		(0x01 << 16)	/* Disable Read-Ahead */
225ca987d46SWarner Losh 
226ca987d46SWarner Losh #define isascii(c)	(((c) & ~0x7F) == 0)
227ca987d46SWarner Losh 
228ca987d46SWarner Losh static __inline int isupper(int c)
229ca987d46SWarner Losh {
230ca987d46SWarner Losh     return c >= 'A' && c <= 'Z';
231ca987d46SWarner Losh }
232ca987d46SWarner Losh 
233ca987d46SWarner Losh static __inline int islower(int c)
234ca987d46SWarner Losh {
235ca987d46SWarner Losh     return c >= 'a' && c <= 'z';
236ca987d46SWarner Losh }
237ca987d46SWarner Losh 
238ca987d46SWarner Losh static __inline int isspace(int c)
239ca987d46SWarner Losh {
240ca987d46SWarner Losh     return c == ' ' || (c >= 0x9 && c <= 0xd);
241ca987d46SWarner Losh }
242ca987d46SWarner Losh 
243ca987d46SWarner Losh static __inline int isdigit(int c)
244ca987d46SWarner Losh {
245ca987d46SWarner Losh     return c >= '0' && c <= '9';
246ca987d46SWarner Losh }
247ca987d46SWarner Losh 
248ca987d46SWarner Losh static __inline int isxdigit(int c)
249ca987d46SWarner Losh {
250ca987d46SWarner Losh     return isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
251ca987d46SWarner Losh }
252ca987d46SWarner Losh 
253ca987d46SWarner Losh static __inline int isalpha(int c)
254ca987d46SWarner Losh {
255ca987d46SWarner Losh     return isupper(c) || islower(c);
256ca987d46SWarner Losh }
257ca987d46SWarner Losh 
258ca987d46SWarner Losh static __inline int isalnum(int c)
259ca987d46SWarner Losh {
260ca987d46SWarner Losh     return isalpha(c) || isdigit(c);
261ca987d46SWarner Losh }
262ca987d46SWarner Losh 
2636856cf68SWarner Losh static __inline int iscntrl(int c)
2646856cf68SWarner Losh {
2656856cf68SWarner Losh 	return (c >= 0 && c < ' ') || c == 127;
2666856cf68SWarner Losh }
2676856cf68SWarner Losh 
2686856cf68SWarner Losh static __inline int isgraph(int c)
2696856cf68SWarner Losh {
2706856cf68SWarner Losh 	return c >= '!' && c <= '~';
2716856cf68SWarner Losh }
2726856cf68SWarner Losh 
2736856cf68SWarner Losh static __inline int ispunct(int c)
2746856cf68SWarner Losh {
2756856cf68SWarner Losh 	return (c >= '!' && c <= '/') || (c >= ':' && c <= '@') ||
2766856cf68SWarner Losh 	    (c >= '[' && c <= '`') || (c >= '{' && c <= '~');
2776856cf68SWarner Losh }
2786856cf68SWarner Losh 
279ca987d46SWarner Losh static __inline int toupper(int c)
280ca987d46SWarner Losh {
281ca987d46SWarner Losh     return islower(c) ? c - 'a' + 'A' : c;
282ca987d46SWarner Losh }
283ca987d46SWarner Losh 
284ca987d46SWarner Losh static __inline int tolower(int c)
285ca987d46SWarner Losh {
286ca987d46SWarner Losh     return isupper(c) ? c - 'A' + 'a' : c;
287ca987d46SWarner Losh }
288ca987d46SWarner Losh 
289ca987d46SWarner Losh /* sbrk emulation */
290ca987d46SWarner Losh extern void	setheap(void *base, void *top);
291ca987d46SWarner Losh extern char	*sbrk(int incr);
292ca987d46SWarner Losh 
293ca987d46SWarner Losh extern int	printf(const char *fmt, ...) __printflike(1, 2);
2945e84b578SToomas Soome extern int	asprintf(char **buf, const char *cfmt, ...) __printflike(2, 3);
295ca987d46SWarner Losh extern int	sprintf(char *buf, const char *cfmt, ...) __printflike(2, 3);
296ca987d46SWarner Losh extern int	snprintf(char *buf, size_t size, const char *cfmt, ...) __printflike(3, 4);
2973e9c7874SIan Lepore extern int	vprintf(const char *fmt, __va_list);
2983e9c7874SIan Lepore extern int	vsprintf(char *buf, const char *cfmt, __va_list);
2993e9c7874SIan Lepore extern int	vsnprintf(char *buf, size_t size, const char *cfmt, __va_list);
300ca987d46SWarner Losh 
301ca987d46SWarner Losh extern void	twiddle(u_int callerdiv);
302ca987d46SWarner Losh extern void	twiddle_divisor(u_int globaldiv);
303ca987d46SWarner Losh 
304ca987d46SWarner Losh extern void	ngets(char *, int);
305ca987d46SWarner Losh #define gets(x)	ngets((x), 0)
306ca987d46SWarner Losh extern int	fgetstr(char *buf, int size, int fd);
307ca987d46SWarner Losh 
308b4cb3fe0SToomas Soome extern int	mount(const char *dev, const char *path, int flags, void *data);
309b4cb3fe0SToomas Soome extern int	unmount(const char *dev, int flags);
310ca987d46SWarner Losh extern int	open(const char *, int);
311ca987d46SWarner Losh #define	O_RDONLY	0x0
312ca987d46SWarner Losh #define O_WRONLY	0x1
313ca987d46SWarner Losh #define O_RDWR		0x2
3142ef9ff7dSSimon J. Gerraty #define O_ACCMODE	0x3
31582c85a42SKyle Evans /* NOT IMPLEMENTED */
31682c85a42SKyle Evans #define	O_CREAT		0x0200		/* create if nonexistent */
31782c85a42SKyle Evans #define	O_TRUNC		0x0400		/* truncate to zero length */
318ca987d46SWarner Losh extern int	close(int);
319ca987d46SWarner Losh extern void	closeall(void);
320ca987d46SWarner Losh extern ssize_t	read(int, void *, size_t);
321061577c5SConrad Meyer extern ssize_t	write(int, const void *, size_t);
3222e1e68cbSWarner Losh extern int	ioctl(int, u_long, void *);
323ca987d46SWarner Losh extern struct	dirent *readdirfd(int);
324c25d9affSEmmanuel Vadot extern void	preload(int);
325ca987d46SWarner Losh 
326dcaa2d76SWarner Losh extern void	srandom(unsigned int);
327e294a126SWarner Losh extern long	random(void);
328ca987d46SWarner Losh 
329ca987d46SWarner Losh /* imports from stdlib, locally modified */
330ca987d46SWarner Losh extern char	*optarg;			/* getopt(3) external variables */
331ca987d46SWarner Losh extern int	optind, opterr, optopt, optreset;
332ca987d46SWarner Losh extern int	getopt(int, char * const [], const char *);
333ca987d46SWarner Losh 
334ca987d46SWarner Losh /* pager.c */
335ca987d46SWarner Losh extern void	pager_open(void);
336ca987d46SWarner Losh extern void	pager_close(void);
337ca987d46SWarner Losh extern int	pager_output(const char *lines);
338ca987d46SWarner Losh extern int	pager_file(const char *fname);
339ca987d46SWarner Losh 
340ca987d46SWarner Losh /* No signal state to preserve */
341ca987d46SWarner Losh #define setjmp	_setjmp
342ca987d46SWarner Losh #define longjmp	_longjmp
343ca987d46SWarner Losh 
344ca987d46SWarner Losh /* environment.c */
345ca987d46SWarner Losh #define EV_DYNAMIC	(1<<0)		/* value was dynamically allocated, free if changed/unset */
346ca987d46SWarner Losh #define EV_VOLATILE	(1<<1)		/* value is volatile, make a copy of it */
347ca987d46SWarner Losh #define EV_NOHOOK	(1<<2)		/* don't call hook when setting */
348ca987d46SWarner Losh 
349ca987d46SWarner Losh struct env_var;
350ca987d46SWarner Losh typedef char	*(ev_format_t)(struct env_var *ev);
351ca987d46SWarner Losh typedef int	(ev_sethook_t)(struct env_var *ev, int flags,
352ca987d46SWarner Losh 		    const void *value);
353ca987d46SWarner Losh typedef int	(ev_unsethook_t)(struct env_var *ev);
354ca987d46SWarner Losh 
355ca987d46SWarner Losh struct env_var
356ca987d46SWarner Losh {
357ca987d46SWarner Losh     char		*ev_name;
358ca987d46SWarner Losh     int			ev_flags;
359ca987d46SWarner Losh     void		*ev_value;
360ca987d46SWarner Losh     ev_sethook_t	*ev_sethook;
361ca987d46SWarner Losh     ev_unsethook_t	*ev_unsethook;
362ca987d46SWarner Losh     struct env_var	*ev_next, *ev_prev;
363ca987d46SWarner Losh };
364ca987d46SWarner Losh extern struct env_var	*environ;
365ca987d46SWarner Losh 
366ca987d46SWarner Losh extern struct env_var	*env_getenv(const char *name);
367ca987d46SWarner Losh extern int		env_setenv(const char *name, int flags,
368ca987d46SWarner Losh 				   const void *value, ev_sethook_t sethook,
369ca987d46SWarner Losh 				   ev_unsethook_t unsethook);
370588f0a1eSToomas Soome extern void		env_discard(struct env_var *);
371ca987d46SWarner Losh extern char		*getenv(const char *name);
372ca987d46SWarner Losh extern int		setenv(const char *name, const char *value,
373ca987d46SWarner Losh 			       int overwrite);
374e8e6a5f9SWarner Losh extern int		putenv(char *string);
375ca987d46SWarner Losh extern int		unsetenv(const char *name);
376ca987d46SWarner Losh 
377ca987d46SWarner Losh extern ev_sethook_t	env_noset;		/* refuse set operation */
378ca987d46SWarner Losh extern ev_unsethook_t	env_nounset;		/* refuse unset operation */
379ca987d46SWarner Losh 
3802b0268cfSWarner Losh /* stdlib.h routines */
38124dfa658SWarner Losh extern int		abs(int a);
38230883627SWarner Losh extern void		abort(void) __dead2;
3833a8a081bSWarner Losh extern long		strtol(const char * __restrict, char ** __restrict, int);
3843a8a081bSWarner Losh extern long long	strtoll(const char * __restrict, char ** __restrict, int);
3853a8a081bSWarner Losh extern unsigned long	strtoul(const char * __restrict, char ** __restrict, int);
3863a8a081bSWarner Losh extern unsigned long long strtoull(const char * __restrict, char ** __restrict, int);
3872b0268cfSWarner Losh 
388ca987d46SWarner Losh /* BCD conversions (undocumented) */
389ca987d46SWarner Losh extern u_char const	bcd2bin_data[];
390ca987d46SWarner Losh extern u_char const	bin2bcd_data[];
391ca987d46SWarner Losh extern char const	hex2ascii_data[];
392ca987d46SWarner Losh 
393ca987d46SWarner Losh #define	bcd2bin(bcd)	(bcd2bin_data[bcd])
394ca987d46SWarner Losh #define	bin2bcd(bin)	(bin2bcd_data[bin])
395ca987d46SWarner Losh #define	hex2ascii(hex)	(hex2ascii_data[hex])
396b6f4732cSIan Lepore #define	validbcd(bcd)	(bcd == 0 || (bcd > 0 && bcd <= 0x99 && bcd2bin_data[bcd] != 0))
397ca987d46SWarner Losh 
398ca987d46SWarner Losh /* min/max (undocumented) */
399ca987d46SWarner Losh static __inline int imax(int a, int b) { return (a > b ? a : b); }
400ca987d46SWarner Losh static __inline int imin(int a, int b) { return (a < b ? a : b); }
401ca987d46SWarner Losh static __inline long lmax(long a, long b) { return (a > b ? a : b); }
402ca987d46SWarner Losh static __inline long lmin(long a, long b) { return (a < b ? a : b); }
403ca987d46SWarner Losh static __inline u_int max(u_int a, u_int b) { return (a > b ? a : b); }
404ca987d46SWarner Losh static __inline u_int min(u_int a, u_int b) { return (a < b ? a : b); }
405ca987d46SWarner Losh static __inline quad_t qmax(quad_t a, quad_t b) { return (a > b ? a : b); }
406ca987d46SWarner Losh static __inline quad_t qmin(quad_t a, quad_t b) { return (a < b ? a : b); }
407ca987d46SWarner Losh static __inline u_long ulmax(u_long a, u_long b) { return (a > b ? a : b); }
408ca987d46SWarner Losh static __inline u_long ulmin(u_long a, u_long b) { return (a < b ? a : b); }
409ca987d46SWarner Losh 
410ca987d46SWarner Losh /* null functions for device/filesystem switches (undocumented) */
411ca987d46SWarner Losh extern int	nodev(void);
412ca987d46SWarner Losh extern int	noioctl(struct open_file *, u_long, void *);
413ca987d46SWarner Losh extern void	nullsys(void);
414ca987d46SWarner Losh 
415ca987d46SWarner Losh extern int	null_open(const char *path, struct open_file *f);
416ca987d46SWarner Losh extern int	null_close(struct open_file *f);
417ca987d46SWarner Losh extern int	null_read(struct open_file *f, void *buf, size_t size, size_t *resid);
4182e7e6fbcSConrad Meyer extern int	null_write(struct open_file *f, const void *buf, size_t size, size_t *resid);
419ca987d46SWarner Losh extern off_t	null_seek(struct open_file *f, off_t offset, int where);
420ca987d46SWarner Losh extern int	null_stat(struct open_file *f, struct stat *sb);
421ca987d46SWarner Losh extern int	null_readdir(struct open_file *f, struct dirent *d);
422ca987d46SWarner Losh 
423ca987d46SWarner Losh 
424ca987d46SWarner Losh /*
425ca987d46SWarner Losh  * Machine dependent functions and data, must be provided or stubbed by
426ca987d46SWarner Losh  * the consumer
427ca987d46SWarner Losh  */
42886bb84d5SWarner Losh extern void		exit(int) __dead2;
429ca987d46SWarner Losh extern int		getchar(void);
430ca987d46SWarner Losh extern int		ischar(void);
431ca987d46SWarner Losh extern void		putchar(int);
432ca987d46SWarner Losh extern int		devopen(struct open_file *, const char *, const char **);
433ca987d46SWarner Losh extern int		devclose(struct open_file *f);
434ca987d46SWarner Losh extern void		panic(const char *, ...) __dead2 __printflike(1, 2);
43586bb84d5SWarner Losh extern void		panic_action(void) __weak_symbol __dead2;
43631d05586SWarner Losh extern time_t		getsecs(void);
437ca987d46SWarner Losh extern struct fs_ops	*file_system[];
438ca987d46SWarner Losh extern struct fs_ops	*exclusive_file_system;
439ca987d46SWarner Losh extern struct devsw	*devsw[];
440ca987d46SWarner Losh 
441ca987d46SWarner Losh /*
442b9c5b432SWarner Losh  * Time routines
443b9c5b432SWarner Losh  */
444b9c5b432SWarner Losh time_t time(time_t *);
445b9c5b432SWarner Losh 
446b9c5b432SWarner Losh /*
447ca987d46SWarner Losh  * Expose byteorder(3) functions.
448ca987d46SWarner Losh  */
449ca987d46SWarner Losh #ifndef _BYTEORDER_PROTOTYPED
450ca987d46SWarner Losh #define	_BYTEORDER_PROTOTYPED
451ca987d46SWarner Losh extern uint32_t		htonl(uint32_t);
452ca987d46SWarner Losh extern uint16_t		htons(uint16_t);
453ca987d46SWarner Losh extern uint32_t		ntohl(uint32_t);
454ca987d46SWarner Losh extern uint16_t		ntohs(uint16_t);
455ca987d46SWarner Losh #endif
456ca987d46SWarner Losh 
457ca987d46SWarner Losh #ifndef _BYTEORDER_FUNC_DEFINED
458ca987d46SWarner Losh #define	_BYTEORDER_FUNC_DEFINED
459ca987d46SWarner Losh #define	htonl(x)	__htonl(x)
460ca987d46SWarner Losh #define	htons(x)	__htons(x)
461ca987d46SWarner Losh #define	ntohl(x)	__ntohl(x)
462ca987d46SWarner Losh #define	ntohs(x)	__ntohs(x)
463ca987d46SWarner Losh #endif
464ca987d46SWarner Losh 
465ca987d46SWarner Losh void *Malloc(size_t, const char *, int);
46611db1a16SToomas Soome void *Memalign(size_t, size_t, const char *, int);
467ca987d46SWarner Losh void *Calloc(size_t, size_t, const char *, int);
468ca987d46SWarner Losh void *Realloc(void *, size_t, const char *, int);
46911db1a16SToomas Soome void *Reallocf(void *, size_t, const char *, int);
470ca987d46SWarner Losh void Free(void *, const char *, int);
47162ea4c11SToomas Soome extern void	mallocstats(void);
472ca987d46SWarner Losh 
473e9b148a3SSimon J. Gerraty const char *x86_hypervisor(void);
474e9b148a3SSimon J. Gerraty 
475c907ec9eSToomas Soome #ifdef USER_MALLOC
476c907ec9eSToomas Soome extern void *malloc(size_t);
477c907ec9eSToomas Soome extern void *memalign(size_t, size_t);
478c907ec9eSToomas Soome extern void *calloc(size_t, size_t);
479c907ec9eSToomas Soome extern void free(void *);
480c907ec9eSToomas Soome extern void *realloc(void *, size_t);
481c907ec9eSToomas Soome extern void *reallocf(void *, size_t);
4825292d0a7SToomas Soome #elif defined(DEBUG_MALLOC)
483ca987d46SWarner Losh #define malloc(x)	Malloc(x, __FILE__, __LINE__)
48411db1a16SToomas Soome #define memalign(x, y)	Memalign(x, y, __FILE__, __LINE__)
485ca987d46SWarner Losh #define calloc(x, y)	Calloc(x, y, __FILE__, __LINE__)
486ca987d46SWarner Losh #define free(x)		Free(x, __FILE__, __LINE__)
487ca987d46SWarner Losh #define realloc(x, y)	Realloc(x, y, __FILE__, __LINE__)
48862ea4c11SToomas Soome #define reallocf(x, y)	Reallocf(x, y, __FILE__, __LINE__)
489ca987d46SWarner Losh #else
490ca987d46SWarner Losh #define malloc(x)	Malloc(x, NULL, 0)
49111db1a16SToomas Soome #define memalign(x, y)	Memalign(x, y, NULL, 0)
492ca987d46SWarner Losh #define calloc(x, y)	Calloc(x, y, NULL, 0)
493ca987d46SWarner Losh #define free(x)		Free(x, NULL, 0)
494ca987d46SWarner Losh #define realloc(x, y)	Realloc(x, y, NULL, 0)
49562ea4c11SToomas Soome #define reallocf(x, y)	Reallocf(x, y, NULL, 0)
496ca987d46SWarner Losh #endif
497ca987d46SWarner Losh 
498ed2a6576SWarner Losh /*
499ed2a6576SWarner Losh  * va <-> pa routines. MD code must supply.
500ed2a6576SWarner Losh  */
501ed2a6576SWarner Losh caddr_t ptov(uintptr_t);
502ed2a6576SWarner Losh 
503ed19b7c5SSimon J. Gerraty /* hexdump.c */
504ed19b7c5SSimon J. Gerraty void	hexdump(caddr_t region, size_t len);
505ed19b7c5SSimon J. Gerraty 
506e193d3baSColin Percival /* tslog.c */
507e193d3baSColin Percival #define TSRAW(a, b, c) tslog(a, b, c)
508e193d3baSColin Percival #define TSENTER() TSRAW("ENTER", __func__, NULL)
50901cad731SEmmanuel Vadot #define TSENTER2(x) TSRAW("ENTER", __func__, x)
510e193d3baSColin Percival #define TSEXIT() TSRAW("EXIT", __func__, NULL)
511e193d3baSColin Percival #define TSLINE() TSRAW("EVENT", __FILE__, __XSTRING(__LINE__))
512e193d3baSColin Percival void tslog(const char *, const char *, const char *);
513e193d3baSColin Percival void tslog_setbuf(void * buf, size_t len);
514e193d3baSColin Percival void tslog_getbuf(void ** buf, size_t * len);
515e193d3baSColin Percival 
5165cf20707SWarner Losh __END_DECLS
5175cf20707SWarner Losh 
518ca987d46SWarner Losh #endif	/* STAND_H */
519