Synchronise with NetBSD: get rid of __STDC__ selective compilation.
[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.6 2004/07/07 08:20:19 asmodai 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(void);
85 static  void    setsrc(const char *);
86 static  void    setfnid(int, const char *);
87 static  void    funccall(pos_t *, const char *);
88 static  void    decldef(pos_t *, const char *);
89 static  void    usedsym(pos_t *, const char *);
90 static  u_short inptype(const char *, const char **);
91 static  int     gettlen(const char *, const char **);
92 static  u_short findtype(const char *, size_t, int);
93 static  u_short storetyp(type_t *, const char *, size_t, int);
94 static  int     thash(const char *, size_t);
95 static  char    *inpqstrg(const char *, const char **);
96 static  const   char *inpname(const char *, const char **);
97 static  int     getfnidx(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                 break;
617         }
618
619         *epp = cp;
620         return (tidx);
621 }
622
623 /*
624  * Get the length of a type string.
625  */
626 static int
627 gettlen(cp, epp)
628         const   char *cp, **epp;
629 {
630         const   char *cp1;
631         char    c, s, *eptr;
632         tspec_t t;
633         int     narg, i, cm, vm;
634
635         cp1 = cp;
636
637         c = *cp++;
638
639         cm = vm = 0;
640
641         while (c == 'c' || c == 'v') {
642                 if (c == 'c') {
643                         if (cm)
644                                 inperr();
645                         cm = 1;
646                 } else {
647                         if (vm)
648                                 inperr();
649                         vm = 1;
650                 }
651                 c = *cp++;
652         }
653
654         if (c == 's' || c == 'u' || c == 'l' || c == 'e') {
655                 s = c;
656                 c = *cp++;
657         } else {
658                 s = '\0';
659         }
660
661         t = NOTSPEC;
662
663         switch (c) {
664         case 'C':
665                 if (s == 's') {
666                         t = SCHAR;
667                 } else if (s == 'u') {
668                         t = UCHAR;
669                 } else if (s == '\0') {
670                         t = CHAR;
671                 }
672                 break;
673         case 'S':
674                 if (s == 'u') {
675                         t = USHORT;
676                 } else if (s == '\0') {
677                         t = SHORT;
678                 }
679                 break;
680         case 'I':
681                 if (s == 'u') {
682                         t = UINT;
683                 } else if (s == '\0') {
684                         t = INT;
685                 }
686                 break;
687         case 'L':
688                 if (s == 'u') {
689                         t = ULONG;
690                 } else if (s == '\0') {
691                         t = LONG;
692                 }
693                 break;
694         case 'Q':
695                 if (s == 'u') {
696                         t = UQUAD;
697                 } else if (s == '\0') {
698                         t = QUAD;
699                 }
700                 break;
701         case 'D':
702                 if (s == 's') {
703                         t = FLOAT;
704                 } else if (s == 'l') {
705                         t = LDOUBLE;
706                 } else if (s == '\0') {
707                         t = DOUBLE;
708                 }
709                 break;
710         case 'V':
711                 if (s == '\0')
712                         t = VOID;
713                 break;
714         case 'P':
715                 if (s == '\0')
716                         t = PTR;
717                 break;
718         case 'A':
719                 if (s == '\0')
720                         t = ARRAY;
721                 break;
722         case 'F':
723         case 'f':
724                 if (s == '\0')
725                         t = FUNC;
726                 break;
727         case 'T':
728                 if (s == 'e') {
729                         t = ENUM;
730                 } else if (s == 's') {
731                         t = STRUCT;
732                 } else if (s == 'u') {
733                         t = UNION;
734                 }
735                 break;
736         default:
737                 inperr();
738         }
739
740         if (t == NOTSPEC)
741                 inperr();
742
743         switch (t) {
744         case ARRAY:
745                 (void)strtol(cp, &eptr, 10);
746                 if (cp == eptr)
747                         inperr();
748                 cp = eptr;
749                 (void)gettlen(cp, &cp);
750                 break;
751         case PTR:
752                 (void)gettlen(cp, &cp);
753                 break;
754         case FUNC:
755                 c = *cp;
756                 if (isdigit((u_char)c)) {
757                         narg = (int)strtol(cp, &eptr, 10);
758                         cp = eptr;
759                         for (i = 0; i < narg; i++) {
760                                 if (i == narg - 1 && *cp == 'E') {
761                                         cp++;
762                                 } else {
763                                         (void)gettlen(cp, &cp);
764                                 }
765                         }
766                 }
767                 (void)gettlen(cp, &cp);
768                 break;
769         case ENUM:
770         case STRUCT:
771         case UNION:
772                 switch (*cp++) {
773                 case '0':
774                         break;
775                 case '1':
776                         (void)inpname(cp, &cp);
777                         break;
778                 case '2':
779                         (void)inpname(cp, &cp);
780                         break;
781                 default:
782                         inperr();
783                 }
784                 break;
785                 /* LINTED (enumeration value(s) not handled in switch) */
786         default:
787                 break;
788         }
789
790         *epp = cp;
791         return (cp - cp1);
792 }
793
794 /*
795  * Search a type by it's type string.
796  */
797 static u_short
798 findtype(cp, len, h)
799         const   char *cp;
800         size_t  len;
801         int     h;
802 {
803         thtab_t *thte;
804
805         for (thte = thtab[h]; thte != NULL; thte = thte->th_nxt) {
806                 if (strncmp(thte->th_name, cp, len) != 0)
807                         continue;
808                 if (thte->th_name[len] == '\0')
809                         return (thte->th_idx);
810         }
811
812         return (0);
813 }
814
815 /*
816  * Store a type and it's type string so we can later share this type
817  * if we read the same type string from the input file.
818  */
819 static u_short
820 storetyp(tp, cp, len, h)
821         type_t  *tp;
822         const   char *cp;
823         size_t  len;
824         int     h;
825 {
826         /* 0 ist reserved */
827         static  u_int   tidx = 1;
828         thtab_t *thte;
829         char    *name;
830
831         if (tidx >= USHRT_MAX)
832                 errx(1, "sorry, too many types");
833
834         if (tidx == tlstlen - 1) {
835                 tlst = xrealloc(tlst, (tlstlen * 2) * sizeof (type_t *));
836                 (void)memset(tlst + tlstlen, 0, tlstlen * sizeof (type_t *));
837                 tlstlen *= 2;
838         }
839
840         tlst[tidx] = tp;
841
842         /* create a hash table entry */
843         name = xalloc(len + 1);
844         (void)memcpy(name, cp, len);
845         name[len] = '\0';
846
847         thte = xalloc(sizeof (thtab_t));
848         thte->th_name = name;
849         thte->th_idx = tidx;
850         thte->th_nxt = thtab[h];
851         thtab[h] = thte;
852
853         return ((u_short)tidx++);
854 }
855
856 /*
857  * Hash function for types
858  */
859 static int
860 thash(s, len)
861         const   char *s;
862         size_t  len;
863 {
864         u_int   v;
865
866         v = 0;
867         while (len-- != 0) {
868                 v = (v << sizeof (v)) + (u_char)*s++;
869                 v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v));
870         }
871         return (v % THSHSIZ2);
872 }
873
874 /*
875  * Read a string enclosed by "". This string may contain quoted chars.
876  */
877 static char *
878 inpqstrg(src, epp)
879         const   char *src, **epp;
880 {
881         char    *strg, *dst;
882         size_t  slen;
883         int     c;
884         int     v;
885
886         dst = strg = xmalloc(slen = 32);
887
888         if ((c = *src++) != '"')
889                 inperr();
890         if ((c = *src++) == '\0')
891                 inperr();
892
893         while (c != '"') {
894                 if (c == '\\') {
895                         if ((c = *src++) == '\0')
896                                 inperr();
897                         switch (c) {
898                         case 'n':
899                                 c = '\n';
900                                 break;
901                         case 't':
902                                 c = '\t';
903                                 break;
904                         case 'v':
905                                 c = '\v';
906                                 break;
907                         case 'b':
908                                 c = '\b';
909                                 break;
910                         case 'r':
911                                 c = '\r';
912                                 break;
913                         case 'f':
914                                 c = '\f';
915                                 break;
916                         case 'a':
917                                 c = '\a';
918                                 break;
919                         case '\\':
920                                 c = '\\';
921                                 break;
922                         case '"':
923                                 c = '"';
924                                 break;
925                         case '\'':
926                                 c = '\'';
927                                 break;
928                         case '0': case '1': case '2': case '3':
929                                 v = (c - '0') << 6;
930                                 if ((c = *src++) < '0' || c > '7')
931                                         inperr();
932                                 v |= (c - '0') << 3;
933                                 if ((c = *src++) < '0' || c > '7')
934                                         inperr();
935                                 v |= c - '0';
936                                 c = (u_char)v;
937                                 break;
938                         default:
939                                 inperr();
940                         }
941                 }
942                 /* keep space for trailing '\0' */
943                 if (dst - strg == slen - 1) {
944                         strg = xrealloc(strg, slen * 2);
945                         dst = strg + (slen - 1);
946                         slen *= 2;
947                 }
948                 *dst++ = (char)c;
949                 if ((c = *src++) == '\0')
950                         inperr();
951         }
952         *dst = '\0';
953
954         *epp = src;
955         return (strg);
956 }
957
958 /*
959  * Read the name of a symbol in static memory.
960  */
961 static const char *
962 inpname(cp, epp)
963         const   char *cp, **epp;
964 {
965         static  char    *buf;
966         static  size_t  blen = 0;
967         size_t  len, i;
968         char    *eptr, c;
969
970         len = (int)strtol(cp, &eptr, 10);
971         if (cp == eptr)
972                 inperr();
973         cp = eptr;
974         if (len + 1 > blen)
975                 buf = xrealloc(buf, blen = len + 1);
976         for (i = 0; i < len; i++) {
977                 c = *cp++;
978                 if (!isalnum(c) && c != '_')
979                         inperr();
980                 buf[i] = c;
981         }
982         buf[i] = '\0';
983
984         *epp = cp;
985         return (buf);
986 }
987
988 /*
989  * Return the index of a file name. If the name cannot be found, create
990  * a new entry and return the index of the newly created entry.
991  */
992 static int
993 getfnidx(fn)
994         const   char *fn;
995 {
996         int     i;
997
998         /* 0 ist reserved */
999         for (i = 1; fnames[i] != NULL; i++) {
1000                 if (strcmp(fnames[i], fn) == 0)
1001                         break;
1002         }
1003         if (fnames[i] != NULL)
1004                 return (i);
1005
1006         if (i == nfnames - 1) {
1007                 fnames = xrealloc(fnames, (nfnames * 2) * sizeof (char *));
1008                 (void)memset(fnames + nfnames, 0, nfnames * sizeof (char *));
1009                 nfnames *= 2;
1010         }
1011
1012         fnames[i] = xstrdup(fn);
1013         return (i);
1014 }
1015
1016 /*
1017  * Separate symbols with static and external linkage.
1018  */
1019 void
1020 mkstatic(hte)
1021         hte_t   *hte;
1022 {
1023         sym_t   *sym1, **symp, *sym;
1024         fcall_t **callp, *call;
1025         usym_t  **usymp, *usym;
1026         hte_t   *nhte;
1027         int     ofnd;
1028
1029         /* Look for first static definition */
1030         for (sym1 = hte->h_syms; sym1 != NULL; sym1 = sym1->s_nxt) {
1031                 if (sym1->s_static)
1032                         break;
1033         }
1034         if (sym1 == NULL)
1035                 return;
1036
1037         /* Do nothing if this name is used only in one translation unit. */
1038         ofnd = 0;
1039         for (sym = hte->h_syms; sym != NULL && !ofnd; sym = sym->s_nxt) {
1040                 if (sym->s_pos.p_src != sym1->s_pos.p_src)
1041                         ofnd = 1;
1042         }
1043         for (call = hte->h_calls; call != NULL && !ofnd; call = call->f_nxt) {
1044                 if (call->f_pos.p_src != sym1->s_pos.p_src)
1045                         ofnd = 1;
1046         }
1047         for (usym = hte->h_usyms; usym != NULL && !ofnd; usym = usym->u_nxt) {
1048                 if (usym->u_pos.p_src != sym1->s_pos.p_src)
1049                         ofnd = 1;
1050         }
1051         if (!ofnd) {
1052                 hte->h_used = 1;
1053                 /* errors about undef. static symbols are printed in lint1 */
1054                 hte->h_def = 1;
1055                 hte->h_static = 1;
1056                 return;
1057         }
1058
1059         /*
1060          * Create a new hash table entry
1061          *
1062          * XXX this entry should be put at the beginning of the list to
1063          * avoid to process the same symbol twice.
1064          */
1065         for (nhte = hte; nhte->h_link != NULL; nhte = nhte->h_link) ;
1066         nhte->h_link = xalloc(sizeof (hte_t));
1067         nhte = nhte->h_link;
1068         nhte->h_name = hte->h_name;
1069         nhte->h_static = 1;
1070         nhte->h_used = 1;
1071         nhte->h_def = 1;        /* error in lint1 */
1072         nhte->h_lsym = &nhte->h_syms;
1073         nhte->h_lcall = &nhte->h_calls;
1074         nhte->h_lusym = &nhte->h_usyms;
1075
1076         /*
1077          * move all symbols used in this translation unit into the new
1078          * hash table entry.
1079          */
1080         for (symp = &hte->h_syms; (sym = *symp) != NULL; ) {
1081                 if (sym->s_pos.p_src == sym1->s_pos.p_src) {
1082                         sym->s_static = 1;
1083                         (*symp) = sym->s_nxt;
1084                         if (hte->h_lsym == &sym->s_nxt)
1085                                 hte->h_lsym = symp;
1086                         sym->s_nxt = NULL;
1087                         *nhte->h_lsym = sym;
1088                         nhte->h_lsym = &sym->s_nxt;
1089                 } else {
1090                         symp = &sym->s_nxt;
1091                 }
1092         }
1093         for (callp = &hte->h_calls; (call = *callp) != NULL; ) {
1094                 if (call->f_pos.p_src == sym1->s_pos.p_src) {
1095                         (*callp) = call->f_nxt;
1096                         if (hte->h_lcall == &call->f_nxt)
1097                                 hte->h_lcall = callp;
1098                         call->f_nxt = NULL;
1099                         *nhte->h_lcall = call;
1100                         nhte->h_lcall = &call->f_nxt;
1101                 } else {
1102                         callp = &call->f_nxt;
1103                 }
1104         }
1105         for (usymp = &hte->h_usyms; (usym = *usymp) != NULL; ) {
1106                 if (usym->u_pos.p_src == sym1->s_pos.p_src) {
1107                         (*usymp) = usym->u_nxt;
1108                         if (hte->h_lusym == &usym->u_nxt)
1109                                 hte->h_lusym = usymp;
1110                         usym->u_nxt = NULL;
1111                         *nhte->h_lusym = usym;
1112                         nhte->h_lusym = &usym->u_nxt;
1113                 } else {
1114                         usymp = &usym->u_nxt;
1115                 }
1116         }
1117
1118         /* h_def must be recalculated for old hte */
1119         hte->h_def = nhte->h_def = 0;
1120         for (sym = hte->h_syms; sym != NULL; sym = sym->s_nxt) {
1121                 if (sym->s_def == DEF || sym->s_def == TDEF) {
1122                         hte->h_def = 1;
1123                         break;
1124                 }
1125         }
1126
1127         mkstatic(hte);
1128 }