Home | History | Annotate | Line # | Download | only in lint2
      1 /*	$NetBSD: main2.c,v 1.36 2025/05/24 07:38:59 rillig 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 
     34 #if HAVE_NBTOOL_CONFIG_H
     35 #include "nbtool_config.h"
     36 #endif
     37 
     38 #include <sys/cdefs.h>
     39 #if defined(__RCSID)
     40 __RCSID("$NetBSD: main2.c,v 1.36 2025/05/24 07:38:59 rillig Exp $");
     41 #endif
     42 
     43 #include <stdio.h>
     44 #include <stdlib.h>
     45 #include <string.h>
     46 #include <unistd.h>
     47 
     48 #include "lint2.h"
     49 
     50 bool Cflag;
     51 bool Fflag;
     52 bool Hflag;
     53 bool hflag;
     54 bool sflag;
     55 bool tflag;
     56 bool uflag;
     57 bool xflag;
     58 const char *libname;
     59 
     60 /*
     61  * List of libraries (from -l flag). These libraries are read after all
     62  * other input files has been read and, for Cflag, after the new lint library
     63  * has been written.
     64  */
     65 static const char **libs;
     66 
     67 static void usage(void) __attribute__((noreturn));
     68 
     69 int
     70 main(int argc, char *argv[])
     71 {
     72 	int c, i;
     73 	size_t len;
     74 	char *lname;
     75 
     76 	libs = xcalloc(1, sizeof(*libs));
     77 
     78 	opterr = 0;
     79 	while ((c = getopt(argc, argv, "hl:stuxC:HF")) != -1) {
     80 		switch (c) {
     81 		case 's':
     82 			sflag = true;
     83 			break;
     84 		case 't':
     85 			tflag = true;
     86 			break;
     87 		case 'u':
     88 			uflag = true;
     89 			break;
     90 		case 'x':
     91 			xflag = true;
     92 			break;
     93 		case 'C':
     94 			len = strlen(optarg);
     95 			lname = xmalloc(len + 10);
     96 			(void)snprintf(lname, len + 10, "llib-l%s.ln", optarg);
     97 			libname = lname;
     98 			Cflag = true;
     99 			uflag = true;
    100 			break;
    101 		case 'H':
    102 			Hflag = true;
    103 			break;
    104 		case 'h':
    105 			hflag = true;
    106 			break;
    107 		case 'F':
    108 			Fflag = true;
    109 			break;
    110 		case 'l':
    111 			for (i = 0; libs[i] != NULL; i++)
    112 				continue;
    113 			libs = xrealloc(libs, (i + 2) * sizeof(*libs));
    114 			libs[i] = xstrdup(optarg);
    115 			libs[i + 1] = NULL;
    116 			break;
    117 		default:
    118 			usage();
    119 		}
    120 	}
    121 
    122 	argc -= optind;
    123 	argv += optind;
    124 
    125 	if (argc == 0)
    126 		usage();
    127 
    128 	symtab_init();
    129 
    130 	for (i = 0; i < argc; i++)
    131 		readfile(argv[i]);
    132 
    133 	/* write the lint library */
    134 	if (Cflag) {
    135 		symtab_forall(mkstatic);
    136 		outlib(libname);
    137 	}
    138 
    139 	/* read additional libraries */
    140 	for (i = 0; libs[i] != NULL; i++)
    141 		readfile(libs[i]);
    142 
    143 	symtab_forall(mkstatic);
    144 
    145 	mark_main_as_used();
    146 
    147 	/* perform all tests */
    148 	symtab_forall_sorted(check_name);
    149 
    150 	return 0;
    151 }
    152 
    153 static void
    154 usage(void)
    155 {
    156 	(void)fprintf(stderr,
    157 	    "usage: %s [-hstuxHF] -Clib -l lib ... src1 ...\n", getprogname());
    158 	exit(1);
    159 }
    160