Lines Matching +full:ps +full:- +full:hold
1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
59 static SPACE HS, PS, SS, YS; variable
60 #define pd PS.deleted
61 #define ps PS.space macro
62 #define psl PS.len
63 #define psanl PS.append_newline
88 fwrite(ps, 1, psl, outfile); \
104 for (linenum = 0; mf_fgets(&PS, REPLACE);) { in process()
111 cp = cp->next; in process()
114 switch (cp->code) { in process()
116 cp = cp->u.c; in process()
125 appends[appendx].s = cp->t; in process()
126 appends[appendx].len = strlen(cp->t); in process()
130 cp = cp->u.c; in process()
135 if (cp->a2 == NULL || lastaddr || lastline()) in process()
136 (void)fprintf(outfile, "%s", cp->t); in process()
145 (p = memchr(ps, '\n', psl)) == NULL) { in process()
149 psl -= (p + 1) - ps; in process()
150 memmove(ps, p + 1, psl); in process()
154 cspace(&PS, hs, hsl, REPLACE); in process()
157 cspace(&PS, "\n", 1, APPEND); in process()
158 cspace(&PS, hs, hsl, APPEND); in process()
161 cspace(&HS, ps, psl, REPLACE); in process()
165 cspace(&HS, ps, psl, APPEND); in process()
168 (void)fprintf(outfile, "%s", cp->t); in process()
171 lputs(ps, psl); in process()
177 if (!mf_fgets(&PS, REPLACE)) in process()
183 cspace(&PS, "\n", 1, APPEND); in process()
184 if (!mf_fgets(&PS, APPEND)) in process()
195 if ((p = memchr(ps, '\n', psl)) != NULL) { in process()
198 psl = p - ps; in process()
223 appends[appendx].s = cp->t; in process()
224 appends[appendx].len = strlen(cp->t); in process()
233 cp = cp->u.c; in process()
240 if (cp->u.fd == -1 && (cp->u.fd = open(cp->t, in process()
242 DEFFILEMODE)) == -1) in process()
243 err(1, "%s", cp->t); in process()
244 if (write(cp->u.fd, ps, psl) != (ssize_t)psl || in process()
245 write(cp->u.fd, "\n", 1) != 1) in process()
246 err(1, "%s", cp->t); in process()
250 * If the hold space is null, make it empty in process()
257 tspace = PS; in process()
258 PS = HS; in process()
265 do_tr(cp->u.y); in process()
273 cp = cp->next; in process()
284 * (lastline, linenumber, ps).
287 ((a)->type == AT_RE ? regexec_e((a)->u.r, ps, 0, 1, 0, psl) : \
288 (a)->type == AT_LINE ? linenum == (a)->u.l : lastline())
292 * line for process ranges. Interprets the non-select (``!'') flag.
300 if (cp->a1 == NULL && cp->a2 == NULL) in applies()
302 else if (cp->a2) in applies()
303 if (cp->startline > 0) { in applies()
304 switch (cp->a2->type) { in applies()
306 if (linenum - cp->startline <= cp->a2->u.l) in applies()
309 cp->startline = 0; in applies()
314 if (MATCH(cp->a2)) { in applies()
315 cp->startline = 0; in applies()
318 } else if (cp->a2->type == AT_LINE && in applies()
319 linenum > cp->a2->u.l) { in applies()
325 cp->startline = 0; in applies()
330 } else if (cp->a1 && MATCH(cp->a1)) { in applies()
335 * -- POSIX 1003.2 in applies()
338 if ((cp->a2->type == AT_LINE && in applies()
339 linenum >= cp->a2->u.l) || in applies()
340 (cp->a2->type == AT_RELLINE && cp->a2->u.l == 0)) in applies()
343 cp->startline = linenum; in applies()
349 r = MATCH(cp->a1); in applies()
350 return (cp->nonsel ? ! r : r); in applies()
362 * Reset all in-range markers. in resetstate()
364 for (cp = prog; cp; cp = cp->code == '{' ? cp->u.c : cp->next) in resetstate()
365 if (cp->a2) in resetstate()
366 cp->startline = 0; in resetstate()
369 * Clear out the hold space. in resetstate()
375 * substitute --
390 s = ps; in substitute()
391 re = cp->u.s->re; in substitute()
393 if (defpreg != NULL && cp->u.s->maxbref > defpreg->re_nsub) { in substitute()
394 linenum = cp->u.s->linenum; in substitute()
396 linenum, fname, cp->u.s->maxbref); in substitute()
399 if (!regexec_e(re, ps, 0, 0, 0, psl)) in substitute()
404 n = cp->u.s->n; in substitute()
410 cspace(&SS, s, match[0].rm_so - le, APPEND); in substitute()
412 /* Skip zero-length matches right after other matches. */ in substitute()
413 if (lastempty || (match[0].rm_so - le) || in substitute()
417 regsub(&SS, ps, cp->u.s->new); in substitute()
419 n = -1; in substitute()
422 if (match[0].rm_eo - le) in substitute()
423 cspace(&SS, s, match[0].rm_eo - le, in substitute()
425 n--; in substitute()
430 s = ps + match[0].rm_eo; in substitute()
431 slen = psl - match[0].rm_eo; in substitute()
435 * After a zero-length match, advance one byte, in substitute()
441 slen--; in substitute()
444 slen = -1; in substitute()
450 regexec_e(re, ps, REG_NOTBOL, 0, le, psl)); in substitute()
464 tspace = PS; in substitute()
465 PS = SS; in substitute()
471 if (cp->u.s->p) in substitute()
475 if (cp->u.s->wfile && !pd) { in substitute()
476 if (cp->u.s->wfd == -1 && (cp->u.s->wfd = open(cp->u.s->wfile, in substitute()
477 O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, DEFFILEMODE)) == -1) in substitute()
478 err(1, "%s", cp->u.s->wfile); in substitute()
479 if (write(cp->u.s->wfd, ps, psl) != (ssize_t)psl || in substitute()
480 write(cp->u.s->wfd, "\n", 1) != 1) in substitute()
481 err(1, "%s", cp->u.s->wfile); in substitute()
487 * do_tr --
500 * Single-byte encoding: perform in-place translation in do_tr()
503 for (p = ps; p < &ps[psl]; p++) in do_tr()
504 *p = y->bytetab[(u_char)*p]; in do_tr()
507 * Multi-byte encoding: perform translation into the in do_tr()
513 for (p = ps, left = psl; left > 0; p += clen, left -= clen) { in do_tr()
514 if ((c = y->bytetab[(u_char)*p]) != '\0') { in do_tr()
519 for (i = 0; i < y->nmultis; i++) in do_tr()
520 if (left >= y->multis[i].fromlen && in do_tr()
521 memcmp(p, y->multis[i].from, in do_tr()
522 y->multis[i].fromlen) == 0) in do_tr()
524 if (i < y->nmultis) { in do_tr()
525 cspace(&YS, y->multis[i].to, in do_tr()
526 y->multis[i].tolen, APPEND); in do_tr()
527 clen = y->multis[i].fromlen; in do_tr()
534 tmp = PS; in do_tr()
535 PS = YS; in do_tr()
562 * it's not an error to read a non-existent file, in flush_appends()
587 static int termwidth = -1; in lputs()
594 if (termwidth == -1) { in lputs()
612 if (clen == (size_t)-1 || clen == (size_t)-2) { in lputs()
637 fprintf(outfile, "\\%c", "\\abfrtv"[p - escapes]); in lputs()
650 len -= clen; in lputs()
689 * regsub - perform substitutions after a regexp match
700 if (sp->len + (reqlen) + 1 >= sp->blen) { \ in regsub()
701 sp->blen += (reqlen) + 1024; \ in regsub()
702 if ((sp->space = sp->back = realloc(sp->back, sp->blen)) \ in regsub()
705 dst = sp->space + sp->len; \ in regsub()
708 dst = sp->space + sp->len; in regsub()
713 no = *src++ - '0'; in regsub()
715 no = -1; in regsub()
721 ++sp->len; in regsub()
722 } else if (match[no].rm_so != -1 && match[no].rm_eo != -1) { in regsub()
723 len = match[no].rm_eo - match[no].rm_so; in regsub()
727 sp->len += len; in regsub()
735 * cspace --
745 tlen = sp->len + len + 1; in cspace()
746 if (tlen > sp->blen) { in cspace()
747 sp->blen = tlen + 1024; in cspace()
748 if ((sp->space = sp->back = realloc(sp->back, sp->blen)) == in cspace()
754 sp->len = 0; in cspace()
756 memmove(sp->space + sp->len, p, len); in cspace()
758 sp->space[sp->len += len] = '\0'; in cspace()
768 for (; cp != end; cp = cp->next) in cfclose()
769 switch(cp->code) { in cfclose()
771 if (cp->u.s->wfd != -1 && close(cp->u.s->wfd)) in cfclose()
772 err(1, "%s", cp->u.s->wfile); in cfclose()
773 cp->u.s->wfd = -1; in cfclose()
776 if (cp->u.fd != -1 && close(cp->u.fd)) in cfclose()
777 err(1, "%s", cp->t); in cfclose()
778 cp->u.fd = -1; in cfclose()
781 cfclose(cp->u.c, cp->next); in cfclose()