| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header$"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 2000, 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 | vmbiftio.cpp - TADS Input/Output functions |
| 15 | Function |
| 16 | |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 02/08/00 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | #include <time.h> |
| 26 | |
| 27 | #include "t3std.h" |
| 28 | #include "os.h" |
| 29 | #include "utf8.h" |
| 30 | #include "charmap.h" |
| 31 | #include "vmbiftio.h" |
| 32 | #include "vmstack.h" |
| 33 | #include "vmerr.h" |
| 34 | #include "vmerrnum.h" |
| 35 | #include "vmglob.h" |
| 36 | #include "vmpool.h" |
| 37 | #include "vmobj.h" |
| 38 | #include "vmstr.h" |
| 39 | #include "vmlst.h" |
| 40 | #include "vmrun.h" |
| 41 | #include "vmfile.h" |
| 42 | #include "vmconsol.h" |
| 43 | #include "vmstrres.h" |
| 44 | #include "vmvsn.h" |
| 45 | #include "vmhost.h" |
| 46 | #include "vmpredef.h" |
| 47 | #include "vmcset.h" |
| 48 | #include "vmfilobj.h" |
| 49 | #include "vmfilnam.h" |
| 50 | #include "vmnetfil.h" |
| 51 | #include "vmnet.h" |
| 52 | |
| 53 | |
| 54 | /* ------------------------------------------------------------------------ */ |
| 55 | /* |
| 56 | * Display a value |
| 57 | */ |
| 58 | void CVmBifTIO::say(VMG_ uint argc) |
| 59 | { |
| 60 | /* write the value to the main console */ |
| 61 | say_to_console(vmg_ G_console, argc); |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * Display the value or values at top of stack to the given console |
| 66 | */ |
| 67 | void CVmBifTIO::say_to_console(VMG_ CVmConsole *console, uint argc) |
| 68 | { |
| 69 | vm_val_t val; |
| 70 | const char *str; |
| 71 | char buf[30]; |
| 72 | size_t len; |
| 73 | vm_val_t new_str; |
| 74 | |
| 75 | /* presume we won't need to create a new string value */ |
| 76 | new_str.set_nil(); |
| 77 | |
| 78 | /* display each argument */ |
| 79 | for ( ; argc != 0 ; --argc) |
| 80 | { |
| 81 | /* get our argument */ |
| 82 | G_stk->pop(&val); |
| 83 | |
| 84 | /* see what we have */ |
| 85 | switch(val.typ) |
| 86 | { |
| 87 | case VM_SSTRING: |
| 88 | /* get the string */ |
| 89 | str = G_const_pool->get_ptr(val.val.ofs); |
| 90 | |
| 91 | /* the original value is our string */ |
| 92 | new_str = val; |
| 93 | |
| 94 | disp_str: |
| 95 | /* push the string to protect from garbage collection */ |
| 96 | G_stk->push(&new_str); |
| 97 | |
| 98 | /* display the string through the output formatter */ |
| 99 | console->format_text(vmg_ str + 2, osrp2(str)); |
| 100 | |
| 101 | /* discard the saved string now that we no longer need it */ |
| 102 | G_stk->discard(); |
| 103 | |
| 104 | /* done */ |
| 105 | break; |
| 106 | |
| 107 | case VM_OBJ: |
| 108 | /* convert it to a string */ |
| 109 | str = vm_objp(vmg_ val.val.obj) |
| 110 | ->cast_to_string(vmg_ val.val.obj, &new_str); |
| 111 | |
| 112 | /* go display it */ |
| 113 | goto disp_str; |
| 114 | |
| 115 | case VM_LIST: |
| 116 | /* convert to a string */ |
| 117 | str = val.cast_to_string(vmg_ &new_str); |
| 118 | goto disp_str; |
| 119 | |
| 120 | case VM_INT: |
| 121 | /* convert it to a string */ |
| 122 | sprintf(buf + 2, "%ld", (long)val.val.intval); |
| 123 | |
| 124 | /* set its length */ |
| 125 | len = strlen(buf + 2); |
| 126 | oswp2(buf, len); |
| 127 | |
| 128 | /* display it */ |
| 129 | str = buf; |
| 130 | goto disp_str; |
| 131 | |
| 132 | case VM_NIL: |
| 133 | /* display nothing */ |
| 134 | break; |
| 135 | |
| 136 | default: |
| 137 | /* other types are invalid */ |
| 138 | err_throw(VMERR_BAD_TYPE_BIF); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /* ------------------------------------------------------------------------ */ |
| 144 | /* |
| 145 | * Logging - turn on or off output text capture |
| 146 | */ |
| 147 | |
| 148 | #define LOG_SCRIPT 1 |
| 149 | #define LOG_CMD 2 |
| 150 | #define LOG_EVENT 3 |
| 151 | |
| 152 | void CVmBifTIO::logging(VMG_ uint argc) |
| 153 | { |
| 154 | /* presume success */ |
| 155 | int ok = TRUE; |
| 156 | |
| 157 | /* check arguments */ |
| 158 | check_argc_range(vmg_ argc, 1, 2); |
| 159 | |
| 160 | /* get the arguments: filename, log type */ |
| 161 | const vm_val_t *filespec = G_stk->get(0); |
| 162 | int log_type = (argc >= 2 ? G_stk->get(1)->num_to_int(vmg0_) : LOG_SCRIPT); |
| 163 | |
| 164 | /* |
| 165 | * if they passed us nil, turn off logging; otherwise, start logging |
| 166 | * to the filename given by the string |
| 167 | */ |
| 168 | if (filespec->typ == VM_NIL) |
| 169 | { |
| 170 | /* turn off the appropriate type of logging */ |
| 171 | switch(log_type) |
| 172 | { |
| 173 | case LOG_SCRIPT: |
| 174 | G_console->close_log_file(vmg0_); |
| 175 | break; |
| 176 | |
| 177 | case LOG_CMD: |
| 178 | case LOG_EVENT: |
| 179 | G_console->close_command_log(vmg0_); |
| 180 | break; |
| 181 | |
| 182 | default: |
| 183 | err_throw(VMERR_BAD_VAL_BIF); |
| 184 | } |
| 185 | } |
| 186 | else |
| 187 | { |
| 188 | /* set up the recursive call context */ |
| 189 | vm_rcdesc rc(vmg_ "setLogFile", bif_table, 1, |
| 190 | G_stk->get(0), argc); |
| 191 | |
| 192 | /* open the appropriate log file */ |
| 193 | switch(log_type) |
| 194 | { |
| 195 | case LOG_SCRIPT: |
| 196 | ok = !G_console->open_log_file(vmg_ filespec, &rc); |
| 197 | break; |
| 198 | |
| 199 | case LOG_CMD: |
| 200 | case LOG_EVENT: |
| 201 | ok = !G_console->open_command_log( |
| 202 | vmg_ filespec, &rc, log_type == LOG_EVENT); |
| 203 | break; |
| 204 | |
| 205 | default: |
| 206 | err_throw(VMERR_BAD_VAL_BIF); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /* discard arguments */ |
| 211 | G_stk->discard(argc); |
| 212 | |
| 213 | /* return true on success, nil on error */ |
| 214 | retval_bool(vmg_ ok); |
| 215 | } |
| 216 | |
| 217 | /* ------------------------------------------------------------------------ */ |
| 218 | /* |
| 219 | * clearscreen - clear the main display screen |
| 220 | */ |
| 221 | void CVmBifTIO::clearscreen(VMG_ uint argc) |
| 222 | { |
| 223 | /* check arguments */ |
| 224 | check_argc(vmg_ argc, 0); |
| 225 | |
| 226 | /* ask the console to clear the screen */ |
| 227 | G_console->clear_window(vmg0_); |
| 228 | } |
| 229 | |
| 230 | /* ------------------------------------------------------------------------ */ |
| 231 | /* |
| 232 | * more - show MORE prompt |
| 233 | */ |
| 234 | void CVmBifTIO::more(VMG_ uint argc) |
| 235 | { |
| 236 | /* check arguments */ |
| 237 | check_argc(vmg_ argc, 0); |
| 238 | |
| 239 | /* ignore in web host mode */ |
| 240 | if (G_net_config != 0) |
| 241 | return; |
| 242 | |
| 243 | /* |
| 244 | * if we're reading from a script, ignore this request - these types of |
| 245 | * interactive pauses are irrelevant when reading a script, since we're |
| 246 | * getting our input non-interactively |
| 247 | */ |
| 248 | if (G_console->is_reading_script()) |
| 249 | return; |
| 250 | |
| 251 | /* flush the display output */ |
| 252 | G_console->flush_all(vmg_ VM_NL_NONE); |
| 253 | |
| 254 | /* show the MORE prompt */ |
| 255 | G_console->show_more_prompt(vmg0_); |
| 256 | } |
| 257 | |
| 258 | /* ------------------------------------------------------------------------ */ |
| 259 | /* |
| 260 | * input - get a line of input from the keyboard |
| 261 | */ |
| 262 | void CVmBifTIO::input(VMG_ uint argc) |
| 263 | { |
| 264 | /* check arguments */ |
| 265 | check_argc(vmg_ argc, 0); |
| 266 | |
| 267 | /* read a line of text from the keyboard */ |
| 268 | char buf[256]; |
| 269 | if (G_console->read_line(vmg_ buf, sizeof(buf))) |
| 270 | { |
| 271 | /* end of file - return nil */ |
| 272 | retval_nil(vmg0_); |
| 273 | } |
| 274 | else |
| 275 | { |
| 276 | /* return the string */ |
| 277 | retval_str(vmg_ buf); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /* ------------------------------------------------------------------------ */ |
| 282 | /* |
| 283 | * inputkey - read a keystroke |
| 284 | */ |
| 285 | void CVmBifTIO::inputkey(VMG_ uint argc) |
| 286 | { |
| 287 | char buf[32]; |
| 288 | char c[10]; |
| 289 | size_t len; |
| 290 | int evt; |
| 291 | static const int filter[] = { OS_EVT_KEY }; |
| 292 | |
| 293 | /* check arguments */ |
| 294 | check_argc(vmg_ argc, 0); |
| 295 | |
| 296 | /* check for script input */ |
| 297 | if (G_console->read_event_script( |
| 298 | vmg_ &evt, buf, sizeof(buf), |
| 299 | filter, sizeof(filter)/sizeof(filter[0]), 0)) |
| 300 | { |
| 301 | /* we got a key from the script */ |
| 302 | retval_ui_str(vmg_ buf); |
| 303 | |
| 304 | /* log the event */ |
| 305 | G_console->log_event(vmg_ OS_EVT_KEY, buf, strlen(buf), FALSE); |
| 306 | |
| 307 | /* done */ |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | /* flush any output */ |
| 312 | G_console->flush_all(vmg_ VM_NL_INPUT); |
| 313 | |
| 314 | /* there's no console in web host mode - simply return eof */ |
| 315 | if (G_net_config != 0) |
| 316 | { |
| 317 | retval_ui_str(vmg_ "[eof]"); |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | /* get a keystroke */ |
| 322 | c[0] = (char)os_getc_raw(); |
| 323 | len = 1; |
| 324 | |
| 325 | /* if it's an extended key, map it specially */ |
| 326 | if (c[0] == 0) |
| 327 | { |
| 328 | char extc; |
| 329 | |
| 330 | /* get the second part of the sequence */ |
| 331 | extc = (char)os_getc_raw(); |
| 332 | |
| 333 | /* map the extended key */ |
| 334 | map_ext_key(vmg_ buf, (unsigned char)extc); |
| 335 | } |
| 336 | else |
| 337 | { |
| 338 | /* continue fetching bytes until we have a full character */ |
| 339 | while (!raw_key_complete(vmg_ c, len) && len + 1 < sizeof(c)) |
| 340 | { |
| 341 | /* |
| 342 | * We don't yet have enough bytes for a complete character, so |
| 343 | * read another raw byte. The keyboard driver should already |
| 344 | * have queued up all of the bytes we need to complete the |
| 345 | * character sequence, so there should never be a delay from |
| 346 | * os_getc_raw() here - it should simply return the next byte |
| 347 | * of the sequence immediately. |
| 348 | */ |
| 349 | c[len++] = (char)os_getc_raw(); |
| 350 | } |
| 351 | c[len] = '\0'; |
| 352 | |
| 353 | /* |
| 354 | * translate the key from the local character set to UTF-8, and map |
| 355 | * the extended key code to the portable representation |
| 356 | */ |
| 357 | map_raw_key(vmg_ buf, c, len); |
| 358 | } |
| 359 | |
| 360 | /* reset the [MORE] counter */ |
| 361 | G_console->reset_line_count(FALSE); |
| 362 | |
| 363 | /* log the event */ |
| 364 | G_console->log_event(vmg_ OS_EVT_KEY, buf, strlen(buf), TRUE); |
| 365 | |
| 366 | /* return the string */ |
| 367 | retval_str(vmg_ buf); |
| 368 | } |
| 369 | |
| 370 | /* ------------------------------------------------------------------------ */ |
| 371 | /* |
| 372 | * inputevent - read an event |
| 373 | */ |
| 374 | void CVmBifTIO::inputevent(VMG_ uint argc) |
| 375 | { |
| 376 | int use_timeout; |
| 377 | unsigned long timeout; |
| 378 | os_event_info_t info; |
| 379 | int evt; |
| 380 | int ele_count; |
| 381 | vm_obj_id_t lst_obj; |
| 382 | CVmObjList *lst; |
| 383 | char keyname[32]; |
| 384 | vm_val_t val; |
| 385 | int from_script = FALSE; |
| 386 | static const int filter[] = |
| 387 | { |
| 388 | OS_EVT_KEY, OS_EVT_TIMEOUT, OS_EVT_NOTIMEOUT, |
| 389 | OS_EVT_HREF, OS_EVT_EOF, OS_EVT_COMMAND |
| 390 | }; |
| 391 | |
| 392 | /* check arguments */ |
| 393 | check_argc_range(vmg_ argc, 0, 1); |
| 394 | |
| 395 | /* if there's a timeout argument, get it */ |
| 396 | if (argc == 0) |
| 397 | { |
| 398 | /* there's no timeout */ |
| 399 | use_timeout = FALSE; |
| 400 | timeout = 0; |
| 401 | } |
| 402 | else if (G_stk->get(0)->typ == VM_NIL) |
| 403 | { |
| 404 | /* the timeout is nil, which is the same as no timeout */ |
| 405 | use_timeout = FALSE; |
| 406 | timeout = 0; |
| 407 | |
| 408 | /* discard the nil timeout value */ |
| 409 | G_stk->discard(); |
| 410 | } |
| 411 | else |
| 412 | { |
| 413 | /* pop the timeout value */ |
| 414 | timeout = pop_long_val(vmg0_); |
| 415 | |
| 416 | /* note that we have a timeout to use */ |
| 417 | use_timeout = TRUE; |
| 418 | } |
| 419 | |
| 420 | /* check for script input */ |
| 421 | if (G_console->read_event_script( |
| 422 | vmg_ &evt, info.href, sizeof(info.href), |
| 423 | filter, sizeof(filter)/sizeof(filter[0]), 0)) |
| 424 | { |
| 425 | /* we got a script event - note it */ |
| 426 | from_script = TRUE; |
| 427 | |
| 428 | /* translate certain events */ |
| 429 | switch (evt) |
| 430 | { |
| 431 | case OS_EVT_COMMAND: |
| 432 | /* read the numeric parameter */ |
| 433 | info.cmd_id = atoi(info.href); |
| 434 | break; |
| 435 | } |
| 436 | } |
| 437 | else if (G_net_config != 0) |
| 438 | { |
| 439 | /* there's no console in web host mode - return eof */ |
| 440 | evt = OS_EVT_EOF; |
| 441 | } |
| 442 | else |
| 443 | { |
| 444 | /* flush any buffered output */ |
| 445 | G_console->flush_all(vmg_ VM_NL_INPUT); |
| 446 | |
| 447 | /* reset the [MORE] counter */ |
| 448 | G_console->reset_line_count(FALSE); |
| 449 | |
| 450 | /* read an event from the OS layer */ |
| 451 | evt = os_get_event(timeout, use_timeout, &info); |
| 452 | } |
| 453 | |
| 454 | /* figure out how big a list we need to allocate */ |
| 455 | switch(evt) |
| 456 | { |
| 457 | case OS_EVT_KEY: |
| 458 | /* |
| 459 | * we need two elements - one for the event type code, one for the |
| 460 | * keystroke string |
| 461 | */ |
| 462 | ele_count = 2; |
| 463 | break; |
| 464 | |
| 465 | case OS_EVT_COMMAND: |
| 466 | /* we need a second element for the command ID */ |
| 467 | ele_count = 2; |
| 468 | break; |
| 469 | |
| 470 | case OS_EVT_HREF: |
| 471 | /* |
| 472 | * we need two elements - one for the event type code, one for the |
| 473 | * HREF string |
| 474 | */ |
| 475 | ele_count = 2; |
| 476 | break; |
| 477 | |
| 478 | default: |
| 479 | /* for anything else, we need only the event type code element */ |
| 480 | ele_count = 1; |
| 481 | break; |
| 482 | } |
| 483 | |
| 484 | /* create the return list */ |
| 485 | lst_obj = CVmObjList::create(vmg_ FALSE, ele_count); |
| 486 | lst = (CVmObjList *)vm_objp(vmg_ lst_obj); |
| 487 | lst->cons_clear(); |
| 488 | |
| 489 | /* save the list on the stack to protect against garbage collection */ |
| 490 | val.set_obj(lst_obj); |
| 491 | G_stk->push(&val); |
| 492 | |
| 493 | /* fill in the first element with the event type code */ |
| 494 | val.set_int(evt); |
| 495 | lst->cons_set_element(0, &val); |
| 496 | |
| 497 | /* set additional elements, according to the event type */ |
| 498 | switch(evt) |
| 499 | { |
| 500 | case OS_EVT_KEY: |
| 501 | /* map the extended or ordinary key, as appropriate */ |
| 502 | if (from_script) |
| 503 | { |
| 504 | /* we got a key from the script - it's in the 'href' field */ |
| 505 | val.set_obj(str_from_ui_str(vmg_ info.href)); |
| 506 | |
| 507 | /* log the event */ |
| 508 | G_console->log_event( |
| 509 | vmg_ OS_EVT_KEY, info.href, strlen(info.href), FALSE); |
| 510 | } |
| 511 | else if (info.key[0] == 0) |
| 512 | { |
| 513 | /* it's an extended key */ |
| 514 | map_ext_key(vmg_ keyname, info.key[1]); |
| 515 | |
| 516 | /* create a string for the key name */ |
| 517 | val.set_obj(CVmObjString::create( |
| 518 | vmg_ FALSE, keyname, strlen(keyname))); |
| 519 | |
| 520 | /* log the event */ |
| 521 | G_console->log_event( |
| 522 | vmg_ OS_EVT_KEY, keyname, strlen(keyname), TRUE); |
| 523 | } |
| 524 | else |
| 525 | { |
| 526 | char c[4]; |
| 527 | size_t len; |
| 528 | |
| 529 | /* fetch more bytes until we have a complete character */ |
| 530 | for (c[0] = (char)info.key[0], len = 1 ; |
| 531 | !raw_key_complete(vmg_ c, len) && len < sizeof(c) ; ) |
| 532 | { |
| 533 | /* |
| 534 | * Read another input event. The keyboard driver should |
| 535 | * already have queued up all of the bytes needed to |
| 536 | * complete this character sequence, so there should never |
| 537 | * be a delay from os_get_event() here - it should simply |
| 538 | * return immediately with another OS_EVT_KEY event with |
| 539 | * the next byte of the sequence. |
| 540 | */ |
| 541 | evt = os_get_event(0, FALSE, &info); |
| 542 | |
| 543 | /* |
| 544 | * if it's not a keystroke event, something's wrong - |
| 545 | * ignore the event and stop trying to read the remaining |
| 546 | * bytes of the character sequence |
| 547 | */ |
| 548 | if (evt != OS_EVT_KEY) |
| 549 | break; |
| 550 | |
| 551 | /* store the next byte of the sequence */ |
| 552 | c[len++] = (char)info.key[0]; |
| 553 | } |
| 554 | |
| 555 | /* it's an ordinary key - map it */ |
| 556 | map_raw_key(vmg_ keyname, c, len); |
| 557 | |
| 558 | /* create a string for the key name */ |
| 559 | val.set_obj(CVmObjString::create( |
| 560 | vmg_ FALSE, keyname, strlen(keyname))); |
| 561 | |
| 562 | /* log the event */ |
| 563 | G_console->log_event( |
| 564 | vmg_ OS_EVT_KEY, keyname, strlen(keyname), TRUE); |
| 565 | } |
| 566 | |
| 567 | /* add it to the list */ |
| 568 | lst->cons_set_element(1, &val); |
| 569 | |
| 570 | break; |
| 571 | |
| 572 | case OS_EVT_HREF: |
| 573 | /* create the string for the href text */ |
| 574 | val.set_obj(str_from_ui_str(vmg_ info.href)); |
| 575 | |
| 576 | /* add it to the list */ |
| 577 | lst->cons_set_element(1, &val); |
| 578 | |
| 579 | /* log it */ |
| 580 | G_console->log_event(vmg_ OS_EVT_HREF, |
| 581 | info.href, strlen(info.href), FALSE); |
| 582 | break; |
| 583 | |
| 584 | case OS_EVT_COMMAND: |
| 585 | /* the second element is the command ID code */ |
| 586 | val.set_int(info.cmd_id); |
| 587 | lst->cons_set_element(1, &val); |
| 588 | |
| 589 | /* log it */ |
| 590 | { |
| 591 | char buf[20]; |
| 592 | sprintf(buf, "%d", info.cmd_id); |
| 593 | G_console->log_event(vmg_ OS_EVT_COMMAND, |
| 594 | buf, strlen(buf), TRUE); |
| 595 | } |
| 596 | break; |
| 597 | |
| 598 | default: |
| 599 | /* other event types have no extra data */ |
| 600 | G_console->log_event(vmg_ evt); |
| 601 | break; |
| 602 | } |
| 603 | |
| 604 | /* return the list */ |
| 605 | retval_obj(vmg_ lst_obj); |
| 606 | |
| 607 | /* we can drop the garbage collection protection now */ |
| 608 | G_stk->discard(); |
| 609 | } |
| 610 | |
| 611 | /* ------------------------------------------------------------------------ */ |
| 612 | /* |
| 613 | * Service routine: Map an "extended" keystroke from raw os_getc_raw() |
| 614 | * notation to a UTF-8 key name. The caller should pass the second byte of |
| 615 | * the extended two-byte raw sequence. |
| 616 | */ |
| 617 | int CVmBifTIO::map_ext_key(VMG_ char *namebuf, int extc) |
| 618 | { |
| 619 | /* |
| 620 | * Portable key names for the extended keystrokes. We map the extended |
| 621 | * key codes to these strings, so that the TADS code can access arrow |
| 622 | * keys and the like. |
| 623 | */ |
| 624 | static const char *ext_key_names[] = |
| 625 | { |
| 626 | "[up]", /* CMD_UP - 1 */ |
| 627 | "[down]", /* CMD_DOWN - 2 */ |
| 628 | "[right]", /* CMD_RIGHT - 3 */ |
| 629 | "[left]", /* CMD_LEFT - 4 */ |
| 630 | "[end]", /* CMD_END - 5 */ |
| 631 | "[home]", /* CMD_HOME - 6 */ |
| 632 | "[del-eol]", /* CMD_DEOL - 7 */ |
| 633 | "[del-line]", /* CMD_KILL - 8 */ |
| 634 | "[del]", /* CMD_DEL - 9 */ |
| 635 | "[scroll]", /* CMD_SCR - 10 */ |
| 636 | "[page up]", /* CMD_PGUP - 11 */ |
| 637 | "[page down]", /* CMD_PGDN - 12 */ |
| 638 | "[top]", /* CMD_TOP - 13 */ |
| 639 | "[bottom]", /* CMD_BOT - 14 */ |
| 640 | "[f1]", /* CMD_F1 - 15 */ |
| 641 | "[f2]", /* CMD_F2 - 16 */ |
| 642 | "[f3]", /* CMD_F3 - 17 */ |
| 643 | "[f4]", /* CMD_F4 - 18 */ |
| 644 | "[f5]", /* CMD_F5 - 19 */ |
| 645 | "[f6]", /* CMD_F6 - 20 */ |
| 646 | "[f7]", /* CMD_F7 - 21 */ |
| 647 | "[f8]", /* CMD_F8 - 22 */ |
| 648 | "[f9]", /* CMD_F9 - 23 */ |
| 649 | "[f10]", /* CMD_F10 - 24 */ |
| 650 | "[?]", /* invalid key - CMD_CHOME - 25 */ |
| 651 | "[tab]", /* CMD_TAB - 26 */ |
| 652 | "[?]", /* invalid key - shift-F2 - 27 */ |
| 653 | "[?]", /* not used (obsoleted) - 28 */ |
| 654 | "[word-left]", /* CMD_WORD_LEFT - 29 */ |
| 655 | "[word-right]", /* CMD_WORD_RIGHT - 30 */ |
| 656 | "[del-word]", /* CMD_WORDKILL - 31 */ |
| 657 | "[eof]", /* CMD_EOF - 32 */ |
| 658 | "[break]", /* CMD_BREAK - 33 */ |
| 659 | "[insert]" /* CMD_INS - 34 */ |
| 660 | }; |
| 661 | |
| 662 | /* if it's in the key name array, use the array entry */ |
| 663 | if (extc >= 1 |
| 664 | && extc <= (int)sizeof(ext_key_names)/sizeof(ext_key_names[0])) |
| 665 | { |
| 666 | /* use the array name */ |
| 667 | strcpy(namebuf, ext_key_names[extc - 1]); |
| 668 | return TRUE; |
| 669 | } |
| 670 | |
| 671 | /* if it's in the ALT key range, generate an ALT key name */ |
| 672 | if ((unsigned char)extc >= CMD_ALT && (unsigned char)extc <= CMD_ALT + 25) |
| 673 | { |
| 674 | /* generate an ALT key name */ |
| 675 | strcpy(namebuf, "[alt-?]"); |
| 676 | namebuf[5] = (char)(extc - CMD_ALT + 'a'); |
| 677 | return TRUE; |
| 678 | } |
| 679 | |
| 680 | /* it's not a valid key - use '[?]' as the name */ |
| 681 | strcpy(namebuf, "[?]"); |
| 682 | return FALSE; |
| 683 | } |
| 684 | |
| 685 | /* |
| 686 | * Service routine: Map a keystroke from the raw notation, consisting of a |
| 687 | * normal keystroke in the local character set or an extended command key |
| 688 | * using a CMD_xxx code, to UTF-8. If the keystroke is a control character |
| 689 | * or any CMD_xxx code, we'll map the key to a high-level keystroke name |
| 690 | * enclosed in square brackets. |
| 691 | */ |
| 692 | int CVmBifTIO::map_raw_key(VMG_ char *namebuf, const char *c, size_t len) |
| 693 | { |
| 694 | size_t outlen; |
| 695 | |
| 696 | /* if it's a control character, give it a portable key name */ |
| 697 | if (len == 1 && ((c[0] >= 1 && c[0] <= 27) || c[0] == 127)) |
| 698 | { |
| 699 | switch(c[0]) |
| 700 | { |
| 701 | case 10: |
| 702 | case 13: |
| 703 | /* |
| 704 | * return an ASCII 10 (regardless of local newline conventions |
| 705 | * - this is the internal string representation, which we |
| 706 | * define to use ASCII 10 to represent a newline everywhere) |
| 707 | */ |
| 708 | namebuf[0] = 10; |
| 709 | namebuf[1] = '\0'; |
| 710 | return TRUE; |
| 711 | |
| 712 | case 9: |
| 713 | /* return ASCII 9 for TAB characters */ |
| 714 | namebuf[0] = 9; |
| 715 | namebuf[1] = '\0'; |
| 716 | return TRUE; |
| 717 | |
| 718 | case 8: |
| 719 | case 127: |
| 720 | /* return '[bksp]' for backspace/del characters */ |
| 721 | strcpy(namebuf, "[bksp]"); |
| 722 | return TRUE; |
| 723 | |
| 724 | case 27: |
| 725 | /* return '[esc]' for the escape key */ |
| 726 | strcpy(namebuf, "[esc]"); |
| 727 | return TRUE; |
| 728 | |
| 729 | default: |
| 730 | /* return '[ctrl-X]' for other control characters */ |
| 731 | strcpy(namebuf, "[ctrl-?]"); |
| 732 | namebuf[6] = (char)(c[0] + 'a' - 1); |
| 733 | return TRUE; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | /* map the character to wide Unicode */ |
| 738 | outlen = 32; |
| 739 | G_cmap_from_ui->map(&namebuf, &outlen, c, len); |
| 740 | |
| 741 | /* null-terminate the result */ |
| 742 | *namebuf = '\0'; |
| 743 | |
| 744 | /* successfully mapped */ |
| 745 | return TRUE; |
| 746 | } |
| 747 | |
| 748 | /* |
| 749 | * Service routine: determine if a raw byte sequence forms a complete |
| 750 | * character in the local character set. |
| 751 | */ |
| 752 | int CVmBifTIO::raw_key_complete(VMG_ const char *c, size_t len) |
| 753 | { |
| 754 | /* ask the local character mapper if it's a complete character */ |
| 755 | return G_cmap_from_ui->is_complete_char(c, len); |
| 756 | } |
| 757 | |
| 758 | |
| 759 | /* ------------------------------------------------------------------------ */ |
| 760 | /* |
| 761 | * Standard system button labels for bifinpdlg() |
| 762 | */ |
| 763 | #define BIFINPDLG_LBL_OK 1 |
| 764 | #define BIFINPDLG_LBL_CANCEL 2 |
| 765 | #define BIFINPDLG_LBL_YES 3 |
| 766 | #define BIFINPDLG_LBL_NO 4 |
| 767 | |
| 768 | /* |
| 769 | * inputdialog - run a dialog |
| 770 | */ |
| 771 | void CVmBifTIO::inputdialog(VMG_ uint argc) |
| 772 | { |
| 773 | int icon_id; |
| 774 | char prompt[256]; |
| 775 | char label_buf[256]; |
| 776 | vm_val_t label_val[10]; |
| 777 | const char *labels[10]; |
| 778 | int lst_cnt; |
| 779 | int std_btns; |
| 780 | int btn_cnt; |
| 781 | char *dst; |
| 782 | size_t dstrem; |
| 783 | int default_resp; |
| 784 | int cancel_resp; |
| 785 | int resp; |
| 786 | char numbuf[32]; |
| 787 | |
| 788 | /* check arguments */ |
| 789 | check_argc(vmg_ argc, 5); |
| 790 | |
| 791 | /* get the icon number */ |
| 792 | icon_id = pop_int_val(vmg0_); |
| 793 | |
| 794 | /* get the prompt string */ |
| 795 | pop_str_val_ui(vmg_ prompt, sizeof(prompt)); |
| 796 | |
| 797 | /* there aren't any buttons yet */ |
| 798 | btn_cnt = 0; |
| 799 | |
| 800 | /* check for the button type */ |
| 801 | if (G_stk->get(0)->typ == VM_INT) |
| 802 | { |
| 803 | /* get the standard button set ID */ |
| 804 | std_btns = pop_int_val(vmg0_); |
| 805 | } |
| 806 | else if (G_stk->get(0)->is_listlike(vmg0_) |
| 807 | && (lst_cnt = G_stk->get(0)->ll_length(vmg0_)) >= 0) |
| 808 | { |
| 809 | int i; |
| 810 | vm_val_t *valp; |
| 811 | |
| 812 | /* we're not using any standard button set */ |
| 813 | std_btns = 0; |
| 814 | |
| 815 | /* |
| 816 | * run through the list and get the button items into our array |
| 817 | * (we do this rather than traversing the list directly so that |
| 818 | * we don't have to worry about a constant list's data being |
| 819 | * paged out) |
| 820 | */ |
| 821 | vm_val_t *lst = G_stk->get(0); |
| 822 | |
| 823 | /* limit the number of elements to our private array size */ |
| 824 | if (lst_cnt > (int)sizeof(label_val)/sizeof(label_val[0])) |
| 825 | lst_cnt = sizeof(label_val)/sizeof(label_val[0]); |
| 826 | |
| 827 | /* copy the list */ |
| 828 | for (i = 1, valp = label_val ; i <= lst_cnt ; ++i, ++valp) |
| 829 | lst->ll_index(vmg_ valp, i); |
| 830 | |
| 831 | /* done with the list - discard it */ |
| 832 | G_stk->discard(); |
| 833 | |
| 834 | /* set up to write into our label buffer */ |
| 835 | dst = label_buf; |
| 836 | dstrem = sizeof(label_buf); |
| 837 | |
| 838 | /* now build our internal button list from the array elements */ |
| 839 | for (i = 0, valp = label_val ; i < lst_cnt ; ++i, ++valp) |
| 840 | { |
| 841 | const char *p; |
| 842 | |
| 843 | /* |
| 844 | * We could have a number or a string in each element. If |
| 845 | * the element is a number, it refers to a standard label. |
| 846 | * If it's a string, use the string directly. |
| 847 | */ |
| 848 | if ((p = valp->get_as_string(vmg0_)) != 0) |
| 849 | { |
| 850 | size_t copy_len; |
| 851 | |
| 852 | /* |
| 853 | * it's a string - make a copy in the label buffer, |
| 854 | * making sure to leave space for null termination |
| 855 | */ |
| 856 | copy_len = vmb_get_len(p); |
| 857 | if (copy_len > dstrem - 1) |
| 858 | copy_len = utf8_ptr::s_trunc(p + VMB_LEN, dstrem - 1); |
| 859 | memcpy(dst, p + VMB_LEN, copy_len); |
| 860 | |
| 861 | /* null-terminate the buffer */ |
| 862 | dst[copy_len++] = '\0'; |
| 863 | |
| 864 | /* set this button to point to the converted text */ |
| 865 | labels[btn_cnt++] = dst; |
| 866 | |
| 867 | /* skip past this label */ |
| 868 | dst += copy_len; |
| 869 | dstrem -= copy_len; |
| 870 | } |
| 871 | else if (valp->typ == VM_INT) |
| 872 | { |
| 873 | int id; |
| 874 | int resid; |
| 875 | char rscbuf[128]; |
| 876 | |
| 877 | /* it's a standard system label ID - get the ID */ |
| 878 | id = (int)valp->val.intval; |
| 879 | |
| 880 | /* translate it to the appropriate string resource */ |
| 881 | switch(id) |
| 882 | { |
| 883 | case BIFINPDLG_LBL_OK: |
| 884 | resid = VMRESID_BTN_OK; |
| 885 | break; |
| 886 | |
| 887 | case BIFINPDLG_LBL_CANCEL: |
| 888 | resid = VMRESID_BTN_CANCEL; |
| 889 | break; |
| 890 | |
| 891 | case BIFINPDLG_LBL_YES: |
| 892 | resid = VMRESID_BTN_YES; |
| 893 | break; |
| 894 | |
| 895 | case BIFINPDLG_LBL_NO: |
| 896 | resid = VMRESID_BTN_NO; |
| 897 | break; |
| 898 | |
| 899 | default: |
| 900 | resid = 0; |
| 901 | break; |
| 902 | } |
| 903 | |
| 904 | /* |
| 905 | * if we got a valid resource ID, load the resource; |
| 906 | * otherwise, skip this button |
| 907 | */ |
| 908 | if (resid != 0 |
| 909 | && !os_get_str_rsc(resid, rscbuf, sizeof(rscbuf))) |
| 910 | { |
| 911 | /* set this button to point to the converted text */ |
| 912 | labels[btn_cnt++] = dst; |
| 913 | |
| 914 | /* convert the resource text to UTF-8 */ |
| 915 | G_cmap_from_ui->map(&dst, &dstrem, |
| 916 | rscbuf, strlen(rscbuf)); |
| 917 | |
| 918 | /* null-terminate the converted text */ |
| 919 | *dst++ = '\0'; |
| 920 | --dstrem; |
| 921 | } |
| 922 | } |
| 923 | } |
| 924 | } |
| 925 | else |
| 926 | { |
| 927 | /* invalid button type */ |
| 928 | err_throw(VMERR_BAD_TYPE_BIF); |
| 929 | } |
| 930 | |
| 931 | /* get the default response */ |
| 932 | if (G_stk->get(0)->typ == VM_NIL) |
| 933 | { |
| 934 | /* discard the nil argument */ |
| 935 | G_stk->discard(); |
| 936 | |
| 937 | /* there's no default response */ |
| 938 | default_resp = 0; |
| 939 | } |
| 940 | else |
| 941 | { |
| 942 | /* get the default response index */ |
| 943 | default_resp = pop_int_val(vmg0_); |
| 944 | } |
| 945 | |
| 946 | /* get the cancel response */ |
| 947 | if (G_stk->get(0)->typ == VM_NIL) |
| 948 | { |
| 949 | /* discard the nil argument */ |
| 950 | G_stk->discard(); |
| 951 | |
| 952 | /* there's no cancel response */ |
| 953 | cancel_resp = 0; |
| 954 | } |
| 955 | else |
| 956 | { |
| 957 | /* get the cancel response index */ |
| 958 | cancel_resp = pop_int_val(vmg0_); |
| 959 | } |
| 960 | |
| 961 | /* check for script input */ |
| 962 | static int filter[] = { VMCON_EVT_DIALOG }; |
| 963 | int evt; |
| 964 | if (G_console->read_event_script( |
| 965 | vmg_ &evt, numbuf, sizeof(numbuf), |
| 966 | filter, sizeof(filter)/sizeof(filter[0]), 0)) |
| 967 | { |
| 968 | /* we got a script response - no need to show the dialog */ |
| 969 | resp = atoi(numbuf); |
| 970 | |
| 971 | /* log the event */ |
| 972 | G_console->log_event(vmg_ VMCON_EVT_DIALOG, |
| 973 | numbuf, strlen(numbuf), FALSE); |
| 974 | } |
| 975 | else if (G_net_config != 0) |
| 976 | { |
| 977 | /* there's no console in web host mode - return eof */ |
| 978 | resp = 0; |
| 979 | } |
| 980 | else |
| 981 | { |
| 982 | /* flush output before showing the dialog */ |
| 983 | G_console->flush_all(vmg_ VM_NL_INPUT); |
| 984 | |
| 985 | /* show the dialog */ |
| 986 | resp = G_console->input_dialog(vmg_ icon_id, prompt, |
| 987 | std_btns, labels, btn_cnt, |
| 988 | default_resp, cancel_resp); |
| 989 | |
| 990 | /* log the event */ |
| 991 | sprintf(numbuf, "%d", resp); |
| 992 | G_console->log_event(vmg_ VMCON_EVT_DIALOG, |
| 993 | numbuf, strlen(numbuf), TRUE); |
| 994 | } |
| 995 | |
| 996 | /* return the result */ |
| 997 | retval_int(vmg_ resp); |
| 998 | } |
| 999 | |
| 1000 | /* ------------------------------------------------------------------------ */ |
| 1001 | /* |
| 1002 | * askfile - ask for a filename via a standard file dialog |
| 1003 | */ |
| 1004 | void CVmBifTIO::askfile(VMG_ uint argc) |
| 1005 | { |
| 1006 | char prompt[256]; |
| 1007 | int dialog_type; |
| 1008 | os_filetype_t file_type; |
| 1009 | int result; |
| 1010 | char fname[OSFNMAX*3 + 1]; |
| 1011 | vm_obj_id_t lst_obj; |
| 1012 | CVmObjList *lst; |
| 1013 | vm_val_t val; |
| 1014 | int from_script = FALSE; |
| 1015 | char warning[OSFNMAX + 255] = ""; |
| 1016 | int from_ui = FALSE; |
| 1017 | |
| 1018 | /* check arguments */ |
| 1019 | check_argc(vmg_ argc, 4); |
| 1020 | |
| 1021 | /* get the prompt string */ |
| 1022 | pop_str_val_ui(vmg_ prompt, sizeof(prompt)); |
| 1023 | |
| 1024 | /* get the dialog type and file type */ |
| 1025 | dialog_type = pop_int_val(vmg0_); |
| 1026 | file_type = (os_filetype_t)pop_int_val(vmg0_); |
| 1027 | |
| 1028 | /* |
| 1029 | * Pop and discard the flags. (This argument isn't used currently; |
| 1030 | * it's just there in case we need some option flags in the future. |
| 1031 | * Pop it as an integer to ensure the caller specified the correct |
| 1032 | * type, but discard the value.) |
| 1033 | */ |
| 1034 | (void)pop_long_val(vmg0_); |
| 1035 | |
| 1036 | /* check for a script response */ |
| 1037 | static int filter[] = { VMCON_EVT_FILE }; |
| 1038 | int evt; |
| 1039 | unsigned long attrs; |
| 1040 | if (G_console->read_event_script( |
| 1041 | vmg_ &evt, fname, sizeof(fname), filter, countof(filter), &attrs)) |
| 1042 | { |
| 1043 | int ok = TRUE; |
| 1044 | |
| 1045 | /* we got a response from the script */ |
| 1046 | from_script = TRUE; |
| 1047 | result = (fname[0] != '\0' ? OS_AFE_SUCCESS : OS_AFE_CANCEL); |
| 1048 | |
| 1049 | /* |
| 1050 | * If this is a "save" prompt, and the OVERWRITE flag isn't set, |
| 1051 | * and the file already exists, show an interactive warning that |
| 1052 | * we're about to ovewrite the file. |
| 1053 | */ |
| 1054 | if (ok |
| 1055 | && dialog_type == OS_AFP_SAVE |
| 1056 | && result == OS_AFE_SUCCESS |
| 1057 | && (attrs & VMCON_EVTATTR_OVERWRITE) == 0 |
| 1058 | && CVmNetFile::exists(vmg_ fname, 0)) |
| 1059 | { |
| 1060 | /* the file exists - warn about the overwrite */ |
| 1061 | ok = FALSE; |
| 1062 | t3sprintf(warning, sizeof(warning), |
| 1063 | "OV The script might overwrite the file %s. ", fname); |
| 1064 | } |
| 1065 | |
| 1066 | /* |
| 1067 | * If this is a "save" prompt, check to see if we can write the |
| 1068 | * file, and warn if not. |
| 1069 | */ |
| 1070 | if (ok |
| 1071 | && dialog_type == OS_AFP_SAVE |
| 1072 | && result == OS_AFE_SUCCESS |
| 1073 | && !CVmNetFile::can_write(vmg_ fname, 0)) |
| 1074 | { |
| 1075 | /* try creating the file */ |
| 1076 | osfildef *fp = osfopwb(fname, file_type); |
| 1077 | |
| 1078 | /* if that succeeded, undo the creation; otherwise warn */ |
| 1079 | if (fp != 0) |
| 1080 | { |
| 1081 | /* it worked - close and delete the test file */ |
| 1082 | osfcls(fp); |
| 1083 | osfdel(fname); |
| 1084 | } |
| 1085 | else |
| 1086 | { |
| 1087 | /* didn't work - warn about it */ |
| 1088 | ok = FALSE; |
| 1089 | t3sprintf(warning, sizeof(warning), |
| 1090 | "WR The script is attempting to write file %s, " |
| 1091 | "but that file cannot be written.", fname); |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | /* |
| 1096 | * If this is an "open" prompt, and the file isn't readable, warn |
| 1097 | * about it. |
| 1098 | */ |
| 1099 | if (ok |
| 1100 | && dialog_type == OS_AFP_OPEN |
| 1101 | && result == OS_AFE_SUCCESS |
| 1102 | && !CVmNetFile::exists(vmg_ fname, 0)) |
| 1103 | { |
| 1104 | ok = FALSE; |
| 1105 | t3sprintf(warning, sizeof(warning), |
| 1106 | "RD The script is attempting to open file %s, " |
| 1107 | "but this file doesn't exist or isn't readable.", |
| 1108 | fname); |
| 1109 | } |
| 1110 | |
| 1111 | /* |
| 1112 | * If we generated a warning, and we're not in Web UI mode, display |
| 1113 | * the warning as a Yes/No/Cancel console input dialog. We can't |
| 1114 | * do this in Web UI mode, since we can't use the regular console |
| 1115 | * in Web mode. Instead, we'll return the warning information to |
| 1116 | * the caller for display. |
| 1117 | */ |
| 1118 | if (!ok && G_net_config == 0) |
| 1119 | { |
| 1120 | char fullprompt[OSFNMAX + 255 + 150]; |
| 1121 | |
| 1122 | /* build the full prompt */ |
| 1123 | t3sprintf(fullprompt, sizeof(fullprompt), |
| 1124 | "%s Do you wish to proceed? Select Yes to " |
| 1125 | "proceed with this file, No to choose a different " |
| 1126 | "file, or Cancel to stop replaying this script.", |
| 1127 | warning + 3); |
| 1128 | |
| 1129 | /* |
| 1130 | * display a dialog - note that this goes directly to user, |
| 1131 | * bypassing the active script, since this is a question about |
| 1132 | * how to handle a problem in the script |
| 1133 | */ |
| 1134 | show_warning: |
| 1135 | switch (G_console->input_dialog( |
| 1136 | vmg_ OS_INDLG_ICON_WARNING, fullprompt, |
| 1137 | OS_INDLG_YESNOCANCEL, 0, 0, 2, 3, TRUE)) |
| 1138 | { |
| 1139 | case 1: |
| 1140 | /* yes - proceed with fname */ |
| 1141 | break; |
| 1142 | |
| 1143 | case 2: |
| 1144 | /* no - ask for a new file */ |
| 1145 | result = G_console->askfile( |
| 1146 | vmg_ prompt, strlen(prompt), |
| 1147 | fname, sizeof(fname), dialog_type, file_type, TRUE); |
| 1148 | |
| 1149 | /* this didn't come from the script after all */ |
| 1150 | from_script = FALSE; |
| 1151 | |
| 1152 | /* |
| 1153 | * If they canceled the file selection, go back to the |
| 1154 | * warning dialog; if the dialog itself failed, cancel the |
| 1155 | * script entirely. If they selected a file, proceed with |
| 1156 | * their new file replacing the script input. Note that we |
| 1157 | * don't have to repeat the tests above on the new file, |
| 1158 | * since the user explicitly entered it and thus presumably |
| 1159 | * knows that it's the one they really want to use. |
| 1160 | */ |
| 1161 | if (result == OS_AFE_CANCEL) |
| 1162 | goto show_warning; |
| 1163 | else if (result == OS_AFE_FAILURE) |
| 1164 | close_script_file(vmg0_); |
| 1165 | |
| 1166 | /* handled */ |
| 1167 | break; |
| 1168 | |
| 1169 | case 3: |
| 1170 | /* cancel - stop the script playback */ |
| 1171 | close_script_file(vmg0_); |
| 1172 | |
| 1173 | /* indicate cancellation */ |
| 1174 | result = OS_AFE_CANCEL; |
| 1175 | break; |
| 1176 | } |
| 1177 | |
| 1178 | /* we've displayed the warning, so don't return it */ |
| 1179 | warning[0] = '\0'; |
| 1180 | } |
| 1181 | } |
| 1182 | else if (G_net_config != 0) |
| 1183 | { |
| 1184 | #ifdef TADSNET |
| 1185 | /* |
| 1186 | * If we're running in the local stand-alone configuration, we have |
| 1187 | * a bit of a special case: we're accessing files on the local file |
| 1188 | * system, but we're presenting the rest of the program's UI |
| 1189 | * through a browser window, which might be in a separate child |
| 1190 | * process; we have to present the file dialog UI in the same |
| 1191 | * manner. We have a special OS function for this situation. |
| 1192 | * |
| 1193 | * This only applies in the stand-alone configuration, where we |
| 1194 | * have no "hostname" parameter in the net config. If we have a |
| 1195 | * hostname, it means that we're operating in full client/server |
| 1196 | * mode, in which case we go through the Web UI directly. |
| 1197 | */ |
| 1198 | if (G_net_config->get("hostname") == 0) |
| 1199 | { |
| 1200 | result = osnet_askfile(prompt, fname, sizeof(fname), |
| 1201 | dialog_type, file_type); |
| 1202 | } |
| 1203 | else |
| 1204 | #endif |
| 1205 | { |
| 1206 | /* there's no console in web host mode - return eof */ |
| 1207 | result = OS_AFE_FAILURE; |
| 1208 | } |
| 1209 | } |
| 1210 | else |
| 1211 | { |
| 1212 | /* ask for a file via the console UI */ |
| 1213 | result = G_console->askfile( |
| 1214 | vmg_ prompt, strlen(prompt), |
| 1215 | fname, sizeof(fname), dialog_type, file_type); |
| 1216 | |
| 1217 | /* this file came from the console UI */ |
| 1218 | from_ui = TRUE; |
| 1219 | } |
| 1220 | |
| 1221 | /* |
| 1222 | * Create the return list. In all cases, we need one element for the |
| 1223 | * status code. On success, we need three addition elements (the |
| 1224 | * filename string, the descriptive text [future expansion], and the |
| 1225 | * warning message text). |
| 1226 | */ |
| 1227 | lst_obj = CVmObjList::create(vmg_ FALSE, |
| 1228 | result == OS_AFE_SUCCESS ? 4 : 1); |
| 1229 | lst = (CVmObjList *)vm_objp(vmg_ lst_obj); |
| 1230 | lst->cons_clear(); |
| 1231 | |
| 1232 | /* save it on the stack as protection against garbage collection */ |
| 1233 | val.set_obj(lst_obj); |
| 1234 | G_stk->push(&val); |
| 1235 | |
| 1236 | /* set the first element to the result code */ |
| 1237 | val.set_int(result); |
| 1238 | lst->cons_set_element(0, &val); |
| 1239 | |
| 1240 | /* add the extra elements for the success case */ |
| 1241 | if (result == OS_AFE_SUCCESS) |
| 1242 | { |
| 1243 | char *fnamep = fname, *fname2 = 0; |
| 1244 | err_try |
| 1245 | { |
| 1246 | /* if the name came from a script, map from the local char set */ |
| 1247 | if (from_script) |
| 1248 | { |
| 1249 | G_cmap_from_ui->map_str_alo(&fname2, fname); |
| 1250 | fnamep = fname2; |
| 1251 | } |
| 1252 | |
| 1253 | /* create a FileName object to represent the filename */ |
| 1254 | val.set_obj(CVmObjFileName::create_from_local( |
| 1255 | vmg_ fnamep, strlen(fnamep))); |
| 1256 | |
| 1257 | /* |
| 1258 | * if the name came from the console UI, flag the FileName as |
| 1259 | * being user-selected, which allows it to override the file |
| 1260 | * safety settings; since the user manually selected the name, |
| 1261 | * they implicitly granted permission to use the file for the |
| 1262 | * type of operation proposed by the dialog |
| 1263 | */ |
| 1264 | if (from_ui) |
| 1265 | { |
| 1266 | vm_objid_cast(CVmObjFileName, val.val.obj)->set_from_ui( |
| 1267 | dialog_type); |
| 1268 | } |
| 1269 | } |
| 1270 | err_finally |
| 1271 | { |
| 1272 | lib_free_str(fname2); |
| 1273 | } |
| 1274 | err_end; |
| 1275 | |
| 1276 | /* store the string as the second list element */ |
| 1277 | lst->cons_set_element(1, &val); |
| 1278 | |
| 1279 | /* |
| 1280 | * Add nil for the descriptive text, since we don't currently |
| 1281 | * implement this feature. (This is a future enhancement that some |
| 1282 | * system file selector dialogs might provide, allowing the user to |
| 1283 | * enter descriptive text in addition to the filename. The Web UI |
| 1284 | * storage server already uses this to save user descriptive text |
| 1285 | * with saved game files.) |
| 1286 | */ |
| 1287 | val.set_nil(); |
| 1288 | lst->cons_set_element(2, &val); |
| 1289 | |
| 1290 | /* |
| 1291 | * Add the script warning message, or nil if there's no warning. |
| 1292 | * The warning message contains the filename text from the script, |
| 1293 | * so translate it to the local character set. |
| 1294 | */ |
| 1295 | if (warning[0] != '\0') |
| 1296 | val.set_obj(str_from_ui_str(vmg_ warning)); |
| 1297 | else |
| 1298 | val.set_nil(); |
| 1299 | lst->cons_set_element(3, &val); |
| 1300 | } |
| 1301 | |
| 1302 | /* log the event */ |
| 1303 | if (result == OS_AFE_SUCCESS) |
| 1304 | G_console->log_event(vmg_ VMCON_EVT_FILE, |
| 1305 | fname, strlen(fname), FALSE); |
| 1306 | else |
| 1307 | G_console->log_event(vmg_ VMCON_EVT_FILE); |
| 1308 | |
| 1309 | /* return the list */ |
| 1310 | retval_obj(vmg_ lst_obj); |
| 1311 | |
| 1312 | /* we no longer need the garbage collector protection */ |
| 1313 | G_stk->discard(); |
| 1314 | } |
| 1315 | |
| 1316 | |
| 1317 | /* ------------------------------------------------------------------------ */ |
| 1318 | /* |
| 1319 | * timeDelay - pause for a specified interval |
| 1320 | */ |
| 1321 | void CVmBifTIO::timedelay(VMG_ uint argc) |
| 1322 | { |
| 1323 | long delay_ms; |
| 1324 | |
| 1325 | /* check arguments */ |
| 1326 | check_argc(vmg_ argc, 1); |
| 1327 | |
| 1328 | /* get the delay time in milliseconds */ |
| 1329 | delay_ms = pop_long_val(vmg0_); |
| 1330 | |
| 1331 | /* flush any pending output */ |
| 1332 | G_console->flush_all(vmg_ VM_NL_NONE); |
| 1333 | |
| 1334 | /* ask the system code to pause */ |
| 1335 | os_sleep_ms(delay_ms); |
| 1336 | } |
| 1337 | |
| 1338 | /* ------------------------------------------------------------------------ */ |
| 1339 | /* |
| 1340 | * systemInfo |
| 1341 | */ |
| 1342 | void CVmBifTIO::sysinfo(VMG_ uint argc) |
| 1343 | { |
| 1344 | int info; |
| 1345 | long result; |
| 1346 | |
| 1347 | /* make sure we have at least one argument */ |
| 1348 | if (argc < 1) |
| 1349 | err_throw(VMERR_WRONG_NUM_OF_ARGS); |
| 1350 | |
| 1351 | /* get the information type code */ |
| 1352 | info = pop_int_val(vmg0_); |
| 1353 | |
| 1354 | /* see what we have */ |
| 1355 | switch(info) |
| 1356 | { |
| 1357 | case SYSINFO_SYSINFO: |
| 1358 | /* there are no additional arguments for this information type */ |
| 1359 | check_argc(vmg_ argc, 1); |
| 1360 | |
| 1361 | /* system information is supported in this version - return true */ |
| 1362 | retval_true(vmg0_); |
| 1363 | break; |
| 1364 | |
| 1365 | case SYSINFO_VERSION: |
| 1366 | /* there are no additional arguments for this information type */ |
| 1367 | check_argc(vmg_ argc, 1); |
| 1368 | |
| 1369 | /* return the VM version string, formatted as a string */ |
| 1370 | { |
| 1371 | char buf[30]; |
| 1372 | |
| 1373 | sprintf(buf, "%d.%d.%d", |
| 1374 | (int)((T3VM_VSN_NUMBER >> 16) & 0xffff), |
| 1375 | (int)((T3VM_VSN_NUMBER >> 8) & 0xff), |
| 1376 | (int)(T3VM_VSN_NUMBER & 0xff)); |
| 1377 | retval_str(vmg_ buf); |
| 1378 | } |
| 1379 | break; |
| 1380 | |
| 1381 | case SYSINFO_OS_NAME: |
| 1382 | /* there are no additional arguments for this information type */ |
| 1383 | check_argc(vmg_ argc, 1); |
| 1384 | |
| 1385 | /* return the OS name as a string */ |
| 1386 | retval_str(vmg_ OS_SYSTEM_NAME); |
| 1387 | break; |
| 1388 | |
| 1389 | case SYSINFO_HTML: |
| 1390 | case SYSINFO_JPEG: |
| 1391 | case SYSINFO_PNG: |
| 1392 | case SYSINFO_WAV: |
| 1393 | case SYSINFO_MIDI: |
| 1394 | case SYSINFO_WAV_MIDI_OVL: |
| 1395 | case SYSINFO_WAV_OVL: |
| 1396 | case SYSINFO_PREF_IMAGES: |
| 1397 | case SYSINFO_PREF_SOUNDS: |
| 1398 | case SYSINFO_PREF_MUSIC: |
| 1399 | case SYSINFO_PREF_LINKS: |
| 1400 | case SYSINFO_MPEG: |
| 1401 | case SYSINFO_MPEG1: |
| 1402 | case SYSINFO_MPEG2: |
| 1403 | case SYSINFO_MPEG3: |
| 1404 | case SYSINFO_LINKS_HTTP: |
| 1405 | case SYSINFO_LINKS_FTP: |
| 1406 | case SYSINFO_LINKS_NEWS: |
| 1407 | case SYSINFO_LINKS_MAILTO: |
| 1408 | case SYSINFO_LINKS_TELNET: |
| 1409 | case SYSINFO_PNG_TRANS: |
| 1410 | case SYSINFO_PNG_ALPHA: |
| 1411 | case SYSINFO_OGG: |
| 1412 | case SYSINFO_MNG: |
| 1413 | case SYSINFO_MNG_TRANS: |
| 1414 | case SYSINFO_MNG_ALPHA: |
| 1415 | case SYSINFO_TEXT_COLORS: |
| 1416 | case SYSINFO_TEXT_HILITE: |
| 1417 | case SYSINFO_BANNERS: |
| 1418 | case SYSINFO_INTERP_CLASS: |
| 1419 | /* there are no additional arguments for these information types */ |
| 1420 | check_argc(vmg_ argc, 1); |
| 1421 | |
| 1422 | /* ask the OS layer for the information */ |
| 1423 | if (os_get_sysinfo(info, 0, &result)) |
| 1424 | { |
| 1425 | /* we got a valid result - return it */ |
| 1426 | retval_int(vmg_ result); |
| 1427 | } |
| 1428 | else |
| 1429 | { |
| 1430 | /* |
| 1431 | * the information type is not known to the OS layer - |
| 1432 | * return nil to indicate that the information isn't |
| 1433 | * available |
| 1434 | */ |
| 1435 | retval_nil(vmg0_); |
| 1436 | } |
| 1437 | break; |
| 1438 | |
| 1439 | case SYSINFO_HTML_MODE: |
| 1440 | /* |
| 1441 | * This sysinfo flag is explicitly not used in TADS 3, since we're |
| 1442 | * always in HTML mode. (We make this case explicit to call |
| 1443 | * attention to the fact that it was not accidentally omitted, but |
| 1444 | * is intentionally not used.) |
| 1445 | */ |
| 1446 | /* fall through to default case */ |
| 1447 | |
| 1448 | default: |
| 1449 | /* |
| 1450 | * Other codes fail harmlessly with a nil return value. Pop all |
| 1451 | * remaining arguments and return nil. (Note that we discard |
| 1452 | * only (argc-1) arguments because we've already popped the |
| 1453 | * first argument.) |
| 1454 | */ |
| 1455 | G_stk->discard(argc - 1); |
| 1456 | retval_nil(vmg0_); |
| 1457 | break; |
| 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | /* ------------------------------------------------------------------------ */ |
| 1462 | /* |
| 1463 | * status_mode - set the status line mode |
| 1464 | */ |
| 1465 | void CVmBifTIO::status_mode(VMG_ uint argc) |
| 1466 | { |
| 1467 | int mode; |
| 1468 | |
| 1469 | /* check arguments */ |
| 1470 | check_argc(vmg_ argc, 1); |
| 1471 | |
| 1472 | /* pop the mode value */ |
| 1473 | mode = pop_int_val(vmg0_); |
| 1474 | |
| 1475 | /* set the new status mode in the console */ |
| 1476 | G_console->set_statusline_mode(vmg_ mode); |
| 1477 | } |
| 1478 | |
| 1479 | /* ------------------------------------------------------------------------ */ |
| 1480 | /* |
| 1481 | * status_right - set the string in the right half of the status line |
| 1482 | */ |
| 1483 | void CVmBifTIO::status_right(VMG_ uint argc) |
| 1484 | { |
| 1485 | char msg[256]; |
| 1486 | |
| 1487 | /* check arguments */ |
| 1488 | check_argc(vmg_ argc, 1); |
| 1489 | |
| 1490 | /* pop the status string */ |
| 1491 | pop_str_val_ui(vmg_ msg, sizeof(msg)); |
| 1492 | |
| 1493 | /* set the string */ |
| 1494 | os_strsc(msg); |
| 1495 | } |
| 1496 | |
| 1497 | /* ------------------------------------------------------------------------ */ |
| 1498 | /* |
| 1499 | * res_exists - check to see if an external resource can be loaded |
| 1500 | * through the host application |
| 1501 | */ |
| 1502 | void CVmBifTIO::res_exists(VMG_ uint argc) |
| 1503 | { |
| 1504 | const char *res_name; |
| 1505 | int result; |
| 1506 | |
| 1507 | /* check arguments */ |
| 1508 | check_argc(vmg_ argc, 1); |
| 1509 | |
| 1510 | /* pop the resource name */ |
| 1511 | res_name = pop_str_val(vmg0_); |
| 1512 | |
| 1513 | /* ask the host application if the resource can be loaded */ |
| 1514 | result = G_host_ifc->resfile_exists(res_name + VMB_LEN, osrp2(res_name)); |
| 1515 | |
| 1516 | /* return the result */ |
| 1517 | retval_bool(vmg_ result); |
| 1518 | } |
| 1519 | |
| 1520 | /* ------------------------------------------------------------------------ */ |
| 1521 | /* |
| 1522 | * set_script_file flags |
| 1523 | */ |
| 1524 | |
| 1525 | /* read in 'quiet' mode - do not dipslay output while reading the script */ |
| 1526 | #define VMBIFTADS_SCRIPT_QUIET 0x0001 |
| 1527 | |
| 1528 | /* turn off 'more' mode while reading the script */ |
| 1529 | #define VMBIFTADS_SCRIPT_NONSTOP 0x0002 |
| 1530 | |
| 1531 | /* the script is an "event" script - this is a query-only flag */ |
| 1532 | #define VMBIFTADS_SCRIPT_EVENT 0x0004 |
| 1533 | |
| 1534 | /* |
| 1535 | * set_script_file special request codes |
| 1536 | */ |
| 1537 | #define VMBIFTADS_SCRIPTREQ_GET_STATUS 0x7000 |
| 1538 | |
| 1539 | |
| 1540 | /* |
| 1541 | * set_script_file - open a command input scripting file |
| 1542 | */ |
| 1543 | void CVmBifTIO::set_script_file(VMG_ uint argc) |
| 1544 | { |
| 1545 | /* check arguments */ |
| 1546 | check_argc_range(vmg_ argc, 1, 2); |
| 1547 | |
| 1548 | /* |
| 1549 | * If the filename is nil, close the current script file. If the |
| 1550 | * "filename" is a number, it's a special request. Otherwise, the |
| 1551 | * filename must be a string giving the name of the file to open. |
| 1552 | */ |
| 1553 | if (G_stk->get(0)->typ == VM_NIL) |
| 1554 | { |
| 1555 | /* discard the nil filename */ |
| 1556 | G_stk->discard(); |
| 1557 | |
| 1558 | /* pop the flags if present - they're superfluous in this case */ |
| 1559 | if (argc >= 2) |
| 1560 | G_stk->discard(); |
| 1561 | |
| 1562 | /* close the script file */ |
| 1563 | close_script_file(vmg0_); |
| 1564 | |
| 1565 | /* this form always succeeds */ |
| 1566 | retval_true(vmg0_); |
| 1567 | } |
| 1568 | else if (G_stk->get(0)->typ == VM_INT) |
| 1569 | { |
| 1570 | /* get the request code */ |
| 1571 | int flags = pop_int_val(vmg0_); |
| 1572 | |
| 1573 | /* any additional argument is superfluous in this case */ |
| 1574 | if (argc >= 2) |
| 1575 | G_stk->discard(); |
| 1576 | |
| 1577 | /* check the request */ |
| 1578 | switch (flags) |
| 1579 | { |
| 1580 | case VMBIFTADS_SCRIPTREQ_GET_STATUS: |
| 1581 | /* get the current script reading status */ |
| 1582 | if (G_console->is_reading_script()) |
| 1583 | { |
| 1584 | /* a script is in progress - return the script flags */ |
| 1585 | retval_int(vmg_ |
| 1586 | (G_console->is_quiet_script() |
| 1587 | ? VMBIFTADS_SCRIPT_QUIET : 0) |
| 1588 | | (G_console->is_moremode_script() |
| 1589 | ? 0 : VMBIFTADS_SCRIPT_NONSTOP) |
| 1590 | | (G_console->is_event_script() |
| 1591 | ? VMBIFTADS_SCRIPT_EVENT : 0)); |
| 1592 | } |
| 1593 | else |
| 1594 | { |
| 1595 | /* not reading a script - return nil */ |
| 1596 | retval_nil(vmg0_); |
| 1597 | } |
| 1598 | break; |
| 1599 | |
| 1600 | default: |
| 1601 | /* other flags are invalid */ |
| 1602 | err_throw(VMERR_BAD_VAL_BIF); |
| 1603 | } |
| 1604 | } |
| 1605 | else |
| 1606 | { |
| 1607 | /* get the filename argument */ |
| 1608 | const vm_val_t *filespec = G_stk->get(0); |
| 1609 | |
| 1610 | /* if they provided flags, get the flags */ |
| 1611 | int flags = (argc >= 2 ? G_stk->get(1)->num_to_int(vmg0_) : 0); |
| 1612 | |
| 1613 | /* set up our recursive call descriptor */ |
| 1614 | vm_rcdesc rc(vmg_ "setScriptFile", bif_table, 15, |
| 1615 | G_stk->get(0), argc); |
| 1616 | |
| 1617 | /* open the script file */ |
| 1618 | int ok = !G_console->open_script_file( |
| 1619 | vmg_ filespec, &rc, |
| 1620 | (flags & VMBIFTADS_SCRIPT_QUIET) != 0, |
| 1621 | !(flags & VMBIFTADS_SCRIPT_NONSTOP)); |
| 1622 | |
| 1623 | /* discard arguments */ |
| 1624 | G_stk->discard(argc); |
| 1625 | |
| 1626 | /* return true on success, nil on failure */ |
| 1627 | retval_bool(vmg_ ok); |
| 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | /* |
| 1632 | * close the script file |
| 1633 | */ |
| 1634 | void CVmBifTIO::close_script_file(VMG0_) |
| 1635 | { |
| 1636 | int old_more_mode; |
| 1637 | |
| 1638 | /* close the script */ |
| 1639 | old_more_mode = G_console->close_script_file(vmg0_); |
| 1640 | |
| 1641 | /* restore the MORE mode in effect when the script was opened */ |
| 1642 | G_console->set_more_state(old_more_mode); |
| 1643 | } |
| 1644 | |
| 1645 | /* ------------------------------------------------------------------------ */ |
| 1646 | /* |
| 1647 | * get_charset selectors |
| 1648 | */ |
| 1649 | |
| 1650 | /* display character set */ |
| 1651 | #define VMBIFTADS_CHARSET_DISPLAY 0x0001 |
| 1652 | |
| 1653 | /* file system character set for filenames */ |
| 1654 | #define VMBIFTADS_CHARSET_FILENAME 0x0002 |
| 1655 | |
| 1656 | /* typical character set for text file contents */ |
| 1657 | #define VMBIFTADS_CHARSET_FILECONTENTS 0x0003 |
| 1658 | |
| 1659 | /* |
| 1660 | * get_charset - get a local character set name |
| 1661 | */ |
| 1662 | void CVmBifTIO::get_charset(VMG_ uint argc) |
| 1663 | { |
| 1664 | char csname[32]; |
| 1665 | int which; |
| 1666 | |
| 1667 | /* check arguments */ |
| 1668 | check_argc(vmg_ argc, 1); |
| 1669 | |
| 1670 | /* get the character set selector */ |
| 1671 | which = pop_int_val(vmg0_); |
| 1672 | |
| 1673 | /* map the selector to the appropriate value */ |
| 1674 | switch(which) |
| 1675 | { |
| 1676 | case VMBIFTADS_CHARSET_DISPLAY: |
| 1677 | /* |
| 1678 | * if there was an explicit character set parameter specified at |
| 1679 | * start-up time, use that |
| 1680 | */ |
| 1681 | if (G_disp_cset_name != 0) |
| 1682 | { |
| 1683 | /* there's an explicit parameter - return it */ |
| 1684 | retval_str(vmg_ G_disp_cset_name); |
| 1685 | |
| 1686 | /* we're done */ |
| 1687 | return; |
| 1688 | } |
| 1689 | |
| 1690 | /* no explicit setting - use the OS default character set */ |
| 1691 | which = OS_CHARMAP_DISPLAY; |
| 1692 | break; |
| 1693 | |
| 1694 | case VMBIFTADS_CHARSET_FILENAME: |
| 1695 | which = OS_CHARMAP_FILENAME; |
| 1696 | break; |
| 1697 | |
| 1698 | case VMBIFTADS_CHARSET_FILECONTENTS: |
| 1699 | which = OS_CHARMAP_FILECONTENTS; |
| 1700 | break; |
| 1701 | |
| 1702 | default: |
| 1703 | /* others are unrecognized; simply return nil for these */ |
| 1704 | retval_nil(vmg0_); |
| 1705 | return; |
| 1706 | } |
| 1707 | |
| 1708 | /* get the character set */ |
| 1709 | os_get_charmap(csname, which); |
| 1710 | |
| 1711 | /* return a string with the name */ |
| 1712 | retval_str(vmg_ csname); |
| 1713 | } |
| 1714 | |
| 1715 | /* ------------------------------------------------------------------------ */ |
| 1716 | /* |
| 1717 | * flush_output - flush the display output |
| 1718 | */ |
| 1719 | void CVmBifTIO::flush_output(VMG_ uint argc) |
| 1720 | { |
| 1721 | /* we take no arguments */ |
| 1722 | check_argc(vmg_ argc, 0); |
| 1723 | |
| 1724 | /* flush output */ |
| 1725 | G_console->flush(vmg_ VM_NL_NONE); |
| 1726 | |
| 1727 | /* immediately update the display */ |
| 1728 | G_console->update_display(vmg0_); |
| 1729 | } |
| 1730 | |
| 1731 | /* ------------------------------------------------------------------------ */ |
| 1732 | /* |
| 1733 | * input_timeout - get a line of input from the keyboard, with an optional |
| 1734 | * timeout |
| 1735 | */ |
| 1736 | void CVmBifTIO::input_timeout(VMG_ uint argc) |
| 1737 | { |
| 1738 | char buf[256]; |
| 1739 | long timeout; |
| 1740 | int use_timeout; |
| 1741 | int evt; |
| 1742 | int ele_count; |
| 1743 | vm_obj_id_t lst_obj; |
| 1744 | CVmObjList *lst; |
| 1745 | vm_val_t val; |
| 1746 | |
| 1747 | /* check arguments */ |
| 1748 | check_argc_range(vmg_ argc, 0, 1); |
| 1749 | |
| 1750 | /* if there's a timeout argument, retrieve it */ |
| 1751 | if (argc == 0) |
| 1752 | { |
| 1753 | /* no arguments - there's no timeout */ |
| 1754 | use_timeout = FALSE; |
| 1755 | timeout = 0; |
| 1756 | } |
| 1757 | else if (G_stk->get(0)->typ == VM_NIL) |
| 1758 | { |
| 1759 | /* |
| 1760 | * there's a timeout argument, but it's nil, so this means there's |
| 1761 | * no timeout |
| 1762 | */ |
| 1763 | use_timeout = FALSE; |
| 1764 | timeout = 0; |
| 1765 | |
| 1766 | /* discard the argument */ |
| 1767 | G_stk->discard(); |
| 1768 | } |
| 1769 | else |
| 1770 | { |
| 1771 | /* we have a timeout specified */ |
| 1772 | use_timeout = TRUE; |
| 1773 | timeout = pop_long_val(vmg0_); |
| 1774 | } |
| 1775 | |
| 1776 | /* read a line of text from the keyboard */ |
| 1777 | evt = G_console->read_line_timeout(vmg_ buf, sizeof(buf), |
| 1778 | timeout, use_timeout); |
| 1779 | |
| 1780 | /* figure out how big a list we'll return */ |
| 1781 | switch(evt) |
| 1782 | { |
| 1783 | case OS_EVT_LINE: |
| 1784 | case OS_EVT_TIMEOUT: |
| 1785 | /* two elements - the event type, and the line of text */ |
| 1786 | ele_count = 2; |
| 1787 | break; |
| 1788 | |
| 1789 | default: |
| 1790 | /* for anything else, we need only the event type code */ |
| 1791 | ele_count = 1; |
| 1792 | break; |
| 1793 | } |
| 1794 | |
| 1795 | /* create the return list */ |
| 1796 | lst_obj = CVmObjList::create(vmg_ FALSE, ele_count); |
| 1797 | lst = (CVmObjList *)vm_objp(vmg_ lst_obj); |
| 1798 | lst->cons_clear(); |
| 1799 | |
| 1800 | /* save the list on the stack to protect against garbage collection */ |
| 1801 | val.set_obj(lst_obj); |
| 1802 | G_stk->push(&val); |
| 1803 | |
| 1804 | /* fill in the first element with the event type code */ |
| 1805 | val.set_int(evt); |
| 1806 | lst->cons_set_element(0, &val); |
| 1807 | |
| 1808 | /* set additional elements, according to the event type */ |
| 1809 | switch(evt) |
| 1810 | { |
| 1811 | case OS_EVT_LINE: |
| 1812 | case OS_EVT_TIMEOUT: |
| 1813 | /* the second element is the line of text we read */ |
| 1814 | val.set_obj(CVmObjString::create(vmg_ FALSE, buf, strlen(buf))); |
| 1815 | lst->cons_set_element(1, &val); |
| 1816 | break; |
| 1817 | |
| 1818 | default: |
| 1819 | /* other event types have no extra data */ |
| 1820 | break; |
| 1821 | } |
| 1822 | |
| 1823 | /* return the list */ |
| 1824 | retval_obj(vmg_ lst_obj); |
| 1825 | |
| 1826 | /* we can drop the garbage collection protection now */ |
| 1827 | G_stk->discard(); |
| 1828 | } |
| 1829 | |
| 1830 | /* ------------------------------------------------------------------------ */ |
| 1831 | /* |
| 1832 | * input_cancel - cancel input previously interrupted by timeout |
| 1833 | */ |
| 1834 | void CVmBifTIO::input_cancel(VMG_ uint argc) |
| 1835 | { |
| 1836 | vm_val_t val; |
| 1837 | int reset; |
| 1838 | |
| 1839 | /* check arguments */ |
| 1840 | check_argc(vmg_ argc, 1); |
| 1841 | |
| 1842 | /* get the 'reset' flag */ |
| 1843 | G_stk->pop(&val); |
| 1844 | reset = (val.typ == VM_TRUE); |
| 1845 | |
| 1846 | /* cancel the line */ |
| 1847 | G_console->read_line_cancel(vmg_ reset); |
| 1848 | } |
| 1849 | |
| 1850 | /* ------------------------------------------------------------------------ */ |
| 1851 | /* |
| 1852 | * Banner Window Functions |
| 1853 | */ |
| 1854 | |
| 1855 | /* |
| 1856 | * create a banner |
| 1857 | */ |
| 1858 | void CVmBifTIO::banner_create(VMG_ uint argc) |
| 1859 | { |
| 1860 | int parent_id; |
| 1861 | int other_id; |
| 1862 | int where; |
| 1863 | int wintype; |
| 1864 | int align; |
| 1865 | int siz; |
| 1866 | int siz_units; |
| 1867 | unsigned long style; |
| 1868 | int hdl; |
| 1869 | |
| 1870 | /* check arguments */ |
| 1871 | check_argc_range(vmg_ argc, 7, 8); |
| 1872 | |
| 1873 | /* retrieve the 'parent' parameter */ |
| 1874 | if (argc == 7) |
| 1875 | { |
| 1876 | /* |
| 1877 | * there's no parent argument - this is an obsolete format for the |
| 1878 | * arguments, but accept it for now, and simply treat it as |
| 1879 | * equivalent to a nil parent |
| 1880 | */ |
| 1881 | parent_id = 0; |
| 1882 | } |
| 1883 | else if (G_stk->get(0)->typ == VM_NIL) |
| 1884 | { |
| 1885 | /* parent is nil - use zero as the ID and discard the nil */ |
| 1886 | parent_id = 0; |
| 1887 | G_stk->discard(); |
| 1888 | } |
| 1889 | else |
| 1890 | { |
| 1891 | /* retrieve the parent ID */ |
| 1892 | parent_id = pop_int_val(vmg0_); |
| 1893 | } |
| 1894 | |
| 1895 | /* retrieve the 'where' parameter */ |
| 1896 | where = pop_int_val(vmg0_); |
| 1897 | |
| 1898 | /* retrieve the 'other' parameter, if it's needed for the 'where' */ |
| 1899 | switch(where) |
| 1900 | { |
| 1901 | case OS_BANNER_BEFORE: |
| 1902 | case OS_BANNER_AFTER: |
| 1903 | /* we need another banner ID for the relative insertion point */ |
| 1904 | other_id = pop_int_val(vmg0_); |
| 1905 | break; |
| 1906 | |
| 1907 | default: |
| 1908 | /* we don't need 'other' for this insertion point */ |
| 1909 | other_id = 0; |
| 1910 | G_stk->discard(); |
| 1911 | break; |
| 1912 | } |
| 1913 | |
| 1914 | /* retrieve the window type argument */ |
| 1915 | wintype = pop_int_val(vmg0_); |
| 1916 | |
| 1917 | /* retrieve the alignment argument */ |
| 1918 | align = pop_int_val(vmg0_); |
| 1919 | |
| 1920 | /* retrieve the size (as a percentage of the full screen size) */ |
| 1921 | if (G_stk->get(0)->typ == VM_NIL) |
| 1922 | { |
| 1923 | /* nil size - use zero for the size */ |
| 1924 | siz = 0; |
| 1925 | siz_units = OS_BANNER_SIZE_ABS; |
| 1926 | |
| 1927 | /* discard the size and size units */ |
| 1928 | G_stk->discard(); |
| 1929 | G_stk->discard(); |
| 1930 | } |
| 1931 | else |
| 1932 | { |
| 1933 | /* retrieve the size and size units as integer values */ |
| 1934 | siz = pop_int_val(vmg0_); |
| 1935 | siz_units = pop_int_val(vmg0_); |
| 1936 | } |
| 1937 | |
| 1938 | /* retrieve the flags */ |
| 1939 | style = pop_long_val(vmg0_); |
| 1940 | |
| 1941 | if (G_net_config != 0) |
| 1942 | { |
| 1943 | /* there's no console in web host mode - return failure */ |
| 1944 | retval_nil(vmg0_); |
| 1945 | return; |
| 1946 | } |
| 1947 | |
| 1948 | /* try creating the banner */ |
| 1949 | hdl = G_console->get_banner_manager()->create_banner( |
| 1950 | vmg_ parent_id, where, other_id, wintype, |
| 1951 | align, siz, siz_units, style); |
| 1952 | |
| 1953 | /* |
| 1954 | * If we succeeded, return the handle; otherwise, return nil. A banner |
| 1955 | * handle of zero indicates failure. |
| 1956 | */ |
| 1957 | if (hdl != 0) |
| 1958 | retval_int(vmg_ hdl); |
| 1959 | else |
| 1960 | retval_nil(vmg0_); |
| 1961 | } |
| 1962 | |
| 1963 | /* |
| 1964 | * delete a banner |
| 1965 | */ |
| 1966 | void CVmBifTIO::banner_delete(VMG_ uint argc) |
| 1967 | { |
| 1968 | /* check arguments */ |
| 1969 | check_argc(vmg_ argc, 1); |
| 1970 | |
| 1971 | /* delete the banner */ |
| 1972 | G_console->get_banner_manager()->delete_banner(vmg_ pop_int_val(vmg0_)); |
| 1973 | } |
| 1974 | |
| 1975 | /* |
| 1976 | * clear a window |
| 1977 | */ |
| 1978 | void CVmBifTIO::banner_clear(VMG_ uint argc) |
| 1979 | { |
| 1980 | int id; |
| 1981 | CVmConsole *console; |
| 1982 | |
| 1983 | /* check arguments */ |
| 1984 | check_argc(vmg_ argc, 1); |
| 1985 | |
| 1986 | /* get the banner ID */ |
| 1987 | id = pop_int_val(vmg0_); |
| 1988 | |
| 1989 | /* get the banner - if it's invalid, throw an error */ |
| 1990 | console = G_console->get_banner_manager()->get_console(id); |
| 1991 | if (console == 0) |
| 1992 | err_throw(VMERR_BAD_VAL_BIF); |
| 1993 | |
| 1994 | /* tell the console that we're clearing it */ |
| 1995 | console->clear_window(vmg0_); |
| 1996 | } |
| 1997 | |
| 1998 | /* |
| 1999 | * write values to a banner |
| 2000 | */ |
| 2001 | void CVmBifTIO::banner_say(VMG_ uint argc) |
| 2002 | { |
| 2003 | CVmConsole *console; |
| 2004 | |
| 2005 | /* check arguments */ |
| 2006 | check_argc_range(vmg_ argc, 1, 32767); |
| 2007 | |
| 2008 | /* get the banner - if it's invalid, throw an error */ |
| 2009 | console = |
| 2010 | G_console->get_banner_manager()->get_console(pop_int_val(vmg0_)); |
| 2011 | if (console == 0) |
| 2012 | err_throw(VMERR_BAD_VAL_BIF); |
| 2013 | |
| 2014 | /* |
| 2015 | * write the argument(s) to the console (note that the first argument, |
| 2016 | * which we've already retrieved, is the console handle, so don't count |
| 2017 | * it among the arguments to display) |
| 2018 | */ |
| 2019 | say_to_console(vmg_ console, argc - 1); |
| 2020 | } |
| 2021 | |
| 2022 | /* |
| 2023 | * flush text to a banner |
| 2024 | */ |
| 2025 | void CVmBifTIO::banner_flush(VMG_ uint argc) |
| 2026 | { |
| 2027 | CVmConsole *console; |
| 2028 | |
| 2029 | /* check arguments */ |
| 2030 | check_argc(vmg_ argc, 1); |
| 2031 | |
| 2032 | /* get the banner - if it's invalid, throw an error */ |
| 2033 | console = |
| 2034 | G_console->get_banner_manager()->get_console(pop_int_val(vmg0_)); |
| 2035 | if (console == 0) |
| 2036 | err_throw(VMERR_BAD_VAL_BIF); |
| 2037 | |
| 2038 | /* flush the console */ |
| 2039 | console->flush(vmg_ VM_NL_NONE); |
| 2040 | |
| 2041 | /* immediately update the display */ |
| 2042 | console->update_display(vmg0_); |
| 2043 | } |
| 2044 | |
| 2045 | /* |
| 2046 | * set the banner size |
| 2047 | */ |
| 2048 | void CVmBifTIO::banner_set_size(VMG_ uint argc) |
| 2049 | { |
| 2050 | int id; |
| 2051 | void *hdl; |
| 2052 | int siz; |
| 2053 | int siz_units; |
| 2054 | int is_advisory; |
| 2055 | |
| 2056 | /* check arguments */ |
| 2057 | check_argc(vmg_ argc, 4); |
| 2058 | |
| 2059 | /* get the banner ID */ |
| 2060 | id = pop_int_val(vmg0_); |
| 2061 | |
| 2062 | /* get the size and size units */ |
| 2063 | siz = pop_int_val(vmg0_); |
| 2064 | siz_units = pop_int_val(vmg0_); |
| 2065 | |
| 2066 | /* get the is-advisory flag */ |
| 2067 | is_advisory = pop_bool_val(vmg0_); |
| 2068 | |
| 2069 | /* get the banner - if it's invalid, throw an error */ |
| 2070 | hdl = G_console->get_banner_manager()->get_os_handle(id); |
| 2071 | if (hdl == 0) |
| 2072 | err_throw(VMERR_BAD_VAL_BIF); |
| 2073 | |
| 2074 | /* set the size */ |
| 2075 | os_banner_set_size(hdl, siz, siz_units, is_advisory); |
| 2076 | } |
| 2077 | |
| 2078 | /* |
| 2079 | * size a banner to its contents in one or both dimensions |
| 2080 | */ |
| 2081 | void CVmBifTIO::banner_size_to_contents(VMG_ uint argc) |
| 2082 | { |
| 2083 | int id; |
| 2084 | void *hdl; |
| 2085 | CVmConsole *console; |
| 2086 | |
| 2087 | /* check arguments */ |
| 2088 | check_argc(vmg_ argc, 1); |
| 2089 | |
| 2090 | /* get the banner ID */ |
| 2091 | id = pop_int_val(vmg0_); |
| 2092 | |
| 2093 | /* get the banner - if it's invalid, throw an error */ |
| 2094 | hdl = G_console->get_banner_manager()->get_os_handle(id); |
| 2095 | console = G_console->get_banner_manager()->get_console(id); |
| 2096 | if (hdl == 0 || console == 0) |
| 2097 | err_throw(VMERR_BAD_VAL_BIF); |
| 2098 | |
| 2099 | /* make sure we've flushed any pending output to the banner */ |
| 2100 | console->flush(vmg_ VM_NL_NONE); |
| 2101 | |
| 2102 | /* set the size */ |
| 2103 | os_banner_size_to_contents(hdl); |
| 2104 | } |
| 2105 | |
| 2106 | /* |
| 2107 | * move the output position in a text grid banner |
| 2108 | */ |
| 2109 | void CVmBifTIO::banner_goto(VMG_ uint argc) |
| 2110 | { |
| 2111 | int id; |
| 2112 | void *hdl; |
| 2113 | CVmConsole *console; |
| 2114 | int row, col; |
| 2115 | |
| 2116 | /* check arguments */ |
| 2117 | check_argc(vmg_ argc, 3); |
| 2118 | |
| 2119 | /* get the banner ID */ |
| 2120 | id = pop_int_val(vmg0_); |
| 2121 | |
| 2122 | /* get the coordinates */ |
| 2123 | row = pop_int_val(vmg0_); |
| 2124 | col = pop_int_val(vmg0_); |
| 2125 | |
| 2126 | /* make sure the values are valid */ |
| 2127 | if (row < 1 || col < 1) |
| 2128 | err_throw(VMERR_BAD_VAL_BIF); |
| 2129 | |
| 2130 | /* get the banner - if it's invalid, throw an error */ |
| 2131 | hdl = G_console->get_banner_manager()->get_os_handle(id); |
| 2132 | console = G_console->get_banner_manager()->get_console(id); |
| 2133 | if (hdl == 0 || console == 0) |
| 2134 | err_throw(VMERR_BAD_VAL_BIF); |
| 2135 | |
| 2136 | /* |
| 2137 | * make sure we've flushed and discarded any pending output (since we |
| 2138 | * don't want any pending output to show up at the new cursor position) |
| 2139 | */ |
| 2140 | console->flush(vmg_ VM_NL_NONE); |
| 2141 | console->empty_buffers(vmg0_); |
| 2142 | |
| 2143 | /* move the cursor, adjusting from 1-based to 0-based coordinates */ |
| 2144 | os_banner_goto(hdl, row - 1, col - 1); |
| 2145 | } |
| 2146 | |
| 2147 | /* |
| 2148 | * set the text color in a banner |
| 2149 | */ |
| 2150 | void CVmBifTIO::banner_set_text_color(VMG_ uint argc) |
| 2151 | { |
| 2152 | int id; |
| 2153 | void *hdl; |
| 2154 | CVmConsole *console; |
| 2155 | os_color_t fg, bg; |
| 2156 | |
| 2157 | /* check arguments */ |
| 2158 | check_argc(vmg_ argc, 3); |
| 2159 | |
| 2160 | /* get the banner ID */ |
| 2161 | id = pop_int_val(vmg0_); |
| 2162 | |
| 2163 | /* get the foreground and background color values */ |
| 2164 | fg = (os_color_t)pop_long_val(vmg0_); |
| 2165 | bg = (os_color_t)pop_long_val(vmg0_); |
| 2166 | |
| 2167 | /* get the banner - if it's invalid, throw an error */ |
| 2168 | hdl = G_console->get_banner_manager()->get_os_handle(id); |
| 2169 | console = G_console->get_banner_manager()->get_console(id); |
| 2170 | if (hdl == 0 || console == 0) |
| 2171 | err_throw(VMERR_BAD_VAL_BIF); |
| 2172 | |
| 2173 | /* set the text output color in the console's formatter */ |
| 2174 | console->set_text_color(vmg_ fg, bg); |
| 2175 | } |
| 2176 | |
| 2177 | /* |
| 2178 | * set the screen color in a banner |
| 2179 | */ |
| 2180 | void CVmBifTIO::banner_set_screen_color(VMG_ uint argc) |
| 2181 | { |
| 2182 | int id; |
| 2183 | void *hdl; |
| 2184 | CVmConsole *console; |
| 2185 | os_color_t color; |
| 2186 | |
| 2187 | /* check arguments */ |
| 2188 | check_argc(vmg_ argc, 2); |
| 2189 | |
| 2190 | /* get the banner ID */ |
| 2191 | id = pop_int_val(vmg0_); |
| 2192 | |
| 2193 | /* get the body color */ |
| 2194 | color = (os_color_t)pop_long_val(vmg0_); |
| 2195 | |
| 2196 | /* get the banner - if it's invalid, throw an error */ |
| 2197 | hdl = G_console->get_banner_manager()->get_os_handle(id); |
| 2198 | console = G_console->get_banner_manager()->get_console(id); |
| 2199 | if (hdl == 0 || console == 0) |
| 2200 | err_throw(VMERR_BAD_VAL_BIF); |
| 2201 | |
| 2202 | /* set the body color in the console */ |
| 2203 | console->set_body_color(vmg_ color); |
| 2204 | } |
| 2205 | |
| 2206 | /* service routine: store an integer in a list under construction */ |
| 2207 | static void set_list_int(CVmObjList *lst, size_t idx, long intval) |
| 2208 | { |
| 2209 | vm_val_t val; |
| 2210 | |
| 2211 | /* set the value */ |
| 2212 | val.set_int(intval); |
| 2213 | |
| 2214 | /* store it in the list */ |
| 2215 | lst->cons_set_element(idx, &val); |
| 2216 | } |
| 2217 | |
| 2218 | /* |
| 2219 | * get information on a banner |
| 2220 | */ |
| 2221 | void CVmBifTIO::banner_get_info(VMG_ uint argc) |
| 2222 | { |
| 2223 | int id; |
| 2224 | void *hdl; |
| 2225 | os_banner_info_t info; |
| 2226 | CVmConsoleBanner *console; |
| 2227 | |
| 2228 | /* check arguments */ |
| 2229 | check_argc(vmg_ argc, 1); |
| 2230 | |
| 2231 | /* get the banner ID */ |
| 2232 | id = pop_int_val(vmg0_); |
| 2233 | |
| 2234 | /* get the banner - if it's invalid, throw an error */ |
| 2235 | hdl = G_console->get_banner_manager()->get_os_handle(id); |
| 2236 | console = G_console->get_banner_manager()->get_console(id); |
| 2237 | if (hdl == 0 || console == 0) |
| 2238 | err_throw(VMERR_BAD_VAL_BIF); |
| 2239 | |
| 2240 | /* get information on the banner */ |
| 2241 | if (console->get_banner_info(&info)) |
| 2242 | { |
| 2243 | vm_obj_id_t lst_obj; |
| 2244 | CVmObjList *lst; |
| 2245 | vm_val_t val; |
| 2246 | |
| 2247 | /* set up a return list with space for six entries */ |
| 2248 | lst_obj = CVmObjList::create(vmg_ FALSE, 6); |
| 2249 | lst = (CVmObjList *)vm_objp(vmg_ lst_obj); |
| 2250 | |
| 2251 | /* save the list on the stack to protect against garbage collection */ |
| 2252 | val.set_obj(lst_obj); |
| 2253 | G_stk->push(&val); |
| 2254 | |
| 2255 | /* |
| 2256 | * return the values: [align, style, rows, columns, pix_height, |
| 2257 | * pix_width] |
| 2258 | */ |
| 2259 | set_list_int(lst, 0, info.align); |
| 2260 | set_list_int(lst, 1, info.style); |
| 2261 | set_list_int(lst, 2, info.rows); |
| 2262 | set_list_int(lst, 3, info.columns); |
| 2263 | set_list_int(lst, 4, info.pix_height); |
| 2264 | set_list_int(lst, 5, info.pix_width); |
| 2265 | |
| 2266 | /* return the list */ |
| 2267 | retval_obj(vmg_ lst_obj); |
| 2268 | |
| 2269 | /* discard our gc protection */ |
| 2270 | G_stk->discard(); |
| 2271 | } |
| 2272 | else |
| 2273 | { |
| 2274 | /* no information available - return nil */ |
| 2275 | retval_nil(vmg0_); |
| 2276 | } |
| 2277 | } |
| 2278 | |
| 2279 | /* ------------------------------------------------------------------------ */ |
| 2280 | /* |
| 2281 | * Log Console Functions |
| 2282 | */ |
| 2283 | |
| 2284 | /* |
| 2285 | * create a log console |
| 2286 | */ |
| 2287 | void CVmBifTIO::log_console_create(VMG_ uint argc) |
| 2288 | { |
| 2289 | /* check arguments */ |
| 2290 | check_argc(vmg_ argc, 3); |
| 2291 | |
| 2292 | /* get the arguments: log file name, character set, and width */ |
| 2293 | const vm_val_t *filespec = G_stk->get(0); |
| 2294 | const vm_val_t *charset = G_stk->get(1); |
| 2295 | int width = G_stk->get(2)->num_to_int(vmg0_); |
| 2296 | |
| 2297 | /* |
| 2298 | * Retrieve the character mapper, which can be given as either a |
| 2299 | * CharacterSet object or a string giving the character set name. |
| 2300 | */ |
| 2301 | CCharmapToLocal *cmap = 0; |
| 2302 | if (charset->typ == VM_NIL) |
| 2303 | { |
| 2304 | /* nil - use the default log file character set */ |
| 2305 | cmap = G_cmap_to_log; |
| 2306 | cmap->add_ref(); |
| 2307 | } |
| 2308 | else if (charset->typ == VM_OBJ |
| 2309 | && CVmObjCharSet::is_charset(vmg_ charset->val.obj)) |
| 2310 | { |
| 2311 | /* it's a CharacterSet object - pop the reference */ |
| 2312 | vm_obj_id_t obj = charset->val.obj; |
| 2313 | |
| 2314 | /* retrieve the character mapper from the character set */ |
| 2315 | cmap = ((CVmObjCharSet *)vm_objp(vmg_ obj))->get_to_local(vmg0_); |
| 2316 | cmap->add_ref(); |
| 2317 | } |
| 2318 | else |
| 2319 | { |
| 2320 | /* it's not a CharacterSet, so it must be a character set name */ |
| 2321 | const char *str = charset->get_as_string(vmg0_); |
| 2322 | if (str == 0) |
| 2323 | err_throw(VMERR_BAD_TYPE_BIF); |
| 2324 | |
| 2325 | /* get the length and skip the length prefix */ |
| 2326 | size_t len = vmb_get_len(str); |
| 2327 | str += VMB_LEN; |
| 2328 | |
| 2329 | /* get a null-terminated version of the name */ |
| 2330 | char *nm = lib_copy_str(str, len); |
| 2331 | |
| 2332 | /* |
| 2333 | * Create a character mapping for the given name. Note that this |
| 2334 | * will automatically add a reference to the mapper on our behalf, |
| 2335 | * so we don't have to add our own extra reference. |
| 2336 | */ |
| 2337 | cmap = CCharmapToLocal::load(G_host_ifc->get_sys_res_loader(), nm); |
| 2338 | |
| 2339 | /* done with the null-terminated version of the name string */ |
| 2340 | lib_free_str(nm); |
| 2341 | } |
| 2342 | |
| 2343 | /* if we didn't get a character map, use us-ascii by default */ |
| 2344 | if (cmap == 0) |
| 2345 | cmap = CCharmapToLocal::load(G_host_ifc->get_sys_res_loader(), |
| 2346 | "us-ascii"); |
| 2347 | |
| 2348 | /* open the file and start logging */ |
| 2349 | osfildef *fp = 0; |
| 2350 | CVmNetFile *nf = 0; |
| 2351 | int hdl = 0; |
| 2352 | err_try |
| 2353 | { |
| 2354 | /* create the network file descriptor */ |
| 2355 | vm_rcdesc rc(vmg_ "logConsoleCreate", bif_table, 30, |
| 2356 | G_stk->get(0), argc); |
| 2357 | nf = CVmNetFile::open(vmg_ filespec, &rc, |
| 2358 | NETF_NEW, OSFTLOG, "text/plain"); |
| 2359 | |
| 2360 | /* make sure the file safety level allows the operation */ |
| 2361 | CVmObjFile::check_safety_for_open(vmg_ nf, VMOBJFILE_ACCESS_WRITE); |
| 2362 | |
| 2363 | /* open the file for writing (in text mode) */ |
| 2364 | fp = osfopwt(nf->lclfname, OSFTLOG); |
| 2365 | |
| 2366 | /* if that failed, we can't contine */ |
| 2367 | if (fp == 0) |
| 2368 | G_interpreter->throw_new_class(vmg_ G_predef->file_creation_exc, |
| 2369 | 0, "error creating log file"); |
| 2370 | |
| 2371 | /* create the log console */ |
| 2372 | hdl = G_console->get_log_console_manager()->create_log_console( |
| 2373 | vmg_ nf, fp, cmap, width); |
| 2374 | |
| 2375 | /* we've handed off the file information to the console */ |
| 2376 | nf = 0; |
| 2377 | fp = 0; |
| 2378 | } |
| 2379 | err_finally |
| 2380 | { |
| 2381 | /* |
| 2382 | * release our character map reference - if we succeeded in |
| 2383 | * creating the log console, it will have added its own reference |
| 2384 | * by now |
| 2385 | */ |
| 2386 | cmap->release_ref(); |
| 2387 | |
| 2388 | /* close the file if we opened it and didn't hand it off */ |
| 2389 | if (fp != 0) |
| 2390 | osfcls(fp); |
| 2391 | |
| 2392 | /* if we have a network file object, abandon it */ |
| 2393 | if (nf != 0) |
| 2394 | nf->abandon(vmg0_); |
| 2395 | } |
| 2396 | err_end; |
| 2397 | |
| 2398 | /* discard arguments */ |
| 2399 | G_stk->discard(argc); |
| 2400 | |
| 2401 | /* |
| 2402 | * If we succeeded, return the handle; otherwise, return nil. A handle |
| 2403 | * of zero indicates failure. |
| 2404 | */ |
| 2405 | if (hdl != 0) |
| 2406 | retval_int(vmg_ hdl); |
| 2407 | else |
| 2408 | retval_nil(vmg0_); |
| 2409 | } |
| 2410 | |
| 2411 | /* |
| 2412 | * close (delete) a log console |
| 2413 | */ |
| 2414 | void CVmBifTIO::log_console_close(VMG_ uint argc) |
| 2415 | { |
| 2416 | int handle; |
| 2417 | CVmConsole *console; |
| 2418 | |
| 2419 | /* check arguments */ |
| 2420 | check_argc(vmg_ argc, 1); |
| 2421 | |
| 2422 | /* get the handle */ |
| 2423 | handle = pop_int_val(vmg0_); |
| 2424 | |
| 2425 | /* get the console based on the handle */ |
| 2426 | console = G_console->get_log_console_manager()->get_console(handle); |
| 2427 | if (console == 0) |
| 2428 | err_throw(VMERR_BAD_VAL_BIF); |
| 2429 | |
| 2430 | /* flush the console */ |
| 2431 | console->flush(vmg_ VM_NL_NONE); |
| 2432 | |
| 2433 | /* delete the console */ |
| 2434 | G_console->get_log_console_manager()->delete_log_console(vmg_ handle); |
| 2435 | |
| 2436 | /* no return value */ |
| 2437 | retval_nil(vmg0_); |
| 2438 | } |
| 2439 | |
| 2440 | /* |
| 2441 | * write values to a log console |
| 2442 | */ |
| 2443 | void CVmBifTIO::log_console_say(VMG_ uint argc) |
| 2444 | { |
| 2445 | int hdl; |
| 2446 | CVmConsole *console; |
| 2447 | |
| 2448 | /* check arguments */ |
| 2449 | check_argc_range(vmg_ argc, 1, 32767); |
| 2450 | |
| 2451 | /* get the console handle */ |
| 2452 | hdl = pop_int_val(vmg0_); |
| 2453 | |
| 2454 | /* |
| 2455 | * if it's the special value -1, it means that we want to write to the |
| 2456 | * main console's log file; otherwise, it's a log console that we |
| 2457 | * previously created explicitly via log_console_create() |
| 2458 | */ |
| 2459 | if (hdl == -1) |
| 2460 | { |
| 2461 | /* use the main log */ |
| 2462 | console = new CVmConsoleMainLog(); |
| 2463 | } |
| 2464 | else |
| 2465 | { |
| 2466 | /* get the console by handle - if it's invalid, throw an error */ |
| 2467 | console = G_console->get_log_console_manager()->get_console(hdl); |
| 2468 | if (console == 0) |
| 2469 | err_throw(VMERR_BAD_VAL_BIF); |
| 2470 | } |
| 2471 | |
| 2472 | /* |
| 2473 | * write the argument(s) to the console (note that the first argument, |
| 2474 | * which we've already retrieved, is the console handle, so don't count |
| 2475 | * it among the arguments to display) |
| 2476 | */ |
| 2477 | err_try |
| 2478 | { |
| 2479 | say_to_console(vmg_ console, argc - 1); |
| 2480 | } |
| 2481 | err_finally |
| 2482 | { |
| 2483 | /* if we created a "main console" object, we're done with it */ |
| 2484 | if (hdl == -1) |
| 2485 | console->delete_obj(vmg0_); |
| 2486 | } |
| 2487 | err_end; |
| 2488 | } |
| 2489 | |
| 2490 | /* ------------------------------------------------------------------------ */ |
| 2491 | /* |
| 2492 | * Log an input event obtained from an external source, such as the Web UI |
| 2493 | */ |
| 2494 | void CVmBifTIO::log_input_event(VMG_ uint argc) |
| 2495 | { |
| 2496 | /* check arguments */ |
| 2497 | check_argc(vmg_ argc, 1); |
| 2498 | |
| 2499 | /* retrieve the list */ |
| 2500 | const vm_val_t *lst = G_stk->get(0); |
| 2501 | if (!lst->is_listlike(vmg0_)) |
| 2502 | err_throw(VMERR_BAD_TYPE_BIF); |
| 2503 | |
| 2504 | /* make sure we have at least one element */ |
| 2505 | int len = lst->ll_length(vmg0_); |
| 2506 | if (len < 1) |
| 2507 | err_throw(VMERR_BAD_VAL_BIF); |
| 2508 | |
| 2509 | /* retrieve the event type code or tag name from the first element */ |
| 2510 | vm_val_t ele1; |
| 2511 | lst->ll_index(vmg_ &ele1, 1); |
| 2512 | |
| 2513 | /* if there's a second element, retrieve the parameter string */ |
| 2514 | const char *param = 0; |
| 2515 | size_t paramlen = 0; |
| 2516 | if (len >= 2) |
| 2517 | { |
| 2518 | /* there's a parameter - retrieve it */ |
| 2519 | vm_val_t ele2; |
| 2520 | lst->ll_index(vmg_ &ele2, 2); |
| 2521 | |
| 2522 | /* |
| 2523 | * Get its string value. If it's a filename object, use the |
| 2524 | * filename path string. |
| 2525 | */ |
| 2526 | CVmObjFileName *fname_param = vm_val_cast(CVmObjFileName, &ele2); |
| 2527 | if (fname_param != 0) |
| 2528 | param = fname_param->get_path_string(); |
| 2529 | else |
| 2530 | param = ele2.get_as_string(vmg0_); |
| 2531 | |
| 2532 | /* if we didn't get a parameter string value, it's an error */ |
| 2533 | if (param == 0) |
| 2534 | err_throw(VMERR_BAD_VAL_BIF); |
| 2535 | |
| 2536 | /* get the parameter length and buffer pointer */ |
| 2537 | paramlen = vmb_get_len(param); |
| 2538 | param += VMB_LEN; |
| 2539 | } |
| 2540 | |
| 2541 | /* log the event, according to the type of the event code */ |
| 2542 | if (ele1.is_numeric(vmg0_)) |
| 2543 | { |
| 2544 | /* it's an integer event type code - log it */ |
| 2545 | G_console->log_event( |
| 2546 | vmg_ ele1.num_to_int(vmg0_), param, paramlen, TRUE); |
| 2547 | } |
| 2548 | else if (ele1.get_as_string(vmg0_) != 0) |
| 2549 | { |
| 2550 | /* it's a string event tag - retrieve it in the UI character set */ |
| 2551 | char tag[128]; |
| 2552 | G_stk->push(&ele1); |
| 2553 | pop_str_val_ui(vmg_ tag, sizeof(tag)); |
| 2554 | |
| 2555 | /* log the event with the given tag */ |
| 2556 | G_console->log_event(vmg_ tag, param, paramlen, TRUE); |
| 2557 | } |
| 2558 | else |
| 2559 | { |
| 2560 | /* other types are invalid */ |
| 2561 | err_throw(VMERR_BAD_VAL_BIF); |
| 2562 | } |
| 2563 | |
| 2564 | /* discard arguments */ |
| 2565 | G_stk->discard(1); |
| 2566 | |
| 2567 | /* no return value */ |
| 2568 | retval_nil(vmg0_); |
| 2569 | } |