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