| 1 |
#ifdef RCSID |
| 2 |
static char RCSid[] = |
| 3 |
"$Header: d:/cvsroot/tads/tads3/vmerr.cpp,v 1.4 1999/07/11 00:46:59 MJRoberts Exp $"; |
| 4 |
#endif |
| 5 |
|
| 6 |
/* |
| 7 |
* Copyright (c) 1998, 2002 Michael J. Roberts. All Rights Reserved. |
| 8 |
* |
| 9 |
* Please see the accompanying license file, LICENSE.TXT, for information |
| 10 |
* on using and copying this software. |
| 11 |
*/ |
| 12 |
/* |
| 13 |
Name |
| 14 |
vmerr.cpp - VM error handling |
| 15 |
Function |
| 16 |
This module contains global variables required for the error handler. |
| 17 |
Notes |
| 18 |
|
| 19 |
Modified |
| 20 |
10/20/98 MJRoberts - Creation |
| 21 |
*/ |
| 22 |
|
| 23 |
#include <stdarg.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <assert.h> |
| 26 |
|
| 27 |
#include "t3std.h" |
| 28 |
#include "vmtype.h" |
| 29 |
#include "vmerr.h" |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
/* ------------------------------------------------------------------------ */ |
| 34 |
/* |
| 35 |
* Global error context pointer, and reference count for the error |
| 36 |
* subsystem. |
| 37 |
*/ |
| 38 |
OS_DECL_TLS(err_frame_t *, G_err_frame); |
| 39 |
static int G_err_refs = 0; |
| 40 |
|
| 41 |
/* ------------------------------------------------------------------------ */ |
| 42 |
/* |
| 43 |
* Initialize the global error context |
| 44 |
*/ |
| 45 |
void err_init(size_t /*param_stack_size*/) |
| 46 |
{ |
| 47 |
/* increase the error system reference counter */ |
| 48 |
if (++G_err_refs == 1) |
| 49 |
{ |
| 50 |
/* |
| 51 |
* first initialization - allocate the thread-local variable slot |
| 52 |
* for the error frame pointer |
| 53 |
*/ |
| 54 |
os_tls_create(G_err_frame); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/* ------------------------------------------------------------------------ */ |
| 59 |
/* |
| 60 |
* Delete the global error context |
| 61 |
*/ |
| 62 |
void err_terminate() |
| 63 |
{ |
| 64 |
/* decrease the error system reference counter */ |
| 65 |
--G_err_refs; |
| 66 |
|
| 67 |
/* if that leaves no references, delete the error context */ |
| 68 |
if (G_err_refs == 0) |
| 69 |
{ |
| 70 |
/* delete external messages, if we loaded them */ |
| 71 |
err_delete_message_array(&vm_messages, &vm_message_count, |
| 72 |
vm_messages_english, |
| 73 |
vm_message_count_english); |
| 74 |
|
| 75 |
/* delete the thrad-local slot for the error frame pointer */ |
| 76 |
os_tls_delete(G_err_frame); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/* ------------------------------------------------------------------------ */ |
| 81 |
/* |
| 82 |
* Throw the error currently on the stack |
| 83 |
*/ |
| 84 |
static void err_throw_current() |
| 85 |
{ |
| 86 |
err_state_t new_state; |
| 87 |
|
| 88 |
/* |
| 89 |
* Figure out what state the enclosing frame will be in after this |
| 90 |
* jump. If we're currently in a 'trying' block, we'll now be in an |
| 91 |
* 'exception' state. Otherwise, we must have been in a 'catch' or |
| 92 |
* 'finally' already - in these cases, the new state is 'rethrown', |
| 93 |
* because we have an exception within an exception handler. |
| 94 |
*/ |
| 95 |
err_state_t old_state = os_tls_get(err_frame_t *, G_err_frame)->state_; |
| 96 |
err_state_t f = (old_state & ERR_STATE_FINALLY); |
| 97 |
old_state &= ~ERR_STATE_FINALLY; |
| 98 |
if (old_state == ERR_STATE_TRYING) |
| 99 |
new_state = ERR_STATE_EXCEPTION | f; |
| 100 |
else |
| 101 |
new_state = ERR_STATE_RETHROWN | f; |
| 102 |
|
| 103 |
/* jump to the enclosing frame's exception handler */ |
| 104 |
longjmp(os_tls_get(err_frame_t *, G_err_frame)->jmpbuf_, new_state); |
| 105 |
} |
| 106 |
|
| 107 |
/* ------------------------------------------------------------------------ */ |
| 108 |
/* |
| 109 |
* Throw an exception |
| 110 |
*/ |
| 111 |
void err_throw(err_id_t error_code) |
| 112 |
{ |
| 113 |
/* throw the error, with no parameters */ |
| 114 |
err_throw_a(error_code, 0); |
| 115 |
} |
| 116 |
|
| 117 |
/* ------------------------------------------------------------------------ */ |
| 118 |
/* |
| 119 |
* store a string parameter |
| 120 |
*/ |
| 121 |
static char *err_store_str(char* &strspace, const char *str, size_t len) |
| 122 |
{ |
| 123 |
/* remember the starting position of the stored string */ |
| 124 |
char *ret = strspace; |
| 125 |
|
| 126 |
/* copy it into the string space */ |
| 127 |
memcpy(strspace, str, len); |
| 128 |
|
| 129 |
/* advance the allocation pointer */ |
| 130 |
strspace += len; |
| 131 |
|
| 132 |
/* add a trailing null */ |
| 133 |
*strspace++ = '\0'; |
| 134 |
|
| 135 |
/* return the start of the string */ |
| 136 |
return ret; |
| 137 |
} |
| 138 |
|
| 139 |
/* ------------------------------------------------------------------------ */ |
| 140 |
/* |
| 141 |
* Throw an exception with parameters in va_list format |
| 142 |
*/ |
| 143 |
static size_t err_throw_v(err_id_t error_code, int param_count, va_list va, |
| 144 |
CVmException *exc) |
| 145 |
{ |
| 146 |
/* |
| 147 |
* Assert that the size of the err_param_type enum is no larger than |
| 148 |
* the size of the native 'int' type. Since this routine is called |
| 149 |
* with a varargs list, and since ANSI requires compilers to promote |
| 150 |
* any enum type smaller than int to int when passing to a varargs |
| 151 |
* function, we must retrieve our err_param_type arguments (via the |
| 152 |
* va_arg() macro) with type 'int' rather than type 'enum |
| 153 |
* err_param_type'. |
| 154 |
* |
| 155 |
* The promotion to int is mandatory, so retrieving our values as |
| 156 |
* 'int' values is the correct, portable thing to do; the only |
| 157 |
* potential problem is that our enum type could be defined to be |
| 158 |
* *larger* than int, in which case the compiler would pass it to us |
| 159 |
* as the larger type, not int, and our retrieval would be incorrect. |
| 160 |
* The only way a compiler would be allowed to do this is if our enum |
| 161 |
* type actually required a type larger than unsigned int (in other |
| 162 |
* words, we had enum values higher than the largest unsigned int |
| 163 |
* value for the local platform). This is extremely unlikely, but |
| 164 |
* we'll assert our assumption here to ensure that the problem is |
| 165 |
* readily apparent should it ever occur. |
| 166 |
*/ |
| 167 |
assert(sizeof(err_param_type) <= sizeof(int)); |
| 168 |
|
| 169 |
/* |
| 170 |
* Figure out how much space we need. Start with the size of the base |
| 171 |
* CVmException class, since we always need a CVmException structure. |
| 172 |
* This structure has space for one argument descriptor built in, so |
| 173 |
* subtract that off from our base size, since we'll add in space for |
| 174 |
* the argument descriptors separately. |
| 175 |
*/ |
| 176 |
size_t siz = sizeof(CVmException) - sizeof(CVmExcParam); |
| 177 |
|
| 178 |
/* |
| 179 |
* Add in space the parameter descriptors. We need one CVmExcParam |
| 180 |
* structure to describe each parameter. |
| 181 |
*/ |
| 182 |
siz += param_count * sizeof(CVmExcParam); |
| 183 |
|
| 184 |
/* fill in our base structure, if allocated */ |
| 185 |
CVmExcParam *param = 0; |
| 186 |
char *strspace = 0; |
| 187 |
if (exc != 0) |
| 188 |
{ |
| 189 |
/* set the error code and parameter count */ |
| 190 |
exc->error_code_ = error_code; |
| 191 |
exc->param_count_ = param_count; |
| 192 |
|
| 193 |
/* presume this is not an out-of-date version error */ |
| 194 |
exc->version_flag_ = FALSE; |
| 195 |
exc->metaclass_ = 0; |
| 196 |
exc->funcset_ = 0; |
| 197 |
|
| 198 |
/* |
| 199 |
* If the current stack frame is already handling an error, delete |
| 200 |
* the current frame's exception object, since the current frame |
| 201 |
* will no longer be accessible after this throw. |
| 202 |
*/ |
| 203 |
if (os_tls_get(err_frame_t *, G_err_frame)->state_ |
| 204 |
& (ERR_STATE_EXCEPTION | ERR_STATE_CAUGHT | ERR_STATE_RETHROWN)) |
| 205 |
t3free(os_tls_get(err_frame_t *, G_err_frame)->exc_); |
| 206 |
|
| 207 |
/* this is now the current exception */ |
| 208 |
os_tls_get(err_frame_t *, G_err_frame)->exc_ = exc; |
| 209 |
|
| 210 |
/* start at the first parameter slot */ |
| 211 |
param = exc->params_; |
| 212 |
|
| 213 |
/* |
| 214 |
* store strings in the allocated space after the base struct and |
| 215 |
* (varying-size) parameter array |
| 216 |
*/ |
| 217 |
strspace = (char *)&exc->params_[param_count]; |
| 218 |
} |
| 219 |
|
| 220 |
/* |
| 221 |
* We now have our base exception structure, with enough space for the |
| 222 |
* parameter descriptors, but we still need to store the parameter |
| 223 |
* values themselves. |
| 224 |
*/ |
| 225 |
for (int i = 0 ; i < param_count ; ++i, ++param) |
| 226 |
{ |
| 227 |
/* get the type indicator, and store it in the descriptor */ |
| 228 |
err_param_type typ = (err_param_type)va_arg(va, int); |
| 229 |
|
| 230 |
/* store the type */ |
| 231 |
if (exc != 0) |
| 232 |
param->type_ = typ; |
| 233 |
|
| 234 |
/* store the argument's value */ |
| 235 |
int ival; |
| 236 |
ulong ulval; |
| 237 |
const char *sptr; |
| 238 |
size_t slen; |
| 239 |
switch(typ) |
| 240 |
{ |
| 241 |
case ERR_TYPE_INT: |
| 242 |
/* store the integer */ |
| 243 |
ival = va_arg(va, int); |
| 244 |
if (exc != 0) |
| 245 |
param->val_.intval_ = ival; |
| 246 |
break; |
| 247 |
|
| 248 |
case ERR_TYPE_ULONG: |
| 249 |
/* store the unsigned long */ |
| 250 |
ulval = va_arg(va, unsigned long); |
| 251 |
if (exc != 0) |
| 252 |
param->val_.ulong_ = ulval; |
| 253 |
break; |
| 254 |
|
| 255 |
case ERR_TYPE_TEXTCHAR: |
| 256 |
/* |
| 257 |
* It's a (textchar_t *) string, null-terminated. Get the |
| 258 |
* string pointer and calculate its length. |
| 259 |
*/ |
| 260 |
sptr = va_arg(va, textchar_t *); |
| 261 |
slen = get_strlen(sptr); |
| 262 |
|
| 263 |
/* count the string space needed */ |
| 264 |
siz += slen + 1; |
| 265 |
|
| 266 |
/* store it in parameter memory */ |
| 267 |
if (exc != 0) |
| 268 |
param->val_.strval_ = err_store_str(strspace, sptr, slen); |
| 269 |
break; |
| 270 |
|
| 271 |
case ERR_TYPE_TEXTCHAR_LEN: |
| 272 |
/* |
| 273 |
* It's a (textchar_t *) string with an explicit length given |
| 274 |
* as a separate size_t parameter. |
| 275 |
*/ |
| 276 |
sptr = va_arg(va, textchar_t *); |
| 277 |
slen = va_arg(va, size_t); |
| 278 |
|
| 279 |
/* count the string space */ |
| 280 |
siz += slen + 1; |
| 281 |
|
| 282 |
/* store it in parameter memory */ |
| 283 |
if (exc != 0) |
| 284 |
{ |
| 285 |
param->type_ = ERR_TYPE_TEXTCHAR; |
| 286 |
param->val_.strval_ = err_store_str(strspace, sptr, slen); |
| 287 |
} |
| 288 |
|
| 289 |
break; |
| 290 |
|
| 291 |
case ERR_TYPE_CHAR: |
| 292 |
/* it's a (char *) string, null-terminated */ |
| 293 |
sptr = va_arg(va, char *); |
| 294 |
slen = strlen(sptr); |
| 295 |
|
| 296 |
/* count the string space */ |
| 297 |
siz += slen + 1; |
| 298 |
|
| 299 |
/* store it */ |
| 300 |
if (exc != 0) |
| 301 |
param->val_.charval_ = err_store_str(strspace, sptr, slen); |
| 302 |
break; |
| 303 |
|
| 304 |
case ERR_TYPE_CHAR_LEN: |
| 305 |
/* it's a (char *) string with an explicit size_t size */ |
| 306 |
sptr = va_arg(va, char *); |
| 307 |
slen = va_arg(va, size_t); |
| 308 |
|
| 309 |
/* count the string space */ |
| 310 |
siz += slen + 1; |
| 311 |
|
| 312 |
/* store it */ |
| 313 |
if (exc != 0) |
| 314 |
{ |
| 315 |
param->val_.charval_ = err_store_str(strspace, sptr, slen); |
| 316 |
param->type_ = ERR_TYPE_CHAR; |
| 317 |
} |
| 318 |
break; |
| 319 |
|
| 320 |
case ERR_TYPE_FUNCSET: |
| 321 |
/* |
| 322 |
* It's a char* string with a function set ID. These are not |
| 323 |
* stored in the parameters, but go in the funcset_ slot in the |
| 324 |
* exception object. |
| 325 |
*/ |
| 326 |
sptr = va_arg(va, char *); |
| 327 |
siz += strlen(sptr) + 1; |
| 328 |
if (exc != 0) |
| 329 |
exc->funcset_ = err_store_str(strspace, sptr, strlen(sptr)); |
| 330 |
break; |
| 331 |
|
| 332 |
case ERR_TYPE_METACLASS: |
| 333 |
/* |
| 334 |
* It's a char* string with a metaclass ID. These are not |
| 335 |
* stored in the parameters, but go in the metaclass_ slot in |
| 336 |
* the exception object. |
| 337 |
*/ |
| 338 |
sptr = va_arg(va, char *); |
| 339 |
siz += strlen(sptr) + 1; |
| 340 |
if (exc != 0) |
| 341 |
exc->metaclass_ = err_store_str(strspace, sptr, strlen(sptr)); |
| 342 |
break; |
| 343 |
|
| 344 |
case ERR_TYPE_VERSION_FLAG: |
| 345 |
/* |
| 346 |
* This parameter is a flag indicating that the error is due to |
| 347 |
* an out-of-date interpreter build. This has no parameter |
| 348 |
* data; we simply set the flag in the exception to indicate |
| 349 |
* the version error type. |
| 350 |
*/ |
| 351 |
if (exc != 0) |
| 352 |
exc->version_flag_ = TRUE; |
| 353 |
break; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
/* |
| 358 |
* if we have an exception, throw it; if not, we're doing a dry run to |
| 359 |
* compute the exception object size, so simply return the computed |
| 360 |
* size |
| 361 |
*/ |
| 362 |
if (exc != 0) |
| 363 |
{ |
| 364 |
/* throw the exception object that we just populated */ |
| 365 |
err_throw_current(); |
| 366 |
VMERR_AFTER_ERR_THROW(return 0;) |
| 367 |
} |
| 368 |
else |
| 369 |
{ |
| 370 |
/* we're only computing the size on this pass */ |
| 371 |
return siz; |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
/* ------------------------------------------------------------------------ */ |
| 376 |
#ifdef MICROSOFT |
| 377 |
/* |
| 378 |
* Microsoft Visual C++ optimizer workaround - not applicable to other |
| 379 |
* systems. |
| 380 |
* |
| 381 |
* For MSVC, we need to turn off warning 4702 ("unreachable code"). MSVC |
| 382 |
* is too clever by half for our implementation here of err_throw_a(), and |
| 383 |
* the only recourse (empirically) seems to be to turn off this warning for |
| 384 |
* the duration of this function. |
| 385 |
* |
| 386 |
* The issue is that err_throw_a() makes a call to err_throw_v() within a |
| 387 |
* va_start()...va_end() pair. The MSVC optimizer recognizes that |
| 388 |
* err_throw_v() never returns, so it marks any code following a call to |
| 389 |
* same as unreachable. But we *have to* include the va_end() call after |
| 390 |
* the err_throw_v() call. The va_end() is *required* for portability - |
| 391 |
* some compilers expand va_end() to structural C code (for example, to |
| 392 |
* insert a close brace to balance an open brace inserted by va_start()). |
| 393 |
* So we can't omit the va_end() call regardless of its run-time |
| 394 |
* reachability. |
| 395 |
* |
| 396 |
* So, we have code that's both unreachable and required. The only |
| 397 |
* apparent solution is to disable the unreachable-code warning for the |
| 398 |
* duration of this function. |
| 399 |
* |
| 400 |
* (Conceivably, we could trick the optimizer by moving the err_throw_a() |
| 401 |
* implementation to a separate module; the optimizer probably couldn't |
| 402 |
* track the does-not-return status of err_throw_v() across modules. But |
| 403 |
* that wouldn't be any cleaner in any sense - it would just trick the |
| 404 |
* compiler in a different way. Better to explicitly turn off the warning; |
| 405 |
* at least that way the workaround is plain and explicit.) |
| 406 |
*/ |
| 407 |
#pragma warning(push) |
| 408 |
#pragma warning(disable: 4702) |
| 409 |
#endif |
| 410 |
|
| 411 |
/* |
| 412 |
* Throw an exception with parameters |
| 413 |
*/ |
| 414 |
void err_throw_a(err_id_t error_code, int param_count, ...) |
| 415 |
{ |
| 416 |
va_list marker; |
| 417 |
|
| 418 |
/* do a dry run to determine the exception object size */ |
| 419 |
va_start(marker, param_count); |
| 420 |
size_t siz = err_throw_v(error_code, param_count, marker, 0); |
| 421 |
va_end(marker); |
| 422 |
|
| 423 |
/* allocate the new exception object */ |
| 424 |
CVmException *exc = (CVmException *)t3malloc(siz); |
| 425 |
|
| 426 |
/* build the argument list and throw the error */ |
| 427 |
va_start(marker, param_count); |
| 428 |
err_throw_v(error_code, param_count, marker, exc); |
| 429 |
va_end(marker); |
| 430 |
} |
| 431 |
|
| 432 |
/* MSVC - restore previous warning state (see above) */ |
| 433 |
#ifdef MICROSOFT |
| 434 |
#pragma warning(pop) |
| 435 |
#endif |
| 436 |
|
| 437 |
|
| 438 |
/* ------------------------------------------------------------------------ */ |
| 439 |
/* |
| 440 |
* Re-throw the current exception. This is valid only from 'catch' |
| 441 |
* blocks. |
| 442 |
*/ |
| 443 |
void err_rethrow() |
| 444 |
{ |
| 445 |
/* throw the error currently on the stack */ |
| 446 |
err_throw_current(); |
| 447 |
} |
| 448 |
|
| 449 |
/* ------------------------------------------------------------------------ */ |
| 450 |
/* |
| 451 |
* Abort the program with a serious, unrecoverable error |
| 452 |
*/ |
| 453 |
void err_abort(const char *message) |
| 454 |
{ |
| 455 |
printf("%s\n", message); |
| 456 |
exit(2); |
| 457 |
} |
| 458 |
|
| 459 |
/* ------------------------------------------------------------------------ */ |
| 460 |
/* |
| 461 |
* Retrieve the current exception being handled in the nearest enclosing |
| 462 |
* err_catch frame. |
| 463 |
*/ |
| 464 |
CVmException *err_get_cur_exc() |
| 465 |
{ |
| 466 |
/* |
| 467 |
* search the error frame stack for a frame in the 'caught exception' |
| 468 |
* state, starting at the current (innermost) frame |
| 469 |
*/ |
| 470 |
for (err_frame_t *fr = os_tls_get(err_frame_t *, G_err_frame) ; |
| 471 |
fr != 0 ; fr = fr->prv_) |
| 472 |
{ |
| 473 |
/* if this frame is in the 'caught' state, return its exception */ |
| 474 |
if ((fr->state_ & ERR_STATE_CAUGHT) != 0) |
| 475 |
return fr->exc_; |
| 476 |
} |
| 477 |
|
| 478 |
/* didn't find an exception */ |
| 479 |
return 0; |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
/* ------------------------------------------------------------------------ */ |
| 484 |
/* |
| 485 |
* Try loading a message file. Returns zero on success, non-zero if an |
| 486 |
* error occurred. |
| 487 |
*/ |
| 488 |
int err_load_message_file(osfildef *fp, |
| 489 |
const err_msg_t **arr, size_t *arr_size, |
| 490 |
const err_msg_t *default_arr, |
| 491 |
size_t default_arr_size) |
| 492 |
{ |
| 493 |
char buf[128]; |
| 494 |
size_t i; |
| 495 |
err_msg_t *msg; |
| 496 |
|
| 497 |
/* read the file signature */ |
| 498 |
if (osfrb(fp, buf, sizeof(VM_MESSAGE_FILE_SIGNATURE)) |
| 499 |
|| memcmp(buf, VM_MESSAGE_FILE_SIGNATURE, |
| 500 |
sizeof(VM_MESSAGE_FILE_SIGNATURE)) != 0) |
| 501 |
goto fail; |
| 502 |
|
| 503 |
/* delete any previously-loaded message array */ |
| 504 |
err_delete_message_array(arr, arr_size, default_arr, default_arr_size); |
| 505 |
|
| 506 |
/* read the message count */ |
| 507 |
if (osfrb(fp, buf, 2)) |
| 508 |
goto fail; |
| 509 |
|
| 510 |
/* set the new message count */ |
| 511 |
*arr_size = osrp2(buf); |
| 512 |
|
| 513 |
/* allocate the message array */ |
| 514 |
*arr = (err_msg_t *)t3malloc(*arr_size * sizeof(err_msg_t)); |
| 515 |
if (*arr == 0) |
| 516 |
goto fail; |
| 517 |
|
| 518 |
/* clear the memory */ |
| 519 |
memset((err_msg_t *)*arr, 0, *arr_size * sizeof(err_msg_t)); |
| 520 |
|
| 521 |
/* read the individual messages */ |
| 522 |
for (i = 0, msg = (err_msg_t *)*arr ; i < *arr_size ; ++i, ++msg) |
| 523 |
{ |
| 524 |
size_t len1, len2; |
| 525 |
|
| 526 |
/* read the current message ID and the length of the two messages */ |
| 527 |
if (osfrb(fp, buf, 8)) |
| 528 |
goto fail; |
| 529 |
|
| 530 |
/* set the message ID */ |
| 531 |
msg->msgnum = (int)t3rp4u(buf); |
| 532 |
|
| 533 |
/* get the short and long mesage lengths */ |
| 534 |
len1 = osrp2(buf + 4); |
| 535 |
len2 = osrp2(buf + 6); |
| 536 |
|
| 537 |
/* allocate buffers */ |
| 538 |
msg->short_msgtxt = (char *)t3malloc(len1 + 1); |
| 539 |
msg->long_msgtxt = (char *)t3malloc(len2 + 1); |
| 540 |
|
| 541 |
/* if either one failed, give up */ |
| 542 |
if (msg->short_msgtxt == 0 || msg->long_msgtxt == 0) |
| 543 |
goto fail; |
| 544 |
|
| 545 |
/* read the two messages */ |
| 546 |
if (osfrb(fp, (char *)msg->short_msgtxt, len1) |
| 547 |
|| osfrb(fp, (char *)msg->long_msgtxt, len2)) |
| 548 |
goto fail; |
| 549 |
|
| 550 |
/* null-terminate the strings */ |
| 551 |
*(char *)(msg->short_msgtxt + len1) = '\0'; |
| 552 |
*(char *)(msg->long_msgtxt + len2) = '\0'; |
| 553 |
} |
| 554 |
|
| 555 |
/* success */ |
| 556 |
return 0; |
| 557 |
|
| 558 |
fail: |
| 559 |
/* revert back to the built-in array */ |
| 560 |
err_delete_message_array(arr, arr_size, |
| 561 |
default_arr, default_arr_size); |
| 562 |
|
| 563 |
/* indicate failure */ |
| 564 |
return 1; |
| 565 |
} |
| 566 |
|
| 567 |
/* |
| 568 |
* Determine if an external message file has been loaded for the default |
| 569 |
* VM message set |
| 570 |
*/ |
| 571 |
int err_is_message_file_loaded() |
| 572 |
{ |
| 573 |
/* |
| 574 |
* if we're not using the compiled-in message array, we must have |
| 575 |
* loaded an external message set |
| 576 |
*/ |
| 577 |
return vm_messages != &vm_messages_english[0]; |
| 578 |
} |
| 579 |
|
| 580 |
/* |
| 581 |
* Delete the message array, if one is loaded |
| 582 |
*/ |
| 583 |
void err_delete_message_array(const err_msg_t **arr, size_t *arr_size, |
| 584 |
const err_msg_t *default_arr, |
| 585 |
size_t default_arr_size) |
| 586 |
{ |
| 587 |
/* |
| 588 |
* If the message array is valid, and it's not set to point to the |
| 589 |
* built-in array of English messages, we must have allocated it, so |
| 590 |
* we must now free it. We don't need to free it if it points to |
| 591 |
* the English array, because that's static data linked into the VM |
| 592 |
* executable. |
| 593 |
*/ |
| 594 |
if (*arr != 0 && *arr != default_arr) |
| 595 |
{ |
| 596 |
size_t i; |
| 597 |
err_msg_t *msg; |
| 598 |
|
| 599 |
/* delete each message in the array */ |
| 600 |
for (i = 0, msg = (err_msg_t *)*arr ; i < *arr_size ; ++i, ++msg) |
| 601 |
{ |
| 602 |
/* delete the strings for this entry */ |
| 603 |
if (msg->short_msgtxt != 0) |
| 604 |
t3free((char *)msg->short_msgtxt); |
| 605 |
if (msg->long_msgtxt != 0) |
| 606 |
t3free((char *)msg->long_msgtxt); |
| 607 |
} |
| 608 |
|
| 609 |
/* delete the message array itself */ |
| 610 |
t3free((err_msg_t *)*arr); |
| 611 |
} |
| 612 |
|
| 613 |
/* set the messages array back to the built-in english messages */ |
| 614 |
*arr = default_arr; |
| 615 |
*arr_size = default_arr_size; |
| 616 |
} |
| 617 |
|
| 618 |
/* ------------------------------------------------------------------------ */ |
| 619 |
/* |
| 620 |
* Find an error message |
| 621 |
*/ |
| 622 |
const char *err_get_msg(const err_msg_t *msg_array, size_t msg_count, |
| 623 |
int msgnum, int verbose) |
| 624 |
{ |
| 625 |
int hi, lo, cur; |
| 626 |
|
| 627 |
/* perform a binary search of the message list */ |
| 628 |
lo = 0; |
| 629 |
hi = msg_count - 1; |
| 630 |
while (lo <= hi) |
| 631 |
{ |
| 632 |
/* split the difference */ |
| 633 |
cur = lo + (hi - lo)/2; |
| 634 |
|
| 635 |
/* is it a match? */ |
| 636 |
if (msg_array[cur].msgnum == msgnum) |
| 637 |
{ |
| 638 |
/* it's the one - return the text */ |
| 639 |
return (verbose |
| 640 |
? msg_array[cur].long_msgtxt |
| 641 |
: msg_array[cur].short_msgtxt); |
| 642 |
} |
| 643 |
else if (msgnum > msg_array[cur].msgnum) |
| 644 |
{ |
| 645 |
/* we need to go higher */ |
| 646 |
lo = (cur == lo ? cur + 1 : cur); |
| 647 |
} |
| 648 |
else |
| 649 |
{ |
| 650 |
/* we need to go lower */ |
| 651 |
hi = (cur == hi ? cur - 1 : cur); |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
/* no such message */ |
| 656 |
return 0; |
| 657 |
} |
| 658 |
|
| 659 |
/* ------------------------------------------------------------------------ */ |
| 660 |
/* |
| 661 |
* Format a message, allocating a buffer |
| 662 |
*/ |
| 663 |
char *err_format_msg(const char *msg, const CVmException *exc) |
| 664 |
{ |
| 665 |
/* |
| 666 |
* format the message into a null buffer to calculate the length; add a |
| 667 |
* byte for null termination |
| 668 |
*/ |
| 669 |
size_t len = err_format_msg(0, 0, msg, exc) + 1; |
| 670 |
|
| 671 |
/* allocate a buffer */ |
| 672 |
char *buf = (char *)t3malloc(len); |
| 673 |
|
| 674 |
/* format the message into the new buffer */ |
| 675 |
err_format_msg(buf, len, msg, exc); |
| 676 |
|
| 677 |
/* return the buffer */ |
| 678 |
return buf; |
| 679 |
} |
| 680 |
|
| 681 |
/* |
| 682 |
* Format a message with the parameters contained in an exception object. |
| 683 |
* Suports the following format codes: |
| 684 |
* |
| 685 |
* %s - String. Formats an ERR_TYPE_CHAR or ERR_TYPE_TEXTCHAR value, |
| 686 |
* including the counted-length versions. |
| 687 |
* |
| 688 |
* %d, %u, %x - signed/unsigned decimal integer, hexadecimal integer. |
| 689 |
* Formats an ERR_TYPE_INT value or an ERR_TYPE_ULONG value. |
| 690 |
* Automatically uses the correct size for the argument. |
| 691 |
* |
| 692 |
* %% - Formats as a single percent sign. |
| 693 |
*/ |
| 694 |
size_t err_format_msg(char *outbuf, size_t outbuflen, |
| 695 |
const char *msg, const CVmException *exc) |
| 696 |
{ |
| 697 |
int curarg; |
| 698 |
const char *p; |
| 699 |
char *dst; |
| 700 |
int exc_argc; |
| 701 |
size_t need = 0; |
| 702 |
size_t rem = outbuflen; |
| 703 |
|
| 704 |
/* get the number of parameters in the exception object */ |
| 705 |
exc_argc = (exc == 0 ? 0 : exc->get_param_count()); |
| 706 |
|
| 707 |
/* start with the first parameter */ |
| 708 |
curarg = 0; |
| 709 |
|
| 710 |
/* start at the beginning of the buffer */ |
| 711 |
dst = outbuf; |
| 712 |
|
| 713 |
/* if there's no message, there's nothing to return */ |
| 714 |
if (msg == 0) |
| 715 |
{ |
| 716 |
++need; |
| 717 |
if (outbuflen != 0) |
| 718 |
*dst = '\0'; |
| 719 |
return need; |
| 720 |
} |
| 721 |
|
| 722 |
/* scan the format string for formatting codes */ |
| 723 |
for (p = msg ; *p != '\0' ; ++p) |
| 724 |
{ |
| 725 |
/* if it's a format specifier, translate it */ |
| 726 |
if (*p == '%') |
| 727 |
{ |
| 728 |
const char *src; |
| 729 |
char srcbuf[30]; |
| 730 |
err_param_type typ; |
| 731 |
size_t len; |
| 732 |
int use_strlen; |
| 733 |
|
| 734 |
/* |
| 735 |
* if no more parameters are available, ignore the |
| 736 |
* formatting code entirely, and leave it in the string as |
| 737 |
* it is |
| 738 |
*/ |
| 739 |
if (curarg >= exc_argc) |
| 740 |
{ |
| 741 |
++need; |
| 742 |
if (rem > 1) |
| 743 |
--rem, *dst++ = *p; |
| 744 |
continue; |
| 745 |
} |
| 746 |
|
| 747 |
/* get the type of the current parameter */ |
| 748 |
typ = exc->get_param_type(curarg); |
| 749 |
|
| 750 |
/* |
| 751 |
* presume we'll want to use strlen to get the length of the |
| 752 |
* source value |
| 753 |
*/ |
| 754 |
use_strlen = TRUE; |
| 755 |
|
| 756 |
/* skip the '%' and determine what follows */ |
| 757 |
++p; |
| 758 |
switch (*p) |
| 759 |
{ |
| 760 |
case 's': |
| 761 |
/* get the string value using the appropriate type */ |
| 762 |
if (typ == ERR_TYPE_TEXTCHAR) |
| 763 |
src = exc->get_param_text(curarg); |
| 764 |
else if (typ == ERR_TYPE_CHAR) |
| 765 |
src = exc->get_param_char(curarg); |
| 766 |
else if (typ == ERR_TYPE_CHAR_LEN) |
| 767 |
{ |
| 768 |
/* get the string value and its length */ |
| 769 |
src = exc->get_param_char_len(curarg, &len); |
| 770 |
|
| 771 |
/* |
| 772 |
* src isn't null terminated, so don't use strlen to |
| 773 |
* get its length - we already have it from the |
| 774 |
* parameter data |
| 775 |
*/ |
| 776 |
use_strlen = FALSE; |
| 777 |
} |
| 778 |
else |
| 779 |
src = "s"; |
| 780 |
break; |
| 781 |
|
| 782 |
case 'd': |
| 783 |
src = srcbuf; |
| 784 |
if (typ == ERR_TYPE_INT) |
| 785 |
sprintf(srcbuf, "%d", exc->get_param_int(curarg)); |
| 786 |
else if (typ == ERR_TYPE_ULONG) |
| 787 |
sprintf(srcbuf, "%ld", exc->get_param_ulong(curarg)); |
| 788 |
else |
| 789 |
src = "d"; |
| 790 |
break; |
| 791 |
|
| 792 |
case 'u': |
| 793 |
src = srcbuf; |
| 794 |
if (typ == ERR_TYPE_INT) |
| 795 |
sprintf(srcbuf, "%u", exc->get_param_int(curarg)); |
| 796 |
else if (typ == ERR_TYPE_ULONG) |
| 797 |
sprintf(srcbuf, "%lu", exc->get_param_ulong(curarg)); |
| 798 |
else |
| 799 |
src = "u"; |
| 800 |
break; |
| 801 |
|
| 802 |
case 'x': |
| 803 |
src = srcbuf; |
| 804 |
if (typ == ERR_TYPE_INT) |
| 805 |
sprintf(srcbuf, "%x", exc->get_param_int(curarg)); |
| 806 |
else if (typ == ERR_TYPE_ULONG) |
| 807 |
sprintf(srcbuf, "%lx", exc->get_param_ulong(curarg)); |
| 808 |
else |
| 809 |
src = "x"; |
| 810 |
break; |
| 811 |
|
| 812 |
case '%': |
| 813 |
/* add a single percent sign */ |
| 814 |
src = "%"; |
| 815 |
break; |
| 816 |
|
| 817 |
default: |
| 818 |
/* invalid format character; leave the whole thing intact */ |
| 819 |
src = srcbuf; |
| 820 |
srcbuf[0] = '%'; |
| 821 |
srcbuf[1] = *p; |
| 822 |
srcbuf[2] = '\0'; |
| 823 |
break; |
| 824 |
} |
| 825 |
|
| 826 |
/* get the length, if it's null-terminated */ |
| 827 |
if (use_strlen) |
| 828 |
len = strlen(src); |
| 829 |
|
| 830 |
/* count the full 'len' in our space needs */ |
| 831 |
need +=len; |
| 832 |
|
| 833 |
/* copy as much as we can */ |
| 834 |
if (rem > 1) |
| 835 |
{ |
| 836 |
/* limit it to the remaining space, minus a null byte */ |
| 837 |
if (len > rem - 1) |
| 838 |
len = rem - 1; |
| 839 |
|
| 840 |
/* copy the value and advance past it in the output buffer */ |
| 841 |
memcpy(dst, src, len); |
| 842 |
dst += len; |
| 843 |
rem -= len; |
| 844 |
} |
| 845 |
|
| 846 |
/* consume the argument */ |
| 847 |
++curarg; |
| 848 |
} |
| 849 |
else |
| 850 |
{ |
| 851 |
/* just copy the current character as it is */ |
| 852 |
++need; |
| 853 |
if (rem > 1) |
| 854 |
--rem, *dst++ = *p; |
| 855 |
} |
| 856 |
} |
| 857 |
|
| 858 |
/* add the trailing null */ |
| 859 |
if (rem != 0) |
| 860 |
*dst++ = '\0'; |
| 861 |
|
| 862 |
/* return the space required */ |
| 863 |
return need; |
| 864 |
} |
| 865 |
|