stand.h revision 38506
138451Smsmith/*
238451Smsmith * Copyright (c) 1998 Michael Smith.
338451Smsmith * All rights reserved.
438451Smsmith *
538451Smsmith * Redistribution and use in source and binary forms, with or without
638451Smsmith * modification, are permitted provided that the following conditions
738451Smsmith * are met:
838451Smsmith * 1. Redistributions of source code must retain the above copyright
938451Smsmith *    notice, this list of conditions and the following disclaimer.
1038451Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1138451Smsmith *    notice, this list of conditions and the following disclaimer in the
1238451Smsmith *    documentation and/or other materials provided with the distribution.
1338451Smsmith *
1438451Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1538451Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1638451Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1738451Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1838451Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1938451Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2038451Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2138451Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2238451Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2338451Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2438451Smsmith * SUCH DAMAGE.
2538451Smsmith *
2638506Sbde *	$Id: stand.h,v 1.1.1.1 1998/08/20 08:19:55 msmith Exp $
2738451Smsmith * From	$NetBSD: stand.h,v 1.22 1997/06/26 19:17:40 drochner Exp $
2838451Smsmith */
2938451Smsmith
3038451Smsmith/*-
3138451Smsmith * Copyright (c) 1993
3238451Smsmith *	The Regents of the University of California.  All rights reserved.
3338451Smsmith *
3438451Smsmith * Redistribution and use in source and binary forms, with or without
3538451Smsmith * modification, are permitted provided that the following conditions
3638451Smsmith * are met:
3738451Smsmith * 1. Redistributions of source code must retain the above copyright
3838451Smsmith *    notice, this list of conditions and the following disclaimer.
3938451Smsmith * 2. Redistributions in binary form must reproduce the above copyright
4038451Smsmith *    notice, this list of conditions and the following disclaimer in the
4138451Smsmith *    documentation and/or other materials provided with the distribution.
4238451Smsmith * 3. All advertising materials mentioning features or use of this software
4338451Smsmith *    must display the following acknowledgement:
4438451Smsmith *	This product includes software developed by the University of
4538451Smsmith *	California, Berkeley and its contributors.
4638451Smsmith * 4. Neither the name of the University nor the names of its contributors
4738451Smsmith *    may be used to endorse or promote products derived from this software
4838451Smsmith *    without specific prior written permission.
4938451Smsmith *
5038451Smsmith * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5138451Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5238451Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5338451Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5438451Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5538451Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5638451Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5738451Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5838451Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5938451Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6038451Smsmith * SUCH DAMAGE.
6138451Smsmith *
6238451Smsmith *	@(#)stand.h	8.1 (Berkeley) 6/11/93
6338451Smsmith */
6438451Smsmith
6538451Smsmith#include <sys/types.h>
6638451Smsmith#include <sys/cdefs.h>
6738451Smsmith#include <sys/stat.h>
6838451Smsmith
6938451Smsmith#ifndef NULL
7038451Smsmith#define	NULL	0
7138451Smsmith#endif
7238451Smsmith
7338451Smsmith/* Avoid unwanted userlandish components */
7438451Smsmith#define KERNEL
7538451Smsmith#include <sys/errno.h>
7638451Smsmith#undef KERNEL
7738451Smsmith
7838451Smsmith/* special stand error codes */
7938451Smsmith#define	EADAPT	(ELAST+1)	/* bad adaptor */
8038451Smsmith#define	ECTLR	(ELAST+2)	/* bad controller */
8138451Smsmith#define	EUNIT	(ELAST+3)	/* bad unit */
8238451Smsmith#define ESLICE	(ELAST+4)	/* bad slice */
8338451Smsmith#define	EPART	(ELAST+5)	/* bad partition */
8438451Smsmith#define	ERDLAB	(ELAST+6)	/* can't read disk label */
8538451Smsmith#define	EUNLAB	(ELAST+7)	/* unlabeled disk */
8638451Smsmith#define	EOFFSET	(ELAST+8)	/* relative seek not supported */
8738451Smsmith#define	ESALAST	(ELAST+8)	/* */
8838451Smsmith
8938451Smsmithstruct open_file;
9038451Smsmith
9138451Smsmith/*
9238451Smsmith * This structure is used to define file system operations in a file system
9338451Smsmith * independent way.
9438451Smsmith *
9538451Smsmith * XXX note that filesystem providers should export a pointer to their fs_ops
9638451Smsmith *     struct, so that consumers can reference this and thus include the
9738451Smsmith *     filesystems that they require.
9838451Smsmith */
9938451Smsmithstruct fs_ops {
10038451Smsmith    char	*fs_name;
10138451Smsmith    int		(*fo_open)(char *path, struct open_file *f);
10238451Smsmith    int		(*fo_close)(struct open_file *f);
10338451Smsmith    int		(*fo_read)(struct open_file *f, void *buf,
10438451Smsmith			   size_t size, size_t *resid);
10538451Smsmith    int		(*fo_write)(struct open_file *f, void *buf,
10638451Smsmith			    size_t size, size_t *resid);
10738451Smsmith    off_t	(*fo_seek)(struct open_file *f, off_t offset, int where);
10838451Smsmith    int		(*fo_stat)(struct open_file *f, struct stat *sb);
10938451Smsmith};
11038451Smsmith
11138451Smsmith/*
11238451Smsmith * libstand-supplied filesystems
11338451Smsmith */
11438451Smsmithextern struct fs_ops ufs_fsops;
11538451Smsmithextern struct fs_ops tftp_fsops;
11638451Smsmithextern struct fs_ops nfs_fsops;
11738451Smsmithextern struct fs_ops cd9660_fsops;
11838451Smsmithextern struct fs_ops zipfs_fsops;
11938451Smsmith#ifdef notyet
12038451Smsmithextern struct fs_ops dosfs_fsops;
12138451Smsmith#endif
12238451Smsmith
12338451Smsmith/* where values for lseek(2) */
12438451Smsmith#define	SEEK_SET	0	/* set file offset to offset */
12538451Smsmith#define	SEEK_CUR	1	/* set file offset to current plus offset */
12638451Smsmith#define	SEEK_END	2	/* set file offset to EOF plus offset */
12738451Smsmith
12838451Smsmith/*
12938451Smsmith * Device switch
13038451Smsmith */
13138451Smsmithstruct devsw {
13238451Smsmith    char	dv_name[8];
13338451Smsmith    int		dv_type;		/* opaque type constant, arch-dependant */
13438451Smsmith    int		(*dv_init)(void);	/* early probe call */
13538451Smsmith    int		(*dv_strategy)(void *devdata, int rw, daddr_t blk, size_t size, void *buf, size_t *rsize);
13638451Smsmith    int		(*dv_open)(struct open_file *f, ...);
13738451Smsmith    int		(*dv_close)(struct open_file *f);
13838451Smsmith    int		(*dv_ioctl)(struct open_file *f, u_long cmd, void *data);
13938451Smsmith};
14038451Smsmith
14138451Smsmithextern int errno;
14238451Smsmith
14338451Smsmithstruct open_file {
14438451Smsmith    int			f_flags;	/* see F_* below */
14538451Smsmith    struct devsw	*f_dev;		/* pointer to device operations */
14638451Smsmith    void		*f_devdata;	/* device specific data */
14738451Smsmith    struct fs_ops	*f_ops;		/* pointer to file system operations */
14838451Smsmith    void		*f_fsdata;	/* file system specific data */
14938451Smsmith    off_t		f_offset;	/* current file offset (F_RAW) */
15038451Smsmith};
15138451Smsmith
15238451Smsmith#define	SOPEN_MAX	8
15338451Smsmithextern struct open_file files[];
15438451Smsmith
15538451Smsmith/* f_flags values */
15638451Smsmith#define	F_READ		0x0001	/* file opened for reading */
15738451Smsmith#define	F_WRITE		0x0002	/* file opened for writing */
15838451Smsmith#define	F_RAW		0x0004	/* raw device open - no file system */
15938451Smsmith#define F_NODEV		0x0008	/* network open - no device */
16038451Smsmith
16138451Smsmith#define isupper(c)	((c) >= 'A' && (c) <= 'Z')
16238451Smsmith#define islower(c)	((c) >= 'a' && (c) <= 'z')
16338451Smsmith#define isspace(c)	((c) == ' ' || (c) == '\t')
16438451Smsmith#define isdigit(c)	((c) >= '0' && (c) <= '9')
16538451Smsmith#define isxdigit(c)	(isdigit(c) || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
16638451Smsmith#define isascii(c)	((c) >= 0 || (c <= 0x7f))
16738451Smsmith#define isalpha(c)	(isupper(c) || (islower(c)))
16838451Smsmith#define toupper(c)	((c) - 'a' + 'A')
16938451Smsmith#define tolower(c)	((c) - 'A' + 'a')
17038451Smsmith
17138451Smsmithextern void	setheap(void *, void *);
17238451Smsmithextern void	*malloc(size_t);
17338451Smsmithextern void	free(void *);
17438451Smsmithextern char	*sbrk(int junk);
17538451Smsmith
17638451Smsmith/* disklabel support (undocumented, may be junk) */
17738451Smsmithstruct		disklabel;
17838451Smsmithextern char	*getdisklabel(const char *, struct disklabel *);
17938451Smsmithextern int	dkcksum(struct disklabel *);
18038451Smsmith
18138451Smsmithextern int	printf(const char *fmt, ...);
18238451Smsmithextern void	vprintf(const char *fmt, _BSD_VA_LIST_);
18338451Smsmithextern int	sprintf(char *buf, const char *cfmt, ...);
18438451Smsmith
18538451Smsmithextern void	twiddle(void);
18638451Smsmith
18738451Smsmithextern void	ngets(char *, int);
18838451Smsmith#define gets(x)	ngets((x), 0)
18938451Smsmithextern int	fgetstr(char *buf, int size, int fd);
19038451Smsmith
19138451Smsmithextern char	*strerror(int);
19238451Smsmith
19338451Smsmithextern int	open(const char *, int);
19438451Smsmith#define	O_RDONLY	0x0
19538451Smsmith#define O_WRONLY	0x1			/* writing not (yet?) supported */
19638451Smsmith#define O_RDWR		0x2
19738451Smsmithextern int	close(int);
19838451Smsmithextern void	closeall(void);
19938451Smsmithextern ssize_t	read(int, void *, size_t);
20038451Smsmithextern ssize_t	write(int, void *, size_t);
20138451Smsmithextern off_t	lseek(int, off_t, int);
20238451Smsmithextern int	stat(const char *, struct stat *);
20338451Smsmith
20438451Smsmithextern void	srandom(u_long seed);
20538451Smsmithextern u_long	random(void);
20638451Smsmith
20738451Smsmith/* imports from stdlib, locally modified */
20838451Smsmithextern long	strtol(const char *, char **, int);
20938451Smsmithextern char	*optarg;			/* getopt(3) external variables */
21038451Smsmithextern int	optind, opterr, optopt;
21138451Smsmithextern int	getopt(int, char * const [], const char *);
21238451Smsmith
21338451Smsmith/* pager.c */
21438451Smsmithextern void	pager_open(void);
21538451Smsmithextern void	pager_close(void);
21638451Smsmithextern int	pager_output(const char *lines);
21738451Smsmithextern int	pager_file(char *fname);
21838451Smsmith
21938451Smsmith/* environment.c */
22038451Smsmith#define EV_DYNAMIC	(1<<0)		/* value was dynamically allocated, free if changed/unset */
22138451Smsmith#define EV_VOLATILE	(1<<1)		/* value is volatile, make a copy of it */
22238451Smsmith#define EV_NOHOOK	(1<<2)		/* don't call hook when setting */
22338451Smsmith
22438451Smsmithstruct env_var;
22538451Smsmithtypedef char	*(ev_format_t)(struct env_var *ev);
22638451Smsmithtypedef int	(ev_sethook_t)(struct env_var *ev, int flags, void *value);
22738451Smsmithtypedef int	(ev_unsethook_t)(struct env_var *ev);
22838451Smsmith
22938451Smsmithstruct env_var
23038451Smsmith{
23138451Smsmith    char		*ev_name;
23238451Smsmith    int			ev_flags;
23338451Smsmith    void		*ev_value;
23438451Smsmith    ev_sethook_t	*ev_sethook;
23538451Smsmith    ev_unsethook_t	*ev_unsethook;
23638451Smsmith    struct env_var	*ev_next, *ev_prev;
23738451Smsmith};
23838451Smsmithextern struct env_var	*environ;
23938451Smsmith
24038451Smsmithextern struct env_var	*env_getenv(const char *name);
24138451Smsmithextern int		env_setenv(const char *name, int flags, void *value,
24238451Smsmith				   ev_sethook_t sethook, ev_unsethook_t unsethook);
24338451Smsmithextern char		*getenv(const char *name);
24438451Smsmithextern int		setenv(const char *name, char *value, int overwrite);
24538451Smsmithextern int		putenv(const char *string);
24638451Smsmithextern int		unsetenv(const char *name);
24738451Smsmith
24838451Smsmithextern ev_sethook_t	env_noset;		/* refuse set operation */
24938451Smsmithextern ev_unsethook_t	env_nounset;		/* refuse unset operation */
25038451Smsmith
25138451Smsmith/* BCD conversions (undocumented) */
25238451Smsmithextern u_char const	bcd2bin_data[];
25338451Smsmithextern u_char const	bin2bcd_data[];
25438451Smsmithextern char const	hex2ascii_data[];
25538451Smsmith
25638451Smsmith#define	bcd2bin(bcd)	(bcd2bin_data[bcd])
25738451Smsmith#define	bin2bcd(bin)	(bin2bcd_data[bin])
25838451Smsmith#define	hex2ascii(hex)	(hex2ascii_data[hex])
25938451Smsmith
26038451Smsmith/* min/max (undocumented) */
26138451Smsmithstatic __inline int imax(int a, int b) { return (a > b ? a : b); }
26238451Smsmithstatic __inline int imin(int a, int b) { return (a < b ? a : b); }
26338451Smsmithstatic __inline long lmax(long a, long b) { return (a > b ? a : b); }
26438451Smsmithstatic __inline long lmin(long a, long b) { return (a < b ? a : b); }
26538451Smsmithstatic __inline u_int max(u_int a, u_int b) { return (a > b ? a : b); }
26638451Smsmithstatic __inline u_int min(u_int a, u_int b) { return (a < b ? a : b); }
26738451Smsmithstatic __inline quad_t qmax(quad_t a, quad_t b) { return (a > b ? a : b); }
26838451Smsmithstatic __inline quad_t qmin(quad_t a, quad_t b) { return (a < b ? a : b); }
26938451Smsmithstatic __inline u_long ulmax(u_long a, u_long b) { return (a > b ? a : b); }
27038451Smsmithstatic __inline u_long ulmin(u_long a, u_long b) { return (a < b ? a : b); }
27138451Smsmith
27238451Smsmith/* swaps (undocumented, useful?) */
27338451Smsmith#ifdef __i386__
27438451Smsmithextern u_int32_t	bswap32(u_int32_t x);
27538451Smsmithextern u_int64_t	bswap64(u_int32_t x);
27638451Smsmith#endif
27738451Smsmith
27838451Smsmith/* null functions for device/filesystem switches (undocumented) */
27938451Smsmithextern int	nodev(void);
28038451Smsmithextern int	noioctl(struct open_file *, u_long, void *);
28138451Smsmithextern void	nullsys(void);
28238451Smsmith
28338451Smsmithextern int	null_open(char *path, struct open_file *f);
28438451Smsmithextern int	null_close(struct open_file *f);
28538451Smsmithextern ssize_t	null_read(struct open_file *f, void *buf, size_t size, size_t *resid);
28638451Smsmithextern ssize_t	null_write(struct open_file *f, void *buf, size_t size, size_t *resid);
28738451Smsmithextern off_t	null_seek(struct open_file *f, off_t offset, int where);
28838451Smsmithextern int	null_stat(struct open_file *f, struct stat *sb);
28938451Smsmith
29038451Smsmith/* stuff should be in bootstrap (undocumented) */
29138451Smsmithextern int	getfile(char *prompt, int mode);
29238451Smsmith
29338451Smsmith/*
29438451Smsmith * Machine dependent functions and data, must be provided or stubbed by
29538451Smsmith * the consumer
29638451Smsmith */
29738451Smsmithextern int		getchar(void);
29838451Smsmithextern int		ischar(void);
29938451Smsmithextern void		putchar(int);
30038451Smsmithextern int		devopen(struct open_file *, const char *, char **);
30138451Smsmithextern int		devclose(struct open_file *f);
30238506Sbdeextern void		panic(const char *, ...) __dead2;
30338451Smsmithextern struct fs_ops	*file_system[];
30438451Smsmithextern struct devsw	*devsw[];
30538451Smsmith
306