diff options
-rw-r--r-- | src/main.cc | 6 | ||||
-rw-r--r-- | src/pyinterp.cc | 41 | ||||
-rw-r--r-- | src/pyinterp.h | 2 |
3 files changed, 49 insertions, 0 deletions
diff --git a/src/main.cc b/src/main.cc index c8917198..6be50910 100644 --- a/src/main.cc +++ b/src/main.cc @@ -50,10 +50,16 @@ namespace { } } +namespace ledger { + extern char * argv0; +} + int main(int argc, char * argv[], char * envp[]) { int status; + argv0 = argv[0]; + // The very first thing we do is handle some very special command-line // options, since they affect how the environment is setup: // diff --git a/src/pyinterp.cc b/src/pyinterp.cc index 7125a18b..fdc22af9 100644 --- a/src/pyinterp.cc +++ b/src/pyinterp.cc @@ -37,6 +37,8 @@ using namespace python; shared_ptr<python_interpreter_t> python_session; +char * argv0; + void export_amount(); void export_balance(); void export_chain(); @@ -243,6 +245,34 @@ object python_interpreter_t::eval(const string& str, py_eval_mode_t mode) return object(); } +value_t python_interpreter_t::python_command(call_scope_t& args) +{ + if (! is_initialized) + initialize(); + + char ** argv(new char *[args.size() + 1]); + + argv[0] = new char[std::strlen(argv0) + 1]; + std::strcpy(argv[0], argv0); + + for (std::size_t i = 0; i < args.size(); i++) { + string arg = args[i].as_string(); + argv[i + 1] = new char[arg.length() + 1]; + std::strcpy(argv[i + 1], arg.c_str()); + } + + int status = Py_Main(args.size() + 1, argv); + + for (std::size_t i = 0; i < args.size() + 1; i++) + delete[] argv[i]; + delete[] argv; + + if (status != 0) + throw status; + + return NULL_VALUE; +} + option_t<python_interpreter_t> * python_interpreter_t::lookup_option(const char * p) { @@ -268,6 +298,17 @@ expr_t::ptr_op_t python_interpreter_t::lookup(const string& name) return MAKE_OPT_HANDLER(python_interpreter_t, handler); } break; + + case 'p': + if (WANT_PRECMD()) { const char * q = p + PRECMD_PREFIX_LEN; + switch (*q) { + case 'p': + if (is_eq(q, "python")) + return MAKE_FUNCTOR(python_interpreter_t::python_command); + break; + } + } + break; } if (is_initialized && main_nspace.has_key(name.c_str())) { diff --git a/src/pyinterp.h b/src/pyinterp.h index a550e5ba..dd9ca64c 100644 --- a/src/pyinterp.h +++ b/src/pyinterp.h @@ -76,6 +76,8 @@ public: return eval(str, mode); } + value_t python_command(call_scope_t& scope); + class functor_t { functor_t(); |