blob: 1e26dde0c52f7697e9a884a10f8fa73ee4fd000f [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Denys Vlasenkoa02a4e92017-10-05 15:19:25 +02002/*
3 * fold -- wrap each input line to fit in specified width.
Denys Vlasenkoebe6d9d2017-10-05 14:40:24 +02004 *
5 * Written by David MacKenzie, djm@gnu.ai.mit.edu.
6 * Copyright (C) 91, 1995-2002 Free Software Foundation, Inc.
7 *
8 * Modified for busybox based on coreutils v 5.0
9 * Copyright (C) 2003 Glenn McGrath
10 *
11 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
12 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010013//config:config FOLD
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020014//config: bool "fold (4.6 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010015//config: default y
16//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020017//config: Wrap text to fit a specific width.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010018
19//applet:IF_FOLD(APPLET_NOEXEC(fold, fold, BB_DIR_USR_BIN, BB_SUID_DROP, fold))
20
21//kbuild:lib-$(CONFIG_FOLD) += fold.o
Pere Orga34425382011-03-31 14:43:25 +020022
23//usage:#define fold_trivial_usage
24//usage: "[-bs] [-w WIDTH] [FILE]..."
25//usage:#define fold_full_usage "\n\n"
26//usage: "Wrap input lines in each FILE (or stdin), writing to stdout\n"
Pere Orga34425382011-03-31 14:43:25 +020027//usage: "\n -b Count bytes rather than columns"
28//usage: "\n -s Break at spaces"
29//usage: "\n -w Use WIDTH columns instead of 80"
30
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000031#include "libbb.h"
Denys Vlasenko28055022010-01-04 20:49:58 +010032#include "unicode.h"
Glenn L McGrathf01b8052003-04-27 06:02:14 +000033
Dan Fandrich2d1a78b2010-09-30 14:31:12 -070034/* This is a NOEXEC applet. Be very careful! */
35
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000036/* Must match getopt32 call */
37#define FLAG_COUNT_BYTES 1
38#define FLAG_BREAK_SPACES 2
39#define FLAG_WIDTH 4
Glenn L McGrathf01b8052003-04-27 06:02:14 +000040
41/* Assuming the current column is COLUMN, return the column that
42 printing C will move the cursor to.
43 The first column is 0. */
Denys Vlasenko28055022010-01-04 20:49:58 +010044static int adjust_column(unsigned column, char c)
Glenn L McGrathf01b8052003-04-27 06:02:14 +000045{
Denys Vlasenko28055022010-01-04 20:49:58 +010046 if (option_mask32 & FLAG_COUNT_BYTES)
47 return ++column;
48
49 if (c == '\t')
50 return column + 8 - column % 8;
51
52 if (c == '\b') {
53 if ((int)--column < 0)
Glenn L McGrathf01b8052003-04-27 06:02:14 +000054 column = 0;
Denys Vlasenko28055022010-01-04 20:49:58 +010055 }
56 else if (c == '\r')
57 column = 0;
58 else { /* just a printable char */
59 if (unicode_status != UNICODE_ON /* every byte is a new char */
60 || (c & 0xc0) != 0x80 /* it isn't a 2nd+ byte of a Unicode char */
61 ) {
Glenn L McGrathf01b8052003-04-27 06:02:14 +000062 column++;
Denys Vlasenko28055022010-01-04 20:49:58 +010063 }
64 }
Glenn L McGrathf01b8052003-04-27 06:02:14 +000065 return column;
66}
67
Denys Vlasenko28055022010-01-04 20:49:58 +010068/* Note that this function can write NULs, unlike fputs etc. */
69static void write2stdout(const void *buf, unsigned size)
70{
71 fwrite(buf, 1, size, stdout);
72}
73
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000074int fold_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoded688c2009-11-04 15:31:19 +010075int fold_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrathf01b8052003-04-27 06:02:14 +000076{
Denis Vlasenko931de892007-06-21 12:43:45 +000077 char *line_out = NULL;
Denys Vlasenko28055022010-01-04 20:49:58 +010078 const char *w_opt = "80";
79 unsigned width;
80 smallint exitcode = EXIT_SUCCESS;
81
82 init_unicode();
Glenn L McGrathf01b8052003-04-27 06:02:14 +000083
Denis Vlasenko08492072006-12-22 13:56:36 +000084 if (ENABLE_INCLUDE_SUSv2) {
Rob Landleyf8fd4db2006-01-30 01:30:39 +000085 /* Turn any numeric options into -w options. */
Denys Vlasenko28055022010-01-04 20:49:58 +010086 int i;
Denys Vlasenkoded688c2009-11-04 15:31:19 +010087 for (i = 1; argv[i]; i++) {
Denys Vlasenko28055022010-01-04 20:49:58 +010088 const char *a = argv[i];
89 if (*a == '-') {
90 a++;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000091 if (*a == '-' && !a[1]) /* "--" */
Rob Landleyf8fd4db2006-01-30 01:30:39 +000092 break;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000093 if (isdigit(*a))
Rob Landleyd921b2e2006-08-03 15:41:12 +000094 argv[i] = xasprintf("-w%s", a);
Glenn L McGrathf01b8052003-04-27 06:02:14 +000095 }
96 }
97 }
98
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000099 getopt32(argv, "bsw:", &w_opt);
Denys Vlasenko28055022010-01-04 20:49:58 +0100100 width = xatou_range(w_opt, 1, 10000);
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000101
102 argv += optind;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000103 if (!*argv)
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000104 *--argv = (char*)"-";
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000105
106 do {
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000107 FILE *istream = fopen_or_warn_stdin(*argv);
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000108 int c;
Denys Vlasenko28055022010-01-04 20:49:58 +0100109 unsigned column = 0; /* Screen column where next char will go */
110 unsigned offset_out = 0; /* Index in 'line_out' for next char */
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000111
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000112 if (istream == NULL) {
Denys Vlasenko28055022010-01-04 20:49:58 +0100113 exitcode = EXIT_FAILURE;
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000114 continue;
115 }
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000116
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000117 while ((c = getc(istream)) != EOF) {
Denys Vlasenko28055022010-01-04 20:49:58 +0100118 /* We grow line_out in chunks of 0x1000 bytes */
119 if ((offset_out & 0xfff) == 0) {
120 line_out = xrealloc(line_out, offset_out + 0x1000);
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000121 }
Denys Vlasenko28055022010-01-04 20:49:58 +0100122 rescan:
123 line_out[offset_out] = c;
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000124 if (c == '\n') {
Denys Vlasenko28055022010-01-04 20:49:58 +0100125 write2stdout(line_out, offset_out + 1);
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000126 column = offset_out = 0;
127 continue;
128 }
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000129 column = adjust_column(column, c);
Denys Vlasenko28055022010-01-04 20:49:58 +0100130 if (column <= width || offset_out == 0) {
131 /* offset_out == 0 case happens
132 * with small width (say, 1) and tabs.
133 * The very first tab already goes to column 8,
134 * but we must not wrap it */
135 offset_out++;
136 continue;
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000137 }
138
Denys Vlasenko28055022010-01-04 20:49:58 +0100139 /* This character would make the line too long.
140 * Print the line plus a newline, and make this character
141 * start the next line */
142 if (option_mask32 & FLAG_BREAK_SPACES) {
143 unsigned i;
144 unsigned logical_end;
145
146 /* Look for the last blank. */
147 for (logical_end = offset_out - 1; (int)logical_end >= 0; logical_end--) {
148 if (!isblank(line_out[logical_end]))
149 continue;
150
151 /* Found a space or tab.
152 * Output up to and including it, and start a new line */
153 logical_end++;
154 /*line_out[logical_end] = '\n'; - NO! this nukes one buffered character */
155 write2stdout(line_out, logical_end);
156 putchar('\n');
157 /* Move the remainder to the beginning of the next line.
158 * The areas being copied here might overlap. */
159 memmove(line_out, line_out + logical_end, offset_out - logical_end);
160 offset_out -= logical_end;
161 for (column = i = 0; i < offset_out; i++) {
162 column = adjust_column(column, line_out[i]);
163 }
164 goto rescan;
165 }
166 /* No blank found, wrap will split the overlong word */
167 }
168 /* Output what we accumulated up to now, and start a new line */
169 line_out[offset_out] = '\n';
170 write2stdout(line_out, offset_out + 1);
171 column = offset_out = 0;
172 goto rescan;
173 } /* while (not EOF) */
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000174
175 if (offset_out) {
Denys Vlasenko28055022010-01-04 20:49:58 +0100176 write2stdout(line_out, offset_out);
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000177 }
178
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000179 if (fclose_if_not_stdin(istream)) {
Denys Vlasenko28055022010-01-04 20:49:58 +0100180 bb_simple_perror_msg(*argv);
181 exitcode = EXIT_FAILURE;
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000182 }
183 } while (*++argv);
184
Denys Vlasenko28055022010-01-04 20:49:58 +0100185 fflush_stdout_and_exit(exitcode);
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000186}