?? cb105.c
字號:
#define VERSION "1.05"
/*
To use the program type the following command line:
A>cb input.fil [output.fil]
Where input.fil is the file to be pretty printed and [output.fil]
is the destination file. If no output file is specified, then
the output goes to the input file name and the old inputfile is
renamed with .BAK
*
* WISH LIST
* - Fix to handle array initialization format w/o indentation
*
*
* REVISION HISTORY
*
* 1.05 18 Aug 1986 by Whitfield Gregg
* 1. show input and output file names
* 2. change 'tabs' to 3 spaces for
* compatibility with Brief editor
* 3. make output file date same as input
*
* 1.04 13 Aug 1986 by Whitfield Gregg
* 1. change inputfile/outputfile options
* for automatic backup etc.
*
* 1.03 10 Aug 1986 by Whitfield Gregg
* 1. update for Lattice Ver 3.0
* 2. show version number on startup
*
* 1.02 14 March 85 1720 Alan Cox
* 1. Fix to eliminate Lattice warnings due to missmatched variables
* 2. Change to own personal C style
* 1.01 30 October 83 John W. Kindschi Jr.
* 1. Modified for Lattice C
* 1.00 Swiped from CPIG'S UNIX system and modified to
* run under BDS C by William C. Colley, III
*
*/
#include <stdio.h>
long getfdate(char *);
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 eflg;
int paren;
int aflg;
int ct;
int stabs[20][10];
int qflg;
int j;
int sflg;
int bflg;
int peek;
int tabs;
FILE *f1, *f2;
static int ind[10] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
static int pflg[10] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
char lchar;
char pchar;
char *wif[2];
char *welse[2];
char *wfor[2];
char *wds[3];
char cc;
char string[500];
int lastchar;
int c;
main(argc,argv)
int argc;
char *argv[];
{
char infile[40], outfile[40], stemp[40];
long time;
if (argc < 2 || argc > 3)
{
char drive[3],path[64],node[9],ext[4];
strsfn(argv[0],drive,path,node,ext);
printf("\nUsage: %s input.fil [output.fil]\n",node);
exit();
}
strcpy(infile,argv[1]);
if (stpchr(infile,'.')==NULL) strcat(infile,".c");
if (argc>2) strcpy(outfile,argv[2]);
else strmfe(outfile,infile,"$$$");
if ((f1 = fopen(infile,"r")) == NULL)
{
printf("File %s not found\n\n",infile);
exit();
}
if ((f2 = fopen(outfile,"w")) == NULL)
{
printf("Couldn't create output file %s\n",outfile);
exit();
}
printf("\nCB ");
printf(VERSION);
printf("\n\t%s --> %s\n",infile,(argc==2)?infile:outfile);
clevel = iflev = level = eflg = paren = 0;
aflg = qflg = j = bflg = tabs = 0;
ifflg = peek = -1;
sflg = 1;
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()) != EOF)
{
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;
puts0();
sflg = 0;
break;
}
if (sflg == 0 || j > 0) string[j++] = c;
break;
case '\n':
if (eflg = lookup(welse) == 1) gotelse();
puts0();
print ("\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;
tabs++;
puts0();
getnl();
puts0();
print("\n");
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];
puts0();
ptabs();
tabs--;
if ((peek = getchr()) == ';')
{
print("%c;",c);
peek = -1;
}
else print("%c",c);
getnl();
puts0();
print("\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')
{
puts0();
sflg = 1;
}
}
string[j++] = cc;
if (getnl() == 1)
{
lchar = cc;
peek = '\n';
}
break;
case ';':
string[j++] = c;
puts0();
if (pflg[level] > 0 && ind[level] == 0)
{
tabs -= pflg[level];
pflg[level] = 0;
}
getnl();
puts0();
print("\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;
puts0();
}
else
{
tabs--;
puts0();
tabs++;
}
if ((peek = getchr()) == ';')
{
print(";");
peek = -1;
}
getnl();
puts0();
print("\n");
sflg = 1;
break;
case '/':
string[j++] = c;
if ((peek = getchr()) != '*') break;
string[j++] = peek;
peek = -1;
comment();
break;
case ')':
paren--;
string[j++] = c;
puts0();
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;
puts0();
sflg = 1;
break;
case '(':
string[j++] = c;
paren++;
if (lookup(wfor) == 1)
{
while ( (c = gets0() ) != ';' )
;
ct = 0;
cont:
while ( (c = gets0() ) != ')' )
{
if (c == '(') ct++;
}
if (ct != 0)
{
ct--;
goto cont;
}
paren--;
puts0();
if (getnl() == 1)
{
peek = '\n';
pflg[level]++;
tabs++;
ind[level] = 0;
}
break;
}
if ( lookup(wif) == 1)
{
puts0();
stabs[clevel][iflev] = tabs;
spflg[clevel][iflev] = pflg[level];
sind[clevel][iflev] = ind[level];
iflev++;
ifflg = 1;
}
/* end of case '(' */
} /* end of switch (c) */
} /* end of while c = getchr() */
fcloseal();
if (argc!=2) return(0);
time=getfdate(infile);
strmfe(stemp,infile,"bak");
remove(stemp);
if (rename(infile,stemp))
{
printf("\noutput in file %s\n",outfile);
return(0);
}
rename(outfile,infile);
setfdate(infile,time);
setfdate(stemp,time);
} /* end of main() */
ptabs()
{
int i;
for (i=0; i < tabs; i++) print(" "); /* 3 spaces */
}
getchr()
{
if (peek<0 && lastchar != ' ' && lastchar != '\t')pchar = lastchar;
lastchar = (peek < 0) ? getc(f1) : peek;
peek = -1;
return(lastchar == '\r' ? getchr() : lastchar);
}
puts0()
{
if (j > 0)
{
if (sflg != 0)
{
ptabs();
sflg = 0;
if (aflg == 1)
{
aflg = 0;
if (tabs > 0) print (" ");
}
}
string[j] = '\0';
print("%s",string);
j = 0;
}
else
{
if (sflg != 0)
{
sflg = 0;
aflg = 0;
}
}
}
lookup(tab)
char *tab[];
{
char r;
int i,kk,k,l;
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);
}
gets0()
{
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')
{
puts0();
aflg = 1;
goto beg;
}
else return( (int) 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()
{
rep:
while ((c = string[j++] = getchr()) != '*')
if (c == '\n')
{
puts0();
sflg = 1;
}
gotstar:
if ((c = string[j++] = getchr()) != '/')
{
if (c == '*') goto gotstar;
goto rep;
}
}
print(fmt,args)
char *fmt;
unsigned args;
{
fprintf(f2,fmt,args);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -