sed(1): Sync with FreeBSD (adds -u switch)
[dragonfly.git] / usr.bin / fold / fold.c
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: src/usr.bin/fold/fold.c,v 1.4.2.3 2002/07/11 01:01:44 tjr Exp $
35  * $DragonFly: src/usr.bin/fold/fold.c,v 1.5 2006/08/13 02:15:07 swildner Exp $
36  */
37
38 #include <ctype.h>
39 #include <err.h>
40 #include <limits.h>
41 #include <locale.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46
47 #define DEFLINEWIDTH    80
48
49 void fold(int);
50 static int newpos(int, int);
51 static void usage(void);
52
53 int bflag;                      /* Count bytes, not columns */
54 int sflag;                      /* Split on word boundaries */
55
56 int
57 main(int argc, char **argv)
58 {
59         int ch;
60         int rval, width;
61         char *p;
62
63         (void) setlocale(LC_CTYPE, "");
64
65         width = -1;
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                         if (width == -1) {
82                                 p = argv[optind - 1];
83                                 if (p[0] == '-' && p[1] == ch && !p[2])
84                                         width = atoi(++p);
85                                 else
86                                         width = atoi(argv[optind] + 1);
87                         }
88                         break;
89                 default:
90                         usage();
91                 }
92         argv += optind;
93         argc -= optind;
94
95         if (width == -1)
96                 width = DEFLINEWIDTH;
97         rval = 0;
98         if (!*argv)
99                 fold(width);
100         else for (; *argv; ++argv)
101                 if (!freopen(*argv, "r", stdin)) {
102                         warn("%s", *argv);
103                         rval = 1;
104                 } else
105                         fold(width);
106         exit(rval);
107 }
108
109 static void
110 usage(void)
111 {
112         (void)fprintf(stderr, "usage: fold [-bs] [-w width] [file ...]\n");
113         exit(1);
114 }
115
116 /*
117  * Fold the contents of standard input to fit within WIDTH columns (or bytes)
118  * and write to standard output.
119  *
120  * If sflag is set, split the line at the last space character on the line.
121  * This flag necessitates storing the line in a buffer until the current
122  * column > width, or a newline or EOF is read.
123  *
124  * The buffer can grow larger than WIDTH due to backspaces and carriage
125  * returns embedded in the input stream.
126  */
127 void
128 fold(int width)
129 {
130         static char *buf;
131         static int buf_max;
132         int ch, col, i, indx, space;
133
134         col = indx = space = 0;
135         while ((ch = getchar()) != EOF) {
136                 if (ch == '\n') {
137                         if (indx != 0)
138                                 fwrite(buf, 1, indx, stdout);
139                         putchar('\n');
140                         col = indx = 0;
141                         continue;
142                 }
143                 if ((col = newpos(col, ch)) > width) {
144                         if (sflag) {
145                                 i = indx;
146                                 while (--i >= 0 && !isblank((unsigned char)buf[i]))
147                                         ;
148                                 space = i;
149                         }
150                         if (sflag && space != -1) {
151                                 space++;
152                                 fwrite(buf, 1, space, stdout);
153                                 memmove(buf, buf + space, indx - space);
154                                 indx -= space;
155                                 col = 0;
156                                 for (i = 0; i < indx; i++)
157                                         col = newpos(col,
158                                             (unsigned char)buf[i]);
159                         } else {
160                                 fwrite(buf, 1, indx, stdout);
161                                 col = indx = 0;
162                         }
163                         putchar('\n');
164                         col = newpos(col, ch);
165                 }
166                 if (indx + 1 > buf_max) {
167                         buf_max += LINE_MAX;
168                         if ((buf = realloc(buf, buf_max)) == NULL)
169                                 err(1, "realloc()");
170                 }
171                 buf[indx++] = ch;
172         }
173
174         if (indx != 0)
175                 fwrite(buf, 1, indx, stdout);
176 }
177
178 /*
179  * Update the current column position for a character.
180  */
181 static int
182 newpos(int col, int ch)
183 {
184
185         if (bflag)
186                 ++col;
187         else
188                 switch (ch) {
189                 case '\b':
190                         if (col > 0)
191                                 --col;
192                         break;
193                 case '\r':
194                         col = 0;
195                         break;
196                 case '\t':
197                         col = (col + 8) & ~7;
198                         break;
199                 default:
200                         if (isprint(ch))
201                                 ++col;
202                         break;
203                 }
204
205         return (col);
206 }