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 43 static 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 58 int main(int, char **); 59 static void fold(int); 60 static int new_column_position(int, wint_t); 61 static void usage(void); 62 63 int count_bytes = 0; 64 int split_words = 0; 65 66 int 67 main(int argc, char **argv) 68 { 69 int ch; 70 int width; 71 char *p; 72 73 setlocale(LC_CTYPE, ""); 74 setprogname(argv[0]); 75 76 width = -1; 77 while ((ch = getopt(argc, argv, "0123456789bsw:")) != -1) 78 switch (ch) { 79 case 'b': 80 count_bytes = 1; 81 break; 82 case 's': 83 split_words = 1; 84 break; 85 case 'w': 86 if ((width = atoi(optarg)) <= 0) 87 errx(1, "illegal width value"); 88 break; 89 case '0': case '1': case '2': case '3': case '4': 90 case '5': case '6': case '7': case '8': case '9': 91 if (width == -1) { 92 p = argv[optind - 1]; 93 if (p[0] == '-' && p[1] == ch && !p[2]) 94 width = atoi(++p); 95 else 96 width = atoi(argv[optind] + 1); 97 } 98 break; 99 default: 100 usage(); 101 } 102 argv += optind; 103 argc -= optind; 104 105 if (width == -1) 106 width = DEFLINEWIDTH; 107 108 if (!*argv) 109 fold(width); 110 else for (; *argv; ++argv) 111 if (!freopen(*argv, "r", stdin)) { 112 err (1, "%s", *argv); 113 /* NOTREACHED */ 114 } else 115 fold(width); 116 exit(0); 117 } 118 119 /* 120 * Fold the contents of standard input to fit within WIDTH columns 121 * (or bytes) and write to standard output. 122 * 123 * If split_words is set, split the line at the last space character 124 * on the line. This flag necessitates storing the line in a buffer 125 * until the current column > width, or a newline or EOF is read. 126 * 127 * The buffer can grow larger than WIDTH due to backspaces and carriage 128 * returns embedded in the input stream. 129 */ 130 static void 131 fold(int width) 132 { 133 static wchar_t *buf = NULL; 134 wchar_t *nbuf; 135 static int buf_max = 0; 136 wint_t ch; 137 int col, indx, i; 138 139 col = indx = 0; 140 while ((ch = getwchar()) != WEOF) { 141 if (ch == L'\n') { 142 if (indx != 0) { 143 for (i = 0; i < indx; i++) 144 putwchar(buf[i]); 145 } 146 putwchar(L'\n'); 147 col = indx = 0; 148 continue; 149 } 150 151 col = new_column_position (col, ch); 152 if (col > width) { 153 int last_space; 154 155 #ifdef __GNUC__ 156 last_space = 0; /* XXX gcc */ 157 #endif 158 if (split_words) { 159 for (i = 0, last_space = -1; i < indx; i++) 160 if (buf[i] == L' ') 161 last_space = i; 162 } 163 164 if (split_words && last_space != -1) { 165 for (i = 0; i < last_space; i++) 166 putwchar(buf[i]); 167 168 /* increase last_space here, so we skip trailing whitespace */ 169 last_space++; 170 wmemmove (buf, buf+last_space, indx-last_space); 171 172 indx -= last_space; 173 col = 0; 174 for (i = 0; i < indx; i++) { 175 col = new_column_position (col, buf[i]); 176 } 177 } else { 178 for (i = 0; i < indx; i++) 179 putwchar(buf[i]); 180 col = indx = 0; 181 } 182 putwchar('\n'); 183 184 /* calculate the column position for the next line. */ 185 col = new_column_position (col, ch); 186 } 187 188 if (indx + 1 > buf_max) { 189 /* Allocate buffer in LINE_MAX increments */ 190 if ((nbuf = realloc (buf, buf_max + 2048)) == NULL) { 191 err (1, "realloc"); 192 /* NOTREACHED */ 193 } 194 buf = nbuf; 195 buf_max += 2048; 196 } 197 buf[indx++] = ch; 198 } 199 200 if (indx != 0) { 201 for (i = 0; i < indx; i++) 202 putwchar(buf[i]); 203 } 204 } 205 206 /* 207 * calculate the column position 208 */ 209 static int 210 new_column_position (int col, wint_t ch) 211 { 212 int w; 213 214 if (!count_bytes) { 215 switch (ch) { 216 case L'\b': 217 if (col > 0) 218 --col; 219 break; 220 case L'\r': 221 col = 0; 222 break; 223 case L'\t': 224 col = (col + 8) & ~7; 225 break; 226 default: 227 w = wcwidth(ch); 228 if (w > 0) 229 col += w; 230 break; 231 } 232 } else { 233 char dummy[MB_LEN_MAX]; 234 235 /* XXX: we assume stateless encoding */ 236 col += wcrtomb(dummy, ch, NULL); 237 } 238 239 return col; 240 } 241 242 static void 243 usage(void) 244 { 245 (void)fprintf(stderr, 246 "usage: %s [-bs] [-w width] [file ...]\n", getprogname()); 247 exit(1); 248 } 249 250