Lines Matching +full:ps +full:- +full:hold

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