home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The C Users' Group Library 1994 August
/
wc-cdrom-cusersgrouplibrary-1994-08.iso
/
vol_200
/
213_01
/
cbr.c
< prev
next >
Wrap
Text File
|
1979-12-31
|
9KB
|
553 lines
/* CBR.C VERS:- 01.00 DATE:- 09/26/86 TIME:- 09:36:10 PM */
/*
%CC1 $1.C -O -X -E5000
%CLINK $1 DIO -S -E5000
%DELETE $1.CRL
*/
/*
Description:
Filter to format C code, from CUG distribution disk.
Indents according to control level.
Minor modifications:
adapted for BDS C v1.50a;
adapted for use as filter under DIO;
Corrected:
exit test;
read of strings of *'s.
Best to filter input file first through CBPREP, to produce
consistent spacing of text and one statment per line.
By J.A. Rupley, Tucson, Arizona
Coded for BDS C compiler, version 1.50a
*/
/* HEADER: CUG120.03;
TITLE: C beautifier;
DATE: 09/28/1981;
DESCRIPTION: "A pretty-printer for C source code.";
KEYWORDS: C programming,printing;
SYSTEM: CP/M;
FILENAME: CB.C;
CRC: 595B;
AUTHORS: William C. Colley, Rick Hollinbeck;
COMPILERS: BDS C v1.44;
*/
/*
Pretty-printer for C Programs
Swiped from the CIPG's UNIX system and modified to run
under BDS C by William C. Colley, III
Mods made July 1980
Updated to BDS C, v1.44 by Rick Hollinbeck, 9/28/81
To use the program, type the following command line:
A>cb <input.fil [>output.fil or |DIO_pipe or default = crt]
where input.fil is the file to be pretty-printed and [output.fil] is an
optional output file. If no output file is specified, the output is sent
to the console.
*/
#include "bdscio.h"
#include "dio.h"
int slevel[10];
int clevel;
int spflg[20][10];
int sind[20][10];
int siflev[10];
int sifflg[10];
int iflev;
int ifflg;
int level;
int ind[10];
int eflg;
int paren;
int pflg[10];
char lchar;
char pchar;
int aflg;
int ct;
int stabs[20][10];
int qflg;
char *wif[2];
char *welse[2];
char *wfor[2];
char *wds[3];
int j;
char string[200];
char cc;
int sflg;
int bflg;
int peek;
int tabs;
char lastchar;
char c;
main(argc, argv)
int argc;
char *argv[];
{
int k;
/* Initialize everything here. */
dioinit(&argc, argv);
if (!_dioinflg)
{
printf(
"\nUSAGE:\n");
printf(
" cb <input_fid [>out_fid or |DIO_pipe or default = crt]\n\n");
printf(
"one of three formatting filters:\n\n");
printf(
"unvanuys - converts keywords of Van Nuys Toolkit to standard K & R 'c'\n");
printf(
"cbprep - produces consistent spacing of 'c' text and one statement per line\n");
printf(
" output must be filtered by cb\n");
printf(
"cb - indents according to control level\n\n");
dioexit();
}
clevel = iflev = level = eflg = paren = 0;
aflg = qflg = j = bflg = tabs = 0;
ifflg = peek = -1;
sflg = 1;
initw(ind, "0,0,0,0,0,0,0,0,0,0");
initw(pflg, "0,0,0,0,0,0,0,0,0,0");
wif[0] = "if";
welse[0] = "else";
wfor[0] = "for";
wds[0] = "case";
wds[1] = "default";
wif[1] = welse[1] = wfor[1] = wds[2] = 0;
/* End of initialization. */
while ((c = getchr()) != 0xff)
{
switch (c)
{
default :
string[j++] = c;
if (c != ',')
lchar = c;
break;
case ' ' :
case '\t' :
if (lookup(welse) == 1)
{
gotelse();
if (sflg == 0 || j > 0)
string[j++] = c;
puts();
sflg = 0;
break;
}
if (sflg == 0 || j > 0)
string[j++] = c;
break;
case '\n' :
if (eflg = lookup(welse) == 1)
gotelse();
puts();
printf("\n");
sflg = 1;
if (eflg == 1)
{
pflg[level]++;
tabs++;
}
else
if (pchar == lchar)
aflg = 1;
break;
case '{' :
if (lookup(welse) == 1)
gotelse();
siflev[clevel] = iflev;
sifflg[clevel] = ifflg;
iflev = ifflg = 0;
clevel++;
if (sflg == 1 && pflg[level] != 0)
{
pflg[level]--;
tabs--;
}
string[j++] = c;
puts();
getnl();
puts();
printf("\n");
tabs++;
sflg = 1;
if (pflg[level] > 0)
{
ind[level] = 1;
level++;
slevel[level] = clevel;
}
break;
case '}' :
clevel--;
if ((iflev = siflev[clevel] - 1) < 0)
iflev = 0;
ifflg = sifflg[clevel];
puts();
tabs--;
ptabs();
if ((peek = getchr()) == ';')
{
printf("%c;", c);
peek = -1;
}
else
printf("%c", c);
getnl();
puts();
printf("\n");
sflg = 1;
if (clevel < slevel[level])
if (level > 0)
level--;
if (ind[level] != 0)
{
tabs -= pflg[level];
pflg[level] = 0;
ind[level] = 0;
}
break;
case '"' :
case '\'' :
string[j++] = c;
while ((cc = getchr()) != c)
{
string[j++] = cc;
if (cc == '\\')
{
string[j++] = getchr();
}
if (cc == '\n')
{
puts();
sflg = 1;
}
}
string[j++] = cc;
if (getnl() == 1)
{
lchar = cc;
peek = '\n';
}
break;
case ';' :
string[j++] = c;
puts();
if (pflg[level] > 0 && ind[level] == 0)
{
tabs -= pflg[level];
pflg[level] = 0;
}
getnl();
puts();
printf("\n");
sflg = 1;
if (iflev > 0)
if (ifflg == 1)
{
iflev--;
ifflg = 0;
}
else
iflev = 0;
break;
case '\\' :
string[j++] = c;
string[j++] = getchr();
break;
case '?' :
qflg = 1;
string[j++] = c;
break;
case ':' :
string[j++] = c;
if (qflg == 1)
{
qflg = 0;
break;
}
if (lookup(wds) == 0)
{
sflg = 0;
puts();
}
else
{
tabs--;
puts();
tabs++;
}
if ((peek = getchr()) == ';')
{
printf(";");
peek = -1;
}
getnl();
puts();
printf("\n");
sflg = 1;
break;
case '/' :
string[j++] = c;
if ((peek = getchr()) != '*')
break;
string[j++] = peek;
peek = -1;
comment();
break;
case ')' :
paren--;
string[j++] = c;
puts();
if (getnl() == 1)
{
peek = '\n';
if (paren != 0)
aflg = 1;
else
if (tabs > 0)
{
pflg[level]++;
tabs++;
ind[level] = 0;
}
}
break;
case '#' :
string[j++] = c;
while ((cc = getchr()) != '\n')
string[j++] = cc;
string[j++] = cc;
sflg = 0;
puts();
sflg = 1;
break;
case '(' :
string[j++] = c;
paren++;
if (lookup(wfor) == 1)
{
while ((c = gets()) != ';')
;
ct = 0;
cont :
while ((c = gets()) != ')')
{
if (c == '(')
ct++;
}
if (ct != 0)
{
ct--;
goto cont;
}
paren--;
puts();
if (getnl() == 1)
{
peek = '\n';
pflg[level]++;
tabs++;
ind[level] = 0;
}
break;
}
if (lookup(wif) == 1)
{
puts();
stabs[clevel][iflev] = tabs;
spflg[clevel][iflev] = pflg[level];
sind[clevel][iflev] = ind[level];
iflev++;
ifflg = 1;
}
}
}
dioflush();
exit();
}
ptabs()
{
int i;
for (i = 0; i < tabs; i++)
printf("\t");
}
getchr()
{
int temp, tempchar;
if (peek < 0 && lastchar != ' ' && lastchar != '\t')
pchar = lastchar;
lastchar = (peek < 0) ? getchar() : peek;
if (lastchar == 0xff)
{
if (pchar != '\n')
{
puts();
printf("\n");
}
dioflush();
exit();
}
else
lastchar = (lastchar & 0x7f);
peek = -1;
return (lastchar == '\r' ? getchr() : lastchar);
}
puts()
{
if (j > 0)
{
if (sflg != 0)
{
ptabs();
sflg = 0;
if (aflg == 1)
{
aflg = 0;
if (tabs > 0)
printf(" ");
}
}
string[j] = '\0';
printf("%s", string);
j = 0;
}
else
{
if (sflg != 0)
{
sflg = 0;
aflg = 0;
}
}
}
lookup(tab)
char *tab[];
{
char r;
int l, kk, k, i;
if (j < 1)
return (0);
kk = 0;
while (string[kk] == ' ')
kk++;
for (i = 0; tab[i] != 0; i++)
{
l = 0;
for (k = kk; (r = tab[i][l++]) == string[k] && r != '\0'; k++)
;
if (r == '\0' && (string[k] < 'a' || string[k] > 'z'))
return (1);
}
return (0);
}
gets()
{
char ch;
beg :
if ((ch = string[j++] = getchr()) == '\\')
{
string[j++] = getchr();
goto beg;
}
if (ch == '\'' || ch == '"')
{
while ((cc = string[j++] = getchr()) != ch)
if (cc == '\\')
string[j++] = getchr();
goto beg;
}
if (ch == '\n')
{
puts();
aflg = 1;
goto beg;
}
else
return (ch);
}
gotelse()
{
tabs = stabs[clevel][iflev];
pflg[level] = spflg[clevel][iflev];
ind[level] = sind[clevel][iflev];
ifflg = 1;
}
getnl()
{
while ((peek = getchr()) == '\t' || peek == ' ')
{
string[j++] = peek;
peek = -1;
}
if ((peek = getchr()) == '/')
{
peek = -1;
if ((peek = getchr()) == '*')
{
string[j++] = '/';
string[j++] = '*';
peek = -1;
comment();
}
else
string[j++] = '/';
}
if ((peek = getchr()) == '\n')
{
peek = -1;
return (1);
}
return (0);
}
comment()
{
int cflg;
cflg = 1;
rep :
while ((c = string[j++] = getchr()) != '*')
if (c == '\n')
{
puts();
sflg = 1;
}
if ((j > 1) && (string[j - 2] == '/'))
{
cflg++;
goto rep;
}
while ((c = string[j++] = getchr()) == '*')
;
if ((c == '/') && !(--cflg))
return;
else
{
if (c == '\n')
{
puts();
sflg = 1;
}
goto rep;
}
}