forked from torvalds/uemacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.c
58 lines (51 loc) · 1.28 KB
/
debug.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
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include "estruct.h"
#include "edef.h"
#include "efunc.h"
void debug(const char *fn, const char *fmt, ...)
{
if (fdebug) {
int c; /* current char in format string */
FILE *f;
int v;
va_list ap;
f = fopen(fn, "a");
if (f == NULL)
return;
va_start(ap, fmt);
while ((c = *fmt++) != 0) {
if (c != '%') {
fputc(c, f);
} else {
c = *fmt++;
switch (c) {
case 'd':
v = va_arg(ap, int);
fprintf(f, "0x%x", v);
if (v >= (CONTROL + 0x20) && v <= (CONTROL + 0x7e))
fprintf(f, " (CONTROL + '%c')", v - CONTROL);
else if (v >= (META + 0x20) && v <= (META + 0x7e))
fprintf(f, " (META + '%c')", v - META);
else if (v >= (CTLX + CONTROL + 0x20) && v <= (CTLX + CONTROL + 0x7e))
fprintf(f, " (CTLX | CONTROL | '%c')", v - CTLX - CONTROL);
else if (v >= (META + CONTROL + 0x20) && v <= (META + CONTROL + 0x7e))
fprintf(f, " (META | CONTROL | '%c')", v - META - CONTROL);
else if (v >= (CTLX + 0x20) && v <= (CTLX + 0x7e))
fprintf(f, " (CTLX | '%c')", v - CTLX);
else if (v >= 0x20 && v <= 0x7e)
fprintf(f, " ('%c')", (char)v);
break;
case 's':
fprintf(f, "%s", va_arg(ap, char *));
break;
default:
fputc(c, f);
}
}
}
va_end(ap);
fclose(f);
}
}