OpenIndiana and Solaris port
[oweals/cde.git] / cde / programs / dtksh / ksh93 / src / lib / libast / include / sfio.h
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: sfio.h /main/3 1995/11/01 17:39:20 rswiston $ */
24 /***************************************************************
25 *                                                              *
26 *                      AT&T - PROPRIETARY                      *
27 *                                                              *
28 *         THIS IS PROPRIETARY SOURCE CODE LICENSED BY          *
29 *                          AT&T CORP.                          *
30 *                                                              *
31 *                Copyright (c) 1995 AT&T Corp.                 *
32 *                     All Rights Reserved                      *
33 *                                                              *
34 *           This software is licensed by AT&T Corp.            *
35 *       under the terms and conditions of the license in       *
36 *       http://www.research.att.com/orgs/ssr/book/reuse        *
37 *                                                              *
38 *               This software was created by the               *
39 *           Software Engineering Research Department           *
40 *                    AT&T Bell Laboratories                    *
41 *                                                              *
42 *               For further information contact                *
43 *                     gsf@research.att.com                     *
44 *                                                              *
45 ***************************************************************/
46 #ifndef _SFIO_H
47 #define _SFIO_H 1
48
49 /*      Public header file for the safe fast io package.
50 **
51 **      Written by Kiem-Phong Vo, kpv@research.att.com.
52 */
53
54 #ifndef __KPV__
55 #define __KPV__         1
56
57 #ifndef __STD_C
58 #ifdef __STDC__
59 #define __STD_C         1
60 #else
61 #if __cplusplus
62 #define __STD_C         1
63 #else
64 #define __STD_C         0
65 #endif /*__cplusplus*/
66 #endif /*__STDC__*/
67 #endif /*__STD_C*/
68
69 #ifndef _BEGIN_EXTERNS_
70 #if __cplusplus
71 #define _BEGIN_EXTERNS_ extern "C" {
72 #define _END_EXTERNS_   }
73 #else
74 #define _BEGIN_EXTERNS_
75 #define _END_EXTERNS_
76 #endif
77 #endif /*_BEGIN_EXTERNS_*/
78
79 #ifndef _ARG_
80 #if __STD_C
81 #define _ARG_(x)        x
82 #else
83 #define _ARG_(x)        ()
84 #endif
85 #endif /*_ARG_*/
86
87 #ifndef Void_t
88 #if __STD_C
89 #define Void_t          void
90 #else
91 #define Void_t          char
92 #endif
93 #endif /*Void_t*/
94
95 #ifndef NIL
96 #define NIL(type)       ((type)0)
97 #endif /*NIL*/
98
99 #endif /*__KPV__*/
100
101 /* mostly to prevent stupid C++ stdarg.h from including stdio.h */
102 #if __cplusplus
103 #ifndef __stdio_h__
104 #define __stdio_h__     1
105 #endif
106 #ifndef _stdio_h_
107 #define _stdio_h_       1
108 #endif
109 #ifndef _stdio_h
110 #define _stdio_h        1
111 #endif
112 #ifndef __h_stdio__
113 #define __h_stdio__     1
114 #endif
115 #ifndef _h_stdio_
116 #define _h_stdio_       1
117 #endif
118 #ifndef _h_stdio
119 #define _h_stdio        1
120 #endif
121 #ifndef __STDIO_H__
122 #define __STDIO_H__     1
123 #endif
124 #ifndef _STDIO_H_
125 #define _STDIO_H_       1
126 #endif
127 #ifndef _STDIO_H
128 #define _STDIO_H        1
129 #endif
130 #ifndef __H_STDIO__
131 #define __H_STDIO__     1
132 #endif
133 #ifndef _H_STDIO_
134 #define _H_STDIO_       1
135 #endif
136 #ifndef _H_STDIO
137 #define _H_STDIO        1
138 #endif
139 #ifndef _stdio_included
140 #define _stdio_included 1
141 #endif
142 #ifndef _included_stdio
143 #define _included_stdio 1
144 #endif
145 #ifndef _INCLUDED_STDIO
146 #define _INCLUDED_STDIO 1
147 #endif
148 #ifndef _STDIO_INCLUDED
149 #define _STDIO_INCLUDED 1
150 #endif
151
152 #if !defined(CSRG_BASED)
153 #ifndef FILE
154 #define FILE    Sfio_t
155 #endif
156 #endif
157 #endif /* __cplusplus */
158
159 #if defined(sun)
160 #define _FILEDEFED      /**/
161 #endif
162
163 typedef struct _sfio_   Sfile_t, Sfio_t, SFIO;
164 typedef struct _sfdc_   Sfdisc_t;
165 typedef int             (*Sfread_f)_ARG_((Sfio_t*, Void_t*, int, Sfdisc_t*));
166 typedef int             (*Sfwrite_f)_ARG_((Sfio_t*, const Void_t*, int, Sfdisc_t*));
167 typedef long            (*Sfseek_f)_ARG_((Sfio_t*, long, int, Sfdisc_t*));
168 typedef int             (*Sfexcept_f)_ARG_((Sfio_t*, int, Sfdisc_t*));
169
170 /* discipline structure */
171 struct _sfdc_
172 {
173         Sfread_f        readf;          /* read function                */
174         Sfwrite_f       writef;         /* write function               */
175         Sfseek_f        seekf;          /* seek function                */
176         Sfexcept_f      exceptf;        /* to handle exceptions         */
177         Sfdisc_t*       disc;           /* the continuing discipline    */
178 };
179
180 /* a file structure */
181 struct _sfio_
182 {
183         unsigned char*  next;   /* next position to read/write from     */
184         unsigned char*  endw;   /* end of write buffer                  */
185         unsigned char*  endr;   /* end of read buffer                   */
186         unsigned char*  endb;   /* end of buffer                        */
187         struct _sfio_*  push;   /* the stream that was pushed on        */
188         unsigned short  flags;  /* type of stream                       */
189         short           file;   /* file descriptor                      */
190         unsigned char*  data;   /* base of data buffer                  */
191         int             size;   /* buffer size                          */
192 #ifdef _SFIO_PRIVATE
193         _SFIO_PRIVATE
194 #endif
195 };
196
197 /* various constants */
198 #ifndef NULL
199 #define NULL    0
200 #endif
201 #ifndef EOF
202 #define EOF     (-1)
203 #endif
204 #ifndef __osf__
205 #ifndef SEEK_SET
206 #define SEEK_SET        0
207 #define SEEK_CUR        1
208 #define SEEK_END        2
209 #endif
210 #endif
211
212 /* bits for various types of files */
213 #define SF_READ         0000001 /* open for reading                     */
214 #define SF_WRITE        0000002 /* open for writing                     */
215 #define SF_STRING       0000004 /* a string stream                      */
216 #define _SF_APPEND      0000010 /* associated file is in append mode    */
217 #define SF_APPEND       _SF_APPEND      /* ask bsd about this           */
218 #define SF_MALLOC       0000020 /* buffered space malloc-ed             */
219 #define SF_LINE         0000040 /* line buffering                       */
220 #define SF_SHARE        0000100 /* file stream that is shared           */
221 #define SF_EOF          0000200 /* eof was detected                     */
222 #define SF_ERROR        0000400 /* an error happened                    */
223 #define SF_STATIC       0001000 /* a stream that cannot be freed        */
224 #define SF_IOCHECK      0002000 /* call exceptf before doing IO         */
225 #define SF_PUBLIC       0004000 /* SF_SHARE and follow physical seek    */
226
227 #define SF_FLAGS        0005177 /* PUBLIC FLAGS PASSABLE TO SFNEW()     */
228 #define SF_SETS         0007163 /* flags passable to sfset()            */
229
230 /* exception events: SF_NEW(0), SF_READ(1), SF_WRITE(2) and the below   */
231 #define SF_SEEK         3       /* seek error                           */
232 #define SF_CLOSE        4       /* when stream is being closed          */
233 #define SF_DPUSH        5       /* when discipline is being pushed      */
234 #define SF_DPOP         6       /* when discipline is being popped      */
235 #define SF_DPOLL        7       /* see if stream is ready for I/O       */
236 #define SF_DBUFFER      8       /* buffer not empty during push or pop  */
237 #define SF_SYNC         9       /* a sfsync() call was issued           */
238 #define SF_PURGE        10      /* a sfpurge() call was issued          */
239
240 /* for stack and disciplines */
241 #define SF_POPSTACK     NIL(Sfio_t*)    /* pop the stream stack         */
242 #define SF_POPDISC      NIL(Sfdisc_t*)  /* pop the discipline stack     */
243
244 /* for the notify function and discipline exception */
245 #define SF_NEW          0       /* new stream                           */
246 #define SF_SETFD        -1      /* about to set the file descriptor     */
247
248 #define SF_BUFSIZE      8192    /* suggested default buffer size        */
249 #define SF_UNBOUND      (-1)    /* unbounded buffer size                */
250
251 #if __STD_C
252 #include                <stdarg.h>
253 #endif
254
255 _BEGIN_EXTERNS_
256
257 #if _DLL_INDIRECT_DATA && _DLL
258
259 #define sfstdin         ((Sfio_t*)_ast_dll->_ast_stdin)
260 #define sfstdout        ((Sfio_t*)_ast_dll->_ast_stdout)
261 #define sfstderr        ((Sfio_t*)_ast_dll->_ast_stderr)
262
263 #else
264
265 #define sfstdin         (&_Sfstdin)
266 #define sfstdout        (&_Sfstdout)
267 #define sfstderr        (&_Sfstderr)
268
269 extern Sfio_t           _Sfstdin;               /* std input stream     */
270 extern Sfio_t           _Sfstdout;              /* std output stream    */
271 extern Sfio_t           _Sfstderr;              /* std error stream     */
272
273 #endif
274
275 extern int              _Sfi;
276
277 extern Sfio_t*          sfnew _ARG_((Sfio_t*, Void_t*, int, int, int));
278 extern Sfio_t*          sfopen _ARG_((Sfio_t*, const char*, const char*));
279 extern Sfio_t*          sfpopen _ARG_((Sfio_t*, const char*, const char*));
280 extern Sfio_t*          sfstack _ARG_((Sfio_t*, Sfio_t*));
281 extern Sfio_t*          sfswap _ARG_((Sfio_t*, Sfio_t*));
282 extern Sfio_t*          sftmp _ARG_((int));
283 extern int              _sfflsbuf _ARG_((Sfio_t*, int));
284 extern int              _sffilbuf _ARG_((Sfio_t*, int));
285 extern int              sfpurge _ARG_((Sfio_t*));
286 extern int              sfpoll _ARG_((Sfio_t**, int, int));
287 extern int              sfpeek _ARG_((Sfio_t*, Void_t**, int));
288 extern Void_t*          sfreserve _ARG_((Sfio_t*, int, int));
289 extern int              sfsync _ARG_((Sfio_t*));
290 extern int              sfclrlock _ARG_((Sfio_t*));
291 extern Void_t*          sfsetbuf _ARG_((Sfio_t*, Void_t*, int));
292 extern Sfdisc_t*        sfdisc _ARG_((Sfio_t*,Sfdisc_t*));
293 extern int              sfnotify _ARG_((void(*)(Sfio_t*, int, int)));
294 extern int              sfset _ARG_((Sfio_t*, int, int));
295 extern int              sfsetfd _ARG_((Sfio_t*, int));
296 extern Sfio_t*          sfpool _ARG_((Sfio_t*, Sfio_t*, int));
297 extern int              sfread _ARG_((Sfio_t*, Void_t*, int));
298 extern int              sfwrite _ARG_((Sfio_t*, const Void_t*, int));
299 extern long             sfmove _ARG_((Sfio_t*, Sfio_t*, long, int));
300 extern int              sfclose _ARG_((Sfio_t*));
301 extern long             sftell _ARG_((Sfio_t*));
302 extern long             sfseek _ARG_((Sfio_t*, long, int));
303 extern int              sfllen _ARG_((long));
304 extern int              sfdlen _ARG_((double));
305 extern int              sfputr _ARG_((Sfio_t*, const char*, int));
306 extern char*            sfgetr _ARG_((Sfio_t*, int, int));
307 extern int              sfnputc _ARG_((Sfio_t*, int, int));
308 extern int              _sfputu _ARG_((Sfio_t*, unsigned long));
309 extern int              _sfputl _ARG_((Sfio_t*, long));
310 extern unsigned long    _sfgetu _ARG_((Sfio_t*));
311 extern long             _sfgetl _ARG_((Sfio_t*));
312 extern int              _sfputd _ARG_((Sfio_t*, double));
313 extern double           sfgetd _ARG_((Sfio_t*));
314 extern int              sfungetc _ARG_((Sfio_t*, int));
315 extern char*            sfprints _ARG_((const char*, ...));
316 extern int              sfprintf _ARG_((Sfio_t*, const char*, ...));
317 extern int              sfsprintf _ARG_((char*, int, const char*, ...));
318 extern int              sfscanf _ARG_((Sfio_t*, const char*, ...));
319 extern int              sfsscanf _ARG_((const char*, const char*, ...));
320 extern int              sfvprintf _ARG_((Sfio_t*, const char*, va_list));
321 extern int              sfvscanf _ARG_((Sfio_t*, const char*, va_list));
322 extern char*            sfecvt _ARG_((double,int,int*,int*));
323 extern char*            sffcvt _ARG_((double,int,int*,int*));
324
325 /* io functions with discipline continuation */
326 extern int              sfrd _ARG_((Sfio_t*, Void_t*, int, Sfdisc_t*));
327 extern int              sfwr _ARG_((Sfio_t*, const Void_t*, int, Sfdisc_t*));
328 extern long             sfsk _ARG_((Sfio_t*, long, int, Sfdisc_t*));
329 extern int              sfpkrd _ARG_((int, Void_t*, int, int, long, int));
330
331 /* function analogues of fast in-line functions */
332 extern int              sfgetc _ARG_((Sfio_t*));
333 extern long             sfgetl _ARG_((Sfio_t*));
334 extern unsigned long    sfgetu _ARG_((Sfio_t*));
335 extern int              sfputc _ARG_((Sfio_t*,int));
336 extern int              sfputd _ARG_((Sfio_t*,double));
337 extern int              sfputl _ARG_((Sfio_t*,long));
338 extern int              sfputu _ARG_((Sfio_t*,unsigned long));
339 extern int              sfslen _ARG_((void));
340 extern int              sfulen _ARG_((unsigned long));
341 extern long             sfsize _ARG_((Sfio_t*));
342 extern int              sfclrerr _ARG_((Sfio_t*));
343 extern int              sfeof _ARG_((Sfio_t*));
344 extern int              sferror _ARG_((Sfio_t*));
345 extern int              sffileno _ARG_((Sfio_t*));
346 extern int              sfstacked _ARG_((Sfio_t*));
347
348 _END_EXTERNS_
349
350 /* fast in-line functions */
351 #define sfputc(f,c)     ((f)->next >= (f)->endw ? \
352                                 _sfflsbuf(f,(int)((unsigned char)(c))) : \
353                                 (int)(*(f)->next++ = (unsigned char)(c)))
354 #define sfgetc(f)       ((f)->next >= (f)->endr ? _sffilbuf(f,0) : (int)(*(f)->next++))
355 #if !_DLL_INDIRECT_DATA || _DLL
356 #define sfslen()        (_Sfi)
357 #endif
358 #define sffileno(f)     ((f)->file)
359 #define sfeof(f)        ((f)->flags&SF_EOF)
360 #define sferror(f)      ((f)->flags&SF_ERROR)
361 #define sfclrerr(f)     ((f)->flags &= ~(SF_ERROR|SF_EOF))
362 #define sfstacked(f)    ((f)->push != NIL(Sfio_t*))
363
364 /* coding long integers in a portable and compact fashion */
365 #define SF_SBITS        6
366 #define SF_UBITS        7
367 #define SF_SIGN         (1 << SF_SBITS)
368 #define SF_MORE         (1 << SF_UBITS)
369 #define SF_U1           SF_MORE
370 #define SF_U2           (SF_U1*SF_U1)
371 #define SF_U3           (SF_U2*SF_U1)
372 #define SF_U4           (SF_U3*SF_U1)
373 #define sfulen(v)       ((v) < SF_U1 ? 1 : (v) < SF_U2 ? 2 : \
374                          (v) < SF_U3 ? 3 : (v) < SF_U4 ? 4 : 5)
375 #define sfgetu(f)       ((_Sfi = sfgetc(f)) < 0 ? -1 : \
376                                 ((_Sfi&SF_MORE) ? _sfgetu(f) : (unsigned long)_Sfi))
377 #define sfgetl(f)       ((_Sfi = sfgetc(f)) < 0 ? -1 : \
378                                 ((_Sfi&(SF_MORE|SF_SIGN)) ? _sfgetl(f) : (long)_Sfi))
379 #define sfputu(f,v)     _sfputu((f),(unsigned long)(v))
380 #define sfputl(f,v)     _sfputl((f),(long)(v))
381 #define sfputd(f,v)     _sfputd((f),(double)(v))
382
383 #endif /* _SFIO_H */