Esempio n. 1
0
WSLUA_FUNCTION wslua_dofile(lua_State* L) {
    /* Lua's dofile() has been modified so that if a file does not exist
    in the current directory it will look for it in wireshark's user and system directories. */
#define WSLUA_ARG_dofile_FILENAME 1 /* Name of the file to be run. */
    const char *given_fname = luaL_checkstring(L, WSLUA_ARG_dofile_FILENAME);
    char* filename = wslua_get_actual_filename(given_fname);
    int n;

    if (!filename) {
        WSLUA_ARG_ERROR(dofile,FILENAME,"file does not exist");
        return 0;
    }

    n = lua_gettop(L);
    if (luaL_loadfile(L, filename) != 0) lua_error(L);
    g_free(filename);
    lua_call(L, 0, LUA_MULTRET);
    return lua_gettop(L) - n;
}
Esempio n. 2
0
WSLUA_CONSTRUCTOR Dir_open(lua_State* L) {
    /* Opens a directory and returns a `Dir` object representing the files in the directory.

       @code for filename in Dir.open(path) do ... end @endcode
    */
#define WSLUA_ARG_Dir_open_PATHNAME 1 /* The pathname of the directory. */
#define WSLUA_OPTARG_Dir_open_EXTENSION 2 /* If given, only files with this extension will be returned. */

    const char* dirname = luaL_checkstring(L,WSLUA_ARG_Dir_open_PATHNAME);
    const char* extension = luaL_optstring(L,WSLUA_OPTARG_Dir_open_EXTENSION,NULL);
    Dir dir;
    char* dirname_clean;

    dirname_clean = wslua_get_actual_filename(dirname);
    if (!dirname_clean) {
        WSLUA_ARG_ERROR(Dir_open,PATHNAME,"directory does not exist");
        return 0;
    }

    if (!test_for_directory(dirname_clean))  {
        g_free(dirname_clean);
        WSLUA_ARG_ERROR(Dir_open,PATHNAME, "must be a directory");
        return 0;
    }

    dir = (Dir)g_malloc(sizeof(struct _wslua_dir));
    dir->dir = g_dir_open(dirname_clean, 0, dir->dummy);
    g_free(dirname_clean);
    dir->ext = extension ? g_strdup(extension) : NULL;
    dir->dummy = (GError **)g_malloc(sizeof(GError *));
    *(dir->dummy) = NULL;

    if (dir->dir == NULL) {
        g_free(dir->dummy);
        g_free(dir);

        WSLUA_ARG_ERROR(Dir_open,PATHNAME,"could not open directory");
        return 0;
    }

    pushDir(L,dir);
    WSLUA_RETURN(1); /* the `Dir` object. */
}
Esempio n. 3
0
WSLUA_FUNCTION wslua_loadfile(lua_State* L) {
	/* Lua's loadfile() has been modified so that if a file does not exist
	in the current directory it will look for it in wireshark's user and system directories */
#define WSLUA_ARG_loadfile_FILENAME 1 /* Name of the file to be loaded */
	const char *given_fname = luaL_checkstring(L, WSLUA_ARG_loadfile_FILENAME);
	char* filename;

	filename = wslua_get_actual_filename(given_fname);

	if (!filename) WSLUA_ARG_ERROR(loadfile,FILENAME,"file does not exist");

	if (luaL_loadfile(L, filename) == 0) {
		g_free(filename);
		return 1;
	} else {
		g_free(filename);
		lua_pushnil(L);
		lua_insert(L, -2);
		return 2;
	}
}
Esempio n. 4
0
WSLUA_CONSTRUCTOR Dir_open(lua_State* L) {
	/* Usage: for filename in Dir.open(path) do ... end */
#define WSLUA_ARG_Dir_open_PATHNAME 1 /* The pathname of the directory */
#define WSLUA_OPTARG_Dir_open_EXTENSION 2 /* If given, only file with this extension will be returned */

	const char* dirname = luaL_checkstring(L,WSLUA_ARG_Dir_open_PATHNAME);
	const char* extension = luaL_optstring(L,WSLUA_OPTARG_Dir_open_EXTENSION,NULL);
	Dir dir;
	char* dirname_clean;

	if (!dirname) WSLUA_ARG_ERROR(Dir_open,PATHNAME,"must be a string");

	dirname_clean = wslua_get_actual_filename(dirname);
	if (!dirname_clean) WSLUA_ARG_ERROR(Dir_open,PATHNAME,"directory does not exist");

	if (!test_for_directory(dirname_clean))  {
		g_free(dirname_clean);
		WSLUA_ARG_ERROR(Dir_open,PATHNAME, "must be a directory");
	}

	dir = g_malloc(sizeof(struct _wslua_dir));
	dir->dir = OPENDIR_OP(dirname_clean);
	g_free(dirname_clean);
	dir->ext = extension ? g_strdup(extension) : NULL;
	dir->dummy = g_malloc(sizeof(GError *));
	*(dir->dummy) = NULL;

	if (dir->dir == NULL) {
		g_free(dir->dummy);
		g_free(dir);

		WSLUA_ARG_ERROR(Dir_open,PATHNAME,"could not open directory");
	}

	pushDir(L,dir);
	WSLUA_RETURN(1); /* the Dir object */
}