blob: 8f74ed88d292046c089cbe11feb12762fee42b55 [file] [log] [blame]
Glenn L McGrathf01b8052003-04-27 06:02:14 +00001/* 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 McGrathc6992fe2004-04-25 05:11:19 +00007 Copyright (C) 2003 Glenn McGrath <bug1@iinet.net.au>
Glenn L McGrathf01b8052003-04-27 06:02:14 +00008
Rob Landley746cfc82005-12-02 17:55:45 +00009 Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Glenn L McGrathf01b8052003-04-27 06:02:14 +000010*/
11
12#include <ctype.h>
13#include <errno.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
Glenn L McGrathf01b8052003-04-27 06:02:14 +000017#include <sys/types.h>
Rob Landley746cfc82005-12-02 17:55:45 +000018#include <unistd.h>
Glenn L McGrathf01b8052003-04-27 06:02:14 +000019#include "busybox.h"
20
21/* If nonzero, count bytes, not column positions. */
Rob Landley746cfc82005-12-02 17:55:45 +000022static unsigned long flags;
23#define FLAG_COUNT_BYTES 1
24#define FLAG_BREAK_SPACES 2
25#define FLAG_WIDTH 4
Glenn L McGrathf01b8052003-04-27 06:02:14 +000026
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
31static int adjust_column(int column, char c)
32{
Rob Landley746cfc82005-12-02 17:55:45 +000033 if (!(flags & FLAG_COUNT_BYTES)) {
Glenn L McGrathf01b8052003-04-27 06:02:14 +000034 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
48extern int fold_main(int argc, char **argv)
49{
Paul Fox377bdaf2005-12-08 18:48:20 +000050 char *w_opt;
Glenn L McGrathf01b8052003-04-27 06:02:14 +000051 int width = 80;
52 int i;
Glenn L McGrathf01b8052003-04-27 06:02:14 +000053 int errs = 0;
54
Glenn L McGrathf01b8052003-04-27 06:02:14 +000055 /* Turn any numeric options into -w options. */
56 for (i = 1; i < argc; i++) {
57 char const *a = argv[i];
58
59 if (a[0] == '-') {
60 if (a[1] == '-' && !a[2])
61 break;
62 if (isdigit(a[1])) {
63 char *s = xmalloc(strlen(a) + 2);
64
65 s[0] = '-';
66 s[1] = 'w';
67 strcpy(s + 2, a + 1);
68 argv[i] = s;
69 }
70 }
71 }
72
Rob Landley746cfc82005-12-02 17:55:45 +000073 flags = bb_getopt_ulflags(argc, argv, "bsw:", &w_opt);
Bernhard Reutner-Fischerd77b7812005-12-10 20:13:54 +000074 if (flags & FLAG_WIDTH)
Rob Landley746cfc82005-12-02 17:55:45 +000075 width = bb_xgetlarg(w_opt, 10, 1, 10000);
Glenn L McGrathf01b8052003-04-27 06:02:14 +000076
77 argv += optind;
78 if (!*argv) {
79 *--argv = "-";
80 }
81
82 do {
83 FILE *istream = bb_wfopen_input(*argv);
84 if (istream != NULL) {
85 int c;
86 int column = 0; /* Screen column where next char will go. */
87 int offset_out = 0; /* Index in `line_out' for next char. */
88 static char *line_out = NULL;
89 static int allocated_out = 0;
90
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 }
103
104rescan:
105 column = adjust_column(column, c);
106
107 if (column > width) {
108 /* This character would make the line too long.
Bernhard Reutner-Fischerd77b7812005-12-10 20:13:54 +0000109 Print the line plus a newline, and make this character
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000110 start the next line. */
Rob Landley746cfc82005-12-02 17:55:45 +0000111 if (flags & FLAG_BREAK_SPACES) {
Glenn L McGrathf01b8052003-04-27 06:02:14 +0000112 /* Look for the last blank. */
113 int logical_end;
114
115 for (logical_end = offset_out - 1; logical_end >= 0; logical_end--) {
116 if (isblank(line_out[logical_end])) {
117 break;
118 }
119 }
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 }
139 }
140 line_out[offset_out++] = '\n';
141 fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
142 column = offset_out = 0;
143 goto rescan;
144 }
145
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. */
155 errs |= EXIT_FAILURE;
156 }
157 } else {
158 errs |= EXIT_FAILURE;
159 }
160 } while (*++argv);
161
162 bb_fflush_stdout_and_exit(errs);
163}
Bernhard Reutner-Fischerd77b7812005-12-10 20:13:54 +0000164/* vi: set sw=4 ts=4: */