diff options
Diffstat (limited to 'cd-tool.cpp')
-rw-r--r-- | cd-tool.cpp | 112 |
1 files changed, 98 insertions, 14 deletions
diff --git a/cd-tool.cpp b/cd-tool.cpp index e5b2ce5..efb7bfd 100644 --- a/cd-tool.cpp +++ b/cd-tool.cpp @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: cd-tool.cpp,v 1.25 2004-04-28 14:24:17 pixel Exp $ */ +/* $Id: cd-tool.cpp,v 1.26 2004-05-01 11:48:57 pixel Exp $ */ #include <getopt.h> #include "Input.h" @@ -41,13 +41,65 @@ isobuilder * build = 0; static int myprint(lua_State * _L) { Lua * L = Lua::find(_L); - String t = L->tostring(); + String t = "From LUA: " + L->tostring() + "\n"; + char * tc = t.strdup(); - Base::printm(M_STATUS, "From LUA: " + t + "\n"); + Base::printm(M_STATUS, "%s", tc); + + free(tc); return 0; } +class Luabasecdtool : public LuaObject { + public: + static void pushstatics(Lua *) throw (GeneralException); +}; + +typedef void basecdtool; + +enum basecdtool_t { + BASECDTOOL_LOAD = 0, +}; + +struct lua_functypes_t basecdtool_functions[] = { + { BASECDTOOL_LOAD, "load", 0, 1, { LUA_ANY } }, + { -1, 0, 0, 0, 0 } +}; + +class sLua_basecdtool : public Base { + public: + DECLARE_FUNCTION(basecdtool, BASECDTOOL_LOAD); + private: + static int basecdtool_proceed_statics(Lua * L, int n, int caller); +}; + +void Luabasecdtool::pushstatics(Lua * L) throw (GeneralException ) { + CHECK_FUNCTIONS(basecdtool); + + PUSH_FUNCTION(basecdtool, BASECDTOOL_LOAD); +} + +int sLua_basecdtool::basecdtool_proceed_statics(Lua * L, int n, int caller) { + int r = 0; + + switch (caller) { + case BASECDTOOL_LOAD: + if (!n) { + L->load(&Input("cd-tool.lua")); + } else { + if (L->isstring(1)) { + L->load(&Input(L->tostring(1))); + } else { + Handle * t = (Handle *) LuaObject::getme(L, 1); + L->load(t); + } + } + } + + return r; +} + class Luacdtool : public LuaObject { public: static void pushstatics(Lua *) throw (GeneralException); @@ -57,6 +109,7 @@ typedef void cdtool; enum cdtool_functions_t { CDTOOL_PRINT = 0, + CDTOOL_PRINTN, CDTOOL_QUIT, CDTOOL_EXIT, CDTOOL_INFOS, @@ -65,7 +118,8 @@ enum cdtool_functions_t { }; struct lua_functypes_t cdtool_functions[] = { - { CDTOOL_PRINT, "print", 1, 1, { LUA_STRING } }, + { CDTOOL_PRINT, "print", 0, 1, { LUA_STRING } }, + { CDTOOL_PRINTN, "printn", 1, 1, { LUA_STRING } }, { CDTOOL_QUIT, "quit", 0, 0, 0 }, { CDTOOL_EXIT, "exit", 0, 0, 0 }, { CDTOOL_INFOS, "infos", 0, 1, { LUA_OBJECT } }, @@ -77,6 +131,7 @@ struct lua_functypes_t cdtool_functions[] = { class sLua_cdtool : public Base { public: DECLARE_FUNCTION(cdtool, CDTOOL_PRINT); + DECLARE_FUNCTION(cdtool, CDTOOL_PRINTN); DECLARE_FUNCTION(cdtool, CDTOOL_QUIT); DECLARE_FUNCTION(cdtool, CDTOOL_EXIT); DECLARE_FUNCTION(cdtool, CDTOOL_INFOS); @@ -90,6 +145,7 @@ void Luacdtool::pushstatics(Lua * L) throw (GeneralException ) { CHECK_FUNCTIONS(cdtool); PUSH_FUNCTION(cdtool, CDTOOL_PRINT); + PUSH_FUNCTION(cdtool, CDTOOL_PRINTN); PUSH_FUNCTION(cdtool, CDTOOL_QUIT); PUSH_FUNCTION(cdtool, CDTOOL_EXIT); PUSH_FUNCTION(cdtool, CDTOOL_INFOS); @@ -101,11 +157,20 @@ int sLua_cdtool::cdtool_proceed_statics(Lua * L, int n, int caller) { int r = 0; String p; cdutils * cd = cdutil; + char * tc; + String eol = ""; switch (caller) { case CDTOOL_PRINT: - p = L->tostring(1); - printm(M_BARE, p + "\n"); + eol = "\n"; + case CDTOOL_PRINTN: + if (n) + p = L->tostring(1) + eol; + else + p = eol; + tc = p.strdup(); + printm(M_BARE, "%s", tc); + free(tc); break; case CDTOOL_QUIT: case CDTOOL_EXIT: @@ -154,6 +219,7 @@ struct option long_options[] = { {"help", 0, NULL, 'h'}, {"verbose", 0, NULL, 'v'}, {"file", 1, NULL, 'f'}, + {"write", 0, NULL, 'w'}, {"output", 1, NULL, 'o'}, {"archive", 1, NULL, 'a'}, {"compile", 1, NULL, 'c'}, @@ -188,6 +254,8 @@ Lua * start_basic_lua(void) { L->push(myprint); L->setvar(); + Luabasecdtool::pushstatics(L); + return L; } @@ -208,12 +276,13 @@ void showhelp(bool longhelp = false) { "Options:\n" " -v for verbose mode.\n" " -f <iso> to load an initial iso file (object cdutil).\n" +" -w to open the previous iso file in write mode.\n" " -o <iso> to start creating an output iso (object iso).\n" " -a <paq> to load an additionnal archive file.\n" " -c <out> to dump the compiled byte code to file.\n" " -d to enable debug mode (ie, do not strip)\n" " -i to start interactive mode.\n" -" -l to turn off the no exec on end line.\n" +" -l to turn off the exec on end line.\n" " -e <cmd> to execute this single command in LUA.\n" " -h for this help page.\n" , argv[0]); @@ -253,11 +322,12 @@ void probe(void) { virtual int startup() throw (GeneralException) { char c; - bool auto_exec = false, strip = true, todo = false, runit; + bool auto_exec = true, strip = true, todo = false, runit, write = false; char * file = 0, * output = 0, * compile = 0, * exec = 0, * line_read = 0; char prompt[10]; Lua * L = 0; - Output * build_iso = 0; + Handle * read_iso = 0; + Output * build_iso = 0, * write_iso = 0; Buffer command; String line, endline; int pos; @@ -266,7 +336,7 @@ virtual int startup() throw (GeneralException) { /* Let's start parsing options */ - while ((c = getopt_long(argc, argv, "Hhvf:o:a:c:dile:pm:", long_options, NULL)) != EOF) { + while ((c = getopt_long(argc, argv, "Hhvf:wo:a:c:dile:pm:", long_options, NULL)) != EOF) { switch (c) { case 'h': case 'H': @@ -279,6 +349,9 @@ virtual int startup() throw (GeneralException) { case 'f': file = strdup(optarg); break; + case 'w': + write = true; + break; case 'o': output = strdup(optarg); break; @@ -296,7 +369,7 @@ virtual int startup() throw (GeneralException) { todo = true; break; case 'l': - auto_exec = true; + auto_exec = false; break; case 'e': exec = strdup(optarg); @@ -316,10 +389,14 @@ virtual int startup() throw (GeneralException) { else L = start_basic_lua(); + /* Loading cd-tool.lua (only when not compiling) */ + if (!compile) + L->load(&Input("cd-tool.lua")); + /* Loading all the scripts */ while (optind < argc) { todo = true; - L->load(&Input(argv[optind++]), compile); + L->load(&Input(argv[optind++]), !compile); } /* Doh... */ @@ -336,7 +413,14 @@ virtual int startup() throw (GeneralException) { /* The basic input (and eventually output) iso file */ if (file) { - cdutil = new cdutils(cdabstract::open_cd(file)); + /* The write mode can't apply on a CD of course... */ + if (write) { + read_iso = new Input(file); + write_iso = new Output(file, 0, 0); + } else { + read_iso = cdabstract::open_cd(file); + } + cdutil = new cdutils(read_iso, write_iso); if (!cdutil->get_iso_infos()) throw Exit(-1); Luacdutils lcdutil(cdutil); @@ -402,7 +486,7 @@ virtual int startup() throw (GeneralException) { try { L->load(&command); } - catch (GeneralException e) { + catch (LuaException e) { /* If there was an error, ignore it, and free the stack */ while(L->gettop()) L->pop(); |