sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit 612e09af7e9ff1ffca64652b6561871aec3db5de
parent 65615675973acbe985a46c37aa4828d04e2f5cf7
Author: Tuukka Kataja <stuge@xor.fi>
Date:   Mon,  9 Jun 2014 16:54:45 +0100

Implement -i flag for expand

Diffstat:
Mexpand.1 | 3+++
Mexpand.c | 24+++++++++++++++++++-----
2 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/expand.1 b/expand.1 @@ -13,6 +13,9 @@ are preserved into the output and decrement the column count for tab calculations. .SH OPTIONS .TP +.BI \-i +Only change tabs to spaces at the start of lines. +.TP .BI \-t " n" Expand tabs to .I n diff --git a/expand.c b/expand.c @@ -1,4 +1,5 @@ /* See LICENSE file for copyright and license details. */ +#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <wchar.h> @@ -11,6 +12,8 @@ typedef struct { static int expand(Fdescr *f, int tabstop); +static bool iflag = false; + static void usage(void) { @@ -26,7 +29,8 @@ main(int argc, char *argv[]) ARGBEGIN { case 'i': - eprintf("not implemented\n"); + iflag = true; + break; case 't': tabstop = estrtol(EARGF(usage()), 0); break; @@ -77,6 +81,7 @@ expand(Fdescr *dsc, int tabstop) { int col = 0; wint_t c; + bool bol = true; for (;;) { c = in(dsc); @@ -85,22 +90,31 @@ expand(Fdescr *dsc, int tabstop) switch (c) { case '\t': - do { - col++; - out(' '); - } while (col & (tabstop - 1)); + if (bol || !iflag) { + do { + col++; + out(' '); + } while (col & (tabstop - 1)); + } else { + out('\t'); + col += tabstop - col % tabstop; + } break; case '\b': if (col) col--; + bol = false; out(c); break; case '\n': col = 0; + bol = true; out(c); break; default: col++; + if (c != ' ') + bol = false; out(c); break; }