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.
This commit is contained in:
wm4 2020-02-06 19:38:22 +01:00
parent 2aaf531720
commit c58f9a6579
1 changed files with 8 additions and 2 deletions

View File

@ -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;
}