blob: 286db228d45e58a9c31c05aa3375c6bc26fb6194 [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*/
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010012//config:config FOLD
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020013//config: bool "fold (4.6 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010014//config: default y
15//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020016//config: Wrap text to fit a specific width.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010017
18//applet:IF_FOLD(APPLET_NOEXEC(fold, fold, BB_DIR_USR_BIN, BB_SUID_DROP, fold))
19
20//kbuild:lib-$(CONFIG_FOLD) += fold.o
Pere Orga34425382011-03-31 14:43:25 +020021
22//usage:#define fold_trivial_usage
23//usage: "[-bs] [-w WIDTH] [FILE]..."
24//usage:#define fold_full_usage "\n\n"
25//usage: "Wrap input lines in each FILE (or stdin), writing to stdout\n"
Pere Orga34425382011-03-31 14:43:25 +020026//usage: "\n -b Count bytes rather than columns"
27//usage: "\n -s Break at spaces"
28//usage: "\n -w Use WIDTH columns instead of 80"
29
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000030#include "libbb.h"
Denys Vlasenko28055022010-01-04 20:49:58 +010031#include "unicode.h"
Glenn L McGrathf01b8052003-04-27 06:02:14 +000032
Dan Fandrich2d1a78b2010-09-30 14:31:12 -070033/* This is a NOEXEC applet. Be very careful! */
34
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000035/* Must match getopt32 call */
36#define FLAG_COUNT_BYTES 1
37#define FLAG_BREAK_SPACES 2
38#define FLAG_WIDTH 4
Glenn L McGrathf01b8052003-04-27 06:02:14 +000039
40/* Assuming the current column is COLUMN, return the column that
41 printing C will move the cursor to.
42 The first column is 0. */
Denys Vlasenko28055022010-01-04 20:49:58 +010043static int adjust_column(unsigned column, char c)
Glenn L McGrathf01b8052003-04-27 06:02:14 +000044{
Denys Vlasenko28055022010-01-04 20:49:58 +010045 if (option_mask32 & FLAG_COUNT_BYTES)
46 return ++column;
47
48 if (c == '\t')
49 return column + 8 - column % 8;
50
51 if (c == '\b') {
52 if ((int)--column < 0)
Glenn L McGrathf01b8052003-04-27 06:02:14 +000053 column = 0;
Denys Vlasenko28055022010-01-04 20:49:58 +010054 }
55 else if (c == '\r')
56 column = 0;
57 else { /* just a printable char */
58 if (unicode_status != UNICODE_ON /* every byte is a new char */
59 || (c & 0xc0) != 0x80 /* it isn't a 2nd+ byte of a Unicode char */
60 ) {
Glenn L McGrathf01b8052003-04-27 06:02:14 +000061 column++;
Denys Vlasenko28055022010-01-04 20:49:58 +010062 }
63 }
Glenn L McGrathf01b8052003-04-27 06:02:14 +000064 return column;
65}
66
Denys Vlasenko28055022010-01-04 20:49:58 +010067/* Note that this function can write NULs, unlike fputs etc. */
68static void write2stdout(const void *buf, unsigned size)
69{
70 fwrite(buf, 1, size, stdout);
71}
72
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000073int fold_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoded688c2009-11-04 15:31:19 +010074int fold_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrathf01b8052003-04-27 06:02:14 +000075{
Denis Vlasenko931de892007-06-21 12:43:45 +000076 char *line_out = NULL;
Denys Vlasenko28055022010-01-04 20:49:58 +010077 const char *w_opt = "80";
78 unsigned width;
79 smallint exitcode = EXIT_SUCCESS;
80
81 init_unicode();
Glenn L McGrathf01b8052003-04-27 06:02:14 +000082
Denis Vlasenko08492072006-12-22 13:56:36 +000083 if (ENABLE_INCLUDE_SUSv2) {
Rob Landleyf8fd4db2006-01-30 01:30:39 +000084 /* Turn any numeric options into -w options. */
Denys Vlasenko28055022010-01-04 20:49:58 +010085 int i;
Denys Vlasenkoded688c2009-11-04 15:31:19 +010086 for (i = 1; argv[i]; i++) {
Denys Vlasenko28055022010-01-04 20:49:58 +010087 const char *a = argv[i];
88 if (*a == '-') {
89 a++;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000090 if (*a == '-' && !a[1]) /* "--" */
Rob Landleyf8fd4db2006-01-30 01:30:39 +000091 break;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000092 if (isdigit(*a))
Rob Landleyd921b2e2006-08-03 15:41:12 +000093 argv[i] = xasprintf("-w%s", a);
Glenn L McGrathf01b8052003-04-27 06:02:14 +000094 }
95 }
96 }
97
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000098 getopt32(argv, "bsw:", &w_opt);
Denys Vlasenko28055022010-01-04 20:49:58 +010099 width = xatou_range(w_opt, 1, 10000);
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000100
101 argv += optind;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000102 if (!*argv)
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000103 *--argv = (char*)"-";
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000104
105 do {
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000106 FILE *istream = fopen_or_warn_stdin(*argv);
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000107 int c;
Denys Vlasenko28055022010-01-04 20:49:58 +0100108 unsigned column = 0; /* Screen column where next char will go */
109 unsigned offset_out = 0; /* Index in 'line_out' for next char */
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000110
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000111 if (istream == NULL) {
Denys Vlasenko28055022010-01-04 20:49:58 +0100112 exitcode = EXIT_FAILURE;
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000113 continue;
114 }
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000115
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000116 while ((c = getc(istream)) != EOF) {
Denys Vlasenko28055022010-01-04 20:49:58 +0100117 /* We grow line_out in chunks of 0x1000 bytes */
118 if ((offset_out & 0xfff) == 0) {
119 line_out = xrealloc(line_out, offset_out + 0x1000);
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000120 }
Denys Vlasenko28055022010-01-04 20:49:58 +0100121 rescan:
122 line_out[offset_out] = c;
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000123 if (c == '\n') {
Denys Vlasenko28055022010-01-04 20:49:58 +0100124 write2stdout(line_out, offset_out + 1);
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000125 column = offset_out = 0;
126 continue;
127 }
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000128 column = adjust_column(column, c);
Denys Vlasenko28055022010-01-04 20:49:58 +0100129 if (column <= width || offset_out == 0) {
130 /* offset_out == 0 case happens
131 * with small width (say, 1) and tabs.
132 * The very first tab already goes to column 8,
133 * but we must not wrap it */
134 offset_out++;
135 continue;
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000136 }
137
Denys Vlasenko28055022010-01-04 20:49:58 +0100138 /* This character would make the line too long.
139 * Print the line plus a newline, and make this character
140 * start the next line */
141 if (option_mask32 & FLAG_BREAK_SPACES) {
142 unsigned i;
143 unsigned logical_end;
144
145 /* Look for the last blank. */
146 for (logical_end = offset_out - 1; (int)logical_end >= 0; logical_end--) {
147 if (!isblank(line_out[logical_end]))
148 continue;
149
150 /* Found a space or tab.
151 * Output up to and including it, and start a new line */
152 logical_end++;
153 /*line_out[logical_end] = '\n'; - NO! this nukes one buffered character */
154 write2stdout(line_out, logical_end);
155 putchar('\n');
156 /* Move the remainder to the beginning of the next line.
157 * The areas being copied here might overlap. */
158 memmove(line_out, line_out + logical_end, offset_out - logical_end);
159 offset_out -= logical_end;
160 for (column = i = 0; i < offset_out; i++) {
161 column = adjust_column(column, line_out[i]);
162 }
163 goto rescan;
164 }
165 /* No blank found, wrap will split the overlong word */
166 }
167 /* Output what we accumulated up to now, and start a new line */
168 line_out[offset_out] = '\n';
169 write2stdout(line_out, offset_out + 1);
170 column = offset_out = 0;
171 goto rescan;
172 } /* while (not EOF) */
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000173
174 if (offset_out) {
Denys Vlasenko28055022010-01-04 20:49:58 +0100175 write2stdout(line_out, offset_out);
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000176 }
177
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000178 if (fclose_if_not_stdin(istream)) {
Denys Vlasenko28055022010-01-04 20:49:58 +0100179 bb_simple_perror_msg(*argv);
180 exitcode = EXIT_FAILURE;
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000181 }
182 } while (*++argv);
183
Denys Vlasenko28055022010-01-04 20:49:58 +0100184 fflush_stdout_and_exit(exitcode);
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000185}