blob: 4a6429ad7b69d1cf2ce07b9639f1411f00d77855 [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*/
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000012#include "libbb.h"
Denys Vlasenko28055022010-01-04 20:49:58 +010013#include "unicode.h"
Glenn L McGrathf01b8052003-04-27 06:02:14 +000014
Dan Fandrich2d1a78b2010-09-30 14:31:12 -070015/* This is a NOEXEC applet. Be very careful! */
16
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000017/* Must match getopt32 call */
18#define FLAG_COUNT_BYTES 1
19#define FLAG_BREAK_SPACES 2
20#define FLAG_WIDTH 4
Glenn L McGrathf01b8052003-04-27 06:02:14 +000021
22/* Assuming the current column is COLUMN, return the column that
23 printing C will move the cursor to.
24 The first column is 0. */
Denys Vlasenko28055022010-01-04 20:49:58 +010025static int adjust_column(unsigned column, char c)
Glenn L McGrathf01b8052003-04-27 06:02:14 +000026{
Denys Vlasenko28055022010-01-04 20:49:58 +010027 if (option_mask32 & FLAG_COUNT_BYTES)
28 return ++column;
29
30 if (c == '\t')
31 return column + 8 - column % 8;
32
33 if (c == '\b') {
34 if ((int)--column < 0)
Glenn L McGrathf01b8052003-04-27 06:02:14 +000035 column = 0;
Denys Vlasenko28055022010-01-04 20:49:58 +010036 }
37 else if (c == '\r')
38 column = 0;
39 else { /* just a printable char */
40 if (unicode_status != UNICODE_ON /* every byte is a new char */
41 || (c & 0xc0) != 0x80 /* it isn't a 2nd+ byte of a Unicode char */
42 ) {
Glenn L McGrathf01b8052003-04-27 06:02:14 +000043 column++;
Denys Vlasenko28055022010-01-04 20:49:58 +010044 }
45 }
Glenn L McGrathf01b8052003-04-27 06:02:14 +000046 return column;
47}
48
Denys Vlasenko28055022010-01-04 20:49:58 +010049/* Note that this function can write NULs, unlike fputs etc. */
50static void write2stdout(const void *buf, unsigned size)
51{
52 fwrite(buf, 1, size, stdout);
53}
54
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000055int fold_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoded688c2009-11-04 15:31:19 +010056int fold_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrathf01b8052003-04-27 06:02:14 +000057{
Denis Vlasenko931de892007-06-21 12:43:45 +000058 char *line_out = NULL;
Denys Vlasenko28055022010-01-04 20:49:58 +010059 const char *w_opt = "80";
60 unsigned width;
61 smallint exitcode = EXIT_SUCCESS;
62
63 init_unicode();
Glenn L McGrathf01b8052003-04-27 06:02:14 +000064
Denis Vlasenko08492072006-12-22 13:56:36 +000065 if (ENABLE_INCLUDE_SUSv2) {
Rob Landleyf8fd4db2006-01-30 01:30:39 +000066 /* Turn any numeric options into -w options. */
Denys Vlasenko28055022010-01-04 20:49:58 +010067 int i;
Denys Vlasenkoded688c2009-11-04 15:31:19 +010068 for (i = 1; argv[i]; i++) {
Denys Vlasenko28055022010-01-04 20:49:58 +010069 const char *a = argv[i];
70 if (*a == '-') {
71 a++;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000072 if (*a == '-' && !a[1]) /* "--" */
Rob Landleyf8fd4db2006-01-30 01:30:39 +000073 break;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000074 if (isdigit(*a))
Rob Landleyd921b2e2006-08-03 15:41:12 +000075 argv[i] = xasprintf("-w%s", a);
Glenn L McGrathf01b8052003-04-27 06:02:14 +000076 }
77 }
78 }
79
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000080 getopt32(argv, "bsw:", &w_opt);
Denys Vlasenko28055022010-01-04 20:49:58 +010081 width = xatou_range(w_opt, 1, 10000);
Glenn L McGrathf01b8052003-04-27 06:02:14 +000082
83 argv += optind;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000084 if (!*argv)
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000085 *--argv = (char*)"-";
Glenn L McGrathf01b8052003-04-27 06:02:14 +000086
87 do {
Denis Vlasenkoddec5af2006-10-26 23:25:17 +000088 FILE *istream = fopen_or_warn_stdin(*argv);
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +000089 int c;
Denys Vlasenko28055022010-01-04 20:49:58 +010090 unsigned column = 0; /* Screen column where next char will go */
91 unsigned offset_out = 0; /* Index in 'line_out' for next char */
Glenn L McGrathf01b8052003-04-27 06:02:14 +000092
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +000093 if (istream == NULL) {
Denys Vlasenko28055022010-01-04 20:49:58 +010094 exitcode = EXIT_FAILURE;
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +000095 continue;
96 }
Glenn L McGrathf01b8052003-04-27 06:02:14 +000097
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +000098 while ((c = getc(istream)) != EOF) {
Denys Vlasenko28055022010-01-04 20:49:58 +010099 /* We grow line_out in chunks of 0x1000 bytes */
100 if ((offset_out & 0xfff) == 0) {
101 line_out = xrealloc(line_out, offset_out + 0x1000);
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000102 }
Denys Vlasenko28055022010-01-04 20:49:58 +0100103 rescan:
104 line_out[offset_out] = c;
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000105 if (c == '\n') {
Denys Vlasenko28055022010-01-04 20:49:58 +0100106 write2stdout(line_out, offset_out + 1);
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000107 column = offset_out = 0;
108 continue;
109 }
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000110 column = adjust_column(column, c);
Denys Vlasenko28055022010-01-04 20:49:58 +0100111 if (column <= width || offset_out == 0) {
112 /* offset_out == 0 case happens
113 * with small width (say, 1) and tabs.
114 * The very first tab already goes to column 8,
115 * but we must not wrap it */
116 offset_out++;
117 continue;
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000118 }
119
Denys Vlasenko28055022010-01-04 20:49:58 +0100120 /* This character would make the line too long.
121 * Print the line plus a newline, and make this character
122 * start the next line */
123 if (option_mask32 & FLAG_BREAK_SPACES) {
124 unsigned i;
125 unsigned logical_end;
126
127 /* Look for the last blank. */
128 for (logical_end = offset_out - 1; (int)logical_end >= 0; logical_end--) {
129 if (!isblank(line_out[logical_end]))
130 continue;
131
132 /* Found a space or tab.
133 * Output up to and including it, and start a new line */
134 logical_end++;
135 /*line_out[logical_end] = '\n'; - NO! this nukes one buffered character */
136 write2stdout(line_out, logical_end);
137 putchar('\n');
138 /* Move the remainder to the beginning of the next line.
139 * The areas being copied here might overlap. */
140 memmove(line_out, line_out + logical_end, offset_out - logical_end);
141 offset_out -= logical_end;
142 for (column = i = 0; i < offset_out; i++) {
143 column = adjust_column(column, line_out[i]);
144 }
145 goto rescan;
146 }
147 /* No blank found, wrap will split the overlong word */
148 }
149 /* Output what we accumulated up to now, and start a new line */
150 line_out[offset_out] = '\n';
151 write2stdout(line_out, offset_out + 1);
152 column = offset_out = 0;
153 goto rescan;
154 } /* while (not EOF) */
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000155
156 if (offset_out) {
Denys Vlasenko28055022010-01-04 20:49:58 +0100157 write2stdout(line_out, offset_out);
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000158 }
159
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000160 if (fclose_if_not_stdin(istream)) {
Denys Vlasenko28055022010-01-04 20:49:58 +0100161 bb_simple_perror_msg(*argv);
162 exitcode = EXIT_FAILURE;
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000163 }
164 } while (*++argv);
165
Denys Vlasenko28055022010-01-04 20:49:58 +0100166 fflush_stdout_and_exit(exitcode);
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000167}