home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gold Fish 1
/
GoldFishApril1994_CD1.img
/
d1xx
/
d179
/
unixutil
/
entab.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-02-25
|
3KB
|
140 lines
/* entab.c - compress spaces to tabs in a file. If a single tabstop *
* is given, tabs are tabstop spaces apart, otherwise tabstops *
* are at <tab1>, <tab2>,..., <tabn>. If no file is specified, *
* standard input is read and standard output written. *
* *
* entab [-<tab1> -<tab2> -<tabn>] [filename... ] *
* *
* entab (C) 1988 by Gary L. Brant *
* *
* :ts=8 */
#define MAXLINE 1000
#include <stdio.h>
char tabarray[MAXLINE];
main (argc, argv) /* remove trailing blanks and tabs and */
/* compress blanks in source lines */
int argc;
char *argv[];
{
int i = 0, j, k, l, ntabs = 0, tabstop = 8;
int argtabs[99];
char ch, *pch;
FILE *ifile, *fopen ();
/* decode tabstop arguments */
while ((++i < argc) && (argv[i][0] == '-')) {
j = 1;
tabstop = 0;
while ((ch = argv[i][j++]) != '\0') {
if (ch >= '0' && ch <= '9') {
tabstop *= 10;
tabstop += ch - '0';
} else {
fputs ("bad args\n", stderr);
exit (20);
}
}
argtabs[ntabs++] = tabstop;
}
/* fill tabarray with \1 at each tabstop position */
for (k = 0; k < MAXLINE; k++)
tabarray[k] = '\0';
if (ntabs > 1)
for (k = 0; k < ntabs; k++)
if ((l = argtabs[k]-1) < MAXLINE)
tabarray[l] = '\001';
else {
fputs ("bad tab specification\n", stderr);
exit (20);
}
else if ((tabstop > 0) && (tabstop < MAXLINE))
for (k = tabstop; k < MAXLINE; k += tabstop)
tabarray[k] = '\001';
else {
fputs ("bad tab specification\n", stderr);
exit (20);
}
/* remaining arguments should be file names - entab them */
if (i < argc)
while (i < argc)
if ((ifile = fopen ((pch = argv[i++]), "r")) == NULL) {
fputs ("entab: cant open ", stderr);
fputs (pch, stderr);
putc ('\n', stderr);
exit (20);
} else
entab (ifile);
else
entab (stdin);
}
/* entab - insert tabs into one file */
entab (ifile)
FILE *ifile;
{
int n;
char inline[MAXLINE], outline[MAXLINE], *fgets ();
while ((fgets (inline, MAXLINE, ifile)) != NULL) {
n = strlen (inline);
while (--n >= 0) /* back over white space */
if (inline[n] != ' ' && inline[n] != '\t' && inline[n] != '\n')
break;
inline[n+1] = '\0';
compress (inline, outline, MAXLINE);
puts (outline);
}
}
/* compress - compress one line, replacing strings of blanks with tabs *
* to tab stops specified on command line or default */
compress (in, out, lim)
char in[], out[];
int lim;
{
register int i = 0, j = 0;
register char ch;
while (((ch = in[i++]) != '\0') && (i < lim)) {
if (ch == ' ') {
register int k = i, tc;
while (((tc = tabarray[k]) == '\0') && (in[k] == ' ') && (k < lim))
k++;
if ((tc == '\001') && (k > i))
out[j++] = '\t';
else /* avoid running through this again next trip */
while (i++ <= k)
out[j++] = ch;
i = k;
} else if (ch == '\042' || ch == '\047') {
register int tc; /* avoid tabbing quoted strings */
out[j++] = ch;
while (((tc = in[i++]) != ch) && (i < lim)) {
if (tc == '\134') { /* possible protected quote */
out[j++] = tc;
tc = in[i++];
if (i == lim) break;
}
out[j++] = tc;
}
out[j++] = ch;
} else
out[j++] = ch;
}
out[j] = ch;
}
void
_wb_parse ()
{
}