xref: /freebsd/usr.bin/fold/fold.c (revision 4f52dfbb8d6c4d446500c5b097e3806ec219fbd4)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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 #ifndef lint
36 static const char copyright[] =
37 "@(#) Copyright (c) 1990, 1993\n\
38 	The Regents of the University of California.  All rights reserved.\n";
39 #endif /* not lint */
40 
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)fold.c	8.1 (Berkeley) 6/6/93";
44 #endif
45 #endif /* not lint */
46 
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
49 
50 #include <err.h>
51 #include <limits.h>
52 #include <locale.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 #include <wchar.h>
58 #include <wctype.h>
59 
60 #define	DEFLINEWIDTH	80
61 
62 void fold(int);
63 static int newpos(int, wint_t);
64 static void usage(void);
65 
66 static int bflag;		/* Count bytes, not columns */
67 static int sflag;		/* Split on word boundaries */
68 
69 int
70 main(int argc, char **argv)
71 {
72 	int ch, previous_ch;
73 	int rval, width;
74 
75 	(void) setlocale(LC_CTYPE, "");
76 
77 	width = -1;
78 	previous_ch = 0;
79 	while ((ch = getopt(argc, argv, "0123456789bsw:")) != -1) {
80 		switch (ch) {
81 		case 'b':
82 			bflag = 1;
83 			break;
84 		case 's':
85 			sflag = 1;
86 			break;
87 		case 'w':
88 			if ((width = atoi(optarg)) <= 0) {
89 				errx(1, "illegal width value");
90 			}
91 			break;
92 		case '0': case '1': case '2': case '3': case '4':
93 		case '5': case '6': case '7': case '8': case '9':
94 			/* Accept a width as eg. -30. Note that a width
95 			 * specified using the -w option is always used prior
96 			 * to this undocumented option. */
97 			switch (previous_ch) {
98 			case '0': case '1': case '2': case '3': case '4':
99 			case '5': case '6': case '7': case '8': case '9':
100 				/* The width is a number with multiple digits:
101 				 * add the last one. */
102 				width = width * 10 + (ch - '0');
103 				break;
104 			default:
105 				/* Set the width, unless it was previously
106 				 * set. For instance, the following options
107 				 * would all give a width of 5 and not 10:
108 				 *   -10 -w5
109 				 *   -5b10
110 				 *   -5 -10b */
111 				if (width == -1)
112 					width = ch - '0';
113 				break;
114 			}
115 			break;
116 		default:
117 			usage();
118 		}
119 		previous_ch = ch;
120 	}
121 	argv += optind;
122 	argc -= optind;
123 
124 	if (width == -1)
125 		width = DEFLINEWIDTH;
126 	rval = 0;
127 	if (!*argv)
128 		fold(width);
129 	else for (; *argv; ++argv)
130 		if (!freopen(*argv, "r", stdin)) {
131 			warn("%s", *argv);
132 			rval = 1;
133 		} else
134 			fold(width);
135 	exit(rval);
136 }
137 
138 static void
139 usage(void)
140 {
141 	(void)fprintf(stderr, "usage: fold [-bs] [-w width] [file ...]\n");
142 	exit(1);
143 }
144 
145 /*
146  * Fold the contents of standard input to fit within WIDTH columns (or bytes)
147  * and write to standard output.
148  *
149  * If sflag is set, split the line at the last space character on the line.
150  * This flag necessitates storing the line in a buffer until the current
151  * column > width, or a newline or EOF is read.
152  *
153  * The buffer can grow larger than WIDTH due to backspaces and carriage
154  * returns embedded in the input stream.
155  */
156 void
157 fold(int width)
158 {
159 	static wchar_t *buf;
160 	static int buf_max;
161 	int col, i, indx, space;
162 	wint_t ch;
163 
164 	col = indx = 0;
165 	while ((ch = getwchar()) != WEOF) {
166 		if (ch == '\n') {
167 			wprintf(L"%.*ls\n", indx, buf);
168 			col = indx = 0;
169 			continue;
170 		}
171 		if ((col = newpos(col, ch)) > width) {
172 			if (sflag) {
173 				i = indx;
174 				while (--i >= 0 && !iswblank(buf[i]))
175 					;
176 				space = i;
177 			}
178 			if (sflag && space != -1) {
179 				space++;
180 				wprintf(L"%.*ls\n", space, buf);
181 				wmemmove(buf, buf + space, indx - space);
182 				indx -= space;
183 				col = 0;
184 				for (i = 0; i < indx; i++)
185 					col = newpos(col, buf[i]);
186 			} else {
187 				wprintf(L"%.*ls\n", indx, buf);
188 				col = indx = 0;
189 			}
190 			col = newpos(col, ch);
191 		}
192 		if (indx + 1 > buf_max) {
193 			buf_max += LINE_MAX;
194 			buf = realloc(buf, sizeof(*buf) * buf_max);
195 			if (buf == NULL)
196 				err(1, "realloc()");
197 		}
198 		buf[indx++] = ch;
199 	}
200 
201 	if (indx != 0)
202 		wprintf(L"%.*ls", indx, buf);
203 }
204 
205 /*
206  * Update the current column position for a character.
207  */
208 static int
209 newpos(int col, wint_t ch)
210 {
211 	char buf[MB_LEN_MAX];
212 	size_t len;
213 	int w;
214 
215 	if (bflag) {
216 		len = wcrtomb(buf, ch, NULL);
217 		col += len;
218 	} else
219 		switch (ch) {
220 		case '\b':
221 			if (col > 0)
222 				--col;
223 			break;
224 		case '\r':
225 			col = 0;
226 			break;
227 		case '\t':
228 			col = (col + 8) & ~7;
229 			break;
230 		default:
231 			if ((w = wcwidth(ch)) > 0)
232 				col += w;
233 			break;
234 		}
235 
236 	return (col);
237 }
238