-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwb_errors.c
60 lines (49 loc) · 1.12 KB
/
pwb_errors.c
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
#include "pwb_builtin.h"
#include "pwb_handle.h"
#include "pwb_errors.h"
#include <stdarg.h>
#include <stdio.h>
type_error_sink error_sink = pwb_error_shell_var;
int pwb_error_shell_var(const char *format, ...)
{
va_list args;
va_start(args, format);
int len = vsnprintf(NULL, 0, format, args);
va_end(args);
if (len > 0)
{
++len;
char *buffer = xmalloc(len);
if (buffer)
{
va_start(args, format);
vsnprintf(buffer, len, format, args);
va_end(args);
SHELL_VAR *sv = find_variable(ERROR_NAME);
if (!sv)
sv = bind_variable(ERROR_NAME, "", 0);
if (sv)
{
pwb_dispose_variable_value(sv);
sv->value = buffer;
}
}
}
return len;
}
int pwb_error_print(const char *format, ...)
{
va_list args;
va_start(args, format);
int len = fprintf(stderr, format, args);
va_end(args);
return len;
}
void pwb_error_clear(void)
{
SHELL_VAR *sv = find_variable(ERROR_NAME);
if (!sv)
sv = bind_variable(ERROR_NAME, "", 0);
if (sv)
pwb_dispose_variable_value(sv);
}