From a2be81780ef466f6007a8a5ec909106c5f8aebf0 Mon Sep 17 00:00:00 2001 From: "Peder O. Klingenberg" Date: Sun, 20 Sep 2020 00:16:36 +0200 Subject: [PATCH] Extend process-lines to allow exit status handling * subr.el (process-lines-handling-status): Extension of the old process-lines, with more flexible handling of the exit status. (process-lines): Old API implemented using the new function. (process-lines-ignore-status): Another use of the new function - return the output lines regardless of the exit status (bug#1321). --- doc/lispref/processes.texi | 5 +++++ etc/NEWS | 6 ++++++ lisp/subr.el | 26 ++++++++++++++++++++++---- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/doc/lispref/processes.texi b/doc/lispref/processes.texi index 4002004cd6f..e0884520757 100644 --- a/doc/lispref/processes.texi +++ b/doc/lispref/processes.texi @@ -603,6 +603,11 @@ This function works by calling @code{call-process}, so program output is decoded in the same way as for @code{call-process}. @end defun +@defun process-lines-ignore-status program &rest args +This function is just like @code{process-lines}, but does not signal +an error if @var{program} exists with a non-zero exit status. +@end defun + @node Asynchronous Processes @section Creating an Asynchronous Process @cindex asynchronous subprocess diff --git a/etc/NEWS b/etc/NEWS index 67cfd5fd00a..fb8f2845f74 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -1371,6 +1371,12 @@ ledit.el, lmenu.el, lucid.el and old-whitespace.el. * Lisp Changes in Emacs 28.1 ++++ +*** New function 'process-lines-ignore-status'. +This is like 'process-lines', but does not signal an error if the +return status is non-zero. 'process-lines-handling-status' has also +been added, and takes a callback to handle the return status. + +++ *** New function 'replace-in-string'. This function works along the line of 'replace-regexp-in-string', but diff --git a/lisp/subr.el b/lisp/subr.el index f3c1e206603..f00e4ef867c 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -2344,13 +2344,19 @@ use `start-file-process'." (if program (list :command (cons program program-args)))))) -(defun process-lines (program &rest args) +(defun process-lines-handling-status (program status-handler &rest args) "Execute PROGRAM with ARGS, returning its output as a list of lines. -Signal an error if the program returns with a non-zero exit status." +If STATUS-HANDLER is non-NIL, it must be a function with one +argument, which will be called with the exit status of the +program before the output is collected. If STATUS-HANDLER is +NIL, an error is signalled if the program returns with a non-zero +exit status." (with-temp-buffer (let ((status (apply 'call-process program nil (current-buffer) nil args))) - (unless (eq status 0) - (error "%s exited with status %s" program status)) + (if status-handler + (funcall status-handler status) + (unless (eq status 0) + (error "%s exited with status %s" program status))) (goto-char (point-min)) (let (lines) (while (not (eobp)) @@ -2361,6 +2367,18 @@ Signal an error if the program returns with a non-zero exit status." (forward-line 1)) (nreverse lines))))) +(defun process-lines (program &rest args) + "Execute PROGRAM with ARGS, returning its output as a list of lines. +Signal an error if the program returns with a non-zero exit status. +Also see `process-lines-ignore-status'." + (apply #'process-lines-handling-status program nil args)) + +(defun process-lines-ignore-status (program &rest args) + "Execute PROGRAM with ARGS, returning its output as a list of lines. +The exit status of the program is ignored. +Also see `process-lines'." + (apply #'process-lines-handling-status program #'identity args)) + (defun process-live-p (process) "Return non-nil if PROCESS is alive. A process is considered alive if its status is `run', `open', -- 2.39.5