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