| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header$"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 1987, 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 | vmconsol.cpp - TADS 3 console input reader and output formatter |
| 15 | Function |
| 16 | Provides console input and output for the TADS 3 built-in function set |
| 17 | for the T3 VM, including the output formatter. |
| 18 | |
| 19 | T3 uses the UTF-8 character set to represent character strings. The OS |
| 20 | functions use the local character set. We perform the mapping between |
| 21 | UTF-8 and the local character set within this module, so that OS routines |
| 22 | see local characters only, not UTF-8. |
| 23 | |
| 24 | This code is based on the TADS 2 output formatter, but has been |
| 25 | substantially reworked for C++, Unicode, and the slightly different |
| 26 | TADS 3 formatting model. |
| 27 | Notes |
| 28 | |
| 29 | Returns |
| 30 | None |
| 31 | Modified |
| 32 | 08/25/99 MJRoberts - created from TADS 2 output formatter |
| 33 | */ |
| 34 | |
| 35 | #include <stdio.h> |
| 36 | #include <ctype.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | #include <stdarg.h> |
| 40 | #include "wchar.h" |
| 41 | |
| 42 | #include "os.h" |
| 43 | #include "t3std.h" |
| 44 | #include "utf8.h" |
| 45 | #include "charmap.h" |
| 46 | #include "vmuni.h" |
| 47 | #include "vmconsol.h" |
| 48 | #include "vmglob.h" |
| 49 | #include "vmhash.h" |
| 50 | #include "vmdatasrc.h" |
| 51 | #include "vmnetfil.h" |
| 52 | #include "vmfilobj.h" |
| 53 | #include "vmerr.h" |
| 54 | #include "vmobj.h" |
| 55 | |
| 56 | |
| 57 | /* ------------------------------------------------------------------------ */ |
| 58 | /* |
| 59 | * Log-file formatter subclass implementation |
| 60 | */ |
| 61 | |
| 62 | /* |
| 63 | * Open a new log file |
| 64 | */ |
| 65 | int CVmFormatterLog::open_log_file(VMG_ const char *fname) |
| 66 | { |
| 67 | CVmNetFile *nf = 0; |
| 68 | err_try |
| 69 | { |
| 70 | /* create the network file descriptor */ |
| 71 | nf = CVmNetFile::open( |
| 72 | vmg_ fname, 0, NETF_NEW, OSFTLOG, "text/plain"); |
| 73 | } |
| 74 | err_catch_disc |
| 75 | { |
| 76 | nf = 0; |
| 77 | } |
| 78 | err_end; |
| 79 | |
| 80 | /* open the log file using the network file descriptor */ |
| 81 | return open_log_file(vmg_ nf); |
| 82 | } |
| 83 | |
| 84 | int CVmFormatterLog::open_log_file(VMG_ const vm_val_t *filespec, |
| 85 | const struct vm_rcdesc *rc) |
| 86 | { |
| 87 | CVmNetFile *nf = 0; |
| 88 | err_try |
| 89 | { |
| 90 | /* create the network file descriptor */ |
| 91 | nf = CVmNetFile::open(vmg_ filespec, rc, |
| 92 | NETF_NEW, OSFTLOG, "text/plain"); |
| 93 | |
| 94 | /* validate file safety */ |
| 95 | CVmObjFile::check_safety_for_open(vmg_ nf, VMOBJFILE_ACCESS_WRITE); |
| 96 | } |
| 97 | err_catch_disc |
| 98 | { |
| 99 | /* if we got a file, it must be a safety exception - rethrow it */ |
| 100 | if (nf != 0) |
| 101 | { |
| 102 | nf->abandon(vmg0_); |
| 103 | err_rethrow(); |
| 104 | } |
| 105 | } |
| 106 | err_end; |
| 107 | |
| 108 | /* open the log file using the network file descriptor */ |
| 109 | int err = open_log_file(vmg_ nf); |
| 110 | |
| 111 | /* if that succeeded, remember the file spec in our global variable */ |
| 112 | if (!err) |
| 113 | { |
| 114 | /* create our VM global if we don't have one already */ |
| 115 | if (logglob_ == 0) |
| 116 | logglob_ = G_obj_table->create_global_var(); |
| 117 | |
| 118 | /* remember the file spec */ |
| 119 | logglob_->val = *filespec; |
| 120 | } |
| 121 | |
| 122 | /* return the result */ |
| 123 | return err; |
| 124 | } |
| 125 | |
| 126 | int CVmFormatterLog::open_log_file(VMG_ CVmNetFile *nf) |
| 127 | { |
| 128 | /* close any existing log file */ |
| 129 | if (close_log_file(vmg0_)) |
| 130 | { |
| 131 | if (nf != 0) |
| 132 | nf->abandon(vmg0_); |
| 133 | return 1; |
| 134 | } |
| 135 | |
| 136 | /* if there's no network file spec, return failure */ |
| 137 | if (nf == 0) |
| 138 | return 1; |
| 139 | |
| 140 | /* reinitialize */ |
| 141 | init(); |
| 142 | |
| 143 | /* remember the network file descriptor */ |
| 144 | lognf_ = nf; |
| 145 | |
| 146 | /* open the local file */ |
| 147 | logfp_ = osfopwt(lognf_->lclfname, OSFTLOG); |
| 148 | |
| 149 | /* if we couldn't open the file, abandon the network file descriptor */ |
| 150 | if (logfp_ == 0) |
| 151 | { |
| 152 | lognf_->abandon(vmg0_); |
| 153 | lognf_ = 0; |
| 154 | } |
| 155 | |
| 156 | /* return success if we successfully opened the file, failure otherwise */ |
| 157 | return (logfp_ == 0); |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Set the log file to a file opened by the caller |
| 162 | */ |
| 163 | int CVmFormatterLog::set_log_file(VMG_ CVmNetFile *nf, osfildef *fp) |
| 164 | { |
| 165 | /* close any existing log file */ |
| 166 | if (close_log_file(vmg0_)) |
| 167 | return 1; |
| 168 | |
| 169 | /* reinitialize */ |
| 170 | init(); |
| 171 | |
| 172 | /* remember the file */ |
| 173 | logfp_ = fp; |
| 174 | lognf_ = nf; |
| 175 | |
| 176 | /* success */ |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Close the log file |
| 182 | */ |
| 183 | int CVmFormatterLog::close_log_file(VMG0_) |
| 184 | { |
| 185 | /* presume success */ |
| 186 | int err = FALSE; |
| 187 | |
| 188 | /* if we have a file, close it */ |
| 189 | if (logfp_ != 0) |
| 190 | { |
| 191 | /* close and forget the handle */ |
| 192 | osfcls(logfp_); |
| 193 | logfp_ = 0; |
| 194 | } |
| 195 | |
| 196 | /* forget the log network file descriptor, if we have one */ |
| 197 | if (lognf_ != 0) |
| 198 | { |
| 199 | err_try |
| 200 | { |
| 201 | lognf_->close(vmg0_); |
| 202 | } |
| 203 | err_catch_disc |
| 204 | { |
| 205 | /* flag the error, but otherwise discard the exception */ |
| 206 | err = TRUE; |
| 207 | } |
| 208 | err_end; |
| 209 | |
| 210 | /* forget the descriptor, as we've just deleted it */ |
| 211 | lognf_ = 0; |
| 212 | } |
| 213 | |
| 214 | /* clear our global for the file spec */ |
| 215 | if (logglob_ != 0) |
| 216 | logglob_->val.set_nil(); |
| 217 | |
| 218 | /* success */ |
| 219 | return err; |
| 220 | } |
| 221 | |
| 222 | |
| 223 | /* ------------------------------------------------------------------------ */ |
| 224 | /* |
| 225 | * Base Formatter |
| 226 | */ |
| 227 | |
| 228 | /* |
| 229 | * deletion |
| 230 | */ |
| 231 | CVmFormatter::~CVmFormatter() |
| 232 | { |
| 233 | /* if we have a table of horizontal tabs, delete it */ |
| 234 | if (tabs_ != 0) |
| 235 | delete tabs_; |
| 236 | |
| 237 | /* forget the character mapper */ |
| 238 | set_charmap(0); |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * set a new character mapper |
| 243 | */ |
| 244 | void CVmFormatter::set_charmap(CCharmapToLocal *cmap) |
| 245 | { |
| 246 | /* add a reference to the new mapper, if we have one */ |
| 247 | if (cmap != 0) |
| 248 | cmap->add_ref(); |
| 249 | |
| 250 | /* release our reference on any old mapper */ |
| 251 | if (cmap_ != 0) |
| 252 | cmap_->release_ref(); |
| 253 | |
| 254 | /* remember the new mapper */ |
| 255 | cmap_ = cmap; |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Write out a line. Text we receive is in the UTF-8 character set. |
| 260 | */ |
| 261 | void CVmFormatter::write_text(VMG_ const wchar_t *txt, size_t cnt, |
| 262 | const vmcon_color_t *colors, vm_nl_type nl) |
| 263 | { |
| 264 | /* |
| 265 | * Check the "script quiet" mode - this indicates that we're reading |
| 266 | * a script and not echoing output to the display. If this mode is |
| 267 | * on, and we're writing to the display, suppress this write. If |
| 268 | * the mode is off, or we're writing to a non-display stream (such |
| 269 | * as a log file stream), show the output as normal. |
| 270 | */ |
| 271 | if (!console_->is_quiet_script() || !is_disp_stream_) |
| 272 | { |
| 273 | char local_buf[128]; |
| 274 | char *dst; |
| 275 | size_t rem; |
| 276 | |
| 277 | /* |
| 278 | * Check to see if we've reached the end of the screen, and if so |
| 279 | * run the MORE prompt. Note that we don't show a MORE prompt |
| 280 | * unless we're in "formatter more mode," since if we're not, then |
| 281 | * the OS layer code is taking responsibility for pagination |
| 282 | * issues. |
| 283 | * |
| 284 | * Note that we suppress the MORE prompt if we're showing a |
| 285 | * continuation of a line already partially shown. We only want to |
| 286 | * show a MORE prompt at the start of a new line. |
| 287 | * |
| 288 | * Skip the MORE prompt if this stream doesn't use it. |
| 289 | */ |
| 290 | if (formatter_more_mode() |
| 291 | && console_->is_more_mode() |
| 292 | && !is_continuation_ |
| 293 | && linecnt_ + 1 >= console_->get_page_length()) |
| 294 | { |
| 295 | /* set the standard text color */ |
| 296 | set_os_text_color(OS_COLOR_P_TEXT, OS_COLOR_P_TEXTBG); |
| 297 | set_os_text_attr(0); |
| 298 | |
| 299 | /* display the MORE prompt */ |
| 300 | console_->show_more_prompt(vmg0_); |
| 301 | |
| 302 | /* restore the current color scheme */ |
| 303 | set_os_text_color(os_color_.fg, os_color_.bg); |
| 304 | set_os_text_attr(os_color_.attr); |
| 305 | } |
| 306 | |
| 307 | /* count the line if a newline follows */ |
| 308 | if (nl != VM_NL_NONE && nl != VM_NL_NONE_INTERNAL) |
| 309 | ++linecnt_; |
| 310 | |
| 311 | /* convert and display the text */ |
| 312 | for (dst = local_buf, rem = sizeof(local_buf) - 1 ; cnt != 0 ; ) |
| 313 | { |
| 314 | size_t cur; |
| 315 | size_t old_rem; |
| 316 | wchar_t c; |
| 317 | |
| 318 | /* |
| 319 | * if this character is in a new color, write out the OS-level |
| 320 | * color switch code |
| 321 | */ |
| 322 | if (colors != 0 && !colors->equals(&os_color_)) |
| 323 | { |
| 324 | /* |
| 325 | * null-terminate and display what's in the buffer so far, |
| 326 | * so that we close out all of the remaining text in the |
| 327 | * old color and attributes |
| 328 | */ |
| 329 | *dst = '\0'; |
| 330 | print_to_os(local_buf); |
| 331 | |
| 332 | /* reset to the start of the local output buffer */ |
| 333 | dst = local_buf; |
| 334 | rem = sizeof(local_buf) - 1; |
| 335 | |
| 336 | /* set the text attributes, if they changed */ |
| 337 | if (colors->attr != os_color_.attr) |
| 338 | set_os_text_attr(colors->attr); |
| 339 | |
| 340 | /* set the color, if it changed */ |
| 341 | if (colors->fg != os_color_.fg |
| 342 | || colors->bg != os_color_.bg) |
| 343 | set_os_text_color(colors->fg, colors->bg); |
| 344 | |
| 345 | /* |
| 346 | * Whatever happened, set our new color internally as the |
| 347 | * last color we sent to the OS. Even if we didn't |
| 348 | * actually do anything, we'll at least know we won't have |
| 349 | * to do anything more until we find another new color. |
| 350 | */ |
| 351 | os_color_ = *colors; |
| 352 | } |
| 353 | |
| 354 | /* get this character */ |
| 355 | c = *txt; |
| 356 | |
| 357 | /* |
| 358 | * translate non-breaking spaces into ordinary spaces if the |
| 359 | * underlying target isn't HTML-based |
| 360 | */ |
| 361 | if (!html_target_ && c == 0x00A0) |
| 362 | c = ' '; |
| 363 | |
| 364 | /* try storing another character */ |
| 365 | old_rem = rem; |
| 366 | cur = (cmap_ != 0 ? cmap_ : G_cmap_to_ui)->map(c, &dst, &rem); |
| 367 | |
| 368 | /* if that failed, flush the buffer and try again */ |
| 369 | if (cur > old_rem) |
| 370 | { |
| 371 | /* null-terminate the buffer */ |
| 372 | *dst = '\0'; |
| 373 | |
| 374 | /* display the text */ |
| 375 | print_to_os(local_buf); |
| 376 | |
| 377 | /* reset to the start of the local output buffer */ |
| 378 | dst = local_buf; |
| 379 | rem = sizeof(local_buf) - 1; |
| 380 | } |
| 381 | else |
| 382 | { |
| 383 | /* we've now consumed this character of input */ |
| 384 | ++txt; |
| 385 | --cnt; |
| 386 | if (colors != 0) |
| 387 | ++colors; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | /* if we have a partially-filled buffer, display it */ |
| 392 | if (dst > local_buf) |
| 393 | { |
| 394 | /* null-terminate and display the buffer */ |
| 395 | *dst = '\0'; |
| 396 | print_to_os(local_buf); |
| 397 | } |
| 398 | |
| 399 | /* write the appropriate type of line termination */ |
| 400 | switch(nl) |
| 401 | { |
| 402 | case VM_NL_NONE: |
| 403 | case VM_NL_INPUT: |
| 404 | case VM_NL_NONE_INTERNAL: |
| 405 | /* no line termination is needed */ |
| 406 | break; |
| 407 | |
| 408 | case VM_NL_NEWLINE: |
| 409 | /* write a newline */ |
| 410 | print_to_os(html_target_ && html_pre_level_ == 0 ? |
| 411 | "<BR HEIGHT=0>\n" : "\n"); |
| 412 | break; |
| 413 | |
| 414 | case VM_NL_OSNEWLINE: |
| 415 | /* |
| 416 | * the OS will provide a newline, but add a space to make it |
| 417 | * explicit that we can break the line here |
| 418 | */ |
| 419 | print_to_os(" "); |
| 420 | break; |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | /* ------------------------------------------------------------------------ */ |
| 426 | /* |
| 427 | * Flush the current line to the display, using the given type of line |
| 428 | * termination. |
| 429 | * |
| 430 | * VM_NL_NONE: flush the current line but do not start a new line; more |
| 431 | * text will follow on the current line. This is used, for example, to |
| 432 | * flush text after displaying a prompt and before waiting for user |
| 433 | * input. |
| 434 | * |
| 435 | * VM_NL_INPUT: acts like VM_NL_NONE, except that we flush everything, |
| 436 | * including trailing spaces. |
| 437 | * |
| 438 | * VM_NL_NONE_INTERNAL: same as VM_NL_NONE, but doesn't flush at the OS |
| 439 | * level. This is used when we're only flushing our buffers in order to |
| 440 | * clear out space internally, not because we want the underlying OS |
| 441 | * renderer to display things immediately. This distinction is |
| 442 | * important in HTML mode, since it ensures that the HTML parser only |
| 443 | * sees well-formed strings when flushing. |
| 444 | * |
| 445 | * VM_NL_NEWLINE: flush the line and start a new line by writing out a |
| 446 | * newline character. |
| 447 | * |
| 448 | * VM_NL_OSNEWLINE: flush the line as though starting a new line, but |
| 449 | * don't add an actual newline character to the output, since the |
| 450 | * underlying OS display code will handle this. Instead, add a space |
| 451 | * after the line to indicate to the OS code that a line break is |
| 452 | * possible there. (This differs from VM_NL_NONE in that VM_NL_NONE |
| 453 | * doesn't add anything at all after the line.) |
| 454 | */ |
| 455 | void CVmFormatter::flush(VMG_ vm_nl_type nl) |
| 456 | { |
| 457 | int cnt; |
| 458 | vm_nl_type write_nl; |
| 459 | |
| 460 | /* null-terminate the current output line buffer */ |
| 461 | linebuf_[linepos_] = '\0'; |
| 462 | |
| 463 | /* |
| 464 | * Expand any pending tab. Allow "anonymous" tabs only if we're |
| 465 | * flushing because we're ending the line normally; if we're not |
| 466 | * ending the line, we can't handle tabs that depend on the line |
| 467 | * ending. |
| 468 | */ |
| 469 | expand_pending_tab(vmg_ nl == VM_NL_NEWLINE); |
| 470 | |
| 471 | /* |
| 472 | * note number of characters to display - assume we'll display all of |
| 473 | * the characters in the buffer |
| 474 | */ |
| 475 | cnt = wcslen(linebuf_); |
| 476 | |
| 477 | /* |
| 478 | * Trim trailing spaces, unless we're about to read input or are doing |
| 479 | * an internal flush. (Show trailing spaces when reading input, since |
| 480 | * we won't be able to revise the layout after this point. Don't trim |
| 481 | * on an internal flush either, as this kind of flushing simply empties |
| 482 | * out our buffer exactly as it is.) |
| 483 | */ |
| 484 | if (nl != VM_NL_INPUT && nl != VM_NL_NONE_INTERNAL) |
| 485 | { |
| 486 | /* |
| 487 | * look for last non-space character, but keep any spaces that come |
| 488 | * before an explicit non-breaking flag |
| 489 | */ |
| 490 | for ( ; cnt > 0 && linebuf_[cnt-1] == ' ' ; --cnt) |
| 491 | { |
| 492 | /* don't remove this character if it's marked as non-breaking */ |
| 493 | if ((flagbuf_[cnt-1] & VMCON_OBF_NOBREAK) != 0) |
| 494 | break; |
| 495 | } |
| 496 | |
| 497 | /* |
| 498 | * if we're actually doing a newline, discard the trailing spaces |
| 499 | * for good - we don't want them at the start of the next line |
| 500 | */ |
| 501 | if (nl == VM_NL_NEWLINE) |
| 502 | linepos_ = cnt; |
| 503 | } |
| 504 | |
| 505 | /* check the newline mode */ |
| 506 | switch(nl) |
| 507 | { |
| 508 | case VM_NL_NONE: |
| 509 | case VM_NL_NONE_INTERNAL: |
| 510 | /* no newline - just flush out what we have */ |
| 511 | write_nl = VM_NL_NONE; |
| 512 | break; |
| 513 | |
| 514 | case VM_NL_INPUT: |
| 515 | /* no newline - flush out what we have */ |
| 516 | write_nl = VM_NL_NONE; |
| 517 | |
| 518 | /* on input, reset the HTML parsing state */ |
| 519 | html_passthru_state_ = VMCON_HPS_NORMAL; |
| 520 | break; |
| 521 | |
| 522 | case VM_NL_NEWLINE: |
| 523 | /* |
| 524 | * We're adding a newline. We want to suppress redundant |
| 525 | * newlines -- we reduce any run of consecutive vertical |
| 526 | * whitespace to a single newline. So, if we have anything in |
| 527 | * this line, or we didn't already just write a newline, write |
| 528 | * out a newline now; otherwise, write nothing. |
| 529 | */ |
| 530 | if (linecol_ != 0 || !just_did_nl_ || html_pre_level_ > 0) |
| 531 | { |
| 532 | /* add the newline */ |
| 533 | write_nl = VM_NL_NEWLINE; |
| 534 | } |
| 535 | else |
| 536 | { |
| 537 | /* |
| 538 | * Don't write out a newline after all - the line buffer is |
| 539 | * empty, and we just wrote a newline, so this is a |
| 540 | * redundant newline that we wish to suppress (so that we |
| 541 | * collapse a run of vertical whitespace down to a single |
| 542 | * newline). |
| 543 | */ |
| 544 | write_nl = VM_NL_NONE; |
| 545 | } |
| 546 | break; |
| 547 | |
| 548 | case VM_NL_OSNEWLINE: |
| 549 | /* |
| 550 | * we're going to depend on the underlying OS output layer to do |
| 551 | * line breaking, so we won't add a newline, but we will add a |
| 552 | * space, so that the underlying OS layer knows we have a word |
| 553 | * break here |
| 554 | */ |
| 555 | write_nl = VM_NL_OSNEWLINE; |
| 556 | break; |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * display the line, as long as we have something buffered to |
| 561 | * display; even if we don't, display it if our column is non-zero |
| 562 | * and we didn't just do a newline, since this must mean that we've |
| 563 | * flushed a partial line and are just now doing the newline |
| 564 | */ |
| 565 | if (cnt != 0 || (linecol_ != 0 && !just_did_nl_) |
| 566 | || html_pre_level_ > 0) |
| 567 | { |
| 568 | /* write it out */ |
| 569 | write_text(vmg_ linebuf_, cnt, colorbuf_, write_nl); |
| 570 | } |
| 571 | |
| 572 | /* check the line ending */ |
| 573 | switch (nl) |
| 574 | { |
| 575 | case VM_NL_NONE: |
| 576 | case VM_NL_INPUT: |
| 577 | /* we're not displaying a newline, so flush what we have */ |
| 578 | flush_to_os(); |
| 579 | |
| 580 | /* |
| 581 | * the subsequent buffer will be a continuation of the current |
| 582 | * text, if we've displayed anything at all here |
| 583 | */ |
| 584 | is_continuation_ = (linecol_ != 0); |
| 585 | break; |
| 586 | |
| 587 | case VM_NL_NONE_INTERNAL: |
| 588 | /* |
| 589 | * internal buffer flush only - subsequent text will be a |
| 590 | * continuation of the current line, if there's anything on the |
| 591 | * current line |
| 592 | */ |
| 593 | is_continuation_ = (linecol_ != 0); |
| 594 | break; |
| 595 | |
| 596 | default: |
| 597 | /* we displayed a newline, so reset the column position */ |
| 598 | linecol_ = 0; |
| 599 | |
| 600 | /* the next buffer starts a new line on the display */ |
| 601 | is_continuation_ = FALSE; |
| 602 | break; |
| 603 | } |
| 604 | |
| 605 | /* |
| 606 | * Move any trailing characters we didn't write in this go to the start |
| 607 | * of the buffer. |
| 608 | */ |
| 609 | if (cnt < linepos_) |
| 610 | { |
| 611 | size_t movecnt; |
| 612 | |
| 613 | /* calculate how many trailing characters we didn't write */ |
| 614 | movecnt = linepos_ - cnt; |
| 615 | |
| 616 | /* move the characters, colors, and flags */ |
| 617 | memmove(linebuf_, linebuf_ + cnt, movecnt * sizeof(linebuf_[0])); |
| 618 | memmove(colorbuf_, colorbuf_ + cnt, movecnt * sizeof(colorbuf_[0])); |
| 619 | memmove(flagbuf_, flagbuf_ + cnt, movecnt * sizeof(flagbuf_[0])); |
| 620 | } |
| 621 | |
| 622 | /* move the line output position to follow the preserved characters */ |
| 623 | linepos_ -= cnt; |
| 624 | |
| 625 | /* |
| 626 | * If we just output a newline, note it. If we didn't just output a |
| 627 | * newline, but we did write out anything else, note that we're no |
| 628 | * longer at the start of a line on the underlying output device. |
| 629 | */ |
| 630 | if (nl == VM_NL_NEWLINE) |
| 631 | just_did_nl_ = TRUE; |
| 632 | else if (cnt != 0) |
| 633 | just_did_nl_ = FALSE; |
| 634 | |
| 635 | /* |
| 636 | * if the current buffering color doesn't match the current osifc-layer |
| 637 | * color, then we must need to flush just the new color/attribute |
| 638 | * settings (this can happen when we have changed the attributes in |
| 639 | * preparation for reading input, since we won't have any actual text |
| 640 | * to write after the color change) |
| 641 | */ |
| 642 | if (!cur_color_.equals(&os_color_)) |
| 643 | { |
| 644 | /* set the text attributes in the OS window, if they changed */ |
| 645 | if (cur_color_.attr != os_color_.attr) |
| 646 | set_os_text_attr(cur_color_.attr); |
| 647 | |
| 648 | /* set the color in the OS window, if it changed */ |
| 649 | if (cur_color_.fg != os_color_.fg |
| 650 | || cur_color_.bg != os_color_.bg) |
| 651 | set_os_text_color(cur_color_.fg, cur_color_.bg); |
| 652 | |
| 653 | /* set the new osifc color */ |
| 654 | os_color_ = cur_color_; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | /* ------------------------------------------------------------------------ */ |
| 659 | /* |
| 660 | * Clear out our buffers |
| 661 | */ |
| 662 | void CVmFormatter::empty_buffers(VMG0_) |
| 663 | { |
| 664 | /* reset our buffer pointers */ |
| 665 | linepos_ = 0; |
| 666 | linecol_ = 0; |
| 667 | linebuf_[0] = '\0'; |
| 668 | just_did_nl_ = FALSE; |
| 669 | is_continuation_ = FALSE; |
| 670 | |
| 671 | /* there's no pending tab now */ |
| 672 | pending_tab_align_ = VMFMT_TAB_NONE; |
| 673 | |
| 674 | /* start out at the first line */ |
| 675 | linecnt_ = 0; |
| 676 | |
| 677 | /* reset the HTML lexical state */ |
| 678 | html_passthru_state_ = VMCON_HPS_NORMAL; |
| 679 | } |
| 680 | |
| 681 | /* ------------------------------------------------------------------------ */ |
| 682 | /* |
| 683 | * Immediately update the display window |
| 684 | */ |
| 685 | void CVmFormatter::update_display(VMG0_) |
| 686 | { |
| 687 | /* update the display window at the OS layer */ |
| 688 | os_update_display(); |
| 689 | } |
| 690 | |
| 691 | /* ------------------------------------------------------------------------ */ |
| 692 | /* |
| 693 | * Display a blank line to the stream |
| 694 | */ |
| 695 | void CVmFormatter::write_blank_line(VMG0_) |
| 696 | { |
| 697 | /* flush the stream */ |
| 698 | flush(vmg_ VM_NL_NEWLINE); |
| 699 | |
| 700 | /* if generating for an HTML display target, add an HTML line break */ |
| 701 | if (html_target_) |
| 702 | write_text(vmg_ L"<BR>", 4, 0, VM_NL_NONE); |
| 703 | |
| 704 | /* write out a blank line */ |
| 705 | write_text(vmg_ L"", 0, 0, VM_NL_NEWLINE); |
| 706 | } |
| 707 | |
| 708 | /* ------------------------------------------------------------------------ */ |
| 709 | /* |
| 710 | * Generate a tab for a "\t" sequence in the game text, or a <TAB |
| 711 | * MULTIPLE> or <TAB INDENT> sequence parsed in our mini-parser. |
| 712 | * |
| 713 | * Standard (non-HTML) version: we'll generate enough spaces to take us to |
| 714 | * the next tab stop. |
| 715 | * |
| 716 | * HTML version: if we're in native HTML mode, we'll just generate the |
| 717 | * equivalent HTML; if we're not in HTML mode, we'll generate a hard tab |
| 718 | * character, which the HTML formatter will interpret as a <TAB |
| 719 | * MULTIPLE=4>. |
| 720 | */ |
| 721 | void CVmFormatter::write_tab(VMG_ int indent, int multiple) |
| 722 | { |
| 723 | int maxcol; |
| 724 | |
| 725 | /* check to see what the underlying system is expecting */ |
| 726 | if (html_target_) |
| 727 | { |
| 728 | char buf[40]; |
| 729 | |
| 730 | /* |
| 731 | * the underlying system is HTML - generate an appropriate <TAB> |
| 732 | * sequence to produce the desired effect |
| 733 | */ |
| 734 | sprintf(buf, "<TAB %s=%d>", |
| 735 | indent != 0 ? "INDENT" : "MULTIPLE", |
| 736 | indent != 0 ? indent : multiple); |
| 737 | |
| 738 | /* write it out */ |
| 739 | buffer_string(vmg_ buf); |
| 740 | } |
| 741 | else if (multiple != 0) |
| 742 | { |
| 743 | /* get the maximum column */ |
| 744 | maxcol = get_buffer_maxcol(); |
| 745 | |
| 746 | /* |
| 747 | * We don't have an HTML target, and we have a tab to an every-N |
| 748 | * stop: expand the tab with spaces. Keep going until we reach |
| 749 | * the next tab stop of the given multiple. |
| 750 | */ |
| 751 | do |
| 752 | { |
| 753 | /* stop if we've reached the maximum column */ |
| 754 | if (linecol_ >= maxcol) |
| 755 | break; |
| 756 | |
| 757 | /* add another space */ |
| 758 | linebuf_[linepos_] = ' '; |
| 759 | flagbuf_[linepos_] = cur_flags_; |
| 760 | colorbuf_[linepos_] = cur_color_; |
| 761 | |
| 762 | /* advance one character in the buffer */ |
| 763 | ++linepos_; |
| 764 | |
| 765 | /* advance the column counter */ |
| 766 | ++linecol_; |
| 767 | } while ((linecol_ + 1) % multiple != 0); |
| 768 | } |
| 769 | else if (indent != 0) |
| 770 | { |
| 771 | /* |
| 772 | * We don't have an HTML target, and we just want to add a given |
| 773 | * number of spaces. Simply write out the given number of spaces, |
| 774 | * up to our maximum column limit. |
| 775 | */ |
| 776 | for (maxcol = get_buffer_maxcol() ; |
| 777 | indent != 0 && linecol_ < maxcol ; --indent) |
| 778 | { |
| 779 | /* add another space */ |
| 780 | linebuf_[linepos_] = ' '; |
| 781 | flagbuf_[linepos_] = cur_flags_; |
| 782 | colorbuf_[linepos_] = cur_color_; |
| 783 | |
| 784 | /* advance one character in the buffer and one column */ |
| 785 | ++linepos_; |
| 786 | ++linecol_; |
| 787 | } |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | |
| 792 | /* ------------------------------------------------------------------------ */ |
| 793 | /* |
| 794 | * Flush a line |
| 795 | */ |
| 796 | void CVmFormatter::flush_line(VMG_ int padding) |
| 797 | { |
| 798 | /* |
| 799 | * check to see if we're using the underlying display layer's line |
| 800 | * wrapping |
| 801 | */ |
| 802 | if (os_line_wrap_) |
| 803 | { |
| 804 | /* |
| 805 | * In the HTML version, we don't need the normal *MORE* |
| 806 | * processing, since the HTML layer will handle that. |
| 807 | * Furthermore, we don't need to provide actual newline breaks |
| 808 | * -- that happens after the HTML is parsed, so we don't have |
| 809 | * enough information here to figure out actual line breaks. |
| 810 | * So, we'll just flush out our buffer whenever it fills up, and |
| 811 | * suppress newlines. |
| 812 | * |
| 813 | * Similarly, if we have OS-level line wrapping, don't try to |
| 814 | * figure out where the line breaks go -- just flush our buffer |
| 815 | * without a trailing newline whenever the buffer is full, and |
| 816 | * let the OS layer worry about formatting lines and paragraphs. |
| 817 | * |
| 818 | * If we're using padding, use newline mode VM_NL_OSNEWLINE. If |
| 819 | * we don't want padding (which is the case if we completely |
| 820 | * fill up the buffer without finding any word breaks), write |
| 821 | * out in mode VM_NL_NONE, which just flushes the buffer exactly |
| 822 | * like it is. |
| 823 | */ |
| 824 | flush(vmg_ padding ? VM_NL_OSNEWLINE : VM_NL_NONE_INTERNAL); |
| 825 | } |
| 826 | else |
| 827 | { |
| 828 | /* |
| 829 | * Normal mode - we process the *MORE* prompt ourselves, and we |
| 830 | * are responsible for figuring out where the actual line breaks |
| 831 | * go. Use flush() to generate an actual newline whenever we |
| 832 | * flush out our buffer. |
| 833 | */ |
| 834 | flush(vmg_ VM_NL_NEWLINE); |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | |
| 839 | /* ------------------------------------------------------------------------ */ |
| 840 | /* |
| 841 | * Write a character to an output stream. The character is provided to us |
| 842 | * as a wide Unicode character. |
| 843 | */ |
| 844 | void CVmFormatter::buffer_char(VMG_ wchar_t c) |
| 845 | { |
| 846 | const wchar_t *exp; |
| 847 | size_t exp_len; |
| 848 | |
| 849 | /* check for a display expansion */ |
| 850 | exp = (cmap_ != 0 ? cmap_ : G_cmap_to_ui)->get_expansion(c, &exp_len); |
| 851 | if (exp != 0) |
| 852 | { |
| 853 | /* write each character of the expansion */ |
| 854 | for ( ; exp_len != 0 ; ++exp, --exp_len) |
| 855 | buffer_expchar(vmg_ *exp); |
| 856 | } |
| 857 | else |
| 858 | { |
| 859 | /* there's no expansion - buffer the character as-is */ |
| 860 | buffer_expchar(vmg_ c); |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | /* |
| 865 | * Write an expanded character to an output stream. |
| 866 | */ |
| 867 | void CVmFormatter::buffer_expchar(VMG_ wchar_t c) |
| 868 | { |
| 869 | /* presume the character takes up only one column */ |
| 870 | int cwid = 1; |
| 871 | |
| 872 | /* presume we'll use the current flags for the new character */ |
| 873 | unsigned char cflags = cur_flags_; |
| 874 | |
| 875 | /* assume it's not a quoted space */ |
| 876 | int qspace = FALSE; |
| 877 | |
| 878 | /* |
| 879 | * Check for some special characters. |
| 880 | * |
| 881 | * If we have an underlying HTML renderer, keep track of the HTML |
| 882 | * lexical state, so we know if we're in a tag or in ordinary text. We |
| 883 | * can pass through all of the special line-breaking and spacing |
| 884 | * characters to the underlying HTML renderer. |
| 885 | * |
| 886 | * If our underlying renderer is a plain text renderer, we actually |
| 887 | * parse the HTML ourselves, so HTML tags will never make it this far - |
| 888 | * the caller will already have interpreted any HTML tags and removed |
| 889 | * them from the text stream, passing only the final plain text to us. |
| 890 | * However, with a plain text renderer, we have to do all of the work |
| 891 | * of line breaking, so we must look at the special spacing and |
| 892 | * line-break control characters. |
| 893 | */ |
| 894 | if (html_target_) |
| 895 | { |
| 896 | /* |
| 897 | * track the lexical state of the HTML stream going to the |
| 898 | * underlying renderer |
| 899 | */ |
| 900 | switch (html_passthru_state_) |
| 901 | { |
| 902 | case VMCON_HPS_MARKUP_END: |
| 903 | case VMCON_HPS_NORMAL: |
| 904 | /* check to see if we're starting a markup */ |
| 905 | if (c == '&') |
| 906 | html_passthru_state_ = VMCON_HPS_ENTITY_1ST; |
| 907 | else if (c == '<') |
| 908 | { |
| 909 | html_passthru_tagp_ = html_passthru_tag_; |
| 910 | html_passthru_state_ = VMCON_HPS_TAG_START; |
| 911 | } |
| 912 | else |
| 913 | html_passthru_state_ = VMCON_HPS_NORMAL; |
| 914 | break; |
| 915 | |
| 916 | case VMCON_HPS_ENTITY_1ST: |
| 917 | /* check to see what kind of entity we have */ |
| 918 | if (c == '#') |
| 919 | html_passthru_state_ = VMCON_HPS_ENTITY_NUM_1ST; |
| 920 | else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) |
| 921 | html_passthru_state_ = VMCON_HPS_ENTITY_NAME; |
| 922 | else |
| 923 | html_passthru_state_ = VMCON_HPS_NORMAL; |
| 924 | break; |
| 925 | |
| 926 | case VMCON_HPS_ENTITY_NUM_1ST: |
| 927 | /* check to see what kind of number we have */ |
| 928 | if (c == 'x' || c == 'X') |
| 929 | html_passthru_state_ = VMCON_HPS_ENTITY_HEX; |
| 930 | else if (c >= '0' && c <= '9') |
| 931 | html_passthru_state_ = VMCON_HPS_ENTITY_DEC; |
| 932 | else |
| 933 | html_passthru_state_ = VMCON_HPS_NORMAL; |
| 934 | break; |
| 935 | |
| 936 | case VMCON_HPS_ENTITY_HEX: |
| 937 | /* see if we're done with hex digits */ |
| 938 | if (c == ';') |
| 939 | html_passthru_state_ = VMCON_HPS_MARKUP_END; |
| 940 | else if ((c < '0' || c > '9') |
| 941 | && (c < 'a' || c > 'f') |
| 942 | && (c < 'A' || c > 'F')) |
| 943 | html_passthru_state_ = VMCON_HPS_NORMAL; |
| 944 | break; |
| 945 | |
| 946 | case VMCON_HPS_ENTITY_DEC: |
| 947 | /* see if we're done with decimal digits */ |
| 948 | if (c == ';') |
| 949 | html_passthru_state_ = VMCON_HPS_MARKUP_END; |
| 950 | else if (c < '0' || c > '9') |
| 951 | html_passthru_state_ = VMCON_HPS_NORMAL; |
| 952 | break; |
| 953 | |
| 954 | case VMCON_HPS_ENTITY_NAME: |
| 955 | /* see if we're done with alphanumerics */ |
| 956 | if (c == ';') |
| 957 | html_passthru_state_ = VMCON_HPS_MARKUP_END; |
| 958 | else if ((c < 'a' || c > 'z') |
| 959 | && (c < 'A' || c > 'Z') |
| 960 | && (c < '0' || c > '9')) |
| 961 | html_passthru_state_ = VMCON_HPS_NORMAL; |
| 962 | break; |
| 963 | |
| 964 | case VMCON_HPS_TAG_START: |
| 965 | /* start of a tag, before the name - check for the name start */ |
| 966 | if (c == '/' && html_passthru_tagp_ == html_passthru_tag_) |
| 967 | { |
| 968 | /* |
| 969 | * note the initial '/', but stay in TAG_START state, so |
| 970 | * that we skip any spaces between the '/' and the tag name |
| 971 | */ |
| 972 | *html_passthru_tagp_++ = '/'; |
| 973 | } |
| 974 | else if ((c >= 'a' && c <= 'z') |
| 975 | || (c >= 'A' && c <= 'Z')) |
| 976 | { |
| 977 | /* start of the tag name */ |
| 978 | html_passthru_state_ = VMCON_HPS_TAG_NAME; |
| 979 | *html_passthru_tagp_++ = (char)c; |
| 980 | } |
| 981 | else if (c == ' ' || c == '\t' || c == '\n' || c == '\r') |
| 982 | { |
| 983 | /* |
| 984 | * ignore whitespace between '<' and the tag name - simply |
| 985 | * stay in TAG_START mode |
| 986 | */ |
| 987 | } |
| 988 | else |
| 989 | { |
| 990 | /* anything else is invalid - must not be a tag after all */ |
| 991 | html_passthru_state_ = VMCON_HPS_NORMAL; |
| 992 | } |
| 993 | break; |
| 994 | |
| 995 | case VMCON_HPS_TAG_NAME: |
| 996 | /* tag name - check for continuation */ |
| 997 | if ((c >= 'a' && c <= 'z') |
| 998 | || (c >= 'A' && c <= 'Z')) |
| 999 | { |
| 1000 | /* gather the tag name if it fits */ |
| 1001 | if (html_passthru_tagp_ - html_passthru_tag_ + 1 |
| 1002 | < sizeof(html_passthru_tag_)) |
| 1003 | *html_passthru_tagp_++ = (char)c; |
| 1004 | } |
| 1005 | else |
| 1006 | { |
| 1007 | /* end of the tag name */ |
| 1008 | *html_passthru_tagp_ = '\0'; |
| 1009 | html_passthru_state_ = VMCON_HPS_TAG; |
| 1010 | goto do_tag; |
| 1011 | } |
| 1012 | break; |
| 1013 | |
| 1014 | case VMCON_HPS_TAG: |
| 1015 | do_tag: |
| 1016 | /* see if we're done with the tag, or entering quoted material */ |
| 1017 | if (c == '>') |
| 1018 | { |
| 1019 | /* switch to end of markup mode */ |
| 1020 | html_passthru_state_ = VMCON_HPS_MARKUP_END; |
| 1021 | |
| 1022 | /* note if entering or exiting a PRE tag */ |
| 1023 | if (stricmp(html_passthru_tag_, "pre") == 0) |
| 1024 | ++html_pre_level_; |
| 1025 | else if (stricmp(html_passthru_tag_, "/pre") == 0 |
| 1026 | && html_pre_level_ != 0) |
| 1027 | --html_pre_level_; |
| 1028 | } |
| 1029 | else if (c == '/' |
| 1030 | && html_passthru_tagp_ != html_passthru_tag_ |
| 1031 | && *(html_passthru_tagp_ - 1) != '/') |
| 1032 | { |
| 1033 | /* add the '/' to the end of the tag name */ |
| 1034 | if (html_passthru_tagp_ - html_passthru_tag_ + 1 |
| 1035 | < sizeof(html_passthru_tag_)) |
| 1036 | { |
| 1037 | *html_passthru_tagp_++ = (char)c; |
| 1038 | *html_passthru_tagp_ = '\0'; |
| 1039 | } |
| 1040 | } |
| 1041 | else if (c == '"') |
| 1042 | html_passthru_state_ = VMCON_HPS_DQUOTE; |
| 1043 | else if (c == '\'') |
| 1044 | html_passthru_state_ = VMCON_HPS_SQUOTE; |
| 1045 | break; |
| 1046 | |
| 1047 | case VMCON_HPS_SQUOTE: |
| 1048 | /* see if we're done with the quoted material */ |
| 1049 | if (c == '\'') |
| 1050 | html_passthru_state_ = VMCON_HPS_TAG; |
| 1051 | break; |
| 1052 | |
| 1053 | case VMCON_HPS_DQUOTE: |
| 1054 | /* see if we're done with the quoted material */ |
| 1055 | if (c == '"') |
| 1056 | html_passthru_state_ = VMCON_HPS_TAG; |
| 1057 | break; |
| 1058 | |
| 1059 | default: |
| 1060 | /* ignore other states */ |
| 1061 | break; |
| 1062 | } |
| 1063 | } |
| 1064 | else |
| 1065 | { |
| 1066 | /* check for special characters */ |
| 1067 | switch(c) |
| 1068 | { |
| 1069 | case 0x00AD: |
| 1070 | /* |
| 1071 | * The Unicode "soft hyphen" character. This indicates a |
| 1072 | * point at which we can insert a hyphen followed by a soft |
| 1073 | * line break, if it's a convenient point to break the line; |
| 1074 | * if we don't choose to break the line here, the soft hyphen |
| 1075 | * is invisible. |
| 1076 | * |
| 1077 | * Don't buffer anything at all; instead, just flag the |
| 1078 | * preceding character as being a soft hyphenation point, so |
| 1079 | * that we can insert a hyphen there when we get around to |
| 1080 | * breaking the line. |
| 1081 | */ |
| 1082 | if (linepos_ != 0) |
| 1083 | flagbuf_[linepos_ - 1] |= VMCON_OBF_SHY; |
| 1084 | |
| 1085 | /* we don't need to buffer anything, so we're done */ |
| 1086 | return; |
| 1087 | |
| 1088 | case 0xFEFF: |
| 1089 | /* |
| 1090 | * The Unicode zero-width non-breaking space. This indicates |
| 1091 | * a point at which we cannot break the line, even if we could |
| 1092 | * normally break here. Flag the preceding character as a |
| 1093 | * non-breaking point. Don't buffer anything for this |
| 1094 | * character, as it's not rendered; it merely controls line |
| 1095 | * breaking. |
| 1096 | */ |
| 1097 | if (linepos_ != 0) |
| 1098 | flagbuf_[linepos_ - 1] |= VMCON_OBF_NOBREAK; |
| 1099 | |
| 1100 | /* we don't buffer anything, so we're done */ |
| 1101 | return; |
| 1102 | |
| 1103 | case 0x200B: /* zero-width space */ |
| 1104 | case 0x200a: /* hair space */ |
| 1105 | case 0x2008: /* punctuation space */ |
| 1106 | /* |
| 1107 | * Zero-width space: This indicates an explicitly allowed |
| 1108 | * line-breaking point, but is rendered as invisible. Flag the |
| 1109 | * preceding character as an OK-to-break point, but don't |
| 1110 | * buffer anything, as the zero-width space isn't rendered. |
| 1111 | * |
| 1112 | * Hair and punctuation spaces: Treat these very thin spaces as |
| 1113 | * invisible in a fixed-width font. These are normally used |
| 1114 | * for subtle typographical effects in proportionally-spaced |
| 1115 | * fonts; for example, for separating a right single quote from |
| 1116 | * an immediately following right double quote (as in a |
| 1117 | * quotation within a quotation: I said, "type 'quote'"). When |
| 1118 | * translating to fixed-pitch type, these special spacing |
| 1119 | * effects aren't usually necessary or desirable because of the |
| 1120 | * built-in space in every character cell. |
| 1121 | * |
| 1122 | * These spaces cancel any explicit non-breaking flag that |
| 1123 | * precedes them, since they cause the flag to act on the |
| 1124 | * space's left edge, while leaving the right edge open for |
| 1125 | * breaking. Since we don't actually take up any buffer space, |
| 1126 | * push our right edge's breakability back to the preceding |
| 1127 | * character. |
| 1128 | */ |
| 1129 | if (linepos_ != 0) |
| 1130 | { |
| 1131 | flagbuf_[linepos_ - 1] &= ~VMCON_OBF_NOBREAK; |
| 1132 | flagbuf_[linepos_ - 1] |= VMCON_OBF_OKBREAK; |
| 1133 | } |
| 1134 | |
| 1135 | /* we don't buffer anything, so we're done */ |
| 1136 | return; |
| 1137 | |
| 1138 | case 0x00A0: |
| 1139 | /* non-breaking space - buffer it as given */ |
| 1140 | break; |
| 1141 | |
| 1142 | case 0x0015: /* special internal quoted space character */ |
| 1143 | case 0x2005: /* four-per-em space */ |
| 1144 | case 0x2006: /* six-per-em space */ |
| 1145 | case 0x2007: /* figure space */ |
| 1146 | case 0x2009: /* thin space */ |
| 1147 | /* |
| 1148 | * Treat all of these as non-combining spaces, and render them |
| 1149 | * all as single ordinary spaces. In text mode, we are |
| 1150 | * limited to a monospaced font, so we can't render any |
| 1151 | * differences among these various thinner-than-normal spaces. |
| 1152 | */ |
| 1153 | qspace = TRUE; |
| 1154 | c = ' '; |
| 1155 | break; |
| 1156 | |
| 1157 | case 0x2002: /* en space */ |
| 1158 | case 0x2004: /* three-per-em space */ |
| 1159 | /* |
| 1160 | * En space, three-per-em space - mark these as non-combining, |
| 1161 | * and render them as a two ordinary spaces. In the case of |
| 1162 | * an en space, we really do want to take up the space of two |
| 1163 | * ordinary spaces; for a three-per-em space, we want about a |
| 1164 | * space and a half, but since we're dealing with a monospaced |
| 1165 | * font, we have to round up to a full two spaces. |
| 1166 | */ |
| 1167 | qspace = TRUE; |
| 1168 | cwid = 2; |
| 1169 | c = ' '; |
| 1170 | break; |
| 1171 | |
| 1172 | case 0x2003: |
| 1173 | /* em space - mark it as non-combining */ |
| 1174 | qspace = TRUE; |
| 1175 | |
| 1176 | /* render this as three ordinary spaces */ |
| 1177 | cwid = 3; |
| 1178 | c = ' '; |
| 1179 | break; |
| 1180 | |
| 1181 | default: |
| 1182 | /* |
| 1183 | * Translate any horizontal whitespace character to a regular |
| 1184 | * space character. Note that, once this is done, we don't |
| 1185 | * need to worry about calling t3_is_space() any more - we can |
| 1186 | * just check that we have a regular ' ' character. |
| 1187 | */ |
| 1188 | if (t3_is_space(c)) |
| 1189 | { |
| 1190 | /* convert it to an ordinary space */ |
| 1191 | c = ' '; |
| 1192 | |
| 1193 | /* if we're in obey-whitespace mode, quote the space */ |
| 1194 | qspace = obey_whitespace_ || html_pre_level_ > 0; |
| 1195 | } |
| 1196 | break; |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | /* if it's a quoted space, mark it as such in the buffer flags */ |
| 1201 | if (qspace) |
| 1202 | cflags |= VMCON_OBF_QSPACE; |
| 1203 | |
| 1204 | /* |
| 1205 | * Check for the caps/nocaps flags - but only if our HTML lexical state |
| 1206 | * in the underlying text stream is plain text, because we don't want |
| 1207 | * to apply these flags to alphabetic characters that are inside tag or |
| 1208 | * entity text. |
| 1209 | */ |
| 1210 | if (html_passthru_state_ == VMCON_HPS_NORMAL) |
| 1211 | { |
| 1212 | if ((capsflag_ || allcapsflag_) && t3_is_alpha(c)) |
| 1213 | { |
| 1214 | /* |
| 1215 | * capsflag or allcapsflag is set, so render this character in |
| 1216 | * title case or upper case, respectively. For the ordinary |
| 1217 | * capsflag, use title case rather than upper case, since |
| 1218 | * capsflag conceptually only applies to a single letter, and |
| 1219 | * some Unicode characters represent ligatures of multiple |
| 1220 | * letters. In such cases we only want to capitalize the first |
| 1221 | * letter in the ligature, which is exactly what we get when we |
| 1222 | * convert it to the title case. |
| 1223 | * |
| 1224 | * Start by consuming the capsflag. |
| 1225 | */ |
| 1226 | capsflag_ = FALSE; |
| 1227 | |
| 1228 | /* get the appropriate expansion */ |
| 1229 | const wchar_t *u = allcapsflag_ ? t3_to_upper(c) : t3_to_title(c); |
| 1230 | |
| 1231 | /* |
| 1232 | * If there's no expansion, continue with the original |
| 1233 | * character. If it's a single-character expansion, continue |
| 1234 | * with the replacement character. If it's a 1:N expansion, |
| 1235 | * recursively buffer the expansion. |
| 1236 | */ |
| 1237 | if (u != 0 && u[0] != 0) |
| 1238 | { |
| 1239 | if (u[1] != 0) |
| 1240 | { |
| 1241 | /* 1:N expansion - handle it recursively */ |
| 1242 | buffer_wstring(vmg_ u); |
| 1243 | return; |
| 1244 | } |
| 1245 | else |
| 1246 | { |
| 1247 | /* 1:1 expansion - continue with the new character */ |
| 1248 | c = u[0]; |
| 1249 | } |
| 1250 | } |
| 1251 | } |
| 1252 | else if (nocapsflag_ && t3_is_alpha(c)) |
| 1253 | { |
| 1254 | /* lower-casing the character - consume the flag */ |
| 1255 | nocapsflag_ = FALSE; |
| 1256 | |
| 1257 | /* get the expansion */ |
| 1258 | const wchar_t *l = t3_to_lower(c); |
| 1259 | |
| 1260 | /* |
| 1261 | * recursively handle 1:N expansions; otherwise continue with |
| 1262 | * the replacement character, if there is one |
| 1263 | */ |
| 1264 | if (l != 0 && l[0] != 0) |
| 1265 | { |
| 1266 | if (l[1] != 0) |
| 1267 | { |
| 1268 | /* 1:N expansion - handle it recursively */ |
| 1269 | buffer_wstring(vmg_ l); |
| 1270 | return; |
| 1271 | } |
| 1272 | else |
| 1273 | { |
| 1274 | /* 1:1 expansion - continue with the new character */ |
| 1275 | c = l[0]; |
| 1276 | } |
| 1277 | } |
| 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | /* |
| 1282 | * If this is a space of some kind, we might be able to consolidate it |
| 1283 | * with a preceding character. If the display layer is an HTML |
| 1284 | * renderer, pass spaces through intact, and let the HTML parser handle |
| 1285 | * whitespace compression. |
| 1286 | */ |
| 1287 | if (c == ' ' && html_pre_level_ == 0) |
| 1288 | { |
| 1289 | /* ignore ordinary whitespace at the start of a line */ |
| 1290 | if (linecol_ == 0 && !qspace) |
| 1291 | return; |
| 1292 | |
| 1293 | /* |
| 1294 | * Consolidate runs of whitespace. Ordinary whitespace is |
| 1295 | * subsumed into any type of quoted spaces, but quoted spaces do |
| 1296 | * not combine. |
| 1297 | */ |
| 1298 | if (linepos_ > 0) |
| 1299 | { |
| 1300 | wchar_t prv; |
| 1301 | |
| 1302 | /* get the previous character */ |
| 1303 | prv = linebuf_[linepos_ - 1]; |
| 1304 | |
| 1305 | /* |
| 1306 | * if the new character is an ordinary (combining) whitespace |
| 1307 | * character, subsume it into any preceding space character |
| 1308 | */ |
| 1309 | if (!qspace && prv == ' ') |
| 1310 | return; |
| 1311 | |
| 1312 | /* |
| 1313 | * if the new character is a quoted space, and the preceding |
| 1314 | * character is a non-quoted space, subsume the preceding |
| 1315 | * space into the new character |
| 1316 | */ |
| 1317 | if (qspace |
| 1318 | && prv == ' ' |
| 1319 | && !(flagbuf_[linepos_ - 1] & VMCON_OBF_QSPACE)) |
| 1320 | { |
| 1321 | /* remove the preceding ordinary whitespace */ |
| 1322 | --linepos_; |
| 1323 | --linecol_; |
| 1324 | } |
| 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | /* if the new character fits in the line, add it */ |
| 1329 | if (linecol_ + cwid < get_buffer_maxcol()) |
| 1330 | { |
| 1331 | /* buffer this character */ |
| 1332 | buffer_rendered(c, cflags, cwid); |
| 1333 | |
| 1334 | /* we're finished processing the character */ |
| 1335 | return; |
| 1336 | } |
| 1337 | |
| 1338 | /* |
| 1339 | * The line would overflow if this character were added. |
| 1340 | * |
| 1341 | * If we're trying to output any kind of breakable space, just add it |
| 1342 | * to the line buffer for now; we'll come back later and figure out |
| 1343 | * where to break upon buffering the next non-space character. This |
| 1344 | * ensures that we don't carry trailing space (even trailing en or em |
| 1345 | * spaces) to the start of the next line if we have an explicit |
| 1346 | * newline before the next non-space character. |
| 1347 | */ |
| 1348 | if (c == ' ') |
| 1349 | { |
| 1350 | /* |
| 1351 | * We're adding a space, so we'll figure out the breaking later, |
| 1352 | * when we output the next non-space character. If the preceding |
| 1353 | * character is any kind of space, don't bother adding the new |
| 1354 | * one, since any contiguous whitespace at the end of the line has |
| 1355 | * no effect on the line's appearance. |
| 1356 | */ |
| 1357 | if (linebuf_[linepos_ - 1] == ' ') |
| 1358 | { |
| 1359 | /* |
| 1360 | * We're adding a space to a line that already ends in a |
| 1361 | * space, so we don't really need to add the character. |
| 1362 | * However, reflect the virtual addition in the output column |
| 1363 | * position, since the space does affect our column position. |
| 1364 | * We know that we're adding the new space even though we have |
| 1365 | * a space preceding, since we wouldn't have gotten this far |
| 1366 | * if we were going to collapse the space with a run of |
| 1367 | * whitespace. |
| 1368 | */ |
| 1369 | } |
| 1370 | else |
| 1371 | { |
| 1372 | /* the line doesn't already end in space, so add the space */ |
| 1373 | linebuf_[linepos_] = ' '; |
| 1374 | flagbuf_[linepos_] = cflags; |
| 1375 | colorbuf_[linepos_] = cur_color_; |
| 1376 | |
| 1377 | /* advance one character in the buffer */ |
| 1378 | ++linepos_; |
| 1379 | } |
| 1380 | |
| 1381 | /* |
| 1382 | * Adjust the column position for the added space. Note that we |
| 1383 | * adjust by the rendered width of the new character even though |
| 1384 | * we actually added only one character; we only add one character |
| 1385 | * to the buffer to avoid buffer overflow, but the column position |
| 1386 | * needs adjustment by the full rendered width. The fact that the |
| 1387 | * actual buffer size and rendered width no longer match isn't |
| 1388 | * important because the difference is entirely in invisible |
| 1389 | * whitespace at the right end of the line. |
| 1390 | */ |
| 1391 | linecol_ += cwid; |
| 1392 | |
| 1393 | /* done for now */ |
| 1394 | return; |
| 1395 | } |
| 1396 | |
| 1397 | /* |
| 1398 | * We're adding something other than an ordinary space to the line, |
| 1399 | * and the new character won't fit, so we must find an appropriate |
| 1400 | * point to break the line. |
| 1401 | * |
| 1402 | * First, add the new character to the buffer - it could be |
| 1403 | * significant in how we calculate the break position. (Note that we |
| 1404 | * allocate the buffer with space for one extra character after |
| 1405 | * reaching the maximum line width, so we know we have room for this.) |
| 1406 | */ |
| 1407 | linebuf_[linepos_] = c; |
| 1408 | flagbuf_[linepos_] = cur_flags_; |
| 1409 | |
| 1410 | /* |
| 1411 | * if the underlying OS layer is doing the line wrapping, just flush |
| 1412 | * out the buffer; don't bother trying to do any line wrapping |
| 1413 | * ourselves, since this work would just be redundant with what the OS |
| 1414 | * layer has to do anyway |
| 1415 | */ |
| 1416 | if (os_line_wrap_) |
| 1417 | { |
| 1418 | /* flush the line, adding no padding after it */ |
| 1419 | flush_line(vmg_ FALSE); |
| 1420 | |
| 1421 | /* |
| 1422 | * we've completely cleared out the line buffer, so reset all of |
| 1423 | * the line buffer counters |
| 1424 | */ |
| 1425 | linepos_ = 0; |
| 1426 | linecol_ = 0; |
| 1427 | linebuf_[0] = '\0'; |
| 1428 | is_continuation_ = FALSE; |
| 1429 | |
| 1430 | /* we're done */ |
| 1431 | goto done_with_wrapping; |
| 1432 | } |
| 1433 | |
| 1434 | /* |
| 1435 | * Scan backwards, looking for a break position. Start at the current |
| 1436 | * column: we know we can fit everything up to this point on a line on |
| 1437 | * the underlying display, so this is the rightmost possible position |
| 1438 | * at which we could break the line. Keep going until we find a |
| 1439 | * breaking point or reach the left edge of the line. |
| 1440 | */ |
| 1441 | int shy, i; |
| 1442 | for (i = linepos_, shy = FALSE ; i >= 0 ; --i) |
| 1443 | { |
| 1444 | unsigned char f; |
| 1445 | unsigned char prvf; |
| 1446 | |
| 1447 | /* |
| 1448 | * There are two break modes: word-break mode and break-anywhere |
| 1449 | * mode. The modes are applied to each character, via the buffer |
| 1450 | * flags. |
| 1451 | * |
| 1452 | * In word-break mode, we can break at any ordinary space, at a |
| 1453 | * soft hyphen, just after a regular hyphen, or at any explicit |
| 1454 | * ok-to-break point; but we can't break after any character |
| 1455 | * marked as a no-break point. |
| 1456 | * |
| 1457 | * In break-anywhere mode, we can break between any two |
| 1458 | * characters, except that we can't break after any character |
| 1459 | * marked as a no-break point. |
| 1460 | */ |
| 1461 | |
| 1462 | /* get the current character's flags */ |
| 1463 | f = flagbuf_[i]; |
| 1464 | |
| 1465 | /* get the preceding character's flags */ |
| 1466 | prvf = (i > 0 ? flagbuf_[i-1] : 0); |
| 1467 | |
| 1468 | /* |
| 1469 | * if the preceding character is marked as a no-break point, we |
| 1470 | * definitely can't break here, so keep looking |
| 1471 | */ |
| 1472 | if ((prvf & VMCON_OBF_NOBREAK) != 0) |
| 1473 | continue; |
| 1474 | |
| 1475 | /* |
| 1476 | * if the preceding character is marked as an explicit ok-to-break |
| 1477 | * point, we definitely can break here |
| 1478 | */ |
| 1479 | if ((prvf & VMCON_OBF_OKBREAK) != 0) |
| 1480 | break; |
| 1481 | |
| 1482 | /* |
| 1483 | * If the current character is in a run of break-anywhere text, |
| 1484 | * then we can insert a break just before the current character. |
| 1485 | * Likewise, if the preceding character is in a run of |
| 1486 | * break-anywhere text, we can break just after the preceding |
| 1487 | * character, which is the same as breaking just before the |
| 1488 | * current character. |
| 1489 | * |
| 1490 | * Note that we must test for both cases to properly handle |
| 1491 | * boundaries between break-anywhere and word-break text. If |
| 1492 | * we're switching from word-break to break-anywhere text, the |
| 1493 | * current character will be marked as break-anywhere, so if we |
| 1494 | * only tested the previous character, we'd miss this transition. |
| 1495 | * If we're switching from break-anywhere to word-break text, the |
| 1496 | * previous character will be marked as break-anywhere, so we'd |
| 1497 | * miss the fact that we could break right here (rather than |
| 1498 | * before the previous character) if we didn't test it explicitly. |
| 1499 | */ |
| 1500 | if ((f & VMCON_OBF_BREAK_ANY) != 0 |
| 1501 | || (i > 0 && (prvf & VMCON_OBF_BREAK_ANY) != 0)) |
| 1502 | break; |
| 1503 | |
| 1504 | /* |
| 1505 | * If the preceding character is marked as a soft hyphenation |
| 1506 | * point, and we're not at the rightmost position, we can break |
| 1507 | * here with hyphenation. We can't break with hyphenation at the |
| 1508 | * last position because hyphenation requires us to actually |
| 1509 | * insert a hyphen character, and we know that at the last |
| 1510 | * position we don't have room for inserting another character. |
| 1511 | */ |
| 1512 | if (i > 0 && i < linepos_ && (prvf & VMCON_OBF_SHY) != 0) |
| 1513 | { |
| 1514 | /* note that we're breaking at a soft hyphen */ |
| 1515 | shy = TRUE; |
| 1516 | |
| 1517 | /* we can break here */ |
| 1518 | break; |
| 1519 | } |
| 1520 | |
| 1521 | /* |
| 1522 | * we can break to the left of a space (i.e., we can break before |
| 1523 | * the current character if the current character is a space) |
| 1524 | */ |
| 1525 | if (linebuf_[i] == ' ') |
| 1526 | break; |
| 1527 | |
| 1528 | /* |
| 1529 | * We can also break to the right of a space. We need to check |
| 1530 | * for this case separately from checking that the current |
| 1531 | * charatcer is a space (which breaks to the left of the space), |
| 1532 | * because we could have a no-break marker on one side of the |
| 1533 | * space but not on the other side. |
| 1534 | */ |
| 1535 | if (i > 0 && linebuf_[i-1] == ' ') |
| 1536 | break; |
| 1537 | |
| 1538 | /* |
| 1539 | * If we're to the right of a hyphen, we can break here. However, |
| 1540 | * don't break in the middle of a set of consecutive hyphens |
| 1541 | * (i.e., we don't want to break up "--" sequences). |
| 1542 | */ |
| 1543 | if (i > 0 && linebuf_[i-1] == '-' && linebuf_[i] != '-') |
| 1544 | break; |
| 1545 | } |
| 1546 | |
| 1547 | /* check to see if we found a good place to break */ |
| 1548 | if (i < 0) |
| 1549 | { |
| 1550 | /* |
| 1551 | * We didn't find a good place to break. If the underlying |
| 1552 | * console allows overrunning the line width, simply add the |
| 1553 | * character, even though it overflows; otherwise, force a break |
| 1554 | * at the line width, even though it doesn't occur at a natural |
| 1555 | * breaking point. |
| 1556 | * |
| 1557 | * In any case, don't let our buffer fill up beyond its maximum |
| 1558 | * size. |
| 1559 | */ |
| 1560 | if (!console_->allow_overrun() || linepos_ + 1 >= OS_MAXWIDTH) |
| 1561 | { |
| 1562 | /* |
| 1563 | * we didn't find any good place to break, and the console |
| 1564 | * doesn't allow us to overrun the terminal width - flush the |
| 1565 | * entire line as-is, breaking arbitrarily in the middle of a |
| 1566 | * word |
| 1567 | */ |
| 1568 | flush_line(vmg_ FALSE); |
| 1569 | |
| 1570 | /* |
| 1571 | * we've completely cleared out the line buffer, so reset all |
| 1572 | * of the line buffer counters |
| 1573 | */ |
| 1574 | linepos_ = 0; |
| 1575 | linecol_ = 0; |
| 1576 | linebuf_[0] = '\0'; |
| 1577 | is_continuation_ = FALSE; |
| 1578 | } |
| 1579 | } |
| 1580 | else |
| 1581 | { |
| 1582 | wchar_t tmpbuf[OS_MAXWIDTH]; |
| 1583 | vmcon_color_t tmpcolor[OS_MAXWIDTH]; |
| 1584 | unsigned char tmpflags[OS_MAXWIDTH]; |
| 1585 | size_t tmpchars; |
| 1586 | int nxti; |
| 1587 | |
| 1588 | /* null-terminate the line buffer */ |
| 1589 | linebuf_[linepos_] = '\0'; |
| 1590 | |
| 1591 | /* trim off leading spaces on the next line after the break */ |
| 1592 | for (nxti = i ; linebuf_[nxti] == ' ' ; ++nxti) ; |
| 1593 | |
| 1594 | /* |
| 1595 | * The next line starts after the break - save a copy. We actually |
| 1596 | * have to save a copy of the trailing material outside the buffer, |
| 1597 | * since we might have to overwrite the trailing part of the buffer |
| 1598 | * to expand tabs. |
| 1599 | */ |
| 1600 | tmpchars = wcslen(&linebuf_[nxti]); |
| 1601 | memcpy(tmpbuf, &linebuf_[nxti], tmpchars*sizeof(tmpbuf[0])); |
| 1602 | memcpy(tmpcolor, &colorbuf_[nxti], tmpchars*sizeof(tmpcolor[0])); |
| 1603 | memcpy(tmpflags, &flagbuf_[nxti], tmpchars*sizeof(tmpflags[0])); |
| 1604 | |
| 1605 | /* if we're breaking at a soft hyphen, insert a real hyphen */ |
| 1606 | if (shy) |
| 1607 | linebuf_[i++] = '-'; |
| 1608 | |
| 1609 | /* trim off trailing spaces */ |
| 1610 | for ( ; i > 0 && linebuf_[i-1] == ' ' ; --i) |
| 1611 | { |
| 1612 | /* stop if we've reached a non-breaking point */ |
| 1613 | if ((flagbuf_[i-1] & VMCON_OBF_NOBREAK) != 0) |
| 1614 | break; |
| 1615 | } |
| 1616 | |
| 1617 | /* terminate the buffer after the break point */ |
| 1618 | linebuf_[i] = '\0'; |
| 1619 | |
| 1620 | /* write out everything up to the break point */ |
| 1621 | flush_line(vmg_ TRUE); |
| 1622 | |
| 1623 | /* move the saved start of the next line into the line buffer */ |
| 1624 | memcpy(linebuf_, tmpbuf, tmpchars*sizeof(tmpbuf[0])); |
| 1625 | memcpy(colorbuf_, tmpcolor, tmpchars*sizeof(tmpcolor[0])); |
| 1626 | memcpy(flagbuf_, tmpflags, tmpchars*sizeof(tmpflags[0])); |
| 1627 | linecol_ = linepos_ = tmpchars; |
| 1628 | } |
| 1629 | |
| 1630 | done_with_wrapping: |
| 1631 | /* add the new character to buffer */ |
| 1632 | buffer_rendered(c, cflags, cwid); |
| 1633 | } |
| 1634 | |
| 1635 | /* |
| 1636 | * Write a rendered character to an output stream buffer. This is a |
| 1637 | * low-level internal routine that we call from buffer_expchar() to put |
| 1638 | * the final rendition of a character into a buffer. |
| 1639 | * |
| 1640 | * Some characters render as multiple copies of a single character; 'wid' |
| 1641 | * gives the number of copies to store. The caller is responsible for |
| 1642 | * ensuring that the rendered representation fits in the buffer and in the |
| 1643 | * available line width. |
| 1644 | */ |
| 1645 | void CVmFormatter::buffer_rendered(wchar_t c, unsigned char flags, int wid) |
| 1646 | { |
| 1647 | unsigned char flags_before; |
| 1648 | |
| 1649 | /* note whether or not we have a break before us */ |
| 1650 | flags_before = (linepos_ > 0 |
| 1651 | ? flagbuf_[linepos_-1] & VMCON_OBF_NOBREAK |
| 1652 | : 0); |
| 1653 | |
| 1654 | /* add the character the given number of times */ |
| 1655 | for ( ; wid != 0 ; --wid) |
| 1656 | { |
| 1657 | /* buffer the character */ |
| 1658 | linebuf_[linepos_] = c; |
| 1659 | flagbuf_[linepos_] = flags; |
| 1660 | colorbuf_[linepos_] = cur_color_; |
| 1661 | |
| 1662 | /* |
| 1663 | * if this isn't the last part of the character, carry forward any |
| 1664 | * no-break flag from the previous part of the character; this will |
| 1665 | * ensure that a no-break to the left of the sequence applies to |
| 1666 | * the entire sequence |
| 1667 | */ |
| 1668 | if (wid > 1) |
| 1669 | flagbuf_[linepos_] |= flags_before; |
| 1670 | |
| 1671 | /* advance one character in the buffer */ |
| 1672 | ++linepos_; |
| 1673 | |
| 1674 | /* adjust our column counter */ |
| 1675 | ++linecol_; |
| 1676 | } |
| 1677 | } |
| 1678 | |
| 1679 | /* ------------------------------------------------------------------------ */ |
| 1680 | /* |
| 1681 | * write out a UTF-8 string |
| 1682 | */ |
| 1683 | void CVmFormatter::buffer_string(VMG_ const char *txt) |
| 1684 | { |
| 1685 | /* write out each character in the string */ |
| 1686 | for ( ; utf8_ptr::s_getch(txt) != 0 ; txt += utf8_ptr::s_charsize(*txt)) |
| 1687 | buffer_char(vmg_ utf8_ptr::s_getch(txt)); |
| 1688 | } |
| 1689 | |
| 1690 | /* |
| 1691 | * write out a wide unicode string |
| 1692 | */ |
| 1693 | void CVmFormatter::buffer_wstring(VMG_ const wchar_t *txt) |
| 1694 | { |
| 1695 | /* write out each wide character */ |
| 1696 | for ( ; *txt != L'\0' ; ++txt) |
| 1697 | buffer_char(vmg_ *txt); |
| 1698 | } |
| 1699 | |
| 1700 | |
| 1701 | /* ------------------------------------------------------------------------ */ |
| 1702 | /* |
| 1703 | * Get the next wide unicode character in a UTF8-encoded string, and |
| 1704 | * update the string pointer and remaining length. Returns zero if no |
| 1705 | * more characters are available in the string. |
| 1706 | */ |
| 1707 | wchar_t CVmFormatter::next_wchar(const char **s, size_t *len) |
| 1708 | { |
| 1709 | wchar_t ret; |
| 1710 | size_t charsize; |
| 1711 | |
| 1712 | /* if there's nothing left, return a null terminator */ |
| 1713 | if (*len == 0) |
| 1714 | return 0; |
| 1715 | |
| 1716 | /* get this character */ |
| 1717 | ret = utf8_ptr::s_getch(*s); |
| 1718 | |
| 1719 | /* advance the string pointer and length counter */ |
| 1720 | charsize = utf8_ptr::s_charsize(**s); |
| 1721 | *len -= charsize; |
| 1722 | *s += charsize; |
| 1723 | |
| 1724 | /* render embedded null bytes as spaces */ |
| 1725 | if (ret == 0) |
| 1726 | ret = ' '; |
| 1727 | |
| 1728 | /* return the result */ |
| 1729 | return ret; |
| 1730 | } |
| 1731 | |
| 1732 | /* ------------------------------------------------------------------------ */ |
| 1733 | /* |
| 1734 | * Display a string of a given length. The text is encoded as UTF-8 |
| 1735 | * characters. |
| 1736 | */ |
| 1737 | int CVmFormatter::format_text(VMG_ const char *s, size_t slen) |
| 1738 | { |
| 1739 | /* get the first character */ |
| 1740 | wchar_t c = next_wchar(&s, &slen); |
| 1741 | |
| 1742 | /* if we have anything to show, show it */ |
| 1743 | while (c != '\0') |
| 1744 | { |
| 1745 | /* |
| 1746 | * first, process the character through our built-in text-only HTML |
| 1747 | * mini-parser, if our HTML mini-parser state indicates that we're |
| 1748 | * in the midst of parsing a tag |
| 1749 | */ |
| 1750 | if (html_parse_state_ != VMCON_HPS_NORMAL |
| 1751 | || (html_in_ignore_ && c != '&' && c != '<')) |
| 1752 | { |
| 1753 | /* run our HTML parsing until we finish the tag */ |
| 1754 | c = resume_html_parsing(vmg_ c, &s, &slen); |
| 1755 | |
| 1756 | /* proceed with the next character */ |
| 1757 | continue; |
| 1758 | } |
| 1759 | |
| 1760 | /* check for special characters */ |
| 1761 | switch(c) |
| 1762 | { |
| 1763 | case 10: |
| 1764 | /* newline */ |
| 1765 | flush(vmg_ VM_NL_NEWLINE); |
| 1766 | break; |
| 1767 | |
| 1768 | case 9: |
| 1769 | /* tab - write an ordinary every-4-columns tab */ |
| 1770 | write_tab(vmg_ 0, 4); |
| 1771 | break; |
| 1772 | |
| 1773 | case 0x000B: |
| 1774 | /* \b - blank line */ |
| 1775 | write_blank_line(vmg0_); |
| 1776 | break; |
| 1777 | |
| 1778 | case 0x000F: |
| 1779 | /* capitalize next character */ |
| 1780 | capsflag_ = TRUE; |
| 1781 | nocapsflag_ = FALSE; |
| 1782 | break; |
| 1783 | |
| 1784 | case 0x000E: |
| 1785 | /* un-capitalize next character */ |
| 1786 | nocapsflag_ = TRUE; |
| 1787 | capsflag_ = FALSE; |
| 1788 | break; |
| 1789 | |
| 1790 | case '<': |
| 1791 | case '&': |
| 1792 | /* HTML markup-start character - process it */ |
| 1793 | if (html_target_ || literal_mode_) |
| 1794 | { |
| 1795 | /* |
| 1796 | * The underlying OS renderer interprets HTML mark-up |
| 1797 | * sequences, OR we're processing all text literally; in |
| 1798 | * either case, we don't need to perform any |
| 1799 | * interpretation. Simply pass through the character as |
| 1800 | * though it were any other. |
| 1801 | */ |
| 1802 | goto normal_char; |
| 1803 | } |
| 1804 | else |
| 1805 | { |
| 1806 | /* |
| 1807 | * The underlying target does not accept HTML sequences. |
| 1808 | * It appears we're at the start of an "&" entity or a tag |
| 1809 | * sequence, so parse it, remove it, and replace it (if |
| 1810 | * possible) with a text-only equivalent. |
| 1811 | */ |
| 1812 | c = parse_html_markup(vmg_ c, &s, &slen); |
| 1813 | |
| 1814 | /* go back and process the next character */ |
| 1815 | continue; |
| 1816 | } |
| 1817 | break; |
| 1818 | |
| 1819 | case 0x0015: /* our own quoted space character */ |
| 1820 | case 0x00A0: /* non-breaking space */ |
| 1821 | case 0x00AD: /* soft hyphen */ |
| 1822 | case 0xFEFF: /* non-breaking zero-width space */ |
| 1823 | case 0x2002: /* en space */ |
| 1824 | case 0x2003: /* em space */ |
| 1825 | case 0x2004: /* three-per-em space */ |
| 1826 | case 0x2005: /* four-per-em space */ |
| 1827 | case 0x2006: /* six-per-em space */ |
| 1828 | case 0x2007: /* figure space */ |
| 1829 | case 0x2008: /* punctuation space */ |
| 1830 | case 0x2009: /* thin space */ |
| 1831 | case 0x200a: /* hair space */ |
| 1832 | case 0x200b: /* zero-width space */ |
| 1833 | /* |
| 1834 | * Special Unicode characters. For HTML targets, write these |
| 1835 | * as &# sequences - this bypasses character set translation |
| 1836 | * and ensures that the HTML parser will see them as intended. |
| 1837 | */ |
| 1838 | if (html_target_) |
| 1839 | { |
| 1840 | char buf[15]; |
| 1841 | char *p; |
| 1842 | |
| 1843 | /* |
| 1844 | * it's an HTML target - render these as &# sequences; |
| 1845 | * generate the decimal representation of 'c' (in reverse |
| 1846 | * order, hence start with the terminating null byte and |
| 1847 | * the semicolon) |
| 1848 | */ |
| 1849 | p = buf + sizeof(buf) - 1; |
| 1850 | *p-- = '\0'; |
| 1851 | *p-- = ';'; |
| 1852 | |
| 1853 | /* generate the decimal representation of 'c' */ |
| 1854 | for ( ; c != 0 ; c /= 10) |
| 1855 | *p-- = (c % 10) + '0'; |
| 1856 | |
| 1857 | /* add the '&#' sequence */ |
| 1858 | *p-- = '#'; |
| 1859 | *p = '&'; |
| 1860 | |
| 1861 | /* write out the sequence */ |
| 1862 | buffer_string(vmg_ p); |
| 1863 | } |
| 1864 | else |
| 1865 | { |
| 1866 | /* for non-HTML targets, treat these as normal */ |
| 1867 | goto normal_char; |
| 1868 | } |
| 1869 | break; |
| 1870 | |
| 1871 | default: |
| 1872 | normal_char: |
| 1873 | /* normal character - write it out */ |
| 1874 | buffer_char(vmg_ c); |
| 1875 | break; |
| 1876 | } |
| 1877 | |
| 1878 | /* move on to the next character */ |
| 1879 | c = next_wchar(&s, &slen); |
| 1880 | } |
| 1881 | |
| 1882 | /* success */ |
| 1883 | return 0; |
| 1884 | } |
| 1885 | |
| 1886 | /* ------------------------------------------------------------------------ */ |
| 1887 | /* |
| 1888 | * Initialize the display object |
| 1889 | */ |
| 1890 | CVmConsole::CVmConsole() |
| 1891 | { |
| 1892 | /* no script file yet */ |
| 1893 | script_sp_ = 0; |
| 1894 | |
| 1895 | /* no command log file yet */ |
| 1896 | command_fp_ = 0; |
| 1897 | command_nf_ = 0; |
| 1898 | command_glob_ = 0; |
| 1899 | |
| 1900 | /* assume we'll double-space after each period */ |
| 1901 | doublespace_ = TRUE; |
| 1902 | |
| 1903 | /* presume we'll have no log stream */ |
| 1904 | log_str_ = 0; |
| 1905 | log_enabled_ = FALSE; |
| 1906 | } |
| 1907 | |
| 1908 | /* |
| 1909 | * Delete the display object |
| 1910 | */ |
| 1911 | void CVmConsole::delete_obj(VMG0_) |
| 1912 | { |
| 1913 | /* close any active script file(s) */ |
| 1914 | while (script_sp_ != 0) |
| 1915 | close_script_file(vmg0_); |
| 1916 | |
| 1917 | /* close any active command log file */ |
| 1918 | close_command_log(vmg0_); |
| 1919 | |
| 1920 | /* delete the log stream if we have one */ |
| 1921 | if (log_str_ != 0) |
| 1922 | log_str_->delete_obj(vmg0_); |
| 1923 | |
| 1924 | /* delete this object */ |
| 1925 | delete this; |
| 1926 | } |
| 1927 | |
| 1928 | /* ------------------------------------------------------------------------ */ |
| 1929 | /* |
| 1930 | * Display a string of a given byte length |
| 1931 | */ |
| 1932 | int CVmConsole::format_text(VMG_ const char *p, size_t len) |
| 1933 | { |
| 1934 | /* display the string */ |
| 1935 | disp_str_->format_text(vmg_ p, len); |
| 1936 | |
| 1937 | /* if there's a log file, write to the log file as well */ |
| 1938 | if (log_enabled_) |
| 1939 | log_str_->format_text(vmg_ p, len); |
| 1940 | |
| 1941 | /* indicate success */ |
| 1942 | return 0; |
| 1943 | } |
| 1944 | |
| 1945 | /* |
| 1946 | * Display a string on the log stream only |
| 1947 | */ |
| 1948 | int CVmConsole::format_text_to_log(VMG_ const char *p, size_t len) |
| 1949 | { |
| 1950 | /* if there's a log file, write to it; otherwise ignore the whole thing */ |
| 1951 | if (log_enabled_) |
| 1952 | log_str_->format_text(vmg_ p, len); |
| 1953 | |
| 1954 | /* indicate success */ |
| 1955 | return 0; |
| 1956 | } |
| 1957 | |
| 1958 | /* ------------------------------------------------------------------------ */ |
| 1959 | /* |
| 1960 | * Set the text color |
| 1961 | */ |
| 1962 | void CVmConsole::set_text_color(VMG_ os_color_t fg, os_color_t bg) |
| 1963 | { |
| 1964 | /* set the color in our main display stream */ |
| 1965 | disp_str_->set_text_color(vmg_ fg, bg); |
| 1966 | } |
| 1967 | |
| 1968 | /* |
| 1969 | * Set the body color |
| 1970 | */ |
| 1971 | void CVmConsole::set_body_color(VMG_ os_color_t color) |
| 1972 | { |
| 1973 | /* set the color in the main display stream */ |
| 1974 | disp_str_->set_os_body_color(color); |
| 1975 | } |
| 1976 | |
| 1977 | /* ------------------------------------------------------------------------ */ |
| 1978 | /* |
| 1979 | * Display a blank line |
| 1980 | */ |
| 1981 | void CVmConsole::write_blank_line(VMG0_) |
| 1982 | { |
| 1983 | /* generate the newline to the standard display */ |
| 1984 | disp_str_->write_blank_line(vmg0_); |
| 1985 | |
| 1986 | /* if we're logging, generate the newline to the log file as well */ |
| 1987 | if (log_enabled_) |
| 1988 | log_str_->write_blank_line(vmg0_); |
| 1989 | } |
| 1990 | |
| 1991 | |
| 1992 | /* ------------------------------------------------------------------------ */ |
| 1993 | /* |
| 1994 | * outcaps() - sets an internal flag which makes the next letter output |
| 1995 | * a capital, whether it came in that way or not. Set the same state in |
| 1996 | * both formatters (standard and log). |
| 1997 | */ |
| 1998 | void CVmConsole::caps() |
| 1999 | { |
| 2000 | disp_str_->caps(); |
| 2001 | if (log_enabled_) |
| 2002 | log_str_->caps(); |
| 2003 | } |
| 2004 | |
| 2005 | /* |
| 2006 | * outnocaps() - sets the next letter to a miniscule, whether it came in |
| 2007 | * that way or not. |
| 2008 | */ |
| 2009 | void CVmConsole::nocaps() |
| 2010 | { |
| 2011 | disp_str_->nocaps(); |
| 2012 | if (log_enabled_) |
| 2013 | log_str_->nocaps(); |
| 2014 | } |
| 2015 | |
| 2016 | /* |
| 2017 | * obey_whitespace() - sets the obey-whitespace mode |
| 2018 | */ |
| 2019 | int CVmConsole::set_obey_whitespace(int f) |
| 2020 | { |
| 2021 | int ret; |
| 2022 | |
| 2023 | /* note the original display stream status */ |
| 2024 | ret = disp_str_->get_obey_whitespace(); |
| 2025 | |
| 2026 | /* set the stream status */ |
| 2027 | disp_str_->set_obey_whitespace(f); |
| 2028 | if (log_enabled_) |
| 2029 | log_str_->set_obey_whitespace(f); |
| 2030 | |
| 2031 | /* return the original status of the display stream */ |
| 2032 | return ret; |
| 2033 | } |
| 2034 | |
| 2035 | /* ------------------------------------------------------------------------ */ |
| 2036 | /* |
| 2037 | * Open a log file |
| 2038 | */ |
| 2039 | int CVmConsole::open_log_file(VMG_ const char *fname) |
| 2040 | { |
| 2041 | /* pass the file to the log stream, if we have one */ |
| 2042 | return log_str_ != 0 ? log_str_->open_log_file(vmg_ fname) : 1; |
| 2043 | } |
| 2044 | |
| 2045 | int CVmConsole::open_log_file(VMG_ const vm_val_t *filespec, |
| 2046 | const struct vm_rcdesc *rc) |
| 2047 | { |
| 2048 | /* pass the file to the log stream, if we have one */ |
| 2049 | return log_str_ != 0 ? log_str_->open_log_file(vmg_ filespec, rc) : 1; |
| 2050 | } |
| 2051 | |
| 2052 | /* |
| 2053 | * Close the log file |
| 2054 | */ |
| 2055 | int CVmConsole::close_log_file(VMG0_) |
| 2056 | { |
| 2057 | /* if there's no log stream, there's obviously no file open */ |
| 2058 | if (log_str_ == 0) |
| 2059 | return 1; |
| 2060 | |
| 2061 | /* tell the log stream to close its file */ |
| 2062 | return log_str_->close_log_file(vmg0_); |
| 2063 | } |
| 2064 | |
| 2065 | #if 0 //$$$ |
| 2066 | /* |
| 2067 | * This code is currently unused. However, I'm leaving it in for now - |
| 2068 | * the algorithm takes a little thought, so it would be nicer to be able |
| 2069 | * to uncomment the existing code should we ever need it in the future. |
| 2070 | */ |
| 2071 | |
| 2072 | /* ------------------------------------------------------------------------ */ |
| 2073 | /* |
| 2074 | * Write UTF-8 text explicitly to the log file. This can be used to add |
| 2075 | * special text (such as prompt text) that would normally be suppressed |
| 2076 | * from the log file. When more mode is turned off, we don't |
| 2077 | * automatically copy text to the log file; any text that the caller |
| 2078 | * knows should be in the log file during times when more mode is turned |
| 2079 | * off can be explicitly added with this function. |
| 2080 | * |
| 2081 | * If nl is true, we'll add a newline at the end of this text. The |
| 2082 | * caller should not include any newlines in the text being displayed |
| 2083 | * here. |
| 2084 | */ |
| 2085 | void CVmConsole::write_to_logfile(VMG_ const char *txt, int nl) |
| 2086 | { |
| 2087 | /* if there's no log file, there's nothing to do */ |
| 2088 | if (logfp_ == 0) |
| 2089 | return; |
| 2090 | |
| 2091 | /* write the text in the log file character set */ |
| 2092 | write_to_file(logfp_, txt, G_cmap_to_log); |
| 2093 | |
| 2094 | /* add a newline if desired */ |
| 2095 | if (nl) |
| 2096 | { |
| 2097 | /* add a normal newline */ |
| 2098 | os_fprintz(logfp_, "\n"); |
| 2099 | |
| 2100 | /* if the logfile is an html target, write an HTML line break */ |
| 2101 | if (log_str_ != 0 && log_str_->is_html_target()) |
| 2102 | os_fprintz(logfp_, "<BR HEIGHT=0>\n"); |
| 2103 | } |
| 2104 | |
| 2105 | /* flush the output */ |
| 2106 | osfflush(logfp_); |
| 2107 | } |
| 2108 | |
| 2109 | /* |
| 2110 | * Write text to a file in the given character set |
| 2111 | */ |
| 2112 | void CVmConsole::write_to_file(osfildef *fp, const char *txt, |
| 2113 | CCharmapToLocal *map) |
| 2114 | { |
| 2115 | size_t txtlen = strlen(txt); |
| 2116 | |
| 2117 | /* |
| 2118 | * convert the text from UTF-8 to the local character set and write the |
| 2119 | * converted text to the log file |
| 2120 | */ |
| 2121 | while (txtlen != 0) |
| 2122 | { |
| 2123 | char local_buf[128]; |
| 2124 | size_t src_bytes_used; |
| 2125 | size_t out_bytes; |
| 2126 | |
| 2127 | /* convert as much as we can (leaving room for a null terminator) */ |
| 2128 | out_bytes = map->map_utf8(local_buf, sizeof(local_buf), |
| 2129 | txt, txtlen, &src_bytes_used); |
| 2130 | |
| 2131 | /* null-terminate the result */ |
| 2132 | local_buf[out_bytes] = '\0'; |
| 2133 | |
| 2134 | /* write the converted text */ |
| 2135 | os_fprintz(fp, local_buf); |
| 2136 | |
| 2137 | /* skip the text we were able to convert */ |
| 2138 | txt += src_bytes_used; |
| 2139 | txtlen -= src_bytes_used; |
| 2140 | } |
| 2141 | } |
| 2142 | #endif /* 0 */ |
| 2143 | |
| 2144 | |
| 2145 | /* ------------------------------------------------------------------------ */ |
| 2146 | /* |
| 2147 | * Reset the MORE line counter. This should be called whenever user |
| 2148 | * input is read, since stopping to read user input makes it unnecessary |
| 2149 | * to show another MORE prompt until the point at which input was |
| 2150 | * solicited scrolls off the screen. |
| 2151 | */ |
| 2152 | void CVmConsole::reset_line_count(int clearing) |
| 2153 | { |
| 2154 | /* reset the MORE counter in the display stream */ |
| 2155 | disp_str_->reset_line_count(clearing); |
| 2156 | } |
| 2157 | |
| 2158 | /* ------------------------------------------------------------------------ */ |
| 2159 | /* |
| 2160 | * Flush the output line. We'll write to both the standard display and |
| 2161 | * the log file, as needed. |
| 2162 | */ |
| 2163 | void CVmConsole::flush(VMG_ vm_nl_type nl) |
| 2164 | { |
| 2165 | /* flush the display stream */ |
| 2166 | disp_str_->flush(vmg_ nl); |
| 2167 | |
| 2168 | /* flush the log stream, if we have an open log file */ |
| 2169 | if (log_enabled_) |
| 2170 | log_str_->flush(vmg_ nl); |
| 2171 | } |
| 2172 | |
| 2173 | /* ------------------------------------------------------------------------ */ |
| 2174 | /* |
| 2175 | * Clear our buffers |
| 2176 | */ |
| 2177 | void CVmConsole::empty_buffers(VMG0_) |
| 2178 | { |
| 2179 | /* tell the formatter to clear its buffer */ |
| 2180 | disp_str_->empty_buffers(vmg0_); |
| 2181 | |
| 2182 | /* same with the log stream, if applicable */ |
| 2183 | if (log_enabled_) |
| 2184 | log_str_->empty_buffers(vmg0_); |
| 2185 | } |
| 2186 | |
| 2187 | /* ------------------------------------------------------------------------ */ |
| 2188 | /* |
| 2189 | * Immediately update the display |
| 2190 | */ |
| 2191 | void CVmConsole::update_display(VMG0_) |
| 2192 | { |
| 2193 | /* update the display for the main display stream */ |
| 2194 | disp_str_->update_display(vmg0_); |
| 2195 | } |
| 2196 | |
| 2197 | /* ------------------------------------------------------------------------ */ |
| 2198 | /* |
| 2199 | * Open a script file |
| 2200 | */ |
| 2201 | int CVmConsole::open_script_file(VMG_ const char *fname, |
| 2202 | int quiet, int script_more_mode) |
| 2203 | { |
| 2204 | CVmNetFile *nf = 0; |
| 2205 | err_try |
| 2206 | { |
| 2207 | /* create the network file descriptor */ |
| 2208 | nf = CVmNetFile::open( |
| 2209 | vmg_ fname, 0, NETF_READ, OSFTCMD, "text/plain"); |
| 2210 | } |
| 2211 | err_catch_disc |
| 2212 | { |
| 2213 | /* failed - no network file */ |
| 2214 | nf = 0; |
| 2215 | } |
| 2216 | err_end; |
| 2217 | |
| 2218 | /* open the script on the network file */ |
| 2219 | return open_script_file(vmg_ nf, 0, quiet, script_more_mode); |
| 2220 | } |
| 2221 | |
| 2222 | int CVmConsole::open_script_file(VMG_ const vm_val_t *filespec, |
| 2223 | const struct vm_rcdesc *rc, |
| 2224 | int quiet, int script_more_mode) |
| 2225 | { |
| 2226 | CVmNetFile *nf = 0; |
| 2227 | err_try |
| 2228 | { |
| 2229 | /* create the network file descriptor */ |
| 2230 | nf = CVmNetFile::open( |
| 2231 | vmg_ filespec, rc, NETF_READ, OSFTCMD, "text/plain"); |
| 2232 | |
| 2233 | /* validate file safety */ |
| 2234 | CVmObjFile::check_safety_for_open(vmg_ nf, VMOBJFILE_ACCESS_READ); |
| 2235 | } |
| 2236 | err_catch_disc |
| 2237 | { |
| 2238 | /* if we got a file, it must be a safety exception - rethrow it */ |
| 2239 | if (nf != 0) |
| 2240 | { |
| 2241 | nf->abandon(vmg0_); |
| 2242 | err_rethrow(); |
| 2243 | } |
| 2244 | } |
| 2245 | err_end; |
| 2246 | |
| 2247 | /* open the script on the network file */ |
| 2248 | return open_script_file(vmg_ nf, filespec, quiet, script_more_mode); |
| 2249 | } |
| 2250 | |
| 2251 | int CVmConsole::open_script_file(VMG_ CVmNetFile *nf, const vm_val_t *filespec, |
| 2252 | int quiet, int script_more_mode) |
| 2253 | { |
| 2254 | int evt; |
| 2255 | char buf[50]; |
| 2256 | |
| 2257 | /* if the network file open failed, return an error */ |
| 2258 | if (nf == 0) |
| 2259 | return 1; |
| 2260 | |
| 2261 | /* open the local file */ |
| 2262 | osfildef *fp = osfoprt(nf->lclfname, OSFTCMD); |
| 2263 | |
| 2264 | /* if that failed, silently ignore the request */ |
| 2265 | if (fp == 0) |
| 2266 | { |
| 2267 | /* abandon the network file descriptor and return failure */ |
| 2268 | nf->abandon(vmg0_); |
| 2269 | return 1; |
| 2270 | } |
| 2271 | |
| 2272 | /* read the first line to see if it looks like an event script */ |
| 2273 | if (osfgets(buf, sizeof(buf), fp) != 0 |
| 2274 | && strcmp(buf, "<eventscript>\n") == 0) |
| 2275 | { |
| 2276 | /* remember that it's an event script */ |
| 2277 | evt = TRUE; |
| 2278 | } |
| 2279 | else |
| 2280 | { |
| 2281 | /* |
| 2282 | * it's not an event script, so it must be a regular command-line |
| 2283 | * script - rewind it so we read the first line again as a regular |
| 2284 | * input line |
| 2285 | */ |
| 2286 | evt = FALSE; |
| 2287 | osfseek(fp, 0, OSFSK_SET); |
| 2288 | } |
| 2289 | |
| 2290 | /* if there's an enclosing script, inherit its modes */ |
| 2291 | if (script_sp_ != 0) |
| 2292 | { |
| 2293 | /* |
| 2294 | * if the enclosing script is quiet, force the nested script to be |
| 2295 | * quiet as well |
| 2296 | */ |
| 2297 | if (script_sp_->quiet) |
| 2298 | quiet = TRUE; |
| 2299 | |
| 2300 | /* |
| 2301 | * if the enclosing script is nonstop, force the nested script to |
| 2302 | * be nonstop as well |
| 2303 | */ |
| 2304 | if (!script_sp_->more_mode) |
| 2305 | script_more_mode = FALSE; |
| 2306 | } |
| 2307 | |
| 2308 | /* push the new script file onto the stack */ |
| 2309 | script_sp_ = new script_stack_entry( |
| 2310 | vmg_ script_sp_, set_more_state(script_more_mode), nf, fp, filespec, |
| 2311 | script_more_mode, quiet, evt); |
| 2312 | |
| 2313 | /* turn on NONSTOP mode in the OS layer if applicable */ |
| 2314 | if (!script_more_mode) |
| 2315 | os_nonstop_mode(TRUE); |
| 2316 | |
| 2317 | /* success */ |
| 2318 | return 0; |
| 2319 | } |
| 2320 | |
| 2321 | /* |
| 2322 | * Close the current script file |
| 2323 | */ |
| 2324 | int CVmConsole::close_script_file(VMG0_) |
| 2325 | { |
| 2326 | script_stack_entry *e; |
| 2327 | |
| 2328 | /* if we have a file, close it */ |
| 2329 | if ((e = script_sp_) != 0) |
| 2330 | { |
| 2331 | int ret; |
| 2332 | |
| 2333 | /* close the file */ |
| 2334 | osfcls(e->fp); |
| 2335 | |
| 2336 | err_try |
| 2337 | { |
| 2338 | /* close the network file */ |
| 2339 | e->netfile->close(vmg0_); |
| 2340 | } |
| 2341 | err_catch_disc |
| 2342 | { |
| 2343 | /* |
| 2344 | * Ignore any error - since we're reading the file, the chances |
| 2345 | * of anything going wrong are pretty minimal anyway; but even |
| 2346 | * if an error did occur, our interface just isn't set up to do |
| 2347 | * anything meaningful with it. |
| 2348 | */ |
| 2349 | } |
| 2350 | err_end; |
| 2351 | |
| 2352 | /* pop the stack */ |
| 2353 | script_sp_ = e->enc; |
| 2354 | |
| 2355 | /* restore the enclosing level's MORE mode */ |
| 2356 | os_nonstop_mode(!e->old_more_mode); |
| 2357 | |
| 2358 | /* |
| 2359 | * return the MORE mode in effect before we started reading the |
| 2360 | * script file |
| 2361 | */ |
| 2362 | ret = e->old_more_mode; |
| 2363 | |
| 2364 | /* delete the stack level */ |
| 2365 | e->delobj(vmg0_); |
| 2366 | |
| 2367 | /* return the result */ |
| 2368 | return ret; |
| 2369 | } |
| 2370 | else |
| 2371 | { |
| 2372 | /* |
| 2373 | * there's no script file - just return the current MORE mode, |
| 2374 | * since we're not making any changes |
| 2375 | */ |
| 2376 | return is_more_mode(); |
| 2377 | } |
| 2378 | } |
| 2379 | |
| 2380 | /* ------------------------------------------------------------------------ */ |
| 2381 | /* |
| 2382 | * Script stack element |
| 2383 | */ |
| 2384 | |
| 2385 | script_stack_entry::script_stack_entry( |
| 2386 | VMG_ script_stack_entry *encp, int old_more, |
| 2387 | class CVmNetFile *netfile, osfildef *outfp, const vm_val_t *filespec, |
| 2388 | int new_more, int is_quiet, int is_event_script) |
| 2389 | { |
| 2390 | /* remember the stack level settings */ |
| 2391 | this->enc = encp; |
| 2392 | this->old_more_mode = old_more; |
| 2393 | this->netfile = netfile; |
| 2394 | this->fp = outfp; |
| 2395 | this->more_mode = new_more; |
| 2396 | this->quiet = is_quiet; |
| 2397 | this->event_script = is_event_script; |
| 2398 | |
| 2399 | /* if we have a file spec, create a global for it for gc protection */ |
| 2400 | this->filespec = 0; |
| 2401 | if (filespec != 0) |
| 2402 | { |
| 2403 | this->filespec = G_obj_table->create_global_var(); |
| 2404 | this->filespec->val = *filespec; |
| 2405 | } |
| 2406 | } |
| 2407 | |
| 2408 | void script_stack_entry::delobj(VMG0_) |
| 2409 | { |
| 2410 | /* we're done with our file spec global, if we had one */ |
| 2411 | if (filespec != 0) |
| 2412 | G_obj_table->delete_global_var(filespec); |
| 2413 | |
| 2414 | /* delete myself */ |
| 2415 | delete this; |
| 2416 | } |
| 2417 | |
| 2418 | /* ------------------------------------------------------------------------ */ |
| 2419 | /* |
| 2420 | * Open a command log file |
| 2421 | */ |
| 2422 | int CVmConsole::open_command_log(VMG_ const char *fname, int event_script) |
| 2423 | { |
| 2424 | CVmNetFile *nf = 0; |
| 2425 | err_try |
| 2426 | { |
| 2427 | /* create the network file descriptor */ |
| 2428 | nf = CVmNetFile::open(vmg_ fname, 0, NETF_NEW, OSFTCMD, "text/plain"); |
| 2429 | } |
| 2430 | err_catch_disc |
| 2431 | { |
| 2432 | /* failed - no network file */ |
| 2433 | nf = 0; |
| 2434 | } |
| 2435 | err_end; |
| 2436 | |
| 2437 | /* create the command log */ |
| 2438 | return open_command_log(vmg_ nf, event_script); |
| 2439 | } |
| 2440 | |
| 2441 | int CVmConsole::open_command_log(VMG_ const vm_val_t *filespec, |
| 2442 | const struct vm_rcdesc *rc, |
| 2443 | int event_script) |
| 2444 | { |
| 2445 | CVmNetFile *nf = 0; |
| 2446 | err_try |
| 2447 | { |
| 2448 | /* create the network file descriptor */ |
| 2449 | nf = CVmNetFile::open(vmg_ filespec, rc, |
| 2450 | NETF_NEW, OSFTCMD, "text/plain"); |
| 2451 | |
| 2452 | /* validate file safety */ |
| 2453 | CVmObjFile::check_safety_for_open(vmg_ nf, VMOBJFILE_ACCESS_WRITE); |
| 2454 | } |
| 2455 | err_catch_disc |
| 2456 | { |
| 2457 | /* if we got a file, it must be a safety exception - rethrow it */ |
| 2458 | if (nf != 0) |
| 2459 | { |
| 2460 | nf->abandon(vmg0_); |
| 2461 | err_rethrow(); |
| 2462 | } |
| 2463 | } |
| 2464 | err_end; |
| 2465 | |
| 2466 | /* create the command log */ |
| 2467 | int err = open_command_log(vmg_ nf, event_script); |
| 2468 | |
| 2469 | /* if that succeeded, save the filespec in our VM global */ |
| 2470 | if (!err) |
| 2471 | { |
| 2472 | /* create our VM global if we don't have one already */ |
| 2473 | if (command_glob_ == 0) |
| 2474 | command_glob_ = G_obj_table->create_global_var(); |
| 2475 | |
| 2476 | /* remember the file spec */ |
| 2477 | command_glob_->val = *filespec; |
| 2478 | } |
| 2479 | |
| 2480 | /* return the result */ |
| 2481 | return err; |
| 2482 | } |
| 2483 | |
| 2484 | int CVmConsole::open_command_log(VMG_ CVmNetFile *nf, int event_script) |
| 2485 | { |
| 2486 | /* close any existing command log file */ |
| 2487 | close_command_log(vmg0_); |
| 2488 | |
| 2489 | /* if the network file open failed, return failure */ |
| 2490 | if (nf == 0) |
| 2491 | return 1; |
| 2492 | |
| 2493 | /* open the file */ |
| 2494 | osfildef *fp = osfopwt(nf->lclfname, OSFTCMD); |
| 2495 | if (fp != 0) |
| 2496 | { |
| 2497 | /* success - remember the file handle and net descriptor */ |
| 2498 | command_nf_ = nf; |
| 2499 | command_fp_ = new CVmFileSource(fp); |
| 2500 | } |
| 2501 | else |
| 2502 | { |
| 2503 | /* failed - abandon the network file */ |
| 2504 | nf->abandon(vmg0_); |
| 2505 | } |
| 2506 | |
| 2507 | /* note the type */ |
| 2508 | command_eventscript_ = event_script; |
| 2509 | |
| 2510 | /* if it's an event script, write the file type tag */ |
| 2511 | if (event_script && command_fp_ != 0) |
| 2512 | { |
| 2513 | command_fp_->writez("<eventscript>\n"); |
| 2514 | command_fp_->flush(); |
| 2515 | } |
| 2516 | |
| 2517 | /* return success if we successfully opened the file */ |
| 2518 | return (command_fp_ == 0); |
| 2519 | } |
| 2520 | |
| 2521 | /* |
| 2522 | * close the active command log file |
| 2523 | */ |
| 2524 | int CVmConsole::close_command_log(VMG0_) |
| 2525 | { |
| 2526 | /* if there's a command log file, close it */ |
| 2527 | if (command_fp_ != 0) |
| 2528 | { |
| 2529 | /* close the file */ |
| 2530 | delete command_fp_; |
| 2531 | |
| 2532 | /* close the network file */ |
| 2533 | err_try |
| 2534 | { |
| 2535 | command_nf_->close(vmg0_); |
| 2536 | } |
| 2537 | err_catch_disc |
| 2538 | { |
| 2539 | /* |
| 2540 | * ignore any errors - our interface doesn't give us any |
| 2541 | * meaningful way to return error information |
| 2542 | */ |
| 2543 | } |
| 2544 | err_end; |
| 2545 | |
| 2546 | /* forget the file */ |
| 2547 | command_fp_ = 0; |
| 2548 | } |
| 2549 | |
| 2550 | /* clear out our file spec global, if we have one */ |
| 2551 | if (command_glob_ != 0) |
| 2552 | command_glob_->val.set_nil(); |
| 2553 | |
| 2554 | /* success */ |
| 2555 | return 0; |
| 2556 | } |
| 2557 | |
| 2558 | |
| 2559 | /* ------------------------------------------------------------------------ */ |
| 2560 | /* |
| 2561 | * Read a line of input from the console. Fills in the buffer with a |
| 2562 | * null-terminated string in the UTF-8 character set. Returns zero on |
| 2563 | * success, non-zero on end-of-file. |
| 2564 | */ |
| 2565 | int CVmConsole::read_line(VMG_ char *buf, size_t buflen, int bypass_script) |
| 2566 | { |
| 2567 | /* cancel any previous interrupted input */ |
| 2568 | read_line_cancel(vmg_ TRUE); |
| 2569 | |
| 2570 | try_again: |
| 2571 | /* use the timeout version, with no timeout specified */ |
| 2572 | switch(read_line_timeout(vmg_ buf, buflen, 0, FALSE, bypass_script)) |
| 2573 | { |
| 2574 | case OS_EVT_LINE: |
| 2575 | /* success */ |
| 2576 | return 0; |
| 2577 | |
| 2578 | case VMCON_EVT_END_QUIET_SCRIPT: |
| 2579 | /* |
| 2580 | * end of script - we have no way to communicate this result back |
| 2581 | * to our caller, so simply ignore the result and ask for another |
| 2582 | * line |
| 2583 | */ |
| 2584 | goto try_again; |
| 2585 | |
| 2586 | default: |
| 2587 | /* anything else is an error */ |
| 2588 | return 1; |
| 2589 | } |
| 2590 | } |
| 2591 | |
| 2592 | |
| 2593 | /* ------------------------------------------------------------------------ */ |
| 2594 | /* |
| 2595 | * translate an OS_EVT_XXX code to an event script file tag |
| 2596 | */ |
| 2597 | static const char *evt_to_tag(int evt) |
| 2598 | { |
| 2599 | switch (evt) |
| 2600 | { |
| 2601 | case OS_EVT_KEY: |
| 2602 | return "key"; |
| 2603 | |
| 2604 | case OS_EVT_TIMEOUT: |
| 2605 | return "timeout"; |
| 2606 | |
| 2607 | case OS_EVT_HREF: |
| 2608 | return "href"; |
| 2609 | |
| 2610 | case OS_EVT_NOTIMEOUT: |
| 2611 | return "notimeout"; |
| 2612 | |
| 2613 | case OS_EVT_EOF: |
| 2614 | return "eof"; |
| 2615 | |
| 2616 | case OS_EVT_LINE: |
| 2617 | return "line"; |
| 2618 | |
| 2619 | case OS_EVT_COMMAND: |
| 2620 | return "command"; |
| 2621 | |
| 2622 | case VMCON_EVT_END_QUIET_SCRIPT: |
| 2623 | return "endqs"; |
| 2624 | |
| 2625 | case VMCON_EVT_DIALOG: |
| 2626 | return "dialog"; |
| 2627 | |
| 2628 | case VMCON_EVT_FILE: |
| 2629 | return "file"; |
| 2630 | |
| 2631 | default: |
| 2632 | return ""; |
| 2633 | } |
| 2634 | } |
| 2635 | |
| 2636 | /* ------------------------------------------------------------------------ */ |
| 2637 | /* |
| 2638 | * Log an event to the output script. The parameter is in the UI character |
| 2639 | * set. |
| 2640 | */ |
| 2641 | int CVmConsole::log_event(VMG_ int evt, |
| 2642 | const char *param, size_t paramlen, |
| 2643 | int param_is_utf8) |
| 2644 | { |
| 2645 | /* translate the event code to a tag, and log the event */ |
| 2646 | log_event(vmg_ evt_to_tag(evt), param, paramlen, param_is_utf8); |
| 2647 | |
| 2648 | /* return the event type code */ |
| 2649 | return evt; |
| 2650 | } |
| 2651 | |
| 2652 | void CVmConsole::log_event(VMG_ const char *tag, |
| 2653 | const char *param, size_t paramlen, |
| 2654 | int param_is_utf8) |
| 2655 | { |
| 2656 | /* if there's a script file, log the event */ |
| 2657 | if (command_fp_ != 0) |
| 2658 | { |
| 2659 | /* if the tag has < > delimiters, remove them */ |
| 2660 | size_t taglen = strlen(tag); |
| 2661 | if (tag[0] == '<') |
| 2662 | ++tag, --taglen; |
| 2663 | if (taglen != 0 && tag[taglen-1] == '>') |
| 2664 | --taglen; |
| 2665 | |
| 2666 | /* write the event in the proper format for the script type */ |
| 2667 | if (command_eventscript_) |
| 2668 | { |
| 2669 | /* |
| 2670 | * It's an event script, so we write all event types. Check |
| 2671 | * for certain special parameter translations. |
| 2672 | */ |
| 2673 | if (taglen == 3 && memicmp(tag, "key", 3) == 0) |
| 2674 | { |
| 2675 | /* |
| 2676 | * key event - for characters that would look like |
| 2677 | * whitespace characters if we wrote them as-is, translate |
| 2678 | * to [xxx] key names |
| 2679 | */ |
| 2680 | if (param != 0 && paramlen == 1) |
| 2681 | { |
| 2682 | switch (param[0]) |
| 2683 | { |
| 2684 | case '\n': |
| 2685 | param = "[enter]"; |
| 2686 | paramlen = 7; |
| 2687 | break; |
| 2688 | |
| 2689 | case '\t': |
| 2690 | param = "[tab]"; |
| 2691 | paramlen = 5; |
| 2692 | break; |
| 2693 | |
| 2694 | case ' ': |
| 2695 | param = "[space]"; |
| 2696 | paramlen = 7; |
| 2697 | break; |
| 2698 | } |
| 2699 | } |
| 2700 | } |
| 2701 | |
| 2702 | /* |
| 2703 | * if the event doesn't have parameters, ignore any parameter |
| 2704 | * provided by the caller |
| 2705 | */ |
| 2706 | if ((taglen == 9 && memicmp(tag, "notimeout", 9) == 0) |
| 2707 | || (taglen == 3 && memicmp(tag, "eof", 3) == 0)) |
| 2708 | param = 0; |
| 2709 | |
| 2710 | /* if we have a non-empty tag, write the event */ |
| 2711 | if (taglen != 0) |
| 2712 | { |
| 2713 | /* write the tag, in the local character set */ |
| 2714 | G_cmap_to_ui->write_file(command_fp_, "<", 1); |
| 2715 | G_cmap_to_ui->write_file(command_fp_, tag, strlen(tag)); |
| 2716 | G_cmap_to_ui->write_file(command_fp_, ">", 1); |
| 2717 | |
| 2718 | /* add the parameter, if present */ |
| 2719 | if (param != 0) |
| 2720 | { |
| 2721 | if (param_is_utf8) |
| 2722 | G_cmap_to_ui->write_file(command_fp_, param, paramlen); |
| 2723 | else |
| 2724 | command_fp_->write(param, paramlen); |
| 2725 | } |
| 2726 | |
| 2727 | /* add the newline */ |
| 2728 | G_cmap_to_ui->write_file(command_fp_, "\n", 1); |
| 2729 | |
| 2730 | /* flush the output */ |
| 2731 | command_fp_->flush(); |
| 2732 | } |
| 2733 | } |
| 2734 | else |
| 2735 | { |
| 2736 | /* |
| 2737 | * It's a plain old command-line script. If the event is an |
| 2738 | * input-line event, record it; otherwise leave it out, as this |
| 2739 | * script file format can't represent any other event types. |
| 2740 | */ |
| 2741 | if (taglen == 4 && memicmp(tag, "line", 4) == 0 && param != 0) |
| 2742 | { |
| 2743 | /* write the ">" prefix */ |
| 2744 | G_cmap_to_ui->write_file(command_fp_, ">", 1); |
| 2745 | |
| 2746 | /* add the command line */ |
| 2747 | if (param_is_utf8) |
| 2748 | G_cmap_to_ui->write_file(command_fp_, param, paramlen); |
| 2749 | else |
| 2750 | command_fp_->write(param, paramlen); |
| 2751 | |
| 2752 | /* add the newline */ |
| 2753 | G_cmap_to_ui->write_file(command_fp_, "\n", 1); |
| 2754 | |
| 2755 | /* flush the output */ |
| 2756 | command_fp_->flush(); |
| 2757 | } |
| 2758 | } |
| 2759 | } |
| 2760 | } |
| 2761 | |
| 2762 | /* ------------------------------------------------------------------------ */ |
| 2763 | /* |
| 2764 | * Static variables for input state. We keep these statically, because we |
| 2765 | * might need to use the values across a series of read_line_timeout calls |
| 2766 | * if timeouts occur. |
| 2767 | */ |
| 2768 | |
| 2769 | /* original 'more' mode, before input began */ |
| 2770 | static int S_old_more_mode; |
| 2771 | |
| 2772 | /* flag: input is pending from an interrupted read_line_timeout invocation */ |
| 2773 | static int S_read_in_progress; |
| 2774 | |
| 2775 | /* local buffer for reading input lines */ |
| 2776 | static char S_read_buf[256]; |
| 2777 | |
| 2778 | |
| 2779 | /* |
| 2780 | * Read a line of input from the console, with an optional timeout value. |
| 2781 | */ |
| 2782 | int CVmConsole::read_line_timeout(VMG_ char *buf, size_t buflen, |
| 2783 | unsigned long timeout, int use_timeout, |
| 2784 | int bypass_script) |
| 2785 | { |
| 2786 | /* no event yet */ |
| 2787 | int evt = OS_EVT_NONE; |
| 2788 | |
| 2789 | /* we haven't received any script input yet */ |
| 2790 | int got_script_input = FALSE; |
| 2791 | |
| 2792 | /* |
| 2793 | * presume we won't echo the text to the display; in most cases, it |
| 2794 | * will be echoed to the display in the course of reading it from |
| 2795 | * the keyboard |
| 2796 | */ |
| 2797 | int echo_text = FALSE; |
| 2798 | |
| 2799 | /* remember the initial MORE mode */ |
| 2800 | S_old_more_mode = is_more_mode(); |
| 2801 | |
| 2802 | /* |
| 2803 | * If we're not resuming an interrupted read already in progress, |
| 2804 | * initialize some display settings. |
| 2805 | */ |
| 2806 | if (!S_read_in_progress) |
| 2807 | { |
| 2808 | /* |
| 2809 | * Turn off MORE mode if it's on - we don't want a MORE prompt |
| 2810 | * showing up in the midst of user input. |
| 2811 | */ |
| 2812 | S_old_more_mode = set_more_state(FALSE); |
| 2813 | |
| 2814 | /* |
| 2815 | * flush the output; don't start a new line, since we might have |
| 2816 | * displayed a prompt that is to be on the same line with the user |
| 2817 | * input |
| 2818 | */ |
| 2819 | flush_all(vmg_ VM_NL_INPUT); |
| 2820 | } |
| 2821 | |
| 2822 | /* |
| 2823 | * if there's a script file, read from it, unless the caller has |
| 2824 | * specifically asked us to bypass it |
| 2825 | */ |
| 2826 | if (script_sp_ != 0 && !bypass_script) |
| 2827 | { |
| 2828 | read_script: |
| 2829 | /* note whether we're in quiet mode */ |
| 2830 | int was_quiet = script_sp_->quiet; |
| 2831 | |
| 2832 | /* try reading a line from the script file */ |
| 2833 | if (read_line_from_script(S_read_buf, sizeof(S_read_buf), &evt)) |
| 2834 | { |
| 2835 | /* we successfully read input from the script */ |
| 2836 | got_script_input = TRUE; |
| 2837 | |
| 2838 | /* |
| 2839 | * if we're not in quiet mode, make a note to echo the text to |
| 2840 | * the display |
| 2841 | */ |
| 2842 | if (!script_sp_->quiet) |
| 2843 | echo_text = TRUE; |
| 2844 | } |
| 2845 | else |
| 2846 | { |
| 2847 | /* |
| 2848 | * End of script file - return to reading from the enclosing |
| 2849 | * level (i.e., the enclosing script, or the keyboard if this |
| 2850 | * is the outermost script). The return value from |
| 2851 | * close_script_file() is the MORE mode that was in effect |
| 2852 | * before we started reading the script file; we'll use this |
| 2853 | * when we restore the enclosing MORE mode so that we restore |
| 2854 | * the pre-script MORE mode when we return. |
| 2855 | */ |
| 2856 | S_old_more_mode = close_script_file(vmg0_); |
| 2857 | |
| 2858 | /* note the new 'quiet' mode */ |
| 2859 | int is_quiet = (script_sp_ != 0 && script_sp_->quiet); |
| 2860 | |
| 2861 | /* |
| 2862 | * if we're still reading from a script (which means we closed |
| 2863 | * the old script and popped out to an enclosing script), and |
| 2864 | * the 'quiet' mode hasn't changed, simply go back for another |
| 2865 | * read |
| 2866 | */ |
| 2867 | if (script_sp_ != 0 && is_quiet == was_quiet) |
| 2868 | goto read_script; |
| 2869 | |
| 2870 | /* |
| 2871 | * temporarily turn off MORE mode, in case we read from the |
| 2872 | * keyboard |
| 2873 | */ |
| 2874 | set_more_state(FALSE); |
| 2875 | |
| 2876 | /* flush any output we generated while reading the script */ |
| 2877 | flush(vmg_ VM_NL_NONE); |
| 2878 | |
| 2879 | /* |
| 2880 | * If we were in quiet mode but no longer are, let the caller |
| 2881 | * know we've finished reading a script, so that the caller can |
| 2882 | * set up the display properly for reading from the keyboard. |
| 2883 | * |
| 2884 | * If we weren't in quiet mode, we'll simply proceed to the |
| 2885 | * normal keyboard reading; when not in quiet mode, no special |
| 2886 | * display fixup is needed. |
| 2887 | */ |
| 2888 | if (was_quiet && !is_quiet) |
| 2889 | { |
| 2890 | /* return to the old MORE mode */ |
| 2891 | set_more_state(S_old_more_mode); |
| 2892 | |
| 2893 | /* add a blank line to the log file, if necessary */ |
| 2894 | if (log_enabled_) |
| 2895 | log_str_->print_to_os("\n"); |
| 2896 | |
| 2897 | /* note in the streams that we've read an input line */ |
| 2898 | disp_str_->note_input_line(); |
| 2899 | if (log_str_ != 0) |
| 2900 | log_str_->note_input_line(); |
| 2901 | |
| 2902 | /* |
| 2903 | * generate a synthetic "end of script" event to let the |
| 2904 | * caller know we're switching back to regular keyboard |
| 2905 | * reading |
| 2906 | */ |
| 2907 | return log_event(vmg_ VMCON_EVT_END_QUIET_SCRIPT); |
| 2908 | } |
| 2909 | |
| 2910 | /* |
| 2911 | * Note that we do not have an event yet - we've merely closed |
| 2912 | * the script file, and now we're going to continue by reading |
| 2913 | * a line from the keyboard instead. The call to |
| 2914 | * close_script_file() above will have left script_sp_ == 0, so |
| 2915 | * we'll shortly read an event from the keyboard. Thus 'evt' |
| 2916 | * is still not set to any value, because we do not yet have an |
| 2917 | * event - this is intentional. |
| 2918 | */ |
| 2919 | } |
| 2920 | } |
| 2921 | |
| 2922 | /* |
| 2923 | * if we're not reading from a scripot, reset the MORE line counter, |
| 2924 | * since we're reading user input at the current point and shouldn't |
| 2925 | * pause for a MORE prompt until the text we're reading has scrolled |
| 2926 | * off the screen |
| 2927 | */ |
| 2928 | if (script_sp_ == 0) |
| 2929 | reset_line_count(FALSE); |
| 2930 | |
| 2931 | /* reading is now in progress */ |
| 2932 | S_read_in_progress = TRUE; |
| 2933 | |
| 2934 | /* if we didn't get input from a script, read from the keyboard */ |
| 2935 | if (!got_script_input) |
| 2936 | { |
| 2937 | /* |
| 2938 | * If we're in network mode, return EOF to indicate that no console |
| 2939 | * input is available. Network programs can only call this routine |
| 2940 | * to read script input, and aren't allowed to read from the |
| 2941 | * regular keyboard. |
| 2942 | */ |
| 2943 | if (G_net_config != 0) |
| 2944 | { |
| 2945 | read_line_done(vmg0_); |
| 2946 | return OS_EVT_EOF; |
| 2947 | } |
| 2948 | |
| 2949 | /* read a line from the keyboard */ |
| 2950 | evt = os_gets_timeout((uchar *)S_read_buf, sizeof(S_read_buf), |
| 2951 | timeout, use_timeout); |
| 2952 | |
| 2953 | /* |
| 2954 | * If that failed because timeout is not supported on this |
| 2955 | * platform, and the caller didn't actually want to use a timeout, |
| 2956 | * try again with an ordinary os_gets(). If they wanted to use a |
| 2957 | * timeout, simply return the NOTIMEOUT indication to our caller. |
| 2958 | */ |
| 2959 | if (evt == OS_EVT_NOTIMEOUT && !use_timeout) |
| 2960 | { |
| 2961 | /* perform an ordinary untimed input */ |
| 2962 | if (os_gets((uchar *)S_read_buf, sizeof(S_read_buf)) != 0) |
| 2963 | { |
| 2964 | /* success */ |
| 2965 | evt = OS_EVT_LINE; |
| 2966 | } |
| 2967 | else |
| 2968 | { |
| 2969 | /* error reading input */ |
| 2970 | evt = OS_EVT_EOF; |
| 2971 | } |
| 2972 | } |
| 2973 | |
| 2974 | /* |
| 2975 | * If we actually read a line, notify the display stream that we |
| 2976 | * read text from the console - it might need to make some |
| 2977 | * internal bookkeeping adjustments to account for the fact that |
| 2978 | * we moved the write position around on the display. |
| 2979 | * |
| 2980 | * Don't note the input if we timed out, since we haven't finished |
| 2981 | * reading the line yet in this case. |
| 2982 | */ |
| 2983 | if (evt == OS_EVT_LINE) |
| 2984 | { |
| 2985 | disp_str_->note_input_line(); |
| 2986 | if (log_str_ != 0) |
| 2987 | log_str_->note_input_line(); |
| 2988 | } |
| 2989 | } |
| 2990 | |
| 2991 | /* if we got an error, return it */ |
| 2992 | if (evt == OS_EVT_EOF) |
| 2993 | { |
| 2994 | set_more_state(S_old_more_mode); |
| 2995 | read_line_done(vmg0_); |
| 2996 | return log_event(vmg_ evt); |
| 2997 | } |
| 2998 | |
| 2999 | /* |
| 3000 | * Convert the text from the local UI character set to UTF-8. Reserve |
| 3001 | * space in the output buffer for the null terminator. |
| 3002 | */ |
| 3003 | char *outp = buf; |
| 3004 | size_t outlen = buflen - 1; |
| 3005 | G_cmap_from_ui->map(&outp, &outlen, S_read_buf, strlen(S_read_buf)); |
| 3006 | |
| 3007 | /* add the null terminator */ |
| 3008 | *outp = '\0'; |
| 3009 | |
| 3010 | /* |
| 3011 | * If we need to echo the text (because we read it from a script file), |
| 3012 | * do so now. Never echo text in the network configuration, since we |
| 3013 | * don't use the local console UI in this mode. |
| 3014 | */ |
| 3015 | if (echo_text && G_net_config == 0) |
| 3016 | { |
| 3017 | /* show the text */ |
| 3018 | format_text(vmg_ buf); |
| 3019 | |
| 3020 | /* add a newline */ |
| 3021 | format_text(vmg_ "\n"); |
| 3022 | } |
| 3023 | |
| 3024 | /* if we finished reading the line, do our line-finishing work */ |
| 3025 | if (evt == OS_EVT_LINE) |
| 3026 | read_line_done(vmg0_); |
| 3027 | |
| 3028 | /* |
| 3029 | * Log and return the event. Note that we log events in the UI |
| 3030 | * character set, so we want to simply use the original, untranslated |
| 3031 | * input buffer. |
| 3032 | */ |
| 3033 | return log_event(vmg_ evt, S_read_buf, strlen(S_read_buf), FALSE); |
| 3034 | } |
| 3035 | |
| 3036 | /* |
| 3037 | * Cancel an interrupted input. |
| 3038 | */ |
| 3039 | void CVmConsole::read_line_cancel(VMG_ int reset) |
| 3040 | { |
| 3041 | /* reset the underling OS layer */ |
| 3042 | os_gets_cancel(reset); |
| 3043 | |
| 3044 | /* do our line-ending work */ |
| 3045 | read_line_done(vmg0_); |
| 3046 | } |
| 3047 | |
| 3048 | /* |
| 3049 | * Perform line-ending work. This is used when we finish reading a line |
| 3050 | * in read_line_timeout(), or when we cancel an interrupted line, thus |
| 3051 | * finishing the line, in read_line_cancel(). |
| 3052 | */ |
| 3053 | void CVmConsole::read_line_done(VMG0_) |
| 3054 | { |
| 3055 | /* if we have a line in progress, finish it off */ |
| 3056 | if (S_read_in_progress) |
| 3057 | { |
| 3058 | /* set the original 'more' mode */ |
| 3059 | set_more_state(S_old_more_mode); |
| 3060 | |
| 3061 | /* |
| 3062 | * Write the input line, followed by a newline, to the log file. |
| 3063 | * Note that the text is still in the local character set, so we |
| 3064 | * can write it directly to the log file. |
| 3065 | * |
| 3066 | * If we're reading from a script file in "echo" mode, skip this. |
| 3067 | * When reading from a script file in "echo" mode, we will manually |
| 3068 | * copy the input commands to the main console, which will |
| 3069 | * automatically copy to the main log file. If we're in quiet |
| 3070 | * scripting mode, though, we won't do that, so we do need to |
| 3071 | * capture the input explicitly here. |
| 3072 | */ |
| 3073 | if (log_enabled_ && (script_sp_ == 0 || script_sp_->quiet)) |
| 3074 | { |
| 3075 | log_str_->print_to_os(S_read_buf); |
| 3076 | log_str_->print_to_os("\n"); |
| 3077 | } |
| 3078 | |
| 3079 | /* note in the streams that we've read an input line */ |
| 3080 | disp_str_->note_input_line(); |
| 3081 | if (log_str_ != 0) |
| 3082 | log_str_->note_input_line(); |
| 3083 | |
| 3084 | /* clear the in-progress flag */ |
| 3085 | S_read_in_progress = FALSE; |
| 3086 | } |
| 3087 | } |
| 3088 | |
| 3089 | /* ------------------------------------------------------------------------ */ |
| 3090 | /* |
| 3091 | * Read an input event from the script file. If we're reading an event |
| 3092 | * script file, we'll read the next event and return TRUE; if we're not |
| 3093 | * reading a script file, or the script file is a command-line script |
| 3094 | * rather than an event script, we'll simply return FALSE. |
| 3095 | * |
| 3096 | * If the event takes a parameter, we'll read the parameter into 'buf'. |
| 3097 | * The value is returned in the local character set, so the caller will |
| 3098 | * need to translate it to UTF-8. |
| 3099 | * |
| 3100 | * If 'filter' is non-null, we'll only return events of the types in the |
| 3101 | * filter list. |
| 3102 | */ |
| 3103 | int CVmConsole::read_event_script(VMG_ int *evt, char *buf, size_t buflen, |
| 3104 | const int *filter, int filter_cnt, |
| 3105 | unsigned long *attrs) |
| 3106 | { |
| 3107 | /* |
| 3108 | * if we're not reading a script, or it's not an event script, skip |
| 3109 | * this |
| 3110 | */ |
| 3111 | if (script_sp_ == 0 || !script_sp_->event_script) |
| 3112 | return FALSE; |
| 3113 | |
| 3114 | /* get the script file */ |
| 3115 | osfildef *fp = script_sp_->fp; |
| 3116 | |
| 3117 | /* keep going until we find something */ |
| 3118 | for (;;) |
| 3119 | { |
| 3120 | /* read the next event */ |
| 3121 | if (!read_script_event_type(evt, attrs)) |
| 3122 | { |
| 3123 | /* end of the script - close it */ |
| 3124 | set_more_state(close_script_file(vmg0_)); |
| 3125 | |
| 3126 | /* if there's no more script file, there's no event */ |
| 3127 | if (script_sp_ == 0) |
| 3128 | return FALSE; |
| 3129 | |
| 3130 | /* go back for the next event */ |
| 3131 | fp = script_sp_->fp; |
| 3132 | continue; |
| 3133 | } |
| 3134 | |
| 3135 | /* if it's not in the filter list, skip it */ |
| 3136 | if (filter != 0) |
| 3137 | { |
| 3138 | int i, found; |
| 3139 | |
| 3140 | /* look for a match in our filter list */ |
| 3141 | for (i = 0, found = FALSE ; i < filter_cnt ; ++i) |
| 3142 | { |
| 3143 | if (filter[i] == *evt) |
| 3144 | { |
| 3145 | found = TRUE; |
| 3146 | break; |
| 3147 | } |
| 3148 | } |
| 3149 | |
| 3150 | /* if we didn't find it, skip this line */ |
| 3151 | if (!found) |
| 3152 | { |
| 3153 | skip_script_line(fp); |
| 3154 | continue; |
| 3155 | } |
| 3156 | } |
| 3157 | |
| 3158 | /* if there's a buffer, read the rest of the line */ |
| 3159 | if (buf != 0) |
| 3160 | { |
| 3161 | /* read the parameter into the buffer, and return the result */ |
| 3162 | if (!read_script_param(buf, buflen, fp)) |
| 3163 | return FALSE; |
| 3164 | |
| 3165 | /* if this is an OS_EVT_KEY event, translate special keys */ |
| 3166 | if (*evt == OS_EVT_KEY) |
| 3167 | { |
| 3168 | if (strcmp(buf, "[enter]") == 0) |
| 3169 | strcpy(buf, "\n"); |
| 3170 | else if (strcmp(buf, "[tab]") == 0) |
| 3171 | strcpy(buf, "\t"); |
| 3172 | else if (strcmp(buf, "[space]") == 0) |
| 3173 | strcpy(buf, " "); |
| 3174 | } |
| 3175 | } |
| 3176 | else |
| 3177 | { |
| 3178 | /* no result buffer - just skip anything left on the line */ |
| 3179 | skip_script_line(fp); |
| 3180 | } |
| 3181 | |
| 3182 | /* success */ |
| 3183 | return TRUE; |
| 3184 | } |
| 3185 | } |
| 3186 | |
| 3187 | /* |
| 3188 | * Read a <tag> or attribute token from a script file. Returns the |
| 3189 | * character after the end of the token. |
| 3190 | */ |
| 3191 | static int read_script_token(char *buf, size_t buflen, osfildef *fp) |
| 3192 | { |
| 3193 | char *p; |
| 3194 | int c; |
| 3195 | |
| 3196 | /* skip leading whitespace */ |
| 3197 | for (c = osfgetc(fp) ; isspace(c) ; c = osfgetc(fp)) ; |
| 3198 | |
| 3199 | /* read from the file until we reach the end of the token */ |
| 3200 | for (p = buf ; |
| 3201 | p < buf + buflen - 1 |
| 3202 | && c != '>' && !isspace(c) |
| 3203 | && c != '\n' && c != '\r' && c != EOF ; ) |
| 3204 | { |
| 3205 | /* store this character */ |
| 3206 | *p++ = (char)c; |
| 3207 | |
| 3208 | /* get the next one */ |
| 3209 | c = osfgetc(fp); |
| 3210 | } |
| 3211 | |
| 3212 | /* null-terminate the token */ |
| 3213 | *p = '\0'; |
| 3214 | |
| 3215 | /* return the character that ended the token */ |
| 3216 | return c; |
| 3217 | } |
| 3218 | |
| 3219 | /* |
| 3220 | * Read the next event type from current event script file. This leaves |
| 3221 | * the file positioned at the parameter data for the event, if any. |
| 3222 | * Returns FALSE if we reach end of file without finding an event. |
| 3223 | */ |
| 3224 | int CVmConsole::read_script_event_type(int *evt, unsigned long *attrs) |
| 3225 | { |
| 3226 | /* clear the caller's attribute flags, if provided */ |
| 3227 | if (attrs != 0) |
| 3228 | *attrs = 0; |
| 3229 | |
| 3230 | /* if there's no script, there's no event */ |
| 3231 | if (script_sp_ == 0) |
| 3232 | return FALSE; |
| 3233 | |
| 3234 | /* get the file */ |
| 3235 | osfildef *fp = script_sp_->fp; |
| 3236 | |
| 3237 | /* if it's a command-line script, there are only line input events */ |
| 3238 | if (!script_sp_->event_script) |
| 3239 | { |
| 3240 | /* keep going until we find an input line */ |
| 3241 | for (;;) |
| 3242 | { |
| 3243 | /* read the first character of the line */ |
| 3244 | int c = osfgetc(fp); |
| 3245 | if (c == '>') |
| 3246 | { |
| 3247 | /* we found a line input event */ |
| 3248 | *evt = OS_EVT_LINE; |
| 3249 | return TRUE; |
| 3250 | } |
| 3251 | else if (c == EOF) |
| 3252 | { |
| 3253 | /* end of file - give up */ |
| 3254 | return FALSE; |
| 3255 | } |
| 3256 | else |
| 3257 | { |
| 3258 | /* |
| 3259 | * anything else is just a comment line - just skip it and |
| 3260 | * keep looking |
| 3261 | */ |
| 3262 | skip_script_line(fp); |
| 3263 | } |
| 3264 | } |
| 3265 | } |
| 3266 | |
| 3267 | /* keep going until we find an event tag */ |
| 3268 | for (;;) |
| 3269 | { |
| 3270 | int c; |
| 3271 | char tag[32]; |
| 3272 | static const struct |
| 3273 | { |
| 3274 | const char *tag; |
| 3275 | int evt; |
| 3276 | } |
| 3277 | *tp, tags[] = |
| 3278 | { |
| 3279 | { "key", OS_EVT_KEY }, |
| 3280 | { "timeout", OS_EVT_TIMEOUT }, |
| 3281 | { "notimeout", OS_EVT_NOTIMEOUT }, |
| 3282 | { "eof", OS_EVT_EOF }, |
| 3283 | { "line", OS_EVT_LINE }, |
| 3284 | { "command", OS_EVT_COMMAND }, |
| 3285 | |
| 3286 | { "endqs", VMCON_EVT_END_QUIET_SCRIPT }, |
| 3287 | { "dialog", VMCON_EVT_DIALOG }, |
| 3288 | { "file", VMCON_EVT_FILE }, |
| 3289 | |
| 3290 | { 0, 0 } |
| 3291 | }; |
| 3292 | |
| 3293 | /* check the start of the line to make sure it's an event */ |
| 3294 | c = osfgetc(fp); |
| 3295 | |
| 3296 | /* if at EOF, return failure */ |
| 3297 | if (c == EOF) |
| 3298 | return FALSE; |
| 3299 | |
| 3300 | /* if it's not an event code line, skip the line */ |
| 3301 | if (c != '<') |
| 3302 | { |
| 3303 | skip_script_line(fp); |
| 3304 | continue; |
| 3305 | } |
| 3306 | |
| 3307 | /* read the event type (up to the '>') */ |
| 3308 | c = read_script_token(tag, sizeof(tag), fp); |
| 3309 | |
| 3310 | /* check for attributes */ |
| 3311 | while (isspace(c)) |
| 3312 | { |
| 3313 | char attr[32]; |
| 3314 | static const struct |
| 3315 | { |
| 3316 | const char *name; |
| 3317 | unsigned long flag; |
| 3318 | } |
| 3319 | *ap, attrlist[] = |
| 3320 | { |
| 3321 | { "overwrite", VMCON_EVTATTR_OVERWRITE }, |
| 3322 | { 0, 0 } |
| 3323 | }; |
| 3324 | |
| 3325 | /* read the attribute name */ |
| 3326 | c = read_script_token(attr, sizeof(attr), fp); |
| 3327 | |
| 3328 | /* if the name is empty, stop */ |
| 3329 | if (attr[0] == '\0') |
| 3330 | break; |
| 3331 | |
| 3332 | /* look up the token */ |
| 3333 | for (ap = attrlist ; ap->name != 0 ; ++ap) |
| 3334 | { |
| 3335 | /* check for a match */ |
| 3336 | if (stricmp(attr, ap->name) == 0) |
| 3337 | { |
| 3338 | /* if the caller wants the flag, set it */ |
| 3339 | if (attrs != 0) |
| 3340 | *attrs |= ap->flag; |
| 3341 | |
| 3342 | /* no need to look any further */ |
| 3343 | break; |
| 3344 | } |
| 3345 | } |
| 3346 | |
| 3347 | /* if we're at the '>' or at end of line or file, stop */ |
| 3348 | if (c == '>' || c == '\n' || c == '\r' || c == EOF) |
| 3349 | break; |
| 3350 | } |
| 3351 | |
| 3352 | /* if it's not a well-formed tag, ignore it */ |
| 3353 | if (c != '>') |
| 3354 | { |
| 3355 | skip_script_line(fp); |
| 3356 | continue; |
| 3357 | } |
| 3358 | |
| 3359 | /* look up the tag */ |
| 3360 | for (tp = tags ; tp->tag != 0 ; ++tp) |
| 3361 | { |
| 3362 | /* check for a match to this tag name */ |
| 3363 | if (stricmp(tp->tag, tag) == 0) |
| 3364 | { |
| 3365 | /* got it - return the event type */ |
| 3366 | *evt = tp->evt; |
| 3367 | return TRUE; |
| 3368 | } |
| 3369 | } |
| 3370 | |
| 3371 | /* we don't recognize the tag name; skip the line and keep looking */ |
| 3372 | skip_script_line(fp); |
| 3373 | } |
| 3374 | } |
| 3375 | |
| 3376 | /* |
| 3377 | * Skip to the next script line |
| 3378 | */ |
| 3379 | void CVmConsole::skip_script_line(osfildef *fp) |
| 3380 | { |
| 3381 | int c; |
| 3382 | |
| 3383 | /* read until we find the end of the current line */ |
| 3384 | for (c = osfgetc(fp) ; c != EOF && c != '\n' && c != '\r' ; |
| 3385 | c = osfgetc(fp)) ; |
| 3386 | } |
| 3387 | |
| 3388 | /* |
| 3389 | * read the rest of the current script line into the given buffer |
| 3390 | */ |
| 3391 | int CVmConsole::read_script_param(char *buf, size_t buflen, osfildef *fp) |
| 3392 | { |
| 3393 | /* ignore zero-size buffer requests */ |
| 3394 | if (buflen == 0) |
| 3395 | return FALSE; |
| 3396 | |
| 3397 | /* read characters until we run out of buffer or reach a newline */ |
| 3398 | for (;;) |
| 3399 | { |
| 3400 | /* get the next character */ |
| 3401 | int c = osfgetc(fp); |
| 3402 | |
| 3403 | /* if it's a newline or end of file, we're done */ |
| 3404 | if (c == '\n' || c == EOF) |
| 3405 | { |
| 3406 | /* null-terminate the buffer */ |
| 3407 | *buf = '\0'; |
| 3408 | |
| 3409 | /* indicate success */ |
| 3410 | return TRUE; |
| 3411 | } |
| 3412 | |
| 3413 | /* |
| 3414 | * if there's room in the buffer, add the character - always leave |
| 3415 | * one byte for the null terminator |
| 3416 | */ |
| 3417 | if (buflen > 1) |
| 3418 | { |
| 3419 | *buf++ = (char)c; |
| 3420 | --buflen; |
| 3421 | } |
| 3422 | } |
| 3423 | } |
| 3424 | |
| 3425 | |
| 3426 | /* |
| 3427 | * Read a line of text from the script file, if there is one. Returns TRUE |
| 3428 | * on success, FALSE if we reach the end of the script file or encounter |
| 3429 | * any other error. |
| 3430 | */ |
| 3431 | int CVmConsole::read_line_from_script(char *buf, size_t buflen, int *evt) |
| 3432 | { |
| 3433 | /* if there's no script file, return failure */ |
| 3434 | if (script_sp_ == 0) |
| 3435 | return FALSE; |
| 3436 | |
| 3437 | /* get the file from the script stack */ |
| 3438 | osfildef *fp = script_sp_->fp; |
| 3439 | |
| 3440 | /* keep going until we find a line that we like */ |
| 3441 | for (;;) |
| 3442 | { |
| 3443 | /* read the script according to its type ('event' or 'line input') */ |
| 3444 | if (script_sp_->event_script) |
| 3445 | { |
| 3446 | /* read to the next event */ |
| 3447 | if (!read_script_event_type(evt, 0)) |
| 3448 | return FALSE; |
| 3449 | |
| 3450 | /* check the event code */ |
| 3451 | switch (*evt) |
| 3452 | { |
| 3453 | case OS_EVT_LINE: |
| 3454 | case OS_EVT_TIMEOUT: |
| 3455 | /* |
| 3456 | * it's one of our line input events - read the line (or |
| 3457 | * partial line, in the case of TIMEOUT) |
| 3458 | */ |
| 3459 | return read_script_param(buf, buflen, fp); |
| 3460 | |
| 3461 | default: |
| 3462 | /* |
| 3463 | * it's not our type of event - skip the rest of the line |
| 3464 | * and keep looking |
| 3465 | */ |
| 3466 | skip_script_line(fp); |
| 3467 | break; |
| 3468 | } |
| 3469 | } |
| 3470 | else |
| 3471 | { |
| 3472 | /* |
| 3473 | * We have a basic line-input script rather than an event |
| 3474 | * script. Each input line starts with a '>'; everything else |
| 3475 | * is a comment. |
| 3476 | * |
| 3477 | * Read the first character on the line. If it's not a |
| 3478 | * newline, there's more text on the same line, so read the |
| 3479 | * rest and determine what to do. |
| 3480 | */ |
| 3481 | int c = osfgetc(fp); |
| 3482 | if (c == '>') |
| 3483 | { |
| 3484 | /* it's a command line - read it */ |
| 3485 | *evt = OS_EVT_LINE; |
| 3486 | return read_script_param(buf, buflen, fp); |
| 3487 | } |
| 3488 | else if (c == EOF) |
| 3489 | { |
| 3490 | /* end of file */ |
| 3491 | return FALSE; |
| 3492 | } |
| 3493 | else if (c == '\n' || c == '\r') |
| 3494 | { |
| 3495 | /* blank line - continue on to the next line */ |
| 3496 | } |
| 3497 | else |
| 3498 | { |
| 3499 | /* it's not a command line - just skip it and keep looking */ |
| 3500 | skip_script_line(fp); |
| 3501 | } |
| 3502 | } |
| 3503 | } |
| 3504 | } |
| 3505 | |
| 3506 | /* ------------------------------------------------------------------------ */ |
| 3507 | /* |
| 3508 | * Main System Console |
| 3509 | */ |
| 3510 | |
| 3511 | /* |
| 3512 | * create |
| 3513 | */ |
| 3514 | CVmConsoleMain::CVmConsoleMain(VMG0_) |
| 3515 | { |
| 3516 | /* create the system banner manager */ |
| 3517 | banner_manager_ = new CVmBannerManager(); |
| 3518 | |
| 3519 | /* create the log console manager */ |
| 3520 | log_console_manager_ = new CVmLogConsoleManager(); |
| 3521 | |
| 3522 | /* create and initialize our display stream */ |
| 3523 | main_disp_str_ = new CVmFormatterMain(this, 256); |
| 3524 | main_disp_str_->init(); |
| 3525 | |
| 3526 | /* initially send text to the main display stream */ |
| 3527 | disp_str_ = main_disp_str_; |
| 3528 | |
| 3529 | /* |
| 3530 | * Create our log stream. The main console always has a log stream, |
| 3531 | * even when it's not in use, so that we can keep the log stream's |
| 3532 | * state synchronized with the display stream in preparation for |
| 3533 | * activation. |
| 3534 | */ |
| 3535 | log_str_ = new CVmFormatterLog(this, 80); |
| 3536 | |
| 3537 | /* |
| 3538 | * use the default log file character mapper - on some systems, files |
| 3539 | * don't use the same character set as the display |
| 3540 | */ |
| 3541 | log_str_->set_charmap(G_cmap_to_log); |
| 3542 | |
| 3543 | /* initialize the log stream */ |
| 3544 | log_str_->init(); |
| 3545 | |
| 3546 | /* |
| 3547 | * the log stream is initially enabled (this is separate from the log |
| 3548 | * file being opened; it merely indicates that we send output |
| 3549 | * operations to the log stream for processing) |
| 3550 | */ |
| 3551 | log_enabled_ = TRUE; |
| 3552 | |
| 3553 | /* we don't have a statusline formatter until asked for one */ |
| 3554 | statline_str_ = 0; |
| 3555 | |
| 3556 | /* reset statics */ |
| 3557 | S_read_in_progress = FALSE; |
| 3558 | S_read_buf[0] = '\0'; |
| 3559 | } |
| 3560 | |
| 3561 | /* |
| 3562 | * delete |
| 3563 | */ |
| 3564 | void CVmConsoleMain::delete_obj(VMG0_) |
| 3565 | { |
| 3566 | /* delete the system banner manager */ |
| 3567 | banner_manager_->delete_obj(vmg0_); |
| 3568 | |
| 3569 | /* delete the system log console manager */ |
| 3570 | log_console_manager_->delete_obj(vmg0_); |
| 3571 | |
| 3572 | /* delete the display stream */ |
| 3573 | main_disp_str_->delete_obj(vmg0_); |
| 3574 | |
| 3575 | /* delete the statusline stream, if we have one */ |
| 3576 | if (statline_str_ != 0) |
| 3577 | statline_str_->delete_obj(vmg0_); |
| 3578 | |
| 3579 | /* do the inherited work */ |
| 3580 | CVmConsole::delete_obj(vmg0_); |
| 3581 | } |
| 3582 | |
| 3583 | /* |
| 3584 | * Clear the window |
| 3585 | */ |
| 3586 | void CVmConsoleMain::clear_window(VMG0_) |
| 3587 | { |
| 3588 | /* flush and empty our output buffer */ |
| 3589 | flush(vmg_ VM_NL_NONE); |
| 3590 | empty_buffers(vmg0_); |
| 3591 | |
| 3592 | /* clear the main window */ |
| 3593 | oscls(); |
| 3594 | |
| 3595 | /* reset the MORE line counter in the display stream */ |
| 3596 | disp_str_->reset_line_count(TRUE); |
| 3597 | } |
| 3598 | |
| 3599 | /* |
| 3600 | * Set statusline mode |
| 3601 | */ |
| 3602 | void CVmConsoleMain::set_statusline_mode(VMG_ int mode) |
| 3603 | { |
| 3604 | CVmFormatterDisp *str; |
| 3605 | |
| 3606 | /* |
| 3607 | * if we're switching into statusline mode, and we don't have a |
| 3608 | * statusline stream yet, create one |
| 3609 | */ |
| 3610 | if (mode && statline_str_ == 0) |
| 3611 | { |
| 3612 | /* create and initialize the statusline stream */ |
| 3613 | statline_str_ = new CVmFormatterStatline(this); |
| 3614 | statline_str_->init(); |
| 3615 | } |
| 3616 | |
| 3617 | /* get the stream selected by the new mode */ |
| 3618 | if (mode) |
| 3619 | str = statline_str_; |
| 3620 | else |
| 3621 | str = main_disp_str_; |
| 3622 | |
| 3623 | /* if this is already the active stream, we have nothing more to do */ |
| 3624 | if (str == disp_str_) |
| 3625 | return; |
| 3626 | |
| 3627 | /* make the new stream current */ |
| 3628 | disp_str_ = str; |
| 3629 | |
| 3630 | /* |
| 3631 | * check which mode we're switching to, so we can do some extra work |
| 3632 | * specific to each mode |
| 3633 | */ |
| 3634 | if (mode) |
| 3635 | { |
| 3636 | /* |
| 3637 | * we're switching to the status line, so disable the log stream - |
| 3638 | * statusline text is never sent to the log, since the log reflects |
| 3639 | * only what was displayed in the main text area |
| 3640 | */ |
| 3641 | log_enabled_ = FALSE; |
| 3642 | } |
| 3643 | else |
| 3644 | { |
| 3645 | /* |
| 3646 | * we're switching back to the main stream, so flush the statusline |
| 3647 | * so we're sure the statusline text is displayed |
| 3648 | */ |
| 3649 | |
| 3650 | /* end the line */ |
| 3651 | statline_str_->format_text(vmg_ "\n", 1); |
| 3652 | |
| 3653 | /* flush output */ |
| 3654 | statline_str_->flush(vmg_ VM_NL_NONE); |
| 3655 | |
| 3656 | /* re-enable the log stream, if we have one */ |
| 3657 | if (log_str_ != 0) |
| 3658 | log_enabled_ = TRUE; |
| 3659 | } |
| 3660 | |
| 3661 | /* switch at the OS layer */ |
| 3662 | os_status(mode); |
| 3663 | } |
| 3664 | |
| 3665 | /* |
| 3666 | * Flush everything |
| 3667 | */ |
| 3668 | void CVmConsoleMain::flush_all(VMG_ vm_nl_type nl) |
| 3669 | { |
| 3670 | /* flush our primary console */ |
| 3671 | flush(vmg_ nl); |
| 3672 | |
| 3673 | /* |
| 3674 | * Flush each banner we're controlling. Note that we explicitly flush |
| 3675 | * the banners with newline mode 'NONE', regardless of the newline mode |
| 3676 | * passed in by the caller: the caller's mode is for the primary |
| 3677 | * console, but for the banners we just want to make sure they're |
| 3678 | * flushed out normally, since whatever we're doing in the primary |
| 3679 | * console that requires flushing doesn't concern the banners. |
| 3680 | */ |
| 3681 | banner_manager_->flush_all(vmg_ VM_NL_NONE); |
| 3682 | } |
| 3683 | |
| 3684 | /* ------------------------------------------------------------------------ */ |
| 3685 | /* |
| 3686 | * Handle manager |
| 3687 | */ |
| 3688 | |
| 3689 | /* initialize */ |
| 3690 | CVmHandleManager::CVmHandleManager() |
| 3691 | { |
| 3692 | size_t i; |
| 3693 | |
| 3694 | /* allocate an initial array of handle slots */ |
| 3695 | handles_max_ = 32; |
| 3696 | handles_ = (void **)t3malloc(handles_max_ * sizeof(*handles_)); |
| 3697 | |
| 3698 | /* all slots are initially empty */ |
| 3699 | for (i = 0 ; i < handles_max_ ; ++i) |
| 3700 | handles_[i] = 0; |
| 3701 | } |
| 3702 | |
| 3703 | /* delete the object - this is the public destructor interface */ |
| 3704 | void CVmHandleManager::delete_obj(VMG0_) |
| 3705 | { |
| 3706 | size_t i; |
| 3707 | |
| 3708 | /* |
| 3709 | * Delete each remaining object. Note that we need to call the virtual |
| 3710 | * delete_handle_object routine, so we must do this before reaching the |
| 3711 | * destructor (once in the base class destructor, we no longer have |
| 3712 | * access to the subclass virtuals). |
| 3713 | */ |
| 3714 | for (i = 0 ; i < handles_max_ ; ++i) |
| 3715 | { |
| 3716 | /* if this banner is still valid, delete it */ |
| 3717 | if (handles_[i] != 0) |
| 3718 | delete_handle_object(vmg_ i + 1, handles_[i]); |
| 3719 | } |
| 3720 | |
| 3721 | /* delete the object */ |
| 3722 | delete this; |
| 3723 | } |
| 3724 | |
| 3725 | /* destructor */ |
| 3726 | CVmHandleManager::~CVmHandleManager() |
| 3727 | { |
| 3728 | /* delete the handle pointer array */ |
| 3729 | t3free(handles_); |
| 3730 | } |
| 3731 | |
| 3732 | /* |
| 3733 | * Allocate a new handle |
| 3734 | */ |
| 3735 | int CVmHandleManager::alloc_handle(void *item) |
| 3736 | { |
| 3737 | size_t slot; |
| 3738 | |
| 3739 | /* scan for a free slot */ |
| 3740 | for (slot = 0 ; slot < handles_max_ ; ++slot) |
| 3741 | { |
| 3742 | /* if this one is free, use it */ |
| 3743 | if (handles_[slot] == 0) |
| 3744 | break; |
| 3745 | } |
| 3746 | |
| 3747 | /* if we didn't find a free slot, extend the array */ |
| 3748 | if (slot == handles_max_) |
| 3749 | { |
| 3750 | size_t i; |
| 3751 | |
| 3752 | /* allocate a larger array */ |
| 3753 | handles_max_ += 32; |
| 3754 | handles_ = (void **) |
| 3755 | t3realloc(handles_, handles_max_ * sizeof(*handles_)); |
| 3756 | |
| 3757 | /* clear out the newly-allocated slots */ |
| 3758 | for (i = slot ; i < handles_max_ ; ++i) |
| 3759 | handles_[i] = 0; |
| 3760 | } |
| 3761 | |
| 3762 | /* store the new item in our pointer array */ |
| 3763 | handles_[slot] = item; |
| 3764 | |
| 3765 | /* |
| 3766 | * convert the slot number to a handle by adjusting it to a 1-based |
| 3767 | * index, and return the result |
| 3768 | */ |
| 3769 | return slot + 1; |
| 3770 | } |
| 3771 | |
| 3772 | |
| 3773 | /* ------------------------------------------------------------------------ */ |
| 3774 | /* |
| 3775 | * Banner manager |
| 3776 | */ |
| 3777 | |
| 3778 | /* |
| 3779 | * Create a banner |
| 3780 | */ |
| 3781 | int CVmBannerManager::create_banner(VMG_ int parent_id, |
| 3782 | int where, int other_id, |
| 3783 | int wintype, int align, |
| 3784 | int siz, int siz_units, |
| 3785 | unsigned long style) |
| 3786 | { |
| 3787 | void *handle; |
| 3788 | void *parent_handle; |
| 3789 | void *other_handle; |
| 3790 | CVmConsoleBanner *item; |
| 3791 | |
| 3792 | /* get the parent handle, if provided */ |
| 3793 | parent_handle = get_os_handle(parent_id); |
| 3794 | |
| 3795 | /* get the 'other' handle, if we need it for the 'where' */ |
| 3796 | switch(where) |
| 3797 | { |
| 3798 | case OS_BANNER_BEFORE: |
| 3799 | case OS_BANNER_AFTER: |
| 3800 | /* retrieve the handle for the other_id */ |
| 3801 | other_handle = get_os_handle(other_id); |
| 3802 | break; |
| 3803 | |
| 3804 | default: |
| 3805 | /* we don't need 'other' for other 'where' modes */ |
| 3806 | other_handle = 0; |
| 3807 | break; |
| 3808 | } |
| 3809 | |
| 3810 | /* try creating the OS-level banner window */ |
| 3811 | handle = os_banner_create(parent_handle, where, other_handle, wintype, |
| 3812 | align, siz, siz_units, style); |
| 3813 | |
| 3814 | /* if we couldn't create the OS-level window, return failure */ |
| 3815 | if (handle == 0) |
| 3816 | return 0; |
| 3817 | |
| 3818 | /* create the new console */ |
| 3819 | item = new CVmConsoleBanner(handle, wintype, style); |
| 3820 | |
| 3821 | /* allocate a handle for the new banner, and return the handle */ |
| 3822 | return alloc_handle(item); |
| 3823 | } |
| 3824 | |
| 3825 | /* |
| 3826 | * Delete or orphan a banner window. Deleting and orphaning both sever |
| 3827 | * all ties from the banner manager (and thus from the T3 program) to the |
| 3828 | * banner. Deleting a banner actually gets deletes it at the OS level; |
| 3829 | * orphaning the banner severs our ties, but hands the banner over to the |
| 3830 | * OS to do with as it pleases. On some implementations, the OS will |
| 3831 | * continue to display the banner after it's orphaned to allow the final |
| 3832 | * display configuration to remain visible even after the program has |
| 3833 | * terminated. |
| 3834 | */ |
| 3835 | void CVmBannerManager::delete_or_orphan_banner(VMG_ int banner, int orphan) |
| 3836 | { |
| 3837 | CVmConsoleBanner *item; |
| 3838 | void *handle; |
| 3839 | |
| 3840 | /* if the banner is invalid, ignore the request */ |
| 3841 | if ((item = (CVmConsoleBanner *)get_object(banner)) == 0) |
| 3842 | return; |
| 3843 | |
| 3844 | /* get the OS-level banner handle */ |
| 3845 | handle = item->get_os_handle(); |
| 3846 | |
| 3847 | /* delete the banner item */ |
| 3848 | item->delete_obj(vmg0_); |
| 3849 | |
| 3850 | /* clear the slot */ |
| 3851 | clear_handle(banner); |
| 3852 | |
| 3853 | /* delete the OS-level banner */ |
| 3854 | if (orphan) |
| 3855 | os_banner_orphan(handle); |
| 3856 | else |
| 3857 | os_banner_delete(handle); |
| 3858 | } |
| 3859 | |
| 3860 | /* |
| 3861 | * Get the OS-level handle for the given banner |
| 3862 | */ |
| 3863 | void *CVmBannerManager::get_os_handle(int banner) |
| 3864 | { |
| 3865 | CVmConsoleBanner *item; |
| 3866 | |
| 3867 | /* if the banner is invalid, return failure */ |
| 3868 | if ((item = (CVmConsoleBanner *)get_object(banner)) == 0) |
| 3869 | return 0; |
| 3870 | |
| 3871 | /* return the handle from the slot */ |
| 3872 | return item->get_os_handle(); |
| 3873 | } |
| 3874 | |
| 3875 | /* |
| 3876 | * Flush all banners |
| 3877 | */ |
| 3878 | void CVmBannerManager::flush_all(VMG_ vm_nl_type nl) |
| 3879 | { |
| 3880 | size_t slot; |
| 3881 | |
| 3882 | /* flush each banner */ |
| 3883 | for (slot = 0 ; slot < handles_max_ ; ++slot) |
| 3884 | { |
| 3885 | /* if this slot has a valid banner, flush it */ |
| 3886 | if (handles_[slot] != 0) |
| 3887 | ((CVmConsoleBanner *)handles_[slot])->flush(vmg_ nl); |
| 3888 | } |
| 3889 | } |
| 3890 | |
| 3891 | /* ------------------------------------------------------------------------ */ |
| 3892 | /* |
| 3893 | * Banner Window Console |
| 3894 | */ |
| 3895 | CVmConsoleBanner::CVmConsoleBanner(void *banner_handle, int win_type, |
| 3896 | unsigned long style) |
| 3897 | { |
| 3898 | CVmFormatterBanner *str; |
| 3899 | os_banner_info_t info; |
| 3900 | int obey_whitespace = FALSE; |
| 3901 | int literal_mode = FALSE; |
| 3902 | |
| 3903 | /* remember our OS-level banner handle */ |
| 3904 | banner_ = banner_handle; |
| 3905 | |
| 3906 | /* get osifc-level information on the banner */ |
| 3907 | if (!os_banner_getinfo(banner_, &info)) |
| 3908 | info.os_line_wrap = FALSE; |
| 3909 | |
| 3910 | /* |
| 3911 | * If it's a text grid window, don't do any line wrapping. Text grids |
| 3912 | * simply don't have any line wrapping, so we don't want to impose any |
| 3913 | * at the formatter level. Set the formatter to "os line wrap" mode, |
| 3914 | * to indicate that the formatter doesn't do wrapping - even though |
| 3915 | * the underlying OS banner window won't do any wrapping either, the |
| 3916 | * lack of line wrapping counts as OS handling of line wrapping. |
| 3917 | */ |
| 3918 | if (win_type == OS_BANNER_TYPE_TEXTGRID) |
| 3919 | { |
| 3920 | /* do not wrap lines in the formatter */ |
| 3921 | info.os_line_wrap = TRUE; |
| 3922 | |
| 3923 | /* use literal mode, and obey whitespace literally */ |
| 3924 | literal_mode = TRUE; |
| 3925 | obey_whitespace = TRUE; |
| 3926 | } |
| 3927 | |
| 3928 | /* create and initialize our display stream */ |
| 3929 | disp_str_ = str = new CVmFormatterBanner(banner_handle, this, |
| 3930 | win_type, style); |
| 3931 | str->init_banner(info.os_line_wrap, obey_whitespace, literal_mode); |
| 3932 | |
| 3933 | /* remember our window type */ |
| 3934 | win_type_ = win_type; |
| 3935 | } |
| 3936 | |
| 3937 | /* |
| 3938 | * Deletion |
| 3939 | */ |
| 3940 | void CVmConsoleBanner::delete_obj(VMG0_) |
| 3941 | { |
| 3942 | /* delete our display stream */ |
| 3943 | disp_str_->delete_obj(vmg0_); |
| 3944 | |
| 3945 | /* do the inherited work */ |
| 3946 | CVmConsole::delete_obj(vmg0_); |
| 3947 | } |
| 3948 | |
| 3949 | /* |
| 3950 | * Clear the banner window |
| 3951 | */ |
| 3952 | void CVmConsoleBanner::clear_window(VMG0_) |
| 3953 | { |
| 3954 | /* flush and empty our output buffer */ |
| 3955 | flush(vmg_ VM_NL_NONE); |
| 3956 | empty_buffers(vmg0_); |
| 3957 | |
| 3958 | /* clear our underlying system banner */ |
| 3959 | os_banner_clear(banner_); |
| 3960 | |
| 3961 | /* tell our display stream to zero its line counter */ |
| 3962 | disp_str_->reset_line_count(TRUE); |
| 3963 | } |
| 3964 | |
| 3965 | /* |
| 3966 | * Get banner information |
| 3967 | */ |
| 3968 | int CVmConsoleBanner::get_banner_info(os_banner_info_t *info) |
| 3969 | { |
| 3970 | int ret; |
| 3971 | |
| 3972 | /* get the OS-level information */ |
| 3973 | ret = os_banner_getinfo(banner_, info); |
| 3974 | |
| 3975 | /* make some adjustments if we got valid information back */ |
| 3976 | if (ret) |
| 3977 | { |
| 3978 | /* |
| 3979 | * check the window type for further adjustments we might need to |
| 3980 | * make to the data returned from the OS layer |
| 3981 | */ |
| 3982 | switch(win_type_) |
| 3983 | { |
| 3984 | case OS_BANNER_TYPE_TEXTGRID: |
| 3985 | /* |
| 3986 | * text grids don't support <TAB> alignment, even if the |
| 3987 | * underlying OS banner says we do, because we simply don't |
| 3988 | * support <TAB> (or any other HTML markups) in a text grid |
| 3989 | * window |
| 3990 | */ |
| 3991 | info->style &= ~OS_BANNER_STYLE_TAB_ALIGN; |
| 3992 | break; |
| 3993 | |
| 3994 | default: |
| 3995 | /* other types don't require any adjustments */ |
| 3996 | break; |
| 3997 | } |
| 3998 | } |
| 3999 | |
| 4000 | /* return the success indication */ |
| 4001 | return ret; |
| 4002 | } |
| 4003 | |
| 4004 | /* ------------------------------------------------------------------------ */ |
| 4005 | /* |
| 4006 | * Log file console manager |
| 4007 | */ |
| 4008 | |
| 4009 | /* |
| 4010 | * create a log console |
| 4011 | */ |
| 4012 | int CVmLogConsoleManager::create_log_console( |
| 4013 | VMG_ CVmNetFile *nf, osfildef *fp, CCharmapToLocal *cmap, int width) |
| 4014 | { |
| 4015 | CVmConsoleLog *con; |
| 4016 | |
| 4017 | /* create the new console */ |
| 4018 | con = new CVmConsoleLog(vmg_ nf, fp, cmap, width); |
| 4019 | |
| 4020 | /* allocate a handle for the new console and return the handle */ |
| 4021 | return alloc_handle(con); |
| 4022 | } |
| 4023 | |
| 4024 | /* |
| 4025 | * delete log a console |
| 4026 | */ |
| 4027 | void CVmLogConsoleManager::delete_log_console(VMG_ int handle) |
| 4028 | { |
| 4029 | CVmConsoleLog *con; |
| 4030 | |
| 4031 | /* if the handle is invalid, ignore the request */ |
| 4032 | if ((con = (CVmConsoleLog *)get_object(handle)) == 0) |
| 4033 | return; |
| 4034 | |
| 4035 | /* delete the console */ |
| 4036 | con->delete_obj(vmg0_); |
| 4037 | |
| 4038 | /* clear the slot */ |
| 4039 | clear_handle(handle); |
| 4040 | } |
| 4041 | |
| 4042 | /* ------------------------------------------------------------------------ */ |
| 4043 | /* |
| 4044 | * Log file console |
| 4045 | */ |
| 4046 | CVmConsoleLog::CVmConsoleLog(VMG_ CVmNetFile *nf, osfildef *fp, |
| 4047 | class CCharmapToLocal *cmap, int width) |
| 4048 | { |
| 4049 | CVmFormatterLog *str; |
| 4050 | |
| 4051 | /* create our display stream */ |
| 4052 | disp_str_ = str = new CVmFormatterLog(this, width); |
| 4053 | |
| 4054 | /* set the file */ |
| 4055 | str->set_log_file(vmg_ nf, fp); |
| 4056 | |
| 4057 | /* set the character mapper */ |
| 4058 | str->set_charmap(cmap); |
| 4059 | } |
| 4060 | |
| 4061 | /* |
| 4062 | * destroy |
| 4063 | */ |
| 4064 | void CVmConsoleLog::delete_obj(VMG0_) |
| 4065 | { |
| 4066 | /* delete our display stream */ |
| 4067 | disp_str_->delete_obj(vmg0_); |
| 4068 | |
| 4069 | /* do the inherited work */ |
| 4070 | CVmConsole::delete_obj(vmg0_); |
| 4071 | } |