1 /* vi:set ts=8 sts=4 sw=4 noet: 2 * 3 * VIM - Vi IMproved by Bram Moolenaar 4 * 5 * Do ":help uganda" in Vim to read copying and usage conditions. 6 * Do ":help credits" in Vim to see a list of people who contributed. 7 * See README.txt for an overview of the Vim source code. 8 */ 9 10 /* 11 * ex_eval.c: functions for Ex command line for the +eval feature. 12 */ 13 14 #include "vim.h" 15 16 #if defined(FEAT_EVAL) || defined(PROTO) 17 18 static void free_msglist(struct msglist *l); 19 static int throw_exception(void *, except_type_T, char_u *); 20 static char_u *get_end_emsg(struct condstack *cstack); 21 22 /* 23 * Exception handling terms: 24 * 25 * :try ":try" command \ 26 * ... try block | 27 * :catch RE ":catch" command | 28 * ... catch clause |- try conditional 29 * :finally ":finally" command | 30 * ... finally clause | 31 * :endtry ":endtry" command / 32 * 33 * The try conditional may have any number of catch clauses and at most one 34 * finally clause. A ":throw" command can be inside the try block, a catch 35 * clause, the finally clause, or in a function called or script sourced from 36 * there or even outside the try conditional. Try conditionals may be nested. 37 */ 38 39 /* 40 * Configuration whether an exception is thrown on error or interrupt. When 41 * the preprocessor macros below evaluate to FALSE, an error (did_emsg) or 42 * interrupt (got_int) under an active try conditional terminates the script 43 * after the non-active finally clauses of all active try conditionals have been 44 * executed. Otherwise, errors and/or interrupts are converted into catchable 45 * exceptions (did_throw additionally set), which terminate the script only if 46 * not caught. For user exceptions, only did_throw is set. (Note: got_int can 47 * be set asynchronously afterwards by a SIGINT, so did_throw && got_int is not 48 * a reliant test that the exception currently being thrown is an interrupt 49 * exception. Similarly, did_emsg can be set afterwards on an error in an 50 * (unskipped) conditional command inside an inactive conditional, so did_throw 51 * && did_emsg is not a reliant test that the exception currently being thrown 52 * is an error exception.) - The macros can be defined as expressions checking 53 * for a variable that is allowed to be changed during execution of a script. 54 */ 55 #if 0 56 /* Expressions used for testing during the development phase. */ 57 # define THROW_ON_ERROR (!eval_to_number("$VIMNOERRTHROW")) 58 # define THROW_ON_INTERRUPT (!eval_to_number("$VIMNOINTTHROW")) 59 # define THROW_TEST 60 #else 61 /* Values used for the Vim release. */ 62 # define THROW_ON_ERROR TRUE 63 # define THROW_ON_ERROR_TRUE 64 # define THROW_ON_INTERRUPT TRUE 65 # define THROW_ON_INTERRUPT_TRUE 66 #endif 67 68 static void catch_exception(except_T *excp); 69 static void finish_exception(except_T *excp); 70 static void discard_exception(except_T *excp, int was_finished); 71 static void report_pending(int action, int pending, void *value); 72 73 /* 74 * When several errors appear in a row, setting "force_abort" is delayed until 75 * the failing command returned. "cause_abort" is set to TRUE meanwhile, in 76 * order to indicate that situation. This is useful when "force_abort" was set 77 * during execution of a function call from an expression: the aborting of the 78 * expression evaluation is done without producing any error messages, but all 79 * error messages on parsing errors during the expression evaluation are given 80 * (even if a try conditional is active). 81 */ 82 static int cause_abort = FALSE; 83 84 /* 85 * Return TRUE when immediately aborting on error, or when an interrupt 86 * occurred or an exception was thrown but not caught. Use for ":{range}call" 87 * to check whether an aborted function that does not handle a range itself 88 * should be called again for the next line in the range. Also used for 89 * cancelling expression evaluation after a function call caused an immediate 90 * abort. Note that the first emsg() call temporarily resets "force_abort" 91 * until the throw point for error messages has been reached. That is, during 92 * cancellation of an expression evaluation after an aborting function call or 93 * due to a parsing error, aborting() always returns the same value. 94 */ 95 int 96 aborting(void) 97 { 98 return (did_emsg && force_abort) || got_int || did_throw; 99 } 100 101 /* 102 * The value of "force_abort" is temporarily reset by the first emsg() call 103 * during an expression evaluation, and "cause_abort" is used instead. It might 104 * be necessary to restore "force_abort" even before the throw point for the 105 * error message has been reached. update_force_abort() should be called then. 106 */ 107 void 108 update_force_abort(void) 109 { 110 if (cause_abort) 111 force_abort = TRUE; 112 } 113 114 /* 115 * Return TRUE if a command with a subcommand resulting in "retcode" should 116 * abort the script processing. Can be used to suppress an autocommand after 117 * execution of a failing subcommand as long as the error message has not been 118 * displayed and actually caused the abortion. 119 */ 120 int 121 should_abort(int retcode) 122 { 123 return ((retcode == FAIL && trylevel != 0 && !emsg_silent) || aborting()); 124 } 125 126 /* 127 * Return TRUE if a function with the "abort" flag should not be considered 128 * ended on an error. This means that parsing commands is continued in order 129 * to find finally clauses to be executed, and that some errors in skipped 130 * commands are still reported. 131 */ 132 int 133 aborted_in_try(void) 134 { 135 /* This function is only called after an error. In this case, "force_abort" 136 * determines whether searching for finally clauses is necessary. */ 137 return force_abort; 138 } 139 140 /* 141 * cause_errthrow(): Cause a throw of an error exception if appropriate. 142 * Return TRUE if the error message should not be displayed by emsg(). 143 * Sets "ignore", if the emsg() call should be ignored completely. 144 * 145 * When several messages appear in the same command, the first is usually the 146 * most specific one and used as the exception value. The "severe" flag can be 147 * set to TRUE, if a later but severer message should be used instead. 148 */ 149 int 150 cause_errthrow( 151 char_u *mesg, 152 int severe, 153 int *ignore) 154 { 155 struct msglist *elem; 156 struct msglist **plist; 157 158 /* 159 * Do nothing when displaying the interrupt message or reporting an 160 * uncaught exception (which has already been discarded then) at the top 161 * level. Also when no exception can be thrown. The message will be 162 * displayed by emsg(). 163 */ 164 if (suppress_errthrow) 165 return FALSE; 166 167 /* 168 * If emsg() has not been called previously, temporarily reset 169 * "force_abort" until the throw point for error messages has been 170 * reached. This ensures that aborting() returns the same value for all 171 * errors that appear in the same command. This means particularly that 172 * for parsing errors during expression evaluation emsg() will be called 173 * multiply, even when the expression is evaluated from a finally clause 174 * that was activated due to an aborting error, interrupt, or exception. 175 */ 176 if (!did_emsg) 177 { 178 cause_abort = force_abort; 179 force_abort = FALSE; 180 } 181 182 /* 183 * If no try conditional is active and no exception is being thrown and 184 * there has not been an error in a try conditional or a throw so far, do 185 * nothing (for compatibility of non-EH scripts). The message will then 186 * be displayed by emsg(). When ":silent!" was used and we are not 187 * currently throwing an exception, do nothing. The message text will 188 * then be stored to v:errmsg by emsg() without displaying it. 189 */ 190 if (((trylevel == 0 && !cause_abort) || emsg_silent) && !did_throw) 191 return FALSE; 192 193 /* 194 * Ignore an interrupt message when inside a try conditional or when an 195 * exception is being thrown or when an error in a try conditional or 196 * throw has been detected previously. This is important in order that an 197 * interrupt exception is catchable by the innermost try conditional and 198 * not replaced by an interrupt message error exception. 199 */ 200 if (mesg == (char_u *)_(e_interr)) 201 { 202 *ignore = TRUE; 203 return TRUE; 204 } 205 206 /* 207 * Ensure that all commands in nested function calls and sourced files 208 * are aborted immediately. 209 */ 210 cause_abort = TRUE; 211 212 /* 213 * When an exception is being thrown, some commands (like conditionals) are 214 * not skipped. Errors in those commands may affect what of the subsequent 215 * commands are regarded part of catch and finally clauses. Catching the 216 * exception would then cause execution of commands not intended by the 217 * user, who wouldn't even get aware of the problem. Therefor, discard the 218 * exception currently being thrown to prevent it from being caught. Just 219 * execute finally clauses and terminate. 220 */ 221 if (did_throw) 222 { 223 /* When discarding an interrupt exception, reset got_int to prevent the 224 * same interrupt being converted to an exception again and discarding 225 * the error exception we are about to throw here. */ 226 if (current_exception->type == ET_INTERRUPT) 227 got_int = FALSE; 228 discard_current_exception(); 229 } 230 231 #ifdef THROW_TEST 232 if (!THROW_ON_ERROR) 233 { 234 /* 235 * Print error message immediately without searching for a matching 236 * catch clause; just finally clauses are executed before the script 237 * is terminated. 238 */ 239 return FALSE; 240 } 241 else 242 #endif 243 { 244 /* 245 * Prepare the throw of an error exception, so that everything will 246 * be aborted (except for executing finally clauses), until the error 247 * exception is caught; if still uncaught at the top level, the error 248 * message will be displayed and the script processing terminated 249 * then. - This function has no access to the conditional stack. 250 * Thus, the actual throw is made after the failing command has 251 * returned. - Throw only the first of several errors in a row, except 252 * a severe error is following. 253 */ 254 if (msg_list != NULL) 255 { 256 plist = msg_list; 257 while (*plist != NULL) 258 plist = &(*plist)->next; 259 260 elem = (struct msglist *)alloc((unsigned)sizeof(struct msglist)); 261 if (elem == NULL) 262 { 263 suppress_errthrow = TRUE; 264 EMSG(_(e_outofmem)); 265 } 266 else 267 { 268 elem->msg = vim_strsave(mesg); 269 if (elem->msg == NULL) 270 { 271 vim_free(elem); 272 suppress_errthrow = TRUE; 273 EMSG(_(e_outofmem)); 274 } 275 else 276 { 277 elem->next = NULL; 278 elem->throw_msg = NULL; 279 *plist = elem; 280 if (plist == msg_list || severe) 281 { 282 char_u *tmsg; 283 284 /* Skip the extra "Vim " prefix for message "E458". */ 285 tmsg = elem->msg; 286 if (STRNCMP(tmsg, "Vim E", 5) == 0 287 && VIM_ISDIGIT(tmsg[5]) 288 && VIM_ISDIGIT(tmsg[6]) 289 && VIM_ISDIGIT(tmsg[7]) 290 && tmsg[8] == ':' 291 && tmsg[9] == ' ') 292 (*msg_list)->throw_msg = &tmsg[4]; 293 else 294 (*msg_list)->throw_msg = tmsg; 295 } 296 } 297 } 298 } 299 return TRUE; 300 } 301 } 302 303 /* 304 * Free a "msg_list" and the messages it contains. 305 */ 306 static void 307 free_msglist(struct msglist *l) 308 { 309 struct msglist *messages, *next; 310 311 messages = l; 312 while (messages != NULL) 313 { 314 next = messages->next; 315 vim_free(messages->msg); 316 vim_free(messages); 317 messages = next; 318 } 319 } 320 321 /* 322 * Free global "*msg_list" and the messages it contains, then set "*msg_list" 323 * to NULL. 324 */ 325 void 326 free_global_msglist(void) 327 { 328 free_msglist(*msg_list); 329 *msg_list = NULL; 330 } 331 332 /* 333 * Throw the message specified in the call to cause_errthrow() above as an 334 * error exception. If cstack is NULL, postpone the throw until do_cmdline() 335 * has returned (see do_one_cmd()). 336 */ 337 void 338 do_errthrow(struct condstack *cstack, char_u *cmdname) 339 { 340 /* 341 * Ensure that all commands in nested function calls and sourced files 342 * are aborted immediately. 343 */ 344 if (cause_abort) 345 { 346 cause_abort = FALSE; 347 force_abort = TRUE; 348 } 349 350 /* If no exception is to be thrown or the conversion should be done after 351 * returning to a previous invocation of do_one_cmd(), do nothing. */ 352 if (msg_list == NULL || *msg_list == NULL) 353 return; 354 355 if (throw_exception(*msg_list, ET_ERROR, cmdname) == FAIL) 356 free_msglist(*msg_list); 357 else 358 { 359 if (cstack != NULL) 360 do_throw(cstack); 361 else 362 need_rethrow = TRUE; 363 } 364 *msg_list = NULL; 365 } 366 367 /* 368 * do_intthrow(): Replace the current exception by an interrupt or interrupt 369 * exception if appropriate. Return TRUE if the current exception is discarded, 370 * FALSE otherwise. 371 */ 372 int 373 do_intthrow(struct condstack *cstack) 374 { 375 /* 376 * If no interrupt occurred or no try conditional is active and no exception 377 * is being thrown, do nothing (for compatibility of non-EH scripts). 378 */ 379 if (!got_int || (trylevel == 0 && !did_throw)) 380 return FALSE; 381 382 #ifdef THROW_TEST /* avoid warning for condition always true */ 383 if (!THROW_ON_INTERRUPT) 384 { 385 /* 386 * The interrupt aborts everything except for executing finally clauses. 387 * Discard any user or error or interrupt exception currently being 388 * thrown. 389 */ 390 if (did_throw) 391 discard_current_exception(); 392 } 393 else 394 #endif 395 { 396 /* 397 * Throw an interrupt exception, so that everything will be aborted 398 * (except for executing finally clauses), until the interrupt exception 399 * is caught; if still uncaught at the top level, the script processing 400 * will be terminated then. - If an interrupt exception is already 401 * being thrown, do nothing. 402 * 403 */ 404 if (did_throw) 405 { 406 if (current_exception->type == ET_INTERRUPT) 407 return FALSE; 408 409 /* An interrupt exception replaces any user or error exception. */ 410 discard_current_exception(); 411 } 412 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) != FAIL) 413 do_throw(cstack); 414 } 415 416 return TRUE; 417 } 418 419 /* 420 * Get an exception message that is to be stored in current_exception->value. 421 */ 422 char_u * 423 get_exception_string( 424 void *value, 425 except_type_T type, 426 char_u *cmdname, 427 int *should_free) 428 { 429 char_u *ret, *mesg; 430 int cmdlen; 431 char_u *p, *val; 432 433 if (type == ET_ERROR) 434 { 435 *should_free = TRUE; 436 mesg = ((struct msglist *)value)->throw_msg; 437 if (cmdname != NULL && *cmdname != NUL) 438 { 439 cmdlen = (int)STRLEN(cmdname); 440 ret = vim_strnsave((char_u *)"Vim(", 441 4 + cmdlen + 2 + (int)STRLEN(mesg)); 442 if (ret == NULL) 443 return ret; 444 STRCPY(&ret[4], cmdname); 445 STRCPY(&ret[4 + cmdlen], "):"); 446 val = ret + 4 + cmdlen + 2; 447 } 448 else 449 { 450 ret = vim_strnsave((char_u *)"Vim:", 4 + (int)STRLEN(mesg)); 451 if (ret == NULL) 452 return ret; 453 val = ret + 4; 454 } 455 456 /* msg_add_fname may have been used to prefix the message with a file 457 * name in quotes. In the exception value, put the file name in 458 * parentheses and move it to the end. */ 459 for (p = mesg; ; p++) 460 { 461 if (*p == NUL 462 || (*p == 'E' 463 && VIM_ISDIGIT(p[1]) 464 && (p[2] == ':' 465 || (VIM_ISDIGIT(p[2]) 466 && (p[3] == ':' 467 || (VIM_ISDIGIT(p[3]) 468 && p[4] == ':')))))) 469 { 470 if (*p == NUL || p == mesg) 471 STRCAT(val, mesg); /* 'E123' missing or at beginning */ 472 else 473 { 474 /* '"filename" E123: message text' */ 475 if (mesg[0] != '"' || p-2 < &mesg[1] || 476 p[-2] != '"' || p[-1] != ' ') 477 /* "E123:" is part of the file name. */ 478 continue; 479 480 STRCAT(val, p); 481 p[-2] = NUL; 482 sprintf((char *)(val + STRLEN(p)), " (%s)", &mesg[1]); 483 p[-2] = '"'; 484 } 485 break; 486 } 487 } 488 } 489 else 490 { 491 *should_free = FALSE; 492 ret = (char_u *)value; 493 } 494 495 return ret; 496 } 497 498 499 /* 500 * Throw a new exception. Return FAIL when out of memory or it was tried to 501 * throw an illegal user exception. "value" is the exception string for a 502 * user or interrupt exception, or points to a message list in case of an 503 * error exception. 504 */ 505 static int 506 throw_exception(void *value, except_type_T type, char_u *cmdname) 507 { 508 except_T *excp; 509 int should_free; 510 511 /* 512 * Disallow faking Interrupt or error exceptions as user exceptions. They 513 * would be treated differently from real interrupt or error exceptions 514 * when no active try block is found, see do_cmdline(). 515 */ 516 if (type == ET_USER) 517 { 518 if (STRNCMP((char_u *)value, "Vim", 3) == 0 519 && (((char_u *)value)[3] == NUL || ((char_u *)value)[3] == ':' 520 || ((char_u *)value)[3] == '(')) 521 { 522 EMSG(_("E608: Cannot :throw exceptions with 'Vim' prefix")); 523 goto fail; 524 } 525 } 526 527 excp = (except_T *)alloc((unsigned)sizeof(except_T)); 528 if (excp == NULL) 529 goto nomem; 530 531 if (type == ET_ERROR) 532 /* Store the original message and prefix the exception value with 533 * "Vim:" or, if a command name is given, "Vim(cmdname):". */ 534 excp->messages = (struct msglist *)value; 535 536 excp->value = get_exception_string(value, type, cmdname, &should_free); 537 if (excp->value == NULL && should_free) 538 goto nomem; 539 540 excp->type = type; 541 excp->throw_name = vim_strsave(sourcing_name == NULL 542 ? (char_u *)"" : sourcing_name); 543 if (excp->throw_name == NULL) 544 { 545 if (should_free) 546 vim_free(excp->value); 547 goto nomem; 548 } 549 excp->throw_lnum = sourcing_lnum; 550 551 if (p_verbose >= 13 || debug_break_level > 0) 552 { 553 int save_msg_silent = msg_silent; 554 555 if (debug_break_level > 0) 556 msg_silent = FALSE; /* display messages */ 557 else 558 verbose_enter(); 559 ++no_wait_return; 560 if (debug_break_level > 0 || *p_vfile == NUL) 561 msg_scroll = TRUE; /* always scroll up, don't overwrite */ 562 563 smsg((char_u *)_("Exception thrown: %s"), excp->value); 564 msg_puts((char_u *)"\n"); /* don't overwrite this either */ 565 566 if (debug_break_level > 0 || *p_vfile == NUL) 567 cmdline_row = msg_row; 568 --no_wait_return; 569 if (debug_break_level > 0) 570 msg_silent = save_msg_silent; 571 else 572 verbose_leave(); 573 } 574 575 current_exception = excp; 576 return OK; 577 578 nomem: 579 vim_free(excp); 580 suppress_errthrow = TRUE; 581 EMSG(_(e_outofmem)); 582 fail: 583 current_exception = NULL; 584 return FAIL; 585 } 586 587 /* 588 * Discard an exception. "was_finished" is set when the exception has been 589 * caught and the catch clause has been ended normally. 590 */ 591 static void 592 discard_exception(except_T *excp, int was_finished) 593 { 594 char_u *saved_IObuff; 595 596 if (excp == NULL) 597 { 598 internal_error("discard_exception()"); 599 return; 600 } 601 602 if (p_verbose >= 13 || debug_break_level > 0) 603 { 604 int save_msg_silent = msg_silent; 605 606 saved_IObuff = vim_strsave(IObuff); 607 if (debug_break_level > 0) 608 msg_silent = FALSE; /* display messages */ 609 else 610 verbose_enter(); 611 ++no_wait_return; 612 if (debug_break_level > 0 || *p_vfile == NUL) 613 msg_scroll = TRUE; /* always scroll up, don't overwrite */ 614 smsg(was_finished 615 ? (char_u *)_("Exception finished: %s") 616 : (char_u *)_("Exception discarded: %s"), 617 excp->value); 618 msg_puts((char_u *)"\n"); /* don't overwrite this either */ 619 if (debug_break_level > 0 || *p_vfile == NUL) 620 cmdline_row = msg_row; 621 --no_wait_return; 622 if (debug_break_level > 0) 623 msg_silent = save_msg_silent; 624 else 625 verbose_leave(); 626 STRCPY(IObuff, saved_IObuff); 627 vim_free(saved_IObuff); 628 } 629 if (excp->type != ET_INTERRUPT) 630 vim_free(excp->value); 631 if (excp->type == ET_ERROR) 632 free_msglist(excp->messages); 633 vim_free(excp->throw_name); 634 vim_free(excp); 635 } 636 637 /* 638 * Discard the exception currently being thrown. 639 */ 640 void 641 discard_current_exception(void) 642 { 643 if (current_exception != NULL) 644 { 645 discard_exception(current_exception, FALSE); 646 current_exception = NULL; 647 } 648 did_throw = FALSE; 649 need_rethrow = FALSE; 650 } 651 652 /* 653 * Put an exception on the caught stack. 654 */ 655 static void 656 catch_exception(except_T *excp) 657 { 658 excp->caught = caught_stack; 659 caught_stack = excp; 660 set_vim_var_string(VV_EXCEPTION, excp->value, -1); 661 if (*excp->throw_name != NUL) 662 { 663 if (excp->throw_lnum != 0) 664 vim_snprintf((char *)IObuff, IOSIZE, _("%s, line %ld"), 665 excp->throw_name, (long)excp->throw_lnum); 666 else 667 vim_snprintf((char *)IObuff, IOSIZE, "%s", excp->throw_name); 668 set_vim_var_string(VV_THROWPOINT, IObuff, -1); 669 } 670 else 671 /* throw_name not set on an exception from a command that was typed. */ 672 set_vim_var_string(VV_THROWPOINT, NULL, -1); 673 674 if (p_verbose >= 13 || debug_break_level > 0) 675 { 676 int save_msg_silent = msg_silent; 677 678 if (debug_break_level > 0) 679 msg_silent = FALSE; /* display messages */ 680 else 681 verbose_enter(); 682 ++no_wait_return; 683 if (debug_break_level > 0 || *p_vfile == NUL) 684 msg_scroll = TRUE; /* always scroll up, don't overwrite */ 685 686 smsg((char_u *)_("Exception caught: %s"), excp->value); 687 msg_puts((char_u *)"\n"); /* don't overwrite this either */ 688 689 if (debug_break_level > 0 || *p_vfile == NUL) 690 cmdline_row = msg_row; 691 --no_wait_return; 692 if (debug_break_level > 0) 693 msg_silent = save_msg_silent; 694 else 695 verbose_leave(); 696 } 697 } 698 699 /* 700 * Remove an exception from the caught stack. 701 */ 702 static void 703 finish_exception(except_T *excp) 704 { 705 if (excp != caught_stack) 706 internal_error("finish_exception()"); 707 caught_stack = caught_stack->caught; 708 if (caught_stack != NULL) 709 { 710 set_vim_var_string(VV_EXCEPTION, caught_stack->value, -1); 711 if (*caught_stack->throw_name != NUL) 712 { 713 if (caught_stack->throw_lnum != 0) 714 vim_snprintf((char *)IObuff, IOSIZE, 715 _("%s, line %ld"), caught_stack->throw_name, 716 (long)caught_stack->throw_lnum); 717 else 718 vim_snprintf((char *)IObuff, IOSIZE, "%s", 719 caught_stack->throw_name); 720 set_vim_var_string(VV_THROWPOINT, IObuff, -1); 721 } 722 else 723 /* throw_name not set on an exception from a command that was 724 * typed. */ 725 set_vim_var_string(VV_THROWPOINT, NULL, -1); 726 } 727 else 728 { 729 set_vim_var_string(VV_EXCEPTION, NULL, -1); 730 set_vim_var_string(VV_THROWPOINT, NULL, -1); 731 } 732 733 /* Discard the exception, but use the finish message for 'verbose'. */ 734 discard_exception(excp, TRUE); 735 } 736 737 /* 738 * Flags specifying the message displayed by report_pending. 739 */ 740 #define RP_MAKE 0 741 #define RP_RESUME 1 742 #define RP_DISCARD 2 743 744 /* 745 * Report information about something pending in a finally clause if required by 746 * the 'verbose' option or when debugging. "action" tells whether something is 747 * made pending or something pending is resumed or discarded. "pending" tells 748 * what is pending. "value" specifies the return value for a pending ":return" 749 * or the exception value for a pending exception. 750 */ 751 static void 752 report_pending(int action, int pending, void *value) 753 { 754 char_u *mesg; 755 char *s; 756 int save_msg_silent; 757 758 759 switch (action) 760 { 761 case RP_MAKE: 762 mesg = (char_u *)_("%s made pending"); 763 break; 764 case RP_RESUME: 765 mesg = (char_u *)_("%s resumed"); 766 break; 767 /* case RP_DISCARD: */ 768 default: 769 mesg = (char_u *)_("%s discarded"); 770 break; 771 } 772 773 switch (pending) 774 { 775 case CSTP_NONE: 776 return; 777 778 case CSTP_CONTINUE: 779 s = ":continue"; 780 break; 781 case CSTP_BREAK: 782 s = ":break"; 783 break; 784 case CSTP_FINISH: 785 s = ":finish"; 786 break; 787 case CSTP_RETURN: 788 /* ":return" command producing value, allocated */ 789 s = (char *)get_return_cmd(value); 790 break; 791 792 default: 793 if (pending & CSTP_THROW) 794 { 795 vim_snprintf((char *)IObuff, IOSIZE, 796 (char *)mesg, _("Exception")); 797 mesg = vim_strnsave(IObuff, (int)STRLEN(IObuff) + 4); 798 STRCAT(mesg, ": %s"); 799 s = (char *)((except_T *)value)->value; 800 } 801 else if ((pending & CSTP_ERROR) && (pending & CSTP_INTERRUPT)) 802 s = _("Error and interrupt"); 803 else if (pending & CSTP_ERROR) 804 s = _("Error"); 805 else /* if (pending & CSTP_INTERRUPT) */ 806 s = _("Interrupt"); 807 } 808 809 save_msg_silent = msg_silent; 810 if (debug_break_level > 0) 811 msg_silent = FALSE; /* display messages */ 812 ++no_wait_return; 813 msg_scroll = TRUE; /* always scroll up, don't overwrite */ 814 smsg(mesg, (char_u *)s); 815 msg_puts((char_u *)"\n"); /* don't overwrite this either */ 816 cmdline_row = msg_row; 817 --no_wait_return; 818 if (debug_break_level > 0) 819 msg_silent = save_msg_silent; 820 821 if (pending == CSTP_RETURN) 822 vim_free(s); 823 else if (pending & CSTP_THROW) 824 vim_free(mesg); 825 } 826 827 /* 828 * If something is made pending in a finally clause, report it if required by 829 * the 'verbose' option or when debugging. 830 */ 831 void 832 report_make_pending(int pending, void *value) 833 { 834 if (p_verbose >= 14 || debug_break_level > 0) 835 { 836 if (debug_break_level <= 0) 837 verbose_enter(); 838 report_pending(RP_MAKE, pending, value); 839 if (debug_break_level <= 0) 840 verbose_leave(); 841 } 842 } 843 844 /* 845 * If something pending in a finally clause is resumed at the ":endtry", report 846 * it if required by the 'verbose' option or when debugging. 847 */ 848 void 849 report_resume_pending(int pending, void *value) 850 { 851 if (p_verbose >= 14 || debug_break_level > 0) 852 { 853 if (debug_break_level <= 0) 854 verbose_enter(); 855 report_pending(RP_RESUME, pending, value); 856 if (debug_break_level <= 0) 857 verbose_leave(); 858 } 859 } 860 861 /* 862 * If something pending in a finally clause is discarded, report it if required 863 * by the 'verbose' option or when debugging. 864 */ 865 void 866 report_discard_pending(int pending, void *value) 867 { 868 if (p_verbose >= 14 || debug_break_level > 0) 869 { 870 if (debug_break_level <= 0) 871 verbose_enter(); 872 report_pending(RP_DISCARD, pending, value); 873 if (debug_break_level <= 0) 874 verbose_leave(); 875 } 876 } 877 878 879 /* 880 * ":if". 881 */ 882 void 883 ex_if(exarg_T *eap) 884 { 885 int error; 886 int skip; 887 int result; 888 struct condstack *cstack = eap->cstack; 889 890 if (cstack->cs_idx == CSTACK_LEN - 1) 891 eap->errmsg = (char_u *)N_("E579: :if nesting too deep"); 892 else 893 { 894 ++cstack->cs_idx; 895 cstack->cs_flags[cstack->cs_idx] = 0; 896 897 /* 898 * Don't do something after an error, interrupt, or throw, or when there 899 * is a surrounding conditional and it was not active. 900 */ 901 skip = did_emsg || got_int || did_throw || (cstack->cs_idx > 0 902 && !(cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE)); 903 904 result = eval_to_bool(eap->arg, &error, &eap->nextcmd, skip); 905 906 if (!skip && !error) 907 { 908 if (result) 909 cstack->cs_flags[cstack->cs_idx] = CSF_ACTIVE | CSF_TRUE; 910 } 911 else 912 /* set TRUE, so this conditional will never get active */ 913 cstack->cs_flags[cstack->cs_idx] = CSF_TRUE; 914 } 915 } 916 917 /* 918 * ":endif". 919 */ 920 void 921 ex_endif(exarg_T *eap) 922 { 923 did_endif = TRUE; 924 if (eap->cstack->cs_idx < 0 925 || (eap->cstack->cs_flags[eap->cstack->cs_idx] 926 & (CSF_WHILE | CSF_FOR | CSF_TRY))) 927 eap->errmsg = (char_u *)N_("E580: :endif without :if"); 928 else 929 { 930 /* 931 * When debugging or a breakpoint was encountered, display the debug 932 * prompt (if not already done). This shows the user that an ":endif" 933 * is executed when the ":if" or a previous ":elseif" was not TRUE. 934 * Handle a ">quit" debug command as if an interrupt had occurred before 935 * the ":endif". That is, throw an interrupt exception if appropriate. 936 * Doing this here prevents an exception for a parsing error being 937 * discarded by throwing the interrupt exception later on. 938 */ 939 if (!(eap->cstack->cs_flags[eap->cstack->cs_idx] & CSF_TRUE) 940 && dbg_check_skipped(eap)) 941 (void)do_intthrow(eap->cstack); 942 943 --eap->cstack->cs_idx; 944 } 945 } 946 947 /* 948 * ":else" and ":elseif". 949 */ 950 void 951 ex_else(exarg_T *eap) 952 { 953 int error; 954 int skip; 955 int result; 956 struct condstack *cstack = eap->cstack; 957 958 /* 959 * Don't do something after an error, interrupt, or throw, or when there is 960 * a surrounding conditional and it was not active. 961 */ 962 skip = did_emsg || got_int || did_throw || (cstack->cs_idx > 0 963 && !(cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE)); 964 965 if (cstack->cs_idx < 0 966 || (cstack->cs_flags[cstack->cs_idx] 967 & (CSF_WHILE | CSF_FOR | CSF_TRY))) 968 { 969 if (eap->cmdidx == CMD_else) 970 { 971 eap->errmsg = (char_u *)N_("E581: :else without :if"); 972 return; 973 } 974 eap->errmsg = (char_u *)N_("E582: :elseif without :if"); 975 skip = TRUE; 976 } 977 else if (cstack->cs_flags[cstack->cs_idx] & CSF_ELSE) 978 { 979 if (eap->cmdidx == CMD_else) 980 { 981 eap->errmsg = (char_u *)N_("E583: multiple :else"); 982 return; 983 } 984 eap->errmsg = (char_u *)N_("E584: :elseif after :else"); 985 skip = TRUE; 986 } 987 988 /* if skipping or the ":if" was TRUE, reset ACTIVE, otherwise set it */ 989 if (skip || cstack->cs_flags[cstack->cs_idx] & CSF_TRUE) 990 { 991 if (eap->errmsg == NULL) 992 cstack->cs_flags[cstack->cs_idx] = CSF_TRUE; 993 skip = TRUE; /* don't evaluate an ":elseif" */ 994 } 995 else 996 cstack->cs_flags[cstack->cs_idx] = CSF_ACTIVE; 997 998 /* 999 * When debugging or a breakpoint was encountered, display the debug prompt 1000 * (if not already done). This shows the user that an ":else" or ":elseif" 1001 * is executed when the ":if" or previous ":elseif" was not TRUE. Handle 1002 * a ">quit" debug command as if an interrupt had occurred before the 1003 * ":else" or ":elseif". That is, set "skip" and throw an interrupt 1004 * exception if appropriate. Doing this here prevents that an exception 1005 * for a parsing errors is discarded when throwing the interrupt exception 1006 * later on. 1007 */ 1008 if (!skip && dbg_check_skipped(eap) && got_int) 1009 { 1010 (void)do_intthrow(cstack); 1011 skip = TRUE; 1012 } 1013 1014 if (eap->cmdidx == CMD_elseif) 1015 { 1016 result = eval_to_bool(eap->arg, &error, &eap->nextcmd, skip); 1017 /* When throwing error exceptions, we want to throw always the first 1018 * of several errors in a row. This is what actually happens when 1019 * a conditional error was detected above and there is another failure 1020 * when parsing the expression. Since the skip flag is set in this 1021 * case, the parsing error will be ignored by emsg(). */ 1022 1023 if (!skip && !error) 1024 { 1025 if (result) 1026 cstack->cs_flags[cstack->cs_idx] = CSF_ACTIVE | CSF_TRUE; 1027 else 1028 cstack->cs_flags[cstack->cs_idx] = 0; 1029 } 1030 else if (eap->errmsg == NULL) 1031 /* set TRUE, so this conditional will never get active */ 1032 cstack->cs_flags[cstack->cs_idx] = CSF_TRUE; 1033 } 1034 else 1035 cstack->cs_flags[cstack->cs_idx] |= CSF_ELSE; 1036 } 1037 1038 /* 1039 * Handle ":while" and ":for". 1040 */ 1041 void 1042 ex_while(exarg_T *eap) 1043 { 1044 int error; 1045 int skip; 1046 int result; 1047 struct condstack *cstack = eap->cstack; 1048 1049 if (cstack->cs_idx == CSTACK_LEN - 1) 1050 eap->errmsg = (char_u *)N_("E585: :while/:for nesting too deep"); 1051 else 1052 { 1053 /* 1054 * The loop flag is set when we have jumped back from the matching 1055 * ":endwhile" or ":endfor". When not set, need to initialise this 1056 * cstack entry. 1057 */ 1058 if ((cstack->cs_lflags & CSL_HAD_LOOP) == 0) 1059 { 1060 ++cstack->cs_idx; 1061 ++cstack->cs_looplevel; 1062 cstack->cs_line[cstack->cs_idx] = -1; 1063 } 1064 cstack->cs_flags[cstack->cs_idx] = 1065 eap->cmdidx == CMD_while ? CSF_WHILE : CSF_FOR; 1066 1067 /* 1068 * Don't do something after an error, interrupt, or throw, or when 1069 * there is a surrounding conditional and it was not active. 1070 */ 1071 skip = did_emsg || got_int || did_throw || (cstack->cs_idx > 0 1072 && !(cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE)); 1073 if (eap->cmdidx == CMD_while) 1074 { 1075 /* 1076 * ":while bool-expr" 1077 */ 1078 result = eval_to_bool(eap->arg, &error, &eap->nextcmd, skip); 1079 } 1080 else 1081 { 1082 void *fi; 1083 1084 /* 1085 * ":for var in list-expr" 1086 */ 1087 if ((cstack->cs_lflags & CSL_HAD_LOOP) != 0) 1088 { 1089 /* Jumping here from a ":continue" or ":endfor": use the 1090 * previously evaluated list. */ 1091 fi = cstack->cs_forinfo[cstack->cs_idx]; 1092 error = FALSE; 1093 } 1094 else 1095 { 1096 /* Evaluate the argument and get the info in a structure. */ 1097 fi = eval_for_line(eap->arg, &error, &eap->nextcmd, skip); 1098 cstack->cs_forinfo[cstack->cs_idx] = fi; 1099 } 1100 1101 /* use the element at the start of the list and advance */ 1102 if (!error && fi != NULL && !skip) 1103 result = next_for_item(fi, eap->arg); 1104 else 1105 result = FALSE; 1106 1107 if (!result) 1108 { 1109 free_for_info(fi); 1110 cstack->cs_forinfo[cstack->cs_idx] = NULL; 1111 } 1112 } 1113 1114 /* 1115 * If this cstack entry was just initialised and is active, set the 1116 * loop flag, so do_cmdline() will set the line number in cs_line[]. 1117 * If executing the command a second time, clear the loop flag. 1118 */ 1119 if (!skip && !error && result) 1120 { 1121 cstack->cs_flags[cstack->cs_idx] |= (CSF_ACTIVE | CSF_TRUE); 1122 cstack->cs_lflags ^= CSL_HAD_LOOP; 1123 } 1124 else 1125 { 1126 cstack->cs_lflags &= ~CSL_HAD_LOOP; 1127 /* If the ":while" evaluates to FALSE or ":for" is past the end of 1128 * the list, show the debug prompt at the ":endwhile"/":endfor" as 1129 * if there was a ":break" in a ":while"/":for" evaluating to 1130 * TRUE. */ 1131 if (!skip && !error) 1132 cstack->cs_flags[cstack->cs_idx] |= CSF_TRUE; 1133 } 1134 } 1135 } 1136 1137 /* 1138 * ":continue" 1139 */ 1140 void 1141 ex_continue(exarg_T *eap) 1142 { 1143 int idx; 1144 struct condstack *cstack = eap->cstack; 1145 1146 if (cstack->cs_looplevel <= 0 || cstack->cs_idx < 0) 1147 eap->errmsg = (char_u *)N_("E586: :continue without :while or :for"); 1148 else 1149 { 1150 /* Try to find the matching ":while". This might stop at a try 1151 * conditional not in its finally clause (which is then to be executed 1152 * next). Therefor, inactivate all conditionals except the ":while" 1153 * itself (if reached). */ 1154 idx = cleanup_conditionals(cstack, CSF_WHILE | CSF_FOR, FALSE); 1155 if (idx >= 0 && (cstack->cs_flags[idx] & (CSF_WHILE | CSF_FOR))) 1156 { 1157 rewind_conditionals(cstack, idx, CSF_TRY, &cstack->cs_trylevel); 1158 1159 /* 1160 * Set CSL_HAD_CONT, so do_cmdline() will jump back to the 1161 * matching ":while". 1162 */ 1163 cstack->cs_lflags |= CSL_HAD_CONT; /* let do_cmdline() handle it */ 1164 } 1165 else 1166 { 1167 /* If a try conditional not in its finally clause is reached first, 1168 * make the ":continue" pending for execution at the ":endtry". */ 1169 cstack->cs_pending[idx] = CSTP_CONTINUE; 1170 report_make_pending(CSTP_CONTINUE, NULL); 1171 } 1172 } 1173 } 1174 1175 /* 1176 * ":break" 1177 */ 1178 void 1179 ex_break(exarg_T *eap) 1180 { 1181 int idx; 1182 struct condstack *cstack = eap->cstack; 1183 1184 if (cstack->cs_looplevel <= 0 || cstack->cs_idx < 0) 1185 eap->errmsg = (char_u *)N_("E587: :break without :while or :for"); 1186 else 1187 { 1188 /* Inactivate conditionals until the matching ":while" or a try 1189 * conditional not in its finally clause (which is then to be 1190 * executed next) is found. In the latter case, make the ":break" 1191 * pending for execution at the ":endtry". */ 1192 idx = cleanup_conditionals(cstack, CSF_WHILE | CSF_FOR, TRUE); 1193 if (idx >= 0 && !(cstack->cs_flags[idx] & (CSF_WHILE | CSF_FOR))) 1194 { 1195 cstack->cs_pending[idx] = CSTP_BREAK; 1196 report_make_pending(CSTP_BREAK, NULL); 1197 } 1198 } 1199 } 1200 1201 /* 1202 * ":endwhile" and ":endfor" 1203 */ 1204 void 1205 ex_endwhile(exarg_T *eap) 1206 { 1207 struct condstack *cstack = eap->cstack; 1208 int idx; 1209 char_u *err; 1210 int csf; 1211 int fl; 1212 1213 if (eap->cmdidx == CMD_endwhile) 1214 { 1215 err = e_while; 1216 csf = CSF_WHILE; 1217 } 1218 else 1219 { 1220 err = e_for; 1221 csf = CSF_FOR; 1222 } 1223 1224 if (cstack->cs_looplevel <= 0 || cstack->cs_idx < 0) 1225 eap->errmsg = err; 1226 else 1227 { 1228 fl = cstack->cs_flags[cstack->cs_idx]; 1229 if (!(fl & csf)) 1230 { 1231 /* If we are in a ":while" or ":for" but used the wrong endloop 1232 * command, do not rewind to the next enclosing ":for"/":while". */ 1233 if (fl & CSF_WHILE) 1234 eap->errmsg = (char_u *)_("E732: Using :endfor with :while"); 1235 else if (fl & CSF_FOR) 1236 eap->errmsg = (char_u *)_("E733: Using :endwhile with :for"); 1237 } 1238 if (!(fl & (CSF_WHILE | CSF_FOR))) 1239 { 1240 if (!(fl & CSF_TRY)) 1241 eap->errmsg = e_endif; 1242 else if (fl & CSF_FINALLY) 1243 eap->errmsg = e_endtry; 1244 /* Try to find the matching ":while" and report what's missing. */ 1245 for (idx = cstack->cs_idx; idx > 0; --idx) 1246 { 1247 fl = cstack->cs_flags[idx]; 1248 if ((fl & CSF_TRY) && !(fl & CSF_FINALLY)) 1249 { 1250 /* Give up at a try conditional not in its finally clause. 1251 * Ignore the ":endwhile"/":endfor". */ 1252 eap->errmsg = err; 1253 return; 1254 } 1255 if (fl & csf) 1256 break; 1257 } 1258 /* Cleanup and rewind all contained (and unclosed) conditionals. */ 1259 (void)cleanup_conditionals(cstack, CSF_WHILE | CSF_FOR, FALSE); 1260 rewind_conditionals(cstack, idx, CSF_TRY, &cstack->cs_trylevel); 1261 } 1262 1263 /* 1264 * When debugging or a breakpoint was encountered, display the debug 1265 * prompt (if not already done). This shows the user that an 1266 * ":endwhile"/":endfor" is executed when the ":while" was not TRUE or 1267 * after a ":break". Handle a ">quit" debug command as if an 1268 * interrupt had occurred before the ":endwhile"/":endfor". That is, 1269 * throw an interrupt exception if appropriate. Doing this here 1270 * prevents that an exception for a parsing error is discarded when 1271 * throwing the interrupt exception later on. 1272 */ 1273 else if (cstack->cs_flags[cstack->cs_idx] & CSF_TRUE 1274 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE) 1275 && dbg_check_skipped(eap)) 1276 (void)do_intthrow(cstack); 1277 1278 /* 1279 * Set loop flag, so do_cmdline() will jump back to the matching 1280 * ":while" or ":for". 1281 */ 1282 cstack->cs_lflags |= CSL_HAD_ENDLOOP; 1283 } 1284 } 1285 1286 1287 /* 1288 * ":throw expr" 1289 */ 1290 void 1291 ex_throw(exarg_T *eap) 1292 { 1293 char_u *arg = eap->arg; 1294 char_u *value; 1295 1296 if (*arg != NUL && *arg != '|' && *arg != '\n') 1297 value = eval_to_string_skip(arg, &eap->nextcmd, eap->skip); 1298 else 1299 { 1300 EMSG(_(e_argreq)); 1301 value = NULL; 1302 } 1303 1304 /* On error or when an exception is thrown during argument evaluation, do 1305 * not throw. */ 1306 if (!eap->skip && value != NULL) 1307 { 1308 if (throw_exception(value, ET_USER, NULL) == FAIL) 1309 vim_free(value); 1310 else 1311 do_throw(eap->cstack); 1312 } 1313 } 1314 1315 /* 1316 * Throw the current exception through the specified cstack. Common routine 1317 * for ":throw" (user exception) and error and interrupt exceptions. Also 1318 * used for rethrowing an uncaught exception. 1319 */ 1320 void 1321 do_throw(struct condstack *cstack) 1322 { 1323 int idx; 1324 int inactivate_try = FALSE; 1325 1326 /* 1327 * Cleanup and inactivate up to the next surrounding try conditional that 1328 * is not in its finally clause. Normally, do not inactivate the try 1329 * conditional itself, so that its ACTIVE flag can be tested below. But 1330 * if a previous error or interrupt has not been converted to an exception, 1331 * inactivate the try conditional, too, as if the conversion had been done, 1332 * and reset the did_emsg or got_int flag, so this won't happen again at 1333 * the next surrounding try conditional. 1334 */ 1335 #ifndef THROW_ON_ERROR_TRUE 1336 if (did_emsg && !THROW_ON_ERROR) 1337 { 1338 inactivate_try = TRUE; 1339 did_emsg = FALSE; 1340 } 1341 #endif 1342 #ifndef THROW_ON_INTERRUPT_TRUE 1343 if (got_int && !THROW_ON_INTERRUPT) 1344 { 1345 inactivate_try = TRUE; 1346 got_int = FALSE; 1347 } 1348 #endif 1349 idx = cleanup_conditionals(cstack, 0, inactivate_try); 1350 if (idx >= 0) 1351 { 1352 /* 1353 * If this try conditional is active and we are before its first 1354 * ":catch", set THROWN so that the ":catch" commands will check 1355 * whether the exception matches. When the exception came from any of 1356 * the catch clauses, it will be made pending at the ":finally" (if 1357 * present) and rethrown at the ":endtry". This will also happen if 1358 * the try conditional is inactive. This is the case when we are 1359 * throwing an exception due to an error or interrupt on the way from 1360 * a preceding ":continue", ":break", ":return", ":finish", error or 1361 * interrupt (not converted to an exception) to the finally clause or 1362 * from a preceding throw of a user or error or interrupt exception to 1363 * the matching catch clause or the finally clause. 1364 */ 1365 if (!(cstack->cs_flags[idx] & CSF_CAUGHT)) 1366 { 1367 if (cstack->cs_flags[idx] & CSF_ACTIVE) 1368 cstack->cs_flags[idx] |= CSF_THROWN; 1369 else 1370 /* THROWN may have already been set for a catchable exception 1371 * that has been discarded. Ensure it is reset for the new 1372 * exception. */ 1373 cstack->cs_flags[idx] &= ~CSF_THROWN; 1374 } 1375 cstack->cs_flags[idx] &= ~CSF_ACTIVE; 1376 cstack->cs_exception[idx] = current_exception; 1377 } 1378 #if 0 1379 /* TODO: Add optimization below. Not yet done because of interface 1380 * problems to eval.c and ex_cmds2.c. (Servatius) */ 1381 else 1382 { 1383 /* 1384 * There are no catch clauses to check or finally clauses to execute. 1385 * End the current script or function. The exception will be rethrown 1386 * in the caller. 1387 */ 1388 if (getline_equal(eap->getline, eap->cookie, get_func_line)) 1389 current_funccal->returned = TRUE; 1390 elseif (eap->get_func_line == getsourceline) 1391 ((struct source_cookie *)eap->cookie)->finished = TRUE; 1392 } 1393 #endif 1394 1395 did_throw = TRUE; 1396 } 1397 1398 /* 1399 * ":try" 1400 */ 1401 void 1402 ex_try(exarg_T *eap) 1403 { 1404 int skip; 1405 struct condstack *cstack = eap->cstack; 1406 1407 if (cstack->cs_idx == CSTACK_LEN - 1) 1408 eap->errmsg = (char_u *)N_("E601: :try nesting too deep"); 1409 else 1410 { 1411 ++cstack->cs_idx; 1412 ++cstack->cs_trylevel; 1413 cstack->cs_flags[cstack->cs_idx] = CSF_TRY; 1414 cstack->cs_pending[cstack->cs_idx] = CSTP_NONE; 1415 1416 /* 1417 * Don't do something after an error, interrupt, or throw, or when there 1418 * is a surrounding conditional and it was not active. 1419 */ 1420 skip = did_emsg || got_int || did_throw || (cstack->cs_idx > 0 1421 && !(cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE)); 1422 1423 if (!skip) 1424 { 1425 /* Set ACTIVE and TRUE. TRUE means that the corresponding ":catch" 1426 * commands should check for a match if an exception is thrown and 1427 * that the finally clause needs to be executed. */ 1428 cstack->cs_flags[cstack->cs_idx] |= CSF_ACTIVE | CSF_TRUE; 1429 1430 /* 1431 * ":silent!", even when used in a try conditional, disables 1432 * displaying of error messages and conversion of errors to 1433 * exceptions. When the silent commands again open a try 1434 * conditional, save "emsg_silent" and reset it so that errors are 1435 * again converted to exceptions. The value is restored when that 1436 * try conditional is left. If it is left normally, the commands 1437 * following the ":endtry" are again silent. If it is left by 1438 * a ":continue", ":break", ":return", or ":finish", the commands 1439 * executed next are again silent. If it is left due to an 1440 * aborting error, an interrupt, or an exception, restoring 1441 * "emsg_silent" does not matter since we are already in the 1442 * aborting state and/or the exception has already been thrown. 1443 * The effect is then just freeing the memory that was allocated 1444 * to save the value. 1445 */ 1446 if (emsg_silent) 1447 { 1448 eslist_T *elem; 1449 1450 elem = (eslist_T *)alloc((unsigned)sizeof(struct eslist_elem)); 1451 if (elem == NULL) 1452 EMSG(_(e_outofmem)); 1453 else 1454 { 1455 elem->saved_emsg_silent = emsg_silent; 1456 elem->next = cstack->cs_emsg_silent_list; 1457 cstack->cs_emsg_silent_list = elem; 1458 cstack->cs_flags[cstack->cs_idx] |= CSF_SILENT; 1459 emsg_silent = 0; 1460 } 1461 } 1462 } 1463 1464 } 1465 } 1466 1467 /* 1468 * ":catch /{pattern}/" and ":catch" 1469 */ 1470 void 1471 ex_catch(exarg_T *eap) 1472 { 1473 int idx = 0; 1474 int give_up = FALSE; 1475 int skip = FALSE; 1476 int caught = FALSE; 1477 char_u *end; 1478 int save_char = 0; 1479 char_u *save_cpo; 1480 regmatch_T regmatch; 1481 int prev_got_int; 1482 struct condstack *cstack = eap->cstack; 1483 char_u *pat; 1484 1485 if (cstack->cs_trylevel <= 0 || cstack->cs_idx < 0) 1486 { 1487 eap->errmsg = (char_u *)N_("E603: :catch without :try"); 1488 give_up = TRUE; 1489 } 1490 else 1491 { 1492 if (!(cstack->cs_flags[cstack->cs_idx] & CSF_TRY)) 1493 { 1494 /* Report what's missing if the matching ":try" is not in its 1495 * finally clause. */ 1496 eap->errmsg = get_end_emsg(cstack); 1497 skip = TRUE; 1498 } 1499 for (idx = cstack->cs_idx; idx > 0; --idx) 1500 if (cstack->cs_flags[idx] & CSF_TRY) 1501 break; 1502 if (cstack->cs_flags[idx] & CSF_FINALLY) 1503 { 1504 /* Give up for a ":catch" after ":finally" and ignore it. 1505 * Just parse. */ 1506 eap->errmsg = (char_u *)N_("E604: :catch after :finally"); 1507 give_up = TRUE; 1508 } 1509 else 1510 rewind_conditionals(cstack, idx, CSF_WHILE | CSF_FOR, 1511 &cstack->cs_looplevel); 1512 } 1513 1514 if (ends_excmd(*eap->arg)) /* no argument, catch all errors */ 1515 { 1516 pat = (char_u *)".*"; 1517 end = NULL; 1518 eap->nextcmd = find_nextcmd(eap->arg); 1519 } 1520 else 1521 { 1522 pat = eap->arg + 1; 1523 end = skip_regexp(pat, *eap->arg, TRUE, NULL); 1524 } 1525 1526 if (!give_up) 1527 { 1528 /* 1529 * Don't do something when no exception has been thrown or when the 1530 * corresponding try block never got active (because of an inactive 1531 * surrounding conditional or after an error or interrupt or throw). 1532 */ 1533 if (!did_throw || !(cstack->cs_flags[idx] & CSF_TRUE)) 1534 skip = TRUE; 1535 1536 /* 1537 * Check for a match only if an exception is thrown but not caught by 1538 * a previous ":catch". An exception that has replaced a discarded 1539 * exception is not checked (THROWN is not set then). 1540 */ 1541 if (!skip && (cstack->cs_flags[idx] & CSF_THROWN) 1542 && !(cstack->cs_flags[idx] & CSF_CAUGHT)) 1543 { 1544 if (end != NULL && *end != NUL && !ends_excmd(*skipwhite(end + 1))) 1545 { 1546 EMSG(_(e_trailing)); 1547 return; 1548 } 1549 1550 /* When debugging or a breakpoint was encountered, display the 1551 * debug prompt (if not already done) before checking for a match. 1552 * This is a helpful hint for the user when the regular expression 1553 * matching fails. Handle a ">quit" debug command as if an 1554 * interrupt had occurred before the ":catch". That is, discard 1555 * the original exception, replace it by an interrupt exception, 1556 * and don't catch it in this try block. */ 1557 if (!dbg_check_skipped(eap) || !do_intthrow(cstack)) 1558 { 1559 /* Terminate the pattern and avoid the 'l' flag in 'cpoptions' 1560 * while compiling it. */ 1561 if (end != NULL) 1562 { 1563 save_char = *end; 1564 *end = NUL; 1565 } 1566 save_cpo = p_cpo; 1567 p_cpo = (char_u *)""; 1568 /* Disable error messages, it will make current_exception 1569 * invalid. */ 1570 ++emsg_off; 1571 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING); 1572 --emsg_off; 1573 regmatch.rm_ic = FALSE; 1574 if (end != NULL) 1575 *end = save_char; 1576 p_cpo = save_cpo; 1577 if (regmatch.regprog == NULL) 1578 EMSG2(_(e_invarg2), pat); 1579 else 1580 { 1581 /* 1582 * Save the value of got_int and reset it. We don't want 1583 * a previous interruption cancel matching, only hitting 1584 * CTRL-C while matching should abort it. 1585 */ 1586 prev_got_int = got_int; 1587 got_int = FALSE; 1588 caught = vim_regexec_nl(®match, current_exception->value, 1589 (colnr_T)0); 1590 got_int |= prev_got_int; 1591 vim_regfree(regmatch.regprog); 1592 } 1593 } 1594 } 1595 1596 if (caught) 1597 { 1598 /* Make this ":catch" clause active and reset did_emsg, got_int, 1599 * and did_throw. Put the exception on the caught stack. */ 1600 cstack->cs_flags[idx] |= CSF_ACTIVE | CSF_CAUGHT; 1601 did_emsg = got_int = did_throw = FALSE; 1602 catch_exception((except_T *)cstack->cs_exception[idx]); 1603 /* It's mandatory that the current exception is stored in the cstack 1604 * so that it can be discarded at the next ":catch", ":finally", or 1605 * ":endtry" or when the catch clause is left by a ":continue", 1606 * ":break", ":return", ":finish", error, interrupt, or another 1607 * exception. */ 1608 if (cstack->cs_exception[cstack->cs_idx] != current_exception) 1609 internal_error("ex_catch()"); 1610 } 1611 else 1612 { 1613 /* 1614 * If there is a preceding catch clause and it caught the exception, 1615 * finish the exception now. This happens also after errors except 1616 * when this ":catch" was after the ":finally" or not within 1617 * a ":try". Make the try conditional inactive so that the 1618 * following catch clauses are skipped. On an error or interrupt 1619 * after the preceding try block or catch clause was left by 1620 * a ":continue", ":break", ":return", or ":finish", discard the 1621 * pending action. 1622 */ 1623 cleanup_conditionals(cstack, CSF_TRY, TRUE); 1624 } 1625 } 1626 1627 if (end != NULL) 1628 eap->nextcmd = find_nextcmd(end); 1629 } 1630 1631 /* 1632 * ":finally" 1633 */ 1634 void 1635 ex_finally(exarg_T *eap) 1636 { 1637 int idx; 1638 int skip = FALSE; 1639 int pending = CSTP_NONE; 1640 struct condstack *cstack = eap->cstack; 1641 1642 if (cstack->cs_trylevel <= 0 || cstack->cs_idx < 0) 1643 eap->errmsg = (char_u *)N_("E606: :finally without :try"); 1644 else 1645 { 1646 if (!(cstack->cs_flags[cstack->cs_idx] & CSF_TRY)) 1647 { 1648 eap->errmsg = get_end_emsg(cstack); 1649 for (idx = cstack->cs_idx - 1; idx > 0; --idx) 1650 if (cstack->cs_flags[idx] & CSF_TRY) 1651 break; 1652 /* Make this error pending, so that the commands in the following 1653 * finally clause can be executed. This overrules also a pending 1654 * ":continue", ":break", ":return", or ":finish". */ 1655 pending = CSTP_ERROR; 1656 } 1657 else 1658 idx = cstack->cs_idx; 1659 1660 if (cstack->cs_flags[idx] & CSF_FINALLY) 1661 { 1662 /* Give up for a multiple ":finally" and ignore it. */ 1663 eap->errmsg = (char_u *)N_("E607: multiple :finally"); 1664 return; 1665 } 1666 rewind_conditionals(cstack, idx, CSF_WHILE | CSF_FOR, 1667 &cstack->cs_looplevel); 1668 1669 /* 1670 * Don't do something when the corresponding try block never got active 1671 * (because of an inactive surrounding conditional or after an error or 1672 * interrupt or throw) or for a ":finally" without ":try" or a multiple 1673 * ":finally". After every other error (did_emsg or the conditional 1674 * errors detected above) or after an interrupt (got_int) or an 1675 * exception (did_throw), the finally clause must be executed. 1676 */ 1677 skip = !(cstack->cs_flags[cstack->cs_idx] & CSF_TRUE); 1678 1679 if (!skip) 1680 { 1681 /* When debugging or a breakpoint was encountered, display the 1682 * debug prompt (if not already done). The user then knows that the 1683 * finally clause is executed. */ 1684 if (dbg_check_skipped(eap)) 1685 { 1686 /* Handle a ">quit" debug command as if an interrupt had 1687 * occurred before the ":finally". That is, discard the 1688 * original exception and replace it by an interrupt 1689 * exception. */ 1690 (void)do_intthrow(cstack); 1691 } 1692 1693 /* 1694 * If there is a preceding catch clause and it caught the exception, 1695 * finish the exception now. This happens also after errors except 1696 * when this is a multiple ":finally" or one not within a ":try". 1697 * After an error or interrupt, this also discards a pending 1698 * ":continue", ":break", ":finish", or ":return" from the preceding 1699 * try block or catch clause. 1700 */ 1701 cleanup_conditionals(cstack, CSF_TRY, FALSE); 1702 1703 /* 1704 * Make did_emsg, got_int, did_throw pending. If set, they overrule 1705 * a pending ":continue", ":break", ":return", or ":finish". Then 1706 * we have particularly to discard a pending return value (as done 1707 * by the call to cleanup_conditionals() above when did_emsg or 1708 * got_int is set). The pending values are restored by the 1709 * ":endtry", except if there is a new error, interrupt, exception, 1710 * ":continue", ":break", ":return", or ":finish" in the following 1711 * finally clause. A missing ":endwhile", ":endfor" or ":endif" 1712 * detected here is treated as if did_emsg and did_throw had 1713 * already been set, respectively in case that the error is not 1714 * converted to an exception, did_throw had already been unset. 1715 * We must not set did_emsg here since that would suppress the 1716 * error message. 1717 */ 1718 if (pending == CSTP_ERROR || did_emsg || got_int || did_throw) 1719 { 1720 if (cstack->cs_pending[cstack->cs_idx] == CSTP_RETURN) 1721 { 1722 report_discard_pending(CSTP_RETURN, 1723 cstack->cs_rettv[cstack->cs_idx]); 1724 discard_pending_return(cstack->cs_rettv[cstack->cs_idx]); 1725 } 1726 if (pending == CSTP_ERROR && !did_emsg) 1727 pending |= (THROW_ON_ERROR) ? CSTP_THROW : 0; 1728 else 1729 pending |= did_throw ? CSTP_THROW : 0; 1730 pending |= did_emsg ? CSTP_ERROR : 0; 1731 pending |= got_int ? CSTP_INTERRUPT : 0; 1732 cstack->cs_pending[cstack->cs_idx] = pending; 1733 1734 /* It's mandatory that the current exception is stored in the 1735 * cstack so that it can be rethrown at the ":endtry" or be 1736 * discarded if the finally clause is left by a ":continue", 1737 * ":break", ":return", ":finish", error, interrupt, or another 1738 * exception. When emsg() is called for a missing ":endif" or 1739 * a missing ":endwhile"/":endfor" detected here, the 1740 * exception will be discarded. */ 1741 if (did_throw && cstack->cs_exception[cstack->cs_idx] 1742 != current_exception) 1743 internal_error("ex_finally()"); 1744 } 1745 1746 /* 1747 * Set CSL_HAD_FINA, so do_cmdline() will reset did_emsg, 1748 * got_int, and did_throw and make the finally clause active. 1749 * This will happen after emsg() has been called for a missing 1750 * ":endif" or a missing ":endwhile"/":endfor" detected here, so 1751 * that the following finally clause will be executed even then. 1752 */ 1753 cstack->cs_lflags |= CSL_HAD_FINA; 1754 } 1755 } 1756 } 1757 1758 /* 1759 * ":endtry" 1760 */ 1761 void 1762 ex_endtry(exarg_T *eap) 1763 { 1764 int idx; 1765 int skip; 1766 int rethrow = FALSE; 1767 int pending = CSTP_NONE; 1768 void *rettv = NULL; 1769 struct condstack *cstack = eap->cstack; 1770 1771 if (cstack->cs_trylevel <= 0 || cstack->cs_idx < 0) 1772 eap->errmsg = (char_u *)N_("E602: :endtry without :try"); 1773 else 1774 { 1775 /* 1776 * Don't do something after an error, interrupt or throw in the try 1777 * block, catch clause, or finally clause preceding this ":endtry" or 1778 * when an error or interrupt occurred after a ":continue", ":break", 1779 * ":return", or ":finish" in a try block or catch clause preceding this 1780 * ":endtry" or when the try block never got active (because of an 1781 * inactive surrounding conditional or after an error or interrupt or 1782 * throw) or when there is a surrounding conditional and it has been 1783 * made inactive by a ":continue", ":break", ":return", or ":finish" in 1784 * the finally clause. The latter case need not be tested since then 1785 * anything pending has already been discarded. */ 1786 skip = did_emsg || got_int || did_throw || 1787 !(cstack->cs_flags[cstack->cs_idx] & CSF_TRUE); 1788 1789 if (!(cstack->cs_flags[cstack->cs_idx] & CSF_TRY)) 1790 { 1791 eap->errmsg = get_end_emsg(cstack); 1792 /* Find the matching ":try" and report what's missing. */ 1793 idx = cstack->cs_idx; 1794 do 1795 --idx; 1796 while (idx > 0 && !(cstack->cs_flags[idx] & CSF_TRY)); 1797 rewind_conditionals(cstack, idx, CSF_WHILE | CSF_FOR, 1798 &cstack->cs_looplevel); 1799 skip = TRUE; 1800 1801 /* 1802 * If an exception is being thrown, discard it to prevent it from 1803 * being rethrown at the end of this function. It would be 1804 * discarded by the error message, anyway. Resets did_throw. 1805 * This does not affect the script termination due to the error 1806 * since "trylevel" is decremented after emsg() has been called. 1807 */ 1808 if (did_throw) 1809 discard_current_exception(); 1810 } 1811 else 1812 { 1813 idx = cstack->cs_idx; 1814 1815 /* 1816 * If we stopped with the exception currently being thrown at this 1817 * try conditional since we didn't know that it doesn't have 1818 * a finally clause, we need to rethrow it after closing the try 1819 * conditional. 1820 */ 1821 if (did_throw && (cstack->cs_flags[idx] & CSF_TRUE) 1822 && !(cstack->cs_flags[idx] & CSF_FINALLY)) 1823 rethrow = TRUE; 1824 } 1825 1826 /* If there was no finally clause, show the user when debugging or 1827 * a breakpoint was encountered that the end of the try conditional has 1828 * been reached: display the debug prompt (if not already done). Do 1829 * this on normal control flow or when an exception was thrown, but not 1830 * on an interrupt or error not converted to an exception or when 1831 * a ":break", ":continue", ":return", or ":finish" is pending. These 1832 * actions are carried out immediately. 1833 */ 1834 if ((rethrow || (!skip 1835 && !(cstack->cs_flags[idx] & CSF_FINALLY) 1836 && !cstack->cs_pending[idx])) 1837 && dbg_check_skipped(eap)) 1838 { 1839 /* Handle a ">quit" debug command as if an interrupt had occurred 1840 * before the ":endtry". That is, throw an interrupt exception and 1841 * set "skip" and "rethrow". */ 1842 if (got_int) 1843 { 1844 skip = TRUE; 1845 (void)do_intthrow(cstack); 1846 /* The do_intthrow() call may have reset did_throw or 1847 * cstack->cs_pending[idx].*/ 1848 rethrow = FALSE; 1849 if (did_throw && !(cstack->cs_flags[idx] & CSF_FINALLY)) 1850 rethrow = TRUE; 1851 } 1852 } 1853 1854 /* 1855 * If a ":return" is pending, we need to resume it after closing the 1856 * try conditional; remember the return value. If there was a finally 1857 * clause making an exception pending, we need to rethrow it. Make it 1858 * the exception currently being thrown. 1859 */ 1860 if (!skip) 1861 { 1862 pending = cstack->cs_pending[idx]; 1863 cstack->cs_pending[idx] = CSTP_NONE; 1864 if (pending == CSTP_RETURN) 1865 rettv = cstack->cs_rettv[idx]; 1866 else if (pending & CSTP_THROW) 1867 current_exception = cstack->cs_exception[idx]; 1868 } 1869 1870 /* 1871 * Discard anything pending on an error, interrupt, or throw in the 1872 * finally clause. If there was no ":finally", discard a pending 1873 * ":continue", ":break", ":return", or ":finish" if an error or 1874 * interrupt occurred afterwards, but before the ":endtry" was reached. 1875 * If an exception was caught by the last of the catch clauses and there 1876 * was no finally clause, finish the exception now. This happens also 1877 * after errors except when this ":endtry" is not within a ":try". 1878 * Restore "emsg_silent" if it has been reset by this try conditional. 1879 */ 1880 (void)cleanup_conditionals(cstack, CSF_TRY | CSF_SILENT, TRUE); 1881 1882 --cstack->cs_idx; 1883 --cstack->cs_trylevel; 1884 1885 if (!skip) 1886 { 1887 report_resume_pending(pending, 1888 (pending == CSTP_RETURN) ? rettv : 1889 (pending & CSTP_THROW) ? (void *)current_exception : NULL); 1890 switch (pending) 1891 { 1892 case CSTP_NONE: 1893 break; 1894 1895 /* Reactivate a pending ":continue", ":break", ":return", 1896 * ":finish" from the try block or a catch clause of this try 1897 * conditional. This is skipped, if there was an error in an 1898 * (unskipped) conditional command or an interrupt afterwards 1899 * or if the finally clause is present and executed a new error, 1900 * interrupt, throw, ":continue", ":break", ":return", or 1901 * ":finish". */ 1902 case CSTP_CONTINUE: 1903 ex_continue(eap); 1904 break; 1905 case CSTP_BREAK: 1906 ex_break(eap); 1907 break; 1908 case CSTP_RETURN: 1909 do_return(eap, FALSE, FALSE, rettv); 1910 break; 1911 case CSTP_FINISH: 1912 do_finish(eap, FALSE); 1913 break; 1914 1915 /* When the finally clause was entered due to an error, 1916 * interrupt or throw (as opposed to a ":continue", ":break", 1917 * ":return", or ":finish"), restore the pending values of 1918 * did_emsg, got_int, and did_throw. This is skipped, if there 1919 * was a new error, interrupt, throw, ":continue", ":break", 1920 * ":return", or ":finish". in the finally clause. */ 1921 default: 1922 if (pending & CSTP_ERROR) 1923 did_emsg = TRUE; 1924 if (pending & CSTP_INTERRUPT) 1925 got_int = TRUE; 1926 if (pending & CSTP_THROW) 1927 rethrow = TRUE; 1928 break; 1929 } 1930 } 1931 1932 if (rethrow) 1933 /* Rethrow the current exception (within this cstack). */ 1934 do_throw(cstack); 1935 } 1936 } 1937 1938 /* 1939 * enter_cleanup() and leave_cleanup() 1940 * 1941 * Functions to be called before/after invoking a sequence of autocommands for 1942 * cleanup for a failed command. (Failure means here that a call to emsg() 1943 * has been made, an interrupt occurred, or there is an uncaught exception 1944 * from a previous autocommand execution of the same command.) 1945 * 1946 * Call enter_cleanup() with a pointer to a cleanup_T and pass the same 1947 * pointer to leave_cleanup(). The cleanup_T structure stores the pending 1948 * error/interrupt/exception state. 1949 */ 1950 1951 /* 1952 * This function works a bit like ex_finally() except that there was not 1953 * actually an extra try block around the part that failed and an error or 1954 * interrupt has not (yet) been converted to an exception. This function 1955 * saves the error/interrupt/ exception state and prepares for the call to 1956 * do_cmdline() that is going to be made for the cleanup autocommand 1957 * execution. 1958 */ 1959 void 1960 enter_cleanup(cleanup_T *csp) 1961 { 1962 int pending = CSTP_NONE; 1963 1964 /* 1965 * Postpone did_emsg, got_int, did_throw. The pending values will be 1966 * restored by leave_cleanup() except if there was an aborting error, 1967 * interrupt, or uncaught exception after this function ends. 1968 */ 1969 if (did_emsg || got_int || did_throw || need_rethrow) 1970 { 1971 csp->pending = (did_emsg ? CSTP_ERROR : 0) 1972 | (got_int ? CSTP_INTERRUPT : 0) 1973 | (did_throw ? CSTP_THROW : 0) 1974 | (need_rethrow ? CSTP_THROW : 0); 1975 1976 /* If we are currently throwing an exception (did_throw), save it as 1977 * well. On an error not yet converted to an exception, update 1978 * "force_abort" and reset "cause_abort" (as do_errthrow() would do). 1979 * This is needed for the do_cmdline() call that is going to be made 1980 * for autocommand execution. We need not save *msg_list because 1981 * there is an extra instance for every call of do_cmdline(), anyway. 1982 */ 1983 if (did_throw || need_rethrow) 1984 { 1985 csp->exception = current_exception; 1986 current_exception = NULL; 1987 } 1988 else 1989 { 1990 csp->exception = NULL; 1991 if (did_emsg) 1992 { 1993 force_abort |= cause_abort; 1994 cause_abort = FALSE; 1995 } 1996 } 1997 did_emsg = got_int = did_throw = need_rethrow = FALSE; 1998 1999 /* Report if required by the 'verbose' option or when debugging. */ 2000 report_make_pending(pending, csp->exception); 2001 } 2002 else 2003 { 2004 csp->pending = CSTP_NONE; 2005 csp->exception = NULL; 2006 } 2007 } 2008 2009 /* 2010 * See comment above enter_cleanup() for how this function is used. 2011 * 2012 * This function is a bit like ex_endtry() except that there was not actually 2013 * an extra try block around the part that failed and an error or interrupt 2014 * had not (yet) been converted to an exception when the cleanup autocommand 2015 * sequence was invoked. 2016 * 2017 * This function has to be called with the address of the cleanup_T structure 2018 * filled by enter_cleanup() as an argument; it restores the error/interrupt/ 2019 * exception state saved by that function - except there was an aborting 2020 * error, an interrupt or an uncaught exception during execution of the 2021 * cleanup autocommands. In the latter case, the saved error/interrupt/ 2022 * exception state is discarded. 2023 */ 2024 void 2025 leave_cleanup(cleanup_T *csp) 2026 { 2027 int pending = csp->pending; 2028 2029 if (pending == CSTP_NONE) /* nothing to do */ 2030 return; 2031 2032 /* If there was an aborting error, an interrupt, or an uncaught exception 2033 * after the corresponding call to enter_cleanup(), discard what has been 2034 * made pending by it. Report this to the user if required by the 2035 * 'verbose' option or when debugging. */ 2036 if (aborting() || need_rethrow) 2037 { 2038 if (pending & CSTP_THROW) 2039 /* Cancel the pending exception (includes report). */ 2040 discard_exception((except_T *)csp->exception, FALSE); 2041 else 2042 report_discard_pending(pending, NULL); 2043 2044 /* If an error was about to be converted to an exception when 2045 * enter_cleanup() was called, free the message list. */ 2046 if (msg_list != NULL) 2047 free_global_msglist(); 2048 } 2049 2050 /* 2051 * If there was no new error, interrupt, or throw between the calls 2052 * to enter_cleanup() and leave_cleanup(), restore the pending 2053 * error/interrupt/exception state. 2054 */ 2055 else 2056 { 2057 /* 2058 * If there was an exception being thrown when enter_cleanup() was 2059 * called, we need to rethrow it. Make it the exception currently 2060 * being thrown. 2061 */ 2062 if (pending & CSTP_THROW) 2063 current_exception = csp->exception; 2064 2065 /* 2066 * If an error was about to be converted to an exception when 2067 * enter_cleanup() was called, let "cause_abort" take the part of 2068 * "force_abort" (as done by cause_errthrow()). 2069 */ 2070 else if (pending & CSTP_ERROR) 2071 { 2072 cause_abort = force_abort; 2073 force_abort = FALSE; 2074 } 2075 2076 /* 2077 * Restore the pending values of did_emsg, got_int, and did_throw. 2078 */ 2079 if (pending & CSTP_ERROR) 2080 did_emsg = TRUE; 2081 if (pending & CSTP_INTERRUPT) 2082 got_int = TRUE; 2083 if (pending & CSTP_THROW) 2084 need_rethrow = TRUE; /* did_throw will be set by do_one_cmd() */ 2085 2086 /* Report if required by the 'verbose' option or when debugging. */ 2087 report_resume_pending(pending, 2088 (pending & CSTP_THROW) ? (void *)current_exception : NULL); 2089 } 2090 } 2091 2092 2093 /* 2094 * Make conditionals inactive and discard what's pending in finally clauses 2095 * until the conditional type searched for or a try conditional not in its 2096 * finally clause is reached. If this is in an active catch clause, finish 2097 * the caught exception. 2098 * Return the cstack index where the search stopped. 2099 * Values used for "searched_cond" are (CSF_WHILE | CSF_FOR) or CSF_TRY or 0, 2100 * the latter meaning the innermost try conditional not in its finally clause. 2101 * "inclusive" tells whether the conditional searched for should be made 2102 * inactive itself (a try conditional not in its finally clause possibly find 2103 * before is always made inactive). If "inclusive" is TRUE and 2104 * "searched_cond" is CSF_TRY|CSF_SILENT, the saved former value of 2105 * "emsg_silent", if reset when the try conditional finally reached was 2106 * entered, is restored (used by ex_endtry()). This is normally done only 2107 * when such a try conditional is left. 2108 */ 2109 int 2110 cleanup_conditionals( 2111 struct condstack *cstack, 2112 int searched_cond, 2113 int inclusive) 2114 { 2115 int idx; 2116 int stop = FALSE; 2117 2118 for (idx = cstack->cs_idx; idx >= 0; --idx) 2119 { 2120 if (cstack->cs_flags[idx] & CSF_TRY) 2121 { 2122 /* 2123 * Discard anything pending in a finally clause and continue the 2124 * search. There may also be a pending ":continue", ":break", 2125 * ":return", or ":finish" before the finally clause. We must not 2126 * discard it, unless an error or interrupt occurred afterwards. 2127 */ 2128 if (did_emsg || got_int || (cstack->cs_flags[idx] & CSF_FINALLY)) 2129 { 2130 switch (cstack->cs_pending[idx]) 2131 { 2132 case CSTP_NONE: 2133 break; 2134 2135 case CSTP_CONTINUE: 2136 case CSTP_BREAK: 2137 case CSTP_FINISH: 2138 report_discard_pending(cstack->cs_pending[idx], NULL); 2139 cstack->cs_pending[idx] = CSTP_NONE; 2140 break; 2141 2142 case CSTP_RETURN: 2143 report_discard_pending(CSTP_RETURN, 2144 cstack->cs_rettv[idx]); 2145 discard_pending_return(cstack->cs_rettv[idx]); 2146 cstack->cs_pending[idx] = CSTP_NONE; 2147 break; 2148 2149 default: 2150 if (cstack->cs_flags[idx] & CSF_FINALLY) 2151 { 2152 if (cstack->cs_pending[idx] & CSTP_THROW) 2153 { 2154 /* Cancel the pending exception. This is in the 2155 * finally clause, so that the stack of the 2156 * caught exceptions is not involved. */ 2157 discard_exception((except_T *) 2158 cstack->cs_exception[idx], 2159 FALSE); 2160 } 2161 else 2162 report_discard_pending(cstack->cs_pending[idx], 2163 NULL); 2164 cstack->cs_pending[idx] = CSTP_NONE; 2165 } 2166 break; 2167 } 2168 } 2169 2170 /* 2171 * Stop at a try conditional not in its finally clause. If this try 2172 * conditional is in an active catch clause, finish the caught 2173 * exception. 2174 */ 2175 if (!(cstack->cs_flags[idx] & CSF_FINALLY)) 2176 { 2177 if ((cstack->cs_flags[idx] & CSF_ACTIVE) 2178 && (cstack->cs_flags[idx] & CSF_CAUGHT)) 2179 finish_exception((except_T *)cstack->cs_exception[idx]); 2180 /* Stop at this try conditional - except the try block never 2181 * got active (because of an inactive surrounding conditional 2182 * or when the ":try" appeared after an error or interrupt or 2183 * throw). */ 2184 if (cstack->cs_flags[idx] & CSF_TRUE) 2185 { 2186 if (searched_cond == 0 && !inclusive) 2187 break; 2188 stop = TRUE; 2189 } 2190 } 2191 } 2192 2193 /* Stop on the searched conditional type (even when the surrounding 2194 * conditional is not active or something has been made pending). 2195 * If "inclusive" is TRUE and "searched_cond" is CSF_TRY|CSF_SILENT, 2196 * check first whether "emsg_silent" needs to be restored. */ 2197 if (cstack->cs_flags[idx] & searched_cond) 2198 { 2199 if (!inclusive) 2200 break; 2201 stop = TRUE; 2202 } 2203 cstack->cs_flags[idx] &= ~CSF_ACTIVE; 2204 if (stop && searched_cond != (CSF_TRY | CSF_SILENT)) 2205 break; 2206 2207 /* 2208 * When leaving a try conditional that reset "emsg_silent" on its 2209 * entry after saving the original value, restore that value here and 2210 * free the memory used to store it. 2211 */ 2212 if ((cstack->cs_flags[idx] & CSF_TRY) 2213 && (cstack->cs_flags[idx] & CSF_SILENT)) 2214 { 2215 eslist_T *elem; 2216 2217 elem = cstack->cs_emsg_silent_list; 2218 cstack->cs_emsg_silent_list = elem->next; 2219 emsg_silent = elem->saved_emsg_silent; 2220 vim_free(elem); 2221 cstack->cs_flags[idx] &= ~CSF_SILENT; 2222 } 2223 if (stop) 2224 break; 2225 } 2226 return idx; 2227 } 2228 2229 /* 2230 * Return an appropriate error message for a missing endwhile/endfor/endif. 2231 */ 2232 static char_u * 2233 get_end_emsg(struct condstack *cstack) 2234 { 2235 if (cstack->cs_flags[cstack->cs_idx] & CSF_WHILE) 2236 return e_endwhile; 2237 if (cstack->cs_flags[cstack->cs_idx] & CSF_FOR) 2238 return e_endfor; 2239 return e_endif; 2240 } 2241 2242 2243 /* 2244 * Rewind conditionals until index "idx" is reached. "cond_type" and 2245 * "cond_level" specify a conditional type and the address of a level variable 2246 * which is to be decremented with each skipped conditional of the specified 2247 * type. 2248 * Also free "for info" structures where needed. 2249 */ 2250 void 2251 rewind_conditionals( 2252 struct condstack *cstack, 2253 int idx, 2254 int cond_type, 2255 int *cond_level) 2256 { 2257 while (cstack->cs_idx > idx) 2258 { 2259 if (cstack->cs_flags[cstack->cs_idx] & cond_type) 2260 --*cond_level; 2261 if (cstack->cs_flags[cstack->cs_idx] & CSF_FOR) 2262 free_for_info(cstack->cs_forinfo[cstack->cs_idx]); 2263 --cstack->cs_idx; 2264 } 2265 } 2266 2267 /* 2268 * ":endfunction" when not after a ":function" 2269 */ 2270 void 2271 ex_endfunction(exarg_T *eap UNUSED) 2272 { 2273 EMSG(_("E193: :endfunction not inside a function")); 2274 } 2275 2276 /* 2277 * Return TRUE if the string "p" looks like a ":while" or ":for" command. 2278 */ 2279 int 2280 has_loop_cmd(char_u *p) 2281 { 2282 int len; 2283 2284 /* skip modifiers, white space and ':' */ 2285 for (;;) 2286 { 2287 while (*p == ' ' || *p == '\t' || *p == ':') 2288 ++p; 2289 len = modifier_len(p); 2290 if (len == 0) 2291 break; 2292 p += len; 2293 } 2294 if ((p[0] == 'w' && p[1] == 'h') 2295 || (p[0] == 'f' && p[1] == 'o' && p[2] == 'r')) 2296 return TRUE; 2297 return FALSE; 2298 } 2299 2300 #endif /* FEAT_EVAL */ 2301