1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2025-01-31 20:02:42 +00:00

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).
This commit is contained in:
Peder O. Klingenberg 2020-09-20 00:16:36 +02:00 committed by Lars Ingebrigtsen
parent eda48b6fed
commit a2be81780e
3 changed files with 33 additions and 4 deletions

View File

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

View File

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

View File

@ -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',