read.c revision 1.4 1 /* $NetBSD: read.c,v 1.4 1997/11/03 22:37:09 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
5 * Copyright (c) 1994, 1995 Jochen Pohl
6 * All Rights Reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Jochen Pohl for
19 * The NetBSD Project.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #ifndef lint
36 static char rcsid[] = "$NetBSD: read.c,v 1.4 1997/11/03 22:37:09 cgd Exp $";
37 #endif
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <ctype.h>
43 #include <limits.h>
44 #include <err.h>
45
46 #include "lint2.h"
47
48
49 /* index of current (included) source file */
50 static int srcfile;
51
52 /*
53 * The array pointed to by inpfns maps the file name indices of input files
54 * to the file name indices used in lint2
55 */
56 static short *inpfns;
57 static size_t ninpfns;
58
59 /*
60 * The array pointed to by *fnames maps file name indizes to file names.
61 * Indices of type short are used instead of pointers to save memory.
62 */
63 const char **fnames;
64 static size_t nfnames;
65
66 /*
67 * Types are shared (to save memory for the types itself) and accessed
68 * via indices (to save memory for references to types (indices are short)).
69 * To share types, a equal type must be located fast. This is done by a
70 * hash table. Access by indices is done via an array of pointers to the
71 * types.
72 */
73 typedef struct thtab {
74 const char *th_name;
75 u_short th_idx;
76 struct thtab *th_nxt;
77 } thtab_t;
78 static thtab_t **thtab; /* hash table */
79 type_t **tlst; /* array for indexed access */
80 static size_t tlstlen; /* length of tlst */
81
82 static hte_t **renametab;
83
84 /* index of current C source file (as spezified at the command line) */
85 static int csrcfile;
86
87
88 static void inperr __P((void));
89 static void setsrc __P((const char *));
90 static void setfnid __P((int, const char *));
91 static void funccall __P((pos_t *, const char *));
92 static void decldef __P((pos_t *, const char *));
93 static void usedsym __P((pos_t *, const char *));
94 static u_short inptype __P((const char *, const char **));
95 static int gettlen __P((const char *, const char **));
96 static u_short findtype __P((const char *, size_t, int));
97 static u_short storetyp __P((type_t *, const char *, size_t, int));
98 static int thash __P((const char *, size_t));
99 static char *inpqstrg __P((const char *, const char **));
100 static const char *inpname __P((const char *, const char **));
101 static int getfnidx __P((const char *));
102
103 void
104 readfile(name)
105 const char *name;
106 {
107 FILE *inp;
108 size_t len;
109 const char *cp;
110 char *line, *eptr, rt;
111 int cline, isrc, iline;
112 pos_t pos;
113
114 if (inpfns == NULL)
115 inpfns = xcalloc(ninpfns = 128, sizeof (short));
116 if (fnames == NULL)
117 fnames = xcalloc(nfnames = 256, sizeof (char *));
118 if (tlstlen == 0)
119 tlst = xcalloc(tlstlen = 256, sizeof (type_t *));
120 if (thtab == NULL)
121 thtab = xcalloc(THSHSIZ2, sizeof (thtab_t));
122
123 _inithash(&renametab);
124
125 srcfile = getfnidx(name);
126
127 if ((inp = fopen(name, "r")) == NULL)
128 err(1, "cannot open %s", name);
129
130 while ((line = fgetln(inp, &len)) != NULL) {
131
132 if (len == 0 || line[len - 1] != '\n')
133 inperr();
134 line[len - 1] = '\0';
135 cp = line;
136
137 /* line number in csrcfile */
138 cline = (int)strtol(cp, &eptr, 10);
139 if (cp == eptr) {
140 cline = -1;
141 } else {
142 cp = eptr;
143 }
144
145 /* record type */
146 if (*cp != '\0') {
147 rt = *cp++;
148 } else {
149 inperr();
150 }
151
152 if (rt == 'S') {
153 setsrc(cp);
154 continue;
155 } else if (rt == 's') {
156 setfnid(cline, cp);
157 continue;
158 }
159
160 /*
161 * Index of (included) source file. If this index is
162 * different from csrcfile, it refers to an included
163 * file.
164 */
165 isrc = (int)strtol(cp, &eptr, 10);
166 if (cp == eptr)
167 inperr();
168 cp = eptr;
169 isrc = inpfns[isrc];
170
171 /* line number in isrc */
172 if (*cp++ != '.')
173 inperr();
174 iline = (int)strtol(cp, &eptr, 10);
175 if (cp == eptr)
176 inperr();
177 cp = eptr;
178
179 pos.p_src = (u_short)csrcfile;
180 pos.p_line = (u_short)cline;
181 pos.p_isrc = (u_short)isrc;
182 pos.p_iline = (u_short)iline;
183
184 /* process rest of this record */
185 switch (rt) {
186 case 'c':
187 funccall(&pos, cp);
188 break;
189 case 'd':
190 decldef(&pos, cp);
191 break;
192 case 'u':
193 usedsym(&pos, cp);
194 break;
195 default:
196 inperr();
197 }
198
199 }
200
201 _destroyhash(renametab);
202
203 if (ferror(inp))
204 err(1, "read error on %s", name);
205
206 (void)fclose(inp);
207 }
208
209
210 static void
211 inperr()
212 {
213 errx(1, "input file error: %s", fnames[srcfile]);
214 }
215
216 /*
217 * Set the name of the C source file of the .ln file which is
218 * currently read.
219 */
220 static void
221 setsrc(cp)
222 const char *cp;
223 {
224 csrcfile = getfnidx(cp);
225 }
226
227 /*
228 * setfnid() gets as input an index as used in an input file and the
229 * associated file name. If neccessary, it creates a new lint2 file
230 * name index for this file name and creates the mapping of the index
231 * as used in the input file to the index used in lint2.
232 */
233 static void
234 setfnid(fid, cp)
235 int fid;
236 const char *cp;
237 {
238 if (fid == -1)
239 inperr();
240
241 if (fid >= ninpfns) {
242 inpfns = xrealloc(inpfns, (ninpfns * 2) * sizeof (short));
243 (void)memset(inpfns + ninpfns, 0, ninpfns * sizeof (short));
244 ninpfns *= 2;
245 }
246 /*
247 * Should always be true because indices written in the output
248 * file by lint1 are always the previous index + 1.
249 */
250 if (fid >= ninpfns)
251 errx(1, "internal error: setfnid()");
252 inpfns[fid] = (u_short)getfnidx(cp);
253 }
254
255 /*
256 * Process a function call record (c-record).
257 */
258 static void
259 funccall(posp, cp)
260 pos_t *posp;
261 const char *cp;
262 {
263 arginf_t *ai, **lai;
264 char c, *eptr;
265 int rused, rdisc;
266 hte_t *hte;
267 fcall_t *fcall;
268 const char *name;
269
270 fcall = xalloc(sizeof (fcall_t));
271 STRUCT_ASSIGN(fcall->f_pos, *posp);
272
273 /* read flags */
274 rused = rdisc = 0;
275 lai = &fcall->f_args;
276 while ((c = *cp) == 'u' || c == 'i' || c == 'd' ||
277 c == 'z' || c == 'p' || c == 'n' || c == 's') {
278 cp++;
279 switch (c) {
280 case 'u':
281 if (rused || rdisc)
282 inperr();
283 rused = 1;
284 break;
285 case 'i':
286 if (rused || rdisc)
287 inperr();
288 break;
289 case 'd':
290 if (rused || rdisc)
291 inperr();
292 rdisc = 1;
293 break;
294 case 'z':
295 case 'p':
296 case 'n':
297 case 's':
298 ai = xalloc(sizeof (arginf_t));
299 ai->a_num = (int)strtol(cp, &eptr, 10);
300 if (cp == eptr)
301 inperr();
302 cp = eptr;
303 if (c == 'z') {
304 ai->a_pcon = ai->a_zero = 1;
305 } else if (c == 'p') {
306 ai->a_pcon = 1;
307 } else if (c == 'n') {
308 ai->a_ncon = 1;
309 } else {
310 ai->a_fmt = 1;
311 ai->a_fstrg = inpqstrg(cp, &cp);
312 }
313 *lai = ai;
314 lai = &ai->a_nxt;
315 break;
316 }
317 }
318 fcall->f_rused = rused;
319 fcall->f_rdisc = rdisc;
320
321 /* read name of function */
322 name = inpname(cp, &cp);
323
324 /* first look it up in the renaming table, then in the normal table */
325 hte = _hsearch(renametab, name, 0);
326 if (hte != NULL)
327 hte = hte->h_hte;
328 else
329 hte = hsearch(name, 1);
330 hte->h_used = 1;
331
332 fcall->f_type = inptype(cp, &cp);
333
334 *hte->h_lcall = fcall;
335 hte->h_lcall = &fcall->f_nxt;
336
337 if (*cp != '\0')
338 inperr();
339 }
340
341 /*
342 * Process a declaration or definition (d-record).
343 */
344 static void
345 decldef(posp, cp)
346 pos_t *posp;
347 const char *cp;
348 {
349 sym_t *symp, sym;
350 char c, *ep, *pos1;
351 int used, renamed;
352 hte_t *hte, *renamehte;
353 const char *name, *rename;
354
355 (void)memset(&sym, 0, sizeof (sym));
356 STRUCT_ASSIGN(sym.s_pos, *posp);
357 sym.s_def = NODECL;
358
359 used = 0;
360
361 while ((c = *cp) == 't' || c == 'd' || c == 'e' || c == 'u' ||
362 c == 'r' || c == 'o' || c == 's' || c == 'v' ||
363 c == 'P' || c == 'S') {
364 cp++;
365 switch (c) {
366 case 't':
367 if (sym.s_def != NODECL)
368 inperr();
369 sym.s_def = TDEF;
370 break;
371 case 'd':
372 if (sym.s_def != NODECL)
373 inperr();
374 sym.s_def = DEF;
375 break;
376 case 'e':
377 if (sym.s_def != NODECL)
378 inperr();
379 sym.s_def = DECL;
380 break;
381 case 'u':
382 if (used)
383 inperr();
384 used = 1;
385 break;
386 case 'r':
387 if (sym.s_rval)
388 inperr();
389 sym.s_rval = 1;
390 break;
391 case 'o':
392 if (sym.s_osdef)
393 inperr();
394 sym.s_osdef = 1;
395 break;
396 case 's':
397 if (sym.s_static)
398 inperr();
399 sym.s_static = 1;
400 break;
401 case 'v':
402 if (sym.s_va)
403 inperr();
404 sym.s_va = 1;
405 sym.s_nva = (short)strtol(cp, &ep, 10);
406 if (cp == ep)
407 inperr();
408 cp = ep;
409 break;
410 case 'P':
411 if (sym.s_prfl)
412 inperr();
413 sym.s_prfl = 1;
414 sym.s_nprfl = (short)strtol(cp, &ep, 10);
415 if (cp == ep)
416 inperr();
417 cp = ep;
418 break;
419 case 'S':
420 if (sym.s_scfl)
421 inperr();
422 sym.s_scfl = 1;
423 sym.s_nscfl = (short)strtol(cp, &ep, 10);
424 if (cp == ep)
425 inperr();
426 cp = ep;
427 break;
428 }
429 }
430
431 /* read symbol name, doing renaming if necessary */
432 name = inpname(cp, &cp);
433 renamed = 0;
434 if (*cp == 'r') {
435 cp++;
436 name = xstrdup(name);
437 rename = inpname(cp, &cp);
438
439 /* enter it and see if it's already been renamed */
440 renamehte = _hsearch(renametab, name, 1);
441 if (renamehte->h_hte == NULL) {
442 hte = hsearch(rename, 1);
443 renamehte->h_hte = hte;
444 renamed = 1;
445 } else if (strcmp((hte = renamehte->h_hte)->h_name, rename)) {
446 pos1 = xstrdup(mkpos(&renamehte->h_syms->s_pos));
447 /* %s renamed multiple times\t%s :: %s */
448 msg(18, name, pos1, mkpos(&sym.s_pos));
449 free(pos1);
450 }
451 free((char *)name);
452 } else {
453 /* it might be a previously-done rename */
454 hte = _hsearch(renametab, name, 0);
455 if (hte != NULL)
456 hte = hte->h_hte;
457 else
458 hte = hsearch(name, 1);
459 }
460 hte->h_used |= used;
461 if (sym.s_def == DEF || sym.s_def == TDEF)
462 hte->h_def = 1;
463
464 sym.s_type = inptype(cp, &cp);
465
466 /*
467 * Allocate memory for this symbol only if it was not already
468 * declared or tentatively defined at the same location with
469 * the same type. Works only for symbols with external linkage,
470 * because static symbols, tentatively defined at the same location
471 * but in different translation units are really different symbols.
472 */
473 for (symp = hte->h_syms; symp != NULL; symp = symp->s_nxt) {
474 if (symp->s_pos.p_isrc == sym.s_pos.p_isrc &&
475 symp->s_pos.p_iline == sym.s_pos.p_iline &&
476 symp->s_type == sym.s_type &&
477 ((symp->s_def == DECL && sym.s_def == DECL) ||
478 (!sflag && symp->s_def == TDEF && sym.s_def == TDEF)) &&
479 !symp->s_static && !sym.s_static) {
480 break;
481 }
482 }
483
484 if (symp == NULL) {
485 /* allocsym reserviert keinen Platz fuer s_nva */
486 if (sym.s_va || sym.s_prfl || sym.s_scfl) {
487 symp = xalloc(sizeof (sym_t));
488 STRUCT_ASSIGN(*symp, sym);
489 } else {
490 symp = xalloc(sizeof (symp->s_s));
491 STRUCT_ASSIGN(symp->s_s, sym.s_s);
492 }
493 *hte->h_lsym = symp;
494 hte->h_lsym = &symp->s_nxt;
495
496 /* XXX hack so we can remember where a symbol was renamed */
497 if (renamed)
498 renamehte->h_syms = symp;
499 }
500
501 if (*cp != '\0')
502 inperr();
503 }
504
505 /*
506 * Read an u-record (emited by lint1 if a symbol was used).
507 */
508 static void
509 usedsym(posp, cp)
510 pos_t *posp;
511 const char *cp;
512 {
513 usym_t *usym;
514 hte_t *hte;
515 const char *name;
516
517 usym = xalloc(sizeof (usym_t));
518 STRUCT_ASSIGN(usym->u_pos, *posp);
519
520 /* needed as delimiter between two numbers */
521 if (*cp++ != 'x')
522 inperr();
523
524 name = inpname(cp, &cp);
525 hte = _hsearch(renametab, name, 0);
526 if (hte != NULL)
527 hte = hte->h_hte;
528 else
529 hte = hsearch(name, 1);
530 hte->h_used = 1;
531
532 *hte->h_lusym = usym;
533 hte->h_lusym = &usym->u_nxt;
534 }
535
536 /*
537 * Read a type and return the index of this type.
538 */
539 static u_short
540 inptype(cp, epp)
541 const char *cp, **epp;
542 {
543 char c, s, *eptr;
544 const char *ep;
545 type_t *tp;
546 int narg, i, osdef;
547 size_t tlen;
548 u_short tidx;
549 int h;
550
551 /* If we have this type already, return it's index. */
552 tlen = gettlen(cp, &ep);
553 h = thash(cp, tlen);
554 if ((tidx = findtype(cp, tlen, h)) != 0) {
555 *epp = ep;
556 return (tidx);
557 }
558
559 /* No, we must create a new type. */
560 tp = xalloc(sizeof (type_t));
561
562 tidx = storetyp(tp, cp, tlen, h);
563
564 c = *cp++;
565
566 while (c == 'c' || c == 'v') {
567 if (c == 'c') {
568 tp->t_const = 1;
569 } else {
570 tp->t_volatile = 1;
571 }
572 c = *cp++;
573 }
574
575 if (c == 's' || c == 'u' || c == 'l' || c == 'e') {
576 s = c;
577 c = *cp++;
578 } else {
579 s = '\0';
580 }
581
582 switch (c) {
583 case 'C':
584 tp->t_tspec = s == 's' ? SCHAR : (s == 'u' ? UCHAR : CHAR);
585 break;
586 case 'S':
587 tp->t_tspec = s == 'u' ? USHORT : SHORT;
588 break;
589 case 'I':
590 tp->t_tspec = s == 'u' ? UINT : INT;
591 break;
592 case 'L':
593 tp->t_tspec = s == 'u' ? ULONG : LONG;
594 break;
595 case 'Q':
596 tp->t_tspec = s == 'u' ? UQUAD : QUAD;
597 break;
598 case 'D':
599 tp->t_tspec = s == 's' ? FLOAT : (s == 'l' ? LDOUBLE : DOUBLE);
600 break;
601 case 'V':
602 tp->t_tspec = VOID;
603 break;
604 case 'P':
605 tp->t_tspec = PTR;
606 break;
607 case 'A':
608 tp->t_tspec = ARRAY;
609 break;
610 case 'F':
611 case 'f':
612 osdef = c == 'f';
613 tp->t_tspec = FUNC;
614 break;
615 case 'T':
616 tp->t_tspec = s == 'e' ? ENUM : (s == 's' ? STRUCT : UNION);
617 break;
618 }
619
620 switch (tp->t_tspec) {
621 case ARRAY:
622 tp->t_dim = (int)strtol(cp, &eptr, 10);
623 cp = eptr;
624 tp->t_subt = TP(inptype(cp, &cp));
625 break;
626 case PTR:
627 tp->t_subt = TP(inptype(cp, &cp));
628 break;
629 case FUNC:
630 c = *cp;
631 if (isdigit((u_char)c)) {
632 if (!osdef)
633 tp->t_proto = 1;
634 narg = (int)strtol(cp, &eptr, 10);
635 cp = eptr;
636 tp->t_args = xcalloc((size_t)(narg + 1),
637 sizeof (type_t *));
638 for (i = 0; i < narg; i++) {
639 if (i == narg - 1 && *cp == 'E') {
640 tp->t_vararg = 1;
641 cp++;
642 } else {
643 tp->t_args[i] = TP(inptype(cp, &cp));
644 }
645 }
646 }
647 tp->t_subt = TP(inptype(cp, &cp));
648 break;
649 case ENUM:
650 tp->t_tspec = INT;
651 tp->t_isenum = 1;
652 /* FALLTHROUGH */
653 case STRUCT:
654 case UNION:
655 switch (*cp++) {
656 case '1':
657 tp->t_istag = 1;
658 tp->t_tag = hsearch(inpname(cp, &cp), 1);
659 break;
660 case '2':
661 tp->t_istynam = 1;
662 tp->t_tynam = hsearch(inpname(cp, &cp), 1);
663 break;
664 case '3':
665 tp->t_isuniqpos = 1;
666 tp->t_uniqpos.p_line = strtol(cp, &eptr, 10);
667 cp = eptr;
668 cp++;
669 /* xlate to 'global' file name. */
670 tp->t_uniqpos.p_file =
671 addoutfile(inpfns[strtol(cp, &eptr, 10)]);
672 cp = eptr;
673 cp++;
674 tp->t_uniqpos.p_uniq = strtol(cp, &eptr, 10);
675 cp = eptr;
676 break;
677 }
678 break;
679 /* LINTED (enumeration value(s) not handled in switch) */
680 }
681
682 *epp = cp;
683 return (tidx);
684 }
685
686 /*
687 * Get the length of a type string.
688 */
689 static int
690 gettlen(cp, epp)
691 const char *cp, **epp;
692 {
693 const char *cp1;
694 char c, s, *eptr;
695 tspec_t t;
696 int narg, i, cm, vm;
697
698 cp1 = cp;
699
700 c = *cp++;
701
702 cm = vm = 0;
703
704 while (c == 'c' || c == 'v') {
705 if (c == 'c') {
706 if (cm)
707 inperr();
708 cm = 1;
709 } else {
710 if (vm)
711 inperr();
712 vm = 1;
713 }
714 c = *cp++;
715 }
716
717 if (c == 's' || c == 'u' || c == 'l' || c == 'e') {
718 s = c;
719 c = *cp++;
720 } else {
721 s = '\0';
722 }
723
724 t = NOTSPEC;
725
726 switch (c) {
727 case 'C':
728 if (s == 's') {
729 t = SCHAR;
730 } else if (s == 'u') {
731 t = UCHAR;
732 } else if (s == '\0') {
733 t = CHAR;
734 }
735 break;
736 case 'S':
737 if (s == 'u') {
738 t = USHORT;
739 } else if (s == '\0') {
740 t = SHORT;
741 }
742 break;
743 case 'I':
744 if (s == 'u') {
745 t = UINT;
746 } else if (s == '\0') {
747 t = INT;
748 }
749 break;
750 case 'L':
751 if (s == 'u') {
752 t = ULONG;
753 } else if (s == '\0') {
754 t = LONG;
755 }
756 break;
757 case 'Q':
758 if (s == 'u') {
759 t = UQUAD;
760 } else if (s == '\0') {
761 t = QUAD;
762 }
763 break;
764 case 'D':
765 if (s == 's') {
766 t = FLOAT;
767 } else if (s == 'l') {
768 t = LDOUBLE;
769 } else if (s == '\0') {
770 t = DOUBLE;
771 }
772 break;
773 case 'V':
774 if (s == '\0')
775 t = VOID;
776 break;
777 case 'P':
778 if (s == '\0')
779 t = PTR;
780 break;
781 case 'A':
782 if (s == '\0')
783 t = ARRAY;
784 break;
785 case 'F':
786 case 'f':
787 if (s == '\0')
788 t = FUNC;
789 break;
790 case 'T':
791 if (s == 'e') {
792 t = ENUM;
793 } else if (s == 's') {
794 t = STRUCT;
795 } else if (s == 'u') {
796 t = UNION;
797 }
798 break;
799 default:
800 inperr();
801 }
802
803 if (t == NOTSPEC)
804 inperr();
805
806 switch (t) {
807 case ARRAY:
808 (void)strtol(cp, &eptr, 10);
809 if (cp == eptr)
810 inperr();
811 cp = eptr;
812 (void)gettlen(cp, &cp);
813 break;
814 case PTR:
815 (void)gettlen(cp, &cp);
816 break;
817 case FUNC:
818 c = *cp;
819 if (isdigit((u_char)c)) {
820 narg = (int)strtol(cp, &eptr, 10);
821 cp = eptr;
822 for (i = 0; i < narg; i++) {
823 if (i == narg - 1 && *cp == 'E') {
824 cp++;
825 } else {
826 (void)gettlen(cp, &cp);
827 }
828 }
829 }
830 (void)gettlen(cp, &cp);
831 break;
832 case ENUM:
833 case STRUCT:
834 case UNION:
835 switch (*cp++) {
836 case '1':
837 (void)inpname(cp, &cp);
838 break;
839 case '2':
840 (void)inpname(cp, &cp);
841 break;
842 case '3':
843 /* unique position: line.file.uniquifier */
844 (void)strtol(cp, &eptr, 10);
845 if (cp == eptr)
846 inperr();
847 cp = eptr;
848 if (*cp++ != '.')
849 inperr();
850 (void)strtol(cp, &eptr, 10);
851 if (cp == eptr)
852 inperr();
853 cp = eptr;
854 if (*cp++ != '.')
855 inperr();
856 (void)strtol(cp, &eptr, 10);
857 if (cp == eptr)
858 inperr();
859 cp = eptr;
860 break;
861 default:
862 inperr();
863 }
864 break;
865 /* LINTED (enumeration value(s) not handled in switch) */
866 }
867
868 *epp = cp;
869 return (cp - cp1);
870 }
871
872 /*
873 * Search a type by it's type string.
874 */
875 static u_short
876 findtype(cp, len, h)
877 const char *cp;
878 size_t len;
879 int h;
880 {
881 thtab_t *thte;
882
883 for (thte = thtab[h]; thte != NULL; thte = thte->th_nxt) {
884 if (strncmp(thte->th_name, cp, len) != 0)
885 continue;
886 if (thte->th_name[len] == '\0')
887 return (thte->th_idx);
888 }
889
890 return (0);
891 }
892
893 /*
894 * Store a type and it's type string so we can later share this type
895 * if we read the same type string from the input file.
896 */
897 static u_short
898 storetyp(tp, cp, len, h)
899 type_t *tp;
900 const char *cp;
901 size_t len;
902 int h;
903 {
904 /* 0 ist reserved */
905 static u_int tidx = 1;
906 thtab_t *thte;
907 char *name;
908
909 if (tidx >= USHRT_MAX)
910 errx(1, "sorry, too many types");
911
912 if (tidx == tlstlen - 1) {
913 tlst = xrealloc(tlst, (tlstlen * 2) * sizeof (type_t *));
914 (void)memset(tlst + tlstlen, 0, tlstlen * sizeof (type_t *));
915 tlstlen *= 2;
916 }
917
918 tlst[tidx] = tp;
919
920 /* create a hash table entry */
921 name = xalloc(len + 1);
922 (void)memcpy(name, cp, len);
923 name[len] = '\0';
924
925 thte = xalloc(sizeof (thtab_t));
926 thte->th_name = name;
927 thte->th_idx = tidx;
928 thte->th_nxt = thtab[h];
929 thtab[h] = thte;
930
931 return ((u_short)tidx++);
932 }
933
934 /*
935 * Hash function for types
936 */
937 static int
938 thash(s, len)
939 const char *s;
940 size_t len;
941 {
942 u_int v;
943
944 v = 0;
945 while (len-- != 0) {
946 v = (v << sizeof (v)) + (u_char)*s++;
947 v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v));
948 }
949 return (v % THSHSIZ2);
950 }
951
952 /*
953 * Read a string enclosed by "". This string may contain quoted chars.
954 */
955 static char *
956 inpqstrg(src, epp)
957 const char *src, **epp;
958 {
959 char *strg, *dst;
960 size_t slen;
961 int c;
962 int v;
963
964 dst = strg = xmalloc(slen = 32);
965
966 if ((c = *src++) != '"')
967 inperr();
968 if ((c = *src++) == '\0')
969 inperr();
970
971 while (c != '"') {
972 if (c == '\\') {
973 if ((c = *src++) == '\0')
974 inperr();
975 switch (c) {
976 case 'n':
977 c = '\n';
978 break;
979 case 't':
980 c = '\t';
981 break;
982 case 'v':
983 #ifdef __STDC__
984 c = '\v';
985 #else
986 c = '\013';
987 #endif
988 break;
989 case 'b':
990 c = '\b';
991 break;
992 case 'r':
993 c = '\r';
994 break;
995 case 'f':
996 c = '\f';
997 break;
998 case 'a':
999 #ifdef __STDC__
1000 c = '\a';
1001 #else
1002 c = '\007';
1003 #endif
1004 break;
1005 case '\\':
1006 c = '\\';
1007 break;
1008 case '"':
1009 c = '"';
1010 break;
1011 case '\'':
1012 c = '\'';
1013 break;
1014 case '0': case '1': case '2': case '3':
1015 v = (c - '0') << 6;
1016 if ((c = *src++) < '0' || c > '7')
1017 inperr();
1018 v |= (c - '0') << 3;
1019 if ((c = *src++) < '0' || c > '7')
1020 inperr();
1021 v |= c - '0';
1022 c = (u_char)v;
1023 break;
1024 default:
1025 inperr();
1026 }
1027 }
1028 /* keep space for trailing '\0' */
1029 if (dst - strg == slen - 1) {
1030 strg = xrealloc(strg, slen * 2);
1031 dst = strg + (slen - 1);
1032 slen *= 2;
1033 }
1034 *dst++ = (char)c;
1035 if ((c = *src++) == '\0')
1036 inperr();
1037 }
1038 *dst = '\0';
1039
1040 *epp = src;
1041 return (strg);
1042 }
1043
1044 /*
1045 * Read the name of a symbol in static memory.
1046 */
1047 static const char *
1048 inpname(cp, epp)
1049 const char *cp, **epp;
1050 {
1051 static char *buf;
1052 static size_t blen = 0;
1053 size_t len, i;
1054 char *eptr, c;
1055
1056 len = (int)strtol(cp, &eptr, 10);
1057 if (cp == eptr)
1058 inperr();
1059 cp = eptr;
1060 if (len + 1 > blen)
1061 buf = xrealloc(buf, blen = len + 1);
1062 for (i = 0; i < len; i++) {
1063 c = *cp++;
1064 if (!isalnum(c) && c != '_')
1065 inperr();
1066 buf[i] = c;
1067 }
1068 buf[i] = '\0';
1069
1070 *epp = cp;
1071 return (buf);
1072 }
1073
1074 /*
1075 * Return the index of a file name. If the name cannot be found, create
1076 * a new entry and return the index of the newly created entry.
1077 */
1078 static int
1079 getfnidx(fn)
1080 const char *fn;
1081 {
1082 int i;
1083
1084 /* 0 ist reserved */
1085 for (i = 1; fnames[i] != NULL; i++) {
1086 if (strcmp(fnames[i], fn) == 0)
1087 break;
1088 }
1089 if (fnames[i] != NULL)
1090 return (i);
1091
1092 if (i == nfnames - 1) {
1093 fnames = xrealloc(fnames, (nfnames * 2) * sizeof (char *));
1094 (void)memset(fnames + nfnames, 0, nfnames * sizeof (char *));
1095 nfnames *= 2;
1096 }
1097
1098 fnames[i] = xstrdup(fn);
1099 return (i);
1100 }
1101
1102 /*
1103 * Separate symbols with static and external linkage.
1104 */
1105 void
1106 mkstatic(hte)
1107 hte_t *hte;
1108 {
1109 sym_t *sym1, **symp, *sym;
1110 fcall_t **callp, *call;
1111 usym_t **usymp, *usym;
1112 hte_t *nhte;
1113 int ofnd;
1114
1115 /* Look for first static definition */
1116 for (sym1 = hte->h_syms; sym1 != NULL; sym1 = sym1->s_nxt) {
1117 if (sym1->s_static)
1118 break;
1119 }
1120 if (sym1 == NULL)
1121 return;
1122
1123 /* Do nothing if this name is used only in one translation unit. */
1124 ofnd = 0;
1125 for (sym = hte->h_syms; sym != NULL && !ofnd; sym = sym->s_nxt) {
1126 if (sym->s_pos.p_src != sym1->s_pos.p_src)
1127 ofnd = 1;
1128 }
1129 for (call = hte->h_calls; call != NULL && !ofnd; call = call->f_nxt) {
1130 if (call->f_pos.p_src != sym1->s_pos.p_src)
1131 ofnd = 1;
1132 }
1133 for (usym = hte->h_usyms; usym != NULL && !ofnd; usym = usym->u_nxt) {
1134 if (usym->u_pos.p_src != sym1->s_pos.p_src)
1135 ofnd = 1;
1136 }
1137 if (!ofnd) {
1138 hte->h_used = 1;
1139 /* errors about undef. static symbols are printed in lint1 */
1140 hte->h_def = 1;
1141 hte->h_static = 1;
1142 return;
1143 }
1144
1145 /*
1146 * Create a new hash table entry
1147 *
1148 * XXX this entry should be put at the beginning of the list to
1149 * avoid to process the same symbol twice.
1150 */
1151 for (nhte = hte; nhte->h_link != NULL; nhte = nhte->h_link) ;
1152 nhte->h_link = xmalloc(sizeof (hte_t));
1153 nhte = nhte->h_link;
1154 nhte->h_name = hte->h_name;
1155 nhte->h_static = 1;
1156 nhte->h_used = 1;
1157 nhte->h_def = 1; /* error in lint1 */
1158 nhte->h_lsym = &nhte->h_syms;
1159 nhte->h_lcall = &nhte->h_calls;
1160 nhte->h_lusym = &nhte->h_usyms;
1161
1162 /*
1163 * move all symbols used in this translation unit into the new
1164 * hash table entry.
1165 */
1166 for (symp = &hte->h_syms; (sym = *symp) != NULL; ) {
1167 if (sym->s_pos.p_src == sym1->s_pos.p_src) {
1168 sym->s_static = 1;
1169 (*symp) = sym->s_nxt;
1170 if (hte->h_lsym == &sym->s_nxt)
1171 hte->h_lsym = symp;
1172 sym->s_nxt = NULL;
1173 *nhte->h_lsym = sym;
1174 nhte->h_lsym = &sym->s_nxt;
1175 } else {
1176 symp = &sym->s_nxt;
1177 }
1178 }
1179 for (callp = &hte->h_calls; (call = *callp) != NULL; ) {
1180 if (call->f_pos.p_src == sym1->s_pos.p_src) {
1181 (*callp) = call->f_nxt;
1182 if (hte->h_lcall == &call->f_nxt)
1183 hte->h_lcall = callp;
1184 call->f_nxt = NULL;
1185 *nhte->h_lcall = call;
1186 nhte->h_lcall = &call->f_nxt;
1187 } else {
1188 callp = &call->f_nxt;
1189 }
1190 }
1191 for (usymp = &hte->h_usyms; (usym = *usymp) != NULL; ) {
1192 if (usym->u_pos.p_src == sym1->s_pos.p_src) {
1193 (*usymp) = usym->u_nxt;
1194 if (hte->h_lusym == &usym->u_nxt)
1195 hte->h_lusym = usymp;
1196 usym->u_nxt = NULL;
1197 *nhte->h_lusym = usym;
1198 nhte->h_lusym = &usym->u_nxt;
1199 } else {
1200 usymp = &usym->u_nxt;
1201 }
1202 }
1203
1204 /* h_def must be recalculated for old hte */
1205 hte->h_def = nhte->h_def = 0;
1206 for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
1207 if (sym->s_def == DEF || sym->s_def == TDEF) {
1208 hte->h_def = 1;
1209 break;
1210 }
1211 }
1212
1213 mkstatic(hte);
1214 }
1215