Mercurial > hg > toybox
comparison toys/pending/fold.c @ 1270:e81b951bf725 draft
fold cleanup: whitespace and curly brackets.
author | Rob Landley <rob@landley.net> |
---|---|
date | Fri, 25 Apr 2014 05:56:16 -0500 |
parents | 8556669d3928 |
children |
comparison
equal
deleted
inserted
replaced
1269:78a6249f46b8 | 1270:e81b951bf725 |
---|---|
10 bool "fold" | 10 bool "fold" |
11 default n | 11 default n |
12 help | 12 help |
13 usage: fold [-bsu] [-w WIDTH] [FILE...] | 13 usage: fold [-bsu] [-w WIDTH] [FILE...] |
14 | 14 |
15 Folds (wraps) or unfolds FILE or stdin by adding or removing newlines. | 15 Folds (wraps) or unfolds ascii text by adding or removing newlines. |
16 Default line width is 80 columns for folding and infinite for unfolding. | 16 Default line width is 80 columns for folding and infinite for unfolding. |
17 | 17 |
18 -b Fold based on bytes instead of columns | 18 -b Fold based on bytes instead of columns |
19 -s Fold/unfold at whitespace boundaries if possible | 19 -s Fold/unfold at whitespace boundaries if possible |
20 -u Unfold text (and refold if -w is given) | 20 -u Unfold text (and refold if -w is given) |
21 -w Set lines to WIDTH columns or bytes | 21 -w Set lines to WIDTH columns or bytes |
22 */ | 22 */ |
23 | 23 |
24 #define FOR_fold | 24 #define FOR_fold |
25 #include "toys.h" | 25 #include "toys.h" |
26 | 26 |
27 GLOBALS( | 27 GLOBALS( |
28 int width; | 28 int width; |
29 ) | 29 ) |
30 | 30 |
31 // wcwidth mbrtowc | |
31 void do_fold(int fd, char *name) | 32 void do_fold(int fd, char *name) |
32 { | 33 { |
33 char *buf; | 34 int bufsz, len = 0, maxlen; |
34 int bufsz, pos, len = 0, maxlen, split; | |
35 | 35 |
36 if (toys.optflags & FLAG_w) | 36 if (toys.optflags & FLAG_w) maxlen = TT.width; |
37 maxlen = TT.width; | 37 else if (toys.optflags & FLAG_u) maxlen = 0; |
38 else if (toys.optflags & FLAG_u) | 38 else maxlen = 80; |
39 maxlen = 0; | |
40 else | |
41 maxlen = 80; | |
42 | 39 |
43 while ((bufsz = read(fd, toybuf, sizeof(toybuf))) > 0) { | 40 while ((bufsz = read(fd, toybuf, sizeof(toybuf))) > 0) { |
44 buf = toybuf; | 41 char *buf = toybuf; |
45 pos = 0; | 42 int pos = 0, split = -1; |
46 split = -1; | |
47 | 43 |
48 while (pos < bufsz) { | 44 while (pos < bufsz) { |
49 switch (buf[pos]) { | 45 switch (buf[pos]) { |
50 case '\n': | 46 case '\n': |
51 //print everything but the \n, then move on to the next buffer | 47 // print everything but the \n, then move on to the next buffer |
52 if ((toys.optflags & FLAG_u) && buf[pos-1] != '\n' | 48 if ((toys.optflags & FLAG_u) && buf[pos-1] != '\n' |
53 && buf[pos+1] != '\n') { | 49 && buf[pos+1] != '\n') { |
54 xwrite(1, buf, pos); | 50 xwrite(1, buf, pos); |
55 bufsz -= pos + 1; | 51 bufsz -= pos + 1; |
56 buf += pos + 1; | 52 buf += pos + 1; |
57 pos = 0; | 53 pos = 0; |
58 split = -1; | 54 split = -1; |
59 } | 55 // reset len, FLAG_b or not; just print multiple lines at once |
60 //reset len, FLAG_b or not; just print multiple lines at once | 56 } else len = 0; |
61 else len = 0; | |
62 break; | 57 break; |
63 case '\b': | 58 case '\b': |
64 //len cannot be negative; not allowed to wrap after backspace | 59 // len cannot be negative; not allowed to wrap after backspace |
65 if (toys.optflags & FLAG_b) len++; | 60 if (toys.optflags & FLAG_b) len++; |
66 else if (len > 0) len--; | 61 else if (len > 0) len--; |
67 break; | 62 break; |
68 case '\r': | 63 case '\r': |
69 //not allowed to wrap after carriage return | 64 // not allowed to wrap after carriage return |
70 if (toys.optflags & FLAG_b) len++; | 65 if (toys.optflags & FLAG_b) len++; |
71 else len = 0; | 66 else len = 0; |
72 break; | 67 break; |
73 case '\t': | 68 case '\t': |
74 //round to 8, but we add one after falling through | 69 // round to 8, but we add one after falling through |
75 //(because of whitespace, but it also takes care of FLAG_b) | 70 // (because of whitespace, but it also takes care of FLAG_b) |
76 if (!(toys.optflags & FLAG_b)) len = (len & -8) + 7; | 71 if (!(toys.optflags & FLAG_b)) len = (len & ~7) + 7; |
77 case ' ': | 72 case ' ': |
78 split = pos; | 73 split = pos; |
79 default: | 74 default: |
80 len++; | 75 len++; |
81 } | 76 } |
82 | 77 |
83 //we don't want to double up \n; not allowed to wrap before \b | 78 // we don't want to double up \n; not allowed to wrap before \b |
84 if (maxlen > 0 && len >= maxlen && buf[pos+1] != '\n' && buf[pos+1] != '\b') { | 79 if (maxlen > 0 && len >= maxlen && buf[pos+1] != '\n' && buf[pos+1] != '\b') { |
85 if (!(toys.optflags & FLAG_s) || split < 0) split = pos; | 80 if (!(toys.optflags & FLAG_s) || split < 0) split = pos; |
86 xwrite(1, buf, split + 1); | 81 xwrite(1, buf, split + 1); |
87 xputc('\n'); | 82 xputc('\n'); |
88 bufsz -= split + 1; | 83 bufsz -= split + 1; |
89 buf += split + 1; | 84 buf += split + 1; |
90 len = pos = 0; | 85 len = pos = 0; |
91 split = -1; | 86 split = -1; |
92 } else { | 87 } else pos++; |
93 pos++; | |
94 } | |
95 } | 88 } |
96 xwrite(1, buf, bufsz); | 89 xwrite(1, buf, bufsz); |
97 } | 90 } |
98 xputc('\n'); | 91 xputc('\n'); |
99 } | 92 } |