unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Confused about sending text to a subprocess
@ 2010-04-03  0:29 Sean McAfee
  2010-04-03  3:09 ` Harald Hanche-Olsen
  0 siblings, 1 reply; 3+ messages in thread
From: Sean McAfee @ 2010-04-03  0:29 UTC (permalink / raw)
  To: help-gnu-emacs

So I have this little Perl script:

  #!/usr/bin/perl
  open X, ">/home/me/ABCXYZ" or die "$!\n";
  print X "Start ", scalar localtime, "\n";
  print X $_ while <STDIN>;
  print X "\nEnd\n";

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.  It continues to appear in the
list returned by process-list, and its status, as reported by
process-status, is "run".  No output appears in that Perl script's
output file, I suppose because it's being buffered and the script is
still stuck in that loop on STDIN.

In most languages I might write something like this in, I would indicate
an end to the output to the subprocess by simply calling close() on a
stream that's connected to the subprocess's pipe.  I had supposed that
that's what Emacs's process-send-eof does, but I don't think that can be
correct.  Actually, the section of the Emacs Lisp manual entitled
"Sending Input to Processes" is rather confusing in this regard; it
states:

>Some operating systems have limited space for buffered input in a
>PTY. On these systems, Emacs sends an EOF periodically amidst the other
>characters, to force them through. For most programs, these EOFs do no
>harm.

I don't even know what it means to send an EOF amidst other characters.
I thought EOF was the special out-of-band code (usually -1) that
getchar(3) et al. return to indicate that a stream has been closed.

Maybe I'm missing something obvious, but I've looked all over the
"Processes" section of the manual, and I haven't found any function that
says it simply closes the connection to the subprocess.  There are
several that send signals to the subprocess, but I don't want to do
that.

What's the right way to indicate that one is totally finished sending
data to a subprocess, and that the connection should be completely
closed?


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Confused about sending text to a subprocess
  2010-04-03  0:29 Confused about sending text to a subprocess Sean McAfee
@ 2010-04-03  3:09 ` Harald Hanche-Olsen
  2010-04-03  8:47   ` Sean McAfee
  0 siblings, 1 reply; 3+ messages in thread
From: Harald Hanche-Olsen @ 2010-04-03  3:09 UTC (permalink / raw)
  To: help-gnu-emacs

+ 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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Confused about sending text to a subprocess
  2010-04-03  3:09 ` Harald Hanche-Olsen
@ 2010-04-03  8:47   ` Sean McAfee
  0 siblings, 0 replies; 3+ messages in thread
From: Sean McAfee @ 2010-04-03  8:47 UTC (permalink / raw)
  To: help-gnu-emacs

Harald Hanche-Olsen <hanche@math.ntnu.no> writes:
> + 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.

Ah, OK, that makes sense.  I was expecting an ordinary pipe.  And a
little more digging in the manual turned up the variable
process-connection-type, which if nil will cause the processes started
by start-process to be connected with an ordinary pipe.

Thanks for the tip!


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2010-04-03  8:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-04-03  0:29 Confused about sending text to a subprocess Sean McAfee
2010-04-03  3:09 ` Harald Hanche-Olsen
2010-04-03  8:47   ` Sean McAfee

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).