xref: /netbsd-src/usr.bin/xlint/lint2/read.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /* $NetBSD: read.c,v 1.27 2017/12/26 17:02:19 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.27 2017/12/26 17:02:19 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 	case ULONG:
717 	case UINT:
718 	case INT:
719 	case USHORT:
720 	case SHORT:
721 	case UCHAR:
722 	case SCHAR:
723 	case CHAR:
724 	case BOOL:
725 	case UNSIGN:
726 	case SIGNED:
727 	case NOTSPEC:
728 	case FCOMPLEX:
729 	case DCOMPLEX:
730 	case LCOMPLEX:
731 	case COMPLEX:
732 		break;
733 	case NTSPEC:
734 		abort();
735 	}
736 
737 	*epp = cp;
738 	return (tidx);
739 }
740 
741 /*
742  * Get the length of a type string.
743  */
744 static int
745 gettlen(const char *cp, const char **epp)
746 {
747 	const	char *cp1;
748 	char	c, s, *eptr;
749 	tspec_t	t;
750 	int	narg, i, cm, vm;
751 
752 	cp1 = cp;
753 
754 	c = *cp++;
755 
756 	cm = vm = 0;
757 
758 	while (c == 'c' || c == 'v') {
759 		if (c == 'c') {
760 			if (cm)
761 				inperr("cm: %c", c);
762 			cm = 1;
763 		} else {
764 			if (vm)
765 				inperr("vm: %c", c);
766 			vm = 1;
767 		}
768 		c = *cp++;
769 	}
770 
771 	switch (c) {
772 	case 's':
773 	case 'u':
774 	case 'l':
775 	case 'e':
776 		s = c;
777 		c = *cp++;
778 		break;
779 	default:
780 		s = '\0';
781 		break;
782 	}
783 
784 	t = NOTSPEC;
785 
786 	switch (c) {
787 	case 'B':
788 		if (s == '\0')
789 			t = BOOL;
790 		break;
791 	case 'C':
792 		if (s == 's') {
793 			t = SCHAR;
794 		} else if (s == 'u') {
795 			t = UCHAR;
796 		} else if (s == '\0') {
797 			t = CHAR;
798 		}
799 		break;
800 	case 'S':
801 		if (s == 'u') {
802 			t = USHORT;
803 		} else if (s == '\0') {
804 			t = SHORT;
805 		}
806 		break;
807 	case 'I':
808 		if (s == 'u') {
809 			t = UINT;
810 		} else if (s == '\0') {
811 			t = INT;
812 		}
813 		break;
814 	case 'L':
815 		if (s == 'u') {
816 			t = ULONG;
817 		} else if (s == '\0') {
818 			t = LONG;
819 		}
820 		break;
821 	case 'Q':
822 		if (s == 'u') {
823 			t = UQUAD;
824 		} else if (s == '\0') {
825 			t = QUAD;
826 		}
827 		break;
828 	case 'D':
829 		if (s == 's') {
830 			t = FLOAT;
831 		} else if (s == 'l') {
832 			t = LDOUBLE;
833 		} else if (s == '\0') {
834 			t = DOUBLE;
835 		}
836 		break;
837 	case 'V':
838 		if (s == '\0')
839 			t = VOID;
840 		break;
841 	case 'P':
842 		if (s == '\0')
843 			t = PTR;
844 		break;
845 	case 'A':
846 		if (s == '\0')
847 			t = ARRAY;
848 		break;
849 	case 'F':
850 	case 'f':
851 		if (s == '\0')
852 			t = FUNC;
853 		break;
854 	case 'T':
855 		if (s == 'e') {
856 			t = ENUM;
857 		} else if (s == 's') {
858 			t = STRUCT;
859 		} else if (s == 'u') {
860 			t = UNION;
861 		}
862 		break;
863 	case 'X':
864 		if (s == 's') {
865 			t = FCOMPLEX;
866 		} else if (s == 'l') {
867 			t = LCOMPLEX;
868 		} else if (s == '\0') {
869 			t = DCOMPLEX;
870 		}
871 		break;
872 	default:
873 		inperr("bad type: %c %c", c, s);
874 	}
875 
876 	if (t == NOTSPEC) {
877 		inperr("undefined type: %c %c", c, s);
878 	}
879 
880 	switch (t) {
881 	case ARRAY:
882 		(void)strtol(cp, &eptr, 10);
883 		if (cp == eptr)
884 			inperr("bad number: %s", cp);
885 		cp = eptr;
886 		(void)gettlen(cp, &cp);
887 		break;
888 	case PTR:
889 		(void)gettlen(cp, &cp);
890 		break;
891 	case FUNC:
892 		c = *cp;
893 		if (isdigit((u_char)c)) {
894 			narg = (int)strtol(cp, &eptr, 10);
895 			cp = eptr;
896 			for (i = 0; i < narg; i++) {
897 				if (i == narg - 1 && *cp == 'E') {
898 					cp++;
899 				} else {
900 					(void)gettlen(cp, &cp);
901 				}
902 			}
903 		}
904 		(void)gettlen(cp, &cp);
905 		break;
906 	case ENUM:
907 	case STRUCT:
908 	case UNION:
909 		switch (*cp++) {
910 		case '1':
911 			(void)inpname(cp, &cp);
912 			break;
913 		case '2':
914 			(void)inpname(cp, &cp);
915 			break;
916 		case '3':
917 			/* unique position: line.file.uniquifier */
918 			(void)strtol(cp, &eptr, 10);
919 			if (cp == eptr)
920 				inperr("bad number: %s", cp);
921 			cp = eptr;
922 			if (*cp++ != '.')
923 				inperr("not dot: %c", cp[-1]);
924 			(void)strtol(cp, &eptr, 10);
925 			if (cp == eptr)
926 				inperr("bad number: %s", cp);
927 			cp = eptr;
928 			if (*cp++ != '.')
929 				inperr("not dot: %c", cp[-1]);
930 			(void)strtol(cp, &eptr, 10);
931 			if (cp == eptr)
932 				inperr("bad number: %s", cp);
933 			cp = eptr;
934 			break;
935 		default:
936 			inperr("bad value: %c\n", cp[-1]);
937 		}
938 		break;
939 	case FLOAT:
940 	case USHORT:
941 	case SHORT:
942 	case UCHAR:
943 	case SCHAR:
944 	case CHAR:
945 	case BOOL:
946 	case UNSIGN:
947 	case SIGNED:
948 	case NOTSPEC:
949 	case INT:
950 	case UINT:
951 	case DOUBLE:
952 	case LDOUBLE:
953 	case VOID:
954 	case ULONG:
955 	case QUAD:
956 	case UQUAD:
957 	case LONG:
958 	case FCOMPLEX:
959 	case DCOMPLEX:
960 	case LCOMPLEX:
961 	case COMPLEX:
962 		break;
963 #ifndef __COVERITY__
964 	case NTSPEC:
965 		abort();
966 #endif
967 	}
968 
969 	*epp = cp;
970 	return (cp - cp1);
971 }
972 
973 /*
974  * Search a type by its type string.
975  */
976 static u_short
977 findtype(const char *cp, size_t len, int h)
978 {
979 	thtab_t	*thte;
980 
981 	for (thte = thtab[h]; thte != NULL; thte = thte->th_nxt) {
982 		if (strncmp(thte->th_name, cp, len) != 0)
983 			continue;
984 		if (thte->th_name[len] == '\0')
985 			return (thte->th_idx);
986 	}
987 
988 	return (0);
989 }
990 
991 /*
992  * Store a type and its type string so we can later share this type
993  * if we read the same type string from the input file.
994  */
995 static u_short
996 storetyp(type_t *tp, const char *cp, size_t len, int h)
997 {
998 	static	u_int	tidx = 1;	/* 0 is reserved */
999 	thtab_t	*thte;
1000 	char	*name;
1001 
1002 	if (tidx >= USHRT_MAX)
1003 		errx(1, "sorry, too many types");
1004 
1005 	if (tidx == tlstlen - 1) {
1006 		tlst = xrealloc(tlst, (tlstlen * 2) * sizeof (type_t *));
1007 		(void)memset(tlst + tlstlen, 0, tlstlen * sizeof (type_t *));
1008 		tlstlen *= 2;
1009 	}
1010 
1011 	tlst[tidx] = tp;
1012 
1013 	/* create a hash table entry */
1014 	name = xalloc(len + 1);
1015 	(void)memcpy(name, cp, len);
1016 	name[len] = '\0';
1017 
1018 	thte = xalloc(sizeof (thtab_t));
1019 	thte->th_name = name;
1020 	thte->th_idx = tidx;
1021 	thte->th_nxt = thtab[h];
1022 	thtab[h] = thte;
1023 
1024 	return ((u_short)tidx++);
1025 }
1026 
1027 /*
1028  * Hash function for types
1029  */
1030 static int
1031 thash(const char *s, size_t len)
1032 {
1033 	u_int	v;
1034 
1035 	v = 0;
1036 	while (len-- != 0) {
1037 		v = (v << sizeof (v)) + (u_char)*s++;
1038 		v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v));
1039 	}
1040 	return (v % THSHSIZ2);
1041 }
1042 
1043 /*
1044  * Read a string enclosed by "". This string may contain quoted chars.
1045  */
1046 static char *
1047 inpqstrg(const char *src, const char **epp)
1048 {
1049 	char	*strg, *dst;
1050 	size_t	slen;
1051 	int	c;
1052 	int	v;
1053 
1054 	dst = strg = xmalloc(slen = 32);
1055 
1056 	if ((c = *src++) != '"')
1057 		inperr("not quote: %c", c);
1058 	if ((c = *src++) == '\0')
1059 		inperr("trailing data: %c", c);
1060 
1061 	while (c != '"') {
1062 		if (c == '\\') {
1063 			if ((c = *src++) == '\0')
1064 				inperr("missing after \\");
1065 			switch (c) {
1066 			case 'n':
1067 				c = '\n';
1068 				break;
1069 			case 't':
1070 				c = '\t';
1071 				break;
1072 			case 'v':
1073 				c = '\v';
1074 				break;
1075 			case 'b':
1076 				c = '\b';
1077 				break;
1078 			case 'r':
1079 				c = '\r';
1080 				break;
1081 			case 'f':
1082 				c = '\f';
1083 				break;
1084 			case 'a':
1085 				c = '\a';
1086 				break;
1087 			case '\\':
1088 				c = '\\';
1089 				break;
1090 			case '"':
1091 				c = '"';
1092 				break;
1093 			case '\'':
1094 				c = '\'';
1095 				break;
1096 			case '0': case '1': case '2': case '3':
1097 				v = (c - '0') << 6;
1098 				if ((c = *src++) < '0' || c > '7')
1099 					inperr("not octal: %c", c);
1100 				v |= (c - '0') << 3;
1101 				if ((c = *src++) < '0' || c > '7')
1102 					inperr("not octal: %c", c);
1103 				v |= c - '0';
1104 				c = (u_char)v;
1105 				break;
1106 			default:
1107 				inperr("bad \\ escape: %c", c);
1108 			}
1109 		}
1110 		/* keep space for trailing '\0' */
1111 		if ((size_t)(dst - strg) == slen - 1) {
1112 			strg = xrealloc(strg, slen * 2);
1113 			dst = strg + (slen - 1);
1114 			slen *= 2;
1115 		}
1116 		*dst++ = (char)c;
1117 		if ((c = *src++) == '\0')
1118 			inperr("missing closing quote");
1119 	}
1120 	*dst = '\0';
1121 
1122 	*epp = src;
1123 	return (strg);
1124 }
1125 
1126 /*
1127  * Read the name of a symbol in static memory.
1128  */
1129 static const char *
1130 inpname(const char *cp, const char **epp)
1131 {
1132 	static	char	*buf;
1133 	static	size_t	blen = 0;
1134 	size_t	len, i;
1135 	char	*eptr, c;
1136 
1137 	len = (int)strtol(cp, &eptr, 10);
1138 	if (cp == eptr)
1139 		inperr("bad number: %s", cp);
1140 	cp = eptr;
1141 	if (len + 1 > blen)
1142 		buf = xrealloc(buf, blen = len + 1);
1143 	for (i = 0; i < len; i++) {
1144 		c = *cp++;
1145 		if (!isalnum((unsigned char)c) && c != '_')
1146 			inperr("not alnum or _: %c", c);
1147 		buf[i] = c;
1148 	}
1149 	buf[i] = '\0';
1150 
1151 	*epp = cp;
1152 	return (buf);
1153 }
1154 
1155 /*
1156  * Return the index of a file name. If the name cannot be found, create
1157  * a new entry and return the index of the newly created entry.
1158  */
1159 static int
1160 getfnidx(const char *fn)
1161 {
1162 	size_t	i;
1163 
1164 	/* 0 is reserved */
1165 	for (i = 1; fnames[i] != NULL; i++) {
1166 		if (strcmp(fnames[i], fn) == 0)
1167 			return i;
1168 	}
1169 
1170 	if (i == nfnames - 1) {
1171 		size_t nlen = nfnames * 2;
1172 		fnames = xrealloc(fnames, nlen * sizeof(char *));
1173 		(void)memset(fnames + nfnames, 0, nfnames * sizeof(char *));
1174 		flines = xrealloc(flines, nlen * sizeof(size_t));
1175 		(void)memset(flines + nfnames, 0, nfnames * sizeof(size_t));
1176 		nfnames = nlen;
1177 	}
1178 
1179 	fnames[i] = xstrdup(fn);
1180 	flines[i] = 0;
1181 	return (i);
1182 }
1183 
1184 /*
1185  * Separate symbols with static and external linkage.
1186  */
1187 void
1188 mkstatic(hte_t *hte)
1189 {
1190 	sym_t	*sym1, **symp, *sym;
1191 	fcall_t	**callp, *call;
1192 	usym_t	**usymp, *usym;
1193 	hte_t	*nhte;
1194 	int	ofnd;
1195 
1196 	/* Look for first static definition */
1197 	for (sym1 = hte->h_syms; sym1 != NULL; sym1 = sym1->s_nxt) {
1198 		if (sym1->s_static)
1199 			break;
1200 	}
1201 	if (sym1 == NULL)
1202 		return;
1203 
1204 	/* Do nothing if this name is used only in one translation unit. */
1205 	ofnd = 0;
1206 	for (sym = hte->h_syms; sym != NULL && !ofnd; sym = sym->s_nxt) {
1207 		if (sym->s_pos.p_src != sym1->s_pos.p_src)
1208 			ofnd = 1;
1209 	}
1210 	for (call = hte->h_calls; call != NULL && !ofnd; call = call->f_nxt) {
1211 		if (call->f_pos.p_src != sym1->s_pos.p_src)
1212 			ofnd = 1;
1213 	}
1214 	for (usym = hte->h_usyms; usym != NULL && !ofnd; usym = usym->u_nxt) {
1215 		if (usym->u_pos.p_src != sym1->s_pos.p_src)
1216 			ofnd = 1;
1217 	}
1218 	if (!ofnd) {
1219 		hte->h_used = 1;
1220 		/* errors about undef. static symbols are printed in lint1 */
1221 		hte->h_def = 1;
1222 		hte->h_static = 1;
1223 		return;
1224 	}
1225 
1226 	/*
1227 	 * Create a new hash table entry
1228 	 *
1229 	 * XXX this entry should be put at the beginning of the list to
1230 	 * avoid to process the same symbol twice.
1231 	 */
1232 	for (nhte = hte; nhte->h_link != NULL; nhte = nhte->h_link)
1233 		continue;
1234 	nhte->h_link = xmalloc(sizeof (hte_t));
1235 	nhte = nhte->h_link;
1236 	nhte->h_name = hte->h_name;
1237 	nhte->h_used = 1;
1238 	nhte->h_def = 1;	/* error in lint1 */
1239 	nhte->h_static = 1;
1240 	nhte->h_syms = NULL;
1241 	nhte->h_lsym = &nhte->h_syms;
1242 	nhte->h_calls = NULL;
1243 	nhte->h_lcall = &nhte->h_calls;
1244 	nhte->h_usyms = NULL;
1245 	nhte->h_lusym = &nhte->h_usyms;
1246 	nhte->h_link = NULL;
1247 	nhte->h_hte = NULL;
1248 
1249 	/*
1250 	 * move all symbols used in this translation unit into the new
1251 	 * hash table entry.
1252 	 */
1253 	for (symp = &hte->h_syms; (sym = *symp) != NULL; ) {
1254 		if (sym->s_pos.p_src == sym1->s_pos.p_src) {
1255 			sym->s_static = 1;
1256 			(*symp) = sym->s_nxt;
1257 			if (hte->h_lsym == &sym->s_nxt)
1258 				hte->h_lsym = symp;
1259 			sym->s_nxt = NULL;
1260 			*nhte->h_lsym = sym;
1261 			nhte->h_lsym = &sym->s_nxt;
1262 		} else {
1263 			symp = &sym->s_nxt;
1264 		}
1265 	}
1266 	for (callp = &hte->h_calls; (call = *callp) != NULL; ) {
1267 		if (call->f_pos.p_src == sym1->s_pos.p_src) {
1268 			(*callp) = call->f_nxt;
1269 			if (hte->h_lcall == &call->f_nxt)
1270 				hte->h_lcall = callp;
1271 			call->f_nxt = NULL;
1272 			*nhte->h_lcall = call;
1273 			nhte->h_lcall = &call->f_nxt;
1274 		} else {
1275 			callp = &call->f_nxt;
1276 		}
1277 	}
1278 	for (usymp = &hte->h_usyms; (usym = *usymp) != NULL; ) {
1279 		if (usym->u_pos.p_src == sym1->s_pos.p_src) {
1280 			(*usymp) = usym->u_nxt;
1281 			if (hte->h_lusym == &usym->u_nxt)
1282 				hte->h_lusym = usymp;
1283 			usym->u_nxt = NULL;
1284 			*nhte->h_lusym = usym;
1285 			nhte->h_lusym = &usym->u_nxt;
1286 		} else {
1287 			usymp = &usym->u_nxt;
1288 		}
1289 	}
1290 
1291 	/* h_def must be recalculated for old hte */
1292 	hte->h_def = nhte->h_def = 0;
1293 	for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
1294 		if (sym->s_def == DEF || sym->s_def == TDEF) {
1295 			hte->h_def = 1;
1296 			break;
1297 		}
1298 	}
1299 
1300 	mkstatic(hte);
1301 }
1302