Pristine Ack-5.5
[Ack-5.5.git] / util / cmisc / prid.c
1 /* $Id: prid.c,v 1.5 1994/06/24 10:16:45 ceriel Exp $ */
2 /*
3  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
4  * See the copyright notice in the ACK home directory, in the file "Copyright".
5  */
6 /*      Print IDentifiers occurring in C programs outside comment, strings
7         and character constants.
8         Flags:
9         -l<num>         : print identifiers of <num> or more characters
10                           (default <num> = 8)
11
12         Author: Erik Baalbergen
13         Date: Oct 23, 1985
14 */
15
16 #include <stdio.h>
17
18 extern char *ProgName;
19
20 #ifndef DEF_LENGTH
21 #define DEF_LENGTH      8
22 #endif
23
24 int maxlen = DEF_LENGTH;
25
26 BeginOfProgram() {}
27
28 DoOption(str)
29         char *str;
30 {
31         switch (str[1]) {
32
33         case 'l':
34                 if ((maxlen = atoi(&str[2])) <= 0) {
35                         fprintf(stderr, "%s: option \"-l%s\" ignored\n",
36                                 ProgName, &str[2]);
37                         maxlen = DEF_LENGTH;
38                 }
39                 break;
40         
41         default:
42                 fprintf(stderr, "%s: bad option \"%s\"\n", ProgName, str);
43                 break;
44         }
45 }
46
47 CheckId(id, len)
48         char *id;
49 {
50         if (len >= maxlen) {
51                 InsertId(id);
52         }
53 }
54
55 #define HASHSIZE 257
56
57 struct idf {
58         char *id_name;
59         struct idf *id_next;
60 };
61
62 struct idf *hash_tab[HASHSIZE];
63
64 char *Malloc(), *Salloc();
65
66 InsertId(id)
67         char *id;
68 {
69         int hash_val = EnHash(id);
70         register struct idf *idp = hash_tab[hash_val];
71         register struct idf *p = 0;
72
73         while (idp && strcmp(idp->id_name, id)) {
74                 p = idp;
75                 idp = idp->id_next;
76         }
77
78         if (idp == 0) {
79                 idp = (struct idf *) Malloc(sizeof(struct idf));
80                 idp->id_next = 0;
81                 if (!p) hash_tab[hash_val] = idp;
82                 else p->id_next = idp;
83                 idp->id_name = Salloc(id);
84         }
85 }
86
87 char *
88 Malloc(n)
89         unsigned n;
90 {
91         char *mem, *malloc();
92
93         if ((mem = malloc(n)) == 0) {
94                 fprintf(stderr, "%s: out of memory\n", ProgName);
95                 exit(1);
96         }
97         return mem;
98 }
99
100 char *
101 Salloc(str)
102         char *str;
103 {
104         char *strcpy();
105
106         if (str == 0)
107                 str = "";
108
109         return strcpy(Malloc((unsigned)strlen(str) + 1), str);
110 }
111
112 EnHash(id)
113         char *id;
114 {
115         register unsigned hash_val = 0;
116         register n = maxlen;
117
118         while (n-- && *id)
119                 hash_val = 31 * hash_val + *id++;
120
121         return hash_val % (unsigned) HASHSIZE;
122 }
123
124 EndOfProgram()
125 {
126         register struct idf *idp;
127         register int i;
128
129         for (i = 0; i < HASHSIZE; i++) {
130                 for (idp = hash_tab[i]; idp; idp = idp->id_next) {
131                         printf("%s\n", idp->id_name);
132                 }
133         }
134 }