From 35821a8e900286fde5b28809669de4a778cc8559 Mon Sep 17 00:00:00 2001 From: stateless Date: Mon, 17 Jun 2013 14:29:13 +0100 Subject: [PATCH] Implement -t n for expand --- expand.1 | 10 +++++++++- expand.c | 23 ++++++++++++++++------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/expand.1 b/expand.1 index 6798a47..82be286 100644 --- a/expand.1 +++ b/expand.1 @@ -3,11 +3,19 @@ expand \- expand tabs to spaces .SH SYNOPSIS .B expand -.IR [file ...] +.RB [ \-t +.IR n ] +.RI [ file ...] .SH DESCRIPTION expand processes the named files or the standard input, writing the standard output with tabs changed into spaces. Backspace characters are preserved into the output and decrement the column count for tab calculations. +.SH OPTIONS +.TP +.BI \-t " n" +Expand tabs to +.I n +spaces. We currently support only a single numerical argument. .SH SEE ALSO .IR fold (1) diff --git a/expand.c b/expand.c index 77b1802..68336ec 100644 --- a/expand.c +++ b/expand.c @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include +#include #include #include "util.h" @@ -8,25 +9,33 @@ typedef struct { const char *name; } Fdescr; -static int expand(Fdescr *f); +static int expand(Fdescr *f, int tabstop); + +static void +usage(void) +{ + eprintf("usage: %s [-t n] [file...]\n", argv0); +} int main(int argc, char *argv[]) { Fdescr dsc; FILE *fp; + int tabstop = 8; ARGBEGIN { case 't': - eprintf("not yet implemented\n"); + tabstop = estrtol(EARGF(usage()), 0); + break; default: - eprintf("usage: %s [file...]\n", argv0); + usage(); } ARGEND; if (argc == 0) { dsc.name = ""; dsc.fp = stdin; - expand(&dsc); + expand(&dsc, tabstop); } else { for (; argc > 0; argc--) { if (!(fp = fopen(*argv, "r"))) { @@ -34,7 +43,7 @@ main(int argc, char *argv[]) } dsc.name = *argv; dsc.fp = fp; - expand(&dsc); + expand(&dsc, tabstop); fclose(fp); argv++; } @@ -62,7 +71,7 @@ out(wint_t c) } static int -expand(Fdescr *dsc) +expand(Fdescr *dsc, int tabstop) { int col = 0; wint_t c; @@ -77,7 +86,7 @@ expand(Fdescr *dsc) do { col++; out(' '); - } while (col & 0x7); + } while (col & (tabstop - 1)); break; case '\b': if (col) -- 1.7.10.4