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