1 /* $NetBSD: conv.c,v 1.16 2003/08/05 14:55:03 erh Exp $ */
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Keith Muller of the University of California, San Diego and Lance
9 * Visser of Convex Computer Corporation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
39 static char sccsid
[] = "@(#)conv.c 8.3 (Berkeley) 4/2/94";
41 __RCSID("$NetBSD: conv.c,v 1.16 2003/08/05 14:55:03 erh Exp $");
45 #include <sys/param.h>
57 * Copy input to output. Input is buffered until reaches obs, and then
58 * output until less than obs remains. Only a single buffer is used.
59 * Worst case buffer calculation is (ibs + obs - 1).
68 if ((t
= ctab
) != NULL
)
69 for (inp
= in
.dbp
- (cnt
= in
.dbrcnt
); cnt
--; ++inp
)
72 /* Make the output buffer look right. */
76 if (in
.dbcnt
>= out
.dbsz
) {
77 /* If the output buffer is full, write it. */
81 * Ddout copies the leftover output to the beginning of
82 * the buffer and resets the output buffer. Reset the
83 * input buffer to match it.
94 /* Just update the count, everything is already in the buffer. */
100 /* Build a smaller version (i.e. for a miniroot) */
101 /* These can not be called, but just in case... */
102 static const char no_block
[] = "unblock and -DNO_CONV?";
103 void block(void) { errx(EXIT_FAILURE
, "%s", no_block
+ 2); }
104 void block_close(void) { errx(EXIT_FAILURE
, "%s", no_block
+ 2); }
105 void unblock(void) { errx(EXIT_FAILURE
, "%s", no_block
); }
106 void unblock_close(void) { errx(EXIT_FAILURE
, "%s", no_block
); }
110 * Copy variable length newline terminated records with a max size cbsz
111 * bytes to output. Records less than cbs are padded with spaces.
113 * max in buffer: MAX(ibs, cbsz)
114 * max out buffer: obs + cbsz
120 int ch
= 0; /* pacify gcc */
121 uint64_t cnt
, maxlen
;
126 * Record truncation can cross block boundaries. If currently in a
127 * truncation state, keep tossing characters until reach a newline.
128 * Start at the beginning of the buffer, as the input buffer is always
132 for (inp
= in
.db
, cnt
= in
.dbrcnt
;
133 cnt
&& *inp
++ != '\n'; --cnt
);
140 /* Adjust the input buffer numbers. */
142 in
.dbp
= inp
+ cnt
- 1;
146 * Copy records (max cbsz size chunks) into the output buffer. The
147 * translation is done as we copy into the output buffer.
149 for (inp
= in
.dbp
- in
.dbcnt
, outp
= out
.dbp
; in
.dbcnt
;) {
150 maxlen
= MIN(cbsz
, in
.dbcnt
);
151 if ((t
= ctab
) != NULL
)
153 cnt
< maxlen
&& (ch
= *inp
++) != '\n'; ++cnt
)
157 cnt
< maxlen
&& (ch
= *inp
++) != '\n'; ++cnt
)
160 * Check for short record without a newline. Reassemble the
163 if (ch
!= '\n' && in
.dbcnt
< cbsz
) {
164 (void)memmove(in
.db
, in
.dbp
- in
.dbcnt
, in
.dbcnt
);
168 /* Adjust the input buffer numbers. */
173 /* Pad short records with spaces. */
175 (void)memset(outp
, ctab
? ctab
[' '] : ' ', cbsz
- cnt
);
178 * If the next character wouldn't have ended the
179 * block, it's a truncation.
181 if (!in
.dbcnt
|| *inp
!= '\n')
184 /* Toss characters to a newline. */
185 for (; in
.dbcnt
&& *inp
++ != '\n'; --in
.dbcnt
);
192 /* Adjust output buffer numbers. */
194 if ((out
.dbcnt
+= cbsz
) >= out
.dbsz
)
198 in
.dbp
= in
.db
+ in
.dbcnt
;
206 * Copy any remaining data into the output buffer and pad to a record.
207 * Don't worry about truncation or translation, the input buffer is
208 * always empty when truncating, and no characters have been added for
209 * translation. The bottom line is that anything left in the input
210 * buffer is a truncated record. Anything left in the output buffer
211 * just wasn't big enough.
215 (void)memmove(out
.dbp
, in
.dbp
- in
.dbcnt
, in
.dbcnt
);
216 (void)memset(out
.dbp
+ in
.dbcnt
,
217 ctab
? ctab
[' '] : ' ', cbsz
- in
.dbcnt
);
223 * Convert fixed length (cbsz) records to variable length. Deletes any
224 * trailing blanks and appends a newline.
226 * max in buffer: MAX(ibs, cbsz) + cbsz
227 * max out buffer: obs + cbsz
236 /* Translation and case conversion. */
237 if ((t
= ctab
) != NULL
)
238 for (cnt
= in
.dbrcnt
, inp
= in
.dbp
- 1; cnt
--; inp
--)
241 * Copy records (max cbsz size chunks) into the output buffer. The
242 * translation has to already be done or we might not recognize the
245 for (inp
= in
.db
; in
.dbcnt
>= cbsz
; inp
+= cbsz
, in
.dbcnt
-= cbsz
) {
246 for (t
= inp
+ cbsz
- 1; t
>= inp
&& *t
== ' '; --t
);
249 (void)memmove(out
.dbp
, inp
, cnt
);
255 if (out
.dbcnt
>= out
.dbsz
)
259 (void)memmove(in
.db
, in
.dbp
- in
.dbcnt
, in
.dbcnt
);
260 in
.dbp
= in
.db
+ in
.dbcnt
;
270 warnx("%s: short input record", in
.name
);
271 for (t
= in
.db
+ in
.dbcnt
- 1; t
>= in
.db
&& *t
== ' '; --t
);
274 (void)memmove(out
.dbp
, in
.db
, cnt
);