From decwrl!ucbvax!tut.cis.ohio-state.edu!snorkelwacker!usc!cs.utexas.edu!uunet!allbery Sat Mar 10 15:35:33 PST 1990
Article 1394 of comp.sources.misc:
Path: decwrl!ucbvax!tut.cis.ohio-state.edu!snorkelwacker!usc!cs.utexas.edu!uunet!allbery
From: paw7w@uvaee.ee.Virginia.EDU (Perry A. D. Wood)
Newsgroups: comp.sources.misc
Subject: v11i019: tab - indent a file one tab, or a specified number of spaces.
Message-ID: <80858@uunet.UU.NET>
Date: 10 Mar 90 19:53:24 GMT
Sender: allbery@uunet.UU.NET
Lines: 473
Approved: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)

Posting-number: Volume 11, Issue 19
Submitted-by: paw7w@uvaee.ee.Virginia.EDU (Perry A. D. Wood)
Archive-name: tab/part01

   Here is a program I wrote several months ago.  It was my second attempt at
writing a program in C, but it works quite well nevertheless.  I have included
a copy of getopt.c for compilation if someone does not have it.  This version
getopt.c is public domain, but I did get it out of a zoo source archive.
The code has been apparently modified somewhat by Rahul Dhesi, but I don't
know in what way.
  This program will indent an input stream or file with a tab character or
a specified number of spaces.  Indented output is sent to standard output.  I 
use this program to provide a left margin on printouts so holes can be punched.
Tab is known to compile with TurboC, SYS V Unix, and BSD 4.3 Unix.  It really
should compile with virtually any C compiler.  Getopt.c is required if it
is not in the C library (it was on our Unix machine, but I needed it for DOS).
   The DOS executable will be posted to comp.binaries.ibm.pc.

                                     Perry

--------------------------------------------------------------------------------
 Perry A. D. Wood                         Phone: 804-924-6575
 Department of Electrical Engineering     Bitnet: paw7w@virginia.bitnet
 University of Virginia                   Internet: paw7w@uvaee.ee.virginia.edu
 Charlottesville, VA   22901
--------------------------------------------------------------------------------

#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	Makefile
#	README
#	getopt.c
#	tab.c
#	tab.cat
#	tab.man
#	tab.prj
# This archive created: Tue Mar  6 20:11:33 1990
export PATH; PATH=/bin:$PATH
echo shar: extracting "'Makefile'" '(404 characters)'
if test -f 'Makefile'
then
	echo shar: will not over-write existing file "'Makefile'"
else
sed 's/^	X//' << \SHAR_EOF > 'Makefile'
	X# Makefile for tab.  There are only two changes that need to be made depending
	X# on whether or not your system has getopt.
	XCC = cc -O
	XPROG = tab
	X
	X# Remove getopt.o from the following line if your system has getopt.
	XOBJ = tab.o getopt.o
	X
	X$(PROG): $(OBJ)
	X	$(CC) -o $(PROG) $(OBJ)
	X
	Xtab.o: tab.c
	X	$(CC) -c tab.c
	X
	X# Remove the follwing 2 lines if your system has getopt.
	Xgetopt.o: getopt.c
	X	$(CC) -c getopt.c
SHAR_EOF
if test 404 -ne "`wc -c < 'Makefile'`"
then
	echo shar: error transmitting "'Makefile'" '(should have been 404 characters)'
fi
fi # end of overwriting check
echo shar: extracting "'README'" '(1033 characters)'
if test -f 'README'
then
	echo shar: will not over-write existing file "'README'"
