Mercurial > hg > toybox
annotate toys/pending/fold.c @ 1240:0d295a46f853 draft
Here is a basic implementation of fold[0]. It does not support multibyte characters, though that would probably just require more switch cases.
author | Samuel Holland <samuel@sholland.net> |
---|---|
date | Thu, 03 Apr 2014 18:01:44 -0500 |
parents | |
children | 8556669d3928 |
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 |
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
|
7 USE_FOLD(NEWTOY(fold, "bsw#", TOYFLAG_USR|TOYFLAG_BIN)) |
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 |
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
|
13 usage: fold [-bs] [-w WIDTH] [FILE...] |
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 |
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
|
15 Folds/wraps FILE or stdin at 80 columns. |
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
|
16 |
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 -b Wrap based on bytes instead of columns |
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
|
18 -s Wrap at farthest right whitespace |
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
|
19 -w Wrap at WIDTH columns instead of 80 |
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
|
20 */ |
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
|
21 |
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 #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
|
23 #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
|
24 |
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 GLOBALS( |
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 int w_number; |
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 ) |
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
|
28 |
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 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
|
30 { |
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
|
31 int buflen, i, len = 0, split; |
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 int max = (toys.optflags & FLAG_w) ? TT.w_number : 80; |
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 char tmp, *buf; |
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
|
34 |
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 if (max > sizeof(toybuf)) { |
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
|
36 error_exit("width (%ld) too big", max); |
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
|
37 } |
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
|
38 |
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 while (read(fd, toybuf, sizeof(toybuf))) { |
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
|
40 split = -1; |
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
|
41 buf = toybuf; |
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
|
42 buflen = strlen(buf); |
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 |
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
|
44 for (i = 0; i < buflen; i++) { |
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
|
45 switch (buf[i]) { |
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': |
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
|
47 //reset len, FLAG_b or not; just print multiple lines at once |
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
|
48 len = 0; |
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
|
49 continue; |
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
|
50 case '\b': |
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
|
51 //len cannot be negative; not allowed to wrap after backspace |
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
|
52 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
|
53 else if (len > 0) 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
|
54 continue; |
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
|
55 case '\r': |
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
|
56 //not allowed to wrap after carriage return |
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
|
57 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
|
58 else len = 0; |
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
|
59 continue; |
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 case '\t': |
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 //round to 8, but we add one after falling through |
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
|
62 //(because of whitespace, but it also takes care of FLAG_b) |
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 if (!(toys.optflags & FLAG_b)) len = (len & -8) + 7; |
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
|
64 case ' ': |
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 split = i; |
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 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
|
67 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
|
68 } |
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
|
69 |
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
|
70 //we don't want to double up \n; not allowed to wrap before \b |
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
|
71 if (len >= max && buf[i+1] != '\n' && buf[i+1] != '\b') { |
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 if (!(toys.optflags & FLAG_s)) split = i; //we split right here |
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
|
73 tmp = buf[split+1]; |
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 buf[split+1] = 0; |
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 xprintf("%s\n", buf); |
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 buf[split+1] = tmp; |
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 len = 0; |
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
|
78 if (split < buflen - 1) { |
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
|
79 buf += split + 1; |
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
|
80 i = 0; |
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
|
81 buflen = strlen(buf); |
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
|
82 } |
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
|
83 } |
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
|
84 } |
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
|
85 |
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
|
86 xputs(buf); |
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
|
87 } |
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 } |
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
|
89 |
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 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
|
91 { |
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 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
|
93 } |