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