aboutsummaryrefslogtreecommitdiffstats
path: root/src/common/error.c
blob: ef6edff6fe36928378cdcb94bdb03f218eeb856b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 * blogc: A blog compiler.
 * Copyright (C) 2014-2018 Rafael G. Martins <rafael@rafaelmartins.eng.br>
 *
 * This program can be distributed under the terms of the BSD License.
 * See the file LICENSE.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */

#ifdef HAVE_WINDOWS_H
#include <windows.h>
#endif /* HAVE_WINDOWS_H */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "error.h"
#include "utils.h"


bc_error_t*
bc_error_new(bc_error_type_t type, const char *msg)
{
    bc_error_t *err = bc_malloc(sizeof(bc_error_t));
    err->type = type;
    err->msg = bc_strdup(msg);
    return err;
}


bc_error_t*
bc_error_new_vprintf(bc_error_type_t type, const char *format, va_list ap)
{
    char *tmp = bc_strdup_vprintf(format, ap);
    bc_error_t *rv = bc_error_new(type, tmp);
    free(tmp);
    return rv;
}


bc_error_t*
bc_error_new_printf(bc_error_type_t type, const char *format, ...)
{
    va_list ap;
    va_start(ap, format);
    bc_error_t *rv = bc_error_new_vprintf(type, format, ap);
    va_end(ap);
    return rv;
}


bc_error_t*
bc_error_new_errno_vprintf(bc_error_type_t type, int errno_, const char *format,
    va_list ap)
{
    char *tmp = bc_strdup_vprintf(format, ap);
#if defined(WIN32) || defined(_WIN32)
    LPTSTR buf = "bola";
    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
        NULL, errno_, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&buf,
        0, NULL);
    bc_error_t *rv = bc_error_new_printf(type, "%s: %s", tmp, buf);
    LocalFree(buf);
#else
    bc_error_t *rv = bc_error_new_printf(type, "%s: %s", tmp, strerror(errno_));
#endif
    free(tmp);
    return rv;
}


bc_error_t*
bc_error_new_errno_printf(bc_error_type_t type, int errno_, const char *format, ...)
{
    va_list ap;
    va_start(ap, format);
    bc_error_t *rv = bc_error_new_errno_vprintf(type, errno_, format, ap);
    va_end(ap);
    return rv;
}


bc_error_t*
bc_error_new_default_errno_printf(bc_error_type_t type, const char *format, ...)
{
    va_list ap;
    va_start(ap, format);
#if defined(WIN32) || defined(_WIN32)
    bc_error_t *rv = bc_error_new_errno_vprintf(type, GetLastError(), format, ap);
#else
    bc_error_t *rv = bc_error_new_errno_vprintf(type, errno, format, ap);
#endif
    va_end(ap);
    return rv;
}


bc_error_t*
bc_error_parser(bc_error_type_t type, const char *src, size_t src_len,
    size_t current, const char *format, ...)
{
    va_list ap;
    va_start(ap, format);
    char *msg = bc_strdup_vprintf(format, ap);
    va_end(ap);

    size_t lineno = 1;
    size_t linestart = 0;
    size_t lineend = 0;
    size_t pos = 1;

    for (size_t i = 0; i < src_len; i++) {
        char c = src[i];
        if (i < current) {
            if ((i + 1) < src_len) {
                if ((c == '\n' && src[i + 1] == '\r') ||
                    (c == '\r' && src[i + 1] == '\n'))
                {
                    lineno++;
                    i++;
                    pos = 1;
                    if ((i + 1) < src_len)
                        linestart = i + 1;
                    continue;
                }
            }
            if (c == '\n' || c == '\r') {
                lineno++;
                pos = 1;
                if ((i + 1) < src_len)
                    linestart = i + 1;
                continue;
            }
            pos++;
        }
        else if (c == '\n' || c == '\r') {
            lineend = i;
            break;
        }
    }

    if (lineend <= linestart && src_len >= linestart)
        lineend = src_len;

    char *line = bc_strndup(src + linestart, lineend - linestart);

    bc_error_t *rv = NULL;

    if (line[0] == '\0')  // "near" message isn't useful if line is empty
        rv = bc_error_new(type, msg);
    else
        rv = bc_error_new_printf(type,
            "%s\nError occurred near line %d, position %d: %s", msg, lineno,
            pos, line);

    free(msg);
    free(line);

    return rv;
}


// error handling is centralized here for the sake of simplicity :/
void
bc_error_print(bc_error_t *err, const char *prefix)
{
    if (err == NULL)
        return;

    if (prefix != NULL)
        fprintf(stderr, "%s: ", prefix);

    switch(err->type) {
        case BC_ERROR_CONFIG_PARSER:
            fprintf(stderr, "error: config-parser: %s\n", err->msg);
            break;
        case BC_ERROR_FILE:
            fprintf(stderr, "error: file: %s\n", err->msg);
            break;
        case BC_ERROR_THREAD:
            fprintf(stderr, "error: thread: %s\n", err->msg);
            break;
        case BLOGC_ERROR_SOURCE_PARSER:
            fprintf(stderr, "error: source: %s\n", err->msg);
            break;
        case BLOGC_ERROR_TEMPLATE_PARSER:
            fprintf(stderr, "error: template: %s\n", err->msg);
            break;
        case BLOGC_ERROR_LOADER:
            fprintf(stderr, "error: loader: %s\n", err->msg);
            break;
        case BLOGC_WARNING_DATETIME_PARSER:
            fprintf(stderr, "warning: datetime: %s\n", err->msg);
            break;
        case BLOGC_MAKE_ERROR_SETTINGS:
            fprintf(stderr, "error: settings: %s\n", err->msg);
            break;
        case BLOGC_MAKE_ERROR_EXEC:
            fprintf(stderr, "error: exec: %s\n", err->msg);
            break;
        default:
            fprintf(stderr, "error: %s\n", err->msg);
    }
}


void
bc_error_free(bc_error_t *err)
{
    if (err == NULL)
        return;
    free(err->msg);
    free(err);
}