1
#ifndef _VSTREAM_H_INCLUDED_
2
#define _VSTREAM_H_INCLUDED_
8
/* simple buffered I/O package
10
/* #include <vstream.h>
29
* Simple buffered stream. The members of this structure are not part of the
30
* official interface and can change without prior notice.
32
typedef int (*VSTREAM_FN) (int, void *, unsigned, int, void *);
33
typedef int (*VSTREAM_WAITPID_FN) (pid_t, WAIT_STATUS_T *, int);
35
typedef struct VSTREAM {
36
VBUF buf; /* generic intelligent buffer */
37
int fd; /* file handle, no 256 limit */
38
VSTREAM_FN read_fn; /* buffer fill action */
39
VSTREAM_FN write_fn; /* buffer fill action */
40
void *context; /* application context */
41
off_t offset; /* cached seek info */
42
char *path; /* give it at least try */
43
int read_fd; /* read channel (double-buffered) */
44
int write_fd; /* write channel (double-buffered) */
45
VBUF read_buf; /* read buffer (double-buffered) */
46
VBUF write_buf; /* write buffer (double-buffered) */
47
pid_t pid; /* vstream_popen/close() */
48
VSTREAM_WAITPID_FN waitpid_fn; /* vstream_popen/close() */
49
int timeout; /* read/write timout */
50
jmp_buf *jbuf; /* exception handling */
51
time_t iotime; /* time of last fill/flush */
54
extern VSTREAM vstream_fstd[]; /* pre-defined streams */
56
#define VSTREAM_IN (&vstream_fstd[0])
57
#define VSTREAM_OUT (&vstream_fstd[1])
58
#define VSTREAM_ERR (&vstream_fstd[2])
60
#define VSTREAM_FLAG_ERR VBUF_FLAG_ERR /* some I/O error */
61
#define VSTREAM_FLAG_EOF VBUF_FLAG_EOF /* end of file */
62
#define VSTREAM_FLAG_TIMEOUT VBUF_FLAG_TIMEOUT /* timeout error */
63
#define VSTREAM_FLAG_FIXED VBUF_FLAG_FIXED /* fixed-size buffer */
64
#define VSTREAM_FLAG_BAD VBUF_FLAG_BAD
66
#define VSTREAM_FLAG_READ (1<<8) /* read buffer */
67
#define VSTREAM_FLAG_WRITE (1<<9) /* write buffer */
68
#define VSTREAM_FLAG_SEEK (1<<10) /* seek info valid */
69
#define VSTREAM_FLAG_NSEEK (1<<11) /* can't seek this file */
70
#define VSTREAM_FLAG_DOUBLE (1<<12) /* double buffer */
72
#define VSTREAM_BUFSIZE 4096
74
extern VSTREAM *vstream_fopen(const char *, int, int);
75
extern int vstream_fclose(VSTREAM *);
76
extern off_t vstream_fseek(VSTREAM *, off_t, int);
77
extern off_t vstream_ftell(VSTREAM *);
78
extern int vstream_fflush(VSTREAM *);
79
extern int vstream_fputs(const char *, VSTREAM *);
80
extern VSTREAM *vstream_fdopen(int, int);
82
#define vstream_fread(v, b, n) vbuf_read(&(v)->buf, (b), (n))
83
#define vstream_fwrite(v, b, n) vbuf_write(&(v)->buf, (b), (n))
85
#define VSTREAM_PUTC(ch, vp) VBUF_PUT(&(vp)->buf, (ch))
86
#define VSTREAM_GETC(vp) VBUF_GET(&(vp)->buf)
87
#define vstream_ungetc(vp, ch) vbuf_unget(&(vp)->buf, (ch))
88
#define VSTREAM_EOF VBUF_EOF
90
#define VSTREAM_PUTCHAR(ch) VSTREAM_PUTC((ch), VSTREAM_OUT)
91
#define VSTREAM_GETCHAR() VSTREAM_GETC(VSTREAM_IN)
93
#define vstream_fileno(vp) ((vp)->fd)
94
#define vstream_context(vp) ((vp)->context)
95
#define vstream_ferror(vp) vbuf_error(&(vp)->buf)
96
#define vstream_feof(vp) vbuf_eof(&(vp)->buf)
97
#define vstream_ftimeout(vp) vbuf_timeout(&(vp)->buf)
98
#define vstream_clearerr(vp) vbuf_clearerr(&(vp)->buf)
99
#define VSTREAM_PATH(vp) ((vp)->path ? (vp)->path : "unknown_stream")
100
#define vstream_ftime(vp) ((vp)->iotime)
102
extern void vstream_control(VSTREAM *, int,...);
104
#define VSTREAM_CTL_END 0
105
#define VSTREAM_CTL_READ_FN 1
106
#define VSTREAM_CTL_WRITE_FN 2
107
#define VSTREAM_CTL_PATH 3
108
#define VSTREAM_CTL_DOUBLE 4
109
#define VSTREAM_CTL_READ_FD 5
110
#define VSTREAM_CTL_WRITE_FD 6
111
#define VSTREAM_CTL_WAITPID_FN 7
112
#define VSTREAM_CTL_TIMEOUT 8
113
#define VSTREAM_CTL_EXCEPT 9
114
#define VSTREAM_CTL_CONTEXT 10
116
extern VSTREAM *PRINTFLIKE(1, 2) vstream_printf(const char *,...);
117
extern VSTREAM *PRINTFLIKE(2, 3) vstream_fprintf(VSTREAM *, const char *,...);
119
extern VSTREAM *vstream_popen(int,...);
120
extern int vstream_pclose(VSTREAM *);
122
#define vstream_ispipe(vp) ((vp)->pid != 0)
124
#define VSTREAM_POPEN_END 0 /* terminator */
125
#define VSTREAM_POPEN_COMMAND 1 /* command is string */
126
#define VSTREAM_POPEN_ARGV 2 /* command is array */
127
#define VSTREAM_POPEN_UID 3 /* privileges */
128
#define VSTREAM_POPEN_GID 4 /* privileges */
129
#define VSTREAM_POPEN_ENV 5 /* extra environment */
130
#define VSTREAM_POPEN_SHELL 6 /* alternative shell */
131
#define VSTREAM_POPEN_WAITPID_FN 7 /* child catcher, waitpid() compat. */
132
#define VSTREAM_POPEN_EXPORT 8 /* exportable environment */
134
extern VSTREAM *vstream_vfprintf(VSTREAM *, const char *, va_list);
136
extern int vstream_peek(VSTREAM *);
139
* Exception handling. We use pointer to jmp_buf to avoid a lot of unused
140
* baggage for streams that don't need this functionality.
142
#define vstream_setjmp(stream) setjmp((stream)->jbuf[0])
143
#define vstream_longjmp(stream, val) longjmp((stream)->jbuf[0], (val))
148
/* The Secure Mailer license must be distributed with this software.
151
/* IBM T.J. Watson Research
153
/* Yorktown Heights, NY 10598, USA