Pristine Ack-5.5
[Ack-5.5.git] / util / cmisc / cid.c
1 /* $Id: cid.c,v 0.6 1994/06/24 10:16:39 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 /*      Change IDentifiers occurring in C programs outside comment, strings
7         and character constants.
8         -Dname=text     : replace all occerences of name by text
9         -Dname          : same as -Dname=
10         -Ffile          : read sentences of the from name=text from file
11
12         Author: Erik Baalbergen
13         Date: Oct 23, 1985
14 */
15
16 #include <stdio.h>
17
18 #ifndef DEF_LENGTH
19 #define DEF_LENGTH      8
20 #endif
21
22 #define LINE_LEN        1024
23
24 #define HASHSIZE 257
25
26 struct idf {
27         struct idf *id_next;
28         char *id_name;
29         char *id_text;
30 };
31
32 struct idf *hash_tab[HASHSIZE];
33
34 char *Malloc(), *Salloc();
35 struct idf *FindId();
36
37 extern char *ProgName;
38
39 DoOption(str)
40         char *str;
41 {
42         switch (str[1]) {
43
44         case 'D':
45                 DoMacro(&str[2]);
46                 break;
47         
48         case 'F':
49                 GetMacros(&str[2]);
50                 break;
51
52         default:
53                 fprintf(stderr, "%s: bad option \"%s\"\n", ProgName, str);
54                 break;
55         }
56 }
57
58 /*ARGSUSED*/
59 CheckId(id, len)
60         char *id;
61 {
62         struct idf *idp = FindId(id);
63
64         if (idp) {
65                 printf("%s", idp->id_text);
66         }
67         else {
68                 printf("%s", id);
69         }
70 }
71
72 DoMacro(str)
73         char *str;
74 {
75         char *id, *text;
76
77         id = str++;
78
79         while (*str != '\0' && *str != '=') {
80                 str++;
81         }
82
83         if (*str == '=') {
84                 *str++ = '\0';
85                 text = str;
86         }
87         else {
88                 text = "";
89         }
90
91         InsertMacro(id, text);
92 }
93
94 GetMacros(fn)
95         char *fn;
96 {
97         FILE *fp;
98         register c;
99         char buf[LINE_LEN];
100         char *bufp = &buf[0];
101
102         if ((fp = fopen(fn, "r")) == NULL) {
103                 fprintf(stderr, "%s: cannot read file \"%s\"\n", ProgName, fn);
104                 return;
105         }
106
107         while ((c = getc(fp)) != EOF) {
108                 if (c == '\n' && bufp != &buf[0]) {
109                         *bufp = '\0';
110                         DoMacro(&buf[0]);
111                         bufp = &buf[0];
112                 }
113                 else {
114                         *bufp++ = c;
115                 }
116         }
117         fclose(fp);
118 }
119
120 InsertMacro(id, text)
121         char *id, *text;
122 {
123         int hash_val = EnHash(id);
124         struct idf *idp = hash_tab[hash_val];
125
126         while (idp) {
127                 if (strcmp(idp->id_name, id) == 0) {
128                         fprintf(stderr, "%s: (warning) redefinition of %s\n",
129                                 ProgName, id);
130                         break;
131                 }
132                 idp = idp->id_next;
133         }
134
135         if (idp == 0) {
136                 idp = (struct idf *) Malloc(sizeof(struct idf));
137         }
138
139         idp->id_next = hash_tab[hash_val];
140         idp->id_name = Salloc(id);
141         idp->id_text = Salloc(text);
142         hash_tab[hash_val] = idp;
143 }
144
145 char *
146 Malloc(n)
147         unsigned n;
148 {
149         char *mem, *malloc();
150
151         if ((mem = malloc(n)) == 0) {
152                 fprintf(stderr, "%s: out of memory\n", ProgName);
153                 exit(1);
154         }
155         return mem;
156 }
157
158 char *
159 Salloc(str)
160         char *str;
161 {
162         char *strcpy();
163
164         if (str == 0) {
165                 str = "";
166         }
167         return strcpy(Malloc((unsigned)strlen(str) + 1), str);
168 }
169
170 struct idf *
171 FindId(id)
172         char *id;
173 {
174         register hash_val = EnHash(id);
175         register struct idf *idp = hash_tab[hash_val];
176
177         while (idp) {
178                 if (strcmp(idp->id_name, id) == 0) {
179                         return idp;
180                 }
181                 idp = idp->id_next;
182         }
183         return 0;
184 }
185
186 EnHash(id)
187         char *id;
188 {
189         register unsigned hash_val = 0;
190
191         while (*id) {
192                 hash_val = 31 * hash_val + *id++;
193         }
194
195         return hash_val % (unsigned) HASHSIZE;
196 }
197
198 extern int GCcopy;
199
200 BeginOfProgram()
201 {
202         GCcopy = 1;
203 }
204
205 EndOfProgram()
206 {
207 }