Let winbind depend directly on libnet rather than through the ejs bindings.
[samba.git] / source4 / lib / appweb / ejs-2.0 / mpr / mpr.c
1 /**
2  *      @file   mpr.c
3  *      @brief  Mpr initialization 
4  *      @overview 
5  *      @remarks Most routines in this file are not thread-safe. It is the callers 
6  *              responsibility to perform all thread synchronization.
7  */
8
9 /*
10  *      @copy   default
11  *      
12  *      Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
13  *      
14  *      This software is distributed under commercial and open source licenses.
15  *      You may use the GPL open source license described below or you may acquire 
16  *      a commercial license from Mbedthis Software. You agree to be fully bound 
17  *      by the terms of either license. Consult the LICENSE.TXT distributed with 
18  *      this software for full details.
19  *      
20  *      This software is open source; you can redistribute it and/or modify it 
21  *      under the terms of the GNU General Public License as published by the 
22  *      Free Software Foundation; either version 2 of the License, or (at your 
23  *      option) any later version. See the GNU General Public License for more 
24  *      details at: http://www.mbedthis.com/downloads/gplLicense.html
25  *      
26  *      This program is distributed WITHOUT ANY WARRANTY; without even the 
27  *      implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
28  *      
29  *      This GPL license does NOT permit incorporating this software into 
30  *      proprietary programs. If you are unable to comply with the GPL, you must
31  *      acquire a commercial license to use this software. Commercial licenses 
32  *      for this software and support services are available from Mbedthis 
33  *      Software at http://www.mbedthis.com 
34  *      
35  *      @end
36  */
37
38 /********************************** Includes **********************************/
39 /*
40  *      We need to use the underlying str(cpy) routines to implement our safe
41  *      alternatives
42  */
43 #if !DOXYGEN
44 #define         UNSAFE_FUNCTIONS_OK 1
45 #endif
46
47 #include        "mpr.h"
48
49 /******************************************************************************/
50 /*
51  *      Initialize the MPR. Create the top level memory context. This routine is 
52  *      the first call an MPR application must do. If using MprServices, the 
53  *      creation of an Mpr object will call this routine.
54  */
55
56 MprApp *mprInit(MprAllocCback cback)
57 {
58         return mprInitEx(cback, 0);
59 }
60
61 /******************************************************************************/
62 /*
63  *      Add a shell parameter then do the regular init
64  */
65
66 MprApp *mprInitEx(MprAllocCback cback, void *shell)
67 {
68         MprApp  *app;
69
70         app = (MprApp*) mprAllocInit(cback);
71
72         mprAssert(app);
73         if (app == 0) {
74                 return 0;
75         }
76
77         app->name = mprStrdup(app, BLD_PRODUCT);
78         app->title = mprStrdup(app, BLD_NAME);
79         app->version = mprStrdup(app, BLD_VERSION);
80
81         mprSetShell(app, shell);
82
83         app->table = mprCreateSymbolTable(app, 0);
84
85         if (mprStartFileServices(app) < 0) {
86                 mprAllocTerm(app);
87                 return 0;
88         }
89
90 #if BLD_FEATURE_MULTITHREAD
91         mprInitThreads(app);
92 #endif
93
94         /*
95          *      See if any of the preceeding allocations failed
96          */
97         if (mprGetAllocErrors(app) > 0) {
98                 mprAllocTerm(app);
99                 return 0;
100         }
101
102         /*
103          *      Mark all blocks allocated so far as required. They will then be 
104          *      omitted from leak reports.
105          */
106         mprSetRequiredAlloc(app, 1);
107
108         return app;
109 }
110
111 /******************************************************************************/
112 /*
113  *      Terminate the MPR. If doStats is true, then output a memory allocation
114  *      report.
115  */
116
117 void mprTerm(MprApp *app, bool doStats)
118 {
119 #if BLD_FEATURE_ALLOC_STATS
120         if (doStats) {
121                 mprPrintAllocReport(app, 1, "MPR Memory Allocation Report");
122         }
123 #endif
124
125 #if BLD_FEATURE_MULTITHREAD
126         mprTermThreads(app);
127 #endif
128
129         mprStopFileServices(app);
130
131 #if BLD_DEBUG
132         mprValidateAllocTree(app);
133 #endif
134         mprAllocTerm(app);
135 }
136
137 /******************************************************************************/
138
139 bool mprIsExiting(MprCtx ctx)
140 {
141         MprApp  *app;
142
143         app = mprGetApp(ctx);
144         if (app == 0) {
145                 return 1;
146         }
147         return app->flags & MPR_APP_EXITING;
148 }
149
150 /******************************************************************************/
151
152 int mprHasAllocError(MprCtx ctx)
153 {
154         MprApp  *app;
155
156         app = mprGetApp(ctx);
157         if (app == 0) {
158                 return 1;
159         }
160
161         return app->flags & MPR_APP_ALLOC_ERROR;
162 }
163
164 /******************************************************************************/
165
166 void mprSignalExit(MprCtx ctx)
167 {
168         MprApp  *app;
169
170         app = mprGetApp(ctx);
171         app->flags |= MPR_APP_EXITING;
172 }
173
174 /******************************************************************************/
175
176 void mprSignalAllocError(MprCtx ctx)
177 {
178         MprApp  *app;
179
180         app = mprGetApp(ctx);
181         app->flags |= MPR_APP_ALLOC_ERROR;
182 }
183
184 /******************************************************************************/
185
186 int mprSetAppName(MprCtx ctx, const char *name, const char *title, 
187         const char *version)
188 {
189         MprApp  *app;
190
191         app = mprGetApp(ctx);
192
193         if (name) {
194                 mprFree(app->name);
195                 if ((app->name = mprStrdup(ctx, name)) == 0) {
196                         return MPR_ERR_CANT_ALLOCATE;
197                 }
198         }
199
200         if (title) {
201                 mprFree(app->title);
202                 if ((app->title = mprStrdup(ctx, title)) == 0) {
203                         return MPR_ERR_CANT_ALLOCATE;
204                 }
205         }
206
207         if (version) {
208                 mprFree(app->version);
209                 if ((app->version = mprStrdup(ctx, version)) == 0) {
210                         return MPR_ERR_CANT_ALLOCATE;
211                 }
212         }
213         return 0;
214 }
215
216 /******************************************************************************/
217
218 const char *mprGetAppName(MprCtx ctx)
219 {
220         MprApp  *app;
221
222         app = mprGetApp(ctx);
223         return app->name;
224 }
225
226 /******************************************************************************/
227
228 const char *mprGetAppTitle(MprCtx ctx)
229 {
230         MprApp  *app;
231
232         app = mprGetApp(ctx);
233         return app->title;
234 }
235
236 /******************************************************************************/
237
238 const char *mprGetAppVersion(MprCtx ctx)
239 {
240         MprApp  *app;
241
242         app = mprGetApp(ctx);
243         return app->version;
244 }
245
246 /******************************************************************************/
247
248 int mprSetKeyValue(MprCtx ctx, const char *key, void *ptr)
249 {
250         MprApp  *app;
251
252         app = mprGetApp(ctx);
253         if (mprInsertSymbol(app->table, key, ptr) == 0) {
254                 return MPR_ERR_CANT_WRITE;
255         }
256         return 0;
257 }
258
259 /******************************************************************************/
260
261 int mprRemoveKeyValue(MprCtx ctx, const char *key)
262 {
263         MprApp  *app;
264
265         app = mprGetApp(ctx);
266         return mprRemoveSymbol(app->table, key);
267 }
268
269 /******************************************************************************/
270
271 void *mprGetKeyValue(MprCtx ctx, const char *key)
272 {
273         MprApp  *app;
274
275         app = mprGetApp(ctx);
276         return mprLookupSymbol(app->table, key);
277 }
278
279 /******************************************************************************/
280
281 bool mprGetDebugMode(MprCtx ctx)
282 {
283         return mprGetApp(ctx)->debugMode;
284 }
285
286 /******************************************************************************/
287
288 void mprSetDebugMode(MprCtx ctx, bool on)
289 {
290         mprGetApp(ctx)->debugMode = on;
291 }
292
293 /******************************************************************************/
294
295 void mprSetLogHandler(MprCtx ctx, MprLogHandler handler)
296 {
297         mprGetApp(ctx)->logHandler = handler;
298 }
299
300 /******************************************************************************/
301
302 MprLogHandler mprGetLogHandler(MprCtx ctx)
303 {
304         return mprGetApp(ctx)->logHandler;
305 }
306
307 #if UNUSED
308 /******************************************************************************/
309
310 void mprSetMprInstance(MprCtx ctx, void *mprInstance)
311 {
312         mprGetApp(ctx)->mprInstance = mprInstance;
313 }
314
315 /******************************************************************************/
316
317 void *mprGetMprInstance(MprCtx ctx)
318 {
319         return mprGetApp(ctx)->mprInstance;
320 }
321
322 #endif
323 /******************************************************************************/
324
325 const char *mprCopyright()
326 {
327         return "Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.";
328 }
329
330 /******************************************************************************/
331
332 /*
333  * Local variables:
334  * tab-width: 4
335  * c-basic-offset: 4
336  * End:
337  * vim:tw=78
338  * vim600: sw=4 ts=4 fdm=marker
339  * vim<600: sw=4 ts=4
340  */