Mercurial > hg > toybox
annotate toys/posix/paste.c @ 943:8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
likely behaves very badly when given too many files or delimiters.
author | Felix Janda <felix.janda@posteo.de> |
---|---|
date | Wed, 03 Jul 2013 02:29:24 -0500 |
parents | |
children | b4faf2ae39e8 |
rev | line source |
---|---|
943
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
1 /* paste.c - Replace newlines |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
2 * |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
3 * Copyright 2012 Felix Janda <felix.janda@posteo.de> |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
4 * |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
5 * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/paste.html |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
6 * |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
7 USE_PASTE(NEWTOY(paste, "d:s", TOYFLAG_BIN)) |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
8 |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
9 config PASTE |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
10 bool "paste" |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
11 default y |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
12 help |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
13 usage: paste [-s] [-d list] [file...] |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
14 |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
15 Replace newlines in files. |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
16 |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
17 -d list list of delimiters to separate lines |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
18 -s process files sequentially instead of in parallel |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
19 |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
20 By default print corresponding lines separated by <tab>. |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
21 */ |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
22 #define FOR_paste |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
23 #include "toys.h" |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
24 |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
25 GLOBALS( |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
26 char *delim; |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
27 ) |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
28 |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
29 void paste_main(void) |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
30 { |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
31 char *p, *buf = toybuf; |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
32 char **args = toys.optargs; |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
33 size_t ndelim = 0; |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
34 int i, j, c; |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
35 |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
36 // Process delimiter list |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
37 // TODO: Handle multibyte characters |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
38 if (!(toys.optflags & FLAG_d)) TT.delim = "\t"; |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
39 p = TT.delim; |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
40 for (; *p; p++, buf++, ndelim++) { |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
41 if (*p == '\\') { |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
42 p++; |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
43 if (-1 == (i = stridx("nt\\0", *p))) |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
44 error_exit("bad delimiter: \\%c", *p); |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
45 *buf = "\n\t\\\0"[i]; |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
46 } |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
47 else *buf = *p; |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
48 } |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
49 *buf = 0; |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
50 |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
51 if (toys.optflags & FLAG_s) { // Sequential |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
52 FILE *f; |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
53 for (; *args; args++) { |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
54 if ((*args)[0] == '-' && !(*args)[1]) f = stdin; |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
55 else if (!(f = fopen(*args, "r"))) perror_exit("%s", *args); |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
56 for (i = 0, c = 0; c != EOF;) { |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
57 switch(c = getc(f)) { |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
58 case '\n': |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
59 putchar(toybuf[i++ % ndelim]); |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
60 case EOF: |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
61 break; |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
62 default: |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
63 putchar(c); |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
64 } |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
65 } |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
66 if (f != stdin) fclose(f); |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
67 putchar('\n'); |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
68 } |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
69 } |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
70 else { // Parallel |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
71 // Need to be careful not to print an extra line at the end |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
72 FILE **files; |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
73 int anyopen = 1; |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
74 files = (FILE**)(buf + 1); |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
75 for (; *args; args++, files++) { |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
76 if ((*args)[0] == '-' && !(*args)[1]) *files = stdin; |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
77 else if (!(*files = fopen(*args, "r"))) perror_exit("%s", *args); |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
78 } |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
79 for (; anyopen;) { |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
80 anyopen = 0; |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
81 for (i = 0; i < toys.optc; i++) { |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
82 FILE **f = (FILE**)(buf + 1) + i; |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
83 if (*f) for (;;) { |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
84 c = getc(*f); |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
85 if (c != EOF) { |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
86 if (!anyopen++) for (j = 0; j < i; j++) putchar(toybuf[j % ndelim]); |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
87 if (c != '\n') putchar(c); |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
88 else break; |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
89 } |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
90 else { |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
91 if (*f != stdin) fclose(*f); |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
92 *f = 0; |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
93 break; |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
94 } |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
95 } |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
96 if (anyopen) putchar((i + 1 == toys.optc) ? toybuf[i % ndelim] : '\n'); |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
97 } |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
98 } |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
99 } |
8cb7d65a40e8
here's a version of paste. It doesn't deal with wide characters yet and
Felix Janda <felix.janda@posteo.de>
parents:
diff
changeset
|
100 } |