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