Deleted Added
full compact
process.c (17522) process.c (28066)
1/*-
2 * Copyright (c) 1992 Diomidis Spinellis.
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Diomidis Spinellis of Imperial College, University of London.
8 *

--- 22 unchanged lines hidden (view full) ---

31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38#ifndef lint
1/*-
2 * Copyright (c) 1992 Diomidis Spinellis.
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Diomidis Spinellis of Imperial College, University of London.
8 *

--- 22 unchanged lines hidden (view full) ---

31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38#ifndef lint
39#if 0
39static char sccsid[] = "@(#)process.c 8.6 (Berkeley) 4/20/94";
40static char sccsid[] = "@(#)process.c 8.6 (Berkeley) 4/20/94";
41#endif
42static const char rcsid[] =
43 "$Id$";
40#endif /* not lint */
41
42#include <sys/types.h>
43#include <sys/stat.h>
44#include <sys/ioctl.h>
45#include <sys/uio.h>
46
47#include <ctype.h>
44#endif /* not lint */
45
46#include <sys/types.h>
47#include <sys/stat.h>
48#include <sys/ioctl.h>
49#include <sys/uio.h>
50
51#include <ctype.h>
52#include <err.h>
48#include <errno.h>
49#include <fcntl.h>
50#include <limits.h>
51#include <regex.h>
52#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
55#include <unistd.h>

--- 159 unchanged lines hidden (view full) ---

215 }
216 break;
217 case 'w':
218 if (pd)
219 break;
220 if (cp->u.fd == -1 && (cp->u.fd = open(cp->t,
221 O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
222 DEFFILEMODE)) == -1)
53#include <errno.h>
54#include <fcntl.h>
55#include <limits.h>
56#include <regex.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <unistd.h>

--- 159 unchanged lines hidden (view full) ---

220 }
221 break;
222 case 'w':
223 if (pd)
224 break;
225 if (cp->u.fd == -1 && (cp->u.fd = open(cp->t,
226 O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
227 DEFFILEMODE)) == -1)
223 err(FATAL, "%s: %s\n",
224 cp->t, strerror(errno));
228 err(1, "%s", cp->t);
225 if (write(cp->u.fd, ps, psl) != psl)
229 if (write(cp->u.fd, ps, psl) != psl)
226 err(FATAL, "%s: %s\n",
227 cp->t, strerror(errno));
230 err(1, "%s", cp->t);
228 break;
229 case 'x':
230 if (hs == NULL)
231 cspace(&HS, "\n", 1, REPLACE);
232 tspace = PS;
233 PS = HS;
234 HS = tspace;
235 break;

--- 82 unchanged lines hidden (view full) ---

318 int lastempty, n;
319 char *s;
320
321 s = ps;
322 re = cp->u.s->re;
323 if (re == NULL) {
324 if (defpreg != NULL && cp->u.s->maxbref > defpreg->re_nsub) {
325 linenum = cp->u.s->linenum;
231 break;
232 case 'x':
233 if (hs == NULL)
234 cspace(&HS, "\n", 1, REPLACE);
235 tspace = PS;
236 PS = HS;
237 HS = tspace;
238 break;

--- 82 unchanged lines hidden (view full) ---

321 int lastempty, n;
322 char *s;
323
324 s = ps;
325 re = cp->u.s->re;
326 if (re == NULL) {
327 if (defpreg != NULL && cp->u.s->maxbref > defpreg->re_nsub) {
328 linenum = cp->u.s->linenum;
326 err(COMPILE, "\\%d not defined in the RE",
327 cp->u.s->maxbref);
329 errx(1, "%lu: %s: \\%d not defined in the RE",
330 linenum, fname, cp->u.s->maxbref);
328 }
329 }
330 if (!regexec_e(re, s, 0, 0, psl))
331 return (0);
332
333 SS.len = 0; /* Clean substitute space. */
334 slen = psl;
335 n = cp->u.s->n;

--- 66 unchanged lines hidden (view full) ---

402 /* Handle the 'p' flag. */
403 if (cp->u.s->p)
404 OUT(ps)
405
406 /* Handle the 'w' flag. */
407 if (cp->u.s->wfile && !pd) {
408 if (cp->u.s->wfd == -1 && (cp->u.s->wfd = open(cp->u.s->wfile,
409 O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, DEFFILEMODE)) == -1)
331 }
332 }
333 if (!regexec_e(re, s, 0, 0, psl))
334 return (0);
335
336 SS.len = 0; /* Clean substitute space. */
337 slen = psl;
338 n = cp->u.s->n;

--- 66 unchanged lines hidden (view full) ---

405 /* Handle the 'p' flag. */
406 if (cp->u.s->p)
407 OUT(ps)
408
409 /* Handle the 'w' flag. */
410 if (cp->u.s->wfile && !pd) {
411 if (cp->u.s->wfd == -1 && (cp->u.s->wfd = open(cp->u.s->wfile,
412 O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, DEFFILEMODE)) == -1)
410 err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno));
413 err(1, "%s", cp->u.s->wfile);
411 if (write(cp->u.s->wfd, ps, psl) != psl)
414 if (write(cp->u.s->wfd, ps, psl) != psl)
412 err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno));
415 err(1, "%s", cp->u.s->wfile);
413 }
414 return (1);
415}
416
417/*
418 * Flush append requests. Always called before reading a line,
419 * therefore it also resets the substitution done (sdone) flag.
420 */

