xref: /plan9/sys/src/ape/cmd/pdksh/shf.h (revision 7dd7cddf99dd7472612f1413b4da293630e6b1bc)
1 #ifndef SHF_H
2 # define SHF_H
3 
4 /*
5  * Shell file I/O routines
6  */
7 /* $Id$ */
8 
9 #define SHF_BSIZE	512
10 
11 #define shf_fileno(shf)	((shf)->fd)
12 #define shf_setfileno(shf,nfd)	((shf)->fd = (nfd))
13 #define shf_getc(shf) ((shf)->rnleft > 0 ? (shf)->rnleft--, *(shf)->rp++ : \
14 			shf_getchar(shf))
15 #define shf_putc(c, shf)	((shf)->wnleft == 0 ? shf_putchar((c), (shf)) \
16 					: ((shf)->wnleft--, *(shf)->wp++ = (c)))
17 #define shf_eof(shf)		((shf)->flags & SHF_EOF)
18 #define shf_error(shf)		((shf)->flags & SHF_ERROR)
19 #define shf_errno(shf)		((shf)->errno_)
20 #define shf_clearerr(shf)	((shf)->flags &= ~(SHF_EOF | SHF_ERROR))
21 
22 /* Flags passed to shf_*open() */
23 #define SHF_RD		0x0001
24 #define SHF_WR		0x0002
25 #define SHF_RDWR	  (SHF_RD|SHF_WR)
26 #define SHF_ACCMODE	  0x0003	/* mask */
27 #define SHF_GETFL	0x0004		/* use fcntl() to figure RD/WR flags */
28 #define SHF_UNBUF	0x0008		/* unbuffered I/O */
29 #define SHF_CLEXEC	0x0010		/* set close on exec flag */
30 #define SHF_MAPHI	0x0020		/* make fd > FDBASE (and close orig)
31 					 * (shf_open() only) */
32 #define SHF_DYNAMIC	0x0040		/* string: increase buffer as needed */
33 #define SHF_INTERRUPT	0x0080		/* EINTR in read/write causes error */
34 /* Flags used internally */
35 #define SHF_STRING	0x0100		/* a string, not a file */
36 #define SHF_ALLOCS	0x0200		/* shf and shf->buf were alloc()ed */
37 #define SHF_ALLOCB	0x0400		/* shf->buf was alloc()ed */
38 #define SHF_ERROR	0x0800		/* read()/write() error */
39 #define SHF_EOF		0x1000		/* read eof (sticky) */
40 #define SHF_READING	0x2000		/* currently reading: rnleft,rp valid */
41 #define SHF_WRITING	0x4000		/* currently writing: wnleft,wp valid */
42 
43 
44 struct shf {
45 	int flags;		/* see SHF_* */
46 	unsigned char *rp;	/* read: current position in buffer */
47 	int rbsize;		/* size of buffer (1 if SHF_UNBUF) */
48 	int rnleft;		/* read: how much data left in buffer */
49 	unsigned char *wp;	/* write: current position in buffer */
50 	int wbsize;		/* size of buffer (0 if SHF_UNBUF) */
51 	int wnleft;		/* write: how much space left in buffer */
52 	unsigned char *buf;	/* buffer */
53 	int fd;			/* file descriptor */
54 	int errno_;		/* saved value of errno after error */
55 	int bsize;		/* actual size of buf */
56 	Area *areap;		/* area shf/buf were allocated in */
57 };
58 
59 extern struct shf shf_iob[];
60 
61 struct shf *shf_open	ARGS((const char *name, int oflags, int mode,
62 			      int sflags));
63 struct shf *shf_fdopen	ARGS((int fd, int sflags, struct shf *shf));
64 struct shf *shf_reopen  ARGS((int fd, int sflags, struct shf *shf));
65 struct shf *shf_sopen	ARGS((char *buf, int bsize, int sflags,
66 			      struct shf *shf));
67 int	    shf_close	ARGS((struct shf *shf));
68 int	    shf_fdclose	ARGS((struct shf *shf));
69 char	   *shf_sclose	ARGS((struct shf *shf));
70 int	    shf_finish	ARGS((struct shf *shf));
71 int	    shf_flush	ARGS((struct shf *shf));
72 int	    shf_seek	ARGS((struct shf *shf, off_t where, int from));
73 int	    shf_read	ARGS((char *buf, int bsize, struct shf *shf));
74 char	   *shf_getse	ARGS((char *buf, int bsize, struct shf *shf));
75 int	    shf_getchar	ARGS((struct shf *shf));
76 int	    shf_ungetc	ARGS((int c, struct shf *shf));
77 int	    shf_putchar	ARGS((int c, struct shf *shf));
78 int	    shf_puts	ARGS((const char *s, struct shf *shf));
79 int	    shf_write	ARGS((const char *buf, int nbytes, struct shf *shf));
80 int	    shf_fprintf ARGS((struct shf *shf, const char *fmt, ...));
81 int	    shf_snprintf ARGS((char *buf, int bsize, const char *fmt, ...));
82 char	    *shf_smprintf ARGS((const char *fmt, ...));
83 int	    shf_vfprintf ARGS((struct shf *, const char *fmt, va_list args));
84 
85 #endif /* SHF_H */
86