1280387Spfg/*	$NetBSD: main1.c,v 1.17 2006/11/08 18:31:15 christos Exp $	*/
212099Sjoerg
312099Sjoerg/*
412099Sjoerg * Copyright (c) 1994, 1995 Jochen Pohl
512099Sjoerg * All Rights Reserved.
612099Sjoerg *
712099Sjoerg * Redistribution and use in source and binary forms, with or without
812099Sjoerg * modification, are permitted provided that the following conditions
912099Sjoerg * are met:
1012099Sjoerg * 1. Redistributions of source code must retain the above copyright
1112099Sjoerg *    notice, this list of conditions and the following disclaimer.
1212099Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
1312099Sjoerg *    notice, this list of conditions and the following disclaimer in the
1412099Sjoerg *    documentation and/or other materials provided with the distribution.
1512099Sjoerg * 3. All advertising materials mentioning features or use of this software
1612099Sjoerg *    must display the following acknowledgement:
1712099Sjoerg *      This product includes software developed by Jochen Pohl for
1812099Sjoerg *	The NetBSD Project.
1912099Sjoerg * 4. The name of the author may not be used to endorse or promote products
2012099Sjoerg *    derived from this software without specific prior written permission.
2112099Sjoerg *
2212099Sjoerg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2312099Sjoerg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2412099Sjoerg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2512099Sjoerg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2612099Sjoerg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2712099Sjoerg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2812099Sjoerg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2912099Sjoerg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3012099Sjoerg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3112099Sjoerg * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3212099Sjoerg */
3312099Sjoerg
3491586Smarkm#include <sys/cdefs.h>
3591586Smarkm#if defined(__RCSID) && !defined(lint)
36280387Spfg__RCSID("$NetBSD: main1.c,v 1.17 2006/11/08 18:31:15 christos Exp $");
3712099Sjoerg#endif
3894124Sru__FBSDID("$FreeBSD: releng/11.0/usr.bin/xlint/lint1/main1.c 280387 2015-03-23 18:45:29Z pfg $");
3912099Sjoerg
4091586Smarkm#include <sys/types.h>
4112099Sjoerg#include <stdio.h>
4291586Smarkm#include <string.h>
4312099Sjoerg#include <stdlib.h>
4412099Sjoerg#include <unistd.h>
45148723Sstefanf#include <err.h>
4691586Smarkm#include <errno.h>
4791586Smarkm#include <limits.h>
4812099Sjoerg
4912099Sjoerg#include "lint1.h"
5012099Sjoerg
5112099Sjoerg/* set yydebug to 1*/
5212099Sjoergint	yflag;
5312099Sjoerg
5412099Sjoerg/*
5512099Sjoerg * Print warnings if an assignment of an integertype to another integertype
56280387Spfg * causes an implicit narrowing conversion. If aflag is 1, these warnings
5712099Sjoerg * are printed only if the source type is at least as wide as long. If aflag
5891586Smarkm * is greater than 1, they are always printed.
5912099Sjoerg */
6012099Sjoergint	aflag;
6112099Sjoerg
6212099Sjoerg/* Print a warning if a break statement cannot be reached. */
6312099Sjoergint	bflag;
6412099Sjoerg
6512099Sjoerg/* Print warnings for pointer casts. */
6612099Sjoergint	cflag;
6712099Sjoerg
6812099Sjoerg/* Print various debug information. */
6912099Sjoergint	dflag;
7012099Sjoerg
7112099Sjoerg/* Perform stricter checking of enum types and operations on enum types. */
7212099Sjoergint	eflag;
7312099Sjoerg
7412099Sjoerg/* Print complete pathnames, not only the basename. */
7512099Sjoergint	Fflag;
7612099Sjoerg
7712099Sjoerg/* Enable some extensions of gcc */
7812099Sjoergint	gflag;
7912099Sjoerg
8091586Smarkm/* Treat warnings as errors */
8191586Smarkmint	wflag;
8291586Smarkm
8312099Sjoerg/*
8412099Sjoerg * Apply a number of heuristic tests to attempt to intuit bugs, improve
8512099Sjoerg * style, and reduce waste.
8612099Sjoerg */
8712099Sjoergint	hflag;
8812099Sjoerg
8912099Sjoerg/* Attempt to check portability to other dialects of C. */
9012099Sjoergint	pflag;
9112099Sjoerg
9212099Sjoerg/*
9312099Sjoerg * In case of redeclarations/redefinitions print the location of the
9412099Sjoerg * previous declaration/definition.
9512099Sjoerg */
9612099Sjoergint	rflag;
9712099Sjoerg
9812099Sjoerg/* Strict ANSI C mode. */
9912099Sjoergint	sflag;
10012099Sjoerg
10112099Sjoerg/* Traditional C mode. */
10212099Sjoergint	tflag;
10312099Sjoerg
104280387Spfg/* Enable C9X extensions */
105280387Spfgint	Sflag;
10612099Sjoerg/*
10712099Sjoerg * Complain about functions and external variables used and not defined,
10812099Sjoerg * or defined and not used.
10912099Sjoerg */
11012099Sjoergint	uflag = 1;
11112099Sjoerg
11212099Sjoerg/* Complain about unused function arguments. */
11312099Sjoergint	vflag = 1;
11412099Sjoerg
11512099Sjoerg/* Complain about structures which are never defined. */
11612099Sjoergint	zflag = 1;
11712099Sjoerg
11891586Smarkmerr_set	msgset;
11912099Sjoerg
12091586Smarkmstatic	void	usage(void);
12191586Smarkm
12291586Smarkmint main(int, char *[]);
12391586Smarkm
12412099Sjoergint
12591586Smarkmmain(int argc, char *argv[])
12612099Sjoerg{
12712099Sjoerg	int	c;
12891586Smarkm	char	*ptr;
12912099Sjoerg
13091586Smarkm	ERR_ZERO(&msgset);
131280387Spfg	while ((c = getopt(argc, argv, "abcdeghmprstuvwyzFSX:")) != -1) {
13212099Sjoerg		switch (c) {
13312099Sjoerg		case 'a':	aflag++;	break;
13412099Sjoerg		case 'b':	bflag = 1;	break;
13512099Sjoerg		case 'c':	cflag = 1;	break;
13612099Sjoerg		case 'd':	dflag = 1;	break;
13712099Sjoerg		case 'e':	eflag = 1;	break;
13812099Sjoerg		case 'F':	Fflag = 1;	break;
13912099Sjoerg		case 'g':	gflag = 1;	break;
14012099Sjoerg		case 'h':	hflag = 1;	break;
14112099Sjoerg		case 'p':	pflag = 1;	break;
14212099Sjoerg		case 'r':	rflag = 1;	break;
14312099Sjoerg		case 's':	sflag = 1;	break;
144280387Spfg		case 'S':	Sflag = 1;	break;
14512099Sjoerg		case 't':	tflag = 1;	break;
14612099Sjoerg		case 'u':	uflag = 0;	break;
14791586Smarkm		case 'w':	wflag = 1;	break;
14812099Sjoerg		case 'v':	vflag = 0;	break;
14912099Sjoerg		case 'y':	yflag = 1;	break;
15012099Sjoerg		case 'z':	zflag = 0;	break;
15191586Smarkm
15291586Smarkm		case 'm':
15391586Smarkm			msglist();
15491586Smarkm			return(0);
15591586Smarkm
15691586Smarkm		case 'X':
15791586Smarkm			for (ptr = strtok(optarg, ","); ptr;
15891586Smarkm			    ptr = strtok(NULL, ",")) {
15991586Smarkm				char *eptr;
160280387Spfg				long msg;
161280387Spfg
162280387Spfg				errno = 0;
163280387Spfg				msg = strtol(ptr, &eptr, 0);
16491586Smarkm				if ((msg == LONG_MIN || msg == LONG_MAX) &&
16591586Smarkm				    errno == ERANGE)
16691586Smarkm				    err(1, "invalid error message id '%s'",
16791586Smarkm					ptr);
16891586Smarkm				if (*eptr || ptr == eptr || msg < 0 ||
16991586Smarkm				    msg >= ERR_SETSIZE)
17091586Smarkm					errx(1, "invalid error message id '%s'",
17191586Smarkm					    ptr);
17291586Smarkm				ERR_SET(msg, &msgset);
17391586Smarkm			}
17491586Smarkm			break;
17591586Smarkm		case '?':
17691586Smarkm		default:
17791586Smarkm			usage();
17891586Smarkm			break;
17912099Sjoerg		}
18012099Sjoerg	}
18112099Sjoerg	argc -= optind;
18212099Sjoerg	argv += optind;
18312099Sjoerg
18412099Sjoerg	if (argc != 2)
18512099Sjoerg		usage();
18612099Sjoerg
18712099Sjoerg	/* open the input file */
18812099Sjoerg	if ((yyin = fopen(argv[0], "r")) == NULL)
18912099Sjoerg		err(1, "cannot open '%s'", argv[0]);
19012099Sjoerg
19112099Sjoerg	/* initialize output */
19212099Sjoerg	outopen(argv[1]);
19312099Sjoerg
19412099Sjoerg	if (yflag)
19512099Sjoerg		yydebug = 1;
19612099Sjoerg
19712099Sjoerg	initmem();
19812099Sjoerg	initdecl();
19912099Sjoerg	initscan();
20012099Sjoerg	initmtab();
20112099Sjoerg
20212099Sjoerg	yyparse();
20312099Sjoerg
20412099Sjoerg	/* Following warnings cannot be suppressed by LINTED */
20512099Sjoerg	nowarn = 0;
206280387Spfg#ifdef DEBUG
207280387Spfg	printf("%s, %d: nowarn = 0\n", curr_pos.p_file, curr_pos.p_line);
208280387Spfg#endif
20912099Sjoerg	chkglsyms();
21012099Sjoerg
21112099Sjoerg	outclose();
21212099Sjoerg
21312099Sjoerg	return (nerr != 0);
21412099Sjoerg}
21512099Sjoerg
21612099Sjoergstatic void
21791586Smarkmusage(void)
21812099Sjoerg{
21991586Smarkm	(void)fprintf(stderr,
220280387Spfg	    "usage: lint1 [-abcdeghmprstuvwyzFS] [-X <id>[,<id>]... src dest\n");
22112099Sjoerg	exit(1);
22212099Sjoerg}
22391586Smarkm
22412099Sjoergvoid
22591586Smarkmnorecover(void)
22612099Sjoerg{
22712099Sjoerg	/* cannot recover from previous errors */
22812099Sjoerg	error(224);
22912099Sjoerg	exit(1);
23012099Sjoerg}
231