diff options
author | wm4 <wm4@nowhere> | 2020-07-17 01:34:46 +0200 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2020-07-20 21:02:17 +0200 |
commit | 0279a44d93a378fbdff393d6568a691817f83518 (patch) | |
tree | e81a2984f7d71cccb871f43c2c6e25c652548834 /osdep/subprocess.c | |
parent | c9742413ac5eeabfdd46503f67b7393c9bd99f49 (diff) | |
download | mpv-0279a44d93a378fbdff393d6568a691817f83518.tar.bz2 mpv-0279a44d93a378fbdff393d6568a691817f83518.tar.xz |
command: extend subprocess command
Add env and detach arguments. This means the command.c code must use the
"new" mp_subprocess2(). So also take this as an opportunity to clean up.
win32 support gets broken by it, because it never made the switch to the
newer function.
The new detach parameter makes the "run" command fully redundant, but I
guess we'll keep it for simplicity. But change its implementation to use
mp_subprocess2() (couldn't do this earlier, because win32).
Privately, I'm going to use the "env" argument to add a key binding that
starts a shell with a FILE environment variable set to the currently
playing file, so this is very useful to me.
Note: breaks windows, so for example youtube-dl on windows will not work
anymore. mp_subprocess2() has to be implemented. The old functions are
gone, and subprocess-win.c is not built anymore. It will probably work
on Cygwin.
Diffstat (limited to 'osdep/subprocess.c')
-rw-r--r-- | osdep/subprocess.c | 102 |
1 files changed, 0 insertions, 102 deletions
diff --git a/osdep/subprocess.c b/osdep/subprocess.c index 4b5770bf5c..6d91b361ef 100644 --- a/osdep/subprocess.c +++ b/osdep/subprocess.c @@ -29,108 +29,6 @@ void mp_devnull(void *ctx, char *data, size_t size) { } -#if HAVE_POSIX - -int mp_subprocess(char **args, struct mp_cancel *cancel, void *ctx, - subprocess_read_cb on_stdout, subprocess_read_cb on_stderr, - char **error) -{ - struct mp_subprocess_opts opts = { - .exe = args[0], - .args = args, - .cancel = cancel, - }; - opts.fds[opts.num_fds++] = (struct mp_subprocess_fd){ - .fd = 0, // stdin - .src_fd = 0, - }; - opts.fds[opts.num_fds++] = (struct mp_subprocess_fd){ - .fd = 1, // stdout - .on_read = on_stdout, - .on_read_ctx = ctx, - .src_fd = on_stdout ? -1 : 1, - }; - opts.fds[opts.num_fds++] = (struct mp_subprocess_fd){ - .fd = 2, // stderr - .on_read = on_stderr, - .on_read_ctx = ctx, - .src_fd = on_stderr ? -1 : 2, - }; - struct mp_subprocess_result res; - mp_subprocess2(&opts, &res); - if (res.error < 0) { - *error = (char *)mp_subprocess_err_str(res.error); - return res.error; - } - return res.exit_status; -} - -void mp_subprocess_detached(struct mp_log *log, char **args) -{ - mp_msg_flush_status_line(log); - - struct mp_subprocess_opts opts = { - .exe = args[0], - .args = args, - .fds = { - {.fd = 0, .src_fd = 0,}, - {.fd = 1, .src_fd = 1,}, - {.fd = 2, .src_fd = 2,}, - }, - .num_fds = 3, - .detach = true, - }; - struct mp_subprocess_result res; - mp_subprocess2(&opts, &res); - if (res.error < 0) { - mp_err(log, "Starting subprocess failed: %s\n", - mp_subprocess_err_str(res.error)); - } -} - -#else - -struct subprocess_args { - struct mp_log *log; - char **args; -}; - -static void *run_subprocess(void *ptr) -{ - struct subprocess_args *p = ptr; - pthread_detach(pthread_self()); - - mp_msg_flush_status_line(p->log); - - char *err = NULL; - if (mp_subprocess(p->args, NULL, NULL, NULL, NULL, &err) < 0) - mp_err(p->log, "Running subprocess failed: %s\n", err); - - talloc_free(p); - return NULL; -} - -void mp_subprocess_detached(struct mp_log *log, char **args) -{ - struct subprocess_args *p = talloc_zero(NULL, struct subprocess_args); - p->log = mp_log_new(p, log, NULL); - int num_args = 0; - for (int n = 0; args[n]; n++) - MP_TARRAY_APPEND(p, p->args, num_args, talloc_strdup(p, args[n])); - MP_TARRAY_APPEND(p, p->args, num_args, NULL); - pthread_t thread; - if (pthread_create(&thread, NULL, run_subprocess, p)) - talloc_free(p); -} - -void mp_subprocess2(struct mp_subprocess_opts *opts, - struct mp_subprocess_result *res) -{ - *res = (struct mp_subprocess_result){.error = MP_SUBPROCESS_EUNSUPPORTED}; -} - -#endif - const char *mp_subprocess_err_str(int num) { // Note: these are visible to the public client API |