stand.h revision 39468
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.2 1998/08/24 02:54:33 bde 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 const char *strerror(int);
192
193extern int	open(const char *, int);
194#define	O_RDONLY	0x0
195#define O_WRONLY	0x1			/* writing not (yet?) supported */
196#define O_RDWR		0x2
197extern int	close(int);
198extern void	closeall(void);
199extern ssize_t	read(int, void *, size_t);
200extern ssize_t	write(int, void *, size_t);
201extern off_t	lseek(int, off_t, int);
202extern int	stat(const char *, struct stat *);
203
204extern void	srandom(u_long seed);
205extern u_long	random(void);
206
207/* imports from stdlib, locally modified */
208extern long	strtol(const char *, char **, int);
209extern char	*optarg;			/* getopt(3) external variables */
210extern int	optind, opterr, optopt;
211extern int	getopt(int, char * const [], const char *);
212
213/* pager.c */
214extern void	pager_open(void);
215extern void	pager_close(void);
216extern int	pager_output(const char *lines);
217extern int	pager_file(const char *fname);
218
219/* environment.c */
220#define EV_DYNAMIC	(1<<0)		/* value was dynamically allocated, free if changed/unset */
221#define EV_VOLATILE	(1<<1)		/* value is volatile, make a copy of it */
222#define EV_NOHOOK	(1<<2)		/* don't call hook when setting */
223
224struct env_var;
225typedef char	*(ev_format_t)(struct env_var *ev);
226typedef int	(ev_sethook_t)(struct env_var *ev, int flags, void *value);
227typedef int	(ev_unsethook_t)(struct env_var *ev);
228
229struct env_var
230{
231    char		*ev_name;
232    int			ev_flags;
233    void		*ev_value;
234    ev_sethook_t	*ev_sethook;
235    ev_unsethook_t	*ev_unsethook;
236    struct env_var	*ev_next, *ev_prev;
237};
238extern struct env_var	*environ;
239
240extern struct env_var	*env_getenv(const char *name);
241extern int		env_setenv(const char *name, int flags, void *value,
242				   ev_sethook_t sethook, ev_unsethook_t unsethook);
243extern char		*getenv(const char *name);
244extern int		setenv(const char *name, char *value, int overwrite);
245extern int		putenv(const char *string);
246extern int		unsetenv(const char *name);
247
248extern ev_sethook_t	env_noset;		/* refuse set operation */
249extern ev_unsethook_t	env_nounset;		/* refuse unset operation */
250
251/* BCD conversions (undocumented) */
252extern u_char const	bcd2bin_data[];
253extern u_char const	bin2bcd_data[];
254extern char const	hex2ascii_data[];
255
256#define	bcd2bin(bcd)	(bcd2bin_data[bcd])
257#define	bin2bcd(bin)	(bin2bcd_data[bin])
258#define	hex2ascii(hex)	(hex2ascii_data[hex])
259
260/* min/max (undocumented) */
261static __inline int imax(int a, int b) { return (a > b ? a : b); }
262static __inline int imin(int a, int b) { return (a < b ? a : b); }
263static __inline long lmax(long a, long b) { return (a > b ? a : b); }
264static __inline long lmin(long a, long b) { return (a < b ? a : b); }
265static __inline u_int max(u_int a, u_int b) { return (a > b ? a : b); }
266static __inline u_int min(u_int a, u_int b) { return (a < b ? a : b); }
267static __inline quad_t qmax(quad_t a, quad_t b) { return (a > b ? a : b); }
268static __inline quad_t qmin(quad_t a, quad_t b) { return (a < b ? a : b); }
269static __inline u_long ulmax(u_long a, u_long b) { return (a > b ? a : b); }
270static __inline u_long ulmin(u_long a, u_long b) { return (a < b ? a : b); }
271
272/* swaps (undocumented, useful?) */
273#ifdef __i386__
274extern u_int32_t	bswap32(u_int32_t x);
275extern u_int64_t	bswap64(u_int32_t x);
276#endif
277
278/* null functions for device/filesystem switches (undocumented) */
279extern int	nodev(void);
280extern int	noioctl(struct open_file *, u_long, void *);
281extern void	nullsys(void);
282
283extern int	null_open(const char *path, struct open_file *f);
284extern int	null_close(struct open_file *f);
285extern ssize_t	null_read(struct open_file *f, void *buf, size_t size, size_t *resid);
286extern ssize_t	null_write(struct open_file *f, void *buf, size_t size, size_t *resid);
287extern off_t	null_seek(struct open_file *f, off_t offset, int where);
288extern int	null_stat(struct open_file *f, struct stat *sb);
289
290/*
291 * Machine dependent functions and data, must be provided or stubbed by
292 * the consumer
293 */
294extern int		getchar(void);
295extern int		ischar(void);
296extern void		putchar(int);
297extern int		devopen(struct open_file *, const char *, char **);
298extern int		devclose(struct open_file *f);
299extern void		panic(const char *, ...) __dead2;
300extern struct fs_ops	*file_system[];
301extern struct devsw	*devsw[];
302
303