comparison src/regexp.c @ 189:504c627f7474

updated for version 7.0057
author vimboss
date Mon, 07 Mar 2005 23:16:51 +0000
parents 7e70fc748752
children 49748afd794b
comparison
equal deleted inserted replaced
188:041a413d626d 189:504c627f7474
3182 curbuf = save_curbuf; 3182 curbuf = save_curbuf;
3183 3183
3184 return r; 3184 return r;
3185 } 3185 }
3186 3186
3187 #if 0 /* disabled, no longer needed now that regmatch() is not recursive */
3188 # ifdef HAVE_SETJMP_H
3189 # define USE_SETJMP
3190 # endif
3191 # ifdef HAVE_TRY_EXCEPT
3192 # define USE_TRY_EXCEPT
3193 # endif
3194 #endif
3195
3196 /* 3187 /*
3197 * Match a regexp against a string ("line" points to the string) or multiple 3188 * Match a regexp against a string ("line" points to the string) or multiple
3198 * lines ("line" is NULL, use reg_getline()). 3189 * lines ("line" is NULL, use reg_getline()).
3199 */ 3190 */
3200 #ifdef USE_SETJMP
3201 static long
3202 vim_regexec_both(line_arg, col_arg)
3203 char_u *line_arg;
3204 colnr_T col_arg; /* column to start looking for match */
3205 #else
3206 static long 3191 static long
3207 vim_regexec_both(line, col) 3192 vim_regexec_both(line, col)
3208 char_u *line; 3193 char_u *line;
3209 colnr_T col; /* column to start looking for match */ 3194 colnr_T col; /* column to start looking for match */
3210 #endif
3211 { 3195 {
3212 regprog_T *prog; 3196 regprog_T *prog;
3213 char_u *s; 3197 char_u *s;
3214 long retval; 3198 long retval = 0L;
3215 #ifdef USE_SETJMP
3216 char_u *line;
3217 colnr_T col;
3218 int did_mch_startjmp = FALSE;
3219 #endif
3220 3199
3221 reg_tofree = NULL; 3200 reg_tofree = NULL;
3222
3223 #ifdef USE_SETJMP
3224 /* Trick to avoid "might be clobbered by `longjmp'" warning from gcc. */
3225 line = line_arg;
3226 col = col_arg;
3227 #endif
3228 retval = 0L;
3229 3201
3230 if (REG_MULTI) 3202 if (REG_MULTI)
3231 { 3203 {
3232 prog = reg_mmatch->regprog; 3204 prog = reg_mmatch->regprog;
3233 line = reg_getline((linenr_T)0); 3205 line = reg_getline((linenr_T)0);
3309 mb_ptr_adv(s); 3281 mb_ptr_adv(s);
3310 } 3282 }
3311 if (s == NULL) /* Not present. */ 3283 if (s == NULL) /* Not present. */
3312 goto theend; 3284 goto theend;
3313 } 3285 }
3314
3315 #ifdef USE_TRY_EXCEPT
3316 __try
3317 {
3318 #endif
3319
3320 #ifdef USE_SETJMP
3321 /*
3322 * Matching with a regexp may cause a very deep recursive call of
3323 * regmatch(). Vim will crash when running out of stack space. Catch
3324 * this here if the system supports it.
3325 * It's a bit slow, do it after the check for "regmust".
3326 * Don't do it if the caller already set it up.
3327 */
3328 if (!lc_active)
3329 {
3330 did_mch_startjmp = TRUE;
3331 mch_startjmp();
3332 if (SETJMP(lc_jump_env) != 0)
3333 {
3334 mch_didjmp();
3335 # ifdef SIGHASARG
3336 if (lc_signal != SIGINT)
3337 # endif
3338 EMSG(_(e_complex));
3339 retval = 0L;
3340 goto inner_end;
3341 }
3342 }
3343 #endif
3344 3286
3345 regline = line; 3287 regline = line;
3346 reglnum = 0; 3288 reglnum = 0;
3347 3289
3348 /* Simplest case: Anchored match need be tried only once. */ 3290 /* Simplest case: Anchored match need be tried only once. */
3412 #endif 3354 #endif
3413 ++col; 3355 ++col;
3414 } 3356 }
3415 } 3357 }
3416 3358
3417 #ifdef USE_SETJMP
3418 inner_end:
3419 if (did_mch_startjmp)
3420 mch_endjmp();
3421 #endif
3422 #ifdef USE_TRY_EXCEPT
3423 }
3424 __except(EXCEPTION_EXECUTE_HANDLER)
3425 {
3426 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
3427 {
3428 RESETSTKOFLW();
3429 EMSG(_(e_outofstack));
3430 }
3431 else
3432 EMSG(_(e_complex));
3433 retval = 0L;
3434 }
3435 #endif
3436
3437 theend: 3359 theend:
3438 /* Didn't find a match. */ 3360 /* Didn't find a match. */
3439 vim_free(reg_tofree); 3361 vim_free(reg_tofree);
3440 return retval; 3362 return retval;
3441 } 3363 }
3503 /* Clear the external match subpointers if necessary. */ 3425 /* Clear the external match subpointers if necessary. */
3504 if (prog->reghasz == REX_SET) 3426 if (prog->reghasz == REX_SET)
3505 need_clear_zsubexpr = TRUE; 3427 need_clear_zsubexpr = TRUE;
3506 #endif 3428 #endif
3507 3429
3508 if (regmatch(prog->program + 1)) 3430 if (regmatch(prog->program + 1) == 0)
3509 { 3431 return 0;
3510 cleanup_subexpr(); 3432
3511 if (REG_MULTI) 3433 cleanup_subexpr();
3434 if (REG_MULTI)
3435 {
3436 if (reg_startpos[0].lnum < 0)
3512 { 3437 {
3513 if (reg_startpos[0].lnum < 0) 3438 reg_startpos[0].lnum = 0;
3514 { 3439 reg_startpos[0].col = col;
3515 reg_startpos[0].lnum = 0; 3440 }
3516 reg_startpos[0].col = col; 3441 if (reg_endpos[0].lnum < 0)
3517 } 3442 {
3518 if (reg_endpos[0].lnum < 0) 3443 reg_endpos[0].lnum = reglnum;
3519 { 3444 reg_endpos[0].col = (int)(reginput - regline);
3520 reg_endpos[0].lnum = reglnum;
3521 reg_endpos[0].col = (int)(reginput - regline);
3522 }
3523 else
3524 /* Use line number of "\ze". */
3525 reglnum = reg_endpos[0].lnum;
3526 } 3445 }
3527 else 3446 else
3447 /* Use line number of "\ze". */
3448 reglnum = reg_endpos[0].lnum;
3449 }
3450 else
3451 {
3452 if (reg_startp[0] == NULL)
3453 reg_startp[0] = regline + col;
3454 if (reg_endp[0] == NULL)
3455 reg_endp[0] = reginput;
3456 }
3457 #ifdef FEAT_SYN_HL
3458 /* Package any found \z(...\) matches for export. Default is none. */
3459 unref_extmatch(re_extmatch_out);
3460 re_extmatch_out = NULL;
3461
3462 if (prog->reghasz == REX_SET)
3463 {
3464 int i;
3465
3466 cleanup_zsubexpr();
3467 re_extmatch_out = make_extmatch();
3468 for (i = 0; i < NSUBEXP; i++)
3528 { 3469 {
3529 if (reg_startp[0] == NULL) 3470 if (REG_MULTI)
3530 reg_startp[0] = regline + col; 3471 {
3531 if (reg_endp[0] == NULL) 3472 /* Only accept single line matches. */
3532 reg_endp[0] = reginput; 3473 if (reg_startzpos[i].lnum >= 0
3474 && reg_endzpos[i].lnum == reg_startzpos[i].lnum)
3475 re_extmatch_out->matches[i] =
3476 vim_strnsave(reg_getline(reg_startzpos[i].lnum)
3477 + reg_startzpos[i].col,
3478 reg_endzpos[i].col - reg_startzpos[i].col);
3479 }
3480 else
3481 {
3482 if (reg_startzp[i] != NULL && reg_endzp[i] != NULL)
3483 re_extmatch_out->matches[i] =
3484 vim_strnsave(reg_startzp[i],
3485 (int)(reg_endzp[i] - reg_startzp[i]));
3486 }
3533 } 3487 }
3534 #ifdef FEAT_SYN_HL
3535 /* Package any found \z(...\) matches for export. Default is none. */
3536 unref_extmatch(re_extmatch_out);
3537 re_extmatch_out = NULL;
3538
3539 if (prog->reghasz == REX_SET)
3540 {
3541 int i;
3542
3543 cleanup_zsubexpr();
3544 re_extmatch_out = make_extmatch();
3545 for (i = 0; i < NSUBEXP; i++)
3546 {
3547 if (REG_MULTI)
3548 {
3549 /* Only accept single line matches. */
3550 if (reg_startzpos[i].lnum >= 0
3551 && reg_endzpos[i].lnum == reg_startzpos[i].lnum)
3552 re_extmatch_out->matches[i] =
3553 vim_strnsave(reg_getline(reg_startzpos[i].lnum)
3554 + reg_startzpos[i].col,
3555 reg_endzpos[i].col - reg_startzpos[i].col);
3556 }
3557 else
3558 {
3559 if (reg_startzp[i] != NULL && reg_endzp[i] != NULL)
3560 re_extmatch_out->matches[i] =
3561 vim_strnsave(reg_startzp[i],
3562 (int)(reg_endzp[i] - reg_startzp[i]));
3563 }
3564 }
3565 }
3566 #endif
3567 return 1 + reglnum;
3568 } 3488 }
3569 return 0; 3489 #endif
3490 return 1 + reglnum;
3570 } 3491 }
3571 3492
3572 #ifdef FEAT_MBYTE 3493 #ifdef FEAT_MBYTE
3573 static int reg_prev_class __ARGS((void)); 3494 static int reg_prev_class __ARGS((void));
3574 3495
4599 ? rst.count >= rst.minval : rst.count >= rst.maxval) 4520 ? rst.count >= rst.minval : rst.count >= rst.maxval)
4600 { 4521 {
4601 /* It could match. Prepare for trying to match what 4522 /* It could match. Prepare for trying to match what
4602 * follows. The code is below. Parameters are stored in 4523 * follows. The code is below. Parameters are stored in
4603 * a regstar_T on the regstack. */ 4524 * a regstar_T on the regstack. */
4604 if (ga_grow(&regstack, sizeof(regstar_T)) == FAIL) 4525 if (((unsigned)regstack.ga_len >> 10) >= p_mmp)
4526 {
4527 EMSG(_(e_maxmempat));
4528 status = RA_FAIL;
4529 }
4530 else if (ga_grow(&regstack, sizeof(regstar_T)) == FAIL)
4605 status = RA_FAIL; 4531 status = RA_FAIL;
4606 else 4532 else
4607 { 4533 {
4608 regstack.ga_len += sizeof(regstar_T); 4534 regstack.ga_len += sizeof(regstar_T);
4609 rp = regstack_push(&regstack, rst.minval <= rst.maxval 4535 rp = regstack_push(&regstack, rst.minval <= rst.maxval
4639 break; 4565 break;
4640 4566
4641 case BEHIND: 4567 case BEHIND:
4642 case NOBEHIND: 4568 case NOBEHIND:
4643 /* Need a bit of room to store extra positions. */ 4569 /* Need a bit of room to store extra positions. */
4644 if (ga_grow(&regstack, sizeof(regbehind_T)) == FAIL) 4570 if (((unsigned)regstack.ga_len >> 10) >= p_mmp)
4571 {
4572 EMSG(_(e_maxmempat));
4573 status = RA_FAIL;
4574 }
4575 else if (ga_grow(&regstack, sizeof(regbehind_T)) == FAIL)
4645 status = RA_FAIL; 4576 status = RA_FAIL;
4646 else 4577 else
4647 { 4578 {
4648 regstack.ga_len += sizeof(regbehind_T); 4579 regstack.ga_len += sizeof(regbehind_T);
4649 rp = regstack_push(&regstack, RS_BEHIND1, scan, startp); 4580 rp = regstack_push(&regstack, RS_BEHIND1, scan, startp);
5027 if (status == RA_CONT || rp == (regitem_T *) 4958 if (status == RA_CONT || rp == (regitem_T *)
5028 ((char *)regstack.ga_data + regstack.ga_len) - 1) 4959 ((char *)regstack.ga_data + regstack.ga_len) - 1)
5029 break; 4960 break;
5030 } 4961 }
5031 4962
5032 /* May want to continue with the inner loop. */ 4963 /* May need to continue with the inner loop, starting at "scan". */
5033 if (status == RA_CONT) 4964 if (status == RA_CONT)
5034 continue; 4965 continue;
5035 4966
5036 /* 4967 /*
5037 * If the regstack is empty or something failed we are done. 4968 * If the regstack is empty or something failed we are done.
5048 EMSG(_(e_re_corr)); 4979 EMSG(_(e_re_corr));
5049 #ifdef DEBUG 4980 #ifdef DEBUG
5050 printf("Premature EOL\n"); 4981 printf("Premature EOL\n");
5051 #endif 4982 #endif
5052 } 4983 }
4984 if (status == RA_FAIL)
4985 got_int = TRUE;
5053 return (status == RA_MATCH); 4986 return (status == RA_MATCH);
5054 } 4987 }
5055 4988
5056 } /* End of loop until the regstack is empty. */ 4989 } /* End of loop until the regstack is empty. */
5057 4990
5069 char_u *scan; 5002 char_u *scan;
5070 long startp; 5003 long startp;
5071 { 5004 {
5072 regitem_T *rp; 5005 regitem_T *rp;
5073 5006
5007 if (((unsigned)regstack->ga_len >> 10) >= p_mmp)
5008 {
5009 EMSG(_(e_maxmempat));
5010 return NULL;
5011 }
5074 if (ga_grow(regstack, sizeof(regitem_T)) == FAIL) 5012 if (ga_grow(regstack, sizeof(regitem_T)) == FAIL)
5075 return NULL; 5013 return NULL;
5076 5014
5077 rp = (regitem_T *)((char *)regstack->ga_data + regstack->ga_len); 5015 rp = (regitem_T *)((char *)regstack->ga_data + regstack->ga_len);
5078 rp->rs_state = state; 5016 rp->rs_state = state;