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