add_shortcut( "luasd" )
set_capability( "services_discovery", 0 )
add_string( "lua-sd", "", NULL, "", "", false )
+ add_string( "lua-longname", "", NULL, "", "", false )
+ set_callbacks( Open_LuaSD, Close_LuaSD )
+
+ add_submodule ()
+ set_description( N_("Freebox TV") )
+ add_shortcut( "freebox" )
+ set_capability( "services_discovery", 0 )
+ set_callbacks( Open_LuaSD, Close_LuaSD )
+
+ add_submodule ()
+ set_description( N_("French TV") )
+ add_shortcut( "frenchtv" )
+ set_capability( "services_discovery", 0 )
set_callbacks( Open_LuaSD, Close_LuaSD )
VLC_SD_PROBE_SUBMODULE
return strcmp( *a, *b );
}
-int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname, char **ppsz_dir_list )
+int vlclua_dir_list( vlc_object_t *p_this, const char *luadirname,
+ char ***pppsz_dir_list )
{
+#define MAX_DIR_LIST_SIZE 5
+ *pppsz_dir_list = malloc(MAX_DIR_LIST_SIZE*sizeof(char *));
+ if (!*pppsz_dir_list)
+ return VLC_EGENERIC;
+ char **ppsz_dir_list = *pppsz_dir_list;
+
int i = 0;
char *datadir = config_GetUserDir( VLC_DATA_DIR );
- if( datadir == NULL )
- return VLC_ENOMEM;
- if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
- datadir, luadirname ) < 0 )
- {
- free( datadir );
- return VLC_ENOMEM;
- }
+ if( likely(datadir != NULL)
+ && likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
+ datadir, luadirname ) != -1) )
+ i++;
free( datadir );
- i++;
+
+#if !(defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32))
+ if( likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
+ config_GetLibDir(), luadirname ) != -1) )
+ i++;
+#endif
char *psz_datapath = config_GetDataDir( p_this );
-# if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
+ if( likely(psz_datapath != NULL) )
{
- if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
- psz_datapath, luadirname ) < 0 )
- return VLC_ENOMEM;
- i++;
- if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "share" DIR_SEP "lua" DIR_SEP "%s",
- psz_datapath, luadirname ) < 0 )
- return VLC_ENOMEM;
- i++;
-
+ if( likely(asprintf( &ppsz_dir_list[i], "%s"DIR_SEP"lua"DIR_SEP"%s",
+ psz_datapath, luadirname ) != -1) )
+ i++;
+
+#if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
+ if( likely(asprintf( &ppsz_dir_list[i],
+ "%s"DIR_SEP"share"DIR_SEP"lua"DIR_SEP"%s",
+ psz_datapath, luadirname ) != -1) )
+ i++;
+#endif
+ free( psz_datapath );
}
-# else
- if( asprintf( &ppsz_dir_list[i], "%s" DIR_SEP "lua" DIR_SEP "%s",
- psz_datapath, luadirname ) < 0 )
- return VLC_ENOMEM;
- i++;
-# endif
- free( psz_datapath );
+
+ ppsz_dir_list[i] = NULL;
+
+ assert( i < MAX_DIR_LIST_SIZE);
+
return VLC_SUCCESS;
}
char **ppsz_dir;
for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
free( *ppsz_dir );
+ free( ppsz_dir_list );
}
/*****************************************************************************
int (*func)(vlc_object_t *, const char *, void *),
void * user_data)
{
- char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
+ char **ppsz_dir_list = NULL;
- int i_ret = vlclua_dir_list( p_this, luadirname, ppsz_dir_list );
+ int i_ret = vlclua_dir_list( p_this, luadirname, &ppsz_dir_list );
if( i_ret != VLC_SUCCESS )
return i_ret;
i_ret = VLC_EGENERIC;
char *vlclua_find_file( vlc_object_t *p_this, const char *psz_luadirname, const char *psz_name )
{
- char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
+ char **ppsz_dir_list = NULL;
char **ppsz_dir;
- vlclua_dir_list( p_this, psz_luadirname, ppsz_dir_list );
+ vlclua_dir_list( p_this, psz_luadirname, &ppsz_dir_list );
for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
{
for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
char **ppsz_fileend = NULL;
char **ppsz_file;
char *psz_name;
- char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
+ char **ppsz_dir_list = NULL;
char **ppsz_dir;
- vlclua_dir_list( obj, "sd", ppsz_dir_list );
+ 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;
{
goto error;
}
- FILE *fd = vlc_fopen( psz_filename, "r" );
- if( fd )
+ L = luaL_newstate();
+ if( !L )
+ {
+ msg_Err( probe, "Could not create new Lua State" );
+ free( psz_filename );
+ goto error;
+ }
+ 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 );
+ free( 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 );
+ free( psz_filename );
+ lua_close( L );
+ continue;
+ }
+ 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 ) )
{
- char description[256];
- if( fgets( description, 256, fd ) != NULL )
+ lua_pop( L, 1 );
+ if( !( psz_longname = strdup( *ppsz_file ) ) )
{
- char *temp = strchr( description, '\n' );
- if( temp )
- *temp = '\0';
- temp = strchr( *ppsz_file, '.' );
- if( temp )
- *temp = '\0';
- char *psz_longname;
- if( !strncmp( description, "--SD_Description=", 17 ) )
- {
- if( !( psz_longname = strdup( description + 17 ) ) )
- {
- fclose( fd );
- free( psz_filename );
- goto error;
- }
- }
- else
- {
- if( !( psz_longname = strdup( *ppsz_file ) ) )
- {
- fclose( fd );
- free( psz_filename );
- goto error;
- }
- }
- if( asprintf( &psz_name, "lua{sd=%s,longname=%s}",
- *ppsz_file, psz_longname ) < 0 )
- {
- fclose( fd );
- 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 );
+ goto error;
}
- fclose( fd );
}
+ 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 )
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 )
+{
+ int count = 0;
+ for( const char **ppsz_ext = ppsz_lua_exts; *ppsz_ext; ppsz_ext++ )
+ {
+ lua_pushfstring( L, "%s"DIR_SEP"modules"DIR_SEP"?%s;",
+ psz_path, *ppsz_ext );
+ count ++;
+ }
+
+ return count;
+}
+
+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';
+
+ /* Push package on stack */
+ int count = 0;
+ lua_getglobal( L, "package" );
+
+ /* psz_path now holds the file's parent directory */
+ count += vlclua_add_modules_path_inner( L, psz_path );
+ *psz_char = DIR_SEP_CHAR;
+
+ /* psz_path now holds the file's directory */
+ count += vlclua_add_modules_path_inner( L, psz_path );
+
+ 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;
+ /* FIXME: doesn't work well with meta/... modules due to the double
+ * directory depth */
+ psz_char = strrchr( psz_path, DIR_SEP_CHAR );
+ if( !psz_char )
+ {
+ vlclua_dir_list_free( ppsz_dir_list );
+ return 1;
+ }
+
+ *psz_char = '\0';
+ count += vlclua_add_modules_path_inner( L, psz_path );
+ *psz_char = DIR_SEP_CHAR;
+ count += vlclua_add_modules_path_inner( L, psz_path );
+ }
+
+ lua_getfield( L, -(count+1), "path" ); /* Get package.path */
+ lua_concat( L, count+1 ); /* Concat vlc module paths and package.path */
+ lua_setfield( L, -2, "path"); /* Set package.path */
+ lua_pop( L, 1 ); /* Pop the package module */
+
+ vlclua_dir_list_free( ppsz_dir_list );
+ return 0;
+}
+