"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 2 | /* 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 Vlasenko | 0beaff8 | 2007-09-21 13:16:32 +0000 | [diff] [blame] | 8 | Copyright (C) 2003 Glenn McGrath |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 9 | |
Rob Landley | 746cfc8 | 2005-12-02 17:55:45 +0000 | [diff] [blame] | 10 | Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 13 | #include "libbb.h" |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 14 | |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 15 | /* Must match getopt32 call */ |
| 16 | #define FLAG_COUNT_BYTES 1 |
| 17 | #define FLAG_BREAK_SPACES 2 |
| 18 | #define FLAG_WIDTH 4 |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 19 | |
| 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 McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 23 | static int adjust_column(int column, char c) |
| 24 | { |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 25 | if (!(option_mask32 & FLAG_COUNT_BYTES)) { |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 26 | 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; |
Denys Vlasenko | ded688c | 2009-11-04 15:31:19 +0100 | [diff] [blame] | 33 | else /* if (isprint(c)) */ |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 34 | column++; |
| 35 | } else |
| 36 | column++; |
| 37 | return column; |
| 38 | } |
| 39 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 40 | int fold_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | ded688c | 2009-11-04 15:31:19 +0100 | [diff] [blame] | 41 | int fold_main(int argc UNUSED_PARAM, char **argv) |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 42 | { |
Denis Vlasenko | 931de89 | 2007-06-21 12:43:45 +0000 | [diff] [blame] | 43 | char *line_out = NULL; |
| 44 | int allocated_out = 0; |
Paul Fox | 377bdaf | 2005-12-08 18:48:20 +0000 | [diff] [blame] | 45 | char *w_opt; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 46 | int width = 80; |
| 47 | int i; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 48 | int errs = 0; |
| 49 | |
Denis Vlasenko | 0849207 | 2006-12-22 13:56:36 +0000 | [diff] [blame] | 50 | if (ENABLE_INCLUDE_SUSv2) { |
Rob Landley | f8fd4db | 2006-01-30 01:30:39 +0000 | [diff] [blame] | 51 | /* Turn any numeric options into -w options. */ |
Denys Vlasenko | ded688c | 2009-11-04 15:31:19 +0100 | [diff] [blame] | 52 | for (i = 1; argv[i]; i++) { |
Rob Landley | f8fd4db | 2006-01-30 01:30:39 +0000 | [diff] [blame] | 53 | char const *a = argv[i]; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 54 | |
"Vladimir N. Oleynik" | b1fd52e | 2006-01-30 10:49:14 +0000 | [diff] [blame] | 55 | if (*a++ == '-') { |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 56 | if (*a == '-' && !a[1]) /* "--" */ |
Rob Landley | f8fd4db | 2006-01-30 01:30:39 +0000 | [diff] [blame] | 57 | break; |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 58 | if (isdigit(*a)) |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 59 | argv[i] = xasprintf("-w%s", a); |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 64 | getopt32(argv, "bsw:", &w_opt); |
| 65 | if (option_mask32 & FLAG_WIDTH) |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 66 | width = xatoul_range(w_opt, 1, 10000); |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 67 | |
| 68 | argv += optind; |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 69 | if (!*argv) |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 70 | *--argv = (char*)"-"; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 71 | |
| 72 | do { |
Denis Vlasenko | ddec5af | 2006-10-26 23:25:17 +0000 | [diff] [blame] | 73 | FILE *istream = fopen_or_warn_stdin(*argv); |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 74 | int c; |
| 75 | int column = 0; /* Screen column where next char will go. */ |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 76 | int offset_out = 0; /* Index in 'line_out' for next char. */ |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 77 | |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 78 | if (istream == NULL) { |
| 79 | errs |= EXIT_FAILURE; |
| 80 | continue; |
| 81 | } |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 82 | |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 83 | 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 Vlasenko | 931de89 | 2007-06-21 12:43:45 +0000 | [diff] [blame] | 95 | rescan: |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 96 | column = adjust_column(column, c); |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 97 | |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 98 | 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 Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 102 | if (option_mask32 & FLAG_BREAK_SPACES) { |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 103 | /* Look for the last blank. */ |
| 104 | int logical_end; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 105 | |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 106 | for (logical_end = offset_out - 1; logical_end >= 0; logical_end--) { |
| 107 | if (isblank(line_out[logical_end])) { |
| 108 | break; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 111 | 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 Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 115 | bb_putchar('\n'); |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 116 | /* 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 | } |
Denys Vlasenko | ded688c | 2009-11-04 15:31:19 +0100 | [diff] [blame] | 125 | } |
| 126 | if (offset_out == 0) { |
| 127 | line_out[offset_out++] = c; |
| 128 | continue; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 129 | } |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 130 | line_out[offset_out++] = '\n'; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 131 | fwrite(line_out, sizeof(char), (size_t) offset_out, stdout); |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 132 | column = offset_out = 0; |
| 133 | goto rescan; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 136 | line_out[offset_out++] = c; |
| 137 | } |
| 138 | |
| 139 | if (offset_out) { |
| 140 | fwrite(line_out, sizeof(char), (size_t) offset_out, stdout); |
| 141 | } |
| 142 | |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 143 | if (fclose_if_not_stdin(istream)) { |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 144 | bb_simple_perror_msg(*argv); /* Avoid multibyte problems. */ |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 145 | errs |= EXIT_FAILURE; |
| 146 | } |
| 147 | } while (*++argv); |
| 148 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 149 | fflush_stdout_and_exit(errs); |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 150 | } |