Implement -i flag for expand

This commit is contained in:
Tuukka Kataja 2014-06-09 16:54:45 +01:00 committed by sin
parent 6561567597
commit 612e09af7e
2 changed files with 22 additions and 5 deletions

View File

@ -13,6 +13,9 @@ are preserved into the output and decrement the column count for tab
calculations. calculations.
.SH OPTIONS .SH OPTIONS
.TP .TP
.BI \-i
Only change tabs to spaces at the start of lines.
.TP
.BI \-t " n" .BI \-t " n"
Expand tabs to Expand tabs to
.I n .I n

View File

@ -1,4 +1,5 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <wchar.h> #include <wchar.h>
@ -11,6 +12,8 @@ typedef struct {
static int expand(Fdescr *f, int tabstop); static int expand(Fdescr *f, int tabstop);
static bool iflag = false;
static void static void
usage(void) usage(void)
{ {
@ -26,7 +29,8 @@ main(int argc, char *argv[])
ARGBEGIN { ARGBEGIN {
case 'i': case 'i':
eprintf("not implemented\n"); iflag = true;
break;
case 't': case 't':
tabstop = estrtol(EARGF(usage()), 0); tabstop = estrtol(EARGF(usage()), 0);
break; break;
@ -77,6 +81,7 @@ expand(Fdescr *dsc, int tabstop)
{ {
int col = 0; int col = 0;
wint_t c; wint_t c;
bool bol = true;
for (;;) { for (;;) {
c = in(dsc); c = in(dsc);
@ -85,22 +90,31 @@ expand(Fdescr *dsc, int tabstop)
switch (c) { switch (c) {
case '\t': case '\t':
if (bol || !iflag) {
do { do {
col++; col++;
out(' '); out(' ');
} while (col & (tabstop - 1)); } while (col & (tabstop - 1));
} else {
out('\t');
col += tabstop - col % tabstop;
}
break; break;
case '\b': case '\b':
if (col) if (col)
col--; col--;
bol = false;
out(c); out(c);
break; break;
case '\n': case '\n':
col = 0; col = 0;
bol = true;
out(c); out(c);
break; break;
default: default:
col++; col++;
if (c != ' ')
bol = false;
out(c); out(c);
break; break;
} }