blob: ed484edf0bf63d408872c23023f553813418a534 [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
Rob Landley746cfc82005-12-02 17:55:45 +000015static unsigned long flags;
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. */
23
24static int adjust_column(int column, char c)
25{
Rob Landley746cfc82005-12-02 17:55:45 +000026 if (!(flags & FLAG_COUNT_BYTES)) {
Glenn L McGrathf01b8052003-04-27 06:02:14 +000027 if (c == '\b') {
28 if (column > 0)
29 column--;
30 } else if (c == '\r')
31 column = 0;
32 else if (c == '\t')
33 column = column + 8 - column % 8;
34 else /* if (isprint (c)) */
35 column++;
36 } else
37 column++;
38 return column;
39}
40
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000041int fold_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +000042int fold_main(int argc, char **argv)
Glenn L McGrathf01b8052003-04-27 06:02:14 +000043{
Denis Vlasenko931de892007-06-21 12:43:45 +000044 char *line_out = NULL;
45 int allocated_out = 0;
Paul Fox377bdaf2005-12-08 18:48:20 +000046 char *w_opt;
Glenn L McGrathf01b8052003-04-27 06:02:14 +000047 int width = 80;
48 int i;
Glenn L McGrathf01b8052003-04-27 06:02:14 +000049 int errs = 0;
50
Denis Vlasenko08492072006-12-22 13:56:36 +000051 if (ENABLE_INCLUDE_SUSv2) {
Rob Landleyf8fd4db2006-01-30 01:30:39 +000052 /* Turn any numeric options into -w options. */
53 for (i = 1; i < argc; i++) {
54 char const *a = argv[i];
Glenn L McGrathf01b8052003-04-27 06:02:14 +000055
"Vladimir N. Oleynik"b1fd52e2006-01-30 10:49:14 +000056 if (*a++ == '-') {
57 if (*a == '-' && !a[1])
Rob Landleyf8fd4db2006-01-30 01:30:39 +000058 break;
"Vladimir N. Oleynik"b1fd52e2006-01-30 10:49:14 +000059 if (isdigit(*a)) {
Rob Landleyd921b2e2006-08-03 15:41:12 +000060 argv[i] = xasprintf("-w%s", a);
Rob Landleyf8fd4db2006-01-30 01:30:39 +000061 }
Glenn L McGrathf01b8052003-04-27 06:02:14 +000062 }
63 }
64 }
65
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000066 flags = getopt32(argv, "bsw:", &w_opt);
Bernhard Reutner-Fischerd77b7812005-12-10 20:13:54 +000067 if (flags & FLAG_WIDTH)
Denis Vlasenko13858992006-10-08 12:49:22 +000068 width = xatoul_range(w_opt, 1, 10000);
Glenn L McGrathf01b8052003-04-27 06:02:14 +000069
70 argv += optind;
71 if (!*argv) {
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000072 *--argv = (char*)"-";
Glenn L McGrathf01b8052003-04-27 06:02:14 +000073 }
74
75 do {
Denis Vlasenkoddec5af2006-10-26 23:25:17 +000076 FILE *istream = fopen_or_warn_stdin(*argv);
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +000077 int c;
78 int column = 0; /* Screen column where next char will go. */
79 int offset_out = 0; /* Index in `line_out' for next char. */
Glenn L McGrathf01b8052003-04-27 06:02:14 +000080
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +000081 if (istream == NULL) {
82 errs |= EXIT_FAILURE;
83 continue;
84 }
Glenn L McGrathf01b8052003-04-27 06:02:14 +000085
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +000086 while ((c = getc(istream)) != EOF) {
87 if (offset_out + 1 >= allocated_out) {
88 allocated_out += 1024;
89 line_out = xrealloc(line_out, allocated_out);
90 }
91
92 if (c == '\n') {
93 line_out[offset_out++] = c;
94 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
95 column = offset_out = 0;
96 continue;
97 }
Denis Vlasenko931de892007-06-21 12:43:45 +000098 rescan:
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +000099 column = adjust_column(column, c);
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000100
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000101 if (column > width) {
102 /* This character would make the line too long.
103 Print the line plus a newline, and make this character
104 start the next line. */
105 if (flags & FLAG_BREAK_SPACES) {
106 /* Look for the last blank. */
107 int logical_end;
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000108
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000109 for (logical_end = offset_out - 1; logical_end >= 0; logical_end--) {
110 if (isblank(line_out[logical_end])) {
111 break;
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000112 }
113 }
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000114 if (logical_end >= 0) {
115 /* Found a blank. Don't output the part after it. */
116 logical_end++;
117 fwrite(line_out, sizeof(char), (size_t) logical_end, stdout);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000118 bb_putchar('\n');
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000119 /* Move the remainder to the beginning of the next line.
120 The areas being copied here might overlap. */
121 memmove(line_out, line_out + logical_end, offset_out - logical_end);
122 offset_out -= logical_end;
123 for (column = i = 0; i < offset_out; i++) {
124 column = adjust_column(column, line_out[i]);
125 }
126 goto rescan;
127 }
128 } else {
129 if (offset_out == 0) {
130 line_out[offset_out++] = c;
131 continue;
132 }
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000133 }
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000134 line_out[offset_out++] = '\n';
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000135 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000136 column = offset_out = 0;
137 goto rescan;
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000138 }
139
Bernhard Reutner-Fischer4bf31272006-01-30 17:41:06 +0000140 line_out[offset_out++] = c;
141 }
142
143 if (offset_out) {
144 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
145 }
146
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000147 if (ferror(istream) || fclose_if_not_stdin(istream)) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000148 bb_simple_perror_msg(*argv); /* Avoid multibyte problems. */
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000149 errs |= EXIT_FAILURE;
150 }
151 } while (*++argv);
152
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000153 fflush_stdout_and_exit(errs);
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000154}