"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 |
Glenn L McGrath | c6992fe | 2004-04-25 05:11:19 +0000 | [diff] [blame] | 8 | Copyright (C) 2003 Glenn McGrath <bug1@iinet.net.au> |
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 | |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 13 | #include "busybox.h" |
| 14 | |
Rob Landley | 746cfc8 | 2005-12-02 17:55:45 +0000 | [diff] [blame] | 15 | static unsigned long flags; |
| 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. */ |
| 23 | |
| 24 | static int adjust_column(int column, char c) |
| 25 | { |
Rob Landley | 746cfc8 | 2005-12-02 17:55:45 +0000 | [diff] [blame] | 26 | if (!(flags & FLAG_COUNT_BYTES)) { |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 27 | 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 Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 41 | int fold_main(int argc, char **argv); |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 42 | int fold_main(int argc, char **argv) |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 43 | { |
Paul Fox | 377bdaf | 2005-12-08 18:48:20 +0000 | [diff] [blame] | 44 | char *w_opt; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 45 | int width = 80; |
| 46 | int i; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 47 | int errs = 0; |
| 48 | |
Denis Vlasenko | 0849207 | 2006-12-22 13:56:36 +0000 | [diff] [blame] | 49 | if (ENABLE_INCLUDE_SUSv2) { |
Rob Landley | f8fd4db | 2006-01-30 01:30:39 +0000 | [diff] [blame] | 50 | /* Turn any numeric options into -w options. */ |
| 51 | for (i = 1; i < argc; i++) { |
| 52 | char const *a = argv[i]; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 53 | |
"Vladimir N. Oleynik" | b1fd52e | 2006-01-30 10:49:14 +0000 | [diff] [blame] | 54 | if (*a++ == '-') { |
| 55 | if (*a == '-' && !a[1]) |
Rob Landley | f8fd4db | 2006-01-30 01:30:39 +0000 | [diff] [blame] | 56 | break; |
"Vladimir N. Oleynik" | b1fd52e | 2006-01-30 10:49:14 +0000 | [diff] [blame] | 57 | if (isdigit(*a)) { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 58 | argv[i] = xasprintf("-w%s", a); |
Rob Landley | f8fd4db | 2006-01-30 01:30:39 +0000 | [diff] [blame] | 59 | } |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 64 | flags = getopt32(argc, argv, "bsw:", &w_opt); |
Bernhard Reutner-Fischer | d77b781 | 2005-12-10 20:13:54 +0000 | [diff] [blame] | 65 | if (flags & 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; |
| 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 | |
| 73 | do { |
Denis Vlasenko | ddec5af | 2006-10-26 23:25:17 +0000 | [diff] [blame] | 74 | FILE *istream = fopen_or_warn_stdin(*argv); |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 75 | int c; |
| 76 | int column = 0; /* Screen column where next char will go. */ |
| 77 | int offset_out = 0; /* Index in `line_out' for next char. */ |
| 78 | static char *line_out = NULL; |
| 79 | static int allocated_out = 0; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 80 | |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 81 | if (istream == NULL) { |
| 82 | errs |= EXIT_FAILURE; |
| 83 | continue; |
| 84 | } |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 85 | |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 86 | 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 | } |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 98 | |
| 99 | rescan: |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 100 | column = adjust_column(column, c); |
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 (column > width) { |
| 103 | /* This character would make the line too long. |
| 104 | Print the line plus a newline, and make this character |
| 105 | start the next line. */ |
| 106 | if (flags & FLAG_BREAK_SPACES) { |
| 107 | /* Look for the last blank. */ |
| 108 | int logical_end; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 109 | |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 110 | for (logical_end = offset_out - 1; logical_end >= 0; logical_end--) { |
| 111 | if (isblank(line_out[logical_end])) { |
| 112 | break; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 115 | if (logical_end >= 0) { |
| 116 | /* Found a blank. Don't output the part after it. */ |
| 117 | logical_end++; |
| 118 | fwrite(line_out, sizeof(char), (size_t) logical_end, stdout); |
| 119 | putchar('\n'); |
| 120 | /* Move the remainder to the beginning of the next line. |
| 121 | The areas being copied here might overlap. */ |
| 122 | memmove(line_out, line_out + logical_end, offset_out - logical_end); |
| 123 | offset_out -= logical_end; |
| 124 | for (column = i = 0; i < offset_out; i++) { |
| 125 | column = adjust_column(column, line_out[i]); |
| 126 | } |
| 127 | goto rescan; |
| 128 | } |
| 129 | } else { |
| 130 | if (offset_out == 0) { |
| 131 | line_out[offset_out++] = c; |
| 132 | continue; |
| 133 | } |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 134 | } |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 135 | line_out[offset_out++] = '\n'; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 136 | fwrite(line_out, sizeof(char), (size_t) offset_out, stdout); |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 137 | column = offset_out = 0; |
| 138 | goto rescan; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 141 | line_out[offset_out++] = c; |
| 142 | } |
| 143 | |
| 144 | if (offset_out) { |
| 145 | fwrite(line_out, sizeof(char), (size_t) offset_out, stdout); |
| 146 | } |
| 147 | |
Denis Vlasenko | ddec5af | 2006-10-26 23:25:17 +0000 | [diff] [blame] | 148 | if (ferror(istream) || fclose_if_not_stdin(istream)) { |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 149 | bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */ |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 150 | errs |= EXIT_FAILURE; |
| 151 | } |
| 152 | } while (*++argv); |
| 153 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 154 | fflush_stdout_and_exit(errs); |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 155 | } |