/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*    detab [ts] <file1 >file2                                         *
*                                                                     *
* DESCRIPTION                                                         *
*                                                                     *
* This program replaces tabs with spaces. The tabstop setting (column *
* width)  ts may be given on the command line; it defaults to every 8 *
* columns.  Note that this program is a "pure filter"; it only  reads *
* from  stdin, and only writes to stdout (and stderr if there is junk *
* on the command line).                                               *
*                                                                     *
* BUGS                                                                *
*                                                                     *
* There's no way to do irregular tab stops.                           *
*                                                                     *
* AUTHOR                                                              *
*                                                                     *
* John Chambers jc@eddie.mit.edu jc@trillian.mit.edu                  *
*                                                                     *
* You are free to do  what  you  wish  with  this  program.   It's  a *
* first-semester  sort  of  problem, and it's hardly worth making any *
* claims on. If you feel like adding a way to do irregular tab stops, *
* you might post the code or send me a copy.                          *
*                                                                     *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * **/
#include "V.h"

int ts = 8;	/* Tabstop setting */
int co = 1;	/* Column counter */

main(ac,av)
	char**av;
{	int x;

	for (x=1; x<ac; x++) {  /* Look for a numeric arg */
		if (sscanf(av[x],"%d",&ts) < 1) {
			fprintf(stderr,"Usage: $s [ts]\n",av[0]);
			fprintf(stderr,"\twhere ts is the tab width.\n");
			fprintf(stderr,"\tThe default is 8 columns.\n");
		}
	}
	while ((x = getchar()) != EOF) {
		switch (x) {
		case '\t':	/* Tabs are expanded here */
			while (co % ts) {
				putchar(' ');
				++co;
			}
			x = ' ';	/* One extra tab to reach the stop */
			break;
		case '\n':
			co = 0;	 /* This will immediately go to 1 */
			break;
		}
		putchar(x);	 /* Put out a space or a data char */
		++co;
	}
	exit(0);
}
