1/*	$NetBSD: fold.c,v 1.16 2009/07/21 01:35:02 ahoka Exp $	*/
2
3/*-
4 * Copyright (c) 1990, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Kevin Ruddy.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#include <sys/cdefs.h>
36#ifndef lint
37__COPYRIGHT("@(#) Copyright (c) 1990, 1993\
38 The Regents of the University of California.  All rights reserved.");
39#endif /* not lint */
40
41#ifndef lint
42#if 0
43static char sccsid[] = "@(#)fold.c	8.1 (Berkeley) 6/6/93";
44#endif
45__RCSID("$NetBSD: fold.c,v 1.16 2009/07/21 01:35:02 ahoka Exp $");
46#endif /* not lint */
47
48#include <limits.h>
49#include <locale.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <unistd.h>
53#include <wchar.h>
54#include <err.h>
55
56#define	DEFLINEWIDTH	80
57
58static	void	fold(int);
59static	int	new_column_position(int, wint_t);
60__dead static	void	usage(void);
61
62static int count_bytes = 0;
63static int split_words = 0;
64
65int
66main(int argc, char **argv)
67{
68	int ch;
69	int width;
70	char *p;
71
72	setlocale(LC_CTYPE, "");
73	setprogname(argv[0]);
74
75	width = -1;
76	while ((ch = getopt(argc, argv, "0123456789bsw:")) != -1)
77		switch (ch) {
78		case 'b':
79			count_bytes = 1;
80			break;
81		case 's':
82			split_words = 1;
83			break;
84		case 'w':
85			if ((width = atoi(optarg)) <= 0)
86				errx(1, "illegal width value");
87			break;
88		case '0': case '1': case '2': case '3': case '4':
89		case '5': case '6': case '7': case '8': case '9':
90			if (width == -1) {
91				p = argv[optind - 1];
92				if (p[0] == '-' && p[1] == ch && !p[2])
93					width = atoi(++p);
94				else
95					width = atoi(argv[optind] + 1);
96			}
97			break;
98		default:
99			usage();
100		}
101	argv += optind;
102	argc -= optind;
103
104	if (width == -1)
105		width = DEFLINEWIDTH;
106
107	if (!*argv)
108		fold(width);
109	else for (; *argv; ++argv)
110		if (!freopen(*argv, "r", stdin)) {
111			err (1, "%s", *argv);
112			/* NOTREACHED */
113		} else
114			fold(width);
115	exit(0);
116}
117
118/*
119 * Fold the contents of standard input to fit within WIDTH columns
120 * (or bytes) and write to standard output.
121 *
122 * If split_words is set, split the line at the last space character
123 * on the line.  This flag necessitates storing the line in a buffer
124 * until the current column > width, or a newline or EOF is read.
125 *
126 * The buffer can grow larger than WIDTH due to backspaces and carriage
127 * returns embedded in the input stream.
128 */
129static void
130fold(int width)
131{
132	static wchar_t *buf = NULL;
133	wchar_t *nbuf;
134	static int   buf_max = 0;
135	wint_t ch;
136	int col, indx, i;
137
138	col = indx = 0;
139	while ((ch = getwchar()) != WEOF) {
140		if (ch == L'\n') {
141			if (indx != 0) {
142				for (i = 0; i < indx; i++)
143					putwchar(buf[i]);
144			}
145			putwchar(L'\n');
146			col = indx = 0;
147			continue;
148		}
149
150		col = new_column_position (col, ch);
151		if (col > width) {
152			int last_space;
153
154#ifdef __GNUC__
155			last_space = 0;	/* XXX gcc */
156#endif
157			if (split_words) {
158				for (i = 0, last_space = -1; i < indx; i++)
159					if (buf[i] == L' ')
160						last_space = i;
161			}
162
163			if (split_words && last_space != -1) {
164				for (i = 0; i < last_space; i++)
165					putwchar(buf[i]);
166
167				/* increase last_space here, so we skip trailing whitespace */
168				last_space++;
169				wmemmove (buf, buf+last_space, indx-last_space);
170
171				indx -= last_space;
172				col = 0;
173				for (i = 0; i < indx; i++) {
174					col = new_column_position (col, buf[i]);
175				}
176			} else {
177				for (i = 0; i < indx; i++)
178					putwchar(buf[i]);
179				col = indx = 0;
180			}
181			putwchar('\n');
182
183			/* calculate the column position for the next line. */
184			col = new_column_position (col, ch);
185		}
186
187		if (indx + 1 > buf_max) {
188			/* Allocate buffer in LINE_MAX increments */
189			if ((nbuf = realloc (buf, buf_max + 2048)) == NULL) {
190				err (1, "realloc");
191				/* NOTREACHED */
192			}
193			buf = nbuf;
194			buf_max += 2048;
195		}
196		buf[indx++] = ch;
197	}
198
199	if (indx != 0) {
200		for (i = 0; i < indx; i++)
201			putwchar(buf[i]);
202	}
203}
204
205/*
206 * calculate the column position
207 */
208static int
209new_column_position (int col, wint_t ch)
210{
211	int w;
212
213	if (!count_bytes) {
214		switch (ch) {
215		case L'\b':
216			if (col > 0)
217				--col;
218			break;
219		case L'\r':
220			col = 0;
221			break;
222		case L'\t':
223			col = (col + 8) & ~7;
224			break;
225		default:
226			w = wcwidth(ch);
227			if (w > 0)
228				col += w;
229			break;
230		}
231	} else {
232		char dummy[MB_LEN_MAX];
233
234		/* XXX: we assume stateless encoding */
235		col += wcrtomb(dummy, ch, NULL);
236	}
237
238	return col;
239}
240
241static void
242usage(void)
243{
244	(void)fprintf(stderr,
245	    "usage: %s [-bs] [-w width] [file ...]\n", getprogname());
246	exit(1);
247}
248
249