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