-
Notifications
You must be signed in to change notification settings - Fork 8
/
demangle.c
88 lines (68 loc) · 1.27 KB
/
demangle.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
/*
* Simple demangler interface using the external `c++filt` program.
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
struct demangler {
int in;
int out;
bool ok;
};
static struct demangler d;
/* popen() doesn't provide bidirectional streams, do it manually */
void setup_demangler(void)
{
int p1[2], p2[2];
d.ok = false;
if (pipe(p1) < 0 || pipe(p2) < 0)
return;
switch (fork()) {
case -1: /* error */
return;
case 0: /* child */
dup2(p1[0], STDIN_FILENO);
dup2(p2[1], STDOUT_FILENO);
close(p1[0]);
close(p1[1]);
close(p2[0]);
close(p2[1]);
execlp("c++filt", NULL);
break;
default: /* parent */
d.in = p1[1];
d.out = p2[0];
close(p1[0]);
close(p2[1]);
d.ok = true;
break;
}
}
void finish_demangler(void)
{
if (d.ok) {
close(d.in);
close(d.out);
}
d.ok = false;
}
bool demangler_enabled(void)
{
return d.ok;
}
int demangle(const char *input, char *output, int outlen)
{
int ret;
int len = strlen(input);
char flush = '\n';
if (!d.ok) {
memcpy(output, input, len+1);
return 0;
}
write(d.in, input, len);
write(d.in, &flush, 1);
ret = read(d.out, output, outlen);
output[ret - 1] = '\0';
return ret;
}