Improve handling of ioctl() errors, cleanup and comment on ordering.

The ordering of alternative rl_ttyset() implementations is important.
We have chosen to place the most common, most portable, code first
and then in lesser order.  Ending with the sgtty.h based code, which
I cannot test on GNU/Linux.  Will test on OpenBSD later.

Also added Emacs indentation style to make it easier to follow the
original style of Mr Turner and Salz.
This commit is contained in:
Joachim Nilsson 2010-07-18 16:07:02 +02:00
parent a0ec848f7b
commit 8b85bab216

View File

@ -20,6 +20,7 @@
*/ */
#include "editline.h" #include "editline.h"
/* Prefer termios over the others since it is likely the most portable. */
#if defined(HAVE_TCGETATTR) #if defined(HAVE_TCGETATTR)
#include <termios.h> #include <termios.h>
@ -28,8 +29,9 @@ void rl_ttyset(int Reset)
static struct termios old; static struct termios old;
struct termios new; struct termios new;
if (Reset == 0) { if (!Reset) {
if (tcgetattr(0, &old) < 0) perror("tcgetattr"); if (-1 == tcgetattr(0, &old))
perror("Failed tcgetattr");
rl_erase = old.c_cc[VERASE]; rl_erase = old.c_cc[VERASE];
rl_kill = old.c_cc[VKILL]; rl_kill = old.c_cc[VKILL];
rl_eof = old.c_cc[VEOF]; rl_eof = old.c_cc[VEOF];
@ -44,9 +46,11 @@ void rl_ttyset(int Reset)
new.c_iflag &= ~(ISTRIP | INPCK); new.c_iflag &= ~(ISTRIP | INPCK);
new.c_cc[VMIN] = 1; new.c_cc[VMIN] = 1;
new.c_cc[VTIME] = 0; new.c_cc[VTIME] = 0;
if (tcsetattr(0, TCSADRAIN, &new) < 0) perror("tcsetattr"); if (-1 == tcsetattr(0, TCSADRAIN, &new))
perror("Failed tcsetattr");
} else { } else {
(void)tcsetattr(0, TCSADRAIN, &old); if (-1 == tcsetattr(0, TCSADRAIN, &old))
perror("Failed tcsetattr");
} }
} }
@ -58,8 +62,9 @@ void rl_ttyset(int Reset)
static struct termio old; static struct termio old;
struct termio new; struct termio new;
if (Reset == 0) { if (!Reset) {
(void)ioctl(0, TCGETA, &old); if (-1 == ioctl(0, TCGETA, &old))
perror("Failed ioctl(TCGETA)");
rl_erase = old.c_cc[VERASE]; rl_erase = old.c_cc[VERASE];
rl_kill = old.c_cc[VKILL]; rl_kill = old.c_cc[VKILL];
rl_eof = old.c_cc[VEOF]; rl_eof = old.c_cc[VEOF];
@ -74,13 +79,15 @@ void rl_ttyset(int Reset)
new.c_iflag &= ~(ISTRIP | INPCK); new.c_iflag &= ~(ISTRIP | INPCK);
new.c_cc[VMIN] = 1; new.c_cc[VMIN] = 1;
new.c_cc[VTIME] = 0; new.c_cc[VTIME] = 0;
(void)ioctl(0, TCSETAW, &new); if (-1 == ioctl(0, TCSETAW, &new))
perror("Failed ioctl(TCSETAW)");
} else { } else {
(void)ioctl(0, TCSETAW, &old); if (-1 == ioctl(0, TCSETAW, &old))
perror("Failed ioctl(TCSETAW)");
} }
} }
#else /* Neither HAVE_TERMIO_H or HAVE_TCGETATTR */ #elif defined(HAVE_SGTTY_H)
#include <sgtty.h> #include <sgtty.h>
void rl_ttyset(int Reset) void rl_ttyset(int Reset)
@ -93,18 +100,21 @@ void rl_ttyset(int Reset)
struct ltchars old_ltchars; struct ltchars old_ltchars;
#endif #endif
if (Reset == 0) { if (!Reset) {
(void)ioctl(0, TIOCGETP, &old_sgttyb); if (-1 == ioctl(0, TIOCGETP, &old_sgttyb))
perror("Failed TIOCGETP");
rl_erase = old_sgttyb.sg_erase; rl_erase = old_sgttyb.sg_erase;
rl_kill = old_sgttyb.sg_kill; rl_kill = old_sgttyb.sg_kill;
(void)ioctl(0, TIOCGETC, &old_tchars); if (-1 == ioctl(0, TIOCGETC, &old_tchars))
perror("Failed TIOCGETC");
rl_eof = old_tchars.t_eofc; rl_eof = old_tchars.t_eofc;
rl_intr = old_tchars.t_intrc; rl_intr = old_tchars.t_intrc;
rl_quit = old_tchars.t_quitc; rl_quit = old_tchars.t_quitc;
#ifdef CONFIG_SIGSTOP #ifdef CONFIG_SIGSTOP
(void)ioctl(0, TIOCGLTC, &old_ltchars); if (-1 == ioctl(0, TIOCGLTC, &old_ltchars))
perror("Failed TIOCGLTC");
rl_susp = old_ltchars.t_suspc; rl_susp = old_ltchars.t_suspc;
#endif #endif
@ -114,24 +124,37 @@ void rl_ttyset(int Reset)
#ifdef PASS8 #ifdef PASS8
new_sgttyb.sg_flags |= PASS8; new_sgttyb.sg_flags |= PASS8;
#endif #endif
(void)ioctl(0, TIOCSETP, &new_sgttyb); if (-1 == ioctl(0, TIOCSETP, &new_sgttyb))
perror("Failed TIOCSETP");
new_tchars = old_tchars; new_tchars = old_tchars;
new_tchars.t_intrc = -1; new_tchars.t_intrc = -1;
new_tchars.t_quitc = -1; new_tchars.t_quitc = -1;
(void)ioctl(0, TIOCSETC, &new_tchars); if (-1 == ioctl(0, TIOCSETC, &new_tchars))
perror("Failed TIOCSETC");
} else { } else {
(void)ioctl(0, TIOCSETP, &old_sgttyb); if (-1 == ioctl(0, TIOCSETP, &old_sgttyb))
(void)ioctl(0, TIOCSETC, &old_tchars); perror("Failed TIOCSETP");
if (-1 == ioctl(0, TIOCSETC, &old_tchars))
perror("Failed TIOCSETC");
} }
} }
#endif /* Neither HAVE_TERMIO_H or HAVE_TCGETATTR */ #else /* Neither HAVE_SGTTY_H, HAVE_TERMIO_H or HAVE_TCGETATTR */
#error Unsupported platform, missing tcgetattr(), termio.h and sgtty.h
#endif /* Neither HAVE_SGTTY_H, HAVE_TERMIO_H or HAVE_TCGETATTR */
void rl_add_slash(char *path, char *p) void rl_add_slash(char *path, char *p)
{ {
struct stat Sb; struct stat Sb;
if (stat(path, &Sb) >= 0) if (stat(path, &Sb) >= 0)
(void)strcat(p, S_ISDIR(Sb.st_mode) ? "/" : " "); strcat(p, S_ISDIR(Sb.st_mode) ? "/" : " ");
} }
/**
* Local Variables:
* version-control: t
* indent-tabs-mode: t
* c-file-style: "ellemtel"
* c-basic-offset: 4
* End:
*/