mirror of
https://github.com/tio/tio.git
synced 2026-05-01 14:57:59 +02:00
Cleanup of error handling
Introduced consistent way of handling errors and printing error messages. Also upgraded some warnings to errors.
This commit is contained in:
parent
4e2bcde010
commit
ad551b363e
6 changed files with 106 additions and 81 deletions
|
|
@ -25,11 +25,12 @@
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include "tio/options.h"
|
#include "tio/options.h"
|
||||||
#include "tio/print.h"
|
#include "tio/print.h"
|
||||||
|
#include "tio/error.h"
|
||||||
|
|
||||||
char *error = "";
|
char error[1000];
|
||||||
|
|
||||||
void error_exit(void)
|
void error_exit(void)
|
||||||
{
|
{
|
||||||
if ((error[0] != 0) && (option.no_autoconnect))
|
if ((error[0] != 0) && (option.no_autoconnect))
|
||||||
printf("Error: %s\n", error);
|
printf("\rError: %s\r\n", error);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,10 @@
|
||||||
#define TIO_SUCCESS 0
|
#define TIO_SUCCESS 0
|
||||||
#define TIO_ERROR 1
|
#define TIO_ERROR 1
|
||||||
|
|
||||||
extern char *error;
|
extern char error[1000];
|
||||||
|
|
||||||
|
#define error_printf(format, args...) \
|
||||||
|
snprintf (error, 1000, format, ## args);
|
||||||
|
|
||||||
void error_exit(void);
|
void error_exit(void);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,21 +32,18 @@ int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int status;
|
int status;
|
||||||
|
|
||||||
|
/* Install error exit handler */
|
||||||
|
atexit(&error_exit);
|
||||||
|
|
||||||
/* Parse options */
|
/* Parse options */
|
||||||
parse_options(argc, argv);
|
parse_options(argc, argv);
|
||||||
|
|
||||||
/* Configure output terminal */
|
/* Configure output terminal */
|
||||||
configure_stdout();
|
configure_stdout();
|
||||||
|
|
||||||
/* Install error exit handler */
|
|
||||||
atexit(&error_exit);
|
|
||||||
|
|
||||||
/* Install log exit handler */
|
/* Install log exit handler */
|
||||||
atexit(&log_exit);
|
atexit(&log_exit);
|
||||||
|
|
||||||
/* Restore output terminal on exit */
|
|
||||||
atexit(&restore_stdout);
|
|
||||||
|
|
||||||
/* Create log file */
|
/* Create log file */
|
||||||
if (option.log)
|
if (option.log)
|
||||||
log_open(option.log_filename);
|
log_open(option.log_filename);
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "tio/options.h"
|
#include "tio/options.h"
|
||||||
#include "tio/print.h"
|
#include "tio/error.h"
|
||||||
|
|
||||||
struct option_t option =
|
struct option_t option =
|
||||||
{
|
{
|
||||||
|
|
@ -71,7 +71,7 @@ void parse_options(int argc, char *argv[])
|
||||||
if (argc == 1)
|
if (argc == 1)
|
||||||
{
|
{
|
||||||
print_options_help(argv);
|
print_options_help(argv);
|
||||||
exit(0);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Set default termios settings:
|
/* Set default termios settings:
|
||||||
|
|
@ -213,7 +213,7 @@ void parse_options(int argc, char *argv[])
|
||||||
baudrate = B4000000;
|
baudrate = B4000000;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
printf("Error: Invalid baud rate.\n");
|
error_printf("Invalid baud rate");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -240,7 +240,7 @@ void parse_options(int argc, char *argv[])
|
||||||
option.tio.c_cflag |= CS8;
|
option.tio.c_cflag |= CS8;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
printf("Error: Invalid data bits.\n");
|
error_printf("Invalid data bits");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
@ -263,7 +263,7 @@ void parse_options(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("Error: Invalid flow control.\n");
|
error_printf("Invalid flow control");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
@ -279,7 +279,7 @@ void parse_options(int argc, char *argv[])
|
||||||
option.tio.c_cflag |= CSTOPB;
|
option.tio.c_cflag |= CSTOPB;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
printf("Error: Invalid stop bits.\n");
|
error_printf("Invalid stop bits");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
@ -299,7 +299,7 @@ void parse_options(int argc, char *argv[])
|
||||||
option.tio.c_cflag &= ~PARENB;
|
option.tio.c_cflag &= ~PARENB;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("Error: Invalid parity.\n");
|
error_printf("Invalid parity");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
@ -324,21 +324,21 @@ void parse_options(int argc, char *argv[])
|
||||||
printf("License GPLv2: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>.\n");
|
printf("License GPLv2: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>.\n");
|
||||||
printf("This is free software: you are free to change and redistribute it.\n");
|
printf("This is free software: you are free to change and redistribute it.\n");
|
||||||
printf("There is NO WARRANTY, to the extent permitted by law.\n");
|
printf("There is NO WARRANTY, to the extent permitted by law.\n");
|
||||||
exit(0);
|
exit(EXIT_SUCCESS);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'h':
|
case 'h':
|
||||||
print_options_help(argv);
|
print_options_help(argv);
|
||||||
exit(0);
|
exit(EXIT_SUCCESS);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '?':
|
case '?':
|
||||||
/* getopt_long already printed an error message */
|
/* getopt_long already printed an error message */
|
||||||
exit(1);
|
exit(EXIT_FAILURE);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
exit(1);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -348,14 +348,14 @@ void parse_options(int argc, char *argv[])
|
||||||
|
|
||||||
if (strlen(option.tty_device) == 0)
|
if (strlen(option.tty_device) == 0)
|
||||||
{
|
{
|
||||||
printf("Error: Missing device name.\n");
|
error_printf("Missing device name");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Print any remaining command line arguments (unknown options) */
|
/* Print any remaining command line arguments (unknown options) */
|
||||||
if (optind < argc)
|
if (optind < argc)
|
||||||
{
|
{
|
||||||
printf("%s: unknown arguments: ", argv[0]);
|
printf("Error: unknown arguments: ");
|
||||||
while (optind < argc)
|
while (optind < argc)
|
||||||
printf("%s ", argv[optind++]);
|
printf("%s ", argv[optind++]);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include "tio/error.h"
|
||||||
|
|
||||||
char * current_time(void)
|
char * current_time(void)
|
||||||
{
|
{
|
||||||
|
|
@ -33,7 +34,7 @@ char * current_time(void)
|
||||||
tmp = localtime(&t);
|
tmp = localtime(&t);
|
||||||
if (tmp == NULL)
|
if (tmp == NULL)
|
||||||
{
|
{
|
||||||
printf("Error: Retrieving local time failed\n");
|
error_printf("Retrieving local time failed");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
139
src/tty.c
139
src/tty.c
|
|
@ -46,7 +46,7 @@ static int fd;
|
||||||
void wait_for_tty_device(void)
|
void wait_for_tty_device(void)
|
||||||
{
|
{
|
||||||
fd_set rdfs;
|
fd_set rdfs;
|
||||||
int ready, status;
|
int status;
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
static char c_stdin[3];
|
static char c_stdin[3];
|
||||||
static bool first = true;
|
static bool first = true;
|
||||||
|
|
@ -71,21 +71,29 @@ void wait_for_tty_device(void)
|
||||||
FD_SET(STDIN_FILENO, &rdfs);
|
FD_SET(STDIN_FILENO, &rdfs);
|
||||||
|
|
||||||
/* Block until input becomes available or timeout */
|
/* Block until input becomes available or timeout */
|
||||||
ready = select(STDIN_FILENO + 1, &rdfs, NULL, NULL, &tv);
|
status = select(STDIN_FILENO + 1, &rdfs, NULL, NULL, &tv);
|
||||||
if (ready)
|
if (status)
|
||||||
{
|
{
|
||||||
/* Input from stdin ready */
|
/* Input from stdin ready */
|
||||||
|
|
||||||
/* Read one character */
|
/* Read one character */
|
||||||
status = read(STDIN_FILENO, &c_stdin[0], 1);
|
status = read(STDIN_FILENO, &c_stdin[0], 1);
|
||||||
if (status < 0)
|
if (status < 0)
|
||||||
printf("Warning: Could not read from stdin\r\n");
|
{
|
||||||
|
error_printf("Could not read from stdin");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
/* Exit upon ctrl-t + q sequence */
|
/* Exit upon ctrl-t + q sequence */
|
||||||
c_stdin[2] = c_stdin[1];
|
c_stdin[2] = c_stdin[1];
|
||||||
c_stdin[1] = c_stdin[0];
|
c_stdin[1] = c_stdin[0];
|
||||||
if ((c_stdin[1] == KEY_Q) && (c_stdin[2] == KEY_CTRL_T))
|
if ((c_stdin[1] == KEY_Q) && (c_stdin[2] == KEY_CTRL_T))
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
|
|
||||||
|
} else if (status == -1)
|
||||||
|
{
|
||||||
|
error_printf("select() failed (%s)", strerror(errno));
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Test for accessible device file */
|
/* Test for accessible device file */
|
||||||
|
|
@ -99,7 +107,7 @@ void configure_stdout(void)
|
||||||
/* Save current stdout settings */
|
/* Save current stdout settings */
|
||||||
if (tcgetattr(STDOUT_FILENO, &old_stdout) < 0)
|
if (tcgetattr(STDOUT_FILENO, &old_stdout) < 0)
|
||||||
{
|
{
|
||||||
printf("Error: Saving current stdio settings failed\n");
|
error_printf("Saving current stdio settings failed");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -119,11 +127,13 @@ void configure_stdout(void)
|
||||||
/* Activate new stdout settings */
|
/* Activate new stdout settings */
|
||||||
tcsetattr(STDOUT_FILENO, TCSANOW, &new_stdout);
|
tcsetattr(STDOUT_FILENO, TCSANOW, &new_stdout);
|
||||||
tcsetattr(STDOUT_FILENO, TCSAFLUSH, &new_stdout);
|
tcsetattr(STDOUT_FILENO, TCSAFLUSH, &new_stdout);
|
||||||
|
|
||||||
|
/* Make sure we restore old stdout settings on exit */
|
||||||
|
atexit(&restore_stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
void restore_stdout(void)
|
void restore_stdout(void)
|
||||||
{
|
{
|
||||||
tcflush(STDOUT_FILENO, TCIOFLUSH);
|
|
||||||
tcsetattr(STDOUT_FILENO, TCSANOW, &old_stdout);
|
tcsetattr(STDOUT_FILENO, TCSANOW, &old_stdout);
|
||||||
tcsetattr(STDOUT_FILENO, TCSAFLUSH, &old_stdout);
|
tcsetattr(STDOUT_FILENO, TCSAFLUSH, &old_stdout);
|
||||||
}
|
}
|
||||||
|
|
@ -132,9 +142,13 @@ void disconnect_tty(void)
|
||||||
{
|
{
|
||||||
if (tainted)
|
if (tainted)
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
color_printf("[tio %s] Disconnected", current_time());
|
|
||||||
close(fd);
|
if (connected)
|
||||||
connected = false;
|
{
|
||||||
|
color_printf("[tio %s] Disconnected", current_time());
|
||||||
|
close(fd);
|
||||||
|
connected = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void restore_tty(void)
|
void restore_tty(void)
|
||||||
|
|
@ -159,14 +173,14 @@ int connect_tty(void)
|
||||||
fd = open(option.tty_device, O_RDWR | O_NOCTTY );
|
fd = open(option.tty_device, O_RDWR | O_NOCTTY );
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
{
|
{
|
||||||
error = strerror(errno);
|
error_printf("Could not open tty device (%s)", strerror(errno));
|
||||||
goto error_open;
|
goto error_open;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Make sure device is of tty type */
|
/* Make sure device is of tty type */
|
||||||
if (!isatty(fd))
|
if (!isatty(fd))
|
||||||
{
|
{
|
||||||
error = "Not a tty device";
|
error_printf("Not a tty device");
|
||||||
goto error_isatty;
|
goto error_isatty;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -214,58 +228,68 @@ int connect_tty(void)
|
||||||
FD_SET(STDIN_FILENO, &rdfs);
|
FD_SET(STDIN_FILENO, &rdfs);
|
||||||
|
|
||||||
/* Block until input becomes available */
|
/* Block until input becomes available */
|
||||||
select(maxfd, &rdfs, NULL, NULL, NULL);
|
status = select(maxfd, &rdfs, NULL, NULL, NULL);
|
||||||
if (FD_ISSET(fd, &rdfs))
|
if (status)
|
||||||
{
|
{
|
||||||
/* Input from tty device ready */
|
if (FD_ISSET(fd, &rdfs))
|
||||||
if (read(fd, &c_tty, 1) > 0)
|
|
||||||
{
|
{
|
||||||
/* Print received tty character to stdout */
|
/* Input from tty device ready */
|
||||||
putchar(c_tty);
|
if (read(fd, &c_tty, 1) > 0)
|
||||||
fflush(stdout);
|
{
|
||||||
|
/* Print received tty character to stdout */
|
||||||
|
putchar(c_tty);
|
||||||
|
fflush(stdout);
|
||||||
|
|
||||||
|
/* Write to log */
|
||||||
|
if (option.log)
|
||||||
|
log_write(c_tty);
|
||||||
|
|
||||||
|
tainted = true;
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
/* Error reading - device is likely unplugged */
|
||||||
|
error_printf("Could not read from tty device");
|
||||||
|
goto error_read;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (FD_ISSET(STDIN_FILENO, &rdfs))
|
||||||
|
{
|
||||||
|
/* Input from stdin ready */
|
||||||
|
status = read(STDIN_FILENO, &c_stdin[0], 1);
|
||||||
|
if (status < 0)
|
||||||
|
{
|
||||||
|
error_printf("Could not read from stdin");
|
||||||
|
goto error_read;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Exit upon ctrl-t + q sequence */
|
||||||
|
c_stdin[2] = c_stdin[1];
|
||||||
|
c_stdin[1] = c_stdin[0];
|
||||||
|
if ((c_stdin[1] == KEY_Q) && (c_stdin[2] == KEY_CTRL_T))
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
|
||||||
|
/* Ignore ctrl-t except when repeated */
|
||||||
|
if ((c_stdin[0] != KEY_CTRL_T) ||
|
||||||
|
((c_stdin[1] == KEY_CTRL_T) && (c_stdin[2] == KEY_CTRL_T)))
|
||||||
|
{
|
||||||
|
/* Forward input to tty device */
|
||||||
|
status = write(fd, &c_stdin[0], 1);
|
||||||
|
if (status < 0)
|
||||||
|
printf("Warning: Could not write to tty device\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
/* Write to log */
|
/* Write to log */
|
||||||
if (option.log)
|
if (option.log)
|
||||||
log_write(c_tty);
|
log_write(c_stdin[0]);
|
||||||
|
|
||||||
tainted = true;
|
/* Insert output delay */
|
||||||
} else
|
if (option.output_delay)
|
||||||
{
|
usleep(option.output_delay * 1000);
|
||||||
/* Error reading - device is likely unplugged */
|
|
||||||
disconnect_tty();
|
|
||||||
goto error_reading;
|
|
||||||
}
|
}
|
||||||
}
|
} else if (status == -1)
|
||||||
if (FD_ISSET(STDIN_FILENO, &rdfs))
|
|
||||||
{
|
{
|
||||||
/* Input from stdin ready */
|
error_printf("Error: select() failed (%s)", strerror(errno));
|
||||||
status = read(STDIN_FILENO, &c_stdin[0], 1);
|
exit(EXIT_FAILURE);
|
||||||
if (status < 0)
|
|
||||||
printf("Warning: Could not read from stdin\r\n");
|
|
||||||
|
|
||||||
/* Exit upon ctrl-t + q sequence */
|
|
||||||
c_stdin[2] = c_stdin[1];
|
|
||||||
c_stdin[1] = c_stdin[0];
|
|
||||||
if ((c_stdin[1] == KEY_Q) && (c_stdin[2] == KEY_CTRL_T))
|
|
||||||
exit(EXIT_SUCCESS);
|
|
||||||
|
|
||||||
/* Ignore ctrl-t except when repeated */
|
|
||||||
if ((c_stdin[0] != KEY_CTRL_T) ||
|
|
||||||
((c_stdin[1] == KEY_CTRL_T) && (c_stdin[2] == KEY_CTRL_T)))
|
|
||||||
{
|
|
||||||
/* Forward input to tty device */
|
|
||||||
status = write(fd, &c_stdin[0], 1);
|
|
||||||
if (status < 0)
|
|
||||||
printf("Warning: Could not write to tty device\r\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Write to log */
|
|
||||||
if (option.log)
|
|
||||||
log_write(c_stdin[0]);
|
|
||||||
|
|
||||||
/* Insert output delay */
|
|
||||||
if (option.output_delay)
|
|
||||||
usleep(option.output_delay * 1000);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -273,9 +297,8 @@ int connect_tty(void)
|
||||||
|
|
||||||
error_tcgetattr:
|
error_tcgetattr:
|
||||||
error_isatty:
|
error_isatty:
|
||||||
close(fd);
|
error_read:
|
||||||
connected = false;
|
disconnect_tty();
|
||||||
error_reading:
|
|
||||||
error_open:
|
error_open:
|
||||||
return TIO_ERROR;
|
return TIO_ERROR;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue