rizin/libr/parse/code.c
pancake 8d8c9f4ee5 Initial import of the new cparse engine using sdb and tcc
* There's still lot of work to do, this is just the base
2013-08-01 00:39:19 +02:00

40 lines
834 B
C

/* radare - LGPL - Copyright 2013 - pancake */
#include "r_types.h"
#include "libr_tcc.h"
/* parse C code and return it in key-value form */
static void appendstring(char *msg, char **s) {
if (!s)
printf ("%s\n", msg);
else
if (*s) {
char *p = malloc (strlen (msg) + strlen (*s)+1);
strcpy (p, *s);
free (*s);
*s = p;
strcpy (p+strlen (p), msg);
} else *s = strdup (msg);
}
R_API char *r_parse_c_file(const char *path) {
char *str = NULL;
TCCState *T = tcc_new ();
tcc_set_callback (T, &appendstring, &str);
if (tcc_add_file (T, path) == -1) {
free (str);
str = NULL;
}
tcc_delete (T);
return str;
}
R_API char *r_parse_c_string(const char *code) {
char *str = NULL;
TCCState *T = tcc_new ();
tcc_set_callback (T, &appendstring, &str);
tcc_compile_string (T, code);
tcc_delete (T);
return str;
}