r7023: reduced the number of warnings in building ejs and esp
[sfrench/samba-autobuild/.git] / source4 / web_server / ejs / miniMpr.h
1 /*
2  *      @file   miniMpr.h
3  *      @brief  Mini Mbedthis Portable Runtime (MPR) Environment.
4  *      @copy   default
5  *      
6  *      Copyright (c) Mbedthis Software LLC, 2003-2005. All Rights Reserved.
7  *      
8  *      This software is distributed under commercial and open source licenses.
9  *      You may use the GPL open source license described below or you may acquire 
10  *      a commercial license from Mbedthis Software. You agree to be fully bound 
11  *      by the terms of either license. Consult the LICENSE.TXT distributed with 
12  *      this software for full details.
13  *      
14  *      This software is open source; you can redistribute it and/or modify it 
15  *      under the terms of the GNU General Public License as published by the 
16  *      Free Software Foundation; either version 2 of the License, or (at your 
17  *      option) any later version. See the GNU General Public License for more 
18  *      details at: http://www.mbedthis.com/downloads/gplLicense.html
19  *      
20  *      This program is distributed WITHOUT ANY WARRANTY; without even the 
21  *      implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
22  *      
23  *      This GPL license does NOT permit incorporating this software into 
24  *      proprietary programs. If you are unable to comply with the GPL, you must
25  *      acquire a commercial license to use this software. Commercial licenses 
26  *      for this software and support services are available from Mbedthis 
27  *      Software at http://www.mbedthis.com 
28  *      
29  *      @end
30  */
31 #ifndef _h_MINI_MPR
32 #define _h_MINI_MPR 1
33
34 /********************************** Includes **********************************/
35 /*
36  *      Find out about our configuration
37  */
38 #ifndef _INCLUDES_H
39         #include        "includes.h"
40 #endif
41
42 /* allow this library to use strcpy() */
43 #undef strcpy
44         #include        "config.h"
45
46 #if BLD_APPWEB
47         /*
48          *      If building within AppWeb, use the full MPR
49          */
50         #include        "mpr.h"
51 #else
52
53         #include        <ctype.h>
54         #include        <fcntl.h>
55         #include        <stdarg.h>
56         #include        <stdlib.h>
57         #include        <stdio.h>
58         #include        <string.h>
59         #include        <sys/stat.h>
60
61 #if !WIN
62         #include        <unistd.h>
63 #endif
64
65 #if CE
66         #include        <io.h>
67         #include        "CE/wincompat.h"
68 #endif
69
70 #if LYNX
71         #include        <unistd.h>
72 #endif
73
74 #if QNX4
75         #include        <dirent.h>
76 #endif
77
78 /********************************** Defines ***********************************/
79
80 #ifdef __cplusplus
81 extern "C" {
82 #endif
83
84 #if BLD_FEATURE_SQUEEZE
85 ///
86 ///     Reasonable length of a file path name to use in most cases where you know
87 ///     the expected file name and it is certain to be less than this limit.
88 ///
89 #define MPR_MAX_FNAME                   128
90 #define MPR_MAX_STRING                  512
91 #define MPR_DEFAULT_HASH_SIZE   23                      // Default size of hash table index 
92 #define MPR_MAX_HEAP_SIZE               (32 * 1024)
93 #else
94 #define MPR_MAX_FNAME                   256
95 #define MPR_MAX_STRING                  4096
96 #define MPR_DEFAULT_HASH_SIZE   43                      // Default size of hash table index 
97 #define MPR_MAX_HEAP_SIZE               (64 * 1024)
98 #endif
99
100 /*
101  *      Useful for debugging
102  */
103 #define MPR_L                   __FILE__, __LINE__
104
105 #if BLD_FEATURE_ASSERT
106 #define mprAssert(C)  \
107         if (C) ; else mprBreakpoint(__FILE__, __LINE__, #C)
108 #else
109         #define mprAssert(C)    if (1) ; else
110 #endif
111
112 ///
113 ///     Standard MPR return and error codes 
114 ///
115 #define MPR_ERR_BASE                                    (-200)                          ///< Error code
116 #define MPR_ERR_GENERAL                                 (MPR_ERR_BASE - 1)      ///< Error code
117 #define MPR_ERR_ABORTED                                 (MPR_ERR_BASE - 2)      ///< Error code
118 #define MPR_ERR_ALREADY_EXISTS                  (MPR_ERR_BASE - 3)      ///< Error code
119 #define MPR_ERR_BAD_ARGS                                (MPR_ERR_BASE - 4)      ///< Error code
120 #define MPR_ERR_BAD_FORMAT                              (MPR_ERR_BASE - 5)      ///< Error code
121 #define MPR_ERR_BAD_HANDLE                              (MPR_ERR_BASE - 6)      ///< Error code
122 #define MPR_ERR_BAD_STATE                               (MPR_ERR_BASE - 7)      ///< Error code
123 #define MPR_ERR_BAD_SYNTAX                              (MPR_ERR_BASE - 8)      ///< Error code
124 #define MPR_ERR_BAD_TYPE                                (MPR_ERR_BASE - 9)      ///< Error code
125 #define MPR_ERR_BAD_VALUE                               (MPR_ERR_BASE - 10)     ///< Error code
126 #define MPR_ERR_BUSY                                    (MPR_ERR_BASE - 11)     ///< Error code
127 #define MPR_ERR_CANT_ACCESS                             (MPR_ERR_BASE - 12)     ///< Error code
128 #define MPR_ERR_CANT_COMPLETE                   (MPR_ERR_BASE - 13)     ///< Error code
129 #define MPR_ERR_CANT_CREATE                             (MPR_ERR_BASE - 14)     ///< Error code
130 #define MPR_ERR_CANT_INITIALIZE                 (MPR_ERR_BASE - 15)     ///< Error code
131 #define MPR_ERR_CANT_OPEN                               (MPR_ERR_BASE - 16)     ///< Error code
132 #define MPR_ERR_CANT_READ                               (MPR_ERR_BASE - 17)     ///< Error code
133 #define MPR_ERR_CANT_WRITE                              (MPR_ERR_BASE - 18)     ///< Error code
134 #define MPR_ERR_DELETED                                 (MPR_ERR_BASE - 19)     ///< Error code
135 #define MPR_ERR_NETWORK                                 (MPR_ERR_BASE - 20)     ///< Error code
136 #define MPR_ERR_NOT_FOUND                               (MPR_ERR_BASE - 21)     ///< Error code
137 #define MPR_ERR_NOT_INITIALIZED                 (MPR_ERR_BASE - 22)     ///< Error code
138 #define MPR_ERR_NOT_READY                               (MPR_ERR_BASE - 23)     ///< Error code
139 #define MPR_ERR_READ_ONLY                               (MPR_ERR_BASE - 24)     ///< Error code
140 #define MPR_ERR_TIMEOUT                                 (MPR_ERR_BASE - 25)     ///< Error code
141 #define MPR_ERR_TOO_MANY                                (MPR_ERR_BASE - 26)     ///< Error code
142 #define MPR_ERR_WONT_FIT                                (MPR_ERR_BASE - 27)     ///< Error code
143 #define MPR_ERR_WOULD_BLOCK                             (MPR_ERR_BASE - 28)     ///< Error code
144 #define MPR_ERR_CANT_ALLOCATE                   (MPR_ERR_BASE - 29)     ///< Error code
145 #define MPR_ERR_MAX                                             (MPR_ERR_BASE - 30)     ///< Error code
146
147 //
148 //      Standard error severity and trace levels. These are ored with the error 
149 //      severities below. The MPR_LOG_MASK is used to extract the trace level 
150 //      from a flags word. We expect most apps to run with level 2 trace.
151 //
152 #define MPR_FATAL               0                               ///< Fatal error. Cant continue.
153 #define MPR_ERROR               1                               ///< Hard error
154 #define MPR_WARN                2                               ///< Soft warning
155 #define MPR_CONFIG              2                               ///< Essential configuration settings 
156 #define MPR_INFO                3                               ///< Informational only 
157 #define MPR_DEBUG               4                               ///< Debug information 
158 #define MPR_VERBOSE             9                               ///< Highest level of trace 
159 #define MPR_LOG_MASK    0xf                             ///< Level mask 
160
161 //
162 //      Error flags. Specify where the error should be sent to. Note that the 
163 //      product.xml setting "headless" will modify how errors are reported.
164 //      Assert errors are trapped when in DEV mode. Otherwise ignored.
165 //
166 #define MPR_TRAP                0x10                    ///< Assert error -- trap in debugger 
167 #define MPR_LOG                 0x20                    ///< Log the error in the O/S event log
168 #define MPR_USER                0x40                    ///< Display to the user 
169 #define MPR_ALERT               0x80                    ///< Send a management alert 
170 #define MPR_TRACE               0x100                   ///< Trace
171
172 //
173 //      Error format flags
174 //
175 #define MPR_RAW                 0x200                   // Raw trace output
176
177 //
178 //      Error line number information
179 //
180 #define MPR_L           __FILE__, __LINE__
181
182 typedef char*                   MprStr;
183
184 #ifndef __cplusplus
185 typedef unsigned char   uchar;
186 typedef int                     bool;
187 #endif
188
189 /*
190  *      Porters: put other operating system type defines here
191  */
192 #if WIN
193         typedef unsigned int            uint;
194         typedef __int64                         int64;
195         typedef unsigned __int64        uint64;
196 #else
197 #define O_BINARY 0
198 #ifndef uint
199         #define uint unsigned
200 #endif
201         __extension__ typedef long long int int64;
202         __extension__ typedef unsigned long long int uint64;
203 #endif
204
205 /*
206  *      Flexible array data type
207  */
208 typedef struct {
209         int             max;                                            /* Size of the handles array */
210         int             used;                                           /* Count of used entries in handles */
211         void    **handles;
212 } MprArray;
213
214 #if BLD_FEATURE_SQUEEZE
215 #define MPR_ARRAY_INCR          8
216 #else
217 #define MPR_ARRAY_INCR          16
218 #endif
219
220 #ifndef max
221 #define max(a,b)  (((a) > (b)) ? (a) : (b))
222 #endif
223
224 /********************************* Prototypes *********************************/
225 /*
226  *      If running in the GoAhead WebServer, map some MPR routines to WebServer
227  *      equivalents.
228  */
229
230 #if BLD_GOAHEAD_WEBSERVER
231 #include "uemf.h"
232 #define mprMalloc(size) balloc(B_L, size)
233 #define mprFree(ptr) bfreeSafe(B_L, ptr)
234 #define mprRealloc(ptr, size) brealloc(B_L, ptr, size)
235 #define mprStrdup(ptr) bstrdup(B_L, ptr)
236 #define mprAllocSprintf fmtAlloc
237 #define mprAllocVsprintf fmtValloc
238 #define mprSprintf fmtStatic
239 #define mprItoa stritoa
240 #define mprLog trace
241 #define mprBreakpoint(file, line, cond) \
242         error(file, line, E_BLD_FEATURE_ASSERT, T("%s"), cond)
243
244 #else /* !BLD_GOAHEAD_WEBSERVER */
245 //#define mprMalloc malloc
246 #define mprSprintf snprintf
247 #define mtVsprintf vsnprintf
248 extern void             *mprMalloc(uint size);
249 extern void             *mprRealloc(void *ptr, uint size);
250 extern void     mprFree(void *ptr);
251 extern char             *mprStrdup(const char *str);
252 extern int              mprAllocVsprintf(char **msgbuf, int maxSize, const char *fmt, 
253                                                                  va_list args) PRINTF_ATTRIBUTE(3,0);
254 extern int              mprAllocSprintf(char **msgbuf, int maxSize, const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
255 extern char     *mprItoa(int num, char *buf, int width);
256 extern void             mprLog(int level, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
257 extern void             mprBreakpoint(const char *file, int line, const char *msg);
258 #endif /* BLD_GOAHEAD_WEBSERVER */
259
260 extern MprArray *mprCreateArray(void);
261 extern void     mprDestroyArray(MprArray *array);
262 extern int              mprAddToArray(MprArray *array, void *item);
263 extern int              mprRemoveFromArray(MprArray *array, int idx);
264 extern char     *mprStrTok(char *str, const char *delim, char **tok);
265
266 extern int              mprGetDirName(char *buf, int bufsize, char *path);
267 extern int              mprReallocStrcat(char **dest, int max, int existingLen,
268                                                 const char *delim, const char *src, ...);
269 extern int              mprStrcpy(char *dest, int destMax, const char *src);
270 extern int              mprMemcpy(char *dest, int destMax, const char *src, int nbytes);
271
272 extern void mprSetCtx(void *ctx);
273
274 #ifdef __cplusplus
275 }
276 #endif
277 #endif /* !BLD_APPWEB */
278 #endif /* _h_MINI_MPR */
279
280 /*****************************************************************************/
281
282 /*
283  * Local variables:
284  * tab-width: 4
285  * c-basic-offset: 4
286  * End:
287  * vim:tw=78
288  * vim600: sw=4 ts=4 fdm=marker
289  * vim<600: sw=4 ts=4
290  */