"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 | |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 10 | Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 11 | */ |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 12 | |
| 13 | //usage:#define fold_trivial_usage |
| 14 | //usage: "[-bs] [-w WIDTH] [FILE]..." |
| 15 | //usage:#define fold_full_usage "\n\n" |
| 16 | //usage: "Wrap input lines in each FILE (or stdin), writing to stdout\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 17 | //usage: "\n -b Count bytes rather than columns" |
| 18 | //usage: "\n -s Break at spaces" |
| 19 | //usage: "\n -w Use WIDTH columns instead of 80" |
| 20 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 21 | #include "libbb.h" |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 22 | #include "unicode.h" |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 23 | |
Dan Fandrich | 2d1a78b | 2010-09-30 14:31:12 -0700 | [diff] [blame] | 24 | /* This is a NOEXEC applet. Be very careful! */ |
| 25 | |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 26 | /* Must match getopt32 call */ |
| 27 | #define FLAG_COUNT_BYTES 1 |
| 28 | #define FLAG_BREAK_SPACES 2 |
| 29 | #define FLAG_WIDTH 4 |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 30 | |
| 31 | /* Assuming the current column is COLUMN, return the column that |
| 32 | printing C will move the cursor to. |
| 33 | The first column is 0. */ |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 34 | static int adjust_column(unsigned column, char c) |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 35 | { |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 36 | if (option_mask32 & FLAG_COUNT_BYTES) |
| 37 | return ++column; |
| 38 | |
| 39 | if (c == '\t') |
| 40 | return column + 8 - column % 8; |
| 41 | |
| 42 | if (c == '\b') { |
| 43 | if ((int)--column < 0) |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 44 | column = 0; |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 45 | } |
| 46 | else if (c == '\r') |
| 47 | column = 0; |
| 48 | else { /* just a printable char */ |
| 49 | if (unicode_status != UNICODE_ON /* every byte is a new char */ |
| 50 | || (c & 0xc0) != 0x80 /* it isn't a 2nd+ byte of a Unicode char */ |
| 51 | ) { |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 52 | column++; |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 53 | } |
| 54 | } |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 55 | return column; |
| 56 | } |
| 57 | |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 58 | /* Note that this function can write NULs, unlike fputs etc. */ |
| 59 | static void write2stdout(const void *buf, unsigned size) |
| 60 | { |
| 61 | fwrite(buf, 1, size, stdout); |
| 62 | } |
| 63 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 64 | int fold_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | ded688c | 2009-11-04 15:31:19 +0100 | [diff] [blame] | 65 | int fold_main(int argc UNUSED_PARAM, char **argv) |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 66 | { |
Denis Vlasenko | 931de89 | 2007-06-21 12:43:45 +0000 | [diff] [blame] | 67 | char *line_out = NULL; |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 68 | const char *w_opt = "80"; |
| 69 | unsigned width; |
| 70 | smallint exitcode = EXIT_SUCCESS; |
| 71 | |
| 72 | init_unicode(); |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 73 | |
Denis Vlasenko | 0849207 | 2006-12-22 13:56:36 +0000 | [diff] [blame] | 74 | if (ENABLE_INCLUDE_SUSv2) { |
Rob Landley | f8fd4db | 2006-01-30 01:30:39 +0000 | [diff] [blame] | 75 | /* Turn any numeric options into -w options. */ |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 76 | int i; |
Denys Vlasenko | ded688c | 2009-11-04 15:31:19 +0100 | [diff] [blame] | 77 | for (i = 1; argv[i]; i++) { |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 78 | const char *a = argv[i]; |
| 79 | if (*a == '-') { |
| 80 | a++; |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 81 | if (*a == '-' && !a[1]) /* "--" */ |
Rob Landley | f8fd4db | 2006-01-30 01:30:39 +0000 | [diff] [blame] | 82 | break; |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 83 | if (isdigit(*a)) |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 84 | argv[i] = xasprintf("-w%s", a); |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 89 | getopt32(argv, "bsw:", &w_opt); |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 90 | width = xatou_range(w_opt, 1, 10000); |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 91 | |
| 92 | argv += optind; |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 93 | if (!*argv) |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 94 | *--argv = (char*)"-"; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 95 | |
| 96 | do { |
Denis Vlasenko | ddec5af | 2006-10-26 23:25:17 +0000 | [diff] [blame] | 97 | FILE *istream = fopen_or_warn_stdin(*argv); |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 98 | int c; |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 99 | unsigned column = 0; /* Screen column where next char will go */ |
| 100 | unsigned offset_out = 0; /* Index in 'line_out' for next char */ |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 101 | |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 102 | if (istream == NULL) { |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 103 | exitcode = EXIT_FAILURE; |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 104 | continue; |
| 105 | } |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 106 | |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 107 | while ((c = getc(istream)) != EOF) { |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 108 | /* We grow line_out in chunks of 0x1000 bytes */ |
| 109 | if ((offset_out & 0xfff) == 0) { |
| 110 | line_out = xrealloc(line_out, offset_out + 0x1000); |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 111 | } |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 112 | rescan: |
| 113 | line_out[offset_out] = c; |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 114 | if (c == '\n') { |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 115 | write2stdout(line_out, offset_out + 1); |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 116 | column = offset_out = 0; |
| 117 | continue; |
| 118 | } |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 119 | column = adjust_column(column, c); |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 120 | if (column <= width || offset_out == 0) { |
| 121 | /* offset_out == 0 case happens |
| 122 | * with small width (say, 1) and tabs. |
| 123 | * The very first tab already goes to column 8, |
| 124 | * but we must not wrap it */ |
| 125 | offset_out++; |
| 126 | continue; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 129 | /* This character would make the line too long. |
| 130 | * Print the line plus a newline, and make this character |
| 131 | * start the next line */ |
| 132 | if (option_mask32 & FLAG_BREAK_SPACES) { |
| 133 | unsigned i; |
| 134 | unsigned logical_end; |
| 135 | |
| 136 | /* Look for the last blank. */ |
| 137 | for (logical_end = offset_out - 1; (int)logical_end >= 0; logical_end--) { |
| 138 | if (!isblank(line_out[logical_end])) |
| 139 | continue; |
| 140 | |
| 141 | /* Found a space or tab. |
| 142 | * Output up to and including it, and start a new line */ |
| 143 | logical_end++; |
| 144 | /*line_out[logical_end] = '\n'; - NO! this nukes one buffered character */ |
| 145 | write2stdout(line_out, logical_end); |
| 146 | putchar('\n'); |
| 147 | /* Move the remainder to the beginning of the next line. |
| 148 | * The areas being copied here might overlap. */ |
| 149 | memmove(line_out, line_out + logical_end, offset_out - logical_end); |
| 150 | offset_out -= logical_end; |
| 151 | for (column = i = 0; i < offset_out; i++) { |
| 152 | column = adjust_column(column, line_out[i]); |
| 153 | } |
| 154 | goto rescan; |
| 155 | } |
| 156 | /* No blank found, wrap will split the overlong word */ |
| 157 | } |
| 158 | /* Output what we accumulated up to now, and start a new line */ |
| 159 | line_out[offset_out] = '\n'; |
| 160 | write2stdout(line_out, offset_out + 1); |
| 161 | column = offset_out = 0; |
| 162 | goto rescan; |
| 163 | } /* while (not EOF) */ |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 164 | |
| 165 | if (offset_out) { |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 166 | write2stdout(line_out, offset_out); |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 169 | if (fclose_if_not_stdin(istream)) { |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 170 | bb_simple_perror_msg(*argv); |
| 171 | exitcode = EXIT_FAILURE; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 172 | } |
| 173 | } while (*++argv); |
| 174 | |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 175 | fflush_stdout_and_exit(exitcode); |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 176 | } |