Root/
Source at commit e1a04fa7beb3ee06cb45ed40aad7e92a91f6fd53 created 13 years 5 months ago. By Xiangfu Liu, update take svn rev: 5983 | |
---|---|
1 | /* |
2 | * error.c - Error reporting |
3 | * |
4 | * Written 2009, 2010 by Werner Almesberger |
5 | * Copyright 2009, 2010 by Werner Almesberger |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by |
9 | * the Free Software Foundation; either version 2 of the License, or |
10 | * (at your option) any later version. |
11 | */ |
12 | |
13 | |
14 | #include <stdarg.h> |
15 | #include <stdlib.h> |
16 | #include <stdio.h> |
17 | |
18 | #include "util.h" |
19 | #include "error.h" |
20 | |
21 | |
22 | extern char *yytext; |
23 | |
24 | int lineno = 1; |
25 | void (*reporter)(const char *s) = report_to_stderr; |
26 | |
27 | |
28 | void yywarn(const char *s) |
29 | { |
30 | /* we use yywarn only when starting */ |
31 | fprintf(stderr, "%d: warning: %s near \"%s\"\n", lineno, s, yytext); |
32 | } |
33 | |
34 | |
35 | void yyerrorf(const char *fmt, ...) |
36 | { |
37 | va_list ap; |
38 | char *buf; |
39 | int n; |
40 | |
41 | va_start(ap, fmt); |
42 | n = vsnprintf(NULL, 0, fmt, ap); |
43 | va_end(ap); |
44 | buf = alloc_size(n+1); |
45 | va_start(ap, fmt); |
46 | vsnprintf(buf, n+1, fmt, ap); |
47 | va_end(ap); |
48 | fail("%s", buf); |
49 | free(buf); |
50 | } |
51 | |
52 | |
53 | void yyerror(const char *s) |
54 | { |
55 | yyerrorf("%s", s); |
56 | } |
57 | |
58 | |
59 | void report_parse_error(const char *s) |
60 | { |
61 | fprintf(stderr, "%d: %s near \"%s\"\n", lineno, s, yytext); |
62 | exit(1); |
63 | } |
64 | |
65 | |
66 | void report_to_stderr(const char *s) |
67 | { |
68 | fprintf(stderr, "%s\n", s); |
69 | exit(1); |
70 | } |
71 | |
72 | |
73 | void fail(const char *fmt, ...) |
74 | { |
75 | va_list ap; |
76 | char *s; |
77 | |
78 | va_start(ap, fmt); |
79 | s = stralloc_vprintf(fmt, ap); |
80 | va_end(ap); |
81 | reporter(s); |
82 | free(s); |
83 | } |
84 |
Branches:
master