Temporarly comment out message based bridge control; there are copyout deep
[dragonfly.git] / bin / dd / conv.c
blob64e8885c4e3f90dc9de483c29d43c3038f9893de
1 /*-
2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
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.
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. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
33 * @(#)conv.c 8.3 (Berkeley) 4/2/94
34 * $FreeBSD: src/bin/dd/conv.c,v 1.15 1999/09/13 21:47:10 green Exp $
35 * $DragonFly: src/bin/dd/conv.c,v 1.6 2008/01/28 16:08:02 matthias Exp $
38 #include <sys/param.h>
40 #include <err.h>
41 #include <string.h>
43 #include "dd.h"
44 #include "extern.h"
47 * def --
48 * Copy input to output. Input is buffered until reaches obs, and then
49 * output until less than obs remains. Only a single buffer is used.
50 * Worst case buffer calculation is (ibs + obs - 1).
52 void
53 def(void)
55 u_char *inp;
56 const u_char *t;
57 size_t cnt;
59 if ((t = ctab) != NULL)
60 for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
61 *inp = t[*inp];
63 /* Make the output buffer look right. */
64 out.dbp = in.dbp;
65 out.dbcnt = in.dbcnt;
67 if (in.dbcnt >= out.dbsz) {
68 /* If the output buffer is full, write it. */
69 dd_out(0);
72 * Ddout copies the leftover output to the beginning of
73 * the buffer and resets the output buffer. Reset the
74 * input buffer to match it.
76 in.dbp = out.dbp;
77 in.dbcnt = out.dbcnt;
81 void
82 def_close(void)
84 /* Just update the count, everything is already in the buffer. */
85 if (in.dbcnt)
86 out.dbcnt = in.dbcnt;
90 * Copy variable length newline terminated records with a max size cbsz
91 * bytes to output. Records less than cbs are padded with spaces.
93 * max in buffer: MAX(ibs, cbsz)
94 * max out buffer: obs + cbsz
96 void
97 block(void)
99 u_char *inp, *outp;
100 const u_char *t;
101 size_t cnt, maxlen;
102 static int intrunc;
103 int ch;
106 * Record truncation can cross block boundaries. If currently in a
107 * truncation state, keep tossing characters until reach a newline.
108 * Start at the beginning of the buffer, as the input buffer is always
109 * left empty.
111 if (intrunc) {
112 for (inp = in.db, cnt = in.dbrcnt; cnt && *inp++ != '\n'; --cnt)
114 if (!cnt) {
115 in.dbcnt = 0;
116 in.dbp = in.db;
117 return;
119 intrunc = 0;
120 /* Adjust the input buffer numbers. */
121 in.dbcnt = cnt - 1;
122 in.dbp = inp + cnt - 1;
126 * Copy records (max cbsz size chunks) into the output buffer. The
127 * translation is done as we copy into the output buffer.
129 ch = 0;
130 for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) {
131 maxlen = MIN(cbsz, in.dbcnt);
132 if ((t = ctab) != NULL)
133 for (cnt = 0; cnt < maxlen && (ch = *inp++) != '\n';
134 ++cnt)
135 *outp++ = t[ch];
136 else
137 for (cnt = 0; cnt < maxlen && (ch = *inp++) != '\n';
138 ++cnt)
139 *outp++ = ch;
141 * Check for short record without a newline. Reassemble the
142 * input block.
144 if (ch != '\n' && in.dbcnt < cbsz) {
145 memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
146 break;
149 /* Adjust the input buffer numbers. */
150 in.dbcnt -= cnt;
151 if (ch == '\n')
152 --in.dbcnt;
154 /* Pad short records with spaces. */
155 if (cnt < cbsz)
156 memset(outp, ctab ? ctab[' '] : ' ', cbsz - cnt);
157 else {
159 * If the next character wouldn't have ended the
160 * block, it's a truncation.
162 if (!in.dbcnt || *inp != '\n')
163 ++st.trunc;
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;
173 /* Adjust output buffer numbers. */
174 out.dbp += cbsz;
175 if ((out.dbcnt += cbsz) >= out.dbsz)
176 dd_out(0);
177 outp = out.dbp;
179 in.dbp = in.db + in.dbcnt;
182 void
183 block_close(void)
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.
193 if (in.dbcnt) {
194 ++st.trunc;
195 memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt);
196 memset(out.dbp + in.dbcnt, ctab ? ctab[' '] : ' ',
197 cbsz - in.dbcnt);
198 out.dbcnt += cbsz;
203 * Convert fixed length (cbsz) records to variable length. Deletes any
204 * trailing blanks and appends a newline.
206 * max in buffer: MAX(ibs, cbsz) + cbsz
207 * max out buffer: obs + cbsz
209 void
210 unblock(void)
212 u_char *inp;
213 const u_char *t;
214 size_t cnt;
216 /* Translation and case conversion. */
217 if ((t = ctab) != NULL)
218 for (cnt = in.dbrcnt, inp = in.dbp; cnt--;) {
219 inp--;
220 *inp = t[*inp];
223 * Copy records (max cbsz size chunks) into the output buffer. The
224 * translation has to already be done or we might not recognize the
225 * spaces.
227 for (inp = in.db; in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) {
228 for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t)
230 if (t >= inp) {
231 cnt = t - inp + 1;
232 memmove(out.dbp, inp, cnt);
233 out.dbp += cnt;
234 out.dbcnt += cnt;
236 *out.dbp++ = '\n';
237 if (++out.dbcnt >= out.dbsz)
238 dd_out(0);
240 if (in.dbcnt)
241 memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
242 in.dbp = in.db + in.dbcnt;
245 void
246 unblock_close(void)
248 u_char *t;
249 size_t cnt;
251 if (in.dbcnt) {
252 warnx("%s: short input record", in.name);
253 for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t)
255 if (t >= in.db) {
256 cnt = t - in.db + 1;
257 memmove(out.dbp, in.db, cnt);
258 out.dbp += cnt;
259 out.dbcnt += cnt;
261 ++out.dbcnt;
262 *out.dbp++ = '\n';