/* detab.c by Michael Hanson */
/* you may use this, but not for profit, and give me credit */ 
/* detab - converts tabs to spaces */
/* syntax detab [# # # ... ][/#] (where # signifies a non negative number )*/
/* /# specifies the number of columns between tab stops */
/* # # # ... is a list of collumns for the coresponding tab stops */
/* the default behavior is the same as detab /4 or detab 4 8 12 16 20 24 /4 */

#include "stdio.h"
#include "local.h"

#define MAXLINE 100
#define ASEP '/'


main(argc,argv)
	int argc;
	char *argv[];
{
	int err,col,tabs[MAXLINE];
	int c;
	int tabpos();

	if( ver() != 0x200 ){
		error("detab: wrong DOS version.",1);
#asm
	db 'by Michael Hanson, v1.0',1ah
#
	}
	settab(tabs,argc,argv); /* set initial tab stops */
	col=1;
	while ((c=getc(STDIN)) != EOF)
		if(c == TAB) 
			do{
				err = putc(BLANK,STDOUT);
				col++;
				}
			while(tabpos(col,tabs) != YES);
		else if(c == NEWLINE) {
			err=putc(NEWLINE,STDOUT);
			col=1;
			}
		else {
			err=putc(c,STDOUT);
			col++;
			};
	}

int tabpos(col,tabs)
  	int col ;  
	int tabs[];

	{
	if( col > MAXLINE )
		return(YES);
	else
		return(tabs[col]);
	}

settab(tabs,argc,argv)
	int tabs[];
	int argc;
	char *argv[];
	{
	int i,j,aind,tlen,npos,tpos,flg;

	tpos = 1;
	flg = 0;
	tlen = 4; 	/* default tab expan */
	for(aind = 1; aind < argc ; aind++)
		if ((argv[aind])[0] != ASEP){
			npos = atoi(argv[aind]);
			if (npos > 0){
				flg = 1;
				for( j = tpos ;j <= npos ; j++)
					tabs[j] = NO;
				tabs[(tpos = ++npos)] = YES;
				if((++ tpos) >= MAXLINE)
					return ;
				}
			}
		else{
			if( isdigit(argv[aind][1]))
				tlen = atoi(&(argv[aind])[1]);
			else
				error("syntax: detab [# # ... ] [/#].",1);
		}
	if (tlen <= 1)
		for(i=tpos ; i<= MAXLINE ; i++)
			tabs[i] = YES;
	else
/*tabs[tpos] = NO;*/
		tpos -= flg;
		for(i = tpos ; i <= MAXLINE ; i++)
			if(((i - tpos) % tlen) == 0)
				tabs[i] = YES;
			else
				tabs[i] = NO;
	}

#include "filps.c"
#include "err.c"
#include "version.c"