else
sed 's/^	X//' << \SHAR_EOF > 'README'
	XFiles contained in this archive:
	X
	X getopt.c   - required for Turbo C compilation
	X README     - this file
	X tab.c      - source code
	X Makefile   - Unix makefile
	X tab.man    - nroff source for tab's manual page
	X tab.cat    - formatted manual page, stripped of backspaces.  Created with;
	X                 nroff -man tab.man | col -b > tab.cat
	X tab.prj    - Turbo C project file
	X
	XTo compile;
	X
	X  (1) Unix: Use Makefile, or...
	X            cc -O -o tab tab.c             (if your system has getopt)
	X            cc -O -o tab tab.c getopt.c     (if you don't have getopt)
	X
	X  (2) MS-DOS: Use tab.prj to make the executable with Turbo C.
	X
	X
	X--------------------------------------------------------------------------------
	X Perry A. D. Wood                         Phone: 804-924-6575
	X Department of Electrical Engineering     Bitnet: paw7w@virginia.bitnet
	X University of Virginia                   Internet: paw7w@uvaee.ee.virginia.edu
	X Charlottesville, VA   22901
	X--------------------------------------------------------------------------------
SHAR_EOF
if test 1033 -ne "`wc -c < 'README'`"
then
	echo shar: error transmitting "'README'" '(should have been 1033 characters)'
fi
fi # end of overwriting check
echo shar: extracting "'getopt.c'" '(2613 characters)'
if test -f 'getopt.c'
then
	echo shar: will not over-write existing file "'getopt.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'getopt.c'
	X/* ::[[ @(#) getopt.c 1.5 89/07/02 00:18:19 ]]:: */
	X#ifndef LINT
	Xstatic char sccsid[]="::[[ @(#) getopt.c 1.5 89/07/02 00:18:19 ]]::";
	X#endif
	X
	X/*
	X * Here's something you've all been waiting for:  the AT&T public domain
	X * source for getopt(3).  It is the code which was given out at the 1985
	X * UNIFORUM conference in Dallas.  I obtained it by electronic mail
	X * directly from AT&T.  The people there assure me that it is indeed
	X * in the public domain.
	X *
	X * There is no manual page.  That is because the one they gave out at
	X * UNIFORUM was slightly different from the current System V Release 2
	X * manual page.  The difference apparently involved a note about the
	X * famous rules 5 and 6, recommending using white space between an option
	X * and its first argument, and not grouping options that have arguments.
	X * Getopt itself is currently lenient about both of these things White
	X * space is allowed, but not mandatory, and the last option in a group can
	X * have an argument.  That particular version of the man page evidently
	X * has no official existence, and my source at AT&T did not send a copy.
	X * The current SVR2 man page reflects the actual behavor of this getopt.
	X * However, I am not about to post a copy of anything licensed by AT&T.
	X */
	X
	X/*
	XMinor modifications by Rahul Dhesi 1989/03/06
	X*/
	X
	X#include <string.h>
	X#include <stdio.h>
	X
	X/* Avoid possible compiler warning if we simply redefine NULL or EOF */
	X#define XNULL   0
	X#define XEOF (-1)
	X
	X#define ERR(szz,czz) if(opterr){fprintf(stderr,"%s%s%c\n",argv[0],szz,czz);}
	X
	Xint   opterr = 1;
	Xint   optind = 1;
	Xint   optopt;
	Xchar  *optarg;
	X
	Xint
	Xgetopt(argc, argv, opts)
	Xint   argc;
	Xchar  **argv, *opts;
	X{
	X   static int sp = 1;
	X   register int c;
	X   register char *cp;
	X
	X   if(sp == 1)
	X      if(optind >= argc ||
	X         argv[optind][0] != '-' || argv[optind][1] == '\0')
	X         return(XEOF);
	X      else if(strcmp(argv[optind], "--") == XNULL) {
	X         optind++;
	X         return(XEOF);
	X      }
	X   optopt = c = argv[optind][sp];
	X   if(c == ':' || (cp=strchr(opts, c)) == XNULL) {
	X      ERR(": illegal option -- ", c);
	X      if(argv[optind][++sp] == '\0') {
	X         optind++;
	X         sp = 1;
	X      }
	X      return('?');
	X   }
	X   if(*++cp == ':') {
	X      if(argv[optind][sp+1] != '\0')
	X         optarg = &argv[optind++][sp+1];
	X      else if(++optind >= argc) {
	X         ERR(": option requires an argument -- ", c);
	X         sp = 1;
	X         return('?');
	X      } else
	X         optarg = argv[optind++];
	X      sp = 1;
	X   } else {
	X      if(argv[optind][++sp] == '\0') {
	X         sp = 1;
	X         optind++;
	X      }
	X      optarg = XNULL;
	X   }
	X   return(c);
	X}
SHAR_EOF
if test 2613 -ne "`wc -c < 'getopt.c'`"
then
	echo shar: error transmitting "'getopt.c'" '(should have been 2613 characters)'
fi
fi # end of overwriting check
echo shar: extracting "'tab.c'" '(2928 characters)'
if test -f 'tab.c'
then
	echo shar: will not over-write existing file "'tab.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'tab.c'
	X/*
	X * tab.c - insert a tab into the left margin of a file.  With the -o option
	X *         the left margin can be indented a specified number of spaces.
	X *         Reads either standard input or the named file and sends output
	X *         to standard output.
	X *
	X * Perry A. D. Wood
	X * August 14, 1989
	X * Department of Electrical Engineering
	X * University of Virginia
	X */
	X
	X#include <stdio.h>
	X
	Xmain(argc, argv)
	Xint argc;
	Xchar *argv[];
	X    {
	X    int c;               /* data variable */
	X    int j;               /* general index variable */
	X    int posit=0;         /* column position, start of line = 0 */
	X    int opt_char;        /* command line variable */
	X    int n=0;             /* # of blank spaces to indent */
	X    FILE *infile;        /* input stream */
	X    extern int optind, opterr;
	X    extern char *optarg;
	X
	X    /*
	X     *  Read the command line options
	X     */
	X
	X    opterr=1;     /* turns on the error messages printed by getopt */
	X
	X    if ((opt_char = getopt(argc, argv, "o:")) == '?')
	X        {
	X	fprintf(stderr, "Usage: tab [-o #_of_spaces] [filename]\n");
	X        exit();
	X        }
	X
	X    /* Check for a numeric argument only if the -o option was given */
	X
	X    if ((n = atoi(optarg)) == NULL && opt_char == 'o')
	X        {
	X	fprintf(stderr, "%s: non-numeric argument for option -- o\n", argv[0]);
	X	fprintf(stderr, "Usage: tab [-o #_of_spaces] [filename]\n");
	X        exit();
	X        }
	X
	X    /*
	X     * Determine if the user has specified an input file.  If no file
	X     * has been specified then use standard input.
	X     */
	X
	X    if (argv[optind] == NULL)
	X        infile = stdin;
	X
	X    else
	X        if ((infile = fopen(argv[optind], "r")) == NULL)
	X            {
	X	    fprintf(stderr, "%s: unable to open %s\n", argv[0], argv[optind]);
	X            exit();
	X	    }
	X
	X    /*
	X     * Indent the input stream, outputting to standard output
	X     */
	X
	X    switch(opt_char)
	X        {
	X        case EOF:         /* no argument so indent one tab */
	X
	X            while((c = getc(infile)) != EOF)
	X                {
	X                posit++;
	X
	X                if(c == '\n')  /* reset column position on a new line */
	X                    posit = 0;
	X
	X                if(posit == 1)  /* put a tab at the start of the line */
	X                    printf("\11%c", c);
	X                else
	X                    printf("%c", c);
	X                }
	X            break;
	X
	X        case 'o':       /* indent n spaces */
	X
	X            while((c = getc(infile)) != EOF)
	X                {
	X                posit++;
	X
	X                if(c == '\n')  /* reset column position on a new line */
	X                    posit = 0;
	X
	X                if(posit == 1)  /* put n spaces at the start of the line */
	X                    {
	X                    for(j = 0; j < n; j++)
	X                        printf(" ");
	X                    printf("%c", c);
	X                    }
	X                else
	X                    printf("%c", c);
	X                }
	X            break;
	X        }
	X    }
SHAR_EOF
if test 2928 -ne "`wc -c < 'tab.c'`"
then
	echo shar: error transmitting "'tab.c'" '(should have been 2928 characters)'
fi
fi # end of overwriting check
echo shar: extracting "'tab.cat'" '(1129 characters)'
if test -f 'tab.cat'
then
	echo shar: will not over-write existing file "'tab.cat'"
else
sed 's/^	X//' << \SHAR_EOF > 'tab.cat'
	X
	X
	X
	X     TAB(1)		       UNIX (LOCAL)			TAB(1)
	X
	X
	X
	X     NAME
	X	  tab -	indent a text file's left margin
	X
	X     SYNOPSIS
	X	  tab [-o #_of_spaces] [filename]
	X
	X     DESCRIPTION
	X	  Tab reads text from the named	file (standard input if	no
	X	  filename is given) and writes	to standard output.  The
	X	  default indentation is one tab position.  The	-o option
	X	  allows the indentation to be specified as a number of
	X	  spaces.
	X
	X	  Some examples	of usage:
	X
	X	       tab infile	  (indents one tab position)
	X	       tab -o 3	infile	  (indents 3 spaces)
	X	       tab < infile	  (reads standard input, indents one tab)
	X	       tab -o 3	< infile  (reads standard input, indents 3 spaces)
	X
	X	  To send the indented output directly to the system printer
	X	  use:
	X
	X	       tab file	| lpr (Berkeley	Unix)
	X	       tab file	| lp  (AT&T SYSV Unix)
	X
	X     SEE ALSO
	X	  cat(1), lpr(1,BSD), lp(1,SYSV)
	X
	X     AUTHOR
	X	  Perry	A. D. Wood     August 14, 1989
	X			       Department of Electrical	Engineering
	X			       University of Virginia
	X
	X
	X
	X
	X
	X
	X
	X
	X
	X
	X
	X
	X
	X
	X
	X
	X
	X
	X
	X
	X
	X
	X     Page 1					     (printed 8/14/89)
	X
	X
	X
SHAR_EOF
if test 1129 -ne "`wc -c < 'tab.cat'`"
then
	echo shar: error transmitting "'tab.cat'" '(should have been 1129 characters)'
fi
fi # end of overwriting check
echo shar: extracting "'tab.man'" '(940 characters)'
if test -f 'tab.man'
then
	echo shar: will not over-write existing file "'tab.man'"
else
sed 's/^	X//' << \SHAR_EOF > 'tab.man'
	X.TH TAB 1 LOCAL UNIX
	X.SH NAME
	Xtab \- indent a text file's left margin
	X.SH SYNOPSIS
	Xtab [-o #_of_spaces] [filename]
	X.SH DESCRIPTION
	X.I Tab
	Xreads text from the named file (standard input if no filename is given)
	Xand writes to standard output.  The default indentation is one
	Xtab position.  The 
	X.I -o
	Xoption allows the indentation to be specified as a number of spaces.
	X.P
	XSome examples of usage:
	X
	X.in +5
	X.nf
	Xtab infile         (indents one tab position)
	Xtab -o 3 infile    (indents 3 spaces)
	Xtab < infile       (reads standard input, indents one tab)
	Xtab -o 3 < infile  (reads standard input, indents 3 spaces)
	X.fi
	X.in -5
	X
	XTo send the indented output directly to the system printer use:
	X
	X.in +5
	X.nf
	Xtab file | lpr (Berkeley Unix)
	Xtab file | lp  (AT&T SYSV Unix)
	X.fi
	X.in -5
	X.SH SEE ALSO
	Xcat(1), lpr(1,BSD), lp(1,SYSV)
	X.SH AUTHOR
	X.nf
	XPerry A. D. Wood     August 14, 1989
	X.in +21
	XDepartment of Electrical Engineering
	XUniversity of Virginia
	X.fi
SHAR_EOF
if test 940 -ne "`wc -c < 'tab.man'`"
then
	echo shar: error transmitting "'tab.man'" '(should have been 940 characters)'
fi
fi # end of overwriting check
echo shar: extracting "'tab.prj'" '(15 characters)'
if test -f 'tab.prj'
then
	echo shar: will not over-write existing file "'tab.prj'"
else
sed 's/^	X//' << \SHAR_EOF > 'tab.prj'
	Xtab.c
	Xgetopt.c
SHAR_EOF
if test 15 -ne "`wc -c < 'tab.prj'`"
then
	echo shar: error transmitting "'tab.prj'" '(should have been 15 characters)'
fi
fi # end of overwriting check
#	End of shell archive
exit 0


