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