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