Root/error.c

Source at commit 103933acf59927d84df7f99d9580dfc0f45059c2 created 2 years 10 months ago.
By Werner Almesberger, fix a few whitespace issues in previous commit
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
22extern char *yytext;
23
24int lineno = 1;
25void (*reporter)(const char *s) = report_to_stderr;
26
27
28void 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
35void 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
53void yyerror(const char *s)
54{
55    yyerrorf("%s", s);
56}
57
58
59void report_parse_error(const char *s)
60{
61    fprintf(stderr, "%d: %s near \"%s\"\n", lineno, s, yytext);
62    exit(1);
63}
64
65
66void report_to_stderr(const char *s)
67{
68    fprintf(stderr, "%s\n", s);
69    exit(1);
70}
71
72
73void 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

Archive Download this file

Branches:
master



interactive