2 * Prototype search and parsing functions
4 * Copyright 2000 Jon Griffiths
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 static char *grep_buff
= NULL
;
23 static char *fgrep_buff
= NULL
;
25 static int symbol_from_prototype (parsed_symbol
*sym
, const char *prototype
);
26 static const char *get_type (parsed_symbol
*sym
, const char *proto
, int arg
);
29 /*******************************************************************
32 * Call Patrik Stridvall's 'function_grep.pl' script to retrieve a
33 * function prototype from include file(s)
35 int symbol_search (parsed_symbol
*sym
)
37 static const size_t MAX_RESULT_LEN
= 1024;
41 assert (globals
.do_code
);
42 assert (globals
.directory
);
43 assert (sym
&& sym
->symbol
);
45 if (!symbol_is_valid_c (sym
))
49 grep_buff
= (char *) malloc (MAX_RESULT_LEN
);
52 fgrep_buff
= (char *) malloc (MAX_RESULT_LEN
);
54 if (!grep_buff
|| !fgrep_buff
)
55 fatal ("Out of Memory");
57 /* Use 'grep' to tell us which possible files the function is in,
58 * then use 'function_grep.pl' to get the prototype. If this fails the
59 * first time then give grep a more general query (that doesn't
60 * require an opening argument brace on the line with the function name).
65 char *cmd
= str_create (4, "grep -d recurse -l \"", sym
->symbol
,
66 !attempt
? "[:blank:]*(\" " : "\" ", globals
.directory
);
71 fflush (NULL
); /* See 'man popen' */
73 if (!(grep
= popen (cmd
, "r")))
74 fatal ("Cannot execute grep -l");
77 while (fgets (grep_buff
, MAX_RESULT_LEN
, grep
))
80 for (i
= 0; grep_buff
[i
] && grep_buff
[i
] != '\n' ; i
++)
87 cmd
= str_create (5, "function_grep.pl ", sym
->symbol
,
88 " \"", grep_buff
, "\"");
93 fflush (NULL
); /* See 'man popen' */
95 if (!(f_grep
= popen (cmd
, "r")))
96 fatal ("Cannot execute function_grep.pl");
99 while (fgets (grep_buff
, MAX_RESULT_LEN
, f_grep
))
101 char *iter
= grep_buff
;
103 /* Keep only the first line */
104 symbol_clean_string(grep_buff
);
106 for (i
= 0; grep_buff
[i
] && grep_buff
[i
] != '\n' ; i
++)
113 while ((iter
= strstr (iter
, sym
->symbol
)))
115 if (iter
> grep_buff
&& iter
[-1] == ' ' &&
116 (iter
[strlen (sym
->symbol
)] == ' ' ||
117 iter
[strlen (sym
->symbol
)] == '('))
120 printf ("Prototype '%s' looks OK, processing\n", grep_buff
);
122 if (!symbol_from_prototype (sym
, grep_buff
))
129 puts ("Failed, trying next");
132 iter
+= strlen (sym
->symbol
);
141 return -1; /* Not found */
145 /*******************************************************************
146 * symbol_from_prototype
148 * Convert a C prototype into a symbol
150 static int symbol_from_prototype (parsed_symbol
*sym
, const char *proto
)
155 proto
= get_type (sym
, proto
, -1); /* Get return type */
159 iter
= (char *)str_match (proto
, sym
->symbol
, &found
);
164 /* Calling Convention */
165 iter
= strchr (iter
, ' ');
169 call
= str_substring (proto
, iter
);
171 if (!strcasecmp (call
, "cdecl") || !strcasecmp (call
, "__cdecl"))
172 sym
->flags
|= SYM_CDECL
;
174 sym
->flags
|= SYM_STDCALL
;
176 iter
= (char *)str_match (iter
, sym
->symbol
, &found
);
182 printf ("Using %s calling convention\n",
183 sym
->flags
& SYM_CDECL
? "cdecl" : "stdcall");
186 sym
->flags
= CALLING_CONVENTION
;
188 sym
->function_name
= strdup (sym
->symbol
);
191 /* Now should be the arguments */
195 for (; *proto
== ' '; proto
++);
197 if (!strncmp (proto
, "void", 4))
202 /* Process next argument */
203 str_match (proto
, "...", &sym
->varargs
);
207 if (!(proto
= get_type (sym
, proto
, sym
->argc
)))
214 else if (*proto
!= ')')
217 } while (*proto
!= ')');
223 /*******************************************************************
226 * Read a type from a prototype
228 static const char *get_type (parsed_symbol
*sym
, const char *proto
, int arg
)
230 int is_const
, is_volatile
, is_struct
, is_signed
, is_unsigned
, ptrs
= 0;
231 char *iter
, *type_str
, *base_type
, *catch_unsigned
, dest_type
;
233 assert (sym
&& sym
->symbol
);
234 assert (proto
&& *proto
);
235 assert (arg
< 0 || (unsigned)arg
== sym
->argc
);
237 type_str
= (char *)proto
;
239 proto
= str_match (proto
, "const", &is_const
);
240 proto
= str_match (proto
, "volatile", &is_volatile
);
241 proto
= str_match (proto
, "struct", &is_struct
);
243 proto
= str_match (proto
, "union", &is_struct
);
245 catch_unsigned
= (char *)proto
;
247 proto
= str_match (proto
, "unsigned", &is_unsigned
);
248 proto
= str_match (proto
, "signed", &is_signed
);
250 /* Can have 'unsigned const' or 'const unsigned' etc */
252 proto
= str_match (proto
, "const", &is_const
);
254 proto
= str_match (proto
, "volatile", &is_volatile
);
256 base_type
= (char *)proto
;
257 iter
= (char *)str_find_set (proto
, " ,*)");
261 if (arg
< 0 && (is_signed
|| is_unsigned
))
263 /* Prevent calling convention from being swallowed by 'un/signed' alone */
264 if (strncmp (base_type
, "int", 3) && strncmp (base_type
, "long", 4) &&
265 strncmp (base_type
, "short", 5) && strncmp (base_type
, "char", 4))
267 iter
= (char *)proto
;
268 base_type
= catch_unsigned
;
272 catch_unsigned
= NULL
;
274 /* FIXME: skip const/volatile here too */
275 for (proto
= iter
; *proto
; proto
++)
278 else if (*proto
!= ' ')
284 type_str
= str_substring (type_str
, proto
);
285 if (iter
== base_type
|| catch_unsigned
)
287 /* 'unsigned' with no type */
288 char *tmp
= str_create (2, type_str
, " int");
292 symbol_clean_string (type_str
);
294 dest_type
= symbol_get_type (type_str
);
298 sym
->return_text
= type_str
;
299 sym
->return_type
= dest_type
;
303 sym
->arg_type
[arg
] = dest_type
;
304 sym
->arg_flag
[arg
] = is_const
? CT_CONST
: is_volatile
? CT_VOLATILE
: 0;
306 if (*proto
== ',' || *proto
== ')')
307 sym
->arg_name
[arg
] = str_create_num (1, arg
, "arg");
310 iter
= (char *)str_find_set (proto
, " ,)");
316 sym
->arg_name
[arg
] = str_substring (proto
, iter
);
319 sym
->arg_text
[arg
] = type_str
;
327 /*******************************************************************
330 * Free memory used while searching (a niceity)
332 void search_cleanup (void) __attribute__ ((destructor
));
333 void search_cleanup (void)