Adapted to use General C Identifier processing Module (GCIPM).

Also changed so that output for "cid" or "cpp" is in alphabetical order.
This commit is contained in:
ceriel 1986-11-11 13:42:42 +00:00
parent 3860ab6f68
commit 3bc3818955

View file

@ -18,9 +18,10 @@
Revised: Wed Jul 23 13:27:16 MDT 1986
by Ceriel Jacobs,
replaced "stoi" by "atoi"
Revised: Wed Oct 1 14:23:35 MDT 1986
Revised: Tue Nov 11 13:32:31 MET 1986
by Ceriel Jacobs,
to produce lists in the order in which they were read.
to produce lists for "cid" or preprocessor in
alphabetical order.
*/
#include <stdio.h>
@ -41,7 +42,7 @@ struct idf {
int maxlen = DEF_LENGTH;
int action = ACT_LISTONLY;
char *ProgName;
extern char *ProgName;
char * keywords[] = {
"asm",
@ -77,6 +78,8 @@ char * keywords[] = {
0
};
struct idf *maplist = 0;
DefineKeys()
{
register char **pkey = &keywords[0];
@ -197,7 +200,9 @@ EnHash(id)
return hash_val % (unsigned) HASHSIZE;
}
Results()
BeginOfProgram() { }
EndOfProgram()
{
register int i;
register struct idf *idp, *p;
@ -230,15 +235,44 @@ Results()
case ACT_CID:
case ACT_MAPFILE:
if (idp->id_key == 0)
mapline(idp->id_name);
for (p = idp->id_same; p; p = p->id_same)
for (p = idp->id_same; p;) {
register struct idf *q = p->id_same;
if (p->id_key == 0)
mapline(p->id_name);
saveline(p);
p = q;
}
if (idp->id_key == 0)
saveline(idp);
break;
}
}
}
switch(action) {
case ACT_CID:
case ACT_MAPFILE:
for (idp = maplist; idp; idp = idp->id_same) {
mapline(idp->id_name);
}
}
}
saveline(p)
register struct idf *p;
{
register struct idf *idp = maplist, *idp1 = 0;
while (idp && strcmp(idp->id_name, p->id_name) < 0) {
idp1 = idp;
idp = idp->id_same;
}
p->id_same = idp;
if (idp1 == 0) {
maplist = p;
}
else {
idp1->id_same = p;
}
}
mapline(nm)
@ -258,208 +292,9 @@ mapline(nm)
}
}
#define MAX_ID_LEN 256
main(argc, argv)
char *argv[];
CheckId(id, s)
char *id;
{
char **nargv;
int nargc = 0;
FILE *fp;
ProgName = *argv++;
nargv = argv;
while (--argc > 0)
if ((*argv)[0] == '-')
DoOption(*argv++);
else
nargv[nargc++] = *argv++;
DefineKeys();
if (nargc > 0)
while (nargc-- > 0) {
if ((fp = fopen(*nargv, "r")) == NULL)
fprintf(stderr, "%s: cannot read file \"%s\"\n",
ProgName, *nargv);
else
DoFile(fp);
nargv++;
}
else
DoFile(stdin);
Results();
return 0;
}
DoFile(fp)
FILE *fp;
{
register c;
while ((c = getc(fp)) != EOF)
switch (c) {
case '"':
case '\'':
SkipString(fp, c);
break;
case '/':
if ((c = getc(fp)) == '*')
SkipComment(fp);
else
ungetc(c, fp);
break;
default:
if (StartId(c))
DoIdent(fp, c);
else
if (StartNum(c))
DoNum(fp, c);
break;
}
fclose(fp);
}
SkipString(fp, stopc)
FILE *fp;
{
register c;
while ((c = getc(fp)) != EOF) {
if (c == stopc)
return;
if (c == '\\')
c = getc(fp);
}
}
SkipComment(fp)
FILE *fp;
{
register c;
while ((c = getc(fp)) != EOF)
if (c == '*') {
if ((c = getc(fp)) == '/')
return;
ungetc(c, fp);
}
}
DoIdent(fp, s)
FILE *fp;
{
char id_buf[MAX_ID_LEN];
register cnt = 1;
register c;
id_buf[0] = s;
while ((c = getc(fp)) != EOF)
if (InId(c))
id_buf[cnt++] = c;
else {
ungetc(c, fp);
id_buf[cnt] = '\0';
if (cnt >= maxlen)
InsertId(&id_buf[0], 0);
return;
}
}
#define inrange(c, l, u) ((unsigned)((c) - (l)) <= ((u) - (l)))
#define isdec(c) inrange(c, '0', '9')
#define isoct(c) inrange(c, '0', '7')
#define ishex(c) (isdec(c) || inrange(c, 'a', 'f') || inrange(c, 'A', 'F'))
#define getdec(c, fp) do c = getc((fp)); while (isdec(c))
#define getoct(c, fp) do c = getc((fp)); while (isoct(c))
#define gethex(c, fp) do c = getc((fp)); while (ishex(c))
DoNum(fp, c)
FILE *fp;
{
if (c != '0') {
getdec(c, fp);
if (c == '.')
getdec(c, fp);
if (c == 'e') {
c = getc(fp);
if (c == '+' || c == '-')
c = getc(fp);
if (isdec(c))
getdec(c, fp);
}
}
else {
c = getc(fp);
if (c == 'x' || c == 'X')
gethex(c, fp);
else
if (isoct(c))
getoct(c, fp);
}
}
StartId(c)
{
switch (c) {
case 'a': case 'b': case 'c': case 'd': case 'e':
case 'f': case 'g': case 'h': case 'i': case 'j':
case 'k': case 'l': case 'm': case 'n': case 'o':
case 'p': case 'q': case 'r': case 's': case 't':
case 'u': case 'v': case 'w': case 'x': case 'y':
case 'z':
case 'A': case 'B': case 'C': case 'D': case 'E':
case 'F': case 'G': case 'H': case 'I': case 'J':
case 'K': case 'L': case 'M': case 'N': case 'O':
case 'P': case 'Q': case 'R': case 'S': case 'T':
case 'U': case 'V': case 'W': case 'X': case 'Y':
case 'Z':
case '_':
return 1;
default:
return 0;
}
}
StartNum(c)
{
switch(c) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
return 1;
}
return 0;
}
InId(c)
{
switch (c) {
case 'a': case 'b': case 'c': case 'd': case 'e':
case 'f': case 'g': case 'h': case 'i': case 'j':
case 'k': case 'l': case 'm': case 'n': case 'o':
case 'p': case 'q': case 'r': case 's': case 't':
case 'u': case 'v': case 'w': case 'x': case 'y':
case 'z':
case 'A': case 'B': case 'C': case 'D': case 'E':
case 'F': case 'G': case 'H': case 'I': case 'J':
case 'K': case 'L': case 'M': case 'N': case 'O':
case 'P': case 'Q': case 'R': case 'S': case 'T':
case 'U': case 'V': case 'W': case 'X': case 'Y':
case 'Z':
case '_':
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
return 1;
default:
return 0;
}
if (s >= maxlen)
InsertId(id, 0);
}