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 | |
| 21 | /* If nonzero, count bytes, not column positions. */ |
Rob Landley | 746cfc8 | 2005-12-02 17:55:45 +0000 | [diff] [blame] | 22 | static unsigned long flags; |
| 23 | #define FLAG_COUNT_BYTES 1 |
| 24 | #define FLAG_BREAK_SPACES 2 |
| 25 | #define FLAG_WIDTH 4 |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 26 | |
| 27 | /* Assuming the current column is COLUMN, return the column that |
| 28 | printing C will move the cursor to. |
| 29 | The first column is 0. */ |
| 30 | |
| 31 | static int adjust_column(int column, char c) |
| 32 | { |
Rob Landley | 746cfc8 | 2005-12-02 17:55:45 +0000 | [diff] [blame] | 33 | if (!(flags & FLAG_COUNT_BYTES)) { |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 34 | if (c == '\b') { |
| 35 | if (column > 0) |
| 36 | column--; |
| 37 | } else if (c == '\r') |
| 38 | column = 0; |
| 39 | else if (c == '\t') |
| 40 | column = column + 8 - column % 8; |
| 41 | else /* if (isprint (c)) */ |
| 42 | column++; |
| 43 | } else |
| 44 | column++; |
| 45 | return column; |
| 46 | } |
| 47 | |
| 48 | extern int fold_main(int argc, char **argv) |
| 49 | { |
Paul Fox | 377bdaf | 2005-12-08 18:48:20 +0000 | [diff] [blame] | 50 | char *w_opt; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 51 | int width = 80; |
| 52 | int i; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 53 | int errs = 0; |
| 54 | |
Glenn L McGrath | 0bd0257 | 2005-12-11 03:09:05 +0000 | [diff] [blame^] | 55 | |
| 56 | #ifdef CONFIG_FEATURE_SUSv2_OBSOLETE |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 57 | /* Turn any numeric options into -w options. */ |
| 58 | for (i = 1; i < argc; i++) { |
| 59 | char const *a = argv[i]; |
| 60 | |
| 61 | if (a[0] == '-') { |
| 62 | if (a[1] == '-' && !a[2]) |
| 63 | break; |
| 64 | if (isdigit(a[1])) { |
| 65 | char *s = xmalloc(strlen(a) + 2); |
| 66 | |
| 67 | s[0] = '-'; |
| 68 | s[1] = 'w'; |
| 69 | strcpy(s + 2, a + 1); |
| 70 | argv[i] = s; |
| 71 | } |
| 72 | } |
| 73 | } |
Glenn L McGrath | 0bd0257 | 2005-12-11 03:09:05 +0000 | [diff] [blame^] | 74 | #endif |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 75 | |
Rob Landley | 746cfc8 | 2005-12-02 17:55:45 +0000 | [diff] [blame] | 76 | flags = bb_getopt_ulflags(argc, argv, "bsw:", &w_opt); |
Bernhard Reutner-Fischer | d77b781 | 2005-12-10 20:13:54 +0000 | [diff] [blame] | 77 | if (flags & FLAG_WIDTH) |
Rob Landley | 746cfc8 | 2005-12-02 17:55:45 +0000 | [diff] [blame] | 78 | width = bb_xgetlarg(w_opt, 10, 1, 10000); |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 79 | |
| 80 | argv += optind; |
| 81 | if (!*argv) { |
| 82 | *--argv = "-"; |
| 83 | } |
| 84 | |
| 85 | do { |
| 86 | FILE *istream = bb_wfopen_input(*argv); |
| 87 | if (istream != NULL) { |
| 88 | int c; |
| 89 | int column = 0; /* Screen column where next char will go. */ |
| 90 | int offset_out = 0; /* Index in `line_out' for next char. */ |
| 91 | static char *line_out = NULL; |
| 92 | static int allocated_out = 0; |
| 93 | |
| 94 | while ((c = getc(istream)) != EOF) { |
| 95 | if (offset_out + 1 >= allocated_out) { |
| 96 | allocated_out += 1024; |
| 97 | line_out = xrealloc(line_out, allocated_out); |
| 98 | } |
| 99 | |
| 100 | if (c == '\n') { |
| 101 | line_out[offset_out++] = c; |
| 102 | fwrite(line_out, sizeof(char), (size_t) offset_out, stdout); |
| 103 | column = offset_out = 0; |
| 104 | continue; |
| 105 | } |
| 106 | |
| 107 | rescan: |
| 108 | column = adjust_column(column, c); |
| 109 | |
| 110 | if (column > width) { |
| 111 | /* This character would make the line too long. |
Bernhard Reutner-Fischer | d77b781 | 2005-12-10 20:13:54 +0000 | [diff] [blame] | 112 | Print the line plus a newline, and make this character |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 113 | start the next line. */ |
Rob Landley | 746cfc8 | 2005-12-02 17:55:45 +0000 | [diff] [blame] | 114 | if (flags & FLAG_BREAK_SPACES) { |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 115 | /* Look for the last blank. */ |
| 116 | int logical_end; |
| 117 | |
| 118 | for (logical_end = offset_out - 1; logical_end >= 0; logical_end--) { |
| 119 | if (isblank(line_out[logical_end])) { |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | if (logical_end >= 0) { |
| 124 | /* Found a blank. Don't output the part after it. */ |
| 125 | logical_end++; |
| 126 | fwrite(line_out, sizeof(char), (size_t) logical_end, stdout); |
| 127 | putchar('\n'); |
| 128 | /* Move the remainder to the beginning of the next line. |
| 129 | The areas being copied here might overlap. */ |
| 130 | memmove(line_out, line_out + logical_end, offset_out - logical_end); |
| 131 | offset_out -= logical_end; |
| 132 | for (column = i = 0; i < offset_out; i++) { |
| 133 | column = adjust_column(column, line_out[i]); |
| 134 | } |
| 135 | goto rescan; |
| 136 | } |
| 137 | } else { |
| 138 | if (offset_out == 0) { |
| 139 | line_out[offset_out++] = c; |
| 140 | continue; |
| 141 | } |
| 142 | } |
| 143 | line_out[offset_out++] = '\n'; |
| 144 | fwrite(line_out, sizeof(char), (size_t) offset_out, stdout); |
| 145 | column = offset_out = 0; |
| 146 | goto rescan; |
| 147 | } |
| 148 | |
| 149 | line_out[offset_out++] = c; |
| 150 | } |
| 151 | |
| 152 | if (offset_out) { |
| 153 | fwrite(line_out, sizeof(char), (size_t) offset_out, stdout); |
| 154 | } |
| 155 | |
| 156 | if (ferror(istream) || bb_fclose_nonstdin(istream)) { |
| 157 | bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */ |
| 158 | errs |= EXIT_FAILURE; |
| 159 | } |
| 160 | } else { |
| 161 | errs |= EXIT_FAILURE; |
| 162 | } |
| 163 | } while (*++argv); |
| 164 | |
| 165 | bb_fflush_stdout_and_exit(errs); |
| 166 | } |
Bernhard Reutner-Fischer | d77b781 | 2005-12-10 20:13:54 +0000 | [diff] [blame] | 167 | /* vi: set sw=4 ts=4: */ |