From 8cb20679109e08d932814d9905eca517e510874b Mon Sep 17 00:00:00 2001
From: Fabio Ritrovato <sephiroth87@videolan.org>
Date: Fri, 19 Feb 2010 00:44:31 +0100
Subject: [PATCH] LuaSD: return the SD description from a function
 This allow to load .luac files correctly

---
 modules/misc/lua/vlc.c    |   94 +++++++++++++++++++++++++-------------------
 share/lua/sd/fmc.lua      |    5 ++-
 share/lua/sd/freebox.lua  |    5 ++-
 share/lua/sd/frenchtv.lua |    5 ++-
 4 files changed, 65 insertions(+), 44 deletions(-)

diff --git a/modules/misc/lua/vlc.c b/modules/misc/lua/vlc.c
index 9084739..2ca1477 100644
--- a/modules/misc/lua/vlc.c
+++ b/modules/misc/lua/vlc.c
@@ -579,6 +579,7 @@ static int vlc_sd_probe_Open( vlc_object_t *obj )
     char *psz_name;
     char  *ppsz_dir_list[] = { NULL, NULL, NULL, 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++ )
     {
@@ -603,52 +604,61 @@ static int vlc_sd_probe_Open( vlc_object_t *obj )
             {
                 goto error;
             }
-            FILE *fd = vlc_fopen( psz_filename, "r" );
-            if( fd )
+            L = luaL_newstate();
+            if( !L )
             {
-                char description[256];
-                if( fgets( description, 256, fd ) != NULL )
+                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_Warn( 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, "description" );
+            if( !lua_isfunction( L, lua_gettop( L ) ) || lua_pcall( L, 0, 1, 0 ) )
+            {
+                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;
+                }
+            }
+            else
+            {
+                if( !( psz_longname = strdup( lua_tostring( L, lua_gettop( L ) ) ) ) )
+                {
+                    free( psz_filename );
+                    goto error;
                 }
-                fclose( fd );
             }
+            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 )
@@ -668,6 +678,8 @@ error:
             free( *ppsz_file );
         free( ppsz_filelist );
     }
+    if( L )
+        lua_close( L );
     vlclua_dir_list_free( ppsz_dir_list );
     return VLC_ENOMEM;
 }
diff --git a/share/lua/sd/fmc.lua b/share/lua/sd/fmc.lua
index 9ca3ebe..6f19403 100644
--- a/share/lua/sd/fmc.lua
+++ b/share/lua/sd/fmc.lua
@@ -1,4 +1,3 @@
---SD_Description=Free Music Charts
 --[[
  $Id$
 
@@ -22,6 +21,10 @@
 --]]
 require "simplexml"
 
+function description()
+    return "Free Music Charts"
+end
+
 function main()
     local tree = simplexml.parse_url("http://www.archive.org/download/freemusiccharts.songs/fmc.xml")
     for i = 1, table.getn(tree.children) do
diff --git a/share/lua/sd/freebox.lua b/share/lua/sd/freebox.lua
index ea0e179..6510174 100644
--- a/share/lua/sd/freebox.lua
+++ b/share/lua/sd/freebox.lua
@@ -1,4 +1,3 @@
---SD_Description=Freebox TV
 --[[
  $Id$
 
@@ -21,6 +20,10 @@
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 --]]
 
+function description()
+    return "Freebox TV"
+end
+
 function main()
     local fd = vlc.stream( "http://mafreebox.freebox.fr/freeboxtv/playlist.m3u" )
     local line=  fd:readline()
diff --git a/share/lua/sd/frenchtv.lua b/share/lua/sd/frenchtv.lua
index bd96c9f..ddf6d66 100644
--- a/share/lua/sd/frenchtv.lua
+++ b/share/lua/sd/frenchtv.lua
@@ -1,4 +1,3 @@
---SD_Description=French TV
 --[[
  $Id$
 
@@ -21,6 +20,10 @@
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 --]]
 
+function description()
+    return "French TV"
+end
+
 function main()
     node = vlc.sd.add_node( {title="Canal +"} )
     node:add_subitem( {title="Le SAV des émissions ",url="http://www.canalplus.fr/index.php?pid=1782",options={"http-forward-cookies"}} )
-- 
1.6.0.4

