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