stand.h revision 40774
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 *
2640774Smsmith *	$Id: stand.h,v 1.8 1998/10/19 09:08:40 dfr 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
6939665Smsmith#define CHK(fmt, args...)	printf("%s(%d): " fmt "\n", __FUNCTION__, __LINE__ , ##args)
7039665Smsmith#define PCHK(fmt, args...)	{printf("%s(%d): " fmt "\n", __FUNCTION__, __LINE__ , ##args); getchar();}
7139665Smsmith
7238451Smsmith#ifndef NULL
7338451Smsmith#define	NULL	0
7438451Smsmith#endif
7538451Smsmith
7638451Smsmith/* Avoid unwanted userlandish components */
7738451Smsmith#define KERNEL
7838451Smsmith#include <sys/errno.h>
7938451Smsmith#undef KERNEL
8038451Smsmith
8138451Smsmith/* special stand error codes */
8238451Smsmith#define	EADAPT	(ELAST+1)	/* bad adaptor */
8338451Smsmith#define	ECTLR	(ELAST+2)	/* bad controller */
8438451Smsmith#define	EUNIT	(ELAST+3)	/* bad unit */
8538451Smsmith#define ESLICE	(ELAST+4)	/* bad slice */
8638451Smsmith#define	EPART	(ELAST+5)	/* bad partition */
8738451Smsmith#define	ERDLAB	(ELAST+6)	/* can't read disk label */
8838451Smsmith#define	EUNLAB	(ELAST+7)	/* unlabeled disk */
8938451Smsmith#define	EOFFSET	(ELAST+8)	/* relative seek not supported */
9038451Smsmith#define	ESALAST	(ELAST+8)	/* */
9138451Smsmith
9238451Smsmithstruct open_file;
9338451Smsmith
9438451Smsmith/*
9538451Smsmith * This structure is used to define file system operations in a file system
9638451Smsmith * independent way.
9738451Smsmith *
9838451Smsmith * XXX note that filesystem providers should export a pointer to their fs_ops
9938451Smsmith *     struct, so that consumers can reference this and thus include the
10038451Smsmith *     filesystems that they require.
10138451Smsmith */
10238451Smsmithstruct fs_ops {
10339468Smsmith    const char	*fs_name;
10439468Smsmith    int		(*fo_open)(const char *path, struct open_file *f);
10538451Smsmith    int		(*fo_close)(struct open_file *f);
10638451Smsmith    int		(*fo_read)(struct open_file *f, void *buf,
10738451Smsmith			   size_t size, size_t *resid);
10838451Smsmith    int		(*fo_write)(struct open_file *f, void *buf,
10938451Smsmith			    size_t size, size_t *resid);
11038451Smsmith    off_t	(*fo_seek)(struct open_file *f, off_t offset, int where);
11138451Smsmith    int		(*fo_stat)(struct open_file *f, struct stat *sb);
11238451Smsmith};
11338451Smsmith
11438451Smsmith/*
11538451Smsmith * libstand-supplied filesystems
11638451Smsmith */
11738451Smsmithextern struct fs_ops ufs_fsops;
11838451Smsmithextern struct fs_ops tftp_fsops;
11938451Smsmithextern struct fs_ops nfs_fsops;
12038451Smsmithextern struct fs_ops cd9660_fsops;
12138451Smsmithextern struct fs_ops zipfs_fsops;
12238451Smsmithextern struct fs_ops dosfs_fsops;
12338451Smsmith
12438451Smsmith/* where values for lseek(2) */
12538451Smsmith#define	SEEK_SET	0	/* set file offset to offset */
12638451Smsmith#define	SEEK_CUR	1	/* set file offset to current plus offset */
12738451Smsmith#define	SEEK_END	2	/* set file offset to EOF plus offset */
12838451Smsmith
12938451Smsmith/*
13038451Smsmith * Device switch
13138451Smsmith */
13238451Smsmithstruct devsw {
13339468Smsmith    const char	dv_name[8];
13438451Smsmith    int		dv_type;		/* opaque type constant, arch-dependant */
13538451Smsmith    int		(*dv_init)(void);	/* early probe call */
13638451Smsmith    int		(*dv_strategy)(void *devdata, int rw, daddr_t blk, size_t size, void *buf, size_t *rsize);
13738451Smsmith    int		(*dv_open)(struct open_file *f, ...);
13838451Smsmith    int		(*dv_close)(struct open_file *f);
13938451Smsmith    int		(*dv_ioctl)(struct open_file *f, u_long cmd, void *data);
14040774Smsmith    void	(*dv_print)(int verbose);	/* print device information */
14138451Smsmith};
14238451Smsmith
14338451Smsmithextern int errno;
14438451Smsmith
14538451Smsmithstruct open_file {
14638451Smsmith    int			f_flags;	/* see F_* below */
14738451Smsmith    struct devsw	*f_dev;		/* pointer to device operations */
14838451Smsmith    void		*f_devdata;	/* device specific data */
14938451Smsmith    struct fs_ops	*f_ops;		/* pointer to file system operations */
15038451Smsmith    void		*f_fsdata;	/* file system specific data */
15138451Smsmith    off_t		f_offset;	/* current file offset (F_RAW) */
15238451Smsmith};
15338451Smsmith
15438451Smsmith#define	SOPEN_MAX	8
15538451Smsmithextern struct open_file files[];
15638451Smsmith
15738451Smsmith/* f_flags values */
15838451Smsmith#define	F_READ		0x0001	/* file opened for reading */
15938451Smsmith#define	F_WRITE		0x0002	/* file opened for writing */
16038451Smsmith#define	F_RAW		0x0004	/* raw device open - no file system */
16138451Smsmith#define F_NODEV		0x0008	/* network open - no device */
16238451Smsmith
16338451Smsmith#define isupper(c)	((c) >= 'A' && (c) <= 'Z')
16438451Smsmith#define islower(c)	((c) >= 'a' && (c) <= 'z')
16538451Smsmith#define isspace(c)	((c) == ' ' || (c) == '\t')
16638451Smsmith#define isdigit(c)	((c) >= '0' && (c) <= '9')
16738451Smsmith#define isxdigit(c)	(isdigit(c) || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
16838451Smsmith#define isascii(c)	((c) >= 0 || (c <= 0x7f))
16938451Smsmith#define isalpha(c)	(isupper(c) || (islower(c)))
17038451Smsmith#define toupper(c)	((c) - 'a' + 'A')
17138451Smsmith#define tolower(c)	((c) - 'A' + 'a')
17238451Smsmith
17339665Smsmith/* sbrk emulation */
17439665Smsmithextern void	setheap(void *base, void *top);
17539665Smsmithextern char	*sbrk(int incr);
17638451Smsmith
17739665Smsmith/* Matt Dillon's zalloc/zmalloc */
17839665Smsmithextern void	*malloc(size_t bytes);
17939665Smsmithextern void	free(void *ptr);
18039665Smsmith/*#define free(p)	{CHK("free %p", p); free(p);} */ /* use for catching guard violations */
18139665Smsmithextern void	*calloc(size_t n1, size_t n2);
18239665Smsmithextern void	*realloc(void *ptr, size_t size);
18339665Smsmithextern void	*reallocf(void *ptr, size_t size);
18439665Smsmithextern void	mallocstats(void);
18540520Sdfr#ifdef __alpha__
18640520Sdfrextern void	free_region(void *start, void *end);
18740520Sdfr#endif
18839665Smsmith
18938451Smsmith/* disklabel support (undocumented, may be junk) */
19038451Smsmithstruct		disklabel;
19138451Smsmithextern char	*getdisklabel(const char *, struct disklabel *);
19238451Smsmithextern int	dkcksum(struct disklabel *);
19338451Smsmith
19438451Smsmithextern int	printf(const char *fmt, ...);
19538451Smsmithextern void	vprintf(const char *fmt, _BSD_VA_LIST_);
19638451Smsmithextern int	sprintf(char *buf, const char *cfmt, ...);
19738451Smsmith
19838451Smsmithextern void	twiddle(void);
19938451Smsmith
20038451Smsmithextern void	ngets(char *, int);
20138451Smsmith#define gets(x)	ngets((x), 0)
20238451Smsmithextern int	fgetstr(char *buf, int size, int fd);
20338451Smsmith
20438451Smsmithextern int	open(const char *, int);
20538451Smsmith#define	O_RDONLY	0x0
20638451Smsmith#define O_WRONLY	0x1			/* writing not (yet?) supported */
20738451Smsmith#define O_RDWR		0x2
20838451Smsmithextern int	close(int);
20938451Smsmithextern void	closeall(void);
21038451Smsmithextern ssize_t	read(int, void *, size_t);
21138451Smsmithextern ssize_t	write(int, void *, size_t);
21238451Smsmithextern off_t	lseek(int, off_t, int);
21338451Smsmithextern int	stat(const char *, struct stat *);
21438451Smsmith
21538451Smsmithextern void	srandom(u_long seed);
21638451Smsmithextern u_long	random(void);
21738451Smsmith
21838451Smsmith/* imports from stdlib, locally modified */
21938451Smsmithextern long	strtol(const char *, char **, int);
22039672Sdfrextern char *	strerror(int err);
22138451Smsmithextern char	*optarg;			/* getopt(3) external variables */
22238451Smsmithextern int	optind, opterr, optopt;
22338451Smsmithextern int	getopt(int, char * const [], const char *);
22438451Smsmith
22538451Smsmith/* pager.c */
22638451Smsmithextern void	pager_open(void);
22738451Smsmithextern void	pager_close(void);
22838451Smsmithextern int	pager_output(const char *lines);
22939468Smsmithextern int	pager_file(const char *fname);
23038451Smsmith
23138451Smsmith/* environment.c */
23238451Smsmith#define EV_DYNAMIC	(1<<0)		/* value was dynamically allocated, free if changed/unset */
23338451Smsmith#define EV_VOLATILE	(1<<1)		/* value is volatile, make a copy of it */
23438451Smsmith#define EV_NOHOOK	(1<<2)		/* don't call hook when setting */
23538451Smsmith
23638451Smsmithstruct env_var;
23738451Smsmithtypedef char	*(ev_format_t)(struct env_var *ev);
23838451Smsmithtypedef int	(ev_sethook_t)(struct env_var *ev, int flags, void *value);
23938451Smsmithtypedef int	(ev_unsethook_t)(struct env_var *ev);
24038451Smsmith
24138451Smsmithstruct env_var
24238451Smsmith{
24338451Smsmith    char		*ev_name;
24438451Smsmith    int			ev_flags;
24538451Smsmith    void		*ev_value;
24638451Smsmith    ev_sethook_t	*ev_sethook;
24738451Smsmith    ev_unsethook_t	*ev_unsethook;
24838451Smsmith    struct env_var	*ev_next, *ev_prev;
24938451Smsmith};
25038451Smsmithextern struct env_var	*environ;
25138451Smsmith
25238451Smsmithextern struct env_var	*env_getenv(const char *name);
25338451Smsmithextern int		env_setenv(const char *name, int flags, void *value,
25438451Smsmith				   ev_sethook_t sethook, ev_unsethook_t unsethook);
25538451Smsmithextern char		*getenv(const char *name);
25638451Smsmithextern int		setenv(const char *name, char *value, int overwrite);
25738451Smsmithextern int		putenv(const char *string);
25838451Smsmithextern int		unsetenv(const char *name);
25938451Smsmith
26038451Smsmithextern ev_sethook_t	env_noset;		/* refuse set operation */
26138451Smsmithextern ev_unsethook_t	env_nounset;		/* refuse unset operation */
26238451Smsmith
26338451Smsmith/* BCD conversions (undocumented) */
26438451Smsmithextern u_char const	bcd2bin_data[];
26538451Smsmithextern u_char const	bin2bcd_data[];
26638451Smsmithextern char const	hex2ascii_data[];
26738451Smsmith
26838451Smsmith#define	bcd2bin(bcd)	(bcd2bin_data[bcd])
26938451Smsmith#define	bin2bcd(bin)	(bin2bcd_data[bin])
27038451Smsmith#define	hex2ascii(hex)	(hex2ascii_data[hex])
27138451Smsmith
27238451Smsmith/* min/max (undocumented) */
27338451Smsmithstatic __inline int imax(int a, int b) { return (a > b ? a : b); }
27438451Smsmithstatic __inline int imin(int a, int b) { return (a < b ? a : b); }
27538451Smsmithstatic __inline long lmax(long a, long b) { return (a > b ? a : b); }
27638451Smsmithstatic __inline long lmin(long a, long b) { return (a < b ? a : b); }
27738451Smsmithstatic __inline u_int max(u_int a, u_int b) { return (a > b ? a : b); }
27838451Smsmithstatic __inline u_int min(u_int a, u_int b) { return (a < b ? a : b); }
27938451Smsmithstatic __inline quad_t qmax(quad_t a, quad_t b) { return (a > b ? a : b); }
28038451Smsmithstatic __inline quad_t qmin(quad_t a, quad_t b) { return (a < b ? a : b); }
28138451Smsmithstatic __inline u_long ulmax(u_long a, u_long b) { return (a > b ? a : b); }
28238451Smsmithstatic __inline u_long ulmin(u_long a, u_long b) { return (a < b ? a : b); }
28338451Smsmith
28438451Smsmith/* swaps (undocumented, useful?) */
28538451Smsmith#ifdef __i386__
28638451Smsmithextern u_int32_t	bswap32(u_int32_t x);
28738451Smsmithextern u_int64_t	bswap64(u_int32_t x);
28838451Smsmith#endif
28938451Smsmith
29038451Smsmith/* null functions for device/filesystem switches (undocumented) */
29138451Smsmithextern int	nodev(void);
29238451Smsmithextern int	noioctl(struct open_file *, u_long, void *);
29338451Smsmithextern void	nullsys(void);
29438451Smsmith
29539468Smsmithextern int	null_open(const char *path, struct open_file *f);
29638451Smsmithextern int	null_close(struct open_file *f);
29738451Smsmithextern ssize_t	null_read(struct open_file *f, void *buf, size_t size, size_t *resid);
29838451Smsmithextern ssize_t	null_write(struct open_file *f, void *buf, size_t size, size_t *resid);
29938451Smsmithextern off_t	null_seek(struct open_file *f, off_t offset, int where);
30038451Smsmithextern int	null_stat(struct open_file *f, struct stat *sb);
30138451Smsmith
30238451Smsmith/*
30338451Smsmith * Machine dependent functions and data, must be provided or stubbed by
30438451Smsmith * the consumer
30538451Smsmith */
30638451Smsmithextern int		getchar(void);
30738451Smsmithextern int		ischar(void);
30838451Smsmithextern void		putchar(int);
30939672Sdfrextern int		devopen(struct open_file *, const char *, const char **);
31038451Smsmithextern int		devclose(struct open_file *f);
31138506Sbdeextern void		panic(const char *, ...) __dead2;
31238451Smsmithextern struct fs_ops	*file_system[];
31338451Smsmithextern struct devsw	*devsw[];
31438451Smsmith
31539672Sdfr#if 0
31639672Sdfr
31739672Sdfrstatic inline void *
31839672Sdfrmalloc_debug(size_t size, const char *file, int line)
31939672Sdfr{
32039672Sdfr    void *p;
32139672Sdfr    printf("%s:%d malloc(%ld)", file, line, size);
32239672Sdfr    p = malloc(size);
32339672Sdfr    printf("=%p\n", p);
32439672Sdfr    return p;
32539672Sdfr}
32639672Sdfr
32739672Sdfrstatic inline void
32839672Sdfrfree_debug(void *p, const char *file, int line)
32939672Sdfr{
33039672Sdfr    printf("%s:%d free(%p)\n", file, line, p);
33139672Sdfr    free(p);
33239672Sdfr}
33339672Sdfr
33439672Sdfr#define malloc(x)	malloc_debug(x, __FILE__, __LINE__)
33539672Sdfr#define free(x)		free_debug(x, __FILE__, __LINE__)
33639672Sdfr
33739672Sdfr#endif
338