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