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