Mercurial > hg > toybox
annotate toys/pending/fold.c @ 1349:bf2e5591bdf0 draft
Cleanup strings.
author | Rob Landley <rob@landley.net> |
---|---|
date | Wed, 11 Jun 2014 08:02:55 -0500 |
parents | e81b951bf725 |
children |
rev | line source |
---|---|
1240
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
1 /* fold.c - fold text |
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
2 * |
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
3 * Copyright 2014 Samuel Holland <samuel@sholland.net> |
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
4 * |
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/fold.html |
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
6 |
1262
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
7 USE_FOLD(NEWTOY(fold, "bsuw#", TOYFLAG_USR|TOYFLAG_BIN)) |
1240
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
8 |
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
9 config FOLD |
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
10 bool "fold" |
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
11 default n |
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
12 help |
1262
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
13 usage: fold [-bsu] [-w WIDTH] [FILE...] |
1240
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
14 |
1270
e81b951bf725
fold cleanup: whitespace and curly brackets.
Rob Landley <rob@landley.net>
parents:
1262
diff
changeset
|
15 Folds (wraps) or unfolds ascii text by adding or removing newlines. |
1262
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
16 Default line width is 80 columns for folding and infinite for unfolding. |
1240
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
17 |
1270
e81b951bf725
fold cleanup: whitespace and curly brackets.
Rob Landley <rob@landley.net>
parents:
1262
diff
changeset
|
18 -b Fold based on bytes instead of columns |
e81b951bf725
fold cleanup: whitespace and curly brackets.
Rob Landley <rob@landley.net>
parents:
1262
diff
changeset
|
19 -s Fold/unfold at whitespace boundaries if possible |
e81b951bf725
fold cleanup: whitespace and curly brackets.
Rob Landley <rob@landley.net>
parents:
1262
diff
changeset
|
20 -u Unfold text (and refold if -w is given) |
e81b951bf725
fold cleanup: whitespace and curly brackets.
Rob Landley <rob@landley.net>
parents:
1262
diff
changeset
|
21 -w Set lines to WIDTH columns or bytes |
1240
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
22 */ |
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
23 |
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
24 #define FOR_fold |
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
25 #include "toys.h" |
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
26 |
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
27 GLOBALS( |
1262
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
28 int width; |
1240
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
29 ) |
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
30 |
1270
e81b951bf725
fold cleanup: whitespace and curly brackets.
Rob Landley <rob@landley.net>
parents:
1262
diff
changeset
|
31 // wcwidth mbrtowc |
1240
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
32 void do_fold(int fd, char *name) |
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
33 { |
1270
e81b951bf725
fold cleanup: whitespace and curly brackets.
Rob Landley <rob@landley.net>
parents:
1262
diff
changeset
|
34 int bufsz, len = 0, maxlen; |
1240
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
35 |
1270
e81b951bf725
fold cleanup: whitespace and curly brackets.
Rob Landley <rob@landley.net>
parents:
1262
diff
changeset
|
36 if (toys.optflags & FLAG_w) maxlen = TT.width; |
e81b951bf725
fold cleanup: whitespace and curly brackets.
Rob Landley <rob@landley.net>
parents:
1262
diff
changeset
|
37 else if (toys.optflags & FLAG_u) maxlen = 0; |
e81b951bf725
fold cleanup: whitespace and curly brackets.
Rob Landley <rob@landley.net>
parents:
1262
diff
changeset
|
38 else maxlen = 80; |
1240
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
39 |
1262
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
40 while ((bufsz = read(fd, toybuf, sizeof(toybuf))) > 0) { |
1270
e81b951bf725
fold cleanup: whitespace and curly brackets.
Rob Landley <rob@landley.net>
parents:
1262
diff
changeset
|
41 char *buf = toybuf; |
e81b951bf725
fold cleanup: whitespace and curly brackets.
Rob Landley <rob@landley.net>
parents:
1262
diff
changeset
|
42 int pos = 0, split = -1; |
1240
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
43 |
1262
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
44 while (pos < bufsz) { |
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
45 switch (buf[pos]) { |
1240
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
46 case '\n': |
1270
e81b951bf725
fold cleanup: whitespace and curly brackets.
Rob Landley <rob@landley.net>
parents:
1262
diff
changeset
|
47 // print everything but the \n, then move on to the next buffer |
1262
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
48 if ((toys.optflags & FLAG_u) && buf[pos-1] != '\n' |
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
49 && buf[pos+1] != '\n') { |
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
50 xwrite(1, buf, pos); |
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
51 bufsz -= pos + 1; |
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
52 buf += pos + 1; |
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
53 pos = 0; |
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
54 split = -1; |
1270
e81b951bf725
fold cleanup: whitespace and curly brackets.
Rob Landley <rob@landley.net>
parents:
1262
diff
changeset
|
55 // reset len, FLAG_b or not; just print multiple lines at once |
e81b951bf725
fold cleanup: whitespace and curly brackets.
Rob Landley <rob@landley.net>
parents:
1262
diff
changeset
|
56 } else len = 0; |
1262
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
57 break; |
1240
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
58 case '\b': |
1270
e81b951bf725
fold cleanup: whitespace and curly brackets.
Rob Landley <rob@landley.net>
parents:
1262
diff
changeset
|
59 // len cannot be negative; not allowed to wrap after backspace |
1240
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
60 if (toys.optflags & FLAG_b) len++; |
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
61 else if (len > 0) len--; |
1262
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
62 break; |
1240
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
63 case '\r': |
1270
e81b951bf725
fold cleanup: whitespace and curly brackets.
Rob Landley <rob@landley.net>
parents:
1262
diff
changeset
|
64 // not allowed to wrap after carriage return |
1240
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
65 if (toys.optflags & FLAG_b) len++; |
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
66 else len = 0; |
1262
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
67 break; |
1240
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
68 case '\t': |
1270
e81b951bf725
fold cleanup: whitespace and curly brackets.
Rob Landley <rob@landley.net>
parents:
1262
diff
changeset
|
69 // round to 8, but we add one after falling through |
e81b951bf725
fold cleanup: whitespace and curly brackets.
Rob Landley <rob@landley.net>
parents:
1262
diff
changeset
|
70 // (because of whitespace, but it also takes care of FLAG_b) |
e81b951bf725
fold cleanup: whitespace and curly brackets.
Rob Landley <rob@landley.net>
parents:
1262
diff
changeset
|
71 if (!(toys.optflags & FLAG_b)) len = (len & ~7) + 7; |
1240
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
72 case ' ': |
1262
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
73 split = pos; |
1240
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
74 default: |
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
75 len++; |
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
76 } |
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
77 |
1270
e81b951bf725
fold cleanup: whitespace and curly brackets.
Rob Landley <rob@landley.net>
parents:
1262
diff
changeset
|
78 // we don't want to double up \n; not allowed to wrap before \b |
1262
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
79 if (maxlen > 0 && len >= maxlen && buf[pos+1] != '\n' && buf[pos+1] != '\b') { |
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
80 if (!(toys.optflags & FLAG_s) || split < 0) split = pos; |
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
81 xwrite(1, buf, split + 1); |
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
82 xputc('\n'); |
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
83 bufsz -= split + 1; |
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
84 buf += split + 1; |
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
85 len = pos = 0; |
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
86 split = -1; |
1270
e81b951bf725
fold cleanup: whitespace and curly brackets.
Rob Landley <rob@landley.net>
parents:
1262
diff
changeset
|
87 } else pos++; |
1240
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
88 } |
1262
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
89 xwrite(1, buf, bufsz); |
1240
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
90 } |
1262
8556669d3928
This version of fold fixes major bugs (infinite loop, overflow) and adds an option for un/refolding text.
Samuel Holland <samuel@sholland.net>
parents:
1240
diff
changeset
|
91 xputc('\n'); |
1240
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
92 } |
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
93 |
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
94 void fold_main(void) |
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
95 { |
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
96 loopfiles(toys.optargs, do_fold); |
0d295a46f853
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
Samuel Holland <samuel@sholland.net>
parents:
diff
changeset
|
97 } |