Improved Connected / Disconnected output

This commit is contained in:
Martin Lund 2014-09-28 12:59:00 +02:00
parent 58071de753
commit 4b535a5c62
2 changed files with 20 additions and 10 deletions

View file

@ -32,8 +32,8 @@
#define ANSI_COLOR_WHITE "\x1b[1;37m" #define ANSI_COLOR_WHITE "\x1b[1;37m"
#define ANSI_COLOR_RESET "\x1b[0m" #define ANSI_COLOR_RESET "\x1b[0m"
#define gotty_printf(format, args...) \ #define color_printf(format, args...) \
fprintf (stdout, "\n\033[300D" ANSI_COLOR_YELLOW "[gotty] " format ANSI_COLOR_RESET "\033[300D\n", ## args); \ fprintf (stdout, "\033[300D" ANSI_COLOR_YELLOW format ANSI_COLOR_RESET "\033[300D\n", ## args); \
fflush(stdout); fflush(stdout);
#ifdef DEBUG #ifdef DEBUG

View file

@ -39,6 +39,7 @@
static int connected = false; static int connected = false;
struct termios stdio, old_stdio, old_tio; struct termios stdio, old_stdio, old_tio;
static int fd; static int fd;
static char c;
void configure_stdout(void) void configure_stdout(void)
{ {
@ -80,14 +81,18 @@ void restore_tty(void)
tcsetattr(fd, TCSAFLUSH, &old_tio); tcsetattr(fd, TCSAFLUSH, &old_tio);
if (connected) if (connected)
gotty_printf("Disconnected\n"); {
if (c == 0)
putchar('\n');
color_printf("[gotty] Disconnected");
}
} }
int connect_tty(void) int connect_tty(void)
{ {
fd_set rdfs; /* Read file descriptor set */ fd_set rdfs; /* Read file descriptor set */
int maxfd; /* Maximum file desciptor used */ int maxfd; /* Maximum file desciptor used */
char c, c0 = 0, c1 = 0; char c0 = 0, c1 = 0;
static bool first = true; static bool first = true;
int status; int status;
@ -106,8 +111,9 @@ int connect_tty(void)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
gotty_printf("Connected"); color_printf("[gotty] Connected");
connected = true; connected = true;
c = 0;
/* Save current port settings */ /* Save current port settings */
if (tcgetattr(fd, &old_tio) < 0) if (tcgetattr(fd, &old_tio) < 0)
@ -156,7 +162,11 @@ int connect_tty(void)
{ {
/* Error reading - device is likely unplugged */ /* Error reading - device is likely unplugged */
if (!option.no_autoconnect) if (!option.no_autoconnect)
gotty_printf("Disconnected"); {
if (c != 0)
putchar('\n');
color_printf("[gotty] Disconnected");
}
close(fd); close(fd);
connected = false; connected = false;
return EXIT_FAILURE; return EXIT_FAILURE;
@ -171,10 +181,10 @@ int connect_tty(void)
c1 = c0; c1 = c0;
c0 = c; c0 = c;
if ((c0 == CTRLQ) && (c1 == CTRLG)) if ((c0 == CTRLQ) && (c1 == CTRLG))
{ {
close(fd); close(fd);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
/* Forward input to tty device */ /* Forward input to tty device */
status = write(fd, &c, 1); status = write(fd, &c, 1);