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