stand.h revision 39469
1/*
2 * Copyright (c) 1998 Michael Smith.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 *	$Id: stand.h,v 1.3 1998/09/18 22:58:00 msmith Exp $
27 * From	$NetBSD: stand.h,v 1.22 1997/06/26 19:17:40 drochner Exp $
28 */
29
30/*-
31 * Copyright (c) 1993
32 *	The Regents of the University of California.  All rights reserved.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 *    notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 *    notice, this list of conditions and the following disclaimer in the
41 *    documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 *    must display the following acknowledgement:
44 *	This product includes software developed by the University of
45 *	California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 *    may be used to endorse or promote products derived from this software
48 *    without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 *
62 *	@(#)stand.h	8.1 (Berkeley) 6/11/93
63 */
64
65#include <sys/types.h>
66#include <sys/cdefs.h>
67#include <sys/stat.h>
68
69#ifndef NULL
70#define	NULL	0
71#endif
72
73/* Avoid unwanted userlandish components */
74#define KERNEL
75#include <sys/errno.h>
76#undef KERNEL
77
78/* special stand error codes */
79#define	EADAPT	(ELAST+1)	/* bad adaptor */
80#define	ECTLR	(ELAST+2)	/* bad controller */
81#define	EUNIT	(ELAST+3)	/* bad unit */
82#define ESLICE	(ELAST+4)	/* bad slice */
83#define	EPART	(ELAST+5)	/* bad partition */
84#define	ERDLAB	(ELAST+6)	/* can't read disk label */
85#define	EUNLAB	(ELAST+7)	/* unlabeled disk */
86#define	EOFFSET	(ELAST+8)	/* relative seek not supported */
87#define	ESALAST	(ELAST+8)	/* */
88
89struct open_file;
90
91/*
92 * This structure is used to define file system operations in a file system
93 * independent way.
94 *
95 * XXX note that filesystem providers should export a pointer to their fs_ops
96 *     struct, so that consumers can reference this and thus include the
97 *     filesystems that they require.
98 */
99struct fs_ops {
100    const char	*fs_name;
101    int		(*fo_open)(const char *path, struct open_file *f);
102    int		(*fo_close)(struct open_file *f);
103    int		(*fo_read)(struct open_file *f, void *buf,
104			   size_t size, size_t *resid);
105    int		(*fo_write)(struct open_file *f, void *buf,
106			    size_t size, size_t *resid);
107    off_t	(*fo_seek)(struct open_file *f, off_t offset, int where);
108    int		(*fo_stat)(struct open_file *f, struct stat *sb);
109};
110
111/*
112 * libstand-supplied filesystems
113 */
114extern struct fs_ops ufs_fsops;
115extern struct fs_ops tftp_fsops;
116extern struct fs_ops nfs_fsops;
117extern struct fs_ops cd9660_fsops;
118extern struct fs_ops zipfs_fsops;
119#ifdef notyet
120extern struct fs_ops dosfs_fsops;
121#endif
122
123/* where values for lseek(2) */
124#define	SEEK_SET	0	/* set file offset to offset */
125#define	SEEK_CUR	1	/* set file offset to current plus offset */
126#define	SEEK_END	2	/* set file offset to EOF plus offset */
127
128/*
129 * Device switch
130 */
131struct devsw {
132    const char	dv_name[8];
133    int		dv_type;		/* opaque type constant, arch-dependant */
134    int		(*dv_init)(void);	/* early probe call */
135    int		(*dv_strategy)(void *devdata, int rw, daddr_t blk, size_t size, void *buf, size_t *rsize);
136    int		(*dv_open)(struct open_file *f, ...);
137    int		(*dv_close)(struct open_file *f);
138    int		(*dv_ioctl)(struct open_file *f, u_long cmd, void *data);
139};
140
141extern int errno;
142
143struct open_file {
144    int			f_flags;	/* see F_* below */
145    struct devsw	*f_dev;		/* pointer to device operations */
146    void		*f_devdata;	/* device specific data */
147    struct fs_ops	*f_ops;		/* pointer to file system operations */
148    void		*f_fsdata;	/* file system specific data */
149    off_t		f_offset;	/* current file offset (F_RAW) */
150};
151
152#define	SOPEN_MAX	8
153extern struct open_file files[];
154
155/* f_flags values */
156#define	F_READ		0x0001	/* file opened for reading */
157#define	F_WRITE		0x0002	/* file opened for writing */
158#define	F_RAW		0x0004	/* raw device open - no file system */
159#define F_NODEV		0x0008	/* network open - no device */
160
161#define isupper(c)	((c) >= 'A' && (c) <= 'Z')
162#define islower(c)	((c) >= 'a' && (c) <= 'z')
163#define isspace(c)	((c) == ' ' || (c) == '\t')
164#define isdigit(c)	((c) >= '0' && (c) <= '9')
165#define isxdigit(c)	(isdigit(c) || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
166#define isascii(c)	((c) >= 0 || (c <= 0x7f))
167#define isalpha(c)	(isupper(c) || (islower(c)))
168#define toupper(c)	((c) - 'a' + 'A')
169#define tolower(c)	((c) - 'A' + 'a')
170
171extern void	setheap(void *, void *);
172extern void	*malloc(size_t);
173extern void	free(void *);
174extern char	*sbrk(int junk);
175
176/* disklabel support (undocumented, may be junk) */
177struct		disklabel;
178extern char	*getdisklabel(const char *, struct disklabel *);
179extern int	dkcksum(struct disklabel *);
180
181extern int	printf(const char *fmt, ...);
182extern void	vprintf(const char *fmt, _BSD_VA_LIST_);
183extern int	sprintf(char *buf, const char *cfmt, ...);
184
185extern void	twiddle(void);
186
187extern void	ngets(char *, int);
188#define gets(x)	ngets((x), 0)
189extern int	fgetstr(char *buf, int size, int fd);
190
191extern int	open(const char *, int);
192#define	O_RDONLY	0x0
193#define O_WRONLY	0x1			/* writing not (yet?) supported */
194#define O_RDWR		0x2
195extern int	close(int);
196extern void	closeall(void);
197extern ssize_t	read(int, void *, size_t);
198extern ssize_t	write(int, void *, size_t);
199extern off_t	lseek(int, off_t, int);
200extern int	stat(const char *, struct stat *);
201
202extern void	srandom(u_long seed);
203extern u_long	random(void);
204
205/* imports from stdlib, locally modified */
206extern long	strtol(const char *, char **, int);
207extern char	*optarg;			/* getopt(3) external variables */
208extern int	optind, opterr, optopt;
209extern int	getopt(int, char * const [], const char *);
210
211/* pager.c */
212extern void	pager_open(void);
213extern void	pager_close(void);
214extern int	pager_output(const char *lines);
215extern int	pager_file(const char *fname);
216
217/* environment.c */
218#define EV_DYNAMIC	(1<<0)		/* value was dynamically allocated, free if changed/unset */
219#define EV_VOLATILE	(1<<1)		/* value is volatile, make a copy of it */
220#define EV_NOHOOK	(1<<2)		/* don't call hook when setting */
221
222struct env_var;
223typedef char	*(ev_format_t)(struct env_var *ev);
224typedef int	(ev_sethook_t)(struct env_var *ev, int flags, void *value);
225typedef int	(ev_unsethook_t)(struct env_var *ev);
226
227struct env_var
228{
229    char		*ev_name;
230    int			ev_flags;
231    void		*ev_value;
232    ev_sethook_t	*ev_sethook;
233    ev_unsethook_t	*ev_unsethook;
234    struct env_var	*ev_next, *ev_prev;
235};
236extern struct env_var	*environ;
237
238extern struct env_var	*env_getenv(const char *name);
239extern int		env_setenv(const char *name, int flags, void *value,
240				   ev_sethook_t sethook, ev_unsethook_t unsethook);
241extern char		*getenv(const char *name);
242extern int		setenv(const char *name, char *value, int overwrite);
243extern int		putenv(const char *string);
244extern int		unsetenv(const char *name);
245
246extern ev_sethook_t	env_noset;		/* refuse set operation */
247extern ev_unsethook_t	env_nounset;		/* refuse unset operation */
248
249/* BCD conversions (undocumented) */
250extern u_char const	bcd2bin_data[];
251extern u_char const	bin2bcd_data[];
252extern char const	hex2ascii_data[];
253
254#define	bcd2bin(bcd)	(bcd2bin_data[bcd])
255#define	bin2bcd(bin)	(bin2bcd_data[bin])
256#define	hex2ascii(hex)	(hex2ascii_data[hex])
257
258/* min/max (undocumented) */
259static __inline int imax(int a, int b) { return (a > b ? a : b); }
260static __inline int imin(int a, int b) { return (a < b ? a : b); }
261static __inline long lmax(long a, long b) { return (a > b ? a : b); }
262static __inline long lmin(long a, long b) { return (a < b ? a : b); }
263static __inline u_int max(u_int a, u_int b) { return (a > b ? a : b); }
264static __inline u_int min(u_int a, u_int b) { return (a < b ? a : b); }
265static __inline quad_t qmax(quad_t a, quad_t b) { return (a > b ? a : b); }
266static __inline quad_t qmin(quad_t a, quad_t b) { return (a < b ? a : b); }
267static __inline u_long ulmax(u_long a, u_long b) { return (a > b ? a : b); }
268static __inline u_long ulmin(u_long a, u_long b) { return (a < b ? a : b); }
269
270/* swaps (undocumented, useful?) */
271#ifdef __i386__
272extern u_int32_t	bswap32(u_int32_t x);
273extern u_int64_t	bswap64(u_int32_t x);
274#endif
275
276/* null functions for device/filesystem switches (undocumented) */
277extern int	nodev(void);
278extern int	noioctl(struct open_file *, u_long, void *);
279extern void	nullsys(void);
280
281extern int	null_open(const char *path, struct open_file *f);
282extern int	null_close(struct open_file *f);
283extern ssize_t	null_read(struct open_file *f, void *buf, size_t size, size_t *resid);
284extern ssize_t	null_write(struct open_file *f, void *buf, size_t size, size_t *resid);
285extern off_t	null_seek(struct open_file *f, off_t offset, int where);
286extern int	null_stat(struct open_file *f, struct stat *sb);
287
288/*
289 * Machine dependent functions and data, must be provided or stubbed by
290 * the consumer
291 */
292extern int		getchar(void);
293extern int		ischar(void);
294extern void		putchar(int);
295extern int		devopen(struct open_file *, const char *, char **);
296extern int		devclose(struct open_file *f);
297extern void		panic(const char *, ...) __dead2;
298extern struct fs_ops	*file_system[];
299extern struct devsw	*devsw[];
300
301