summaryrefslogtreecommitdiff
path: root/attach.c
diff options
context:
space:
mode:
Diffstat (limited to 'attach.c')
-rw-r--r--attach.c90
1 files changed, 48 insertions, 42 deletions
diff --git a/attach.c b/attach.c
index c5fb412..ba2e3a0 100644
--- a/attach.c
+++ b/attach.c
@@ -26,49 +26,52 @@
#endif
#endif
-// The current terminal settings. After coming back from a suspend, we
-// restore this.
+/*
+** The current terminal settings. After coming back from a suspend, we
+** restore this.
+*/
static struct termios cur_term;
-// 1 if the window size changed
+/* 1 if the window size changed */
static int win_changed;
-// 1 if we want a redraw
+/* 1 if we want a redraw */
static int want_redraw;
-// This hopefully moves to the bottom of the screen
+/* This hopefully moves to the bottom of the screen */
#define EOS "\033[999H"
-// Restores the original terminal settings.
+/* Restores the original terminal settings. */
static void
restore_term(void)
{
tcsetattr(0, TCSADRAIN, &orig_term);
- // Make cursor visible. Assumes VT100.
+
+ /* Make cursor visible. Assumes VT100. */
printf("\033[?25h\033[?0c");
fflush(stdout);
}
-// Connects to a unix domain socket
+/* Connects to a unix domain socket */
static int
connect_socket(char *name)
{
int s;
- struct sockaddr_un sun;
+ struct sockaddr_un sockun;
s = socket(PF_UNIX, SOCK_STREAM, 0);
if (s < 0)
return -1;
- sun.sun_family = AF_UNIX;
- strcpy(sun.sun_path, name);
- if (connect(s, (struct sockaddr*)&sun, sizeof(sun)) < 0)
+ sockun.sun_family = AF_UNIX;
+ strcpy(sockun.sun_path, name);
+ if (connect(s, (struct sockaddr*)&sockun, sizeof(sockun)) < 0)
return -1;
return s;
}
-// Signal
+/* Signal */
static RETSIGTYPE
die(int sig)
{
- // Print a nice pretty message for some things.
+ /* Print a nice pretty message for some things. */
if (sig == SIGHUP || sig == SIGINT)
printf(EOS "\r\n[detached]\r\n");
else
@@ -76,50 +79,51 @@ die(int sig)
exit(1);
}
-// Window size change.
+/* Window size change. */
static RETSIGTYPE
win_change()
{
win_changed = 1;
}
-// Handles input from the keyboard.
+/* Handles input from the keyboard. */
static void
process_kbd(int s, struct packet *pkt)
{
- // Suspend?
+ /* Suspend? */
if (!no_suspend && (pkt->u.buf[0] == cur_term.c_cc[VSUSP]))
{
- // Tell the master that we are suspending.
+ /* Tell the master that we are suspending. */
pkt->type = MSG_DETACH;
write(s, pkt, sizeof(*pkt));
- // And suspend...
+ /* And suspend... */
tcsetattr(0, TCSADRAIN, &orig_term);
+ printf(EOS "\r\n");
kill(getpid(), SIGTSTP);
tcsetattr(0, TCSADRAIN, &cur_term);
- // Tell the master that we are returning.
+ /* Tell the master that we are returning. */
pkt->type = MSG_ATTACH;
write(s, pkt, sizeof(*pkt));
- // The window size might have changed, and we definately want
- // a redraw. We don't want to pass the suspend, though.
+ /* The window size might have changed, and we definately want
+ ** a redraw. We don't want to pass the suspend, though. */
win_changed = 1;
want_redraw = 1;
return;
}
- // Detach char?
+ /* Detach char? */
else if (pkt->u.buf[0] == detach_char)
{
printf(EOS "\r\n[detached]\r\n");
exit(1);
}
- // Just in case something pukes out.
+ /* Just in case something pukes out. */
else if (pkt->u.buf[0] == '\f')
win_changed = 1;
- // Push it out
+ /* Push it out */
write(s, pkt, sizeof(*pkt));
}
@@ -131,14 +135,14 @@ attach_main(int noerror)
struct packet pkt;
unsigned char buf[BUFSIZE];
- // The current terminal settings are equal to the original terminal
- // settings at this point.
+ /* The current terminal settings are equal to the original terminal
+ ** settings at this point. */
cur_term = orig_term;
- // Set a trap to restore the terminal when we die.
+ /* Set a trap to restore the terminal when we die. */
atexit(restore_term);
- // Set some signals.
+ /* Set some signals. */
signal(SIGPIPE, SIG_IGN);
signal(SIGXFSZ, SIG_IGN);
signal(SIGHUP, die);
@@ -147,8 +151,8 @@ attach_main(int noerror)
signal(SIGQUIT, die);
signal(SIGWINCH, win_change);
- // Attempt to open the socket. Don't display an error if noerror is
- // set.
+ /* Attempt to open the socket. Don't display an error if noerror is
+ ** set. */
s = connect_socket(sockname);
if (s < 0)
{
@@ -158,20 +162,22 @@ attach_main(int noerror)
return 1;
}
- // Set raw mode, almost. We allow flow control to work, for instance.
+ /* Set raw mode, almost. We allow flow control to work, for instance. */
cur_term.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL);
cur_term.c_oflag &= ~(OPOST);
cur_term.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
cur_term.c_cflag &= ~(CSIZE|PARENB);
cur_term.c_cflag |= CS8;
cur_term.c_cc[VLNEXT] = VDISABLE;
+ cur_term.c_cc[VMIN] = 1;
+ cur_term.c_cc[VTIME] = 0;
tcsetattr(0, TCSADRAIN, &cur_term);
- // Clear the screen. This assumes VT100.
+ /* Clear the screen. This assumes VT100. */
write(1, "\33[H\33[J", 6);
- // Set up the poll structures
+ /* Set up the poll structures */
polls[0].fd = 0;
polls[0].events = POLLIN;
polls[0].revents = 0;
@@ -179,15 +185,15 @@ attach_main(int noerror)
polls[1].events = POLLIN;
polls[1].revents = 0;
- // Send our window size.
+ /* Send our window size. */
pkt.type = MSG_WINCH;
ioctl(0, TIOCGWINSZ, &pkt.u.ws);
write(s, &pkt, sizeof(pkt));
- // We would like a redraw, too.
+ /* We would like a redraw, too. */
pkt.type = MSG_REDRAW;
write(s, &pkt, sizeof(pkt));
- // Wait for things to happen
+ /* Wait for things to happen */
while (1)
{
if (poll(polls, 2, -1) < 0)
@@ -198,7 +204,7 @@ attach_main(int noerror)
exit(1);
}
}
- // Pty activity
+ /* Pty activity */
if (polls[1].revents != 0)
{
int len = read(s, buf, sizeof(buf));
@@ -214,10 +220,10 @@ attach_main(int noerror)
printf(EOS "\r\n[read returned an error]\r\n");
exit(1);
}
- // Send the data to the terminal.
+ /* Send the data to the terminal. */
write(1, buf, len);
}
- // stdin activity
+ /* stdin activity */
if (polls[0].revents != 0)
{
pkt.type = MSG_PUSH;
@@ -228,7 +234,7 @@ attach_main(int noerror)
exit(1);
process_kbd(s, &pkt);
}
- // Window size changed?
+ /* Window size changed? */
if (win_changed)
{
win_changed = 0;
@@ -237,7 +243,7 @@ attach_main(int noerror)
ioctl(0, TIOCGWINSZ, &pkt.u.ws);
write(s, &pkt, sizeof(pkt));
}
- // Want a redraw?
+ /* Want a redraw? */
if (want_redraw)
{
want_redraw = 0;