Skip to content

WIP: print prompt to /dev/tty instead of stderr. #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lineedit/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <wchar.h>
#include <wctype.h>
#include <sys/stat.h>
#include "../redir.h"
#include "../history.h"
#include "../job.h"
#include "../option.h"
Expand Down Expand Up @@ -516,13 +517,13 @@ void finish(void)
display_active = false;
}

/* Flushes the contents of the print buffer to the standard error and destroys
/* Flushes the contents of the print buffer to the tty and destroys
* the buffer. */
void le_display_flush(void)
{
current_position = lebuf.pos;
fwrite(lebuf.buf.contents, 1, lebuf.buf.length, stderr);
fflush(stderr);
write(ttyfd, lebuf.buf.contents, lebuf.buf.length);
fsync(ttyfd);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The write function is not buffered. There is no need to flush the data after writing.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unbuffered-ness of write also means that write may only write part of the contents. You need to examine the return value of write and keep calling it until the whole contents are written.
If you don't want to do it manually, you should keep using fwrite.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, you should not assume ttyfd is always open.

sb_destroy(&lebuf.buf);
}

Expand Down Expand Up @@ -1505,7 +1506,7 @@ size_t select_list_item(size_t index, int offset, size_t listsize)
* terminal and returns true. */
bool le_try_print_prompt(const wchar_t *s)
{
if (isatty(STDERR_FILENO) && le_setupterm(true)) {
if (le_setupterm(true)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this condition would break things when stderr is redirected to a non-terminal file.

lebuf_init((le_pos_T) { 0, 0 });
lebuf_print_prompt(s);
le_display_flush();
Expand Down
7 changes: 3 additions & 4 deletions yash.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ int main(int argc, char **argv)
input.fd = STDIN_FILENO;
inputname = NULL;
if (!options.is_interactive_set && argc == xoptind
&& isatty(STDIN_FILENO) && isatty(STDERR_FILENO))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this condition would break POSIX-conformance.

&& isatty(STDIN_FILENO))
is_interactive = true;
unset_nonblocking(STDIN_FILENO);
} else {
Expand All @@ -212,16 +212,15 @@ int main(int argc, char **argv)
#if YASH_ENABLE_LINEEDIT
/* enable line editing if interactive and connected to a terminal */
if (!options.lineedit_set && shopt_lineedit == SHOPT_NOLINEEDIT)
if (is_interactive && isatty(STDIN_FILENO) && isatty(STDERR_FILENO))
if (is_interactive && isatty(STDIN_FILENO))
set_lineedit_option(SHOPT_VI);
#endif

open_ttyfd();
is_interactive_now = is_interactive;
if (!options.do_job_control_set)
do_job_control = is_interactive;
if (do_job_control) {
open_ttyfd();
if (do_job_control)
ensure_foreground();
Comment on lines +219 to 224
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ttyfd should be opened only when the shell starts job control. Especially, when the shell is non-interactive, opening the ttyfd would be useless.

}
set_signals();
Expand Down