From c58f9a657995b213790204a344d4f5ddcbd1b6c0 Mon Sep 17 00:00:00 2001 From: wm4 Date: Thu, 6 Feb 2020 19:38:22 +0100 Subject: scripting: give proper name to scripts using a directory The recently added feature to load scripts from a sub-directory. A problem was that the script name was still derived from the actual script source file that was loaded, which was always "main.lua" (or similar), resulting in "main" as name. Fix this by using the directory name. One odd corner case is that you can do "--script=/path/foo////". As by POSIX semantics, the extra "/" are ignored. However, the newly added code strips only one separator, so mp_basename() returns "" (as it should), and the empty name makes mp_new_client() fail in turn. I don't really care about this, just don't do it. But mp_new_client() failure was silent (since it normally never happens), so add at least an error message for that, instead of being entirely silent. --- player/scripting.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/player/scripting.c b/player/scripting.c index bab34d0d03..d83c4d241f 100644 --- a/player/scripting.c +++ b/player/scripting.c @@ -104,6 +104,7 @@ static int mp_load_script(struct MPContext *mpctx, const char *fname) void *tmp = talloc_new(NULL); const char *path = NULL; + char *script_name = NULL; const struct mp_scripting *backend = NULL; struct stat s; @@ -129,6 +130,10 @@ static int mp_load_script(struct MPContext *mpctx, const char *fname) talloc_free(tmp); return -1; } + + script_name = talloc_strdup(tmp, path); + mp_path_strip_trailing_separator(script_name); + script_name = mp_basename(script_name); } else { for (int n = 0; scripting_backends[n]; n++) { const struct mp_scripting *b = scripting_backends[n]; @@ -137,6 +142,7 @@ static int mp_load_script(struct MPContext *mpctx, const char *fname) break; } } + script_name = script_name_from_filename(tmp, fname); } if (!backend) { @@ -146,7 +152,6 @@ static int mp_load_script(struct MPContext *mpctx, const char *fname) } struct mp_script_args *arg = talloc_ptrtype(NULL, arg); - char *name = script_name_from_filename(arg, fname); *arg = (struct mp_script_args){ .mpctx = mpctx, .filename = talloc_strdup(arg, fname), @@ -155,12 +160,13 @@ static int mp_load_script(struct MPContext *mpctx, const char *fname) // Create the client before creating the thread; otherwise a race // condition could happen, where MPContext is destroyed while the // thread tries to create the client. - .client = mp_new_client(mpctx->clients, name), + .client = mp_new_client(mpctx->clients, script_name), }; talloc_free(tmp); if (!arg->client) { + MP_ERR(mpctx, "Failed to create client for script: %s\n", fname); talloc_free(arg); return -1; } -- cgit v1.2.3