From 21890ccac73990042ae3cd0414e99f7e6317d0f6 Mon Sep 17 00:00:00 2001 From: David Adam Date: Sun, 29 Apr 2018 18:31:47 +0800 Subject: [PATCH] function: restore '%self' functionality for --on-process-exit One key use of process expansion, used in currently-shipped code, is for running a function on current shell exit. Restore the use of %self as a valid argument (and add `self`) and document this change. (faho: Remove bare "self") --- doc_src/function.txt | 5 ++++- src/builtin_function.cpp | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/doc_src/function.txt b/doc_src/function.txt index d9aad268b..b6db339e9 100644 --- a/doc_src/function.txt +++ b/doc_src/function.txt @@ -25,7 +25,10 @@ The following options are available: - `-j PGID` or `--on-job-exit PGID` tells fish to run this function when the job with group ID PGID exits. Instead of PGID, the string 'caller' can be specified. This is only legal when in a command substitution, and will result in the handler being triggered by the exit of the job which created this command substitution. -- `-p PID` or `--on-process-exit PID` tells fish to run this function when the fish child process with process ID PID exits. +- `-p PID` or `--on-process-exit PID` tells fish to run this function when the fish child process + with process ID PID exits. Instead of a PID, for backward compatibility, + "`%self`" can be specified as an alias for `$fish_pid`, and the function will be run when the + current fish instance exits. - `-s` or `--on-signal SIGSPEC` tells fish to run this function when the signal SIGSPEC is delivered. SIGSPEC can be a signal number, or the signal name, such as SIGHUP (or just HUP). diff --git a/src/builtin_function.cpp b/src/builtin_function.cpp index ca66b301c..7794dc307 100644 --- a/src/builtin_function.cpp +++ b/src/builtin_function.cpp @@ -115,6 +115,10 @@ static int parse_cmd_opts(function_cmd_opts_t &opts, int *optind, //!OCLINT(hig } e.type = EVENT_JOB_ID; e.param1.job_id = job_id; + } else if ((opt == 'p') && (wcscasecmp(w.woptarg, L"%self") == 0)) { + pid = getpid(); + e.type = EVENT_EXIT; + e.param1.pid = pid; } else { pid = fish_wcstoi(w.woptarg); if (errno || pid < 0) {