Start development on 386BSD 0.0
[unix-history] / .ref-BSD-4_3_Net_2 / usr / src / contrib / isode / pepsy / dfns.c
CommitLineData
9319b3c3
C
1/* dfns.c */
2
3#ifndef lint
4static char *rcsid = "$Header: /f/osi/pepsy/RCS/dfns.c,v 7.3 91/02/22 09:48:46 mrose Interim $";
5#endif
6
7/*
8 * $Header: /f/osi/pepsy/RCS/dfns.c,v 7.3 91/02/22 09:48:46 mrose Interim $
9 *
10 *
11 * $Log: dfns.c,v $
12 * Revision 7.3 91/02/22 09:48:46 mrose
13 * Interim 6.8
14 *
15 * Revision 7.2 90/12/23 18:42:03 mrose
16 * update
17 *
18 * Revision 7.1 90/11/04 19:18:19 mrose
19 * update
20 *
21 * Revision 7.0 90/07/01 19:54:14 mrose
22 * *** empty log message ***
23 *
24 */
25
26/*
27 * NOTICE
28 *
29 * Acquisition, use, and distribution of this module and related
30 * materials are subject to the restrictions of a license agreement.
31 * Consult the Preface in the User's Manual for the full terms of
32 * this agreement.
33 *
34 */
35
36
37#include <stdio.h>
38#include "general.h"
39#include "mine.h"
40#include "pepsydefs.h"
41#include "pass2.h"
42
43id_entry *id_table[TABLESIZE];
44
45#define my_error(mesg) (fprintf(stderr, "%s\n",mesg),exit(1))
46
47extern char *notidtoid(), *my_new_str(), *my_strcat();
48extern char *
49proc_name(), *mymodule;
50
51/*
52 * Lookup the hash table (id_table) for the string t and insert it at
53 * the start of the appropriate chain if it is not already there.
54 * The argument flag indicates whether t is being defined (1) or used
55 * (0).
56 */
57char *
58proc_name(t, flag)
59char *t;
60int flag;
61{
62 int i;
63 static int curr = 0;
64 id_entry *ptr;
65
66 i = hash_val(t);
67 for (ptr = id_table[i]; ptr != NULL && strcmp(t, ptr->h_value); ptr = ptr->next);
68
69 if (ptr == NULL) {
70 if ((ptr = (id_entry *) malloc(sizeof(id_entry))) == NULL)
71 my_error("proc_name: Out of memory");
72 ptr->h_value = t;
73 ptr->r_value = my_strcat(notidtoid(t), notidtoid(mymodule));
74 ptr->count = 1;
75 if (flag) {
76 ptr->def_bit = flag;
77 ptr->def_value = curr++;
78 }
79 ptr->next = id_table[i];
80 id_table[i] = ptr;
81 } else if (!ptr->def_bit)
82 ptr->def_bit = flag;
83
84 return ptr->r_value;
85}
86
87/*
88 * output a sequence of #define statements (one for each value stored
89 * in the hash table) to the file specified by fp
90 */
91out_final_defs(fp)
92FILE *fp;
93{
94 int j;
95 id_entry *ptr;
96
97 for (j = 0; j < TABLESIZE; j++)
98 for (ptr = id_table[j]; ptr != NULL; ptr = ptr->next) {
99 if (ptr->def_bit)
100 (void) fprintf(fp, "#define %s%s\t%d\n", PREFIX, ptr->r_value, ptr->def_value);
101 else
102 ferrs(0, "the identifier %s is used but not defined\n", ptr->h_value);
103 if (ptr->count > 1) /* not used */
104 (void) printf("The id %s has a count of %d\n", ptr->r_value, ptr->count);
105 }
106}
107
108/*
109 * return a copy of the string s with '-' replaced by '_'
110 */
111char *
112notidtoid(s)
113char *s;
114{
115
116 char *t, *r;
117
118 t = my_new_str(s);
119 for (r = t; *r != '\0'; r++)
120 if (*r == '-')
121 *r = '_';
122 return t;
123}
124
125/*
126 * return a copy of the string s
127 */
128char *
129my_new_str(s)
130char *s;
131{
132
133 char *t;
134
135 if ((t = (char *) malloc(strlen(s) + 1)) == NULL)
136 my_error("my_new_str: Out of memory");
137
138 strcpy(t, s);
139 return t;
140}
141
142/*
143 * return the concatenation of the strings s1 and s2
144 */
145char *
146my_strcat(s1, s2)
147char *s1, *s2;
148{
149 char *s3, *s, *t;
150
151 if (s1 == NULL || *s1 == '\0')
152 return my_new_str(s2);
153
154 if ((s3 = (char *) malloc(strlen(s1) + strlen(s2) + 1)) == NULL)
155 my_error("my_strcat: Out of memory");
156
157 for (s = s1, t = s3; *s != '\0'; s++, t++)
158 *t = *s;
159 strcpy(t, s2);
160 return s3;
161}
162
163/*
164 * a simple hash function
165 */
166hash_val(s)
167char *s;
168{
169 int i, sum;
170 char *t;
171
172 sum = 0;
173 for (i = 1, t = s; *t != '\0'; i++, t++)
174 sum = sum + *t * i;
175 return (sum % TABLESIZE);
176}
177
178/*
179 * initialize the table id_table
180 */
181init()
182{
183 int i;
184
185 for (i = 0; i <= TABLESIZE; i++)
186 id_table[i] = NULL;
187}
188#define BUFSIZE 128
189#define MAX(a, b) ((a) > (b) ? (a) : (b))
190
191static char *buf = NULL;
192static int len = 0;
193
194/*
195 * Return in a static buffer the two strings concatenated
196 */
197char *
198concat(s1, s2)
199char *s1, *s2;
200{
201 int tot;
202
203 tot = strlen(s1) + strlen(s2) + 1;
204
205 if (tot > len) {
206 len = MAX(BUFSIZE, tot);
207 if (buf == NULL) {
208 if ((buf = malloc(len)) == NULL)
209 ferr(1, "concat:malloc failed\n");
210 } else if ((buf = realloc(buf, len)) == NULL)
211 ferr(1, "concat:realloc failed\n");
212 }
213 strcpy(buf, s1);
214 strcat(buf, s2);
215
216 return (buf);
217}
218
219/*
220 * Generate a free call given the name of the parameter, the module
221 * name, and the name of the type
222 */
223char *
224gfree(module, id, parm)
225char *module; /* name of module we are in (usually
226 * mymodule) */
227char *id; /* name of type we want to free */
228char *parm; /* name of the pointer to the data */
229{
230 char *p1 = notidtoid(module);
231 char *p2 = notidtoid(id);
232
233 if (buf == NULL) {
234 if ((buf = malloc(BUFSIZE)) == NULL)
235 ferr(1, "gfree:malloc failed\n");
236 len = BUFSIZ;
237 }
238 (void) sprintf(buf, "(void) fre_obj((char *) %s, %s%s%s.md_dtab[%s%s%s], &%s%s%s, 1)",
239 parm,
240 PREFIX, p1, MODTYP_SUFFIX,
241 PREFIX, p2, p1,
242 PREFIX, p1, MODTYP_SUFFIX);
243 free(p1);
244 free(p2);
245
246 return (buf);
247}