Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
wserror.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2016 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /*
58  *
59  * wserror.c
60  *
61  * Author: Markku Rossi <mtr@iki.fi>
62  *
63  * Copyright (c) 1999-2000 WAPIT OY LTD.
64  * All rights reserved.
65  *
66  * Error and information reporting functions.
67  *
68  */
69 
70 #include "wsint.h"
71 
72 /********************* High-level functions *****************************/
73 
74 void ws_info(WsCompilerPtr compiler, char *message, ...)
75 {
76  va_list ap;
77 
78  if (!compiler->params.verbose)
79  return;
80 
81  ws_puts(WS_STDOUT, "wsc: ");
82 
83  va_start(ap, message);
84  ws_vfprintf(WS_STDOUT, message, ap);
85  va_end(ap);
86 
88 }
89 
90 
91 void ws_fatal(char *fmt, ...)
92 {
93  va_list ap;
94 
95  fprintf(stderr, "wsc: fatal: ");
96 
97  va_start(ap, fmt);
98  vfprintf(stderr, fmt, ap);
99  va_end(ap);
100 
101  fprintf(stderr, "\n");
102 
103  abort();
104 }
105 
106 
108 {
109  gw_assert(compiler->magic == COMPILER_MAGIC);
110 
111  if (compiler->errors & WS_ERROR_B_MEMORY)
112  /* We have already reported this error. */
113  return;
114 
115  compiler->errors |= WS_ERROR_B_MEMORY;
116  ws_puts(WS_STDERR, "wsc: error: out of memory" WS_LINE_TERMINATOR);
117 }
118 
119 
121 {
122  gw_assert(compiler->magic == COMPILER_MAGIC);
123 
124  if (compiler->errors & WS_ERROR_B_MEMORY)
125  /* It makes no sense to report syntax errors when we have run out
126  of memory. This information is not too valid. */
127  return;
128 
129  if (line == 0)
130  line = compiler->linenum;
131 
132  if (compiler->last_syntax_error_line == line)
133  /* It makes no sense to report multiple syntax errors from the
134  same line. */
135  return;
136 
137  compiler->last_syntax_error_line = line;
138  compiler->errors |= WS_ERROR_B_SYNTAX;
139 
140  ws_fprintf(WS_STDERR, "%s:%u: syntax error" WS_LINE_TERMINATOR,
141  compiler->input_name, line);
142 }
143 
144 
145 void ws_src_error(WsCompilerPtr compiler, WsUInt32 line, char *message, ...)
146 {
147  va_list ap;
148 
149  gw_assert(compiler->magic == COMPILER_MAGIC);
150 
151  if (line == 0)
152  line = compiler->linenum;
153 
154  compiler->errors |= WS_ERROR_B_SEMANTIC;
155 
156  ws_fprintf(WS_STDERR, "%s:%u: ", compiler->input_name, line);
157 
158  va_start(ap, message);
159  ws_vfprintf(WS_STDERR, message, ap);
160  va_end(ap);
161 
163 
164  compiler->num_errors++;
165 }
166 
167 
168 void ws_src_warning(WsCompilerPtr compiler, WsUInt32 line, char *message, ...)
169 {
170  va_list ap;
171 
172  gw_assert(compiler->magic == COMPILER_MAGIC);
173 
174  if (line == 0)
175  line = compiler->linenum;
176 
177  ws_fprintf(WS_STDERR, "%s:%u: warning: ", compiler->input_name, line);
178 
179  va_start(ap, message);
180  ws_vfprintf(WS_STDERR, message, ap);
181  va_end(ap);
182 
184 
185  compiler->num_errors++;
186 }
187 
188 /********************* Low-level functions ******************************/
189 
190 void ws_fprintf(WsIOProc io, void *context, const char *fmt, ...)
191 {
192  va_list ap;
193 
194  va_start(ap, fmt);
195  ws_vfprintf(io, context, fmt, ap);
196  va_end(ap);
197 }
198 
199 
200 void ws_vfprintf(WsIOProc io, void *context, const char *fmt, va_list ap)
201 {
202  int start, i;
203 
204  for (start = 0, i = 0; fmt[i]; i++)
205  if (fmt[i] == '%' && fmt[i + 1]) {
206  char buf[256];
207  char *cp;
208  int ival;
209  unsigned int uival;
210  int padder = ' ';
211  int left = 0;
212  unsigned int width = 0;
213 
214  if (fmt[i + 1] == '%') {
215  /* An escaped `%'. Print leading data including the `%'
216  character. */
217  i++;
218  (*io)(fmt + start, i - start, context);
219  start = i + 1;
220  continue;
221  }
222 
223  /* An escape sequence. */
224 
225  /* Print leading data if any. */
226  if (i > start)
227  (*io)(fmt + start, i - start, context);
228 
229  /* We support a minor sub-set of the printf()'s formatting
230  capabilities. Let's see what we got. */
231  i++;
232 
233  /* Alignment? */
234  if (fmt[i] == '-') {
235  left = 1;
236  i++;
237  }
238 
239  /* Padding? */
240  if (fmt[i] == '0') {
241  padder = '0';
242  i++;
243  }
244 
245  /* Width? */
246  while ('0' <= fmt[i] && fmt[i] <= '9') {
247  width *= 10;
248  width += fmt[i++] - '0';
249  }
250 
251  /* Check the format. */
252  cp = buf;
253  switch (fmt[i]) {
254  case 'c': /* character */
255  ival = (int) va_arg(ap, int);
256 
257  snprintf(buf, sizeof(buf), "%c", (char) ival);
258  cp = buf;
259  break;
260 
261  case 's': /* string */
262  cp = va_arg(ap, char *);
263  break;
264 
265  case 'd': /* integer */
266  ival = va_arg(ap, int);
267 
268  snprintf(buf, sizeof(buf), "%d", ival);
269  cp = buf;
270  break;
271 
272  case 'u': /* unsigned integer */
273  uival = va_arg(ap, unsigned int);
274 
275  snprintf(buf, sizeof(buf), "%u", uival);
276  cp = buf;
277  break;
278 
279  case 'x': /* unsigned integer in hexadecimal format */
280  uival = va_arg(ap, unsigned int);
281 
282  snprintf(buf, sizeof(buf), "%x", uival);
283  cp = buf;
284  break;
285 
286  default:
287  ws_fatal("ws_vfprintf(): format %%%c not implemented", fmt[i]);
288  break;
289  }
290 
291  if (left)
292  /* Output the value left-justified. */
293  (*io)(cp, strlen(cp), context);
294 
295  /* Need padding? */
296  if (width > strlen(cp)) {
297  /* Yes we need. */
298  int amount = width - strlen(cp);
299 
300  while (amount-- > 0)
301  ws_fputc(padder, io, context);
302  }
303 
304  if (!left)
305  /* Output the value right-justified. */
306  (*io)(cp, strlen(cp), context);
307 
308  /* Process more. */
309  start = i + 1;
310  }
311 
312  /* Print trailing data if any. */
313  if (i > start)
314  (*io)(fmt + start, i - start, context);
315 }
316 
317 
318 void ws_puts(WsIOProc io, void *context, const char *str)
319 {
320  (*io)(str, strlen(str), context);
321 }
322 
323 
324 void ws_fputc(int ch, WsIOProc io, void *context)
325 {
326  char c = (char) ch;
327 
328  (*io)(&c, 1, context);
329 }
void ws_fatal(char *fmt,...)
Definition: wserror.c:91
#define WS_STDERR
Definition: wserror.h:114
WsUInt32 num_errors
Definition: wsint.h:253
Definition: parse.c:65
unsigned long WsUInt32
Definition: wsint.h:122
WsUInt32 magic
Definition: wsint.h:190
#define WS_ERROR_B_SYNTAX
Definition: wsint.h:141
#define WS_ERROR_B_SEMANTIC
Definition: wsint.h:146
WsUInt32 linenum
Definition: wsint.h:201
const char * input_name
Definition: wsint.h:200
WsUInt32 errors
Definition: wsint.h:261
void ws_fprintf(WsIOProc io, void *context, const char *fmt,...)
Definition: wserror.c:190
void ws_src_warning(WsCompilerPtr compiler, WsUInt32 line, char *message,...)
Definition: wserror.c:168
void ws_error_syntax(WsCompilerPtr compiler, WsUInt32 line)
Definition: wserror.c:120
gw_assert(wtls_machine->packet_to_send!=NULL)
#define WS_ERROR_B_MEMORY
Definition: wsint.h:138
WsUInt32 last_syntax_error_line
Definition: wsint.h:265
void ws_puts(WsIOProc io, void *context, const char *str)
Definition: wserror.c:318
WsCompilerParams params
Definition: wsint.h:193
#define WS_LINE_TERMINATOR
Definition: wsint.h:97
void ws_vfprintf(WsIOProc io, void *context, const char *fmt, va_list ap)
Definition: wserror.c:200
void ws_src_error(WsCompilerPtr compiler, WsUInt32 line, char *message,...)
Definition: wserror.c:145
#define COMPILER_MAGIC
Definition: wsint.h:185
void ws_error_memory(WsCompilerPtr compiler)
Definition: wserror.c:107
void ws_info(WsCompilerPtr compiler, char *message,...)
Definition: wserror.c:74
#define WS_STDOUT
Definition: wserror.h:113
void(* WsIOProc)(const char *data, size_t len, void *context)
Definition: ws.h:84
unsigned int verbose
Definition: ws.h:151
static int start
void ws_fputc(int ch, WsIOProc io, void *context)
Definition: wserror.c:324
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.