sbase/unexpand.c

120 lines
1.9 KiB
C
Raw Normal View History

2014-06-09 15:52:20 +00:00
/* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
2014-11-21 17:52:09 +00:00
#include "utf.h"
2014-06-09 15:52:20 +00:00
#include "util.h"
2014-11-21 17:52:09 +00:00
static void unexpand(const char *, FILE *);
2014-06-09 15:52:20 +00:00
static int aflag = 0;
2014-06-09 15:52:20 +00:00
static int tabsize = 8;
static void
usage(void)
{
eprintf("usage: %s [-a] [-t n] [file ...]\n", argv0);
}
int
main(int argc, char *argv[])
{
FILE *fp;
2014-11-21 17:52:09 +00:00
int ret = 0;
2014-06-09 15:52:20 +00:00
ARGBEGIN {
case 't':
tabsize = estrtol(EARGF(usage()), 0);
if (tabsize <= 0)
2014-12-21 22:14:39 +00:00
eprintf("unexpand: invalid tabsize\n");
2014-06-09 15:52:20 +00:00
/* Fallthrough: -t implies -a */
case 'a':
aflag = 1;
2014-06-09 15:52:20 +00:00
break;
default:
usage();
} ARGEND;
if (argc == 0) {
2014-11-21 17:52:09 +00:00
unexpand("<stdin>", stdin);
2014-06-09 15:52:20 +00:00
} else {
for (; argc > 0; argc--, argv++) {
2014-11-21 17:52:09 +00:00
if (!(fp = fopen(argv[0], "r"))) {
weprintf("fopen %s:", argv[0]);
ret = 1;
2014-06-09 15:52:20 +00:00
continue;
}
2014-11-21 17:52:09 +00:00
unexpand(argv[0], fp);
2014-06-09 15:52:20 +00:00
fclose(fp);
}
}
2014-11-21 17:52:09 +00:00
return ret;
2014-06-09 15:52:20 +00:00
}
static void
unexpandspan(unsigned int n, unsigned int col)
{
unsigned int off = (col-n) % tabsize;
2014-11-21 17:52:09 +00:00
Rune r;
2014-06-09 15:52:20 +00:00
if (n + off >= tabsize && n > 1)
2014-06-09 15:52:20 +00:00
n += off;
2014-11-21 17:52:09 +00:00
r = '\t';
for (; n >= tabsize; n -= tabsize)
2014-11-21 17:52:09 +00:00
writerune("<stdout>", stdout, &r);
r = ' ';
while (n--)
2014-11-21 17:52:09 +00:00
writerune("<stdout>", stdout, &r);
2014-06-09 15:52:20 +00:00
}
static void
2014-11-21 17:52:09 +00:00
unexpand(const char *file, FILE *fp)
2014-06-09 15:52:20 +00:00
{
unsigned int n = 0, col = 0;
2014-11-21 17:52:09 +00:00
Rune r;
int bol = 1;
2014-06-09 15:52:20 +00:00
2014-11-21 17:52:09 +00:00
while (1) {
if (!readrune(file, fp, &r))
break;
switch (r) {
2014-06-09 15:52:20 +00:00
case ' ':
if (bol || aflag)
n++;
col++;
break;
case '\t':
if (bol || aflag)
n += tabsize - col % tabsize;
col += tabsize - col % tabsize;
break;
case '\b':
if (bol || aflag)
unexpandspan(n, col);
col -= (col > 0);
n = 0;
bol = 0;
2014-06-09 15:52:20 +00:00
break;
case '\n':
if (bol || aflag)
unexpandspan(n, col);
n = col = 0;
bol = 1;
2014-06-09 15:52:20 +00:00
break;
default:
if (bol || aflag)
unexpandspan(n, col);
n = 0;
col++;
bol = 0;
2014-06-09 15:52:20 +00:00
}
2014-11-21 17:52:09 +00:00
if ((r != ' ' && r != '\t') || (!aflag && !bol))
writerune("<stdout>", stdout, &r);
2014-06-09 15:52:20 +00:00
}
if (n > 0 && (bol || aflag))
unexpandspan(n, col);
}