xref: /netbsd-src/usr.bin/fold/fold.c (revision 60dc5170cc56468123203cda164efdced8670d03)
1*60dc5170Sjoerg /*	$NetBSD: fold.c,v 1.17 2011/09/04 20:24:59 joerg Exp $	*/
2af8b52d6Sjtc 
361f28255Scgd /*-
4af8b52d6Sjtc  * Copyright (c) 1990, 1993
5af8b52d6Sjtc  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * This code is derived from software contributed to Berkeley by
861f28255Scgd  * Kevin Ruddy.
961f28255Scgd  *
1061f28255Scgd  * Redistribution and use in source and binary forms, with or without
1161f28255Scgd  * modification, are permitted provided that the following conditions
1261f28255Scgd  * are met:
1361f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1461f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1561f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1761f28255Scgd  *    documentation and/or other materials provided with the distribution.
1889aaa1bbSagc  * 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd  *    may be used to endorse or promote products derived from this software
2061f28255Scgd  *    without specific prior written permission.
2161f28255Scgd  *
2261f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd  * SUCH DAMAGE.
3361f28255Scgd  */
3461f28255Scgd 
35c5a3c533Slukem #include <sys/cdefs.h>
3661f28255Scgd #ifndef lint
3798e5374cSlukem __COPYRIGHT("@(#) Copyright (c) 1990, 1993\
3898e5374cSlukem  The Regents of the University of California.  All rights reserved.");
3961f28255Scgd #endif /* not lint */
4061f28255Scgd 
4161f28255Scgd #ifndef lint
42af8b52d6Sjtc #if 0
43af8b52d6Sjtc static char sccsid[] = "@(#)fold.c	8.1 (Berkeley) 6/6/93";
44af8b52d6Sjtc #endif
45*60dc5170Sjoerg __RCSID("$NetBSD: fold.c,v 1.17 2011/09/04 20:24:59 joerg Exp $");
4661f28255Scgd #endif /* not lint */
4761f28255Scgd 
48b05ff95eSahoka #include <limits.h>
49b05ff95eSahoka #include <locale.h>
5061f28255Scgd #include <stdio.h>
51a02c58f5Sjtc #include <stdlib.h>
522ddbb97fSjtc #include <unistd.h>
53b05ff95eSahoka #include <wchar.h>
54b1c840c3Sjtc #include <err.h>
5561f28255Scgd 
5661f28255Scgd #define	DEFLINEWIDTH	80
5761f28255Scgd 
5888a0bc30Smjl static	void	fold(int);
59b05ff95eSahoka static	int	new_column_position(int, wint_t);
60*60dc5170Sjoerg __dead static	void	usage(void);
61c5a3c533Slukem 
62*60dc5170Sjoerg static int count_bytes = 0;
63*60dc5170Sjoerg static int split_words = 0;
64a02c58f5Sjtc 
65a02c58f5Sjtc int
main(int argc,char ** argv)6688a0bc30Smjl main(int argc, char **argv)
6761f28255Scgd {
68c5a3c533Slukem 	int ch;
6961f28255Scgd 	int width;
7061f28255Scgd 	char *p;
7161f28255Scgd 
72b05ff95eSahoka 	setlocale(LC_CTYPE, "");
73b05ff95eSahoka 	setprogname(argv[0]);
74b05ff95eSahoka 
7561f28255Scgd 	width = -1;
76b1c840c3Sjtc 	while ((ch = getopt(argc, argv, "0123456789bsw:")) != -1)
7761f28255Scgd 		switch (ch) {
78a02c58f5Sjtc 		case 'b':
79a02c58f5Sjtc 			count_bytes = 1;
80a02c58f5Sjtc 			break;
81a02c58f5Sjtc 		case 's':
82a02c58f5Sjtc 			split_words = 1;
83a02c58f5Sjtc 			break;
8461f28255Scgd 		case 'w':
858de13626Smjl 			if ((width = atoi(optarg)) <= 0)
868de13626Smjl 				errx(1, "illegal width value");
8761f28255Scgd 			break;
8861f28255Scgd 		case '0': case '1': case '2': case '3': case '4':
8961f28255Scgd 		case '5': case '6': case '7': case '8': case '9':
9061f28255Scgd 			if (width == -1) {
9161f28255Scgd 				p = argv[optind - 1];
9261f28255Scgd 				if (p[0] == '-' && p[1] == ch && !p[2])
9361f28255Scgd 					width = atoi(++p);
9461f28255Scgd 				else
9561f28255Scgd 					width = atoi(argv[optind] + 1);
9661f28255Scgd 			}
9761f28255Scgd 			break;
9861f28255Scgd 		default:
998de13626Smjl 			usage();
10061f28255Scgd 		}
10161f28255Scgd 	argv += optind;
10261f28255Scgd 	argc -= optind;
10361f28255Scgd 
10461f28255Scgd 	if (width == -1)
10561f28255Scgd 		width = DEFLINEWIDTH;
106a02c58f5Sjtc 
10761f28255Scgd 	if (!*argv)
10861f28255Scgd 		fold(width);
10961f28255Scgd 	else for (; *argv; ++argv)
11061f28255Scgd 		if (!freopen(*argv, "r", stdin)) {
111b1c840c3Sjtc 			err (1, "%s", *argv);
112b1c840c3Sjtc 			/* NOTREACHED */
11361f28255Scgd 		} else
11461f28255Scgd 			fold(width);
11561f28255Scgd 	exit(0);
11661f28255Scgd }
11761f28255Scgd 
118a02c58f5Sjtc /*
119a02c58f5Sjtc  * Fold the contents of standard input to fit within WIDTH columns
120a02c58f5Sjtc  * (or bytes) and write to standard output.
121a02c58f5Sjtc  *
122a02c58f5Sjtc  * If split_words is set, split the line at the last space character
123a02c58f5Sjtc  * on the line.  This flag necessitates storing the line in a buffer
124a02c58f5Sjtc  * until the current column > width, or a newline or EOF is read.
125a02c58f5Sjtc  *
126a02c58f5Sjtc  * The buffer can grow larger than WIDTH due to backspaces and carriage
127a02c58f5Sjtc  * returns embedded in the input stream.
128a02c58f5Sjtc  */
129a02c58f5Sjtc static void
fold(int width)13088a0bc30Smjl fold(int width)
13161f28255Scgd {
132b05ff95eSahoka 	static wchar_t *buf = NULL;
133b05ff95eSahoka 	wchar_t *nbuf;
134a02c58f5Sjtc 	static int   buf_max = 0;
135b05ff95eSahoka 	wint_t ch;
136b05ff95eSahoka 	int col, indx, i;
13761f28255Scgd 
138a02c58f5Sjtc 	col = indx = 0;
139b05ff95eSahoka 	while ((ch = getwchar()) != WEOF) {
140b05ff95eSahoka 		if (ch == L'\n') {
141b05ff95eSahoka 			if (indx != 0) {
142b05ff95eSahoka 				for (i = 0; i < indx; i++)
143b05ff95eSahoka 					putwchar(buf[i]);
144b05ff95eSahoka 			}
145b05ff95eSahoka 			putwchar(L'\n');
146a02c58f5Sjtc 			col = indx = 0;
147a02c58f5Sjtc 			continue;
14861f28255Scgd 		}
14961f28255Scgd 
150a02c58f5Sjtc 		col = new_column_position (col, ch);
151a02c58f5Sjtc 		if (col > width) {
152b05ff95eSahoka 			int last_space;
153a02c58f5Sjtc 
1545e451f42Smrg #ifdef __GNUC__
1555e451f42Smrg 			last_space = 0;	/* XXX gcc */
1565e451f42Smrg #endif
157a02c58f5Sjtc 			if (split_words) {
158a02c58f5Sjtc 				for (i = 0, last_space = -1; i < indx; i++)
159b05ff95eSahoka 					if (buf[i] == L' ')
1605e451f42Smrg 						last_space = i;
161a02c58f5Sjtc 			}
162a02c58f5Sjtc 
163a02c58f5Sjtc 			if (split_words && last_space != -1) {
164b05ff95eSahoka 				for (i = 0; i < last_space; i++)
165b05ff95eSahoka 					putwchar(buf[i]);
1664a0fb013Sahoka 
1674a0fb013Sahoka 				/* increase last_space here, so we skip trailing whitespace */
1684a0fb013Sahoka 				last_space++;
169b05ff95eSahoka 				wmemmove (buf, buf+last_space, indx-last_space);
170a02c58f5Sjtc 
171a02c58f5Sjtc 				indx -= last_space;
172a02c58f5Sjtc 				col = 0;
173a02c58f5Sjtc 				for (i = 0; i < indx; i++) {
174c8ab21f6Sfrueauf 					col = new_column_position (col, buf[i]);
175a02c58f5Sjtc 				}
176a02c58f5Sjtc 			} else {
177b05ff95eSahoka 				for (i = 0; i < indx; i++)
178b05ff95eSahoka 					putwchar(buf[i]);
179a02c58f5Sjtc 				col = indx = 0;
180a02c58f5Sjtc 			}
181b05ff95eSahoka 			putwchar('\n');
182a02c58f5Sjtc 
183a02c58f5Sjtc 			/* calculate the column position for the next line. */
184a02c58f5Sjtc 			col = new_column_position (col, ch);
185a02c58f5Sjtc 		}
186a02c58f5Sjtc 
187a02c58f5Sjtc 		if (indx + 1 > buf_max) {
188a02c58f5Sjtc 			/* Allocate buffer in LINE_MAX increments */
1894f228a52Sitojun 			if ((nbuf = realloc (buf, buf_max + 2048)) == NULL) {
190c5a3c533Slukem 				err (1, "realloc");
191b1c840c3Sjtc 				/* NOTREACHED */
192a02c58f5Sjtc 			}
1934f228a52Sitojun 			buf = nbuf;
1944f228a52Sitojun 			buf_max += 2048;
195a02c58f5Sjtc 		}
196a02c58f5Sjtc 		buf[indx++] = ch;
197a02c58f5Sjtc 	}
198a02c58f5Sjtc 
199b05ff95eSahoka 	if (indx != 0) {
200b05ff95eSahoka 		for (i = 0; i < indx; i++)
201b05ff95eSahoka 			putwchar(buf[i]);
202b05ff95eSahoka 	}
203a02c58f5Sjtc }
204a02c58f5Sjtc 
205a02c58f5Sjtc /*
206a02c58f5Sjtc  * calculate the column position
207a02c58f5Sjtc  */
208a02c58f5Sjtc static int
new_column_position(int col,wint_t ch)209b05ff95eSahoka new_column_position (int col, wint_t ch)
210a02c58f5Sjtc {
211b05ff95eSahoka 	int w;
212b05ff95eSahoka 
213a02c58f5Sjtc 	if (!count_bytes) {
21461f28255Scgd 		switch (ch) {
215b05ff95eSahoka 		case L'\b':
21661f28255Scgd 			if (col > 0)
21761f28255Scgd 				--col;
21861f28255Scgd 			break;
219b05ff95eSahoka 		case L'\r':
22061f28255Scgd 			col = 0;
22161f28255Scgd 			break;
222b05ff95eSahoka 		case L'\t':
223a02c58f5Sjtc 			col = (col + 8) & ~7;
22461f28255Scgd 			break;
22561f28255Scgd 		default:
226b05ff95eSahoka 			w = wcwidth(ch);
227b05ff95eSahoka 			if (w > 0)
228b05ff95eSahoka 				col += w;
22961f28255Scgd 			break;
23061f28255Scgd 		}
231a02c58f5Sjtc 	} else {
232b05ff95eSahoka 		char dummy[MB_LEN_MAX];
233b05ff95eSahoka 
234b05ff95eSahoka 		/* XXX: we assume stateless encoding */
235b05ff95eSahoka 		col += wcrtomb(dummy, ch, NULL);
23661f28255Scgd 	}
237a02c58f5Sjtc 
238a02c58f5Sjtc 	return col;
23961f28255Scgd }
2408de13626Smjl 
24188a0bc30Smjl static void
usage(void)24288a0bc30Smjl usage(void)
2438de13626Smjl {
2448de13626Smjl 	(void)fprintf(stderr,
245b05ff95eSahoka 	    "usage: %s [-bs] [-w width] [file ...]\n", getprogname());
2468de13626Smjl 	exit(1);
2478de13626Smjl }
2488de13626Smjl 
249