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