help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Confused about sending text to a subprocess


From: Harald Hanche-Olsen
Subject: Re: Confused about sending text to a subprocess
Date: Tue, 04 May 2010 15:43:58 -0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (darwin)

+ Sean McAfee <eefacm@gmail.com>:

> So I have this little Perl script:
> [...]
> I invoke it from Emacs like this:
>
>   (let ((proc (start-process "x" nil "/path/to/perl/script")))
>     (process-send-string proc "foo\n")
>     (process-send-eof proc))
>
> It works as I expect.  But if I leave off that newline:
>
>     (process-send-string proc "foo")
>
> ...then the subprocess does not exit.

That is because emacs runs it in a pty, and "eof" on a pty in line
buffered mode is signaled to the reading process by read() returning 0
bytes. In fact, what (process-send-eof proc) does, is to make all
(zero or more) characters waiting in the input buffer available to the
reading process. If the input buffer was empty, the reading process will
normally interpret that as end-of-file. But if the input buffer was
nonempty, the reading process will receive the contents of the buffer,
(in this case, "foo") with no newline at the end. So it will now hang in
a read waiting for more characters followed by a newline.

Try running your perl script from the command line: The equivalent of
your emacs program is to give it the input "foo\C-d" and then stop
typing. Go ahead, try it. Then type some more text, return, and end with
C-d on the beginning of the line. This time the perl program will exit.

-- 
* Harald Hanche-Olsen     <URL:http://www.math.ntnu.no/~hanche/>
- It is undesirable to believe a proposition
  when there is no ground whatsoever for supposing it is true.
  -- Bertrand Russell


reply via email to

[Prev in Thread] Current Thread [Next in Thread]