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 |
| 7 | Copyright (C) 2003 Glenn McGrath <bug1@optushome.com.au> |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation; either version 2, or (at your option) |
| 12 | any later version. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with this program; if not, write to the Free Software Foundation, |
| 21 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 22 | */ |
| 23 | |
| 24 | #include <ctype.h> |
| 25 | #include <errno.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <getopt.h> |
| 30 | #include <sys/types.h> |
| 31 | |
| 32 | #include "busybox.h" |
| 33 | |
| 34 | /* If nonzero, count bytes, not column positions. */ |
| 35 | static int count_bytes; |
| 36 | |
| 37 | /* Assuming the current column is COLUMN, return the column that |
| 38 | printing C will move the cursor to. |
| 39 | The first column is 0. */ |
| 40 | |
| 41 | static int adjust_column(int column, char c) |
| 42 | { |
| 43 | if (!count_bytes) { |
| 44 | if (c == '\b') { |
| 45 | if (column > 0) |
| 46 | column--; |
| 47 | } else if (c == '\r') |
| 48 | column = 0; |
| 49 | else if (c == '\t') |
| 50 | column = column + 8 - column % 8; |
| 51 | else /* if (isprint (c)) */ |
| 52 | column++; |
| 53 | } else |
| 54 | column++; |
| 55 | return column; |
| 56 | } |
| 57 | |
| 58 | extern int fold_main(int argc, char **argv) |
| 59 | { |
| 60 | /* If nonzero, try to break on whitespace. */ |
| 61 | int break_spaces; |
| 62 | |
| 63 | /* If nonzero, at least one of the files we read was standard input. */ |
| 64 | int have_read_stdin; |
| 65 | |
| 66 | int width = 80; |
| 67 | int i; |
| 68 | int optc; |
| 69 | int errs = 0; |
| 70 | |
| 71 | break_spaces = count_bytes = have_read_stdin = 0; |
| 72 | |
| 73 | /* Turn any numeric options into -w options. */ |
| 74 | for (i = 1; i < argc; i++) { |
| 75 | char const *a = argv[i]; |
| 76 | |
| 77 | if (a[0] == '-') { |
| 78 | if (a[1] == '-' && !a[2]) |
| 79 | break; |
| 80 | if (isdigit(a[1])) { |
| 81 | char *s = xmalloc(strlen(a) + 2); |
| 82 | |
| 83 | s[0] = '-'; |
| 84 | s[1] = 'w'; |
| 85 | strcpy(s + 2, a + 1); |
| 86 | argv[i] = s; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | while ((optc = getopt(argc, argv, "bsw:")) > 0) { |
| 92 | switch (optc) { |
| 93 | case 'b': /* Count bytes rather than columns. */ |
| 94 | count_bytes = 1; |
| 95 | break; |
| 96 | case 's': /* Break at word boundaries. */ |
| 97 | break_spaces = 1; |
| 98 | break; |
| 99 | case 'w': { /* Line width. */ |
| 100 | long int tmp_long; |
| 101 | char *end_ptr; |
| 102 | |
| 103 | errno = 0; |
| 104 | tmp_long = strtol(optarg, &end_ptr, 10); |
| 105 | if (!end_ptr || errno || tmp_long < 1) { |
| 106 | bb_error_msg_and_die("invalid number of columns: `%s'", optarg); |
| 107 | } |
| 108 | width = (int) tmp_long; |
| 109 | break; |
| 110 | } |
| 111 | default: |
| 112 | bb_show_usage(); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | argv += optind; |
| 117 | if (!*argv) { |
| 118 | *--argv = "-"; |
| 119 | } |
| 120 | |
| 121 | do { |
| 122 | FILE *istream = bb_wfopen_input(*argv); |
| 123 | if (istream != NULL) { |
| 124 | int c; |
| 125 | int column = 0; /* Screen column where next char will go. */ |
| 126 | int offset_out = 0; /* Index in `line_out' for next char. */ |
| 127 | static char *line_out = NULL; |
| 128 | static int allocated_out = 0; |
| 129 | |
| 130 | while ((c = getc(istream)) != EOF) { |
| 131 | if (offset_out + 1 >= allocated_out) { |
| 132 | allocated_out += 1024; |
| 133 | line_out = xrealloc(line_out, allocated_out); |
| 134 | } |
| 135 | |
| 136 | if (c == '\n') { |
| 137 | line_out[offset_out++] = c; |
| 138 | fwrite(line_out, sizeof(char), (size_t) offset_out, stdout); |
| 139 | column = offset_out = 0; |
| 140 | continue; |
| 141 | } |
| 142 | |
| 143 | rescan: |
| 144 | column = adjust_column(column, c); |
| 145 | |
| 146 | if (column > width) { |
| 147 | /* This character would make the line too long. |
| 148 | Print the line plus a newline, and make this character |
| 149 | start the next line. */ |
| 150 | if (break_spaces) { |
| 151 | /* Look for the last blank. */ |
| 152 | int logical_end; |
| 153 | |
| 154 | for (logical_end = offset_out - 1; logical_end >= 0; logical_end--) { |
| 155 | if (isblank(line_out[logical_end])) { |
| 156 | break; |
| 157 | } |
| 158 | } |
| 159 | if (logical_end >= 0) { |
| 160 | /* Found a blank. Don't output the part after it. */ |
| 161 | logical_end++; |
| 162 | fwrite(line_out, sizeof(char), (size_t) logical_end, stdout); |
| 163 | putchar('\n'); |
| 164 | /* Move the remainder to the beginning of the next line. |
| 165 | The areas being copied here might overlap. */ |
| 166 | memmove(line_out, line_out + logical_end, offset_out - logical_end); |
| 167 | offset_out -= logical_end; |
| 168 | for (column = i = 0; i < offset_out; i++) { |
| 169 | column = adjust_column(column, line_out[i]); |
| 170 | } |
| 171 | goto rescan; |
| 172 | } |
| 173 | } else { |
| 174 | if (offset_out == 0) { |
| 175 | line_out[offset_out++] = c; |
| 176 | continue; |
| 177 | } |
| 178 | } |
| 179 | line_out[offset_out++] = '\n'; |
| 180 | fwrite(line_out, sizeof(char), (size_t) offset_out, stdout); |
| 181 | column = offset_out = 0; |
| 182 | goto rescan; |
| 183 | } |
| 184 | |
| 185 | line_out[offset_out++] = c; |
| 186 | } |
| 187 | |
| 188 | if (offset_out) { |
| 189 | fwrite(line_out, sizeof(char), (size_t) offset_out, stdout); |
| 190 | } |
| 191 | |
| 192 | if (ferror(istream) || bb_fclose_nonstdin(istream)) { |
| 193 | bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */ |
| 194 | errs |= EXIT_FAILURE; |
| 195 | } |
| 196 | } else { |
| 197 | errs |= EXIT_FAILURE; |
| 198 | } |
| 199 | } while (*++argv); |
| 200 | |
| 201 | bb_fflush_stdout_and_exit(errs); |
| 202 | } |