Category : Word Processors
Archive   : TABEDIT.ZIP
Filename : ENTAB.C

 
Output of file : ENTAB.C contained in archive : TABEDIT.ZIP

/*
* entab - convert spaces to tabs based on tab stops
*
* usage: entab [ -tN ] [ file(s) ]
*
* -tN Set tab stops at every N columns. Default is
* 8 unless the TABS environment variable is set.
*
* Output is placed on the standard output, and may be sent
* to a file using ">" redirection on the command line.
*
* Tab stops may also be set using an environment variable.
* The DOS command SET TABS=4 would set tabs at every 4th
* column, rather than using the normal default of 8.
*
*/

#include

/* !!!!!!!! EDIT THESE IF OPTIONS OR COMMAND NAME CHANGES !!!!!!!! */
char me[] = "entab";
char usage[] = "usage: entab [ -tN ] file(s)\n";

#define DEFTABS 8 /* default tabs every 8th column */
#define MAXTABS 120 /* maximum tabs every 120th column */
#define YES 1
#define NO 0

FILE *fopen();
char *getenv();

int tabstop; /* tab stop column increment */

main(argc,argv)
int argc;
register char **argv;
{
FILE *fp; /* input file pointer */
char *stops; /* tab stop option string */

argv++; /* skip command name */

tabstop = DEFTABS;
/* Check for tab settings in environment variable */
if ( (stops=getenv("TABS")) != NULL ) {
if ( (tabstop = atoi(stops)) <= 0 )
tabstop = DEFTABS;
}

/* Process arguments */
while ( *argv != NULL && **argv == '-') {
switch ( (*argv)[1] ) {
case 't':
stops = &(*argv)[2];
tabstop = atoi(stops);
break;
default:
fprintf(stderr, "%s: unknown option \"%s\"\n",
me, *argv);
fputs(usage, stderr);
exit(1);
}
++argv; /* process next arg */
}

if ( tabstop < 1 || tabstop >= MAXTABS ) {
fprintf(stderr, "%s: can't use tab stops -- %s\n", me, stops);
exit(1);
}

if ( *argv == NULL ) {
enprint(stdin); /* no args, use std input */
} else {
for ( ; *argv != NULL; ++argv ) {
if ( strcmp(*argv, "-") == 0 ) {
enprint(stdin);
} else if ( (fp = fopen(*argv, "r") ) == NULL) {
fprintf(stderr,"%s: can't open \"%s\"\n",
me, *argv);
exit(1);
} else {
enprint(fp);
fclose(fp);
}
}
}

exit(0);
}

enprint(fp)
FILE *fp;
{
int colpos; /* current column position */
int ch; /* current character */
char *p_ch; /* current char and pointer to it */
register char *p_space; /* start of current whitespace */
char buf[MAXTABS+2]; /* buffer for text between tabstops */

colpos = 1;
p_ch = &buf[0];
p_space = NULL;

while ( (ch = getc(fp)) != EOF ) {
*p_ch++ = ch; /* add to line buffer */

/* Remember spaces in case there is a run of them */
if ( ch == ' ' ) {
if (!p_space) p_space = p_ch - 1;
} else {
if (p_space) p_space = NULL;
}

/* Flush buffer at tab stops and end of line */
if ( ++colpos > tabstop || ch == '\n' ) {
/* Turn >1 space into a tab at tab stop */
if ( p_space && p_space != p_ch - 1 ) {
*p_space++ = '\t';
p_ch = p_space;
}
for ( p_space = &buf[0]; p_space < p_ch; p_space++ )
putc(*p_space, stdout);
colpos = 1;
p_space = NULL;
p_ch = &buf[0];
}
}

/* Flush any final partial line at EOF */
for ( p_space = &buf[0]; p_space < p_ch; p_space++ )
putc(*p_space, stdout);

}