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