blob: e2a30d5d4e90f1633a79dc840cb4f6de6d1d8714 [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*/
12
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000013#include "libbb.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. */
Glenn L McGrathf01b8052003-04-27 06:02:14 +000023static int adjust_column(int column, char c)
24{
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000025 if (!(option_mask32 & FLAG_COUNT_BYTES)) {
Glenn L McGrathf01b8052003-04-27 06:02:14 +000026 if (c == '\b') {
27 if (column > 0)
28 column--;
29 } else if (c == '\r')
30 column = 0;
31 else if (c == '\t')
32 column = column + 8 - column % 8;
33 else /* if (isprint (c)) */
34 column++;
35 } else
36 column++;
37 return column;
38}
39
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000040int fold_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +000041int fold_main(int argc, char **argv)
Glenn L McGrathf01b8052003-04-27 06:02:14 +000042{
Denis Vlasenko931de892007-06-21 12:43:45 +000043 char *line_out = NULL;
44 int allocated_out = 0;
Paul Fox377bdaf2005-12-08 18:48:20 +000045 char *w_opt;
Glenn L McGrathf01b8052003-04-27 06:02:14 +000046 int width = 80;
47 int i;
Glenn L McGrathf01b8052003-04-27 06:02:14 +000048 int errs = 0;
49
Denis Vlasenko08492072006-12-22 13:56:36 +000050 if (ENABLE_INCLUDE_SUSv2) {
Rob Landleyf8fd4db2006-01-30 01:30:39 +000051 /* Turn any numeric options into -w options. */
52 for (i = 1; i < argc; i++) {
53 char const *a = argv[i];
Glenn L McGrathf01b8052003-04-27 06:02:14 +000054
"Vladimir N. Oleynik"b1fd52e2006-01-30 10:49:14 +000055 if (*a++ == '-') {
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000056 if (*a == '-' && !a[1]) /* "--" */
Rob Landleyf8fd4db2006-01-30 01:30:39 +000057 break;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000058 if (isdigit(*a))
Rob Landleyd921b2e2006-08-03 15:41:12 +000059 argv[i] = xasprintf("-w%s", a);
Glenn L McGrathf01b8052003-04-27 06:02:14 +000060 }
61 }
62 }
63
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000064 getopt32(argv, "bsw:", &w_opt);
65 if (option_mask32 & FLAG_WIDTH)
Denis Vlasenko13858992006-10-08 12:49:22 +000066 width = xatoul_range(w_opt, 1, 10000);
Glenn L McGrathf01b8052003-04-27 06:02:14 +000067
68 argv += optind;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000069 if (!*argv)
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000070 *--argv = (char*)"-";
Glenn L McGrathf01b8052003-04-27 06:02:14 +000071
72 do {
Denis Vlasenkoddec5af2006-10-26 23:25:17 +000073 FILE *istream = fopen_or_warn_stdin(*argv);
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +000074 int c;
75 int column = 0; /* Screen column where next char will go. */
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000076 int offset_out = 0; /* Index in 'line_out' for next char. */
Glenn L McGrathf01b8052003-04-27 06:02:14 +000077
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +000078 if (istream == NULL) {
79 errs |= EXIT_FAILURE;
80 continue;
81 }
Glenn L McGrathf01b8052003-04-27 06:02:14 +000082
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +000083 while ((c = getc(istream)) != EOF) {
84 if (offset_out + 1 >= allocated_out) {
85 allocated_out += 1024;
86 line_out = xrealloc(line_out, allocated_out);
87 }
88
89 if (c == '\n') {
90 line_out[offset_out++] = c;
91 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
92 column = offset_out = 0;
93 continue;
94 }
Denis Vlasenko931de892007-06-21 12:43:45 +000095 rescan:
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +000096 column = adjust_column(column, c);
Glenn L McGrathf01b8052003-04-27 06:02:14 +000097
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +000098 if (column > width) {
99 /* This character would make the line too long.
100 Print the line plus a newline, and make this character
101 start the next line. */
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000102 if (option_mask32 & FLAG_BREAK_SPACES) {
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000103 /* Look for the last blank. */
104 int logical_end;
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000105
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000106 for (logical_end = offset_out - 1; logical_end >= 0; logical_end--) {
107 if (isblank(line_out[logical_end])) {
108 break;
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000109 }
110 }
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000111 if (logical_end >= 0) {
112 /* Found a blank. Don't output the part after it. */
113 logical_end++;
114 fwrite(line_out, sizeof(char), (size_t) logical_end, stdout);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000115 bb_putchar('\n');
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000116 /* Move the remainder to the beginning of the next line.
117 The areas being copied here might overlap. */
118 memmove(line_out, line_out + logical_end, offset_out - logical_end);
119 offset_out -= logical_end;
120 for (column = i = 0; i < offset_out; i++) {
121 column = adjust_column(column, line_out[i]);
122 }
123 goto rescan;
124 }
125 } else {
126 if (offset_out == 0) {
127 line_out[offset_out++] = c;
128 continue;
129 }
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000130 }
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000131 line_out[offset_out++] = '\n';
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000132 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000133 column = offset_out = 0;
134 goto rescan;
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000135 }
136
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000137 line_out[offset_out++] = c;
138 }
139
140 if (offset_out) {
141 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
142 }
143
Denis Vlasenko62a90cd2008-03-17 09:07:36 +0000144 if (fclose_if_not_stdin(istream)) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000145 bb_simple_perror_msg(*argv); /* Avoid multibyte problems. */
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000146 errs |= EXIT_FAILURE;
147 }
148 } while (*++argv);
149
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000150 fflush_stdout_and_exit(errs);
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000151}