1 | /* |
---|
2 | * uemf.c -- GoAhead Micro Embedded Management Framework |
---|
3 | * |
---|
4 | * Copyright (c) GoAhead Software Inc., 1995-2000. All Rights Reserved. |
---|
5 | * |
---|
6 | * See the file "license.txt" for usage and redistribution license requirements |
---|
7 | * |
---|
8 | * $Id$ |
---|
9 | */ |
---|
10 | |
---|
11 | /********************************** Description *******************************/ |
---|
12 | |
---|
13 | /* |
---|
14 | * This module provides compatibility with the full GoAhead EMF. |
---|
15 | * It is a collection of routines which permits the GoAhead WebServer to |
---|
16 | * run stand-alone and to also load as a solution pack under the GoAhead EMF. |
---|
17 | */ |
---|
18 | |
---|
19 | /*********************************** Includes *********************************/ |
---|
20 | |
---|
21 | #include "uemf.h" |
---|
22 | |
---|
23 | /********************************** Local Data ********************************/ |
---|
24 | |
---|
25 | int emfInst; /* Application instance handle */ |
---|
26 | |
---|
27 | /****************************** Forward Declarations **************************/ |
---|
28 | |
---|
29 | extern void defaultErrorHandler(int etype, char_t *buf); |
---|
30 | static void (*errorHandler)(int etype, char_t *msg) = defaultErrorHandler; |
---|
31 | |
---|
32 | extern void defaultTraceHandler(int level, char_t *buf); |
---|
33 | static void (*traceHandler)(int level, char_t *buf) = defaultTraceHandler; |
---|
34 | |
---|
35 | /************************************* Code ***********************************/ |
---|
36 | /* |
---|
37 | * Error message that doesn't need user attention. Customize this code |
---|
38 | * to direct error messages to wherever the developer wishes |
---|
39 | */ |
---|
40 | |
---|
41 | void error(E_ARGS_DEC, int etype, char_t *fmt, ...) |
---|
42 | { |
---|
43 | va_list args; |
---|
44 | char_t *fmtBuf, *buf; |
---|
45 | |
---|
46 | va_start(args, fmt); |
---|
47 | fmtValloc(&fmtBuf, E_MAX_ERROR, fmt, args); |
---|
48 | |
---|
49 | if (etype == E_LOG) { |
---|
50 | fmtAlloc(&buf, E_MAX_ERROR, T("%s\n"), fmtBuf); |
---|
51 | /*#ifdef DEV*/ |
---|
52 | } else if (etype == E_ASSERT) { |
---|
53 | fmtAlloc(&buf, E_MAX_ERROR, |
---|
54 | T("Assertion %s, failed at %s %d\n"), fmtBuf, E_ARGS); |
---|
55 | /*#endif*/ |
---|
56 | } else if (etype == E_USER) { |
---|
57 | fmtAlloc(&buf, E_MAX_ERROR, T("%s\n"), fmtBuf); |
---|
58 | } |
---|
59 | /* |
---|
60 | * bugfix -- if etype is not E_LOG, E_ASSERT, or E_USER, the call to |
---|
61 | * bfreeSafe(B_L, buf) below will fail, because 'buf' is randomly |
---|
62 | * initialized. To be nice, we format a message saying that this is an |
---|
63 | * unknown message type, and in doing so give buf a valid value. Thanks |
---|
64 | * to Simon Byholm. |
---|
65 | */ |
---|
66 | else { |
---|
67 | fmtAlloc(&buf, E_MAX_ERROR, T("Unknown error")); |
---|
68 | } |
---|
69 | va_end(args); |
---|
70 | |
---|
71 | bfree(B_L, fmtBuf); |
---|
72 | |
---|
73 | if (errorHandler) { |
---|
74 | errorHandler(etype, buf); |
---|
75 | } |
---|
76 | |
---|
77 | bfreeSafe(B_L, buf); |
---|
78 | } |
---|
79 | |
---|
80 | /******************************************************************************/ |
---|
81 | /* |
---|
82 | * Replace the default error handler. Return pointer to old error handler. |
---|
83 | */ |
---|
84 | |
---|
85 | void (*errorSetHandler(void (*function)(int etype, char_t *msg))) \ |
---|
86 | (int etype, char_t *msg) |
---|
87 | { |
---|
88 | void (*oldHandler)(int etype, char_t *buf); |
---|
89 | |
---|
90 | oldHandler = errorHandler; |
---|
91 | errorHandler = function; |
---|
92 | return oldHandler; |
---|
93 | } |
---|
94 | |
---|
95 | /******************************************************************************/ |
---|
96 | /* |
---|
97 | * Trace log. Customize this function to log trace output |
---|
98 | */ |
---|
99 | |
---|
100 | void trace(int level, char_t *fmt, ...) |
---|
101 | { |
---|
102 | va_list args; |
---|
103 | char_t *buf; |
---|
104 | |
---|
105 | va_start(args, fmt); |
---|
106 | fmtValloc(&buf, VALUE_MAX_STRING, fmt, args); |
---|
107 | |
---|
108 | if (traceHandler) { |
---|
109 | traceHandler(level, buf); |
---|
110 | } |
---|
111 | bfreeSafe(B_L, buf); |
---|
112 | va_end(args); |
---|
113 | } |
---|
114 | |
---|
115 | /******************************************************************************/ |
---|
116 | /* |
---|
117 | * Trace log. Customize this function to log trace output |
---|
118 | */ |
---|
119 | |
---|
120 | void traceRaw(char_t *buf) |
---|
121 | { |
---|
122 | if (traceHandler) { |
---|
123 | traceHandler(0, buf); |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | /******************************************************************************/ |
---|
128 | /* |
---|
129 | * Replace the default trace handler. Return a pointer to the old handler. |
---|
130 | */ |
---|
131 | |
---|
132 | void (*traceSetHandler(void (*function)(int level, char_t *buf))) |
---|
133 | (int level, char *buf) |
---|
134 | { |
---|
135 | void (*oldHandler)(int level, char_t *buf); |
---|
136 | |
---|
137 | oldHandler = traceHandler; |
---|
138 | if (function) { |
---|
139 | traceHandler = function; |
---|
140 | } |
---|
141 | return oldHandler; |
---|
142 | } |
---|
143 | |
---|
144 | /******************************************************************************/ |
---|
145 | /* |
---|
146 | * Save the instance handle |
---|
147 | */ |
---|
148 | |
---|
149 | void emfInstSet(int inst) |
---|
150 | { |
---|
151 | emfInst = inst; |
---|
152 | } |
---|
153 | |
---|
154 | /******************************************************************************/ |
---|
155 | /* |
---|
156 | * Get the instance handle |
---|
157 | */ |
---|
158 | |
---|
159 | int emfInstGet() |
---|
160 | { |
---|
161 | return emfInst; |
---|
162 | } |
---|
163 | |
---|
164 | /******************************************************************************/ |
---|
165 | /* |
---|
166 | * Convert a string to lower case |
---|
167 | */ |
---|
168 | |
---|
169 | char_t *strlower(char_t *string) |
---|
170 | { |
---|
171 | char_t *s; |
---|
172 | |
---|
173 | a_assert(string); |
---|
174 | |
---|
175 | if (string == NULL) { |
---|
176 | return NULL; |
---|
177 | } |
---|
178 | |
---|
179 | s = string; |
---|
180 | while (*s) { |
---|
181 | if (gisupper(*s)) { |
---|
182 | *s = (char_t) gtolower(*s); |
---|
183 | } |
---|
184 | s++; |
---|
185 | } |
---|
186 | *s = '\0'; |
---|
187 | return string; |
---|
188 | } |
---|
189 | |
---|
190 | /******************************************************************************/ |
---|
191 | /* |
---|
192 | * Convert a string to upper case |
---|
193 | */ |
---|
194 | |
---|
195 | char_t *strupper(char_t *string) |
---|
196 | { |
---|
197 | char_t *s; |
---|
198 | |
---|
199 | a_assert(string); |
---|
200 | if (string == NULL) { |
---|
201 | return NULL; |
---|
202 | } |
---|
203 | |
---|
204 | s = string; |
---|
205 | while (*s) { |
---|
206 | if (gislower(*s)) { |
---|
207 | *s = (char_t) gtoupper(*s); |
---|
208 | } |
---|
209 | s++; |
---|
210 | } |
---|
211 | *s = '\0'; |
---|
212 | return string; |
---|
213 | } |
---|
214 | |
---|
215 | /******************************************************************************/ |
---|
216 | /* |
---|
217 | * Convert integer to ascii string. Allow a NULL string in which case we |
---|
218 | * allocate a dynamic buffer. |
---|
219 | */ |
---|
220 | |
---|
221 | char_t *stritoa(int n, char_t *string, int width) |
---|
222 | { |
---|
223 | char_t *cp, *lim, *s; |
---|
224 | char_t buf[16]; /* Just temp to hold number */ |
---|
225 | int next, minus; |
---|
226 | |
---|
227 | a_assert(string && width > 0); |
---|
228 | |
---|
229 | if (string == NULL) { |
---|
230 | if (width == 0) { |
---|
231 | width = 10; |
---|
232 | } |
---|
233 | if ((string = balloc(B_L, width + 1)) == NULL) { |
---|
234 | return NULL; |
---|
235 | } |
---|
236 | } |
---|
237 | if (n < 0) { |
---|
238 | minus = 1; |
---|
239 | n = -n; |
---|
240 | width--; |
---|
241 | } else { |
---|
242 | minus = 0; |
---|
243 | } |
---|
244 | |
---|
245 | cp = buf; |
---|
246 | lim = &buf[width - 1]; |
---|
247 | while (n > 9 && cp < lim) { |
---|
248 | next = n; |
---|
249 | n /= 10; |
---|
250 | *cp++ = (char_t) (next - n * 10 + '0'); |
---|
251 | } |
---|
252 | if (cp < lim) { |
---|
253 | *cp++ = (char_t) (n + '0'); |
---|
254 | } |
---|
255 | |
---|
256 | s = string; |
---|
257 | if (minus) { |
---|
258 | *s++ = '-'; |
---|
259 | } |
---|
260 | |
---|
261 | while (cp > buf) { |
---|
262 | *s++ = *--cp; |
---|
263 | } |
---|
264 | |
---|
265 | *s++ = '\0'; |
---|
266 | return string; |
---|
267 | } |
---|
268 | |
---|
269 | /******************************************************************************/ |
---|
270 | /* |
---|
271 | * Stubs |
---|
272 | */ |
---|
273 | |
---|
274 | char_t *basicGetProduct() |
---|
275 | { |
---|
276 | return T("uemf"); |
---|
277 | } |
---|
278 | |
---|
279 | char_t *basicGetAddress() |
---|
280 | { |
---|
281 | return T("localhost"); |
---|
282 | } |
---|
283 | |
---|
284 | int errorOpen(char_t *pname) |
---|
285 | { |
---|
286 | return 0; |
---|
287 | } |
---|
288 | |
---|
289 | void errorClose() |
---|
290 | { |
---|
291 | } |
---|
292 | |
---|
293 | /******************************************************************************/ |
---|