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