Import upstream 2.3.14
[packages/xinetd.git] / libs / src / sio / sio.h
1 /*
2  * (c) Copyright 1992, 1993 by Panagiotis Tsirigotis
3  * All rights reserved.  The file named COPYRIGHT specifies the terms 
4  * and conditions for redistribution.
5  */
6
7 /*
8  * $Id: sio.h,v 1.6 2003/06/10 13:15:44 steveg Exp $
9  */
10
11 #ifndef __SIO_H
12 #define __SIO_H
13
14 #include <errno.h>
15 #include <stdarg.h>
16
17 /*
18  * Naming conventions:
19  *              1) SIO functions and macros have names starting with a capital S
20  *              2) SIO constants meant to be used by user programs have 
21  *                      names starting with SIO_
22  *              3) Internal functions, struct identifiers, enum identifiers 
23  *                      etc. have names starting with __sio
24  *              4) Internal constants and macros have names starting with __SIO
25  */
26
27
28 /*
29  * external constants
30  *
31  * SIO_FLUSH_ALL: flush all output streams
32  * SIO_ERR: operation failed
33  * SIO_EOF:     eof on stream
34  * #define SIO_EOF                              (-2)
35  */
36 #define SIO_FLUSH_ALL                           (-1)
37 #define SIO_ERR                                 (-1)
38
39 /*
40  * Buffering types
41  */
42 #define SIO_FULLBUF                     0
43 #define SIO_LINEBUF                     1
44 #define SIO_NOBUF                       2
45
46 /*
47  * Descriptor for an input stream
48  */
49 struct __sio_input_descriptor
50 {
51         /*
52          * buf:         points to the buffer area.
53          *              When doing memory mapping, it is equal to the unit 
54          *              from which we are reading. When doing buffered I/O
55          *              it points to the primary buffer. The auxiliary
56          *              buffer is right below buf and is of the same size.
57          */
58         char *buf ;
59         unsigned buffer_size ;
60
61         char *start ;                 /* start of valid buffer contents         */
62         char *end ;                   /* end of valid buffer contents + 1       */
63         char *nextb ;                 /* pointer to next byte to read/write     */
64                                                                                         /* Always:  start <= nextb < end                        */
65
66         unsigned line_length ;
67         int max_line_length ;
68         int tied_fd ;
69
70         int memory_mapped ;                             /* flag to denote if we use                             */
71                                                                                         /* memory mapping                                                               */
72 } ;
73
74 typedef struct __sio_input_descriptor __sio_id_t ;
75
76
77 /*
78  * Descriptor for an output stream
79  */
80 struct __sio_output_descriptor
81 {
82         /*
83          * buf:         points to the buffer area.
84          * buf_end: is equal to buf + buffer_size
85          */
86         char *buf ;
87         char *buf_end ;
88
89         unsigned buffer_size ;
90
91         char *start ;                 /* start of valid buffer contents         */
92                                                                                         /* (used by the R and W functions)              */
93         char *nextb ;                 /* pointer to next byte to read/write  */
94                                                                                         /* Always:  start <= nextb < buf_end    */
95         int buftype ;                                           /* type of buffering                                            */
96 } ;
97
98 typedef struct __sio_output_descriptor __sio_od_t ;
99
100
101
102 /*
103  * Stream types
104  */
105 enum __sio_stream { __SIO_INPUT_STREAM, __SIO_OUTPUT_STREAM } ;
106
107
108 /*
109  * General descriptor
110  */
111 struct __sio_descriptor
112 {
113         union
114         {
115                 __sio_id_t input_descriptor ;
116                 __sio_od_t output_descriptor ;
117         } descriptor ;
118         enum __sio_stream stream_type ;
119         int initialized ;
120 } ;
121
122 typedef struct __sio_descriptor __sio_descriptor_t ;
123
124
125 /*
126  * The array of descriptors (as many as available file descriptors)
127  */
128 extern int __sio_n_descriptors ;
129 extern __sio_descriptor_t *__sio_descriptors ;
130
131
132 /*
133  * Internally used macros
134  */
135 #define __SIO_FD_INITIALIZED( fd ) \
136    (fd >= 0 && fd < __sio_n_descriptors && __sio_descriptors[ fd ].initialized)
137 #define __SIO_ID( fd )  (__sio_descriptors[ fd ].descriptor.input_descriptor)
138 #define __SIO_OD( fd )  (__sio_descriptors[ fd ].descriptor.output_descriptor)
139 #define __SIO_MUST_FLUSH( od, ch )                              \
140                         ( (od).buftype != SIO_FULLBUF &&        \
141                         ( (od).buftype == SIO_NOBUF || ch == '\n' ) )
142
143
144 /*
145  * SIO Macros:
146  *
147  *              SIOLINELEN( fd )
148  *
149  * NOTE: The maximum line size depends on whether the descriptor
150  *      was originally memory mapped. If it was, then the maximum
151  *      line size will be the map_unit_size (a function of the system
152  *      page size and PAGES_MAPPED). Otherwise, it will be either the
153  *      optimal block size as reported by stat(2) or SIO_BUFFER_SIZE.
154  */
155
156 #define SIOLINELEN( fd )      __SIO_ID( fd ).line_length 
157
158 /*
159  * The Read functions
160  */
161 char *Srdline ( int fd ) ;
162
163 /*
164  * The Write functions
165  */
166 int Swrite ( int fd, const char *buf, unsigned int nbytes );
167 int Sprint ( int fd, const char *format, ... )
168 #ifdef __GNUC__
169         __attribute__ ((format (printf, 2, 3)));
170 #else
171         ;
172 #endif
173 int Sputchar( int fd, char c );
174 int Sprintv ( int fd, const char *format, va_list ap )
175 #ifdef __GNUC__
176         __attribute__ ((format (printf, 2, 0)));
177 #else
178         ;
179 #endif
180
181 /*
182  * other functions
183  */
184 int Sdone ( int fd ) ;
185 int Sflush ( int fd ) ;
186 int Sclose ( int fd ) ;
187 int Sbuftype ( int fd, int type ) ;
188 int Smorefds ( int ) ;
189 int __sio_converter( __sio_od_t *, int , const char *, va_list );
190 int sio_setup(int fd, __sio_descriptor_t **dp, unsigned int type );
191
192 #ifdef __GNUC__
193 __attribute__ ((noreturn))
194 #endif
195 void terminate(const char *);
196
197 #endif /* __SIO_H */
198