1 | /************************************************************************ |
---|
2 | * |
---|
3 | * MP version 1.1.2: Multi Protocol |
---|
4 | * Kent State University, Kent, OH |
---|
5 | * Authors: S. Gray, N. Kajler, P. Wang |
---|
6 | * (C) 1993, 1994, 1995, 1996, 1997 All Rights Reserved |
---|
7 | * |
---|
8 | * NOTICE |
---|
9 | * |
---|
10 | * Permission to use, copy, modify, and distribute this software and |
---|
11 | * its documentation for non-commercial purposes and without fee is |
---|
12 | * hereby granted provided that the above copyright notice appear in all |
---|
13 | * copies and that both the copyright notice and this permission notice |
---|
14 | * appear in supporting documentation. |
---|
15 | * |
---|
16 | * Neither Kent State University nor the Authors make any representations |
---|
17 | * about the suitability of this software for any purpose. The MP Library |
---|
18 | * is distributed in the hope that it will be useful, but is provided "as |
---|
19 | * is" and without any warranty of any kind and without even the implied |
---|
20 | * warranty of merchantability or fitness for a particular purpose. |
---|
21 | * |
---|
22 | * IMPORTANT ADDITION: as of September 2006, MP is also licenced under GPL |
---|
23 | * |
---|
24 | * IMPLEMENTATION FILE: MP_Util.c |
---|
25 | * |
---|
26 | * Useful utility routines used throughout. |
---|
27 | * |
---|
28 | * |
---|
29 | * Change Log: |
---|
30 | * 11/25/95 sgray - commented out call to MP_ClearError() from |
---|
31 | * MP_PrintError(). I think the error value should |
---|
32 | * remain until the next call has cleared it. |
---|
33 | * 4/25/96 sgray - Added logging support routines log_fixnum(), |
---|
34 | * log_op(), and log_fixreal(). |
---|
35 | * 5/7/96 sgray - Added MP_ApIntType to IMP_TypeToString(). |
---|
36 | * 8/30/96 sgray - Added test for NULL link and environment in |
---|
37 | * MP_LogEvent(). |
---|
38 | * |
---|
39 | **********************************************************************/ |
---|
40 | |
---|
41 | #ifndef lint |
---|
42 | static char vcid[] = "@(#) $Id$"; |
---|
43 | #endif /* lint */ |
---|
44 | |
---|
45 | #include "MP.h" |
---|
46 | |
---|
47 | #include <string.h> |
---|
48 | |
---|
49 | #ifndef __WIN32__ |
---|
50 | #include <unistd.h> |
---|
51 | #endif |
---|
52 | |
---|
53 | /* these are used in a bunch of places */ |
---|
54 | char fix_log_msg[log_msg_len]; |
---|
55 | char AnnotFlagsStr[32]; |
---|
56 | |
---|
57 | #ifdef __STDC__ |
---|
58 | char* IMP_StrDup(char* s) |
---|
59 | #else |
---|
60 | void IMP_StrDup(s) |
---|
61 | char* s; |
---|
62 | #endif |
---|
63 | { |
---|
64 | char* d = NULL; |
---|
65 | |
---|
66 | if (s != NULL) |
---|
67 | { |
---|
68 | d = (char*) IMP_RawMemAllocFnc(strlen(s) + 1); |
---|
69 | strcpy(d, s); |
---|
70 | } |
---|
71 | return d; |
---|
72 | } |
---|
73 | |
---|
74 | #ifdef __STDC__ |
---|
75 | void MP_LogEvent(MP_Link_pt link, |
---|
76 | char *event, |
---|
77 | char *msg) |
---|
78 | #else |
---|
79 | void MP_LogEvent(link, event, msg) |
---|
80 | MP_Link_pt link; |
---|
81 | char *event; |
---|
82 | char *msg; |
---|
83 | #endif |
---|
84 | { |
---|
85 | #ifdef MP_DEBUG |
---|
86 | fprintf(stderr, "MP_LogEvent: entering - event = %s\n", event); |
---|
87 | fflush(stderr); |
---|
88 | #endif |
---|
89 | |
---|
90 | if (link == NULL || link->env == NULL){ |
---|
91 | fprintf(stderr, "MP_LogEvent: link or environment pointer NULL!!!\n"); |
---|
92 | return; |
---|
93 | } |
---|
94 | |
---|
95 | /* |
---|
96 | * Be kind and make sure there is a logfile first |
---|
97 | */ |
---|
98 | if (link->env->logfilename != NULL) { |
---|
99 | fprintf(link->env->logfd, "L%d: %s %s\n", link->link_id, event, msg); |
---|
100 | fflush(link->env->logfd); |
---|
101 | } |
---|
102 | |
---|
103 | #ifdef MP_DEBUG |
---|
104 | fprintf(stderr, "MP_LogEvent: exiting \n"); fflush(stderr); |
---|
105 | #endif |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | |
---|
110 | #ifdef __STDC__ |
---|
111 | MP_Status_t MP_SetError(MP_Link_pt link, MP_Status_t the_err) |
---|
112 | #else |
---|
113 | MP_Status_t MP_SetError(link, the_err) |
---|
114 | MP_Link_pt link; |
---|
115 | MP_Status_t the_err; |
---|
116 | #endif |
---|
117 | { |
---|
118 | link->MP_errno = the_err; |
---|
119 | if (the_err != MP_Failure && the_err >= 0 && the_err < MP_MaxError) |
---|
120 | MP_LogEvent(link, MP_ERROR_EVENT, MP_errlist[the_err]); |
---|
121 | else { |
---|
122 | sprintf(fix_log_msg, "Unknown error number %d", link->MP_errno); |
---|
123 | MP_LogEvent(link, MP_ERROR_EVENT, fix_log_msg); |
---|
124 | } |
---|
125 | |
---|
126 | return MP_Failure; |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | /* |
---|
131 | #ifdef __STDC__ |
---|
132 | MP_Status_t MP_ClearError(MP_Link_pt link) |
---|
133 | #else |
---|
134 | MP_Status_t MP_ClearError(link) |
---|
135 | MP_Link_pt link; |
---|
136 | #endif |
---|
137 | { |
---|
138 | return (link->MP_errno = MP_Success); |
---|
139 | } |
---|
140 | */ |
---|
141 | |
---|
142 | |
---|
143 | #ifdef __STDC__ |
---|
144 | void MP_PrintError(MP_Link_pt link) |
---|
145 | #else |
---|
146 | void MP_PrintError(link) |
---|
147 | MP_Link_pt link; |
---|
148 | #endif |
---|
149 | { |
---|
150 | if (link->MP_errno >= 0 && link->MP_errno < MP_MaxError) { |
---|
151 | fprintf(stderr, "\nMP ERROR: %s\n", MP_errlist[link->MP_errno]); |
---|
152 | fflush(stderr); |
---|
153 | } else |
---|
154 | fprintf(stderr, "MP: Unknown error number %d\n", link->MP_errno); |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | #ifdef __STDC__ |
---|
159 | char* MP_ErrorStr(MP_Link_pt link) |
---|
160 | #else |
---|
161 | char* MP_ErrorStr(link) |
---|
162 | MP_Link_pt link; |
---|
163 | #endif |
---|
164 | { |
---|
165 | if (link->MP_errno >= 0 && link->MP_errno < MP_MaxError) |
---|
166 | return MP_errlist[link->MP_errno]; |
---|
167 | else |
---|
168 | return "MP: Unknown error number"; |
---|
169 | |
---|
170 | } |
---|
171 | |
---|
172 | #ifdef __STDC__ |
---|
173 | char* MP_StatusErrorStr(MP_Link_pt link, MP_Status_t status) |
---|
174 | #else |
---|
175 | char* MP_ErrorStr(link, status) |
---|
176 | MP_Link_pt link; |
---|
177 | MP_Status_t status; |
---|
178 | #endif |
---|
179 | { |
---|
180 | if (link->MP_errno != MP_Success && link->MP_errno != MP_Failure |
---|
181 | && link->MP_errno < MP_MaxError && link->MP_errno >= 0) |
---|
182 | return MP_ErrorStr(link); |
---|
183 | if (status != MP_Success && status != MP_Failure && |
---|
184 | status < MP_MaxError && status >= 0) |
---|
185 | return MP_errlist[status]; |
---|
186 | |
---|
187 | if (status == MP_Failure || link->MP_errno == MP_Failure) |
---|
188 | return MP_errlist[MP_Failure]; |
---|
189 | |
---|
190 | if (status == MP_Success && link->MP_errno == MP_Success) |
---|
191 | return MP_errlist[MP_Success]; |
---|
192 | |
---|
193 | return "MP: Unknown Error number"; |
---|
194 | } |
---|
195 | |
---|
196 | |
---|
197 | |
---|
198 | |
---|
199 | /*********************************************************************** |
---|
200 | * FUNCTION: IMP_GetCmdlineArg |
---|
201 | * INPUT: argc - number of arguments in argv |
---|
202 | * argv - arguments as strings |
---|
203 | * cmd - the cmdline option for which we seek an argument |
---|
204 | * OUTPUT: Success: pointer to the option's argument |
---|
205 | * Failure: NULL |
---|
206 | * OPERATION: Just iterate through argv until we run out of things to |
---|
207 | * look at or find what we want. We assume that there are, |
---|
208 | * in fact, argc things in argv. |
---|
209 | ***********************************************************************/ |
---|
210 | #ifdef __STDC__ |
---|
211 | char* IMP_GetCmdlineArg(int argc, |
---|
212 | char **argv, |
---|
213 | const char *cmd) |
---|
214 | #else |
---|
215 | char* IMP_GetCmdlineArg(argc, argv, cmd) |
---|
216 | int argc; |
---|
217 | char **argv; |
---|
218 | char *cmd; |
---|
219 | #endif |
---|
220 | { |
---|
221 | int i; |
---|
222 | |
---|
223 | #ifdef MP_DEBUG |
---|
224 | fprintf(stderr, "IMP_GetCmdlineArg: entering\n"); |
---|
225 | fflush(stderr); |
---|
226 | #endif |
---|
227 | while (*cmd == '-') cmd++; |
---|
228 | |
---|
229 | for (i = 0; i < argc; i++) |
---|
230 | if (strstr(argv[i], cmd) != NULL && (*(argv[i]) == '-')) |
---|
231 | if (i+1 == argc) |
---|
232 | return NULL; |
---|
233 | else |
---|
234 | return argv[i+1]; |
---|
235 | |
---|
236 | #ifdef MP_DEBUG |
---|
237 | fprintf(stderr, "IMP_GetCmdlineArg: exiting\n");fflush(stderr); |
---|
238 | #endif |
---|
239 | |
---|
240 | return NULL; |
---|
241 | } |
---|
242 | |
---|
243 | |
---|
244 | |
---|
245 | #ifdef __STDC__ |
---|
246 | char * IMP_TypeToString(MP_NodeType_t t) |
---|
247 | #else |
---|
248 | char * IMP_TypeToString(t) |
---|
249 | MP_NodeType_t t; |
---|
250 | #endif |
---|
251 | { |
---|
252 | switch (t) { |
---|
253 | case MP_Sint32Type: |
---|
254 | return "MP_Sint32"; |
---|
255 | case MP_Uint32Type: |
---|
256 | return "MP_Uint32"; |
---|
257 | case MP_Uint8Type: |
---|
258 | return "MP_Uint8"; |
---|
259 | case MP_Sint8Type: |
---|
260 | return "MP_Sint8"; |
---|
261 | case MP_ApIntType: |
---|
262 | return "MP_ApInt"; |
---|
263 | case MP_ApRealType: |
---|
264 | return "MP_ApReal"; |
---|
265 | case MP_BooleanType: |
---|
266 | return "MP_Boolean"; |
---|
267 | case MP_Real32Type: |
---|
268 | return "MP_Real32"; |
---|
269 | case MP_Real64Type: |
---|
270 | return "MP_Real64"; |
---|
271 | case MP_IdentifierType: |
---|
272 | return "MP_Identifier"; |
---|
273 | case MP_CommonGreekIdentifierType: |
---|
274 | return "MP_CommonGreekId"; |
---|
275 | case MP_CommonLatinIdentifierType: |
---|
276 | return "MP_CommonLatinId"; |
---|
277 | case MP_ConstantType: |
---|
278 | return "MP_Constant"; |
---|
279 | case MP_CommonConstantType: |
---|
280 | return "MP_CommonConstant"; |
---|
281 | case MP_StringType: |
---|
282 | return "MP_String"; |
---|
283 | case MP_RawType: |
---|
284 | return "MP_Raw"; |
---|
285 | case MP_MetaType: |
---|
286 | return "MP_MetaType"; |
---|
287 | case MP_CommonMetaType: |
---|
288 | return "MP_CommonMeta"; |
---|
289 | case MP_OperatorType: |
---|
290 | return "MP_Operator"; |
---|
291 | case MP_CommonOperatorType: |
---|
292 | return "MP_CommonOp"; |
---|
293 | case MP_MetaOperatorType: |
---|
294 | return "MP_MetaOperator"; |
---|
295 | case MP_CommonMetaOperatorType: |
---|
296 | return "MP_CommonMetaOp"; |
---|
297 | default: |
---|
298 | return "Unknown type"; |
---|
299 | } |
---|
300 | } |
---|
301 | |
---|
302 | |
---|
303 | |
---|
304 | #ifdef __STDC__ |
---|
305 | void log_dicttype(MP_Link_pt link, |
---|
306 | char *event, |
---|
307 | char *type_str, |
---|
308 | MP_NodeType_t type, |
---|
309 | MP_NumAnnot_t na, |
---|
310 | MP_DictTag_t dtag, |
---|
311 | void *data, |
---|
312 | MP_NumChild_t nc) |
---|
313 | #else |
---|
314 | void log_dicttype(link, event, type_str, type, na, dtag, data,nc) |
---|
315 | MP_Link_pt link; |
---|
316 | char *event; |
---|
317 | char *type_str; |
---|
318 | MP_NodeType_t type; |
---|
319 | MP_NumAnnot_t na; |
---|
320 | MP_DictTag_t dtag; |
---|
321 | void *data; |
---|
322 | MP_NumChild_t nc; |
---|
323 | #endif |
---|
324 | { |
---|
325 | char *msg; |
---|
326 | int len = 3; |
---|
327 | |
---|
328 | if (type != MP_CommonOperatorType) { |
---|
329 | len = strlen((char *)data); |
---|
330 | if (len < 24) len = 24; |
---|
331 | } |
---|
332 | |
---|
333 | msg = (char *)IMP_MemAllocFnc(len + 62); |
---|
334 | |
---|
335 | switch (type) { |
---|
336 | case MP_OperatorType: |
---|
337 | case MP_MetaOperatorType: |
---|
338 | |
---|
339 | sprintf(msg, "%-12s annots: %lu dict: %lu op: %s args: %lu", |
---|
340 | type_str, na, dtag, (char *)data, nc); |
---|
341 | break; |
---|
342 | |
---|
343 | case MP_CommonOperatorType: |
---|
344 | case MP_CommonMetaOperatorType: |
---|
345 | sprintf(msg, "%-12s annots: %lu dict: %lu op-tag: %u args: %lu", |
---|
346 | type_str, na, dtag, (unsigned int)*((MP_Common_t *)data), nc); |
---|
347 | break; |
---|
348 | |
---|
349 | case MP_CommonConstantType: |
---|
350 | case MP_CommonMetaType: |
---|
351 | sprintf(msg, "%-12s annots: %lu dict: %lu id-tag: %u", |
---|
352 | type_str, na, dtag, *((MP_Common_t *)data)); |
---|
353 | break; |
---|
354 | |
---|
355 | default: ; |
---|
356 | } |
---|
357 | |
---|
358 | MP_LogEvent(link, event, msg); |
---|
359 | IMP_MemFreeFnc(msg, len+62); |
---|
360 | } |
---|
361 | |
---|
362 | |
---|
363 | |
---|
364 | #ifdef __STDC__ |
---|
365 | void log_fixnum(MP_Link_pt link, |
---|
366 | char *event, |
---|
367 | char *type_str, |
---|
368 | int type, |
---|
369 | MP_NumAnnot_t na, |
---|
370 | void *val) |
---|
371 | #else |
---|
372 | void log_fixnum(link, event, type_str, type, na, val) |
---|
373 | MP_Link_pt link; |
---|
374 | char *event; |
---|
375 | char *type_str; |
---|
376 | int type; |
---|
377 | MP_NumAnnot_t na; |
---|
378 | void *val; |
---|
379 | #endif |
---|
380 | { |
---|
381 | MP_Uint8_t t; |
---|
382 | |
---|
383 | switch (type) { |
---|
384 | case MP_BooleanType: |
---|
385 | case MP_Uint8Type: |
---|
386 | t = (MP_Uint8_t)*((MP_Uint8_t *)val); |
---|
387 | sprintf(fix_log_msg, "%-12s annots: %lu value: %u", type_str, na,t); |
---|
388 | break; |
---|
389 | |
---|
390 | case MP_Uint32Type: |
---|
391 | sprintf(fix_log_msg, "%-12s value: %lu\n", type_str, |
---|
392 | (MP_Uint32_t)*((MP_Uint32_t *)val)); |
---|
393 | break; |
---|
394 | |
---|
395 | case MP_Sint8Type: |
---|
396 | sprintf(fix_log_msg, "%-12s annots: %lu value: %d", type_str, na, |
---|
397 | *((MP_Sint8_t *)val)); |
---|
398 | break; |
---|
399 | |
---|
400 | case MP_Sint32Type: |
---|
401 | sprintf(fix_log_msg, "%-12s value: %ld", type_str, |
---|
402 | (MP_Sint32_t)*((MP_Sint32_t *)val)); |
---|
403 | break; |
---|
404 | |
---|
405 | default: ; |
---|
406 | } |
---|
407 | |
---|
408 | MP_LogEvent(link, event, fix_log_msg); |
---|
409 | } |
---|
410 | |
---|
411 | |
---|
412 | |
---|
413 | #ifdef __STDC__ |
---|
414 | void log_fixreal(MP_Link_pt link, |
---|
415 | char *event, |
---|
416 | char *type_str, |
---|
417 | int type, |
---|
418 | MP_NumAnnot_t na, |
---|
419 | void *val) |
---|
420 | #else |
---|
421 | void log_fixreal(link, event, type_str, type, na, val) |
---|
422 | MP_Link_pt link; |
---|
423 | char *event; |
---|
424 | char *type_str; |
---|
425 | int type; |
---|
426 | MP_NumAnnot_t na; |
---|
427 | void *val; |
---|
428 | #endif |
---|
429 | { |
---|
430 | switch (type) { |
---|
431 | case MP_Real32Type: |
---|
432 | sprintf(fix_log_msg, "%-12s value: %-20.10G", type_str, |
---|
433 | *((MP_Real32_t *)val)); |
---|
434 | break; |
---|
435 | |
---|
436 | case MP_Real64Type: |
---|
437 | sprintf(fix_log_msg, "%-12s value: %-20.15G", type_str, |
---|
438 | *((MP_Real64_t *)val)); |
---|
439 | break; |
---|
440 | |
---|
441 | } |
---|
442 | |
---|
443 | MP_LogEvent(link, event, fix_log_msg); |
---|
444 | } |
---|
445 | |
---|
446 | |
---|
447 | |
---|
448 | #ifdef __STDC__ |
---|
449 | void annot_flags_to_str(MP_AnnotFlags_t flags) |
---|
450 | #else |
---|
451 | void annot_flags_to_str(flags) |
---|
452 | MP_AnnotFlags_t flags; |
---|
453 | #endif |
---|
454 | { |
---|
455 | char *req, *scope, *valuated; |
---|
456 | |
---|
457 | valuated = (flags & MP_AnnotValuated) ? "Arg" : "NoArg"; |
---|
458 | req = (flags & MP_AnnotRequired) ? "Req" : "Sup"; |
---|
459 | scope = (flags & MP_AnnotTreeScope) ? "Node" : "Tree"; |
---|
460 | |
---|
461 | sprintf(AnnotFlagsStr, "%s %s %s", req, valuated, scope); |
---|
462 | } |
---|
463 | |
---|
464 | |
---|