[mod.sources] v08i093: A tab/space conversion program

sources-request@mirror.UUCP (03/02/87)

Submitted by: Bob Larson <seismo!usc-oberon!blarson>
Mod.sources: Volume 8, Issue 93
Archive-name: tabs

Here's a fairly simple program to convert tab stops, delete trailing
spaces, optimize spaces, etc.  It's probably not to hard to convice one
(or more) standard unix utilities into doing this, but I wrote it for a
non-unix system (Os9/68k).  Probably the least portable thing about it
is it expects input on stdin, and outputs to stdout.  (I've only tested
it on Os9/68k and 4.3 bsd.) Sorry I don't have a man page for it, but I
havn't bothered to learn *roff yet.  The comment block in the begining
contains the nessisary information.

[  I wrote the manpage, based on the comments, and the Makefile.   There
   are Unix programs to do this, but not public-domain and this is
   useful.  --r$  ]

#! /bin/sh
# This is a shell archive.  Remove anything before this line,
# then unpack it by saving it in a file and typing "sh file".
# If all goes well, you will see the message "End of shell archive."
# Contents:  Makefile tabs.1 tabs.c
PATH=/bin:/usr/bin:/usr/ucb; export PATH
echo shar: extracting "'Makefile'" '(197 characters)'
if test -f 'Makefile' ; then 
  echo shar: will not over-write existing file "'Makefile'"
else
sed 's/^X//' >Makefile <<'@//E*O*F Makefile//'
Xtabs:	tabs.c
X	$(CC) $(CFLAGS) -o tabs tabs.c
X
X# Edit appropriately
XDESTDI	= /usr/local/bin
XMANDIR	= /usr/man/man1
XMANDEST	= tabs.1
Xinstall:	tabs
X	cp tabs $(DESTDIR)
X	cp tabs.1 $(MANDIR)/$(MANDEST)
@//E*O*F Makefile//
if test 197 -ne "`wc -c <'Makefile'`"; then
    echo shar: error transmitting "'Makefile'" '(should have been 197 characters)'
fi
fi # end of overwriting check
echo shar: extracting "'tabs.1'" '(1437 characters)'
if test -f 'tabs.1' ; then 
  echo shar: will not over-write existing file "'tabs.1'"
else
sed 's/^X//' >tabs.1 <<'@//E*O*F tabs.1//'
X.TH TABS 1 LOCAL
X.SH NAME
Xtabs \- canonicalize spaces and tabs
X.SH SYNOPSIS
Xtabs
X[
X.BI \-i NN
X] [
X.BI \-o NN
X] [
X.B \-t
X] <input >output
X.SH DESCRIPTION
XThis program can be used to:
X.RS
X.nf
Xconvert from one tab size to another
Xoptimize spacing/tabbing to a minimum number of characters
Xreplace tabs with spaces
Xreplace spaces with tabs
Xdelete trailing spaces/tabs on lines
X.fi
X.RE
X.PP
XIt is designed to be used as a filter, reading standard input and writing
Xto standard output.
X.PP
X.I Tabs
Xaccepts the following options:
X.IP "\-iNN"
XTabs on the input file are set every NN (default eight) spaces.
XIf NN is 0 or omitted, input tabs are not treated as special characters;
Xthis is probably only useful with the options ``-o0 and -t''.
X.IP "\-oNN"
XTabs on the output file are set every NN (default eight) spaces.
XIf NN is 0 or omitted, tabs are not used in the output file.
X.IP "\-t"
XSuppress trailing spaces/tabs from output lines.
X.PP
XOptions may be specified separately or together.  Spaces are not allowed
Xbetween the i or o option and the number following.
XThis program will always eliminate redundant spacing.  If this is all
Xthat is desired, set the input and output tab size identically.
X.SH "EXAMPLES"
X	tabs -i8 -o8 <infile >outfile
X.br
XOptimize out extra spaces, normal tab stops.
X.PP
X	tabs -i4t <infile >outfile
X.br
XConvert from Microware's silly four-character tabs to normal
Xeight-character tabs and eliminate trailing spaces.
@//E*O*F tabs.1//
if test 1437 -ne "`wc -c <'tabs.1'`"; then
    echo shar: error transmitting "'tabs.1'" '(should have been 1437 characters)'
fi
fi # end of overwriting check
echo shar: extracting "'tabs.c'" '(1711 characters)'
if test -f 'tabs.c' ; then 
  echo shar: will not over-write existing file "'tabs.c'"
else
sed 's/^X//' >tabs.c <<'@//E*O*F tabs.c//'
X/* tabs.c by Robert A. Larson
X */
X
X#include <stdio.h>
X#define inchar()	getc(stdin)
X#define outchar(c)	putc(c, stdout)
X#define TRUE	1
X#define FALSE	0
X
Xstatic unsigned otab = 8;
Xstatic unsigned lpos = 0;
Xstatic unsigned nsp  = 0;
X
Xoutspace() {
X  register int i;
X  if(otab) {
X    while(nsp>1 && nsp >= (i=otab-(lpos%otab))) {
X      outchar('\t');
X      nsp -= i;
X      lpos += i;
X    }
X  }
X  lpos += nsp;
X  while(nsp--) outchar(' ');
X  nsp = 0;
X}
X
Xmain(argc,argv)
Xint argc;
Xregister char **argv;
X{
X  register int c;
X  register unsigned itab = 8;
X  register int trailsup	 = FALSE;
X
X  while(--argc) {
X    switch(**++argv) {
X      case '-':
X	for(;;) {
X	  switch(*++*argv) {
X	    case '\0': goto nextarg;
X	    case 'i':
X	      itab = 0;
X	      while((c = *++*argv)>='0' && c<='9') {
X		itab *= 10;
X		itab += c - '0';
X	      }
X	      (*argv)--;
X	      break;
X	    case 'o':
X	      otab = 0;
X	      while((c = *++*argv)>='0' && c<='9') {
X		otab *= 10;
X		otab += c - '0';
X	      }
X	      (*argv)--;
X	      break;
X	    case 't':
X	      trailsup = TRUE;
X	      break;
X	    default:
X	      fprintf(stderr, "Unknown switch: %c\n", **argv);
X	      exit(1);
X	  }
X	}
Xnextarg: break;
X      default:
X	fprintf(stderr, "Illegal agument: %s\n", *argv);
X	exit(1);
X    }
X  }
X  for(;;){
X    switch(c = inchar()){
X      case -1:
X	if(nsp && !trailsup) outspace();  /* in case the file ends in space */
X	exit(0);
X      case ' ':
X	nsp++;
X	break;
X      case '\t':
X	if(itab) nsp += itab - ((lpos+nsp)%itab);
X	else {
X	  if(nsp) outspace();
X	  outchar(c);
X	  lpos++;
X	}
X	break;
X      case '\n':
X	if(nsp && !trailsup) outspace();
X	outchar(c);
X	lpos = nsp = 0;
X	break;
X      default:
X	if(nsp) outspace();
X	outchar(c);
X	lpos++;
X	break;
X    }
X  }
X}
@//E*O*F tabs.c//
if test 1711 -ne "`wc -c <'tabs.c'`"; then
    echo shar: error transmitting "'tabs.c'" '(should have been 1711 characters)'
fi
fi # end of overwriting check
echo shar: "End of shell archive."
exit 0