2013-06-17 11:00:36 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2014-11-20 18:33:25 +00:00
|
|
|
#include "utf.h"
|
2013-06-17 11:00:36 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2014-11-20 18:33:25 +00:00
|
|
|
static int expand(const char *, FILE *, int);
|
2013-06-17 11:00:36 +00:00
|
|
|
|
2014-11-13 20:24:47 +00:00
|
|
|
static int iflag = 0;
|
2014-06-09 15:54:45 +00:00
|
|
|
|
2013-06-17 11:00:36 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2013-08-15 09:55:21 +00:00
|
|
|
eprintf("usage: %s [-i] [-t n] [file...]\n", argv0);
|
2013-06-17 11:00:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
int tabstop = 8;
|
2014-11-21 17:53:22 +00:00
|
|
|
int ret = 0;
|
2013-06-17 11:00:36 +00:00
|
|
|
|
|
|
|
ARGBEGIN {
|
2013-08-15 09:55:21 +00:00
|
|
|
case 'i':
|
2014-11-13 20:24:47 +00:00
|
|
|
iflag = 1;
|
2014-06-09 15:54:45 +00:00
|
|
|
break;
|
2013-06-17 11:00:36 +00:00
|
|
|
case 't':
|
|
|
|
tabstop = estrtol(EARGF(usage()), 0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
if (argc == 0) {
|
2014-11-20 18:33:25 +00:00
|
|
|
expand("<stdin>", stdin, tabstop);
|
2013-06-17 11:00:36 +00:00
|
|
|
} else {
|
2014-06-09 15:53:55 +00:00
|
|
|
for (; argc > 0; argc--, argv++) {
|
2014-11-20 18:33:25 +00:00
|
|
|
if (!(fp = fopen(argv[0], "r"))) {
|
|
|
|
weprintf("fopen %s:", argv[0]);
|
2014-11-21 17:53:22 +00:00
|
|
|
ret = 1;
|
2013-11-13 11:39:24 +00:00
|
|
|
continue;
|
2013-06-17 11:00:36 +00:00
|
|
|
}
|
2014-11-20 18:33:25 +00:00
|
|
|
expand(argv[0], fp, tabstop);
|
2013-06-17 11:00:36 +00:00
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
}
|
2014-11-21 17:53:22 +00:00
|
|
|
return ret;
|
2013-06-17 11:00:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2014-11-20 18:33:25 +00:00
|
|
|
expand(const char *file, FILE *fp, int tabstop)
|
2013-06-17 11:00:36 +00:00
|
|
|
{
|
|
|
|
int col = 0;
|
2014-11-20 18:33:25 +00:00
|
|
|
Rune r;
|
2014-11-13 20:24:47 +00:00
|
|
|
int bol = 1;
|
2013-06-17 11:00:36 +00:00
|
|
|
|
|
|
|
for (;;) {
|
2014-11-21 16:20:15 +00:00
|
|
|
if (!readrune(file, fp, &r))
|
2013-06-17 11:00:36 +00:00
|
|
|
break;
|
|
|
|
|
2014-11-20 18:33:25 +00:00
|
|
|
switch (r) {
|
2013-06-17 11:00:36 +00:00
|
|
|
case '\t':
|
2014-06-09 15:54:45 +00:00
|
|
|
if (bol || !iflag) {
|
|
|
|
do {
|
|
|
|
col++;
|
2014-11-20 18:33:25 +00:00
|
|
|
putchar(' ');
|
2014-06-09 15:54:45 +00:00
|
|
|
} while (col & (tabstop - 1));
|
|
|
|
} else {
|
2014-11-20 18:33:25 +00:00
|
|
|
putchar('\t');
|
2014-06-09 15:54:45 +00:00
|
|
|
col += tabstop - col % tabstop;
|
|
|
|
}
|
2013-06-17 11:00:36 +00:00
|
|
|
break;
|
|
|
|
case '\b':
|
|
|
|
if (col)
|
|
|
|
col--;
|
2014-11-13 20:24:47 +00:00
|
|
|
bol = 0;
|
2014-11-21 16:34:44 +00:00
|
|
|
writerune("<stdout>", stdout, &r);
|
2013-06-17 11:00:36 +00:00
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
col = 0;
|
2014-11-13 20:24:47 +00:00
|
|
|
bol = 1;
|
2014-11-21 16:34:44 +00:00
|
|
|
writerune("<stdout>", stdout, &r);
|
2013-06-17 11:00:36 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
col++;
|
2014-11-20 18:33:25 +00:00
|
|
|
if (r != ' ')
|
2014-11-13 20:24:47 +00:00
|
|
|
bol = 0;
|
2014-11-21 16:34:44 +00:00
|
|
|
writerune("<stdout>", stdout, &r);
|
2013-06-17 11:00:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|