blob: 3fe668512468e6923d356da80ee5bfe48c2218ec [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrathf01b8052003-04-27 06:02:14 +00002/* fold -- wrap each input line to fit in specified width.
3
4 Written by David MacKenzie, djm@gnu.ai.mit.edu.
5 Copyright (C) 91, 1995-2002 Free Software Foundation, Inc.
6
7 Modified for busybox based on coreutils v 5.0
Denis Vlasenko0beaff82007-09-21 13:16:32 +00008 Copyright (C) 2003 Glenn McGrath
Glenn L McGrathf01b8052003-04-27 06:02:14 +00009
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020010 Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrathf01b8052003-04-27 06:02:14 +000011*/
Pere Orga34425382011-03-31 14:43:25 +020012
13//usage:#define fold_trivial_usage
14//usage: "[-bs] [-w WIDTH] [FILE]..."
15//usage:#define fold_full_usage "\n\n"
16//usage: "Wrap input lines in each FILE (or stdin), writing to stdout\n"
17//usage: "\nOptions:"
18//usage: "\n -b Count bytes rather than columns"
19//usage: "\n -s Break at spaces"
20//usage: "\n -w Use WIDTH columns instead of 80"
21
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000022#include "libbb.h"
Denys Vlasenko28055022010-01-04 20:49:58 +010023#include "unicode.h"
Glenn L McGrathf01b8052003-04-27 06:02:14 +000024
Dan Fandrich2d1a78b2010-09-30 14:31:12 -070025/* This is a NOEXEC applet. Be very careful! */
26
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000027/* Must match getopt32 call */
28#define FLAG_COUNT_BYTES 1
29#define FLAG_BREAK_SPACES 2
30#define FLAG_WIDTH 4
Glenn L McGrathf01b8052003-04-27 06:02:14 +000031
32/* Assuming the current column is COLUMN, return the column that
33 printing C will move the cursor to.
34 The first column is 0. */
Denys Vlasenko28055022010-01-04 20:49:58 +010035static int adjust_column(unsigned column, char c)
Glenn L McGrathf01b8052003-04-27 06:02:14 +000036{
Denys Vlasenko28055022010-01-04 20:49:58 +010037 if (option_mask32 & FLAG_COUNT_BYTES)
38 return ++column;
39
40 if (c == '\t')
41 return column + 8 - column % 8;
42
43 if (c == '\b') {
44 if ((int)--column < 0)
Glenn L McGrathf01b8052003-04-27 06:02:14 +000045 column = 0;
Denys Vlasenko28055022010-01-04 20:49:58 +010046 }
47 else if (c == '\r')
48 column = 0;
49 else { /* just a printable char */
50 if (unicode_status != UNICODE_ON /* every byte is a new char */
51 || (c & 0xc0) != 0x80 /* it isn't a 2nd+ byte of a Unicode char */
52 ) {
Glenn L McGrathf01b8052003-04-27 06:02:14 +000053 column++;
Denys Vlasenko28055022010-01-04 20:49:58 +010054 }
55 }
Glenn L McGrathf01b8052003-04-27 06:02:14 +000056 return column;
57}
58
Denys Vlasenko28055022010-01-04 20:49:58 +010059/* Note that this function can write NULs, unlike fputs etc. */
60static void write2stdout(const void *buf, unsigned size)
61{
62 fwrite(buf, 1, size, stdout);
63}
64
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000065int fold_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoded688c2009-11-04 15:31:19 +010066int fold_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrathf01b8052003-04-27 06:02:14 +000067{
Denis Vlasenko931de892007-06-21 12:43:45 +000068 char *line_out = NULL;
Denys Vlasenko28055022010-01-04 20:49:58 +010069 const char *w_opt = "80";
70 unsigned width;
71 smallint exitcode = EXIT_SUCCESS;
72
73 init_unicode();
Glenn L McGrathf01b8052003-04-27 06:02:14 +000074
Denis Vlasenko08492072006-12-22 13:56:36 +000075 if (ENABLE_INCLUDE_SUSv2) {
Rob Landleyf8fd4db2006-01-30 01:30:39 +000076 /* Turn any numeric options into -w options. */
Denys Vlasenko28055022010-01-04 20:49:58 +010077 int i;
Denys Vlasenkoded688c2009-11-04 15:31:19 +010078 for (i = 1; argv[i]; i++) {
Denys Vlasenko28055022010-01-04 20:49:58 +010079 const char *a = argv[i];
80 if (*a == '-') {
81 a++;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000082 if (*a == '-' && !a[1]) /* "--" */
Rob Landleyf8fd4db2006-01-30 01:30:39 +000083 break;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000084 if (isdigit(*a))
Rob Landleyd921b2e2006-08-03 15:41:12 +000085 argv[i] = xasprintf("-w%s", a);
Glenn L McGrathf01b8052003-04-27 06:02:14 +000086 }
87 }
88 }
89
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000090 getopt32(argv, "bsw:", &w_opt);
Denys Vlasenko28055022010-01-04 20:49:58 +010091 width = xatou_range(w_opt, 1, 10000);
Glenn L McGrathf01b8052003-04-27 06:02:14 +000092
93 argv += optind;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000094 if (!*argv)
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000095 *--argv = (char*)"-";
Glenn L McGrathf01b8052003-04-27 06:02:14 +000096
97 do {
Denis Vlasenkoddec5af2006-10-26 23:25:17 +000098 FILE *istream = fopen_or_warn_stdin(*argv);
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +000099 int c;
Denys Vlasenko28055022010-01-04 20:49:58 +0100100 unsigned column = 0; /* Screen column where next char will go */
101 unsigned offset_out = 0; /* Index in 'line_out' for next char */
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000102
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000103 if (istream == NULL) {
Denys Vlasenko28055022010-01-04 20:49:58 +0100104 exitcode = EXIT_FAILURE;
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000105 continue;
106 }
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000107
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000108 while ((c = getc(istream)) != EOF) {
Denys Vlasenko28055022010-01-04 20:49:58 +0100109 /* We grow line_out in chunks of 0x1000 bytes */
110 if ((offset_out & 0xfff) == 0) {
111 line_out = xrealloc(line_out, offset_out + 0x1000);
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000112 }
Denys Vlasenko28055022010-01-04 20:49:58 +0100113 rescan:
114 line_out[offset_out] = c;
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000115 if (c == '\n') {
Denys Vlasenko28055022010-01-04 20:49:58 +0100116 write2stdout(line_out, offset_out + 1);
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000117 column = offset_out = 0;
118 continue;
119 }
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000120 column = adjust_column(column, c);
Denys Vlasenko28055022010-01-04 20:49:58 +0100121 if (column <= width || offset_out == 0) {
122 /* offset_out == 0 case happens
123 * with small width (say, 1) and tabs.
124 * The very first tab already goes to column 8,
125 * but we must not wrap it */
126 offset_out++;
127 continue;
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000128 }
129
Denys Vlasenko28055022010-01-04 20:49:58 +0100130 /* This character would make the line too long.
131 * Print the line plus a newline, and make this character
132 * start the next line */
133 if (option_mask32 & FLAG_BREAK_SPACES) {
134 unsigned i;
135 unsigned logical_end;
136
137 /* Look for the last blank. */
138 for (logical_end = offset_out - 1; (int)logical_end >= 0; logical_end--) {
139 if (!isblank(line_out[logical_end]))
140 continue;
141
142 /* Found a space or tab.
143 * Output up to and including it, and start a new line */
144 logical_end++;
145 /*line_out[logical_end] = '\n'; - NO! this nukes one buffered character */
146 write2stdout(line_out, logical_end);
147 putchar('\n');
148 /* Move the remainder to the beginning of the next line.
149 * The areas being copied here might overlap. */
150 memmove(line_out, line_out + logical_end, offset_out - logical_end);
151 offset_out -= logical_end;
152 for (column = i = 0; i < offset_out; i++) {
153 column = adjust_column(column, line_out[i]);
154 }
155 goto rescan;
156 }
157 /* No blank found, wrap will split the overlong word */
158 }
159 /* Output what we accumulated up to now, and start a new line */
160 line_out[offset_out] = '\n';
161 write2stdout(line_out, offset_out + 1);
162 column = offset_out = 0;
163 goto rescan;
164 } /* while (not EOF) */
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000165
166 if (offset_out) {
Denys Vlasenko28055022010-01-04 20:49:58 +0100167 write2stdout(line_out, offset_out);
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000168 }
169
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000170 if (fclose_if_not_stdin(istream)) {
Denys Vlasenko28055022010-01-04 20:49:58 +0100171 bb_simple_perror_msg(*argv);
172 exitcode = EXIT_FAILURE;
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000173 }
174 } while (*++argv);
175
Denys Vlasenko28055022010-01-04 20:49:58 +0100176 fflush_stdout_and_exit(exitcode);
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000177}