2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2002 University of Virginia,
4 ** Massachusetts Institute of Technology
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ** General Public License for more details.
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
20 ** For information on splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
28 # include "splintMacros.nf"
31 /* patch for linux? solaris? */
33 static char strbuf[64];
39 XCHAR, XSTRING, XSTRINGFREE, XTSTRINGFREE, XINT, XFLOAT, XBOOL, XUENTRY,
40 XPERCENT, XCTYPE, XPLURAL, XREPREFIX, XFILELOC, XPOINTER
43 /* char *s, anytype v */
45 # define GETPRINTF(s,v) (snprintf (strbuf, 64, s, v), mstring_copy (strbuf))
48 ** returns control code indicated by *c, and
49 ** advances *c to next character.
52 static ccode identify_control (char **s)
64 if (c >= '0' && c <= '9')
66 modcode = reader_getInt (s);
74 ** handle single-char codes
93 return (XINT); /* unsigned */
95 return (XINT); /* unsigned long */
111 llcontbug (message ("Message: invalid code: %h (%s)", c,
112 cstring_fromChars (*s)));
120 ** returns a cstring containing the message, as formated by s.
122 ** the format codes are similar to printf:
124 ** %s cstring (don't free after print)
125 ** %q cstring (free after print)
128 ** %b bool (uses bool_unparse)
137 message (fmt, va_alist)
141 /*@messagelike@*/ /*@only@*/ cstring
142 message (/*@temp@*/ char *fmt, ...)
147 char *ret = mstring_createEmpty ();
154 va_start (pvar, fmt);
157 while ((c = *fmt++) != '\0')
161 /*@-loopswitchbreak@*/
163 switch (identify_control (&fmt))
167 ret = mstring_concatFree1 (ret, "%");
173 ** some systems don't handle char va_arg correctly, so it must be
174 ** passed as an int here
177 char lc = (char) va_arg (pvar, int);
179 ret = mstring_append (ret, lc);
184 cstring s = va_arg (pvar, cstring);
188 ret = mstring_concatFree (ret, cstring_toCharsSafe
189 (cstring_fill (s, size_fromInt (modcode))));
193 if (cstring_isDefined (s))
195 ret = mstring_concatFree1 (ret, cstring_toCharsSafe (s));
203 cstring s = va_arg (pvar, cstring);
207 ret = mstring_concatFree (ret, cstring_toCharsSafe
208 (cstring_fill (s, size_fromInt (modcode))));
212 if (cstring_isDefined (s))
214 ret = mstring_concatFree
215 (ret, cstring_toCharsSafe (s));
221 lastint = va_arg (pvar, int);
225 ret = mstring_concatFree1 (ret, "re");
231 ret = mstring_concatFree1 (ret, "s");
235 lastint = va_arg (pvar, int);
236 ret = mstring_concatFree (ret, GETPRINTF ("%d", lastint));
239 ret = mstring_concatFree (ret, GETPRINTF ("%.2lf", va_arg (pvar, double)));
242 ret = mstring_concatFree1 (ret, cstring_toCharsSafe
243 (bool_unparse (va_arg (pvar, bool))));
246 ret = mstring_concatFree (ret, cstring_toCharsSafe
247 (uentry_unparse (va_arg (pvar, uentry))));
250 /* cannot free ctype_unparse */
251 ret = mstring_concatFree1 (ret, cstring_toCharsSafe
252 (ctype_unparse (va_arg (pvar, ctype))));
255 ret = mstring_concatFree (ret, GETPRINTF ("%p", va_arg (pvar, void *)));
259 ret = mstring_concatFree (ret, cstring_toCharsSafe
260 (fileloc_unparse (va_arg (pvar, fileloc))));
264 llcontbug (cstring_makeLiteral ("message: bad control flag"));
265 fprintf (stdout, "\tFormat string: %s", ofmt);
267 /*@=loopswitchbreak@*/
271 ret = mstring_append (ret, c);
278 ** cstring_fromChars returns the same storage (exposed)
281 /*@-mustfree@*/ return (cstring_fromChars (ret)); /*@=mustfree@*/