blob: cbea31fad76d299d59fc89a4c300872b5962c674 [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
Rob Landley746cfc82005-12-02 17:55:45 +000010 Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
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
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000015/* Must match getopt32 call */
16#define FLAG_COUNT_BYTES 1
17#define FLAG_BREAK_SPACES 2
18#define FLAG_WIDTH 4
Glenn L McGrathf01b8052003-04-27 06:02:14 +000019
20/* Assuming the current column is COLUMN, return the column that
21 printing C will move the cursor to.
22 The first column is 0. */
Denys Vlasenko28055022010-01-04 20:49:58 +010023static int adjust_column(unsigned column, char c)
Glenn L McGrathf01b8052003-04-27 06:02:14 +000024{
Denys Vlasenko28055022010-01-04 20:49:58 +010025 if (option_mask32 & FLAG_COUNT_BYTES)
26 return ++column;
27
28 if (c == '\t')
29 return column + 8 - column % 8;
30
31 if (c == '\b') {
32 if ((int)--column < 0)
Glenn L McGrathf01b8052003-04-27 06:02:14 +000033 column = 0;
Denys Vlasenko28055022010-01-04 20:49:58 +010034 }
35 else if (c == '\r')
36 column = 0;
37 else { /* just a printable char */
38 if (unicode_status != UNICODE_ON /* every byte is a new char */
39 || (c & 0xc0) != 0x80 /* it isn't a 2nd+ byte of a Unicode char */
40 ) {
Glenn L McGrathf01b8052003-04-27 06:02:14 +000041 column++;
Denys Vlasenko28055022010-01-04 20:49:58 +010042 }
43 }
Glenn L McGrathf01b8052003-04-27 06:02:14 +000044 return column;
45}
46
Denys Vlasenko28055022010-01-04 20:49:58 +010047/* Note that this function can write NULs, unlike fputs etc. */
48static void write2stdout(const void *buf, unsigned size)
49{
50 fwrite(buf, 1, size, stdout);
51}
52
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000053int fold_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoded688c2009-11-04 15:31:19 +010054int fold_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrathf01b8052003-04-27 06:02:14 +000055{
Denis Vlasenko931de892007-06-21 12:43:45 +000056 char *line_out = NULL;
Denys Vlasenko28055022010-01-04 20:49:58 +010057 const char *w_opt = "80";
58 unsigned width;
59 smallint exitcode = EXIT_SUCCESS;
60
61 init_unicode();
Glenn L McGrathf01b8052003-04-27 06:02:14 +000062
Denis Vlasenko08492072006-12-22 13:56:36 +000063 if (ENABLE_INCLUDE_SUSv2) {
Rob Landleyf8fd4db2006-01-30 01:30:39 +000064 /* Turn any numeric options into -w options. */
Denys Vlasenko28055022010-01-04 20:49:58 +010065 int i;
Denys Vlasenkoded688c2009-11-04 15:31:19 +010066 for (i = 1; argv[i]; i++) {
Denys Vlasenko28055022010-01-04 20:49:58 +010067 const char *a = argv[i];
68 if (*a == '-') {
69 a++;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000070 if (*a == '-' && !a[1]) /* "--" */
Rob Landleyf8fd4db2006-01-30 01:30:39 +000071 break;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000072 if (isdigit(*a))
Rob Landleyd921b2e2006-08-03 15:41:12 +000073 argv[i] = xasprintf("-w%s", a);
Glenn L McGrathf01b8052003-04-27 06:02:14 +000074 }
75 }
76 }
77
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000078 getopt32(argv, "bsw:", &w_opt);
Denys Vlasenko28055022010-01-04 20:49:58 +010079 width = xatou_range(w_opt, 1, 10000);
Glenn L McGrathf01b8052003-04-27 06:02:14 +000080
81 argv += optind;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000082 if (!*argv)
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000083 *--argv = (char*)"-";
Glenn L McGrathf01b8052003-04-27 06:02:14 +000084
85 do {
Denis Vlasenkoddec5af2006-10-26 23:25:17 +000086 FILE *istream = fopen_or_warn_stdin(*argv);
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +000087 int c;
Denys Vlasenko28055022010-01-04 20:49:58 +010088 unsigned column = 0; /* Screen column where next char will go */
89 unsigned offset_out = 0; /* Index in 'line_out' for next char */
Glenn L McGrathf01b8052003-04-27 06:02:14 +000090
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +000091 if (istream == NULL) {
Denys Vlasenko28055022010-01-04 20:49:58 +010092 exitcode = EXIT_FAILURE;
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +000093 continue;
94 }
Glenn L McGrathf01b8052003-04-27 06:02:14 +000095
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +000096 while ((c = getc(istream)) != EOF) {
Denys Vlasenko28055022010-01-04 20:49:58 +010097 /* We grow line_out in chunks of 0x1000 bytes */
98 if ((offset_out & 0xfff) == 0) {
99 line_out = xrealloc(line_out, offset_out + 0x1000);
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000100 }
Denys Vlasenko28055022010-01-04 20:49:58 +0100101 rescan:
102 line_out[offset_out] = c;
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000103 if (c == '\n') {
Denys Vlasenko28055022010-01-04 20:49:58 +0100104 write2stdout(line_out, offset_out + 1);
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000105 column = offset_out = 0;
106 continue;
107 }
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000108 column = adjust_column(column, c);
Denys Vlasenko28055022010-01-04 20:49:58 +0100109 if (column <= width || offset_out == 0) {
110 /* offset_out == 0 case happens
111 * with small width (say, 1) and tabs.
112 * The very first tab already goes to column 8,
113 * but we must not wrap it */
114 offset_out++;
115 continue;
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000116 }
117
Denys Vlasenko28055022010-01-04 20:49:58 +0100118 /* This character would make the line too long.
119 * Print the line plus a newline, and make this character
120 * start the next line */
121 if (option_mask32 & FLAG_BREAK_SPACES) {
122 unsigned i;
123 unsigned logical_end;
124
125 /* Look for the last blank. */
126 for (logical_end = offset_out - 1; (int)logical_end >= 0; logical_end--) {
127 if (!isblank(line_out[logical_end]))
128 continue;
129
130 /* Found a space or tab.
131 * Output up to and including it, and start a new line */
132 logical_end++;
133 /*line_out[logical_end] = '\n'; - NO! this nukes one buffered character */
134 write2stdout(line_out, logical_end);
135 putchar('\n');
136 /* Move the remainder to the beginning of the next line.
137 * The areas being copied here might overlap. */
138 memmove(line_out, line_out + logical_end, offset_out - logical_end);
139 offset_out -= logical_end;
140 for (column = i = 0; i < offset_out; i++) {
141 column = adjust_column(column, line_out[i]);
142 }
143 goto rescan;
144 }
145 /* No blank found, wrap will split the overlong word */
146 }
147 /* Output what we accumulated up to now, and start a new line */
148 line_out[offset_out] = '\n';
149 write2stdout(line_out, offset_out + 1);
150 column = offset_out = 0;
151 goto rescan;
152 } /* while (not EOF) */
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000153
154 if (offset_out) {
Denys Vlasenko28055022010-01-04 20:49:58 +0100155 write2stdout(line_out, offset_out);
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000156 }
157
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000158 if (fclose_if_not_stdin(istream)) {
Denys Vlasenko28055022010-01-04 20:49:58 +0100159 bb_simple_perror_msg(*argv);
160 exitcode = EXIT_FAILURE;
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000161 }
162 } while (*++argv);
163
Denys Vlasenko28055022010-01-04 20:49:58 +0100164 fflush_stdout_and_exit(exitcode);
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000165}