xref: /netbsd-src/usr.bin/xlint/lint2/msg.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: msg.c,v 1.4 1998/02/22 15:40:41 christos 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 #include <sys/cdefs.h>
35 #ifndef lint
36 __RCSID("$NetBSD: msg.c,v 1.4 1998/02/22 15:40:41 christos Exp $");
37 #endif
38 
39 #include <string.h>
40 
41 #include <stdio.h>
42 #ifdef __STDC__
43 #include <stdarg.h>
44 #else
45 #include <varargs.h>
46 #endif
47 
48 #include "lint2.h"
49 
50 
51 static	const	char *msgs[] = {
52 	"%s used( %s ), but not defined",			      /* 0 */
53 	"%s defined( %s ), but never used",			      /* 1 */
54 	"%s declared( %s ), but never used or defined",		      /* 2 */
55 	"%s multiply defined  \t%s  ::  %s",			      /* 3 */
56 	"%s value used inconsistently  \t%s  ::  %s",		      /* 4 */
57 	"%s value declared inconsistently  \t%s  ::  %s",	      /* 5 */
58 	"%s, arg %d used inconsistently  \t%s  ::  %s",		      /* 6 */
59 	"%s: variable # of args  \t%s  ::  %s",			      /* 7 */
60 	"%s returns value which is always ignored",		      /* 8 */
61 	"%s returns value which is sometimes ignored",		      /* 9 */
62 	"%s value is used( %s ), but none returned",		      /* 10 */
63 	"%s, arg %d declared inconsistently  \t%s :: %s",	      /* 11 */
64 	"%s: variable # of args declared  \t%s  ::  %s",	      /* 12 */
65 	"%s: malformed format string  \t%s",			      /* 13 */
66 	"%s, arg %d inconsistent with format  \t%s",		      /* 14 */
67 	"%s: too few args for format  \t%s",			      /* 15 */
68 	"%s: too many args for format  \t%s",			      /* 16 */
69 	"%s function value must be declared before use  \t%s  ::  %s",/* 17 */
70 	"%s renamed multiple times  \t%s  ::  %s",		      /* 18 */
71 };
72 
73 static	const	char *basename __P((const char *));
74 
75 #ifdef __STDC__
76 void
77 msg(int n, ...)
78 {
79 #else
80 void
81 msg(va_alist)
82 	va_dcl
83 	int	n;
84 {
85 #endif
86 	va_list	ap;
87 
88 #ifdef __STDC__
89 	va_start(ap, n);
90 #else
91 	va_start(ap);
92 	n = va_arg(ap, int);
93 #endif
94 
95 	(void)vprintf(msgs[n], ap);
96 	(void)printf("\n");
97 
98 	va_end(ap);
99 }
100 
101 /*
102  * Return a pointer to the last component of a path.
103  */
104 static const char *
105 basename(path)
106 	const	char *path;
107 {
108 	const	char *cp, *cp1, *cp2;
109 
110 	if (Fflag)
111 		return (path);
112 
113 	cp = cp1 = cp2 = path;
114 	while (*cp != '\0') {
115 		if (*cp++ == '/') {
116 			cp2 = cp1;
117 			cp1 = cp;
118 		}
119 	}
120 	return (*cp1 == '\0' ? cp2 : cp1);
121 }
122 
123 /*
124  * Create a string which describes a position in a source file.
125  */
126 const char *
127 mkpos(posp)
128 	pos_t	*posp;
129 {
130 	size_t	len;
131 	const	char *fn;
132 	static	char	*buf;
133 	static	size_t	blen = 0;
134 	int	qm, src, line;
135 
136 	if (Hflag && posp->p_src != posp->p_isrc) {
137 		src = posp->p_isrc;
138 		line = posp->p_iline;
139 	} else {
140 		src = posp->p_src;
141 		line = posp->p_line;
142 	}
143 	qm = !Hflag && posp->p_src != posp->p_isrc;
144 
145 	len = strlen(fn = basename(fnames[src]));
146 	len += 3 * sizeof (u_short) + 4;
147 
148 	if (len > blen)
149 		buf = xrealloc(buf, blen = len);
150 	if (line != 0) {
151 		(void)sprintf(buf, "%s%s(%hu)",
152 			      fn, qm ? "?" : "", line);
153 	} else {
154 		(void)sprintf(buf, "%s", fn);
155 	}
156 
157 	return (buf);
158 }
159 
160