ack/util/cmisc/prid.c

138 lines
2.3 KiB
C
Raw Normal View History

1994-06-24 11:31:16 +00:00
/* $Id$ */
1987-03-09 19:15:41 +00:00
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
1986-11-11 13:44:58 +00:00
/* Print IDentifiers occurring in C programs outside comment, strings
and character constants.
Flags:
-l<num> : print identifiers of <num> or more characters
(default <num> = 8)
Author: Erik Baalbergen
Date: Oct 23, 1985
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
1986-11-11 13:44:58 +00:00
extern char *ProgName;
#ifndef DEF_LENGTH
#define DEF_LENGTH 8
#endif
int maxlen = DEF_LENGTH;
void InsertId(char *);
char *Malloc(unsigned int);
char *Salloc(char *);
int EnHash(char*);
void EndOfProgram(void);
void DoOption(char*);
void CheckId(char *, int);
void BeginOfProgram(void) {}
void DoOption(char* str)
1986-11-11 13:44:58 +00:00
{
switch (str[1]) {
case 'l':
if ((maxlen = atoi(&str[2])) <= 0) {
fprintf(stderr, "%s: option \"-l%s\" ignored\n",
ProgName, &str[2]);
maxlen = DEF_LENGTH;
}
break;
default:
fprintf(stderr, "%s: bad option \"%s\"\n", ProgName, str);
break;
}
}
void CheckId(char *id, int len)
1986-11-11 13:44:58 +00:00
{
if (len >= maxlen) {
InsertId(id);
}
}
#define HASHSIZE 257
struct idf {
char *id_name;
struct idf *id_next;
};
struct idf *hash_tab[HASHSIZE];
void InsertId(char *id)
1986-11-11 13:44:58 +00:00
{
int hash_val = EnHash(id);
register struct idf *idp = hash_tab[hash_val];
register struct idf *p = 0;
while (idp && strcmp(idp->id_name, id)) {
p = idp;
idp = idp->id_next;
}
if (idp == 0) {
idp = (struct idf *) Malloc(sizeof(struct idf));
idp->id_next = 0;
if (!p) hash_tab[hash_val] = idp;
else p->id_next = idp;
idp->id_name = Salloc(id);
}
}
char *Malloc(unsigned int n)
1986-11-11 13:44:58 +00:00
{
char *mem;
1986-11-11 13:44:58 +00:00
if ((mem = malloc(n)) == 0) {
fprintf(stderr, "%s: out of memory\n", ProgName);
exit(1);
}
return mem;
}
char *Salloc(char *str)
1986-11-11 13:44:58 +00:00
{
1986-11-11 13:44:58 +00:00
if (str == 0)
str = "";
1988-10-07 10:26:37 +00:00
return strcpy(Malloc((unsigned)strlen(str) + 1), str);
1986-11-11 13:44:58 +00:00
}
int EnHash(char *id)
1986-11-11 13:44:58 +00:00
{
register unsigned hash_val = 0;
register int n = maxlen;
1986-11-11 13:44:58 +00:00
while (n-- && *id)
hash_val = 31 * hash_val + *id++;
return hash_val % (unsigned) HASHSIZE;
}
void EndOfProgram(void)
1986-11-11 13:44:58 +00:00
{
register struct idf *idp;
register int i;
for (i = 0; i < HASHSIZE; i++) {
for (idp = hash_tab[i]; idp; idp = idp->id_next) {
printf("%s\n", idp->id_name);
}
}
}