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