[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Behavior of readline() when it encounters EOF
From: |
Chet Ramey |
Subject: |
Re: Behavior of readline() when it encounters EOF |
Date: |
Thu, 6 Oct 2022 15:33:48 -0400 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.3.1 |
On 10/6/22 12:59 PM, Andrea Monaco wrote:
The readline manual says that: "If readline encounters an EOF while
reading the line, and the line is empty at that point, then (char *)NULL
is returned. Otherwise, the line is ended just as if a newline had been
typed" (from lib/readline/doc/rltech.texi).
But the second sentence appears wrong. When I type Ctrl-D at a readline
prompt and the line is non-empty, nothing happens. You can check at any
readline prompt, including bash.
It's confusing. The ^D character is the stty EOF character, and is a valid
bindable readline key sequence. It's not the same as encountering EOF,
which is indicated by read(2) returning 0.
When readline reads ^D on an empty line, it returns NULL. If it reads
^D and the line isn't empty, it deletes the character following the cursor.
So on an empty line, readline simulates an EOF by returning NULL.
Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU chet@case.edu http://tiswww.cwru.edu/~chet/