aboutsummaryrefslogtreecommitdiff
path: root/src/libmach/mangle.c
blob: 5bf49c9d4287afa97b4d0aa0dabb95dff08a61a8 (plain)
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
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <mach.h>

static char *(*demanglers[])(char*, char*) =
{
	demanglegcc2,
	demanglegcc3,
};

char*
demangle(char *s, char *buf, int strip)
{
	char *t;
	char *r, *w;
	int i, nangle, nparen;

	t = nil;
	for(i=0; i<nelem(demanglers); i++){
		t = demanglers[i](s, buf);
		if(t != s)
			break;
	}
	if(t == s || !strip)
		return t;

	/* copy name without <> and () - not right, but convenient */
	/* convert :: to $ - not right, but convenient (should fix acid) */
	nangle = 0;
	nparen = 0;
	for(r=w=buf; *r; r++){
		switch(*r){
		case '<':
			nangle++;
			break;
		case '>':
			nangle--;
			break;
		case '(':
			nparen++;
			break;
		case ')':
			nparen--;
			break;
		default:
			if(nparen == 0 && nangle == 0)
				*w++ = *r;
			break;
		}
	}
	*w = 0;
	return buf;
}

#ifdef TEST
void
main(int argc, char **argv)
{
	int i;

	for(i=1; i<argc; i++){
		print("%s\n", demangle(argv[i], 0));
		print("\t%s\n", demangle(argv[i], 1));
	}
	exits(nil);
}
#endif