2013-04-29 16:54:36 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <locale.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <wchar.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2013-04-29 16:54:36 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
FILE *fp;
|
|
|
|
const char *name;
|
|
|
|
} Fdescr;
|
|
|
|
|
|
|
|
static size_t unescape(wchar_t *);
|
|
|
|
static wint_t in(Fdescr *);
|
|
|
|
static void out(wchar_t);
|
|
|
|
static void sequential(Fdescr *, int, const wchar_t *, size_t);
|
|
|
|
static void parallel(Fdescr *, int, const wchar_t *, size_t);
|
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
eprintf("usage: %s [-s] [-d list] file...\n", argv0);
|
|
|
|
}
|
|
|
|
|
2013-04-29 16:54:36 +00:00
|
|
|
int
|
2013-06-14 18:20:47 +00:00
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2013-04-29 16:54:36 +00:00
|
|
|
const char *adelim = NULL;
|
2014-11-13 20:24:47 +00:00
|
|
|
int seq = 0;
|
2014-06-01 12:59:47 +00:00
|
|
|
wchar_t *delim = NULL;
|
2013-04-29 16:54:36 +00:00
|
|
|
size_t len;
|
2014-06-01 12:59:47 +00:00
|
|
|
Fdescr *dsc = NULL;
|
2013-06-14 18:20:47 +00:00
|
|
|
int i;
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2013-04-29 16:54:36 +00:00
|
|
|
setlocale(LC_CTYPE, "");
|
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
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (argc == 0)
|
2013-06-14 18:20:47 +00:00
|
|
|
usage();
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2013-04-29 16:54:36 +00:00
|
|
|
/* populate delimeters */
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!adelim)
|
2013-04-29 16:54:36 +00:00
|
|
|
adelim = "\t";
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2013-04-29 16:54:36 +00:00
|
|
|
len = mbstowcs(NULL, adelim, 0);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (len == (size_t) - 1)
|
2013-04-29 16:54:36 +00:00
|
|
|
eprintf("invalid delimiter\n");
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2014-11-16 10:07:26 +00:00
|
|
|
delim = emalloc((len + 1) * sizeof(*delim));
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2013-04-29 16:54:36 +00:00
|
|
|
mbstowcs(delim, adelim, len);
|
|
|
|
len = unescape(delim);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (len == 0)
|
2013-04-29 16:54:36 +00:00
|
|
|
eprintf("no delimiters specified\n");
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2013-04-29 16:54:36 +00:00
|
|
|
/* populate file list */
|
2014-11-16 10:07:26 +00:00
|
|
|
dsc = emalloc(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++) {
|
|
|
|
if (strcmp(argv[i], "-") == 0)
|
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");
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!dsc[i].fp)
|
2014-06-01 12:59:47 +00:00
|
|
|
eprintf("can't open '%s':", argv[i]);
|
2013-05-04 05:49:51 +00:00
|
|
|
|
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
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
if (dsc[i].fp != stdin)
|
2013-04-29 16:54:36 +00:00
|
|
|
(void)fclose(dsc[i].fp);
|
|
|
|
}
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2013-04-29 16:54:36 +00:00
|
|
|
free(delim);
|
|
|
|
free(dsc);
|
2013-06-14 18:20:47 +00:00
|
|
|
|
2014-10-02 22:46:04 +00:00
|
|
|
return 0;
|
2013-04-29 16:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2013-06-14 18:20:47 +00:00
|
|
|
unescape(wchar_t *delim)
|
|
|
|
{
|
2013-04-29 16:54:36 +00:00
|
|
|
wchar_t c;
|
|
|
|
size_t i;
|
|
|
|
size_t len;
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
for (i = 0, len = 0; (c = delim[i++]) != '\0'; len++) {
|
|
|
|
if (c == '\\') {
|
|
|
|
switch (delim[i++]) {
|
2013-04-29 16:54:36 +00:00
|
|
|
case 'n':
|
|
|
|
delim[len] = '\n';
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
delim[len] = '\t';
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
delim[len] = '\0';
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
delim[len] = '\\';
|
|
|
|
break;
|
|
|
|
case '\0':
|
|
|
|
default:
|
|
|
|
/* POSIX: unspecified results */
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
delim[len] = c;
|
|
|
|
}
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2013-04-29 16:54:36 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static wint_t
|
2013-06-14 18:20:47 +00:00
|
|
|
in(Fdescr *f)
|
|
|
|
{
|
2013-04-29 16:54:36 +00:00
|
|
|
wint_t c = fgetwc(f->fp);
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (c == WEOF && ferror(f->fp))
|
2013-04-29 16:54:36 +00:00
|
|
|
eprintf("'%s' read error:", f->name);
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2013-04-29 16:54:36 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-06-14 18:20:47 +00:00
|
|
|
out(wchar_t c)
|
|
|
|
{
|
2013-04-29 16:54:36 +00:00
|
|
|
putwchar(c);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (ferror(stdout))
|
2013-04-29 16:54:36 +00:00
|
|
|
eprintf("write error:");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-06-14 18:20:47 +00:00
|
|
|
sequential(Fdescr *dsc, int len, const wchar_t *delim, size_t cnt)
|
|
|
|
{
|
2013-04-29 16:54:36 +00:00
|
|
|
int i;
|
2014-06-01 12:59:47 +00:00
|
|
|
size_t d;
|
|
|
|
wint_t c, last;
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
2014-06-01 12:59:47 +00:00
|
|
|
d = 0;
|
|
|
|
last = WEOF;
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
while ((c = in(&dsc[i])) != WEOF) {
|
|
|
|
if (last == '\n') {
|
|
|
|
if (delim[d] != '\0')
|
2013-04-29 16:54:36 +00:00
|
|
|
out(delim[d]);
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2013-04-29 16:54:36 +00:00
|
|
|
d++;
|
|
|
|
d %= cnt;
|
|
|
|
}
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (c != '\n')
|
2013-04-29 16:54:36 +00:00
|
|
|
out((wchar_t)c);
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2013-04-29 16:54:36 +00:00
|
|
|
last = c;
|
|
|
|
}
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (last == '\n')
|
2013-04-29 16:54:36 +00:00
|
|
|
out((wchar_t)last);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-06-14 18:20:47 +00:00
|
|
|
parallel(Fdescr *dsc, int len, const wchar_t *delim, size_t cnt)
|
|
|
|
{
|
2014-06-01 12:59:47 +00:00
|
|
|
int last, i;
|
|
|
|
wint_t c, o;
|
|
|
|
wchar_t d;
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2013-04-29 16:54:36 +00:00
|
|
|
do {
|
|
|
|
last = 0;
|
2014-11-13 17:29:30 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
2014-06-01 12:59:47 +00:00
|
|
|
d = delim[i % cnt];
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2013-04-29 16:54:36 +00:00
|
|
|
do {
|
2014-06-01 12:59:47 +00:00
|
|
|
o = in(&dsc[i]);
|
2013-04-29 16:54:36 +00:00
|
|
|
c = o;
|
2014-11-13 17:29:30 +00:00
|
|
|
switch (c) {
|
2013-04-29 16:54:36 +00:00
|
|
|
case WEOF:
|
2014-11-13 17:29:30 +00:00
|
|
|
if (last == 0)
|
2013-04-29 16:54:36 +00:00
|
|
|
break;
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2013-04-29 16:54:36 +00:00
|
|
|
o = '\n';
|
|
|
|
/* fallthrough */
|
|
|
|
case '\n':
|
2014-11-13 17:29:30 +00:00
|
|
|
if (i != len - 1)
|
2013-04-29 16:54:36 +00:00
|
|
|
o = d;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-05-04 05:49:51 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (o != WEOF) {
|
2013-04-29 16:54:36 +00:00
|
|
|
/* pad with delimiters up to this point */
|
2014-11-13 17:29:30 +00:00
|
|
|
while (++last < i) {
|
|
|
|
if (d != '\0')
|
2013-04-29 16:54:36 +00:00
|
|
|
out(d);
|
|
|
|
}
|
|
|
|
out((wchar_t)o);
|
|
|
|
}
|
2014-11-13 17:29:30 +00:00
|
|
|
} while (c != '\n' && c != WEOF);
|
2013-04-29 16:54:36 +00:00
|
|
|
}
|
2014-11-13 17:29:30 +00:00
|
|
|
} while (last > 0);
|
2013-04-29 16:54:36 +00:00
|
|
|
}
|