--- 16 unchanged lines hidden (view full) ---

437 * it's not an error to read a non-existent file,
438 * it's possible that another program is interacting
439 * with the sed script through the file system. It
440 * would be truly bizarre, but possible. It's probably
441 * not that big a performance win, anyhow.
442 */
443 if ((f = fopen(appends[i].s, "r")) == NULL)
444 break;
416 }
417 return (1);
418}
419
420/*
421 * Flush append requests. Always called before reading a line,
422 * therefore it also resets the substitution done (sdone) flag.
423 */

--- 16 unchanged lines hidden (view full) ---

440 * it's not an error to read a non-existent file,
441 * it's possible that another program is interacting
442 * with the sed script through the file system. It
443 * would be truly bizarre, but possible. It's probably
444 * not that big a performance win, anyhow.
445 */
446 if ((f = fopen(appends[i].s, "r")) == NULL)
447 break;
445 while (count = fread(buf, sizeof(char), sizeof(buf), f))
448 while ((count = fread(buf, sizeof(char), sizeof(buf), f)))
446 (void)fwrite(buf, sizeof(char), count, stdout);
447 (void)fclose(f);
448 break;
449 }
450 if (ferror(stdout))
449 (void)fwrite(buf, sizeof(char), count, stdout);
450 (void)fclose(f);
451 break;
452 }
453 if (ferror(stdout))
451 err(FATAL, "stdout: %s", strerror(errno ? errno : EIO));
454 errx(1, "stdout: %s", strerror(errno ? errno : EIO));
452 appendx = sdone = 0;
453}
454
455static void
456lputs(s)
457 register char *s;
458{
459 register int count;
460 register char *escapes, *p;
461 struct winsize win;
462 static int termwidth = -1;
463
464 if (termwidth == -1)
455 appendx = sdone = 0;
456}
457
458static void
459lputs(s)
460 register char *s;
461{
462 register int count;
463 register char *escapes, *p;
464 struct winsize win;
465 static int termwidth = -1;
466
467 if (termwidth == -1)
465 if (p = getenv("COLUMNS"))
468 if ((p = getenv("COLUMNS")))
466 termwidth = atoi(p);
467 else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 &&
468 win.ws_col > 0)
469 termwidth = win.ws_col;
470 else
471 termwidth = 60;
472
473 for (count = 0; *s; ++s) {
474 if (count >= termwidth) {
475 (void)printf("\\\n");
476 count = 0;
477 }
478 if (isprint((unsigned char)*s) && *s != '\\') {
479 (void)putchar(*s);
480 count++;
481 } else {
482 escapes = "\\\a\b\f\n\r\t\v";
483 (void)putchar('\\');
469 termwidth = atoi(p);
470 else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 &&
471 win.ws_col > 0)
472 termwidth = win.ws_col;
473 else
474 termwidth = 60;
475
476 for (count = 0; *s; ++s) {
477 if (count >= termwidth) {
478 (void)printf("\\\n");
479 count = 0;
480 }
481 if (isprint((unsigned char)*s) && *s != '\\') {
482 (void)putchar(*s);
483 count++;
484 } else {
485 escapes = "\\\a\b\f\n\r\t\v";
486 (void)putchar('\\');
484 if (p = strchr(escapes, *s)) {
487 if ((p = strchr(escapes, *s))) {
485 (void)putchar("\\abfnrtv"[p - escapes]);
486 count += 2;
487 } else {
488 (void)printf("%03o", *(u_char *)s);
489 count += 4;
490 }
491 }
492 }
493 (void)putchar('$');
494 (void)putchar('\n');
495 if (ferror(stdout))
488 (void)putchar("\\abfnrtv"[p - escapes]);
489 count += 2;
490 } else {
491 (void)printf("%03o", *(u_char *)s);
492 count += 4;
493 }
494 }
495 }
496 (void)putchar('$');
497 (void)putchar('\n');
498 if (ferror(stdout))
496 err(FATAL, "stdout: %s", strerror(errno ? errno : EIO));
499 errx(1, "stdout: %s", strerror(errno ? errno : EIO));
497}
498
499static inline int
500regexec_e(preg, string, eflags, nomatch, slen)
501 regex_t *preg;
502 const char *string;
503 int eflags, nomatch;
504 size_t slen;
505{
506 int eval;
507
508 if (preg == NULL) {
509 if (defpreg == NULL)
500}
501
502static inline int
503regexec_e(preg, string, eflags, nomatch, slen)
504 regex_t *preg;
505 const char *string;
506 int eflags, nomatch;
507 size_t slen;
508{
509 int eval;
510
511 if (preg == NULL) {
512 if (defpreg == NULL)
510 err(FATAL, "first RE may not be empty");
513 errx(1, "first RE may not be empty");
511 } else
512 defpreg = preg;
513
514 /* Set anchors, discounting trailing newline (if any). */
515 if (slen > 0 && string[slen - 1] == '\n')
516 slen--;
517 match[0].rm_so = 0;
518 match[0].rm_eo = slen;
519
520 eval = regexec(defpreg, string,
521 nomatch ? 0 : maxnsub + 1, match, eflags | REG_STARTEND);
522 switch(eval) {
523 case 0:
524 return (1);
525 case REG_NOMATCH:
526 return (0);
527 }
514 } else
515 defpreg = preg;
516
517 /* Set anchors, discounting trailing newline (if any). */
518 if (slen > 0 && string[slen - 1] == '\n')
519 slen--;
520 match[0].rm_so = 0;
521 match[0].rm_eo = slen;
522
523 eval = regexec(defpreg, string,
524 nomatch ? 0 : maxnsub + 1, match, eflags | REG_STARTEND);
525 switch(eval) {
526 case 0:
527 return (1);
528 case REG_NOMATCH:
529 return (0);
530 }
528 err(FATAL, "RE error: %s", strregerror(eval, defpreg));
531 errx(1, "RE error: %s", strregerror(eval, defpreg));
529 /* NOTREACHED */
530}
531
532/*
533 * regsub - perform substitutions after a regexp match
534 * Based on a routine by Henry Spencer
535 */
536static void

