]> diplodocus.org Git - nmh/blob - sbr/cpydgst.c
60dc78a8797f6090de04ec6044103d4a0b289602
[nmh] / sbr / cpydgst.c
1 /* cpydgst.c -- copy from one fd to another in encapsulating mode
2 * -- (do dashstuffing of input data).
3 *
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
7 */
8
9 #include "h/mh.h"
10 #include "error.h"
11
12 /*
13 * We want to perform the substitution
14 *
15 * \n(-.*)\n --> \n- \1\n
16 *
17 * This is equivalent to the sed substitution
18 *
19 * sed -e 's%^-%- -%' < ifile > ofile
20 *
21 * but the routine below is faster than the pipe, fork, and exec.
22 */
23
24 #define S1 0
25 #define S2 1
26
27 #define output(c) if (bp >= dp) {flush(); *bp++ = c;} else *bp++ = c
28 #define flush() if ((j = bp - outbuf) && write (out, outbuf, j) != j) \
29 adios (ofile, "error writing"); \
30 else \
31 bp = outbuf
32
33
34 void
35 cpydgst (int in, int out, char *ifile, char *ofile)
36 {
37 int i, j, state;
38 char *cp, *ep;
39 char *bp, *dp;
40 char buffer[BUFSIZ], outbuf[BUFSIZ];
41
42 dp = (bp = outbuf) + sizeof outbuf;
43 for (state = S1; (i = read (in, buffer, sizeof buffer)) > 0;)
44 for (ep = (cp = buffer) + i; cp < ep; cp++) {
45 if (*cp == '\0')
46 continue;
47 switch (state) {
48 case S1:
49 if (*cp == '-') {
50 output ('-');
51 output (' ');
52 }
53 state = S2;
54 /* FALLTHRU */
55 case S2:
56 output (*cp);
57 if (*cp == '\n')
58 state = S1;
59 break;
60 }
61 }
62
63 if (i == -1)
64 adios (ifile, "error reading");
65 flush();
66 }