Home | History | Annotate | Line # | Download | only in lint2
main2.c revision 1.32
      1  1.32    rillig /*	$NetBSD: main2.c,v 1.32 2023/07/10 12:40:22 rillig Exp $	*/
      2   1.2       cgd 
      3   1.1       cgd /*
      4   1.1       cgd  * Copyright (c) 1994, 1995 Jochen Pohl
      5   1.1       cgd  * All Rights Reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *      This product includes software developed by Jochen Pohl for
     18   1.1       cgd  *	The NetBSD Project.
     19   1.1       cgd  * 4. The name of the author may not be used to endorse or promote products
     20   1.1       cgd  *    derived from this software without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23   1.1       cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.1       cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.1       cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26   1.1       cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27   1.1       cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28   1.1       cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29   1.1       cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30   1.1       cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31   1.1       cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1       cgd  */
     33   1.1       cgd 
     34   1.7       jmc #if HAVE_NBTOOL_CONFIG_H
     35   1.7       jmc #include "nbtool_config.h"
     36   1.7       jmc #endif
     37   1.7       jmc 
     38   1.3  christos #include <sys/cdefs.h>
     39  1.24    rillig #if defined(__RCSID)
     40  1.32    rillig __RCSID("$NetBSD: main2.c,v 1.32 2023/07/10 12:40:22 rillig Exp $");
     41   1.1       cgd #endif
     42   1.1       cgd 
     43   1.1       cgd #include <stdio.h>
     44   1.1       cgd #include <stdlib.h>
     45   1.1       cgd #include <string.h>
     46   1.1       cgd #include <unistd.h>
     47   1.1       cgd 
     48   1.1       cgd #include "lint2.h"
     49   1.1       cgd 
     50  1.13    rillig bool	Cflag;
     51  1.32    rillig bool	Fflag;
     52  1.32    rillig bool	Hflag;
     53  1.32    rillig bool	hflag;
     54  1.13    rillig bool	sflag;
     55  1.13    rillig bool	tflag;
     56  1.32    rillig bool	uflag;
     57  1.32    rillig bool	xflag;
     58  1.32    rillig const char *libname;
     59   1.1       cgd 
     60   1.1       cgd /*
     61   1.1       cgd  * List of libraries (from -l flag). These libraries are read after all
     62   1.1       cgd  * other input files has been read and, for Cflag, after the new lint library
     63   1.1       cgd  * has been written.
     64   1.1       cgd  */
     65  1.28    rillig static const char **libs;
     66   1.1       cgd 
     67  1.17    rillig static	void	usage(void) __attribute__((noreturn));
     68   1.1       cgd 
     69  1.19    rillig static void
     70  1.26    rillig check_name_non_const(hte_t *hte)
     71  1.19    rillig {
     72  1.26    rillig 	check_name(hte);
     73  1.19    rillig }
     74  1.19    rillig 
     75   1.1       cgd int
     76   1.4     lukem main(int argc, char *argv[])
     77   1.1       cgd {
     78  1.29    rillig 	int c, i;
     79  1.29    rillig 	size_t len;
     80  1.29    rillig 	char *lname;
     81   1.1       cgd 
     82  1.15    rillig 	libs = xcalloc(1, sizeof(*libs));
     83   1.1       cgd 
     84   1.1       cgd 	opterr = 0;
     85  1.31    rillig 	while ((c = getopt(argc, argv, "hl:stuxC:HF")) != -1) {
     86   1.1       cgd 		switch (c) {
     87   1.1       cgd 		case 's':
     88  1.13    rillig 			sflag = true;
     89   1.1       cgd 			break;
     90   1.1       cgd 		case 't':
     91  1.13    rillig 			tflag = true;
     92   1.1       cgd 			break;
     93   1.1       cgd 		case 'u':
     94  1.30    rillig 			uflag = true;
     95   1.1       cgd 			break;
     96   1.1       cgd 		case 'x':
     97  1.13    rillig 			xflag = true;
     98   1.1       cgd 			break;
     99   1.1       cgd 		case 'C':
    100   1.1       cgd 			len = strlen(optarg);
    101   1.1       cgd 			lname = xmalloc(len + 10);
    102   1.1       cgd 			(void)sprintf(lname, "llib-l%s.ln", optarg);
    103   1.1       cgd 			libname = lname;
    104  1.13    rillig 			Cflag = true;
    105  1.30    rillig 			uflag = true;
    106   1.1       cgd 			break;
    107   1.1       cgd 		case 'H':
    108  1.13    rillig 			Hflag = true;
    109   1.1       cgd 			break;
    110   1.1       cgd 		case 'h':
    111  1.13    rillig 			hflag = true;
    112   1.1       cgd 			break;
    113   1.1       cgd 		case 'F':
    114  1.13    rillig 			Fflag = true;
    115   1.1       cgd 			break;
    116   1.1       cgd 		case 'l':
    117   1.4     lukem 			for (i = 0; libs[i] != NULL; i++)
    118   1.4     lukem 				continue;
    119  1.15    rillig 			libs = xrealloc(libs, (i + 2) * sizeof(*libs));
    120   1.1       cgd 			libs[i] = xstrdup(optarg);
    121   1.1       cgd 			libs[i + 1] = NULL;
    122   1.1       cgd 			break;
    123  1.16    rillig 		default:
    124   1.1       cgd 			usage();
    125   1.1       cgd 		}
    126   1.1       cgd 	}
    127   1.4     lukem 
    128   1.1       cgd 	argc -= optind;
    129   1.1       cgd 	argv += optind;
    130   1.1       cgd 
    131   1.1       cgd 	if (argc == 0)
    132   1.1       cgd 		usage();
    133   1.1       cgd 
    134  1.23    rillig 	symtab_init();
    135   1.1       cgd 
    136   1.1       cgd 	for (i = 0; i < argc; i++)
    137   1.1       cgd 		readfile(argv[i]);
    138   1.1       cgd 
    139   1.1       cgd 	/* write the lint library */
    140   1.1       cgd 	if (Cflag) {
    141  1.20    rillig 		symtab_forall(mkstatic);
    142   1.1       cgd 		outlib(libname);
    143   1.1       cgd 	}
    144   1.1       cgd 
    145   1.1       cgd 	/* read additional libraries */
    146   1.1       cgd 	for (i = 0; libs[i] != NULL; i++)
    147   1.1       cgd 		readfile(libs[i]);
    148   1.1       cgd 
    149  1.20    rillig 	symtab_forall(mkstatic);
    150   1.1       cgd 
    151  1.26    rillig 	mark_main_as_used();
    152   1.1       cgd 
    153   1.1       cgd 	/* perform all tests */
    154  1.26    rillig 	symtab_forall_sorted(check_name_non_const);
    155   1.1       cgd 
    156  1.27    rillig 	return 0;
    157   1.1       cgd }
    158   1.1       cgd 
    159   1.1       cgd static void
    160   1.4     lukem usage(void)
    161   1.1       cgd {
    162   1.1       cgd 	(void)fprintf(stderr,
    163  1.31    rillig 	    "usage: %s [-hstuxHF] -Clib -l lib ... src1 ...\n", getprogname());
    164   1.1       cgd 	exit(1);
    165   1.1       cgd }
    166