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