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