home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 11 Util
/
11-Util.zip
/
TABS.ZIP
/
ENTAB.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-09-20
|
4KB
|
136 lines
/* entab [1.0.2] - 07-Dec-87 16:50 John Layman */
/*****************************************************************************\
* This filter substitutes tabs for blank substrings in a file. *
* *
* Synopsis: entab [t1..tn] *
* *
* where the optional parameter(s) indicate tab positions. *
* *
* When no parameter is present, the filter presumes the default system tab *
* settings (i.e. where column MOD 8 = 1). *
* *
* When a single parameter is present, it is considered to be the tab gap *
* (i.e. tabs are where column MOD gap = 1). *
* *
* When multiple parameters are present, they explicitly define each tab *
* position. *
* *
* To prevent the corruption of string literals in C programs, the character *
* stream is monitored for double quotes, and tab substitution is suppressed *
* within a quoted string. Quotes which are escaped, or which appear within *
* a remark are ignored. Nested remarks are supported. *
\*****************************************************************************/
#include <stdio.h>
#define DEFAULT 8
#define MAXLEN 255
#define cycle for(;;)
void chktab(), settab(), settabs();
char pos[MAXLEN]; /* tab bar, where tab stops == '\t' */
int ch, prevch;
main(argc,argv)
int argc;
char *argv[];
{
int col, comment, i, literal, mark;
if (argc == 1)
settabs(DEFAULT);
else
if (argc == 2)
settabs(atoi(argv[1]));
else
for (i = 1; i < argc; i++)
settab(atoi(argv[i]));
comment = literal = 0;
col = 1;
cycle
{
mark = col;
while ((ch = getchar()) == ' ' && !literal)
{
col++;
if (pos[col] == '\t')
{
if (col-mark > 1)
put('\t');
else
put(' ');
mark = col;
}
}
for (; mark < col; mark++)
put(' ');
if (ch == EOF)
break;
if (ch == '\t')
{
fputs("Tab encountered\n",stderr);
exit(1);
}
if (!comment)
if (prevch != '\\' && ch == '\"')
literal = !literal;
if (!literal)
if (prevch == '/' && ch == '*')
++comment;
else
if (prevch == '*' && ch == '/')
--comment;
put(ch);
if (ch == '\n')
col = 1;
else
col++;
}
}
put(ch)
int ch;
{
if (prevch == '\\')
prevch = '\0';
else
prevch = ch;
return(putchar(ch));
}
void settab(col)
int col;
{
chktab(col);
pos[col] = '\t';
}
void settabs(gap)
int gap;
{
int col;
chktab(gap);
for (col = gap+1; col <= MAXLEN; col += gap)
pos[col] = '\t';
}
void chktab(col)
int col;
{
if (col < 1 || col > MAXLEN)
{
fputs("Invalid parameter\n",stderr);
exit(1);
}
}