Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 1 | /* fold -- wrap each input line to fit in specified width. |
| 2 | |
| 3 | Written by David MacKenzie, djm@gnu.ai.mit.edu. |
| 4 | Copyright (C) 91, 1995-2002 Free Software Foundation, Inc. |
| 5 | |
| 6 | Modified for busybox based on coreutils v 5.0 |
Glenn L McGrath | c6992fe | 2004-04-25 05:11:19 +0000 | [diff] [blame] | 7 | Copyright (C) 2003 Glenn McGrath <bug1@iinet.net.au> |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 8 | |
Rob Landley | 746cfc8 | 2005-12-02 17:55:45 +0000 | [diff] [blame] | 9 | 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] | 10 | */ |
| 11 | |
| 12 | #include <ctype.h> |
| 13 | #include <errno.h> |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 17 | #include <sys/types.h> |
Rob Landley | 746cfc8 | 2005-12-02 17:55:45 +0000 | [diff] [blame] | 18 | #include <unistd.h> |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 19 | #include "busybox.h" |
| 20 | |
Rob Landley | 746cfc8 | 2005-12-02 17:55:45 +0000 | [diff] [blame] | 21 | static unsigned long flags; |
| 22 | #define FLAG_COUNT_BYTES 1 |
| 23 | #define FLAG_BREAK_SPACES 2 |
| 24 | #define FLAG_WIDTH 4 |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 25 | |
| 26 | /* Assuming the current column is COLUMN, return the column that |
| 27 | printing C will move the cursor to. |
| 28 | The first column is 0. */ |
| 29 | |
| 30 | static int adjust_column(int column, char c) |
| 31 | { |
Rob Landley | 746cfc8 | 2005-12-02 17:55:45 +0000 | [diff] [blame] | 32 | if (!(flags & FLAG_COUNT_BYTES)) { |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 33 | if (c == '\b') { |
| 34 | if (column > 0) |
| 35 | column--; |
| 36 | } else if (c == '\r') |
| 37 | column = 0; |
| 38 | else if (c == '\t') |
| 39 | column = column + 8 - column % 8; |
| 40 | else /* if (isprint (c)) */ |
| 41 | column++; |
| 42 | } else |
| 43 | column++; |
| 44 | return column; |
| 45 | } |
| 46 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 47 | int fold_main(int argc, char **argv) |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 48 | { |
Paul Fox | 377bdaf | 2005-12-08 18:48:20 +0000 | [diff] [blame] | 49 | char *w_opt; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 50 | int width = 80; |
| 51 | int i; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 52 | int errs = 0; |
| 53 | |
Rob Landley | f8fd4db | 2006-01-30 01:30:39 +0000 | [diff] [blame] | 54 | if(!ENABLE_DEBUG_YANK_SUSv2) { |
| 55 | /* Turn any numeric options into -w options. */ |
| 56 | for (i = 1; i < argc; i++) { |
| 57 | char const *a = argv[i]; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 58 | |
"Vladimir N. Oleynik" | b1fd52e | 2006-01-30 10:49:14 +0000 | [diff] [blame] | 59 | if (*a++ == '-') { |
| 60 | if (*a == '-' && !a[1]) |
Rob Landley | f8fd4db | 2006-01-30 01:30:39 +0000 | [diff] [blame] | 61 | break; |
"Vladimir N. Oleynik" | b1fd52e | 2006-01-30 10:49:14 +0000 | [diff] [blame] | 62 | if (isdigit(*a)) { |
| 63 | argv[i] = bb_xasprintf("-w%s", a); |
Rob Landley | f8fd4db | 2006-01-30 01:30:39 +0000 | [diff] [blame] | 64 | } |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
Rob Landley | 746cfc8 | 2005-12-02 17:55:45 +0000 | [diff] [blame] | 69 | flags = bb_getopt_ulflags(argc, argv, "bsw:", &w_opt); |
Bernhard Reutner-Fischer | d77b781 | 2005-12-10 20:13:54 +0000 | [diff] [blame] | 70 | if (flags & FLAG_WIDTH) |
Rob Landley | 746cfc8 | 2005-12-02 17:55:45 +0000 | [diff] [blame] | 71 | width = bb_xgetlarg(w_opt, 10, 1, 10000); |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 72 | |
| 73 | argv += optind; |
| 74 | if (!*argv) { |
| 75 | *--argv = "-"; |
| 76 | } |
| 77 | |
| 78 | do { |
| 79 | FILE *istream = bb_wfopen_input(*argv); |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 80 | int c; |
| 81 | int column = 0; /* Screen column where next char will go. */ |
| 82 | int offset_out = 0; /* Index in `line_out' for next char. */ |
| 83 | static char *line_out = NULL; |
| 84 | static int allocated_out = 0; |
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 | if (istream == NULL) { |
| 87 | errs |= EXIT_FAILURE; |
| 88 | continue; |
| 89 | } |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 90 | |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 91 | while ((c = getc(istream)) != EOF) { |
| 92 | if (offset_out + 1 >= allocated_out) { |
| 93 | allocated_out += 1024; |
| 94 | line_out = xrealloc(line_out, allocated_out); |
| 95 | } |
| 96 | |
| 97 | if (c == '\n') { |
| 98 | line_out[offset_out++] = c; |
| 99 | fwrite(line_out, sizeof(char), (size_t) offset_out, stdout); |
| 100 | column = offset_out = 0; |
| 101 | continue; |
| 102 | } |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 103 | |
| 104 | rescan: |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 105 | column = adjust_column(column, c); |
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 | if (column > width) { |
| 108 | /* This character would make the line too long. |
| 109 | Print the line plus a newline, and make this character |
| 110 | start the next line. */ |
| 111 | if (flags & FLAG_BREAK_SPACES) { |
| 112 | /* Look for the last blank. */ |
| 113 | int logical_end; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 114 | |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 115 | for (logical_end = offset_out - 1; logical_end >= 0; logical_end--) { |
| 116 | if (isblank(line_out[logical_end])) { |
| 117 | break; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 120 | if (logical_end >= 0) { |
| 121 | /* Found a blank. Don't output the part after it. */ |
| 122 | logical_end++; |
| 123 | fwrite(line_out, sizeof(char), (size_t) logical_end, stdout); |
| 124 | putchar('\n'); |
| 125 | /* Move the remainder to the beginning of the next line. |
| 126 | The areas being copied here might overlap. */ |
| 127 | memmove(line_out, line_out + logical_end, offset_out - logical_end); |
| 128 | offset_out -= logical_end; |
| 129 | for (column = i = 0; i < offset_out; i++) { |
| 130 | column = adjust_column(column, line_out[i]); |
| 131 | } |
| 132 | goto rescan; |
| 133 | } |
| 134 | } else { |
| 135 | if (offset_out == 0) { |
| 136 | line_out[offset_out++] = c; |
| 137 | continue; |
| 138 | } |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 139 | } |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 140 | line_out[offset_out++] = '\n'; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 141 | fwrite(line_out, sizeof(char), (size_t) offset_out, stdout); |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 142 | column = offset_out = 0; |
| 143 | goto rescan; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Bernhard Reutner-Fischer | 4bf3127 | 2006-01-30 17:41:06 +0000 | [diff] [blame] | 146 | line_out[offset_out++] = c; |
| 147 | } |
| 148 | |
| 149 | if (offset_out) { |
| 150 | fwrite(line_out, sizeof(char), (size_t) offset_out, stdout); |
| 151 | } |
| 152 | |
| 153 | if (ferror(istream) || bb_fclose_nonstdin(istream)) { |
| 154 | bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */ |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 155 | errs |= EXIT_FAILURE; |
| 156 | } |
| 157 | } while (*++argv); |
| 158 | |
| 159 | bb_fflush_stdout_and_exit(errs); |
| 160 | } |
Bernhard Reutner-Fischer | d77b781 | 2005-12-10 20:13:54 +0000 | [diff] [blame] | 161 | /* vi: set sw=4 ts=4: */ |