File:  [CSRG BSD Unix] / 43BSDTahoe / new / B / src / b / retab.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Tue Apr 24 16:12:58 2018 UTC (8 years, 1 month ago) by root
Branches: MAIN, BSD
CVS tags: HEAD, BSD43tahoe
BSD 4.3tahoe

/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1984. */
/* $Header: /var/lib/cvsd/repos/CSRG/43BSDTahoe/new/B/src/b/retab.c,v 1.1.1.1 2018/04/24 16:12:58 root Exp $ */

/* 
 * retab [ [-ain] [-t N] ] [ file ]
 *
 * replace all tabs by appropriate number of spaces,
 * and replace these spaces by optimal number of tabs.
 *
 * -i: replace only initial spaces
 * -n: no spaces are replaced
 * -a: all spaces are replaced (Default)
 * -t N: tab positions are multiples of N; default N=8
 *
 */

#define	OPTIONS	"-[a|i|n] -tN"
#include	"par.h"
#include	<ctype.h>

#define	NL	'\n'
#define	TAB	'\t'
#define	BS	'\b'
#define	SP	' '

int tabwidth = 8;
int rpos, wpos;
char opt = 'a';

int
options(c, v) char *v[];	{
	char ch = v[0][1];

	VOID(c);
	switch (ch)	{
	case 'a':
	case 'i':
	case 'n':
		opt = ch;
		return 1;
	case 't':
		tabwidth = atoi(&v[0][2]);
		if (tabwidth <= 1)
			return ERR;
		return 1;
	default:
		return ERR;
	}
}

process()	{
	register int ch;

	rpos = wpos = 0;
	while ((ch = getc(ifile)) NE EOF)
	switch (ch)	{
	case NL:
		rpos = 0;
		wpos = 0;
		putchar(NL);
		break;
	case BS:
		rpos--;
		break;
	case TAB:
		rpos = ntab(rpos);
		break;
	case SP:
		rpos++;
		break;
	default:
		if (isprint(ch))
			rpos++;
		outchar(ch);
		break;
	}
}

outchar(ch) char ch;	{

	while (wpos > rpos-1)	{
		putchar(BS);
		wpos--;
	}
	if (opt EQ 'a' || (opt EQ 'i' && wpos EQ 0))
		while (rpos - wpos > 2 && ntab(wpos) < rpos)	{
			putchar(ntab(wpos) EQ wpos+2 ? SP : TAB);
			wpos = ntab(wpos);
		}
	while (wpos < rpos-1)	{
		putchar(SP);
		wpos++;
	}
	putchar(ch);
	wpos++;
}

int
ntab(n)	{
	return (n + tabwidth) / tabwidth * tabwidth;
}

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.