elua: add build_args function to core cutil library, for building normalized args string in a platform independent way

This commit is contained in:
Daniel Kolesa 2014-05-15 15:43:56 +01:00 committed by Daniel Kolesa
parent 9bb3667e2b
commit 5b7f44ea5b
2 changed files with 23 additions and 11 deletions

View File

@ -190,21 +190,17 @@ end
local build_opt = function(opt)
local buf = {}
if opt.short then
buf[1] = "'-"
buf[1] = "-"
buf[2] = opt.short
if opt.val then
buf[3] = opt.val
end
buf[4] = "'"
else
buf[1] = "'--"
buf[1] = "--"
buf[2] = opt.long
if opt.val then
buf[3] = "="
buf[4] = opt.val
buf[5] = "'"
else
buf[3] = "'"
end
end
return table.concat(buf)
@ -250,7 +246,7 @@ end
args_nolua[#args_nolua + 1] = "--omit-header"
args_nolua[#args_nolua + 1] = "--output=-"
args_nolua = table.concat(args_nolua, " ")
args_nolua = cutil.build_args(unpack(args_nolua))
local parsed_files = {}
for i, fname in ipairs(input_files) do
@ -269,7 +265,7 @@ for i, opt in ipairs(opts_final) do
end
args_final[#args_final + 1] = "--language=PO"
args_final[#args_final + 1] = "-"
local f = assert(io.popen(table.concat(args_final, " "), "w"))
local f = assert(io.popen(cutil.build_args(unpack(args_final)), "w"))
f:write(table.concat(parsed_files, "\n\n"))
f:close()

View File

@ -311,6 +311,21 @@ static int elua_exec(lua_State *L) {
#endif
}
static int elua_build_args(lua_State *L) {
/* todo: will probably need adjustments for Windows */
int nargs = lua_gettop(L), n = 0, i;
for (i = 1; i <= nargs; ++i) {
lua_pushliteral(L, "'"); ++n;
lua_pushvalue(L, i); ++n;
lua_pushliteral(L, "'"); ++n;
if (i != nargs) {
lua_pushliteral(L, " "); ++n;
}
}
lua_concat(L, n);
return 1;
}
struct Main_Data {
int argc;
char **argv;
@ -318,10 +333,11 @@ struct Main_Data {
};
const luaL_reg cutillib[] = {
{ "init_module", init_module },
{ "init_module" , init_module },
{ "register_callbacks", register_callbacks },
{ "exec", elua_exec },
{ NULL, NULL }
{ "exec" , elua_exec },
{ "build_args" , elua_build_args },
{ NULL , NULL }
};
static int gettext_bind_textdomain(lua_State *L) {