]> andersk Git - splint.git/blob - src/message.c
1c9c2313fe57965954a5e50e2177653e1ed6c653
[splint.git] / src / message.c
1 /*
2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 University of Virginia,
4 **         Massachusetts Institute of Technology
5 **
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.
10 ** 
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.
15 ** 
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.
19 **
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
23 */
24 /*
25 ** message.c
26 */
27
28 # include "splintMacros.nf"
29 # include "basic.h"
30  
31 /* patch for linux? solaris? */
32
33 static char strbuf[64];
34 static int modcode;
35
36 typedef enum
37 {
38   XINVALID, 
39   XCHAR, XSTRING, XSTRINGFREE, XTSTRINGFREE, XINT, XFLOAT, XBOOL, XUENTRY,
40   XPERCENT, XCTYPE, XPLURAL, XREPREFIX, XFILELOC, XPOINTER
41 } ccode;
42
43 /*@function void GETPRINTF (char *p_s, anytype p_v) modifies strbuf@*/
44
45 /*@notfunction@*/
46 # ifndef WIN32
47   /* ISO requires this, but not all implementations (e.g., Microsoft's) provide it */
48 # define GETPRINTF(s,v) (snprintf (strbuf, 64, s, v), mstring_copy (strbuf))
49 # else
50   /* MS provides _snprintf instead */
51 # define GETPRINTF(s,v) (_snprintf (strbuf, 64, s, v), mstring_copy (strbuf))
52 # endif
53
54 /*
55 ** returns control code indicated by *c, and
56 ** advances *c to next character.
57 */
58
59 static ccode identify_control (char **s)
60 {
61   char c;
62
63   modcode = 0;
64
65   c = **s;
66   if (c == '\0')
67     {
68       return (XINVALID);
69     }
70
71   if (c >= '0' && c <= '9')
72     {
73       modcode = reader_getInt (s);
74     }
75
76   c = **s;
77
78   (*s)++;
79
80  /*
81  ** handle single-char codes
82  */
83
84   switch (c)
85     {
86     case '%':
87       return (XPERCENT);
88     case 'h':
89     case 'c':
90       return (XCHAR);
91     case 's':
92       return (XSTRING);
93     case 'q':
94       return (XSTRINGFREE);
95     case 'x':
96       return (XSTRINGFREE);
97     case 'd':
98       return (XINT);
99     case 'u':
100       return (XINT); /* unsigned */
101     case 'w':
102       return (XINT); /* unsigned long */
103     case 'f':
104       return (XFLOAT);
105     case 'b':
106       return (XBOOL);
107     case 't':
108       return (XCTYPE);
109     case 'p':
110       return (XPOINTER);
111     case 'l':
112       return (XFILELOC);
113     case '&':
114       return (XPLURAL);
115     case 'r':
116       return (XREPREFIX);
117     default:
118       llcontbug (message ("Message: invalid code: %h (%s)", c, 
119                           cstring_fromChars (*s)));
120       return (XINVALID);
121     }
122 }
123
124 /*
125 ** message
126 **
127 ** returns a cstring containing the message, as formated by s.
128 **
129 ** the format codes are similar to printf:
130 **
131 **         %s    cstring (don't free after print)
132 **         %q    cstring (free after print)
133 **         %d    int
134 **         %f    float
135 **         %b    bool     (uses bool_unparse)
136 **         %u    uentry
137 **         %l    fileloc
138 **         %t    ctype
139 */
140
141
142 # if USEVARARGS
143 cstring
144 message (fmt, va_alist)
145      char *fmt;
146      va_dcl
147 # else
148 /*@messagelike@*/ /*@only@*/ cstring
149 message (/*@temp@*/ char *fmt, ...)
150 # endif
151 {
152   char c;
153   int lastint = 0;
154   char *ret = mstring_createEmpty ();
155   char *ofmt = fmt;
156   va_list pvar;
157
158 # if USEVARARGS
159   va_start (pvar);
160 # else
161   va_start (pvar, fmt);
162 # endif  
163
164   while ((c = *fmt++) != '\0')
165     {
166       if (c == '%')
167         {
168           /*@-loopswitchbreak@*/
169
170           switch (identify_control (&fmt))
171             {
172             case XPERCENT:
173               {
174                 ret = mstring_concatFree1 (ret, "%");
175                 break;
176               }
177             case XCHAR:
178               {
179                 /*
180                 ** some systems don't handle char va_arg correctly, so it must be
181                 ** passed as an int here
182                 */
183
184                 char lc = (char) va_arg (pvar, int);
185
186                 ret = mstring_append (ret, lc);
187                 break;
188               }
189             case XSTRING:
190               {
191                 cstring s = va_arg (pvar, cstring);
192                 
193                 if (modcode != 0)
194                   {
195                     ret = mstring_concatFree (ret, cstring_toCharsSafe 
196                                               (cstring_fill (s, size_fromInt (modcode))));
197                   }
198                 else
199                   {
200                     if (cstring_isDefined (s))
201                       {
202                         ret = mstring_concatFree1 (ret, cstring_toCharsSafe (s));
203                       }
204                   }
205               }
206               break;
207             case XSTRINGFREE:
208             case XTSTRINGFREE:
209               {
210                 cstring s = va_arg (pvar, cstring);
211                 
212                 if (modcode != 0)
213                   {
214                     ret = mstring_concatFree (ret, cstring_toCharsSafe 
215                                               (cstring_fill (s, size_fromInt (modcode))));
216                   }
217                 else
218                   {
219                     if (cstring_isDefined (s))
220                       {
221                         ret = mstring_concatFree 
222                           (ret, cstring_toCharsSafe (s));
223                       }
224                   }
225               }
226               break;
227             case XREPREFIX:
228               lastint = va_arg (pvar, int);
229
230               if (lastint != 0)
231                 {
232                   ret = mstring_concatFree1 (ret, "re");
233                 }
234               break;
235             case XPLURAL:
236               if (lastint != 1)
237                 {
238                   ret = mstring_concatFree1 (ret, "s");
239                 }
240               break;
241             case XINT:
242               lastint = va_arg (pvar, int);
243               ret = mstring_concatFree (ret, GETPRINTF ("%d", lastint));
244               break;
245             case XFLOAT:
246               ret = mstring_concatFree (ret, GETPRINTF ("%.2lf", va_arg (pvar, double)));
247               break;
248             case XBOOL:
249               ret = mstring_concatFree1 (ret, cstring_toCharsSafe 
250                                     (bool_unparse (va_arg (pvar, bool))));
251               break;
252             case XUENTRY:
253               ret = mstring_concatFree (ret, cstring_toCharsSafe 
254                                    (uentry_unparse (va_arg (pvar, uentry))));
255               break;
256             case XCTYPE:
257               /* cannot free ctype_unparse */
258               ret = mstring_concatFree1 (ret, cstring_toCharsSafe 
259                                    (ctype_unparse (va_arg (pvar, ctype)))); 
260               break;
261             case XPOINTER:
262               ret = mstring_concatFree (ret, GETPRINTF ("%p", va_arg (pvar, void *)));
263               break;
264
265             case XFILELOC:
266               ret = mstring_concatFree (ret, cstring_toCharsSafe 
267                                    (fileloc_unparse (va_arg (pvar, fileloc))));
268               break;
269             case XINVALID:
270             default:
271               llcontbug (cstring_makeLiteral ("message: bad control flag"));
272               fprintf (stdout, "\tFormat string: %s", ofmt);
273             }
274           /*@=loopswitchbreak@*/
275         }
276       else
277         {
278           ret = mstring_append (ret, c);
279         }
280     }
281
282   va_end (pvar);
283
284   /*
285   ** cstring_fromChars returns the same storage (exposed)
286   */
287
288   /*@-mustfree@*/ return (cstring_fromChars (ret)); /*@=mustfree@*/
289 }
This page took 0.045143 seconds and 3 git commands to generate.