]> git.sesse.net Git - vlc/blobdiff - modules/misc/lua/vlc.c
Also load .luac files.
[vlc] / modules / misc / lua / vlc.c
index 46997fa4ff3a727c156ed12d9258ce91bd7c00d9..591206a0e3d6d089bafad760d8448df716a09fe6 100644 (file)
@@ -41,6 +41,7 @@
 #include <vlc_charset.h>
 #include <vlc_aout.h>
 #include <vlc_services_discovery.h>
+#include <sys/stat.h>
 
 #include <lua.h>        /* Low level lua C API */
 #include <lauxlib.h>    /* Higher level C API */
@@ -129,10 +130,18 @@ vlc_module_end ()
 /*****************************************************************************
  *
  *****************************************************************************/
+static const char *ppsz_lua_exts[] = { ".luac", ".lua", NULL };
 static int file_select( const char *file )
 {
     int i = strlen( file );
-    return i > 4 && !strcmp( file+i-4, ".lua" );
+    int j;
+    for( j = 0; ppsz_lua_exts[j]; j++ )
+    {
+        int l = strlen( ppsz_lua_exts[j] );
+        if( !strcmp( file+i-l, ppsz_lua_exts[j] ) )
+            return 1;
+    }
+    return 0;
 }
 
 static int file_compare( const char **a, const char **b )
@@ -196,72 +205,89 @@ int vlclua_scripts_batch_execute( vlc_object_t *p_this,
                                   lua_State * L,
                                   void * user_data)
 {
-    int i_ret = VLC_EGENERIC;
-
-    char **ppsz_filelist = NULL;
-    char **ppsz_fileend  = NULL;
-    char **ppsz_file;
-
     char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
-    char **ppsz_dir;
 
-    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;
 
-
-    for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
+    for( char **ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
     {
+        char **ppsz_filelist;
         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;
-        }
-
         msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
         i_files = utf8_scandir( *ppsz_dir, &ppsz_filelist, file_select,
                                 file_compare );
-        if( i_files < 1 ) continue;
-        ppsz_fileend = ppsz_filelist + i_files;
+        if( i_files < 0 )
+            continue;
 
-        for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
+        char **ppsz_file = ppsz_filelist;
+        char **ppsz_fileend = ppsz_filelist + i_files;
+
+        while( ppsz_file < ppsz_fileend )
         {
-            char  *psz_filename;
+            char *psz_filename;
+
             if( asprintf( &psz_filename,
-                          "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) < 0)
+                          "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file ) == -1 )
+                psz_filename = NULL;
+            free( *(ppsz_file++) );
+
+            if( likely(psz_filename != NULL) )
             {
-                vlclua_dir_list_free( ppsz_dir_list );
-                return VLC_ENOMEM;
+                msg_Dbg( p_this, "Trying Lua playlist script %s",
+                         psz_filename );
+                i_ret = func( p_this, psz_filename, L, user_data );
+                free( psz_filename );
+                if( i_ret == VLC_SUCCESS )
+                    break;
             }
-            msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
-
-            i_ret = func( p_this, psz_filename, L, user_data );
-
-            free( psz_filename );
-
-            if( i_ret == VLC_SUCCESS ) break;
         }
-        if( i_ret == VLC_SUCCESS ) break;
-    }
 
-    if( ppsz_filelist )
-    {
-        for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
-             ppsz_file++ )
-            free( *ppsz_file );
+        while( ppsz_file < ppsz_fileend )
+            free( *(ppsz_file++) );
         free( ppsz_filelist );
+
+        if( i_ret == VLC_SUCCESS )
+            break;
     }
     vlclua_dir_list_free( ppsz_dir_list );
-
     return i_ret;
 }
 
+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;
+    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 *psz_filename;
+            struct stat st;
+
+            if( asprintf( &psz_filename, "%s"DIR_SEP"%s%s", *ppsz_dir,
+                          psz_name, *ppsz_ext ) < 0 )
+            {
+                vlclua_dir_list_free( ppsz_dir_list );
+                return NULL;
+            }
+
+            if( utf8_stat( psz_filename, &st ) == 0
+                && S_ISREG( st.st_mode ) )
+            {
+                vlclua_dir_list_free( ppsz_dir_list );
+                return psz_filename;
+            }
+            free( psz_filename );
+        }
+    }
+    vlclua_dir_list_free( ppsz_dir_list );
+    return NULL;
+}
 
 /*****************************************************************************
  * Meta data setters utility.
@@ -406,12 +432,14 @@ int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
                                     input_item_t *p_parent, bool b_play )
 {
     int i_count = 0;
+    input_item_node_t *p_parent_node = NULL;
 
     assert( p_parent || p_playlist );
 
     /* playlist */
     if( lua_istable( L, -1 ) )
     {
+        if( p_parent ) p_parent_node = input_item_node_Create( p_parent );
         lua_pushnil( L );
         /* playlist nil */
         while( lua_next( L, -2 ) )
@@ -486,7 +514,7 @@ int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
                     /* Append item to playlist */
                     if( p_parent ) /* Add to node */
                     {
-                        input_item_PostSubItem( p_parent, p_input );
+                        input_item_node_AppendItem( p_parent_node, p_input );
                     }
                     else /* Play or Enqueue (preparse) */
                         /* FIXME: playlist_AddInput() can fail */
@@ -518,6 +546,11 @@ int __vlclua_playlist_add_internal( vlc_object_t *p_this, lua_State *L,
             /* playlist key */
         }
         /* playlist */
+        if( p_parent )
+        {
+            if( i_count ) input_item_node_PostAndDelete( p_parent_node );
+            else input_item_node_Delete( p_parent_node );
+        }
     }
     else
     {
@@ -569,7 +602,8 @@ static int vlc_sd_probe_Open( vlc_object_t *obj )
                     if( temp )
                         *temp = '\0';
                     *(*ppsz_file + strlen(*ppsz_file) - 4 )= '\0';
-                    if( asprintf( &psz_name, "lua{sd=%s}", *ppsz_file ) < 0 )
+                    if( asprintf( &psz_name, "lua{sd=%s,longname=%s}",
+                                  *ppsz_file, description + 17 ) < 0 )
                     {
                         fclose( fd );
                         free( psz_filename );