2001-01-04 20:45:49 +01:00
|
|
|
/*
|
|
|
|
* Prototype search and parsing functions
|
|
|
|
*
|
|
|
|
* Copyright 2000 Jon Griffiths
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2001-01-04 20:45:49 +01:00
|
|
|
*/
|
2002-04-26 21:05:15 +02:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
2001-09-07 18:04:38 +02:00
|
|
|
#include "winedump.h"
|
2001-01-04 20:45:49 +01:00
|
|
|
|
|
|
|
static char *grep_buff = NULL;
|
|
|
|
static char *fgrep_buff = NULL;
|
|
|
|
|
2014-01-02 02:16:51 +01:00
|
|
|
static BOOL symbol_from_prototype (parsed_symbol *sym, const char *prototype);
|
2001-01-04 20:45:49 +01:00
|
|
|
static const char *get_type (parsed_symbol *sym, const char *proto, int arg);
|
|
|
|
|
|
|
|
|
|
|
|
/*******************************************************************
|
|
|
|
* symbol_search
|
|
|
|
*
|
|
|
|
* Call Patrik Stridvall's 'function_grep.pl' script to retrieve a
|
|
|
|
* function prototype from include file(s)
|
|
|
|
*/
|
2014-01-03 01:57:01 +01:00
|
|
|
BOOL symbol_search (parsed_symbol *sym)
|
2001-01-04 20:45:49 +01:00
|
|
|
{
|
|
|
|
static const size_t MAX_RESULT_LEN = 1024;
|
|
|
|
FILE *grep;
|
|
|
|
int attempt = 0;
|
|
|
|
|
|
|
|
assert (globals.do_code);
|
|
|
|
assert (globals.directory);
|
|
|
|
assert (sym && sym->symbol);
|
|
|
|
|
|
|
|
if (!symbol_is_valid_c (sym))
|
2014-01-03 01:57:01 +01:00
|
|
|
return FALSE;
|
2001-01-04 20:45:49 +01:00
|
|
|
|
|
|
|
if (!grep_buff)
|
2008-11-02 00:20:49 +01:00
|
|
|
grep_buff = malloc (MAX_RESULT_LEN);
|
2001-01-04 20:45:49 +01:00
|
|
|
|
|
|
|
if (!fgrep_buff)
|
2008-11-02 00:20:49 +01:00
|
|
|
fgrep_buff = malloc (MAX_RESULT_LEN);
|
2001-01-04 20:45:49 +01:00
|
|
|
|
|
|
|
if (!grep_buff || !fgrep_buff)
|
|
|
|
fatal ("Out of Memory");
|
|
|
|
|
|
|
|
/* Use 'grep' to tell us which possible files the function is in,
|
|
|
|
* then use 'function_grep.pl' to get the prototype. If this fails the
|
|
|
|
* first time then give grep a more general query (that doesn't
|
|
|
|
* require an opening argument brace on the line with the function name).
|
|
|
|
*/
|
|
|
|
while (attempt < 2)
|
|
|
|
{
|
|
|
|
FILE *f_grep;
|
|
|
|
char *cmd = str_create (4, "grep -d recurse -l \"", sym->symbol,
|
|
|
|
!attempt ? "[:blank:]*(\" " : "\" ", globals.directory);
|
|
|
|
|
|
|
|
if (VERBOSE)
|
|
|
|
puts (cmd);
|
|
|
|
|
|
|
|
fflush (NULL); /* See 'man popen' */
|
|
|
|
|
|
|
|
if (!(grep = popen (cmd, "r")))
|
|
|
|
fatal ("Cannot execute grep -l");
|
|
|
|
free (cmd);
|
|
|
|
|
|
|
|
while (fgets (grep_buff, MAX_RESULT_LEN, grep))
|
|
|
|
{
|
|
|
|
int i;
|
2004-12-20 17:52:26 +01:00
|
|
|
const char *extension = grep_buff;
|
|
|
|
for (i = 0; grep_buff[i] && grep_buff[i] != '\n' ; i++) {
|
|
|
|
if (grep_buff[i] == '.')
|
|
|
|
extension = &grep_buff[i];
|
|
|
|
}
|
2001-01-04 20:45:49 +01:00
|
|
|
grep_buff[i] = '\0';
|
|
|
|
|
2004-12-22 16:02:14 +01:00
|
|
|
/* Definitely not in these: */
|
2004-12-20 17:52:26 +01:00
|
|
|
if (strcmp(extension,".dll") == 0 ||
|
|
|
|
strcmp(extension,".lib") == 0 ||
|
|
|
|
strcmp(extension,".so") == 0 ||
|
|
|
|
strcmp(extension,".o") == 0)
|
|
|
|
continue;
|
|
|
|
|
2001-01-04 20:45:49 +01:00
|
|
|
if (VERBOSE)
|
|
|
|
puts (grep_buff);
|
|
|
|
|
|
|
|
cmd = str_create (5, "function_grep.pl ", sym->symbol,
|
|
|
|
" \"", grep_buff, "\"");
|
|
|
|
|
|
|
|
if (VERBOSE)
|
|
|
|
puts (cmd);
|
|
|
|
|
|
|
|
fflush (NULL); /* See 'man popen' */
|
|
|
|
|
|
|
|
if (!(f_grep = popen (cmd, "r")))
|
|
|
|
fatal ("Cannot execute function_grep.pl");
|
|
|
|
free (cmd);
|
|
|
|
|
|
|
|
while (fgets (grep_buff, MAX_RESULT_LEN, f_grep))
|
|
|
|
{
|
|
|
|
char *iter = grep_buff;
|
|
|
|
|
|
|
|
/* Keep only the first line */
|
|
|
|
symbol_clean_string(grep_buff);
|
|
|
|
|
|
|
|
for (i = 0; grep_buff[i] && grep_buff[i] != '\n' ; i++)
|
|
|
|
;
|
|
|
|
grep_buff[i] = '\0';
|
|
|
|
|
|
|
|
if (VERBOSE)
|
|
|
|
puts (grep_buff);
|
|
|
|
|
|
|
|
while ((iter = strstr (iter, sym->symbol)))
|
|
|
|
{
|
2005-01-03 21:18:23 +01:00
|
|
|
if (iter > grep_buff && (iter[-1] == ' ' || iter[-1] == '*') &&
|
2001-01-04 20:45:49 +01:00
|
|
|
(iter[strlen (sym->symbol)] == ' ' ||
|
2005-01-03 21:18:23 +01:00
|
|
|
iter[strlen (sym->symbol)] == '('))
|
2001-01-04 20:45:49 +01:00
|
|
|
{
|
|
|
|
if (VERBOSE)
|
2001-02-16 20:06:05 +01:00
|
|
|
printf ("Prototype '%s' looks OK, processing\n", grep_buff);
|
2001-01-04 20:45:49 +01:00
|
|
|
|
2014-01-02 02:16:51 +01:00
|
|
|
if (symbol_from_prototype (sym, grep_buff))
|
2001-01-04 20:45:49 +01:00
|
|
|
{
|
|
|
|
pclose (f_grep);
|
|
|
|
pclose (grep);
|
2014-01-03 01:57:01 +01:00
|
|
|
return TRUE; /* OK */
|
2001-01-04 20:45:49 +01:00
|
|
|
}
|
|
|
|
if (VERBOSE)
|
|
|
|
puts ("Failed, trying next");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
iter += strlen (sym->symbol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pclose (f_grep);
|
|
|
|
}
|
|
|
|
pclose (grep);
|
|
|
|
attempt++;
|
|
|
|
}
|
|
|
|
|
2014-01-03 01:57:01 +01:00
|
|
|
return FALSE; /* Not found */
|
2001-01-04 20:45:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*******************************************************************
|
|
|
|
* symbol_from_prototype
|
|
|
|
*
|
|
|
|
* Convert a C prototype into a symbol
|
|
|
|
*/
|
2014-01-02 02:16:51 +01:00
|
|
|
static BOOL symbol_from_prototype (parsed_symbol *sym, const char *proto)
|
2001-01-04 20:45:49 +01:00
|
|
|
{
|
2004-12-06 21:43:55 +01:00
|
|
|
const char *iter;
|
2013-12-05 00:58:10 +01:00
|
|
|
BOOL found;
|
2001-01-04 20:45:49 +01:00
|
|
|
|
|
|
|
proto = get_type (sym, proto, -1); /* Get return type */
|
|
|
|
if (!proto)
|
2014-01-02 02:16:51 +01:00
|
|
|
return FALSE;
|
2001-01-04 20:45:49 +01:00
|
|
|
|
2004-12-06 21:43:55 +01:00
|
|
|
iter = str_match (proto, sym->symbol, &found);
|
2001-01-04 20:45:49 +01:00
|
|
|
|
|
|
|
if (!found)
|
|
|
|
{
|
2001-02-16 20:06:05 +01:00
|
|
|
char *call;
|
2001-01-04 20:45:49 +01:00
|
|
|
/* Calling Convention */
|
|
|
|
iter = strchr (iter, ' ');
|
|
|
|
if (!iter)
|
2014-01-02 02:16:51 +01:00
|
|
|
return FALSE;
|
2001-01-04 20:45:49 +01:00
|
|
|
|
2001-02-16 20:06:05 +01:00
|
|
|
call = str_substring (proto, iter);
|
2001-01-04 20:45:49 +01:00
|
|
|
|
2001-02-16 20:06:05 +01:00
|
|
|
if (!strcasecmp (call, "cdecl") || !strcasecmp (call, "__cdecl"))
|
|
|
|
sym->flags |= SYM_CDECL;
|
|
|
|
else
|
|
|
|
sym->flags |= SYM_STDCALL;
|
|
|
|
free (call);
|
2004-12-06 21:43:55 +01:00
|
|
|
iter = str_match (iter, sym->symbol, &found);
|
2001-01-04 20:45:49 +01:00
|
|
|
|
|
|
|
if (!found)
|
2014-01-02 02:16:51 +01:00
|
|
|
return FALSE;
|
2001-02-16 20:06:05 +01:00
|
|
|
|
|
|
|
if (VERBOSE)
|
|
|
|
printf ("Using %s calling convention\n",
|
|
|
|
sym->flags & SYM_CDECL ? "cdecl" : "stdcall");
|
2001-01-04 20:45:49 +01:00
|
|
|
}
|
|
|
|
else
|
2001-02-16 20:06:05 +01:00
|
|
|
sym->flags = CALLING_CONVENTION;
|
2001-01-04 20:45:49 +01:00
|
|
|
|
|
|
|
sym->function_name = strdup (sym->symbol);
|
|
|
|
proto = iter;
|
|
|
|
|
|
|
|
/* Now should be the arguments */
|
|
|
|
if (*proto++ != '(')
|
2014-01-02 02:16:51 +01:00
|
|
|
return FALSE;
|
2001-01-04 20:45:49 +01:00
|
|
|
|
|
|
|
for (; *proto == ' '; proto++);
|
|
|
|
|
|
|
|
if (!strncmp (proto, "void", 4))
|
2014-01-02 02:16:51 +01:00
|
|
|
return TRUE;
|
2001-01-04 20:45:49 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Process next argument */
|
|
|
|
str_match (proto, "...", &sym->varargs);
|
|
|
|
if (sym->varargs)
|
2014-01-02 02:16:51 +01:00
|
|
|
return TRUE;
|
2001-01-04 20:45:49 +01:00
|
|
|
|
|
|
|
if (!(proto = get_type (sym, proto, sym->argc)))
|
2014-01-02 02:16:51 +01:00
|
|
|
return FALSE;
|
2001-01-04 20:45:49 +01:00
|
|
|
|
|
|
|
sym->argc++;
|
|
|
|
|
|
|
|
if (*proto == ',')
|
|
|
|
proto++;
|
|
|
|
else if (*proto != ')')
|
2014-01-02 02:16:51 +01:00
|
|
|
return FALSE;
|
2001-01-04 20:45:49 +01:00
|
|
|
|
|
|
|
} while (*proto != ')');
|
|
|
|
|
2014-01-02 02:16:51 +01:00
|
|
|
return TRUE;
|
2001-01-04 20:45:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*******************************************************************
|
|
|
|
* get_type
|
|
|
|
*
|
|
|
|
* Read a type from a prototype
|
|
|
|
*/
|
|
|
|
static const char *get_type (parsed_symbol *sym, const char *proto, int arg)
|
|
|
|
{
|
2013-12-05 00:58:10 +01:00
|
|
|
BOOL is_const, is_volatile, is_struct, is_signed, is_unsigned;
|
|
|
|
int ptrs = 0;
|
2010-04-02 18:44:37 +02:00
|
|
|
const char *iter, *base_type, *catch_unsigned, *proto_str;
|
|
|
|
char dest_type, *type_str;
|
2001-01-04 20:45:49 +01:00
|
|
|
|
|
|
|
assert (sym && sym->symbol);
|
|
|
|
assert (proto && *proto);
|
|
|
|
assert (arg < 0 || (unsigned)arg == sym->argc);
|
|
|
|
|
|
|
|
|
2010-04-02 18:44:37 +02:00
|
|
|
proto_str = str_match (proto, "const", &is_const);
|
|
|
|
proto_str = str_match (proto_str, "volatile", &is_volatile);
|
|
|
|
proto_str = str_match (proto_str, "struct", &is_struct);
|
2001-01-04 20:45:49 +01:00
|
|
|
if (!is_struct)
|
2010-04-02 18:44:37 +02:00
|
|
|
proto_str = str_match (proto_str, "union", &is_struct);
|
2001-01-04 20:45:49 +01:00
|
|
|
|
2010-04-02 18:44:37 +02:00
|
|
|
catch_unsigned = proto_str;
|
2001-01-04 20:45:49 +01:00
|
|
|
|
2010-04-02 18:44:37 +02:00
|
|
|
proto_str = str_match (proto_str, "unsigned", &is_unsigned);
|
|
|
|
proto_str = str_match (proto_str, "signed", &is_signed);
|
2001-01-04 20:45:49 +01:00
|
|
|
|
|
|
|
/* Can have 'unsigned const' or 'const unsigned' etc */
|
|
|
|
if (!is_const)
|
2010-04-02 18:44:37 +02:00
|
|
|
proto_str = str_match (proto_str, "const", &is_const);
|
2001-01-04 20:45:49 +01:00
|
|
|
if (!is_volatile)
|
2010-04-02 18:44:37 +02:00
|
|
|
proto_str = str_match (proto_str, "volatile", &is_volatile);
|
2001-01-04 20:45:49 +01:00
|
|
|
|
2010-04-02 18:44:37 +02:00
|
|
|
base_type = proto_str;
|
|
|
|
iter = str_find_set (proto_str, " ,*)");
|
2001-01-04 20:45:49 +01:00
|
|
|
if (!iter)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (arg < 0 && (is_signed || is_unsigned))
|
|
|
|
{
|
|
|
|
/* Prevent calling convention from being swallowed by 'un/signed' alone */
|
|
|
|
if (strncmp (base_type, "int", 3) && strncmp (base_type, "long", 4) &&
|
|
|
|
strncmp (base_type, "short", 5) && strncmp (base_type, "char", 4))
|
|
|
|
{
|
2010-04-02 18:44:37 +02:00
|
|
|
iter = proto_str;
|
2001-01-04 20:45:49 +01:00
|
|
|
base_type = catch_unsigned;
|
2005-01-05 14:23:17 +01:00
|
|
|
} else
|
|
|
|
catch_unsigned = NULL;
|
2001-01-04 20:45:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
catch_unsigned = NULL;
|
|
|
|
|
|
|
|
/* FIXME: skip const/volatile here too */
|
2010-04-02 18:44:37 +02:00
|
|
|
for (proto_str = iter; *proto_str; proto_str++)
|
|
|
|
if (*proto_str == '*')
|
2001-01-04 20:45:49 +01:00
|
|
|
ptrs++;
|
2010-04-02 18:44:37 +02:00
|
|
|
else if (*proto_str != ' ')
|
2001-01-04 20:45:49 +01:00
|
|
|
break;
|
|
|
|
|
2010-04-02 18:44:37 +02:00
|
|
|
if (!*proto_str)
|
2001-01-04 20:45:49 +01:00
|
|
|
return NULL;
|
|
|
|
|
2010-04-02 18:44:37 +02:00
|
|
|
type_str = str_substring (proto, proto_str);
|
2001-01-04 20:45:49 +01:00
|
|
|
if (iter == base_type || catch_unsigned)
|
|
|
|
{
|
|
|
|
/* 'unsigned' with no type */
|
|
|
|
char *tmp = str_create (2, type_str, " int");
|
2010-04-02 18:44:37 +02:00
|
|
|
free (type_str);
|
|
|
|
type_str = tmp;
|
2001-01-04 20:45:49 +01:00
|
|
|
}
|
|
|
|
symbol_clean_string (type_str);
|
|
|
|
|
|
|
|
dest_type = symbol_get_type (type_str);
|
|
|
|
|
|
|
|
if (arg < 0)
|
|
|
|
{
|
2019-03-01 21:08:20 +01:00
|
|
|
sym->return_text = type_str;
|
2001-01-04 20:45:49 +01:00
|
|
|
sym->return_type = dest_type;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sym->arg_type [arg] = dest_type;
|
|
|
|
sym->arg_flag [arg] = is_const ? CT_CONST : is_volatile ? CT_VOLATILE : 0;
|
|
|
|
|
2010-04-02 18:44:37 +02:00
|
|
|
if (*proto_str == ',' || *proto_str == ')')
|
2001-01-04 20:45:49 +01:00
|
|
|
sym->arg_name [arg] = str_create_num (1, arg, "arg");
|
|
|
|
else
|
|
|
|
{
|
2010-04-02 18:44:37 +02:00
|
|
|
iter = str_find_set (proto_str, " ,)");
|
2001-01-04 20:45:49 +01:00
|
|
|
if (!iter)
|
|
|
|
{
|
2010-04-02 18:44:37 +02:00
|
|
|
free (type_str);
|
2001-01-04 20:45:49 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-04-02 18:44:37 +02:00
|
|
|
sym->arg_name [arg] = str_substring (proto_str, iter);
|
|
|
|
proto_str = iter;
|
2001-01-04 20:45:49 +01:00
|
|
|
}
|
2019-03-01 21:08:20 +01:00
|
|
|
sym->arg_text [arg] = type_str;
|
2001-01-04 20:45:49 +01:00
|
|
|
|
|
|
|
}
|
2010-04-02 18:44:37 +02:00
|
|
|
return proto_str;
|
2001-01-04 20:45:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
/*******************************************************************
|
|
|
|
* search_cleanup
|
|
|
|
*
|
|
|
|
* Free memory used while searching (a niceity)
|
|
|
|
*/
|
|
|
|
void search_cleanup (void) __attribute__ ((destructor));
|
|
|
|
void search_cleanup (void)
|
|
|
|
{
|
2006-10-09 23:34:53 +02:00
|
|
|
free (grep_buff);
|
|
|
|
free (fgrep_buff);
|
2001-01-04 20:45:49 +01:00
|
|
|
}
|
|
|
|
#endif
|