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