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; |
| 51 | |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 52 | /* If nonzero, at least one of the files we read was standard input. */ |
Rob Landley | 746cfc8 | 2005-12-02 17:55:45 +0000 | [diff] [blame] | 53 | int have_read_stdin = 0; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 54 | |
| 55 | int width = 80; |
| 56 | int i; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 57 | int errs = 0; |
| 58 | |
Rob Landley | 746cfc8 | 2005-12-02 17:55:45 +0000 | [diff] [blame] | 59 | have_read_stdin = 0; |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 60 | |
| 61 | /* Turn any numeric options into -w options. */ |
| 62 | for (i = 1; i < argc; i++) { |
| 63 | char const *a = argv[i]; |
| 64 | |
| 65 | if (a[0] == '-') { |
| 66 | if (a[1] == '-' && !a[2]) |
| 67 | break; |
| 68 | if (isdigit(a[1])) { |
| 69 | char *s = xmalloc(strlen(a) + 2); |
| 70 | |
| 71 | s[0] = '-'; |
| 72 | s[1] = 'w'; |
| 73 | strcpy(s + 2, a + 1); |
| 74 | argv[i] = s; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
Rob Landley | 746cfc8 | 2005-12-02 17:55:45 +0000 | [diff] [blame] | 79 | flags = bb_getopt_ulflags(argc, argv, "bsw:", &w_opt); |
| 80 | if (flags & 4) |
| 81 | width = bb_xgetlarg(w_opt, 10, 1, 10000); |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 82 | |
| 83 | argv += optind; |
| 84 | if (!*argv) { |
| 85 | *--argv = "-"; |
| 86 | } |
| 87 | |
| 88 | do { |
| 89 | FILE *istream = bb_wfopen_input(*argv); |
| 90 | if (istream != NULL) { |
| 91 | int c; |
| 92 | int column = 0; /* Screen column where next char will go. */ |
| 93 | int offset_out = 0; /* Index in `line_out' for next char. */ |
| 94 | static char *line_out = NULL; |
| 95 | static int allocated_out = 0; |
| 96 | |
| 97 | while ((c = getc(istream)) != EOF) { |
| 98 | if (offset_out + 1 >= allocated_out) { |
| 99 | allocated_out += 1024; |
| 100 | line_out = xrealloc(line_out, allocated_out); |
| 101 | } |
| 102 | |
| 103 | if (c == '\n') { |
| 104 | line_out[offset_out++] = c; |
| 105 | fwrite(line_out, sizeof(char), (size_t) offset_out, stdout); |
| 106 | column = offset_out = 0; |
| 107 | continue; |
| 108 | } |
| 109 | |
| 110 | rescan: |
| 111 | column = adjust_column(column, c); |
| 112 | |
| 113 | if (column > width) { |
| 114 | /* This character would make the line too long. |
| 115 | Print the line plus a newline, and make this character |
| 116 | start the next line. */ |
Rob Landley | 746cfc8 | 2005-12-02 17:55:45 +0000 | [diff] [blame] | 117 | if (flags & FLAG_BREAK_SPACES) { |
Glenn L McGrath | f01b805 | 2003-04-27 06:02:14 +0000 | [diff] [blame] | 118 | /* Look for the last blank. */ |
| 119 | int logical_end; |
| 120 | |
| 121 | for (logical_end = offset_out - 1; logical_end >= 0; logical_end--) { |
| 122 | if (isblank(line_out[logical_end])) { |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | if (logical_end >= 0) { |
| 127 | /* Found a blank. Don't output the part after it. */ |
| 128 | logical_end++; |
| 129 | fwrite(line_out, sizeof(char), (size_t) logical_end, stdout); |
| 130 | putchar('\n'); |
| 131 | /* Move the remainder to the beginning of the next line. |
| 132 | The areas being copied here might overlap. */ |
| 133 | memmove(line_out, line_out + logical_end, offset_out - logical_end); |
| 134 | offset_out -= logical_end; |
| 135 | for (column = i = 0; i < offset_out; i++) { |
| 136 | column = adjust_column(column, line_out[i]); |
| 137 | } |
| 138 | goto rescan; |
| 139 | } |
| 140 | } else { |
| 141 | if (offset_out == 0) { |
| 142 | line_out[offset_out++] = c; |
| 143 | continue; |
| 144 | } |
| 145 | } |
| 146 | line_out[offset_out++] = '\n'; |
| 147 | fwrite(line_out, sizeof(char), (size_t) offset_out, stdout); |
| 148 | column = offset_out = 0; |
| 149 | goto rescan; |
| 150 | } |
| 151 | |
| 152 | line_out[offset_out++] = c; |
| 153 | } |
| 154 | |
| 155 | if (offset_out) { |
| 156 | fwrite(line_out, sizeof(char), (size_t) offset_out, stdout); |
| 157 | } |
| 158 | |
| 159 | if (ferror(istream) || bb_fclose_nonstdin(istream)) { |
| 160 | bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */ |
| 161 | errs |= EXIT_FAILURE; |
| 162 | } |
| 163 | } else { |
| 164 | errs |= EXIT_FAILURE; |
| 165 | } |
| 166 | } while (*++argv); |
| 167 | |
| 168 | bb_fflush_stdout_and_exit(errs); |
| 169 | } |