-
Notifications
You must be signed in to change notification settings - Fork 1
/
tools.c
277 lines (242 loc) · 5.04 KB
/
tools.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "common.h"
#include "tools.h"
#include "main.h"
static const char whitespace_chars[] = " \t\n\v\f\r";
static char output_prefix[64] = "";
static void strip_colors(char *str)
{
for(char *ptr = str; *ptr; ptr++)
{
if(*ptr == '\033')
{
while(*ptr != 'm')
memmove(ptr, ptr + 1, strlen(ptr));
// Now get rid of the 'm'
memmove(ptr, ptr + 1, strlen(ptr));
}
}
}
static int my_vprintf(const char *fmt, va_list args)
{
static char buf[2048]; // Just assume this is big enough for any output..
int ret;
if(!no_colors)
return vprintf(fmt, args);
ret = vsnprintf(buf, sizeof(buf), fmt, args);
strip_colors(buf);
printf("%s", buf);
return ret;
}
void debug(char *text, ...)
{
va_list va;
if(!debug_output_enabled)
return;
if(*output_prefix)
printf("%s", output_prefix);
if(!no_colors)
printf("\033[" COLOR_LIGHT_BLUE "m");
else
printf("DEBUG: ");
va_start(va, text);
my_vprintf(text, va);
if(!no_colors)
printf("\033[0m");
printf("\n");
va_end(va);
}
void out_prefix(char *text, ...)
{
va_list va;
if(!text)
{
*output_prefix = '\0';
return;
}
va_start(va, text);
vsnprintf(output_prefix, sizeof(output_prefix), text, va);
if(no_colors)
strip_colors(output_prefix);
va_end(va);
}
void out(char *text, ...)
{
va_list va;
if(*output_prefix)
printf("%s", output_prefix);
va_start(va, text);
my_vprintf(text, va);
printf("\n");
va_end(va);
}
void out_color(const char *color, char *text, ...)
{
va_list va;
if(*output_prefix)
printf("%s", output_prefix);
if(!no_colors)
printf("\033[%sm", color);
va_start(va, text);
my_vprintf(text, va);
if(!no_colors)
printf("\033[0m");
printf("\n");
va_end(va);
}
void error(char *text, ...)
{
va_list va;
if(!no_colors)
printf("\033[" COLOR_LIGHT_RED "m");
else
printf("ERROR: ");
va_start(va, text);
my_vprintf(text, va);
if(!no_colors)
printf("\033[0m");
printf("\n");
va_end(va);
}
char *ltrim(char *str)
{
size_t len;
for(len = strlen(str); len && isspace(str[len - 1]); len--)
;
str[len] = '\0';
return str;
}
char *rtrim(char *str)
{
char *tmp = str + strspn(str, whitespace_chars);
memmove(str, tmp, strlen(str) - (tmp - str) + 1);
return str;
}
// from ircu 2.10.12
/*
* Compare if a given string (name) matches the given
* mask (which can contain wild cards: '*' - match any
* number of chars, '?' - match any single character.
*
* return 0, if match
* 1, if no match
*
* Originally by Douglas A Lewis ([email protected])
* Rewritten by Timothy Vogelsang (netski), [email protected]
*/
/** Check a string against a mask.
* This test checks using traditional IRC wildcards only: '*' means
* match zero or more characters of any type; '?' means match exactly
* one character of any type. A backslash escapes the next character
* so that a wildcard may be matched exactly.
* @param[in] mask Wildcard-containing mask.
* @param[in] name String to check against \a mask.
* @return Zero if \a mask matches \a name, non-zero if no match.
*/
int match(const char *mask, const char *name)
{
const char *m = mask, *n = name;
const char *m_tmp = mask, *n_tmp = name;
int star_p;
for (;;) switch (*m) {
case '\0':
if (!*n)
return 0;
backtrack:
if (m_tmp == mask)
return 1;
m = m_tmp;
n = ++n_tmp;
break;
case '\\':
m++;
/* allow escaping to force capitalization */
if (*m++ != *n++)
return 1;
break;
case '*': case '?':
for (star_p = 0; ; m++) {
if (*m == '*')
star_p = 1;
else if (*m == '?') {
if (!*n++)
goto backtrack;
} else break;
}
if (star_p) {
if (!*m)
return 0;
else if (*m == '\\') {
m_tmp = ++m;
if (!*m)
return 1;
for (n_tmp = n; *n && *n != *m; n++) ;
} else {
m_tmp = m;
for (n_tmp = n; *n && tolower(*n) != tolower(*m); n++) ;
}
}
/* and fall through */
default:
if (!*n)
return *m != '\0';
if (tolower(*m) != tolower(*n))
goto backtrack;
m++;
n++;
break;
}
}
size_t strlcpy(char *out, const char *in, size_t len)
{
size_t in_len;
in_len = strlen(in);
if (in_len < --len)
memcpy(out, in, in_len + 1);
else
{
memcpy(out, in, len);
out[len] = '\0';
}
return in_len;
}
int file_exists(const char *file)
{
struct stat statbuf;
return (stat(file, &statbuf) == 0);
}
void expand_num_args(char *buf, size_t buf_size, const char *str, unsigned int argc, ...)
{
va_list args;
size_t len = 0;
char **arg_list;
va_start(args, argc);
arg_list = calloc(argc, sizeof(char *));
for(unsigned int i = 0; i < argc; i++)
arg_list[i] = va_arg(args, char *);
va_end(args);
while(*str && len < buf_size - 1)
{
if(*str != '$' || !isdigit(*(str + 1)))
buf[len++] = *(str++);
else // $something
{
char *end;
unsigned long idx = strtoul(str + 1, &end, 10);
if(!idx || idx > argc || (len + strlen(arg_list[idx - 1]) >= buf_size - 1))
{
buf[len++] = *(str++); // copy $
continue;
}
len += strlcpy(buf + len, arg_list[idx - 1], buf_size - len);
str = end;
}
}
buf[len] = '\0';
free(arg_list);
}
char *xstrdup(const char *str)
{
if(!str)
return NULL;
return strdup(str);
}