xref: /openbsd-src/bin/ksh/shf.h (revision 5ef49dcf8743485b5eef75f0694fa5ab9939fc5c)
1*5ef49dcfSmmcc /*	$OpenBSD: shf.h,v 1.8 2015/12/14 06:09:43 mmcc Exp $	*/
2945abdecSmillert 
3945abdecSmillert #ifndef SHF_H
4945abdecSmillert # define SHF_H
57cb960a2Sdownsj 
67cb960a2Sdownsj /*
77cb960a2Sdownsj  * Shell file I/O routines
87cb960a2Sdownsj  */
97cb960a2Sdownsj 
107cb960a2Sdownsj #define SHF_BSIZE	512
117cb960a2Sdownsj 
127cb960a2Sdownsj #define shf_getc(shf) ((shf)->rnleft > 0 ? (shf)->rnleft--, *(shf)->rp++ : \
137cb960a2Sdownsj 			shf_getchar(shf))
147a8124d8Sderaadt #define shf_putc(c, shf)	((shf)->wnleft == 0 ? shf_putchar((c), (shf)) : \
157a8124d8Sderaadt 				    ((shf)->wnleft--, *(shf)->wp++ = (c)))
167cb960a2Sdownsj #define shf_eof(shf)		((shf)->flags & SHF_EOF)
177cb960a2Sdownsj #define shf_error(shf)		((shf)->flags & SHF_ERROR)
187cb960a2Sdownsj #define shf_clearerr(shf)	((shf)->flags &= ~(SHF_EOF | SHF_ERROR))
197cb960a2Sdownsj 
207cb960a2Sdownsj /* Flags passed to shf_*open() */
217cb960a2Sdownsj #define SHF_RD		0x0001
227cb960a2Sdownsj #define SHF_WR		0x0002
237cb960a2Sdownsj #define SHF_RDWR	  (SHF_RD|SHF_WR)
247cb960a2Sdownsj #define SHF_ACCMODE	  0x0003	/* mask */
257cb960a2Sdownsj #define SHF_GETFL	0x0004		/* use fcntl() to figure RD/WR flags */
267cb960a2Sdownsj #define SHF_UNBUF	0x0008		/* unbuffered I/O */
277cb960a2Sdownsj #define SHF_CLEXEC	0x0010		/* set close on exec flag */
287cb960a2Sdownsj #define SHF_MAPHI	0x0020		/* make fd > FDBASE (and close orig)
297cb960a2Sdownsj 					 * (shf_open() only) */
307cb960a2Sdownsj #define SHF_DYNAMIC	0x0040		/* string: increase buffer as needed */
317cb960a2Sdownsj #define SHF_INTERRUPT	0x0080		/* EINTR in read/write causes error */
327cb960a2Sdownsj /* Flags used internally */
337cb960a2Sdownsj #define SHF_STRING	0x0100		/* a string, not a file */
347cb960a2Sdownsj #define SHF_ALLOCS	0x0200		/* shf and shf->buf were alloc()ed */
357cb960a2Sdownsj #define SHF_ALLOCB	0x0400		/* shf->buf was alloc()ed */
367cb960a2Sdownsj #define SHF_ERROR	0x0800		/* read()/write() error */
377cb960a2Sdownsj #define SHF_EOF		0x1000		/* read eof (sticky) */
387cb960a2Sdownsj #define SHF_READING	0x2000		/* currently reading: rnleft,rp valid */
397cb960a2Sdownsj #define SHF_WRITING	0x4000		/* currently writing: wnleft,wp valid */
407cb960a2Sdownsj 
417cb960a2Sdownsj 
427cb960a2Sdownsj struct shf {
437cb960a2Sdownsj 	int flags;		/* see SHF_* */
447cb960a2Sdownsj 	unsigned char *rp;	/* read: current position in buffer */
457cb960a2Sdownsj 	int rbsize;		/* size of buffer (1 if SHF_UNBUF) */
467cb960a2Sdownsj 	int rnleft;		/* read: how much data left in buffer */
477cb960a2Sdownsj 	unsigned char *wp;	/* write: current position in buffer */
487cb960a2Sdownsj 	int wbsize;		/* size of buffer (0 if SHF_UNBUF) */
497cb960a2Sdownsj 	int wnleft;		/* write: how much space left in buffer */
507cb960a2Sdownsj 	unsigned char *buf;	/* buffer */
517cb960a2Sdownsj 	int fd;			/* file descriptor */
527cb960a2Sdownsj 	int errno_;		/* saved value of errno after error */
537cb960a2Sdownsj 	int bsize;		/* actual size of buf */
547cb960a2Sdownsj 	Area *areap;		/* area shf/buf were allocated in */
557cb960a2Sdownsj };
567cb960a2Sdownsj 
577cb960a2Sdownsj extern struct shf shf_iob[];
587cb960a2Sdownsj 
59c5d5393cSotto struct shf *shf_open(const char *, int, int, int);
60c5d5393cSotto struct shf *shf_fdopen(int, int, struct shf *);
61c5d5393cSotto struct shf *shf_reopen(int, int, struct shf *);
62c5d5393cSotto struct shf *shf_sopen(char *, int, int, struct shf *);
63c5d5393cSotto int	    shf_close(struct shf *);
64c5d5393cSotto int	    shf_fdclose(struct shf *);
65c5d5393cSotto char	   *shf_sclose(struct shf *);
66c5d5393cSotto int	    shf_flush(struct shf *);
67c5d5393cSotto int	    shf_read(char *, int, struct shf *);
68c5d5393cSotto char	   *shf_getse(char *, int, struct shf *);
69c5d5393cSotto int	    shf_getchar(struct shf *s);
70c5d5393cSotto int	    shf_ungetc(int, struct shf *);
71c5d5393cSotto int	    shf_putchar(int, struct shf *);
72c5d5393cSotto int	    shf_puts(const char *, struct shf *);
73c5d5393cSotto int	    shf_write(const char *, int, struct shf *);
74c5d5393cSotto int	    shf_fprintf(struct shf *, const char *, ...);
75c5d5393cSotto int	    shf_snprintf(char *, int, const char *, ...);
76c5d5393cSotto char	    *shf_smprintf(const char *, ...);
77c5d5393cSotto int	    shf_vfprintf(struct shf *, const char *, va_list);
78945abdecSmillert 
79945abdecSmillert #endif /* SHF_H */
80