--- 73 unchanged lines hidden (view full) ---

610cfclose(cp, end)
611 register struct s_command *cp, *end;
612{
613
614 for (; cp != end; cp = cp->next)
615 switch(cp->code) {
616 case 's':
617 if (cp->u.s->wfd != -1 && close(cp->u.s->wfd))
532 /* NOTREACHED */
533}
534
535/*
536 * regsub - perform substitutions after a regexp match
537 * Based on a routine by Henry Spencer
538 */
539static void

--- 73 unchanged lines hidden (view full) ---

613cfclose(cp, end)
614 register struct s_command *cp, *end;
615{
616
617 for (; cp != end; cp = cp->next)
618 switch(cp->code) {
619 case 's':
620 if (cp->u.s->wfd != -1 && close(cp->u.s->wfd))
618 err(FATAL,
619 "%s: %s", cp->u.s->wfile, strerror(errno));
621 err(1, "%s", cp->u.s->wfile);
620 cp->u.s->wfd = -1;
621 break;
622 case 'w':
623 if (cp->u.fd != -1 && close(cp->u.fd))
622 cp->u.s->wfd = -1;
623 break;
624 case 'w':
625 if (cp->u.fd != -1 && close(cp->u.fd))
624 err(FATAL, "%s: %s", cp->t, strerror(errno));
626 err(1, "%s", cp->t);
625 cp->u.fd = -1;
626 break;
627 case '{':
628 cfclose(cp->u.c, cp->next);
629 break;
630 }
631}
627 cp->u.fd = -1;
628 break;
629 case '{':
630 cfclose(cp->u.c, cp->next);
631 break;
632 }
633}