1 /******************************************************************************
2  * $Id$
3  *
4  * Project:  MapServer
5  * Purpose:  Definitions for MapServer IO redirection capability.
6  * Author:   Frank Warmerdam, warmerdam@pobox.com
7  *
8  ******************************************************************************
9  * Copyright (c) 2004, Frank Warmerdam <warmerdam@pobox.com>
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in
19  * all copies of this Software or works derived from this Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  ****************************************************************************/
29 
30 #ifndef MAPIO_H
31 #define MAPIO_H
32 
33 /*
34 ** We deliberately emulate stdio functions in the msIO cover functions.
35 ** This makes it easy for folks to understand the semantics, and means we
36 ** can just #define things to use stdio in the simplest case.
37 */
38 
39 #include <stdarg.h>
40 #include "maphash.h"
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 #ifndef MS_PRINT_FUNC_FORMAT
47 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
48 #define MS_PRINT_FUNC_FORMAT( format_idx, arg_idx )  __attribute__((__format__ (__printf__, format_idx, arg_idx)))
49 #else
50 #define MS_PRINT_FUNC_FORMAT( format_idx, arg_idx )
51 #endif
52 #endif
53 
54   /* stdio analogs */
55   int MS_DLL_EXPORT msIO_printf( const char *format, ... ) MS_PRINT_FUNC_FORMAT(1,2);
56   int MS_DLL_EXPORT msIO_fprintf( FILE *stream, const char *format, ... ) MS_PRINT_FUNC_FORMAT(2,3);
57   int MS_DLL_EXPORT msIO_fwrite( const void *ptr, size_t size, size_t nmemb, FILE *stream );
58   int MS_DLL_EXPORT msIO_fread( void *ptr, size_t size, size_t nmemb, FILE *stream );
59   int MS_DLL_EXPORT msIO_vfprintf( FILE *fp, const char *format, va_list ap ) MS_PRINT_FUNC_FORMAT(2,0);
60 
61   /*
62   ** Definitions for the callback function and the details of the IO
63   ** channel contexts.
64   */
65 
66   typedef int (*msIO_llReadWriteFunc)( void *cbData, void *data, int byteCount );
67 
68   typedef struct msIOContext_t {
69     const char           *label;
70     int                   write_channel; /* 1 for stdout/stderr, 0 for stdin */
71     msIO_llReadWriteFunc  readWriteFunc;
72     void                  *cbData;
73   } msIOContext;
74 
75   int MS_DLL_EXPORT msIO_installHandlers( msIOContext *stdin_context,
76                                           msIOContext *stdout_context,
77                                           msIOContext *stderr_context );
78   msIOContext MS_DLL_EXPORT *msIO_getHandler( FILE * );
79   void MS_DLL_EXPORT msIO_setHeaderEnabled(int bFlag);
80   void msIO_setHeader (const char *header, const char* value, ...) MS_PRINT_FUNC_FORMAT(2,3);
81   void msIO_sendHeaders(void);
82 
83   /*
84   ** These can be used instead of the stdio style functions if you have
85   ** msIOContext's for the channel in question.
86   */
87   int msIO_contextRead( msIOContext *context, void *data, int byteCount );
88   int msIO_contextWrite( msIOContext *context, const void *data, int byteCount );
89 
90   /*
91   ** For redirecting IO to a memory buffer.
92   */
93 
94   typedef struct {
95     unsigned char *data;
96     int            data_len;    /* really buffer length */
97     int            data_offset; /* really buffer used */
98   } msIOBuffer;
99 
100   int MS_DLL_EXPORT msIO_bufferRead( void *, void *, int );
101   int MS_DLL_EXPORT msIO_bufferWrite( void *, void *, int );
102 
103   void MS_DLL_EXPORT msIO_resetHandlers(void);
104   void MS_DLL_EXPORT msIO_installStdoutToBuffer(void);
105   void MS_DLL_EXPORT msIO_installStdinFromBuffer(void);
106   void MS_DLL_EXPORT msIO_Cleanup(void);
107   char MS_DLL_EXPORT *msIO_stripStdoutBufferContentType(void);
108   void MS_DLL_EXPORT msIO_stripStdoutBufferContentHeaders(void);
109   hashTableObj MS_DLL_EXPORT *msIO_getAndStripStdoutBufferMimeHeaders(void);
110 
111   msIOContext *msIO_pushStdoutToBufferAndGetOldContext(void);
112   void msIO_restoreOldStdoutContext(msIOContext *context_to_restore);
113 
114   int MS_DLL_EXPORT msIO_isStdContext(void);
115 
116 
117   /* this is just for setting normal stdout's to binary mode on windows */
118 
119   int msIO_needBinaryStdout( void );
120   int msIO_needBinaryStdin( void );
121 
122 #ifdef __cplusplus
123 }
124 #endif
125 
126 #endif /* nef MAPIO_H */
127