2013-04-29 16:54:36 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2015-01-29 19:15:50 +00:00
|
|
|
#include "utf.h"
|
2013-04-29 16:54:36 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-01-29 19:15:50 +00:00
|
|
|
struct fdescr {
|
2013-04-29 16:54:36 +00:00
|
|
|
FILE *fp;
|
|
|
|
const char *name;
|
2015-01-29 19:15:50 +00:00
|
|
|
};
|
2013-04-29 16:54:36 +00:00
|
|
|
|
2015-01-29 19:15:50 +00:00
|
|
|
static void
|
|
|
|
sequential(struct fdescr *dsc, int fdescrlen, Rune *delim, size_t delimlen)
|
|
|
|
{
|
|
|
|
Rune c, last;
|
|
|
|
size_t i, d;
|
|
|
|
|
|
|
|
for (i = 0; i < fdescrlen; i++) {
|
|
|
|
d = 0;
|
|
|
|
last = 0;
|
|
|
|
|
2015-02-11 19:13:43 +00:00
|
|
|
while (efgetrune(&c, dsc[i].fp, dsc[i].name)) {
|
2015-01-29 19:15:50 +00:00
|
|
|
if (last == '\n') {
|
|
|
|
if (delim[d] != '\0')
|
2015-02-11 19:58:00 +00:00
|
|
|
efputrune(&delim[d], stdout, "<stdout>");
|
2015-01-29 19:15:50 +00:00
|
|
|
d = (d + 1) % delimlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c != '\n')
|
2015-02-11 19:58:00 +00:00
|
|
|
efputrune(&c, stdout, "<stdout>");
|
2015-01-29 19:15:50 +00:00
|
|
|
last = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last == '\n')
|
2015-02-11 19:58:00 +00:00
|
|
|
efputrune(&last, stdout, "<stdout>");
|
2015-01-29 19:15:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
parallel(struct fdescr *dsc, int fdescrlen, Rune *delim, size_t delimlen)
|
|
|
|
{
|
|
|
|
Rune c, d;
|
|
|
|
size_t i, m;
|
|
|
|
ssize_t last;
|
|
|
|
|
|
|
|
nextline:
|
|
|
|
last = -1;
|
|
|
|
|
|
|
|
for (i = 0; i < fdescrlen; i++) {
|
|
|
|
d = delim[i % delimlen];
|
|
|
|
c = 0;
|
|
|
|
|
2015-02-11 19:13:43 +00:00
|
|
|
for (; efgetrune(&c, dsc[i].fp, dsc[i].name) ;) {
|
2015-01-29 19:15:50 +00:00
|
|
|
for (m = last + 1; m < i; m++)
|
2015-02-11 19:58:00 +00:00
|
|
|
efputrune(&(delim[m % delimlen]), stdout, "<stdout>");
|
2015-01-29 19:15:50 +00:00
|
|
|
last = i;
|
|
|
|
if (c == '\n') {
|
|
|
|
if (i != fdescrlen - 1)
|
|
|
|
c = d;
|
2015-02-11 19:58:00 +00:00
|
|
|
efputrune(&c, stdout, "<stdout>");
|
2015-01-29 19:15:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-02-11 19:58:00 +00:00
|
|
|
efputrune(&c, stdout, "<stdout>");
|
2015-01-29 19:15:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (c == 0 && last != -1) {
|
|
|
|
if (i == fdescrlen - 1)
|
|
|
|
putchar('\n');
|
|
|
|
else
|
2015-02-11 19:58:00 +00:00
|
|
|
efputrune(&d, stdout, "<stdout>");
|
2015-01-29 19:15:50 +00:00
|
|
|
last++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (last != -1)
|
|
|
|
goto nextline;
|
|
|
|
}
|
2013-04-29 16:54:36 +00:00
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-01-29 19:15:50 +00:00
|
|
|
eprintf("usage: %s [-s] [-d list] file ...\n", argv0);
|
2013-06-14 18:20:47 +00:00
|
|
|
}
|
|
|
|
|
2013-04-29 16:54:36 +00:00
|
|
|
int
|
2013-06-14 18:20:47 +00:00
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2015-03-10 21:19:19 +00:00
|
|
|
struct fdescr *dsc;
|
|
|
|
Rune *delim;
|
2015-01-29 19:15:50 +00:00
|
|
|
size_t i, len;
|
|
|
|
int seq = 0;
|
|
|
|
char *adelim = "\t";
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
ARGBEGIN {
|
|
|
|
case 's':
|
2014-11-13 20:24:47 +00:00
|
|
|
seq = 1;
|
2013-06-14 18:20:47 +00:00
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
adelim = EARGF(usage());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2015-03-17 20:30:12 +00:00
|
|
|
if (!argc)
|
2013-06-14 18:20:47 +00:00
|
|
|
usage();
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2015-01-29 19:15:50 +00:00
|
|
|
/* populate delimiters */
|
2015-01-29 20:52:44 +00:00
|
|
|
unescape(adelim);
|
2015-03-11 09:50:18 +00:00
|
|
|
delim = ereallocarray(NULL, utflen(adelim) + 1, sizeof(*delim));
|
2015-02-11 20:32:09 +00:00
|
|
|
len = utftorunestr(adelim, delim);
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2013-04-29 16:54:36 +00:00
|
|
|
/* populate file list */
|
2015-03-11 09:50:18 +00:00
|
|
|
dsc = ereallocarray(NULL, argc, sizeof(*dsc));
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
for (i = 0; i < argc; i++) {
|
2015-03-17 20:30:12 +00:00
|
|
|
if (!strcmp(argv[i], "-"))
|
2013-04-29 16:54:36 +00:00
|
|
|
dsc[i].fp = stdin;
|
|
|
|
else
|
2014-06-01 12:59:47 +00:00
|
|
|
dsc[i].fp = fopen(argv[i], "r");
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!dsc[i].fp)
|
2015-01-29 19:15:50 +00:00
|
|
|
eprintf("fopen %s:", argv[i]);
|
2014-06-01 12:59:47 +00:00
|
|
|
dsc[i].name = argv[i];
|
2013-04-29 16:54:36 +00:00
|
|
|
}
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (seq)
|
2013-04-29 16:54:36 +00:00
|
|
|
sequential(dsc, argc, delim, len);
|
|
|
|
else
|
|
|
|
parallel(dsc, argc, delim, len);
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2015-02-10 12:08:06 +00:00
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
fclose(dsc[i].fp);
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2014-10-02 22:46:04 +00:00
|
|
|
return 0;
|
2013-04-29 16:54:36 +00:00
|
|
|
}
|