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: #include "sysdep.h"
26: #include "bfd.h"
27: #include "libiberty.h"
28: #include "demangle.h"
29: #include "getopt.h"
30: #include "safe-ctype.h"
31: #include "bucomm.h"
32:
33: static int flags = DMGL_PARAMS | DMGL_ANSI | DMGL_VERBOSE;
34: static int strip_underscore = TARGET_PREPENDS_UNDERSCORE;
35:
36: static const struct option long_options[] =
37: {
38: {"strip-underscore", no_argument, NULL, '_'},
39: {"format", required_argument, NULL, 's'},
40: {"help", no_argument, NULL, 'h'},
41: {"no-params", no_argument, NULL, 'p'},
42: {"no-strip-underscores", no_argument, NULL, 'n'},
43: {"no-verbose", no_argument, NULL, 'i'},
44: {"types", no_argument, NULL, 't'},
45: {"version", no_argument, NULL, 'v'},
46: {NULL, no_argument, NULL, 0}
47: };
48:
49: static void
50: demangle_it (char *mangled_name)
51: {
52: char *result;
53: unsigned int skip_first = 0;
54:
55:
56:
57:
58: if (mangled_name[0] == '.' || mangled_name[0] == '$')
59: ++skip_first;
60: if (strip_underscore && mangled_name[skip_first] == '_')
61: ++skip_first;
62:
63: result = cplus_demangle (mangled_name + skip_first, flags);
64:
65: if (result == NULL)
66: printf (mangled_name);
67: else
68: {
69: if (mangled_name[0] == '.')
70: putchar ('.');
71: printf (result);
72: free (result);
73: }
74: }
75:
76: static void
77: print_demangler_list (FILE *stream)
78: {
79: const struct demangler_engine *demangler;
80:
81: fprintf (stream, "{%s", libiberty_demanglers->demangling_style_name);
82:
83: for (demangler = libiberty_demanglers + 1;
84: demangler->demangling_style != unknown_demangling;
85: ++demangler)
86: fprintf (stream, ",%s", demangler->demangling_style_name);
87:
88: fprintf (stream, "}");
89: }
90:
91: static void
92: usage (FILE *stream, int status)
93: {
94: fprintf (stream, "\
95: Usage: %s [options] [mangled names]\n", program_name);
96: fprintf (stream, "\
97: Options are:\n\
98: [-_|--strip-underscore] Ignore first leading underscore%s\n",
99: TARGET_PREPENDS_UNDERSCORE ? " (default)" : "");
100: fprintf (stream, "\
101: [-n|--no-strip-underscore] Do not ignore a leading underscore%s\n",
102: TARGET_PREPENDS_UNDERSCORE ? "" : " (default)");
103: fprintf (stream, "\
104: [-p|--no-params] Do not display function arguments\n\
105: [-i|--no-verbose] Do not show implementation details (if any)\n\
106: [-t|--types] Also attempt to demangle type encodings\n\
107: [-s|--format ");
108: print_demangler_list (stream);
109: fprintf (stream, "]\n");
110:
111: fprintf (stream, "\
112: [@<file>] Read extra options from <file>\n\
113: [-h|--help] Display this information\n\
114: [-v|--version] Show the version information\n\
115: Demangled names are displayed to stdout.\n\
116: If a name cannot be demangled it is just echoed to stdout.\n\
117: If no names are provided on the command line, stdin is read.\n");
118: if (REPORT_BUGS_TO[0] && status == 0)
119: fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
120: exit (status);
121: }
122:
123:
124:
125:
126:
127: static const char *
128: standard_symbol_characters (void)
129: {
130: return "_$.";
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: static const char *
164: hp_symbol_characters (void)
165: {
166: return "_$.<>#,*&[]:(){}";
167: }
168:
169: extern int main (int, char **);
170:
171: int
172: main (int argc, char **argv)
173: {
174: int c;
175: const char *valid_symbols;
176: enum demangling_styles style = auto_demangling;
177:
178: program_name = argv[0];
179: xmalloc_set_program_name (program_name);
180:
181: expandargv (&argc, &argv);
182:
183: while ((c = getopt_long (argc, argv, "_hinps:tv", long_options, (int *) 0)) != EOF)
184: {
185: switch (c)
186: {
187: case '?':
188: usage (stderr, 1);
189: break;
190: case 'h':
191: usage (stdout, 0);
192: case 'n':
193: strip_underscore = 0;
194: break;
195: case 'p':
196: flags &= ~ DMGL_PARAMS;
197: break;
198: case 't':
199: flags |= DMGL_TYPES;
200: break;
201: case 'i':
202: flags &= ~ DMGL_VERBOSE;
203: break;
204: case 'v':
205: print_version ("c++filt");
206: return 0;
207: case '_':
208: strip_underscore = 1;
209: break;
210: case 's':
211: style = cplus_demangle_name_to_style (optarg);
212: if (style == unknown_demangling)
213: {
214: fprintf (stderr, "%s: unknown demangling style `%s'\n",
215: program_name, optarg);
216: return 1;
217: }
218: cplus_demangle_set_style (style);
219: break;
220: }
221: }
222:
223: if (optind < argc)
224: {
225: for ( ; optind < argc; optind++)
226: {
227: demangle_it (argv[optind]);
228: putchar ('\n');
229: }
230:
231: return 0;
232: }
233:
234: switch (current_demangling_style)
235: {
236: case gnu_demangling:
237: case lucid_demangling:
238: case arm_demangling:
239: case java_demangling:
240: case edg_demangling:
241: case gnat_demangling:
242: case gnu_v3_demangling:
243: case auto_demangling:
244: valid_symbols = standard_symbol_characters ();
245: break;
246: case hp_demangling:
247: valid_symbols = hp_symbol_characters ();
248: break;
249: default:
250:
251:
252:
253: fatal ("Internal error: no symbol alphabet for current style");
254: }
255:
256: for (;;)
257: {
258: static char mbuffer[32767];
259: unsigned i = 0;
260:
261: c = getchar ();
262:
263: while (c != EOF && (ISALNUM (c) || strchr (valid_symbols, c)))
264: {
265: if (i >= sizeof (mbuffer) - 1)
266: break;
267: mbuffer[i++] = c;
268: c = getchar ();
269: }
270:
271: if (i > 0)
272: {
273: mbuffer[i] = 0;
274: demangle_it (mbuffer);
275: }
276:
277: if (c == EOF)
278: break;
279:
280:
281:
282: putchar (c);
283: if (c == '\n')
284: fflush (stdout);
285: }
286:
287: fflush (stdout);
288: return 0;
289: }