Pull in some of the lpt_port_test fixes from lpt.c.
[unix-history] / bin / dd / conv.c
CommitLineData
cb955067
NW
1/*-
2 * Copyright (c) 1991 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Keith Muller of the University of California, San Diego and Lance
7 * Visser of Convex Computer Corporation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
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
39static char sccsid[] = "@(#)conv.c 5.6 (Berkeley) 4/28/93";
40#endif /* not lint */
41
42#include <sys/param.h>
43
44#include <string.h>
45
46#include "dd.h"
47#include "extern.h"
48
49/*
50 * def --
51 * Copy input to output. Input is buffered until reaches obs, and then
52 * output until less than obs remains. Only a single buffer is used.
53 * Worst case buffer calculation is (ibs + obs - 1).
54 */
55void
56def()
57{
58 register int cnt;
59 register u_char *inp, *t;
60
61 if (t = ctab)
62 for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
63 *inp = t[*inp];
64
65 /* Make the output buffer look right. */
66 out.dbp = in.dbp;
67 out.dbcnt = in.dbcnt;
68
69 if (in.dbcnt >= out.dbsz) {
70 /* If the output buffer is full, write it. */
71 dd_out(0);
72
73 /*
74 * Ddout copies the leftover output to the beginning of
75 * the buffer and resets the output buffer. Reset the
76 * input buffer to match it.
77 */
78 in.dbp = out.dbp;
79 in.dbcnt = out.dbcnt;
80 }
81}
82
83void
84def_close()
85{
86 /* Just update the count, everything is already in the buffer. */
87 if (in.dbcnt)
88 out.dbcnt = in.dbcnt;
89}
90
91/*
92 * Copy variable length newline terminated records with a max size cbsz
93 * bytes to output. Records less than cbs are padded with spaces.
94 *
95 * max in buffer: MAX(ibs, cbsz)
96 * max out buffer: obs + cbsz
97 */
98void
99block()
100{
101 static int intrunc;
102 register int ch, cnt;
103 register u_char *inp, *outp, *t;
104 int maxlen;
105
106 /*
107 * Record truncation can cross block boundaries. If currently in a
108 * truncation state, keep tossing characters until reach a newline.
109 * Start at the beginning of the buffer, as the input buffer is always
110 * left empty.
111 */
112 if (intrunc) {
113 for (inp = in.db, cnt = in.dbrcnt;
114 cnt && *inp++ != '\n'; --cnt);
115 if (!cnt) {
116 in.dbcnt = 0;
117 in.dbp = in.db;
118 return;
119 }
120 intrunc = 0;
121 /* Adjust the input buffer numbers. */
122 in.dbcnt = cnt - 1;
123 in.dbp = inp + cnt - 1;
124 }
125
126 /*
127 * Copy records (max cbsz size chunks) into the output buffer. The
128 * translation is done as we copy into the output buffer.
129 */
130 for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) {
131 maxlen = MIN(cbsz, in.dbcnt);
132 if (t = ctab)
133 for (cnt = 0;
134 cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
135 *outp++ = t[ch];
136 else
137 for (cnt = 0;
138 cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
139 *outp++ = ch;
140 /*
141 * Check for short record without a newline. Reassemble the
142 * input block.
143 */
144 if (ch != '\n' && in.dbcnt < cbsz) {
145 memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
146 break;
147 }
148
149 /* Adjust the input buffer numbers. */
150 in.dbcnt -= cnt;
151 if (ch == '\n')
152 --in.dbcnt;
153
154 /* Pad short records with spaces. */
155 if (cnt < cbsz)
156 (void)memset(outp, ctab ? ctab[' '] : ' ', cbsz - cnt);
157 else {
158 /*
159 * If the next character wouldn't have ended the
160 * block, it's a truncation.
161 */
162 if (!in.dbcnt || *inp != '\n')
163 ++st.trunc;
164
165 /* Toss characters to a newline. */
166 for (; in.dbcnt && *inp++ != '\n'; --in.dbcnt);
167 if (!in.dbcnt)
168 intrunc = 1;
169 else
170 --in.dbcnt;
171 }
172
173 /* Adjust output buffer numbers. */
174 out.dbp += cbsz;
175 if ((out.dbcnt += cbsz) >= out.dbsz)
176 dd_out(0);
177 outp = out.dbp;
178 }
179 in.dbp = in.db + in.dbcnt;
180}
181
182void
183block_close()
184{
185 /*
186 * Copy any remaining data into the output buffer and pad to a record.
187 * Don't worry about truncation or translation, the input buffer is
188 * always empty when truncating, and no characters have been added for
189 * translation. The bottom line is that anything left in the input
190 * buffer is a truncated record. Anything left in the output buffer
191 * just wasn't big enough.
192 */
193 if (in.dbcnt) {
194 ++st.trunc;
195 memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt);
196 (void)memset(out.dbp + in.dbcnt,
197 ctab ? ctab[' '] : ' ', cbsz - in.dbcnt);
198 out.dbcnt += cbsz;
199 }
200}
201
202/*
203 * Convert fixed length (cbsz) records to variable length. Deletes any
204 * trailing blanks and appends a newline.
205 *
206 * max in buffer: MAX(ibs, cbsz) + cbsz
207 * max out buffer: obs + cbsz
208 */
209void
210unblock()
211{
212 register int cnt;
213 register u_char *inp, *t;
214
215 /* Translation and case conversion. */
216 if (t = ctab)
217 for (cnt = in.dbrcnt, inp = in.dbp; cnt--;)
218 *--inp = t[*inp];
219 /*
220 * Copy records (max cbsz size chunks) into the output buffer. The
221 * translation has to already be done or we might not recognize the
222 * spaces.
223 */
224 for (inp = in.db; in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) {
225 for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t);
226 if (t >= inp) {
227 cnt = t - inp + 1;
228 memmove(out.dbp, inp, cnt);
229 out.dbp += cnt;
230 out.dbcnt += cnt;
231 }
232 ++out.dbcnt;
233 *out.dbp++ = '\n';
234 if (out.dbcnt >= out.dbsz)
235 dd_out(0);
236 }
237 if (in.dbcnt)
238 memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
239 in.dbp = in.db + in.dbcnt;
240}
241
242void
243unblock_close()
244{
245 register int cnt;
246 register u_char *t;
247
248 if (in.dbcnt) {
249 warn("%s: short input record", in.name);
250 for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t);
251 if (t >= in.db) {
252 cnt = t - in.db + 1;
253 memmove(out.dbp, in.db, cnt);
254 out.dbp += cnt;
255 out.dbcnt += cnt;
256 }
257 ++out.dbcnt;
258 *out.dbp++ = '\n';
259 }
260}