annotate toys/posix/paste.c @ 1776:7bf68329eb3b draft default tip

Repository switched to git at https://github.com/landley/toybox
author Rob Landley <rob@landley.net>
date Thu, 09 Apr 2015 02:28:32 -0500
parents b4faf2ae39e8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 {
944
b4faf2ae39e8 This inlines CRC64, and nothing more.
Isaac Dunham <idunham@lavabit.com>
parents: 943
diff changeset
31 char *p, *buf = toybuf, **args = toys.optargs;
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
32 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
33 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
34
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 // 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
36 // 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
37 if (!(toys.optflags & FLAG_d)) TT.delim = "\t";
944
b4faf2ae39e8 This inlines CRC64, and nothing more.
Isaac Dunham <idunham@lavabit.com>
parents: 943
diff changeset
38 for (p = TT.delim; *p; p++, buf++, ndelim++) {
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
39 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
40 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
41 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
42 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
43 *buf = "\n\t\\\0"[i];
944
b4faf2ae39e8 This inlines CRC64, and nothing more.
Isaac Dunham <idunham@lavabit.com>
parents: 943
diff changeset
44 } else *buf = *p;
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
45 }
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 *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
47
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 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
49 FILE *f;
944
b4faf2ae39e8 This inlines CRC64, and nothing more.
Isaac Dunham <idunham@lavabit.com>
parents: 943
diff changeset
50
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
51 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
52 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
53 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
54 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
55 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
56 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
57 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
58 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
59 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
60 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
61 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
62 }
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 }
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 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
65 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
66 }
944
b4faf2ae39e8 This inlines CRC64, and nothing more.
Isaac Dunham <idunham@lavabit.com>
parents: 943
diff changeset
67 } else { // Parallel
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
68 // 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
69 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
70 int anyopen = 1;
944
b4faf2ae39e8 This inlines CRC64, and nothing more.
Isaac Dunham <idunham@lavabit.com>
parents: 943
diff changeset
71
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
72 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
73 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
74 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
75 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
76 }
944
b4faf2ae39e8 This inlines CRC64, and nothing more.
Isaac Dunham <idunham@lavabit.com>
parents: 943
diff changeset
77 while (anyopen) {
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
78 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
79 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
80 FILE **f = (FILE**)(buf + 1) + i;
944
b4faf2ae39e8 This inlines CRC64, and nothing more.
Isaac Dunham <idunham@lavabit.com>
parents: 943
diff changeset
81
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
82 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
83 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
84 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
85 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
86 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
87 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
88 }
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 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
90 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
91 *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
92 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
93 }
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 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
96 }
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 }