+
+static int vlc_sd_probe_Open( vlc_object_t *obj )
+{
+ vlc_probe_t *probe = (vlc_probe_t *)obj;
+ char **ppsz_filelist = NULL;
+ char **ppsz_fileend = NULL;
+ char **ppsz_file;
+ char *psz_name;
+ char **ppsz_dir_list = NULL;
+ char **ppsz_dir;
+ lua_State *L = NULL;
+ vlclua_dir_list( obj, "sd", &ppsz_dir_list );
+ for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
+ {
+ int i_files;
+ if( ppsz_filelist )
+ {
+ for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
+ ppsz_file++ )
+ free( *ppsz_file );
+ free( ppsz_filelist );
+ ppsz_filelist = NULL;
+ }
+ i_files = vlc_scandir( *ppsz_dir, &ppsz_filelist, file_select,
+ file_compare );
+ if( i_files < 1 ) continue;
+ ppsz_fileend = ppsz_filelist + i_files;
+ for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
+ {
+ char *psz_filename;
+ if( asprintf( &psz_filename,
+ "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0 )
+ {
+ goto error;
+ }
+ L = luaL_newstate();
+ if( !L )
+ {
+ msg_Err( probe, "Could not create new Lua State" );
+ return VLC_EGENERIC;
+ }
+ luaL_openlibs( L );
+ if( vlclua_add_modules_path( probe, L, psz_filename ) )
+ {
+ msg_Err( probe, "Error while setting the module search path for %s",
+ psz_filename );
+ goto error;
+ }
+ if( luaL_dofile( L, psz_filename ) )
+ {
+
+ msg_Err( probe, "Error loading script %s: %s", psz_filename,
+ lua_tostring( L, lua_gettop( L ) ) );
+ lua_pop( L, 1 );
+ goto error;
+ }
+ char *psz_longname;
+ char *temp = strchr( *ppsz_file, '.' );
+ if( temp )
+ *temp = '\0';
+ lua_getglobal( L, "descriptor" );
+ if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) )
+ {
+ lua_pop( L, 1 );
+ if( !( psz_longname = strdup( *ppsz_file ) ) )
+ {
+ free( psz_filename );
+ goto error;
+ }
+ }
+ else
+ {
+ lua_getfield( L, -1, "title" );
+ if( !lua_isstring( L, -1 ) ||
+ !( psz_longname = strdup( lua_tostring( L, -1 ) ) ) )
+ {
+ free( psz_filename );
+ goto error;
+ }
+ }
+ if( asprintf( &psz_name, "lua{sd=%s,longname=%s}",
+ *ppsz_file, psz_longname ) < 0 )
+ {
+ free( psz_filename );
+ free( psz_longname );
+ goto error;
+ }
+ vlc_sd_probe_Add( probe, psz_name, psz_longname, SD_CAT_INTERNET );
+ free( psz_name );
+ free( psz_longname );
+ free( psz_filename );
+ lua_close( L );
+ }
+ }
+ if( ppsz_filelist )
+ {
+ for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
+ ppsz_file++ )
+ free( *ppsz_file );
+ free( ppsz_filelist );
+ }
+ vlclua_dir_list_free( ppsz_dir_list );
+ return VLC_PROBE_CONTINUE;
+error:
+ if( ppsz_filelist )
+ {
+ for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
+ ppsz_file++ )
+ free( *ppsz_file );
+ free( ppsz_filelist );
+ }
+ if( L )
+ lua_close( L );
+ vlclua_dir_list_free( ppsz_dir_list );
+ return VLC_ENOMEM;
+}
+
+static int vlclua_add_modules_path_inner( lua_State *L, const char *psz_path )
+{
+ /* FIXME: don't use luaL_dostring */
+ char *psz_command = NULL;
+ if( asprintf( &psz_command,
+ "package.path =[[%s"DIR_SEP"modules"DIR_SEP"?.lua;]]..package.path",
+ psz_path ) < 0 )
+ {
+ return 1;
+ }
+
+ if( luaL_dostring( L, psz_command ) )
+ {
+ free( psz_command );
+ return 1;
+ }
+ free( psz_command );
+
+ return 0;
+}
+
+int __vlclua_add_modules_path( vlc_object_t *obj, lua_State *L, const char *psz_filename )
+{
+ /* Setup the module search path:
+ * * "The script's directory"/modules
+ * * "The script's parent directory"/modules
+ * * and so on for all the next directories in the directory list
+ */
+
+ char *psz_path = strdup( psz_filename );
+ if( !psz_path )
+ return 1;
+
+ char *psz_char = strrchr( psz_path, DIR_SEP_CHAR );
+ if( !psz_char )
+ {
+ free( psz_path );
+ return 1;
+ }
+ *psz_char = '\0';
+
+ /* psz_path now holds the file's directory */
+ psz_char = strrchr( psz_path, DIR_SEP_CHAR );
+ if( !psz_char )
+ {
+ free( psz_path );
+ return 1;
+ }
+ *psz_char = '\0';
+
+ /* psz_path now holds the file's parent directory */
+ if( vlclua_add_modules_path_inner( L, psz_path ) )
+ {
+ free( psz_path );
+ return 1;
+ }
+ *psz_char = DIR_SEP_CHAR;
+
+ /* psz_path now holds the file's directory */
+ if( vlclua_add_modules_path_inner( L, psz_path ) )
+ {
+ free( psz_path );
+ return 1;
+ }
+
+ char **ppsz_dir_list = NULL;
+ vlclua_dir_list( obj, psz_char+1/* gruik? */, &ppsz_dir_list );
+ char **ppsz_dir = ppsz_dir_list;
+
+ for( ; *ppsz_dir && strcmp( *ppsz_dir, psz_path ); ppsz_dir++ );
+ free( psz_path );
+
+ for( ; *ppsz_dir; ppsz_dir++ )
+ {
+ psz_path = *ppsz_dir;
+ psz_char = strrchr( psz_path, DIR_SEP_CHAR );
+ if( !psz_char )
+ goto exit;
+
+ *psz_char = '\0';
+ if( vlclua_add_modules_path_inner( L, psz_path ) )
+ goto exit;
+ *psz_char = DIR_SEP_CHAR;
+
+ if( vlclua_add_modules_path_inner( L, psz_path ) )
+ goto exit;
+ }
+
+ vlclua_dir_list_free( ppsz_dir_list );
+ return 0;
+
+ exit:
+ vlclua_dir_list_free( ppsz_dir_list );
+ return 1;
+}
+