winedump: Print the network share name in .lnk files.
[wine] / tools / winedump / symbol.c
1 /*
2  *  Symbol functions
3  *
4  *  Copyright 2000 Jon Griffiths
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include "winedump.h"
25
26
27 /* Items that are swapped in arguments after the symbol structure
28  * has been populated
29  */
30 static const char * const swap_after[] =
31 {
32   "\r", " ", /* Remove whitespace, normalise pointers and brackets */
33   "\t", " ",
34   "  ", " ",
35   " * ", " *",
36   "* *", "**",
37   "* ", "*",
38   " ,", ",",
39   "( ", "(",
40   " )", ")",
41   "wchar_t", "WCHAR", /* Help with Unicode compiles */
42   "wctype_t", "WCHAR",
43   "wint_t", "WCHAR",
44   NULL, NULL
45 };
46
47
48 /* Items containing these substrings are assumed to be wide character
49  * strings, unless they contain more that one '*'. A preceding 'LP'
50  * counts as a '*', so 'LPWCSTR *' is a pointer, not a string
51  */
52 static const char * const wide_strings[] =
53 {
54   "WSTR", "WCSTR", NULL
55 };
56
57 /* Items containing these substrings are assumed to be wide characters,
58  * unless they contain one '*'. A preceding 'LP' counts as a '*',
59  * so 'WCHAR *' is string, while 'LPWCHAR *' is a pointer
60  */
61 static const char * const wide_chars[] =
62 {
63   "WCHAR", NULL
64 };
65
66 /* Items containing these substrings are assumed to be ASCII character
67  * strings, as above
68  */
69 static const char * const ascii_strings[] =
70 {
71   "STR", "CSTR", NULL
72 };
73
74
75 /* Items containing these substrings are assumed to be ASCII characters,
76  * as above
77  */
78 static const char * const ascii_chars[] =
79 {
80   "CHAR", "char", NULL
81 };
82
83 /* Any type other than the following will produce a FIXME warning with -v
84  * when mapped to a long, to allow fixups
85  */
86 static const char * const known_longs[] =
87 {
88   "char", "CHAR", "float", "int", "INT", "short", "SHORT", "long", "LONG",
89   "WCHAR", "BOOL", "bool", "INT16", "WORD", "DWORD", NULL
90 };
91
92 int symbol_init(parsed_symbol* sym, const char* name)
93 {
94     memset(sym, 0, sizeof(parsed_symbol));
95     sym->symbol = strdup(name);
96     return 0;
97 }
98
99 /*******************************************************************
100  *         symbol_clear
101  *
102  * Free the memory used by a symbol and initialise it
103  */
104 void symbol_clear(parsed_symbol *sym)
105 {
106  int i;
107
108  assert (sym);
109  assert (sym->symbol);
110
111  free (sym->symbol);
112  free (sym->return_text);
113  free (sym->function_name);
114
115  for (i = sym->argc - 1; i >= 0; i--)
116  {
117    free (sym->arg_text [i]);
118    free (sym->arg_name [i]);
119  }
120  memset (sym, 0, sizeof (parsed_symbol));
121 }
122
123
124 /*******************************************************************
125  *         symbol_is_valid_c
126  *
127  * Check if a symbol is a valid C identifier
128  */
129 int symbol_is_valid_c(const parsed_symbol *sym)
130 {
131   char *name;
132
133   assert (sym);
134   assert (sym->symbol);
135
136   name = sym->symbol;
137
138   while (*name)
139   {
140     if (!isalnum (*name) && *name != '_')
141       return 0;
142     name++;
143   }
144   return 1;
145 }
146
147
148 /*******************************************************************
149  *         symbol_get_call_convention
150  *
151  * Return the calling convention of a symbol
152  */
153 const char *symbol_get_call_convention(const parsed_symbol *sym)
154 {
155   int call = sym->flags ? sym->flags : CALLING_CONVENTION;
156
157   assert (sym);
158   assert (sym->symbol);
159
160   if (call & SYM_CDECL)
161     return "cdecl";
162   return "stdcall";
163 }
164
165
166 /*******************************************************************
167  *         symbol_get_spec_type
168  *
169  * Get the .spec file text for a symbol's argument
170  */
171 const char *symbol_get_spec_type (const parsed_symbol *sym, size_t arg)
172 {
173   assert (arg < sym->argc);
174   switch (sym->arg_type [arg])
175   {
176   case ARG_STRING:      return "str";
177   case ARG_WIDE_STRING: return "wstr";
178   case ARG_POINTER:     return "ptr";
179   case ARG_DOUBLE:      return "double";
180   case ARG_STRUCT:
181   case ARG_FLOAT:
182   case ARG_LONG:        return "long";
183   }
184   assert (0);
185   return NULL;
186 }
187
188
189 /*******************************************************************
190  *         symbol_get_type
191  *
192  * Get the ARG_ constant for a type string
193  */
194 int   symbol_get_type (const char *string)
195 {
196   const char *iter = string;
197   const char * const *tab;
198   int ptrs = 0;
199
200   while (*iter && isspace(*iter))
201     iter++;
202   if (*iter == 'P' || *iter == 'H')
203     ptrs++; /* Win32 type pointer */
204
205   iter = string;
206   while (*iter)
207   {
208     if (*iter == '*' || (*iter == 'L' && iter[1] == 'P')
209         || (*iter == '[' && iter[1] == ']'))
210       ptrs++;
211     if (ptrs > 1)
212       return ARG_POINTER;
213     iter++;
214   }
215
216   /* 0 or 1 pointer */
217   tab = wide_strings;
218   while (*tab++)
219     if (strstr (string, tab[-1]))
220     {
221       if (ptrs < 2) return ARG_WIDE_STRING;
222       else          return ARG_POINTER;
223     }
224   tab = wide_chars;
225   while (*tab++)
226     if (strstr (string, tab[-1]))
227     {
228       if (!ptrs) return ARG_LONG;
229       else       return ARG_WIDE_STRING;
230     }
231   tab = ascii_strings;
232   while (*tab++)
233     if (strstr (string, tab[-1]))
234     {
235       if (ptrs < 2) return ARG_STRING;
236       else          return ARG_POINTER;
237     }
238   tab = ascii_chars;
239   while (*tab++)
240     if (strstr (string, tab[-1]))
241     {
242       if (!ptrs) return ARG_LONG;
243       else {
244         if (!strstr (string, "unsigned")) /* unsigned char * => ptr */
245           return ARG_STRING;
246       }
247     }
248
249   if (ptrs)
250     return ARG_POINTER; /* Pointer to some other type */
251
252   /* No pointers */
253   if (strstr (string, "double"))
254     return ARG_DOUBLE;
255
256   if (strstr (string, "float") || strstr (string, "FLOAT"))
257     return ARG_FLOAT;
258
259   if (strstr (string, "void") || strstr (string, "VOID"))
260     return ARG_VOID;
261
262   if (strstr (string, "struct") || strstr (string, "union"))
263     return ARG_STRUCT; /* Struct by value, ugh */
264
265   if (VERBOSE)
266   {
267     int known = 0;
268
269     tab = known_longs;
270     while (*tab++)
271     if (strstr (string, tab[-1]))
272     {
273       known = 1;
274       break;
275     }
276     /* Unknown types passed by value can be 'grep'ed out for fixup later */
277     if (!known)
278       printf ("/* FIXME: By value type: Assumed 'int' */ typedef int %s;\n",
279               string);
280   }
281   return ARG_LONG;
282 }
283
284
285 /*******************************************************************
286  *         symbol_clean_string
287  *
288  * Make a type string more Wine-friendly. Logically const :-)
289  */
290 void  symbol_clean_string (char *str)
291 {
292   const char * const *tab = swap_after;
293
294 #define SWAP(i, p, x, y) do { i = p; while ((i = str_replace (i, x, y))); } while(0)
295
296   while (tab [0])
297   {
298     char *p;
299     SWAP (p, str, tab [0], tab [1]);
300     tab += 2;
301   }
302   if (str [strlen (str) - 1] == ' ')
303     str [strlen (str) - 1] = '\0'; /* no trailing space */
304
305   if (*str == ' ')
306     memmove (str, str + 1, strlen (str)); /* No leading spaces */
307 }