radare2/libr/lang/lang.c

311 lines
6.8 KiB
C
Raw Normal View History

2016-03-28 06:19:21 +08:00
/* radare - LGPL - Copyright 2009-2016 - pancake */
#include <r_lang.h>
#include <r_util.h>
R_LIB_VERSION(r_lang);
#include "p/pipe.c" // hardcoded
#include "p/vala.c" // hardcoded
2016-03-28 06:19:21 +08:00
#include "p/rust.c" // hardcoded
#include "p/c.c" // hardcoded
#if __UNIX__
#include "p/cpipe.c" // hardcoded
#endif
2014-03-26 08:34:32 +08:00
static RLang *__lang = NULL;
R_API void r_lang_plugin_free (RLangPlugin *p) {
2016-05-31 16:39:34 +08:00
if (p && p->fini) {
p->fini (__lang);
2016-05-31 16:39:34 +08:00
}
}
R_API RLang *r_lang_new() {
RLang *lang = R_NEW0 (RLang);
2016-05-31 16:39:34 +08:00
if (!lang) {
return NULL;
}
2016-05-25 04:22:15 +08:00
lang->user = NULL;
lang->langs = r_list_new ();
if (!lang->langs) {
r_lang_free (lang);
return NULL;
}
lang->langs->free = (RListFree)r_lang_plugin_free;
lang->defs = r_list_new ();
if (!lang->defs) {
r_lang_free (lang);
return NULL;
}
lang->defs->free = (RListFree)r_lang_def_free;
lang->cb_printf = (PrintfCallback)printf;
r_lang_add (lang, &r_lang_plugin_c);
#if __UNIX__
2016-05-25 04:22:15 +08:00
r_lang_add (lang, &r_lang_plugin_cpipe);
#endif
2016-05-25 04:22:15 +08:00
r_lang_add (lang, &r_lang_plugin_vala);
r_lang_add (lang, &r_lang_plugin_rust);
r_lang_add (lang, &r_lang_plugin_pipe);
return lang;
}
R_API void *r_lang_free(RLang *lang) {
if (!lang) return NULL;
__lang = NULL;
r_lang_undef (lang, NULL);
r_list_free (lang->langs);
r_list_free (lang->defs);
// TODO: remove langs plugins
2010-04-07 00:21:41 +08:00
free (lang);
return NULL;
}
// XXX: This is only used actually to pass 'core' structure
// TODO: when language bindings are done we will need an api to
// define symbols from C to the language namespace
// XXX: Depcreate!!
R_API void r_lang_set_user_ptr(RLang *lang, void *user) {
lang->user = user;
}
R_API int r_lang_define(RLang *lang, const char *type, const char *name, void *value) {
RLangDef *def;
RListIter *iter;
r_list_foreach (lang->defs, iter, def) {
2013-12-14 09:35:14 +08:00
if (!strcasecmp (name, def->name)) {
def->value = value;
2015-09-14 08:08:31 +08:00
return true;
}
}
2016-05-31 16:39:34 +08:00
def = R_NEW0 (RLangDef);
if (!def) {
return false;
}
2016-05-31 16:39:34 +08:00
def->type = strdup (type);
def->name = strdup (name);
def->value = value;
r_list_append (lang->defs, def);
return true;
}
R_API void r_lang_def_free (RLangDef *def) {
free (def->name);
free (def->type);
free (def);
}
R_API void r_lang_undef(RLang *lang, const char *name) {
if (name && *name) {
RLangDef *def;
RListIter *iter;
/* No _safe loop necessary because we return immediately after the delete. */
r_list_foreach (lang->defs, iter, def) {
if (!name || !strcasecmp (name, def->name)) {
r_list_delete (lang->defs, iter);
break;
}
}
} else {
2015-08-29 15:58:57 +08:00
r_list_free (lang->defs);
lang->defs = NULL;
}
}
R_API int r_lang_setup(RLang *lang) {
2016-05-31 16:39:34 +08:00
if (lang->cur && lang->cur->setup) {
return lang->cur->setup (lang);
2016-05-31 16:39:34 +08:00
}
2015-09-14 08:08:31 +08:00
return false;
}
R_API int r_lang_add(RLang *lang, RLangPlugin *foo) {
if (foo && (!r_lang_get_by_name (lang, foo->name))) {
2016-05-31 16:39:34 +08:00
if (foo->init) {
foo->init (lang);
2016-05-31 16:39:34 +08:00
}
r_list_append (lang->langs, foo);
}
2015-09-14 08:08:31 +08:00
return true;
}
/* TODO: deprecate all list methods */
R_API int r_lang_list(RLang *lang) {
RListIter *iter;
RLangPlugin *h;
2016-05-31 16:39:34 +08:00
if (!lang) {
2015-09-14 08:08:31 +08:00
return false;
2016-05-31 16:39:34 +08:00
}
r_list_foreach (lang->langs, iter, h) {
2016-05-31 16:39:34 +08:00
const char *license = h->license
? h->license : "???";
lang->cb_printf ("%s: (%s) %s\n",
h->name, license, h->desc);
}
2015-09-14 08:08:31 +08:00
return true;
}
R_API RLangPlugin *r_lang_get_by_extension (RLang *lang, const char *ext) {
RListIter *iter;
RLangPlugin *h;
const char *p = r_str_lchr (ext, '.');
2016-05-31 16:39:34 +08:00
if (p) ext = p + 1;
r_list_foreach (lang->langs, iter, h) {
2016-05-31 16:39:34 +08:00
if (!strcasecmp (h->ext, ext)) {
return h;
2016-05-31 16:39:34 +08:00
}
}
return NULL;
}
R_API RLangPlugin *r_lang_get_by_name (RLang *lang, const char *name) {
RListIter *iter;
RLangPlugin *h;
r_list_foreach (lang->langs, iter, h) {
2016-05-31 16:39:34 +08:00
if (!strcasecmp (h->name, name)) {
return h;
2016-05-31 16:39:34 +08:00
}
}
return NULL;
}
R_API int r_lang_use(RLang *lang, const char *name) {
RLangPlugin *h = r_lang_get_by_name (lang, name);
if (h) {
lang->cur = h;
2015-09-14 08:08:31 +08:00
return true;
}
2015-09-14 08:08:31 +08:00
return false;
}
// TODO: store in r_lang and use it from the plugin?
R_API int r_lang_set_argv(RLang *lang, int argc, char **argv) {
2016-05-31 16:39:34 +08:00
if (lang->cur && lang->cur->set_argv) {
2010-04-07 00:21:41 +08:00
return lang->cur->set_argv (lang, argc, argv);
2016-05-31 16:39:34 +08:00
}
2015-09-14 08:08:31 +08:00
return false;
}
R_API int r_lang_run(RLang *lang, const char *code, int len) {
2016-05-31 16:39:34 +08:00
if (lang->cur && lang->cur->run) {
2010-04-07 00:21:41 +08:00
return lang->cur->run (lang, code, len);
2016-05-31 16:39:34 +08:00
}
2015-09-14 08:08:31 +08:00
return false;
}
R_API int r_lang_run_string(RLang *lang, const char *code) {
return r_lang_run (lang, code, strlen (code));
}
R_API int r_lang_run_file(RLang *lang, const char *file) {
2015-09-14 08:08:31 +08:00
int len, ret = false;
if (lang->cur) {
2016-05-31 16:39:34 +08:00
if (!lang->cur->run_file) {
if (lang->cur->run) {
char *code = r_file_slurp (file, &len);
ret = lang->cur->run (lang, code, len);
free (code);
}
} else ret = lang->cur->run_file (lang, file);
}
return ret;
}
/* TODO: deprecate or make it more modular .. reading from stdin in a lib?!? wtf */
R_API int r_lang_prompt(RLang *lang) {
char buf[1024];
const char *p;
2016-05-31 16:39:34 +08:00
if (!lang || !lang->cur) {
2015-09-14 08:08:31 +08:00
return false;
2016-05-31 16:39:34 +08:00
}
2016-05-31 16:39:34 +08:00
if (lang->cur->prompt) {
if (lang->cur->prompt (lang)) {
2015-09-14 08:08:31 +08:00
return true;
2016-05-31 16:39:34 +08:00
}
}
/* init line */
RLine *line = r_line_singleton ();
RLineHistory hist = line->history;
RLineHistory histnull = {0};
RLineCompletion oc = line->completion;
RLineCompletion ocnull = {0};
char *prompt = strdup (line->prompt);
line->completion = ocnull;
line->history = histnull;
2016-05-31 16:39:34 +08:00
/* foo */
2010-04-07 00:21:41 +08:00
for (;;) {
2016-03-30 21:50:16 +08:00
snprintf (buf, sizeof (buf)-1, "%s> ", lang->cur->name);
r_line_set_prompt (buf);
#if 0
2010-04-07 00:21:41 +08:00
printf ("%s> ", lang->cur->name);
fflush (stdout);
fgets (buf, sizeof (buf)-1, stdin);
2010-04-07 00:21:41 +08:00
if (feof (stdin)) break;
if (*buf) buf[strlen (buf)-1]='\0';
#endif
p = r_line_readline ();
2016-05-31 16:39:34 +08:00
if (!p) {
break;
}
r_line_hist_add (p);
strncpy (buf, p, sizeof (buf) - 1);
if (*buf == '!') {
if (buf[1]) {
2016-05-31 16:39:34 +08:00
r_sandbox_system (buf + 1, 1);
} else {
char *foo, *code = NULL;
do {
foo = r_cons_editor (NULL, code);
r_lang_run (lang, foo, 0);
free (code);
code = foo;
} while (r_cons_yesno ('y', "Edit again? (Y/n)"));
free (foo);
}
continue;
}
if (!memcmp (buf, ". ", 2)) {
char *file = r_file_abspath (buf+2);
if (file) {
r_lang_run_file (lang, file);
free (file);
}
continue;
}
2014-07-29 03:58:44 +08:00
if (!strcmp (buf, "q")) {
free (prompt);
2015-09-14 08:08:31 +08:00
return true;
2014-07-29 03:58:44 +08:00
}
2010-04-07 00:21:41 +08:00
if (!strcmp (buf, "?")) {
RLangDef *def;
RListIter *iter;
eprintf(" ? - show this help message\n"
" ! - run $EDITOR\n"
" !command - run system command\n"
" . file - interpret file\n"
" q - quit prompt\n");
2014-07-31 04:13:55 +08:00
eprintf ("%s example:\n", lang->cur->name);
if (lang->cur->help)
eprintf ("%s", *lang->cur->help);
if (!r_list_empty (lang->defs))
eprintf ("variables:\n");
r_list_foreach (lang->defs, iter, def) {
eprintf (" %s %s\n", def->type, def->name);
}
} else r_lang_run (lang, buf, strlen (buf));
}
// XXX: leaking history
r_line_set_prompt (prompt);
line->completion = oc;
line->history = hist;
clearerr (stdin);
printf ("\n");
2015-09-14 08:08:31 +08:00
free (prompt);
return true;
}