* Makefile.in, demangle.c: Change default demangling style to
[binutils-gdb.git] / gdb / demangle.c
1 /* Basic C++ demangling support for GDB.
2 Copyright 1991, 1992 Free Software Foundation, Inc.
3 Written by Fred Fish at Cygnus Support.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21
22 /* This file contains support code for C++ demangling that is common
23 to a styles of demangling, and GDB specific. */
24
25 #include "defs.h"
26 #include "command.h"
27 #include "gdbcmd.h"
28 #include "demangle.h"
29 #include <string.h>
30
31 #ifndef DEMANGLING_STYLE
32 # define DEMANGLING_STYLE GNU_DEMANGLING_STYLE_STRING
33 #endif
34
35 /* The current demangling style in affect. Global so that the demangler
36 can read it (FIXME: change the interface) */
37
38 enum demangling_styles current_demangling_style;
39
40 /* String name for the current demangling style. Set by the "set demangling"
41 command, printed as part of the output by the "show demangling" command. */
42
43 static char *current_demangling_style_string;
44
45 /* List of supported demangling styles. Contains the name of the style as
46 seen by the user, and the enum value that corresponds to that style. */
47
48 static const struct demangler
49 {
50 char *demangling_style_name;
51 enum demangling_styles demangling_style;
52 char *demangling_style_doc;
53 } demanglers [] =
54 {
55 {AUTO_DEMANGLING_STYLE_STRING,
56 auto_demangling,
57 "Automatic selection based on executable"},
58 {GNU_DEMANGLING_STYLE_STRING,
59 gnu_demangling,
60 "GNU (g++) style demangling"},
61 {LUCID_DEMANGLING_STYLE_STRING,
62 lucid_demangling,
63 "Lucid (lcc) style demangling"},
64 {CFRONT_DEMANGLING_STYLE_STRING,
65 cfront_demangling,
66 "ARM (cfront) style demangling"},
67 {NULL, 0, NULL}
68 };
69
70 /* show current demangling style. */
71
72 static void
73 show_demangling_command (ignore, from_tty)
74 char *ignore;
75 int from_tty;
76 {
77 /* done automatically by show command. */
78 }
79
80
81 /* set current demangling style. called by the "set demangling" command
82 after it has updated the current_demangling_style_string to match
83 what the user has entered.
84
85 if the user has entered a string that matches a known demangling style
86 name in the demanglers[] array then just leave the string alone and update
87 the current_demangling_style enum value to match.
88
89 if the user has entered a string that doesn't match, including an empty
90 string, then print a list of the currently known styles and restore
91 the current_demangling_style_string to match the current_demangling_style
92 enum value.
93
94 Note: Assumes that current_demangling_style_string always points to
95 a malloc'd string, even if it is a null-string. */
96
97 static void
98 set_demangling_command (ignore, from_tty)
99 char *ignore;
100 int from_tty;
101 {
102 const struct demangler *dem;
103
104 /* First just try to match whatever style name the user supplied with
105 one of the known ones. Don't bother special casing for an empty
106 name, we just treat it as any other style name that doesn't match.
107 If we match, update the current demangling style enum. */
108
109 for (dem = demanglers; dem -> demangling_style_name != NULL; dem++)
110 {
111 if (strcmp (current_demangling_style_string,
112 dem -> demangling_style_name) == 0)
113 {
114 current_demangling_style = dem -> demangling_style;
115 break;
116 }
117 }
118
119 /* Check to see if we found a match. If not, gripe about any non-empty
120 style name and supply a list of valid ones. FIXME: This should
121 probably be done with some sort of completion and with help. */
122
123 if (dem -> demangling_style_name == NULL)
124 {
125 if (*current_demangling_style_string != '\0')
126 {
127 printf ("Unknown demangling style `%s'.\n",
128 current_demangling_style_string);
129 }
130 printf ("The currently understood settings are:\n\n");
131 for (dem = demanglers; dem -> demangling_style_name != NULL; dem++)
132 {
133 printf ("%-10s %s\n", dem -> demangling_style_name,
134 dem -> demangling_style_doc);
135 if (dem -> demangling_style == current_demangling_style)
136 {
137 free (current_demangling_style_string);
138 current_demangling_style_string =
139 strdup (dem -> demangling_style_name);
140 }
141 }
142 if (current_demangling_style == unknown_demangling)
143 {
144 /* This can happen during initialization if gdb is compiled with
145 a DEMANGLING_STYLE value that is unknown, so pick the first
146 one as the default. */
147 current_demangling_style = demanglers[0].demangling_style;
148 current_demangling_style_string =
149 strdup (demanglers[0].demangling_style_name);
150 warning ("`%s' style demangling chosen as the default.\n",
151 current_demangling_style_string);
152 }
153 }
154 }
155
156 /* Fake a "set demangling" command. */
157
158 void
159 set_demangling_style (style)
160 char *style;
161 {
162 if (current_demangling_style_string != NULL)
163 {
164 free (current_demangling_style_string);
165 }
166 current_demangling_style_string = strdup (style);
167 set_demangling_command ((char *) NULL, 0);
168 }
169
170 void
171 _initialize_demangler ()
172 {
173 struct cmd_list_element *set, *show;
174
175 set = add_set_cmd ("demangle-style", class_support, var_string_noescape,
176 (char *) &current_demangling_style_string,
177 "Set the current C++ demangling style.",
178 &setlist);
179 show = add_show_from_set (set, &showlist);
180 set -> function.cfunc = set_demangling_command;
181 show -> function.cfunc = show_demangling_command;
182
183 /* Set the default demangling style chosen at compilation time. */
184 set_demangling_style (DEMANGLING_STYLE);
185 }