stand.h revision 55206
117683Spst/*
217683Spst * Copyright (c) 1998 Michael Smith.
317683Spst * All rights reserved.
417683Spst *
517683Spst * Redistribution and use in source and binary forms, with or without
617683Spst * modification, are permitted provided that the following conditions
717683Spst * are met:
817683Spst * 1. Redistributions of source code must retain the above copyright
917683Spst *    notice, this list of conditions and the following disclaimer.
1017683Spst * 2. Redistributions in binary form must reproduce the above copyright
1117683Spst *    notice, this list of conditions and the following disclaimer in the
1217683Spst *    documentation and/or other materials provided with the distribution.
1317683Spst *
1417683Spst * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1517683Spst * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1617683Spst * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1717683Spst * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1817683Spst * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1917683Spst * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2017683Spst * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2117683Spst * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2217683Spst * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2317683Spst * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2417683Spst * SUCH DAMAGE.
2517683Spst *
2617683Spst * $FreeBSD: head/lib/libstand/stand.h 55206 1999-12-29 05:07:58Z peter $
2717683Spst * From	$NetBSD: stand.h,v 1.22 1997/06/26 19:17:40 drochner Exp $
2817683Spst */
2917683Spst
3017683Spst/*-
3117683Spst * Copyright (c) 1993
3217683Spst *	The Regents of the University of California.  All rights reserved.
3317683Spst *
3417683Spst * Redistribution and use in source and binary forms, with or without
3517683Spst * modification, are permitted provided that the following conditions
3617683Spst * are met:
3717683Spst * 1. Redistributions of source code must retain the above copyright
3817683Spst *    notice, this list of conditions and the following disclaimer.
3917683Spst * 2. Redistributions in binary form must reproduce the above copyright
4017683Spst *    notice, this list of conditions and the following disclaimer in the
4117683Spst *    documentation and/or other materials provided with the distribution.
4217683Spst * 3. All advertising materials mentioning features or use of this software
4317683Spst *    must display the following acknowledgement:
4417683Spst *	This product includes software developed by the University of
4517683Spst *	California, Berkeley and its contributors.
4617683Spst * 4. Neither the name of the University nor the names of its contributors
4717683Spst *    may be used to endorse or promote products derived from this software
4817683Spst *    without specific prior written permission.
4917683Spst *
5017683Spst * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5117683Spst * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5217683Spst * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5317683Spst * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5417683Spst * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5517683Spst * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5617683Spst * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5717683Spst * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5817683Spst * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5917683Spst * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6017683Spst * SUCH DAMAGE.
6117683Spst *
6217683Spst *	@(#)stand.h	8.1 (Berkeley) 6/11/93
6317683Spst */
6417683Spst
6517683Spst#include <sys/types.h>
6617683Spst#include <sys/cdefs.h>
6717683Spst#include <sys/stat.h>
6817683Spst
6917683Spst#define CHK(fmt, args...)	printf("%s(%d): " fmt "\n", __FUNCTION__, __LINE__ , ##args)
7017683Spst#define PCHK(fmt, args...)	{printf("%s(%d): " fmt "\n", __FUNCTION__, __LINE__ , ##args); getchar();}
7117683Spst
7217683Spst#ifndef NULL
7317683Spst#define	NULL	0
7417683Spst#endif
7517683Spst
7617683Spst/* Avoid unwanted userlandish components */
7717683Spst#define _KERNEL
7817683Spst#include <sys/errno.h>
7917683Spst#undef _KERNEL
8017683Spst
8117683Spst/* special stand error codes */
8217683Spst#define	EADAPT	(ELAST+1)	/* bad adaptor */
8317683Spst#define	ECTLR	(ELAST+2)	/* bad controller */
8417683Spst#define	EUNIT	(ELAST+3)	/* bad unit */
8517683Spst#define ESLICE	(ELAST+4)	/* bad slice */
8617683Spst#define	EPART	(ELAST+5)	/* bad partition */
8717683Spst#define	ERDLAB	(ELAST+6)	/* can't read disk label */
8817683Spst#define	EUNLAB	(ELAST+7)	/* unlabeled disk */
8917683Spst#define	EOFFSET	(ELAST+8)	/* relative seek not supported */
9017683Spst#define	ESALAST	(ELAST+8)	/* */
9117683Spst
9217683Spststruct open_file;
9317683Spst
9417683Spst/*
9517683Spst * This structure is used to define file system operations in a file system
9617683Spst * independent way.
9717683Spst *
9817683Spst * XXX note that filesystem providers should export a pointer to their fs_ops
9917683Spst *     struct, so that consumers can reference this and thus include the
10017683Spst *     filesystems that they require.
10117683Spst */
10217683Spststruct fs_ops {
10317683Spst    const char	*fs_name;
10417683Spst    int		(*fo_open)(const char *path, struct open_file *f);
10517683Spst    int		(*fo_close)(struct open_file *f);
10617683Spst    int		(*fo_read)(struct open_file *f, void *buf,
10717683Spst			   size_t size, size_t *resid);
10817683Spst    int		(*fo_write)(struct open_file *f, void *buf,
10917683Spst			    size_t size, size_t *resid);
11017683Spst    off_t	(*fo_seek)(struct open_file *f, off_t offset, int where);
11117683Spst    int		(*fo_stat)(struct open_file *f, struct stat *sb);
11217683Spst};
11317683Spst
11417683Spst/*
11517683Spst * libstand-supplied filesystems
11617683Spst */
11717683Spstextern struct fs_ops ufs_fsops;
11817683Spstextern struct fs_ops tftp_fsops;
11917683Spstextern struct fs_ops nfs_fsops;
12017683Spstextern struct fs_ops cd9660_fsops;
12117683Spstextern struct fs_ops zipfs_fsops;
12217683Spstextern struct fs_ops dosfs_fsops;
12317683Spst
12417683Spst/* where values for lseek(2) */
12517683Spst#define	SEEK_SET	0	/* set file offset to offset */
12617683Spst#define	SEEK_CUR	1	/* set file offset to current plus offset */
12717683Spst#define	SEEK_END	2	/* set file offset to EOF plus offset */
12817683Spst
12917683Spst/*
13017683Spst * Device switch
13117683Spst */
13217683Spststruct devsw {
13317683Spst    const char	dv_name[8];
13417683Spst    int		dv_type;		/* opaque type constant, arch-dependant */
13517683Spst    int		(*dv_init)(void);	/* early probe call */
13617683Spst    int		(*dv_strategy)(void *devdata, int rw, daddr_t blk, size_t size, void *buf, size_t *rsize);
13717683Spst    int		(*dv_open)(struct open_file *f, ...);
13817683Spst    int		(*dv_close)(struct open_file *f);
13917683Spst    int		(*dv_ioctl)(struct open_file *f, u_long cmd, void *data);
14017683Spst    void	(*dv_print)(int verbose);	/* print device information */
14117683Spst};
14217683Spst
14317683Spstextern int errno;
14417683Spst
14517683Spststruct open_file {
14617683Spst    int			f_flags;	/* see F_* below */
14717683Spst    struct devsw	*f_dev;		/* pointer to device operations */
14817683Spst    void		*f_devdata;	/* device specific data */
14917683Spst    struct fs_ops	*f_ops;		/* pointer to file system operations */
15017683Spst    void		*f_fsdata;	/* file system specific data */
15117683Spst    off_t		f_offset;	/* current file offset (F_RAW) */
15217683Spst};
15317683Spst
15417683Spst#define	SOPEN_MAX	8
15517683Spstextern struct open_file files[];
15617683Spst
15717683Spst/* f_flags values */
15817683Spst#define	F_READ		0x0001	/* file opened for reading */
15917683Spst#define	F_WRITE		0x0002	/* file opened for writing */
16017683Spst#define	F_RAW		0x0004	/* raw device open - no file system */
16117683Spst#define F_NODEV		0x0008	/* network open - no device */
16217683Spst
16317683Spst#define isupper(c)	((c) >= 'A' && (c) <= 'Z')
16417683Spst#define islower(c)	((c) >= 'a' && (c) <= 'z')
16517683Spst#define isspace(c)	((c) == ' ' || ((c) >= 0x9 && (c) <= 0xd))
16617683Spst#define isdigit(c)	((c) >= '0' && (c) <= '9')
16717683Spst#define isxdigit(c)	(isdigit(c) || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
16817683Spst#define isascii(c)	(((c) & ~0x7F) == 0)
16917683Spst#define isalpha(c)	(isupper(c) || (islower(c)))
17017683Spst
17117683Spststatic __inline int toupper(int c)
17217683Spst{
17317683Spst    return islower(c) ? c - 'a' + 'A' : c;
17417683Spst}
17517683Spst
17617683Spststatic __inline int tolower(int c)
17717683Spst{
17817683Spst    return isupper(c) ? c - 'A' + 'a' : c;
17917683Spst}
18017683Spst
18117683Spst/* sbrk emulation */
18217683Spstextern void	setheap(void *base, void *top);
18317683Spstextern char	*sbrk(int incr);
18417683Spst
18517683Spst/* Matt Dillon's zalloc/zmalloc */
18617683Spstextern void	*malloc(size_t bytes);
18717683Spstextern void	free(void *ptr);
18817683Spst/*#define free(p)	{CHK("free %p", p); free(p);} */ /* use for catching guard violations */
18917683Spstextern void	*calloc(size_t n1, size_t n2);
19017683Spstextern void	*realloc(void *ptr, size_t size);
19117683Spstextern void	*reallocf(void *ptr, size_t size);
19217683Spstextern void	mallocstats(void);
19317683Spst#ifdef __alpha__
19417683Spstextern void	free_region(void *start, void *end);
19517683Spst#endif
19617683Spst
19717683Spst/* disklabel support (undocumented, may be junk) */
19817683Spststruct		disklabel;
19917683Spstextern char	*getdisklabel(const char *, struct disklabel *);
20017683Spstextern int	dkcksum(struct disklabel *);
20117683Spst
20217683Spstextern int	printf(const char *fmt, ...);
20317683Spstextern void	vprintf(const char *fmt, _BSD_VA_LIST_);
20417683Spstextern int	sprintf(char *buf, const char *cfmt, ...);
20517683Spstextern void	vsprintf(char *buf, const char *cfmt, _BSD_VA_LIST_);
20617683Spst
20717683Spstextern void	twiddle(void);
20817683Spst
20917683Spstextern void	ngets(char *, int);
21017683Spst#define gets(x)	ngets((x), 0)
21117683Spstextern int	fgetstr(char *buf, int size, int fd);
21217683Spst
21317683Spstextern int	open(const char *, int);
21417683Spst#define	O_RDONLY	0x0
21517683Spst#define O_WRONLY	0x1			/* writing not (yet?) supported */
21617683Spst#define O_RDWR		0x2
21717683Spstextern int	close(int);
21817683Spstextern void	closeall(void);
21917683Spstextern ssize_t	read(int, void *, size_t);
22017683Spstextern ssize_t	write(int, void *, size_t);
22117683Spstextern off_t	lseek(int, off_t, int);
22217683Spstextern int	stat(const char *, struct stat *);
22317683Spst
22417683Spstextern void	srandom(u_long seed);
22517683Spstextern u_long	random(void);
22617683Spst
22717683Spst/* imports from stdlib, locally modified */
22817683Spstextern long	strtol(const char *, char **, int);
22917683Spstextern char *	strerror(int err);
23017683Spstextern char	*optarg;			/* getopt(3) external variables */
23117683Spstextern int	optind, opterr, optopt, optreset;
23217683Spstextern int	getopt(int, char * const [], const char *);
23317683Spst
23417683Spst/* pager.c */
23517683Spstextern void	pager_open(void);
23617683Spstextern void	pager_close(void);
23717683Spstextern int	pager_output(const char *lines);
23817683Spstextern int	pager_file(const char *fname);
23917683Spst
24017683Spst/* No signal state to preserve */
24117683Spst#define setjmp	_setjmp
24217683Spst#define longjmp	_longjmp
24317683Spst
24417683Spst/* environment.c */
24517683Spst#define EV_DYNAMIC	(1<<0)		/* value was dynamically allocated, free if changed/unset */
24617683Spst#define EV_VOLATILE	(1<<1)		/* value is volatile, make a copy of it */
24717683Spst#define EV_NOHOOK	(1<<2)		/* don't call hook when setting */
24817683Spst
24917683Spststruct env_var;
25017683Spsttypedef char	*(ev_format_t)(struct env_var *ev);
25117683Spsttypedef int	(ev_sethook_t)(struct env_var *ev, int flags, void *value);
25217683Spsttypedef int	(ev_unsethook_t)(struct env_var *ev);
25317683Spst
25417683Spststruct env_var
25517683Spst{
25617683Spst    char		*ev_name;
25717683Spst    int			ev_flags;
25817683Spst    void		*ev_value;
25917683Spst    ev_sethook_t	*ev_sethook;
26017683Spst    ev_unsethook_t	*ev_unsethook;
26117683Spst    struct env_var	*ev_next, *ev_prev;
26217683Spst};
26317683Spstextern struct env_var	*environ;
26417683Spst
26517683Spstextern struct env_var	*env_getenv(const char *name);
26617683Spstextern int		env_setenv(const char *name, int flags, void *value,
26717683Spst				   ev_sethook_t sethook, ev_unsethook_t unsethook);
26817683Spstextern char		*getenv(const char *name);
26917683Spstextern int		setenv(const char *name, char *value, int overwrite);
27017683Spstextern int		putenv(const char *string);
27117683Spstextern int		unsetenv(const char *name);
27217683Spst
27317683Spstextern ev_sethook_t	env_noset;		/* refuse set operation */
27417683Spstextern ev_unsethook_t	env_nounset;		/* refuse unset operation */
27517683Spst
27617683Spst/* BCD conversions (undocumented) */
27717683Spstextern u_char const	bcd2bin_data[];
27817683Spstextern u_char const	bin2bcd_data[];
27917683Spstextern char const	hex2ascii_data[];
28017683Spst
28117683Spst#define	bcd2bin(bcd)	(bcd2bin_data[bcd])
28217683Spst#define	bin2bcd(bin)	(bin2bcd_data[bin])
28317683Spst#define	hex2ascii(hex)	(hex2ascii_data[hex])
28417683Spst
28517683Spst/* min/max (undocumented) */
28617683Spststatic __inline int imax(int a, int b) { return (a > b ? a : b); }
28717683Spststatic __inline int imin(int a, int b) { return (a < b ? a : b); }
28817683Spststatic __inline long lmax(long a, long b) { return (a > b ? a : b); }
28917683Spststatic __inline long lmin(long a, long b) { return (a < b ? a : b); }
29017683Spststatic __inline u_int max(u_int a, u_int b) { return (a > b ? a : b); }
29117683Spststatic __inline u_int min(u_int a, u_int b) { return (a < b ? a : b); }
29217683Spststatic __inline quad_t qmax(quad_t a, quad_t b) { return (a > b ? a : b); }
29317683Spststatic __inline quad_t qmin(quad_t a, quad_t b) { return (a < b ? a : b); }
29417683Spststatic __inline u_long ulmax(u_long a, u_long b) { return (a > b ? a : b); }
29517683Spststatic __inline u_long ulmin(u_long a, u_long b) { return (a < b ? a : b); }
29617683Spst
29717683Spst/* swaps (undocumented, useful?) */
29817683Spst#ifdef __i386__
29917683Spstextern u_int32_t	bswap32(u_int32_t x);
30017683Spstextern u_int64_t	bswap64(u_int32_t x);
30117683Spst#endif
30217683Spst
30317683Spst/* null functions for device/filesystem switches (undocumented) */
30417683Spstextern int	nodev(void);
30517683Spstextern int	noioctl(struct open_file *, u_long, void *);
30617683Spstextern void	nullsys(void);
30717683Spst
30817683Spstextern int	null_open(const char *path, struct open_file *f);
30917683Spstextern int	null_close(struct open_file *f);
31017683Spstextern int	null_read(struct open_file *f, void *buf, size_t size, size_t *resid);
31117683Spstextern int	null_write(struct open_file *f, void *buf, size_t size, size_t *resid);
31217683Spstextern off_t	null_seek(struct open_file *f, off_t offset, int where);
31317683Spstextern int	null_stat(struct open_file *f, struct stat *sb);
31417683Spst
31517683Spst/*
31617683Spst * Machine dependent functions and data, must be provided or stubbed by
31717683Spst * the consumer
31817683Spst */
31917683Spstextern int		getchar(void);
32017683Spstextern int		ischar(void);
32117683Spstextern void		putchar(int);
32217683Spstextern int		devopen(struct open_file *, const char *, const char **);
32317683Spstextern int		devclose(struct open_file *f);
32417683Spstextern void		panic(const char *, ...) __dead2;
32517683Spstextern struct fs_ops	*file_system[];
32617683Spstextern struct devsw	*devsw[];
32717683Spst
32817683Spst#if 0
32917683Spst
33017683Spststatic inline void *
33117683Spstmalloc_debug(size_t size, const char *file, int line)
33217683Spst{
33317683Spst    void *p;
33417683Spst    printf("%s:%d malloc(%ld)", file, line, size);
33517683Spst    p = malloc(size);
33617683Spst    printf("=%p\n", p);
33717683Spst    return p;
33817683Spst}
33917683Spst
34017683Spststatic inline void
34117683Spstfree_debug(void *p, const char *file, int line)
34217683Spst{
34317683Spst    printf("%s:%d free(%p)\n", file, line, p);
34417683Spst    free(p);
34517683Spst}
34617683Spst
34717683Spst#define malloc(x)	malloc_debug(x, __FILE__, __LINE__)
34817683Spst#define free(x)		free_debug(x, __FILE__, __LINE__)
34917683Spst
35017683Spst#endif
35117683Spst