2 do_match(TARG,arg,gimme,arglast)
8 register STR **st = stack->ary_array;
9 register SPAT *spat = arg[2].arg_ptr.arg_spat;
11 register int sp = arglast[0] + 1;
12 STR *srchstr = st[sp];
13 register char *s = str_get(st[sp]);
14 char *strend = s + st[sp]->str_cur;
20 register REGEXP *rx = spat->spat_regexp;
31 global = spat->spat_flags & SPAT_GLOBAL;
32 safebase = (gimme == G_ARRAY) || global;
34 fatal("panic: do_match");
35 if (spat->spat_flags & SPAT_USED) {
48 if (spat->spat_runtime) {
50 sp = eval(spat->spat_runtime,G_SCALAR,sp);
51 st = stack->ary_array;
52 t = str_get(tmpstr = st[sp--]);
56 deb("2.SPAT /%s/\n",t);
60 spat->spat_regexp = Null(REGEXP*); /* crucial if regcomp aborts */
61 spat->spat_regexp = regcomp(t,t+tmpstr->str_cur,
62 spat->spat_flags & SPAT_FOLD);
63 if (!spat->spat_regexp->prelen && lastspat)
65 if (spat->spat_flags & SPAT_KEEP) {
66 if (!(spat->spat_flags & SPAT_FOLD))
67 scanconst(spat,spat->spat_regexp->precomp,
68 spat->spat_regexp->prelen);
69 if (spat->spat_runtime)
70 arg_free(spat->spat_runtime); /* it won't change, so */
71 spat->spat_runtime = Nullarg; /* no point compiling again */
73 if (curcmd->c_expr && (curcmd->c_flags & CF_OPTIMIZE) == CFT_EVAL) {
74 curcmd->c_flags &= ~CF_OPTIMIZE;
75 opt_arg(curcmd, 1, curcmd->c_type == C_EXPR);
82 if (s == rx->startp[0])
86 rx = spat->spat_regexp;
93 else if (!spat->spat_regexp->nparens)
94 gimme = G_SCALAR; /* accidental array context? */
95 rx = spat->spat_regexp;
96 if (regexec(rx, s, strend, s, 0,
97 srchstr->str_pok & SP_STUDIED ? srchstr : Nullstr,
99 if (rx->subbase || global)
105 if (gimme == G_ARRAY)
107 str_sset(TARG,&str_no);
118 if (spat->spat_flags & SPAT_ONCE)
122 deb("2.SPAT %c%s%c\n",ch,rx->precomp,ch);
125 if (!rx->prelen && lastspat) {
127 rx = spat->spat_regexp;
131 if (global && rx->startp[0]) {
133 if (s == rx->startp[0])
139 if (myhint < s || myhint > strend)
140 fatal("panic: hint in do_match");
142 if (rx->regback >= 0) {
150 else if (spat->spat_short) {
151 if (spat->spat_flags & SPAT_SCANFIRST) {
152 if (srchstr->str_pok & SP_STUDIED) {
153 if (screamfirst[spat->spat_short->str_rare] < 0)
155 else if (!(s = screaminstr(srchstr,spat->spat_short)))
157 else if (spat->spat_flags & SPAT_ALL)
161 else if (!(s = fbminstr((unsigned char*)s,
162 (unsigned char*)strend, spat->spat_short)))
165 else if (spat->spat_flags & SPAT_ALL)
167 if (s && rx->regback >= 0) {
168 ++spat->spat_short->str_u.str_useful;
176 else if (!multiline && (*spat->spat_short->str_ptr != *s ||
177 bcmp(spat->spat_short->str_ptr, s, spat->spat_slen) ))
179 if (--spat->spat_short->str_u.str_useful < 0) {
180 str_free(spat->spat_short);
181 spat->spat_short = Nullstr; /* opt is being useless */
184 if (!rx->nparens && !global) {
185 gimme = G_SCALAR; /* accidental array context? */
188 if (regexec(rx, s, strend, truebase, 0,
189 srchstr->str_pok & SP_STUDIED ? srchstr : Nullstr,
191 if (rx->subbase || global)
194 if (spat->spat_flags & SPAT_ONCE)
195 spat->spat_flags |= SPAT_USED;
200 rx->startp[0] = Nullch;
201 if (gimme == G_ARRAY)
203 str_sset(TARG,&str_no);
212 if (gimme == G_ARRAY) {
216 if (global && !iters)
220 if (sp + iters + i >= stack->ary_max) {
221 astore(stack,sp + iters + i, Nullstr);
222 st = stack->ary_array; /* possibly realloced */
225 for (i = !i; i <= iters; i++) {
226 st[++sp] = str_mortal(&str_no);
228 if (s = rx->startp[i]) {
229 len = rx->endp[i] - s;
231 str_nset(st[sp],s,len);
235 truebase = rx->subbeg;
241 str_sset(TARG,&str_yes);
248 ++spat->spat_short->str_u.str_useful;
250 if (spat->spat_flags & SPAT_ONCE)
251 spat->spat_flags |= SPAT_USED;
256 rx->endp[0] = s + spat->spat_short->str_cur;
264 Safefree(rx->subbase);
265 tmps = rx->subbase = nsavestr(t,strend-t);
267 rx->subend = tmps + (strend-t);
268 tmps = rx->startp[0] = tmps + (s - t);
269 rx->endp[0] = tmps + spat->spat_short->str_cur;
272 str_sset(TARG,&str_yes);
278 rx->startp[0] = Nullch;
279 if (spat->spat_short)
280 ++spat->spat_short->str_u.str_useful;
281 if (gimme == G_ARRAY)
283 str_sset(TARG,&str_no);