Add support for implementing LDB modules in Python.
[ira/wip.git] / source4 / lib / appweb / mpr / 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        "lib/appweb/ejs/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 #ifdef HAVE_MATH_H
78         #include        <math.h>
79 #endif
80 /********************************** Defines ***********************************/
81
82 #ifdef __cplusplus
83 extern "C" {
84 #endif
85
86 #if BLD_FEATURE_SQUEEZE
87 /*
88  *      Reasonable length of a file path name to use in most cases where you know
89  *      the expected file name and it is certain to be less than this limit.
90  */
91 #define MPR_MAX_FNAME                   128
92 #define MPR_MAX_STRING                  512
93 #define MPR_DEFAULT_HASH_SIZE   23                      /* Default size of hash table index  */
94 #define MPR_MAX_HEAP_SIZE               (32 * 1024)
95 #else
96 #define MPR_MAX_FNAME                   256
97 #define MPR_MAX_STRING                  4096
98 #define MPR_DEFAULT_HASH_SIZE   43                      /* Default size of hash table index  */
99 #define MPR_MAX_HEAP_SIZE               (64 * 1024)
100 #endif
101
102 /*
103  *      Useful for debugging
104  */
105 #define MPR_L                   __FILE__, __LINE__
106
107 #if BLD_FEATURE_ASSERT
108 #define mprAssert(C)  \
109         if (C) ; else mprBreakpoint(__FILE__, __LINE__, #C)
110 #else
111         #define mprAssert(C)    if (1) ; else
112 #endif
113
114 /*
115  *      Standard MPR return and error codes
116  */
117 #define MPR_ERR_BASE                                    (-200)                          /* Error code */
118 #define MPR_ERR_GENERAL                                 (MPR_ERR_BASE - 1)      /* Error code */
119 #define MPR_ERR_ABORTED                                 (MPR_ERR_BASE - 2)      /* Error code */
120 #define MPR_ERR_ALREADY_EXISTS                  (MPR_ERR_BASE - 3)      /* Error code */
121 #define MPR_ERR_BAD_ARGS                                (MPR_ERR_BASE - 4)      /* Error code */
122 #define MPR_ERR_BAD_FORMAT                              (MPR_ERR_BASE - 5)      /* Error code */
123 #define MPR_ERR_BAD_HANDLE                              (MPR_ERR_BASE - 6)      /* Error code */
124 #define MPR_ERR_BAD_STATE                               (MPR_ERR_BASE - 7)      /* Error code */
125 #define MPR_ERR_BAD_SYNTAX                              (MPR_ERR_BASE - 8)      /* Error code */
126 #define MPR_ERR_BAD_TYPE                                (MPR_ERR_BASE - 9)      /* Error code */
127 #define MPR_ERR_BAD_VALUE                               (MPR_ERR_BASE - 10)     /* Error code */
128 #define MPR_ERR_BUSY                                    (MPR_ERR_BASE - 11)     /* Error code */
129 #define MPR_ERR_CANT_ACCESS                             (MPR_ERR_BASE - 12)     /* Error code */
130 #define MPR_ERR_CANT_COMPLETE                   (MPR_ERR_BASE - 13)     /* Error code */
131 #define MPR_ERR_CANT_CREATE                             (MPR_ERR_BASE - 14)     /* Error code */
132 #define MPR_ERR_CANT_INITIALIZE                 (MPR_ERR_BASE - 15)     /* Error code */
133 #define MPR_ERR_CANT_OPEN                               (MPR_ERR_BASE - 16)     /* Error code */
134 #define MPR_ERR_CANT_READ                               (MPR_ERR_BASE - 17)     /* Error code */
135 #define MPR_ERR_CANT_WRITE                              (MPR_ERR_BASE - 18)     /* Error code */
136 #define MPR_ERR_DELETED                                 (MPR_ERR_BASE - 19)     /* Error code */
137 #define MPR_ERR_NETWORK                                 (MPR_ERR_BASE - 20)     /* Error code */
138 #define MPR_ERR_NOT_FOUND                               (MPR_ERR_BASE - 21)     /* Error code */
139 #define MPR_ERR_NOT_INITIALIZED                 (MPR_ERR_BASE - 22)     /* Error code */
140 #define MPR_ERR_NOT_READY                               (MPR_ERR_BASE - 23)     /* Error code */
141 #define MPR_ERR_READ_ONLY                               (MPR_ERR_BASE - 24)     /* Error code */
142 #define MPR_ERR_TIMEOUT                                 (MPR_ERR_BASE - 25)     /* Error code */
143 #define MPR_ERR_TOO_MANY                                (MPR_ERR_BASE - 26)     /* Error code */
144 #define MPR_ERR_WONT_FIT                                (MPR_ERR_BASE - 27)     /* Error code */
145 #define MPR_ERR_WOULD_BLOCK                             (MPR_ERR_BASE - 28)     /* Error code */
146 #define MPR_ERR_CANT_ALLOCATE                   (MPR_ERR_BASE - 29)     /* Error code */
147 #define MPR_ERR_MAX                                             (MPR_ERR_BASE - 30)     /* Error code */
148
149 /*
150  *      Standard error severity and trace levels. These are ored with the error
151  *      severities below. The MPR_LOG_MASK is used to extract the trace level
152  *      from a flags word. We expect most apps to run with level 2 trace.
153  */
154 #define MPR_FATAL               0                               /* Fatal error. Cant continue. */
155 #define MPR_ERROR               1                               /* Hard error */
156 #define MPR_WARN                2                               /* Soft warning */
157 #define MPR_CONFIG              2                               /* Essential configuration settings  */
158 #define MPR_INFO                3                               /* Informational only  */
159 #define MPR_DEBUG               4                               /* Debug information  */
160 #define MPR_VERBOSE             9                               /* Highest level of trace  */
161 #define MPR_LOG_MASK    0xf                             /* Level mask  */
162
163 /*
164  *      Error flags. Specify where the error should be sent to. Note that the
165  *      product.xml setting "headless" will modify how errors are reported.
166  *      Assert errors are trapped when in DEV mode. Otherwise ignored.
167  */
168 #define MPR_TRAP                0x10                    /* Assert error -- trap in debugger  */
169 #define MPR_LOG                 0x20                    /* Log the error in the O/S event log */
170 #define MPR_USER                0x40                    /* Display to the user  */
171 #define MPR_ALERT               0x80                    /* Send a management alert  */
172 #define MPR_TRACE               0x100                   /* Trace */
173
174 /*
175  *      Error format flags
176  */
177 #define MPR_RAW                 0x200                   /* Raw trace output */
178
179 /*
180  *      Error line number information
181  */
182 #define MPR_L           __FILE__, __LINE__
183
184 typedef char*                   MprStr;
185
186 #ifndef __cplusplus
187 typedef unsigned char   uchar;
188 #endif
189
190 /*
191  *      Porters: put other operating system type defines here
192  */
193 #if WIN
194         typedef unsigned int            uint;
195         typedef __int64                         int64;
196         typedef unsigned __int64        uint64;
197 #else
198 #define O_BINARY 0
199 #ifndef uint
200         #define uint unsigned
201 #endif
202         #define int64 int64_t
203         #define uint64 uint64_t
204 #endif
205
206 /*
207  *      Flexible array data type
208  */
209 typedef struct {
210         int             max;                                            /* Size of the handles array */
211         int             used;                                           /* Count of used entries in handles */
212         void    **handles;
213 } MprArray;
214
215 #if BLD_FEATURE_SQUEEZE
216 #define MPR_ARRAY_INCR          8
217 #else
218 #define MPR_ARRAY_INCR          16
219 #endif
220
221 #ifndef max
222 #define max(a,b)  (((a) > (b)) ? (a) : (b))
223 #endif
224
225 /********************************* Prototypes *********************************/
226 /*
227  *      If running in the GoAhead WebServer, map some MPR routines to WebServer
228  *      equivalents.
229  */
230
231 #if BLD_GOAHEAD_WEBSERVER
232 #include "uemf.h"
233 #define mprMalloc(size) balloc(B_L, size)
234 #define mprFree(ptr) bfreeSafe(B_L, ptr)
235 #define mprRealloc(ptr, size) brealloc(B_L, ptr, size)
236 #define mprStrdup(ptr) bstrdup(B_L, ptr)
237 #define mprAllocSprintf fmtAlloc
238 #define mprAllocVsprintf fmtValloc
239 #define mprSprintf fmtStatic
240 #define mprItoa stritoa
241 #define mprLog trace
242 #define mprBreakpoint(file, line, cond) \
243         error(file, line, E_BLD_FEATURE_ASSERT, T("%s"), cond)
244
245 #else /* !BLD_GOAHEAD_WEBSERVER */
246 /* #define mprMalloc malloc */
247 #define mprSprintf snprintf
248 #define mtVsprintf vsnprintf
249 extern void             *mprMalloc(uint size);
250 extern void             *mprRealloc(void *ptr, uint size);
251 extern void     mprFree(void *ptr);
252 extern char             *mprStrdup(const char *str);
253 extern int              mprAllocVsprintf(char **msgbuf, int maxSize, const char *fmt, 
254                                                                  va_list args) PRINTF_ATTRIBUTE(3,0);
255 extern int              mprAllocSprintf(char **msgbuf, int maxSize, const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
256 extern char     *mprItoa(int num, char *buf, int width);
257 extern void             mprLog(int level, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
258 extern void             mprBreakpoint(const char *file, int line, const char *msg) _NORETURN_;
259 #endif /* BLD_GOAHEAD_WEBSERVER */
260
261 extern MprArray *mprCreateArray(void);
262 extern void     mprDestroyArray(MprArray *array);
263 extern int              mprAddToArray(MprArray *array, void *item);
264 extern int              mprRemoveFromArray(MprArray *array, int idx);
265 extern char     *mprStrTok(char *str, const char *delim, char **tok);
266
267 extern int              mprGetDirName(char *buf, int bufsize, char *path);
268 extern int              mprReallocStrcat(char **dest, int max, int existingLen,
269                                                 const char *delim, const char *src, ...);
270 extern int              mprStrcpy(char *dest, int destMax, const char *src);
271 extern int              mprMemcpy(char *dest, int destMax, const char *src, int nbytes);
272
273 extern void mprSetCtx(void *ctx);
274 extern void *mprMemCtx(void);
275 struct loadparm_context;
276 extern struct loadparm_context *mprLpCtx(void);
277 struct event_context;
278 extern struct event_context *mprEventCtx(void);
279
280 /* This function needs to be provided by anyone using ejs */
281 void ejs_exception(const char *reason);
282
283 #define mprStrCmpAnyCase(s1, s2) strcasecmp_m(s1, s2)
284
285 #ifdef __cplusplus
286 }
287 #endif
288 #endif /* !BLD_APPWEB */
289 #endif /* _h_MINI_MPR */
290
291 /*****************************************************************************/
292
293 /*
294  * Local variables:
295  * tab-width: 4
296  * c-basic-offset: 4
297  * End:
298  * vim:tw=78
299  * vim600: sw=4 ts=4 fdm=marker
300  * vim<600: sw=4 ts=4
301  */