-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.c
139 lines (116 loc) · 1.73 KB
/
misc.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
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
#include <u.h>
#include <libc.h>
#include "cuefs.h"
#include "y.tab.h"
int verbosity = 0;
void*
erealloc(void *p, ulong s)
{
p = realloc(p, s);
if(s > 0 && p == nil)
sysfatal("realloc: %r");
setrealloctag(p, getcallerpc(&p));
return p;
}
void*
emallocz(ulong s, int clr)
{
void* p = mallocz(s, clr);
if(p == nil)
sysfatal("mallocz: %r");
setmalloctag(p, getcallerpc(&s));
return p;
}
void*
emalloc(ulong s)
{
void* p = malloc(s);
if(p == nil)
sysfatal("malloc: %r");
setmalloctag(p, getcallerpc(&s));
return p;
}
char*
esmprint(char *fmt, ...)
{
char *str;
va_list args;
va_start(args, fmt);
str = vsmprint(fmt, args);
if(str == nil)
sysfatal("vsmprint: %r");
va_end(args);
return str;
}
/* it's a mess */
void
yywarn(char *str)
{
extern int yylineno;
fprint(2, "%s:%d: %s\n", fname, yylineno, str);
}
void
yyerror(char *str)
{
yywarn(str);
exits(str);
}
char*
setstr(char *baseline, char **dest, char *str)
{
if(*dest != nil && *dest != baseline)
free(*dest);
*dest = str;
return str;
}
char*
strreplace(char *str, char a, char b)
{
for(char *c = str; *c != 0; c++)
if(*c == a)
*c = b;
return str;
}
void
parserwarn(char *fmt, ...)
{
char *str;
va_list args;
va_start(args, fmt);
str = vsmprint(fmt, args);
yywarn(str);
free(str);
va_end(args);
}
void
parserfatal(char *fmt, ...)
{
char *str;
va_list args;
va_start(args, fmt);
str = vsmprint(fmt, args);
yywarn(str);
free(str);
va_end(args);
exits("cantparse");
}
void
debug(char *fmt, ...)
{
va_list args;
if(verbosity < 2)
return;
va_start(args, fmt);
vfprint(2, fmt, args);
va_end(args);
}
void
trace(char *fmt, ...)
{
va_list args;
if(verbosity < 3)
return;
va_start(args, fmt);
vfprint(2, fmt, args);
va_end(args);
}