// p = process.open(command) tb_int_t xm_process_open(lua_State* lua) { // check tb_assert_and_check_return_val(lua, 0); // get the command size_t command_size = 0; tb_char_t const* command = luaL_checklstring(lua, 1, &command_size); tb_char_t const* outfile = lua_tostring(lua, 2); tb_char_t const* errfile = lua_tostring(lua, 3); tb_check_return_val(command, 0); // init attributes tb_process_attr_t attr = {0}; // redirect stdout? if (outfile) { // redirect stdout to file attr.outfile = outfile; attr.outmode = TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_APPEND; // remove the outfile first if (tb_file_info(outfile, tb_null)) tb_file_remove(outfile); } // redirect stderr? if (errfile) { // redirect stderr to file attr.errfile = errfile; attr.errmode = TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_APPEND; // remove the errfile first if (tb_file_info(errfile, tb_null)) tb_file_remove(errfile); } // init process tb_process_ref_t process = tb_process_init_cmd(command, &attr); // save the process reference lua_pushlightuserdata(lua, process); // ok return 1; }
/* ////////////////////////////////////////////////////////////////////////////////////// * callback */ static tb_void_t tb_directory_walk_remove(tb_char_t const* path, tb_file_info_t const* info, tb_cpointer_t priv) { // check tb_assert_and_check_return(path && info); // remove file if (info->type == TB_FILE_TYPE_FILE) tb_file_remove(path); // remvoe directory else if (info->type == TB_FILE_TYPE_DIRECTORY) { tb_wchar_t temp[TB_PATH_MAXN] = {0}; if (tb_atow(temp, path, TB_PATH_MAXN)) RemoveDirectoryW(temp); } }
/* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ static tb_bool_t tb_directory_walk_remove(tb_char_t const* path, tb_file_info_t const* info, tb_cpointer_t priv) { // check tb_assert_and_check_return_val(path && info, tb_false); // remove file if (info->type == TB_FILE_TYPE_FILE) tb_file_remove(path); // remvoe directory else if (info->type == TB_FILE_TYPE_DIRECTORY) { tb_wchar_t temp[TB_PATH_MAXN]; if (tb_atow(temp, path, TB_PATH_MAXN) != -1) RemoveDirectoryW(temp); } // continue return tb_true; }
/* ////////////////////////////////////////////////////////////////////////////////////// * callback */ static tb_void_t tb_directory_walk_remove(tb_char_t const* path, tb_file_info_t const* info, tb_cpointer_t priv) { // check tb_assert_and_check_return(path && info); // remove switch (info->type) { case TB_FILE_TYPE_FILE: tb_file_remove(path); break; case TB_FILE_TYPE_DIRECTORY: remove(path); break; default: break; } }
static tb_bool_t tb_directory_walk_copy(tb_char_t const* path, tb_file_info_t const* info, tb_cpointer_t priv) { // check tb_value_t* tuple = (tb_value_t*)priv; tb_assert_and_check_return_val(path && info && tuple, tb_false); // the dest directory tb_char_t const* dest = tuple[0].cstr; tb_assert_and_check_return_val(dest, tb_false); // the file name tb_size_t size = tuple[1].ul; tb_char_t const* name = path + size; // the dest file path tb_char_t dpath[8192] = {0}; tb_snprintf(dpath, 8192, "%s\\%s", dest, name[0] == '\\'? name + 1 : name); // remove the dest file first tb_file_info_t dinfo = {0}; if (tb_file_info(dpath, &dinfo)) { if (dinfo.type == TB_FILE_TYPE_FILE) tb_file_remove(dpath); if (dinfo.type == TB_FILE_TYPE_DIRECTORY) tb_directory_remove(dpath); } // copy switch (info->type) { case TB_FILE_TYPE_FILE: if (!tb_file_copy(path, dpath)) tuple[2].b = tb_false; break; case TB_FILE_TYPE_DIRECTORY: if (!tb_directory_create(dpath)) tuple[2].b = tb_false; break; default: break; } // continue return tb_true; }
// p = process.openv(shellname, argv, outfile, errfile) tb_int_t xm_process_openv(lua_State* lua) { // check tb_assert_and_check_return_val(lua, 0); // check table if (!lua_istable(lua, 2)) { // error lua_pushfstring(lua, "invalid argv type(%s) for process.openv", luaL_typename(lua, 2)); lua_error(lua); return 0; } // get the output and error file tb_char_t const* shellname = lua_tostring(lua, 1); tb_char_t const* outfile = lua_tostring(lua, 3); tb_char_t const* errfile = lua_tostring(lua, 4); tb_check_return_val(shellname, 0); // get the arguments count tb_long_t argn = lua_objlen(lua, 2); tb_check_return_val(argn >= 0, 0); // get arguments tb_size_t argi = 0; tb_char_t const** argv = tb_nalloc0_type(1 + argn + 1, tb_char_t const*); tb_check_return_val(argv, 0); // fill arguments argv[0] = shellname; for (argi = 0; argi < argn; argi++) { // get argv[i] lua_pushinteger(lua, argi + 1); lua_gettable(lua, 2); // is string? if (lua_isstring(lua, -1)) { // pass this argument argv[1 + argi] = lua_tostring(lua, -1); } else { // error lua_pushfstring(lua, "invalid argv[%ld] type(%s) for process.openv", argi, luaL_typename(lua, -1)); lua_error(lua); } // pop it lua_pop(lua, 1); } // init attributes tb_process_attr_t attr = {0}; // redirect stdout? if (outfile) { // redirect stdout to file attr.outfile = outfile; attr.outmode = TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_APPEND; // remove the outfile first if (tb_file_info(outfile, tb_null)) tb_file_remove(outfile); } // redirect stderr? if (errfile) { // redirect stderr to file attr.errfile = errfile; attr.errmode = TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_APPEND; // remove the errfile first if (tb_file_info(errfile, tb_null)) tb_file_remove(errfile); } // init process tb_process_ref_t process = tb_process_init(shellname, argv, &attr); if (process) lua_pushlightuserdata(lua, process); else lua_pushnil(lua); // exit argv if (argv) tb_free(argv); argv = tb_null; // ok return 1; }