Cleanup error printing routines

Clean up so that only the following error related printing functions are
used: tio_error_printf(), tio_error_printf_silent(),
tio_warning_printf().

A session mode switch is introduced for error printing so that it will
print error messages with better formatting depending on in or out of
session.
This commit is contained in:
Martin Lund 2022-07-11 23:15:00 +02:00
parent 73a30a89ef
commit ac859f41b9
9 changed files with 122 additions and 77 deletions

View file

@ -58,7 +58,7 @@ static int get_match(const char *input, const char *pattern, char **match)
if (ret)
{
regerror(ret, &re, err, sizeof(err));
fprintf(stderr, "regex error: %s", err);
tio_error_printf("Regex failure: %s", err);
return ret;
}
@ -330,7 +330,7 @@ void config_file_parse(void)
c = malloc(sizeof(struct config_t));
if (!c)
{
fprintf(stderr, "Error: Insufficient memory allocation");
tio_error_printf("Insufficient memory allocation");
exit(EXIT_FAILURE);
}
memset(c, 0, sizeof(struct config_t));
@ -355,7 +355,7 @@ void config_file_parse(void)
ret = ini_parse(c->path, data_handler, NULL);
if (ret < 0)
{
fprintf(stderr, "Error: Unable to parse configuration file (%d)", ret);
tio_error_printf("Unable to parse configuration file (%d)", ret);
exit(EXIT_FAILURE);
}
free(c->section_name);
@ -368,7 +368,7 @@ void config_file_parse(void)
ret = ini_parse(c->path, section_name_search_handler, NULL);
if (!c->section_name)
{
debug_printf("Unable to match user input to configuration section (%d)", ret);
tio_debug_printf("Unable to match user input to configuration section (%d)", ret);
return;
}
}
@ -377,7 +377,7 @@ void config_file_parse(void)
ret = ini_parse(c->path, data_handler, NULL);
if (ret < 0)
{
fprintf(stderr, "Error: Unable to parse configuration file (%d)", ret);
tio_error_printf("Unable to parse configuration file (%d)", ret);
exit(EXIT_FAILURE);
}

View file

@ -19,27 +19,82 @@
* 02110-1301, USA.
*/
#define __STDC_WANT_LIB_EXT2__ 1 // To access vasprintf
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include "options.h"
#include "print.h"
#include "error.h"
char error[2][1000];
static char error[2][1000];
static bool in_session = false;
void error_enter_session_mode(void)
{
in_session = true;
}
void error_printf_(const char *format, ...)
{
va_list args;
char *line;
va_start(args, format);
vasprintf(&line, format, args);
if (in_session)
{
if (print_tainted)
{
putchar('\n');
}
ansi_error_printf("[%s] %s", current_time(), line);
}
else
{
fprintf(stderr, "%s\n", line);
}
va_end(args);
print_tainted = false;
free(line);
}
void tio_error_printf(const char *format, ...)
{
va_list args;
va_start(args, format);
vsnprintf(error[0], 1000, format, args);
va_end(args);
}
void tio_error_printf_silent(const char *format, ...)
{
va_list args;
va_start(args, format);
vsnprintf(error[1], 1000, format, args);
va_end(args);
}
void error_exit(void)
{
if (error[0][0] != 0)
{
/* Print error */
tio_error_printf("Error: %s", error[0]);
error_printf_("Error: %s", error[0]);
}
else if ((error[1][0] != 0) && (option.no_autoconnect))
{
/* Print silent error */
tio_error_printf("Error: %s", error[1]);
error_printf_("Error: %s", error[1]);
}
}

View file

@ -24,6 +24,7 @@
#define TIO_SUCCESS 0
#define TIO_ERROR 1
extern char error[2][1000];
void tio_error_printf(const char *format, ...);
void tio_error_printf_silent(const char *format, ...);
void error_exit(void);
void error_enter_session_mode(void);

View file

@ -188,7 +188,7 @@ void log_exit(void)
if (log_error)
{
error_printf("Could not open log file %s (%s)", option.log_filename, strerror(errno));
tio_error_printf("Could not open log file %s (%s)", option.log_filename, strerror(errno));
}
else if (option.log)
{

View file

@ -88,6 +88,9 @@ int main(int argc, char *argv[])
/* Initialize ANSI text formatting (colors etc.) */
print_init_ansi_formatting();
/* Change error printing mode */
error_enter_session_mode();
/* Print launch hints */
tio_printf("tio v%s", VERSION);
if (interactive_mode)

View file

@ -340,7 +340,7 @@ void options_parse(int argc, char *argv[])
option.color = string_to_long(optarg);
if ((option.color < 0) || (option.color > 255))
{
printf("Error: Invalid color code\n");
tio_error_printf("Invalid color code");
exit(EXIT_FAILURE);
}
break;
@ -382,17 +382,17 @@ void options_parse(int argc, char *argv[])
if (strlen(option.tty_device) == 0)
{
printf("Error: Missing tty device or sub-configuration name\n");
tio_error_printf("Missing tty device or sub-configuration name");
exit(EXIT_FAILURE);
}
/* Print any remaining command line arguments (unknown options) */
if (optind < argc)
{
printf("Error: Unknown argument ");
fprintf(stderr, "Error: Unknown argument ");
while (optind < argc)
printf("%s ", argv[optind++]);
printf("\n");
fprintf(stderr, "\n");
exit(EXIT_FAILURE);
}
}

View file

@ -56,7 +56,7 @@ extern char ansi_format[];
fprintf (stdout, "%s" format ANSI_RESET, ansi_format, ## args); \
}
#define warning_printf(format, args...) \
#define tio_warning_printf(format, args...) \
{ \
if (print_tainted) \
putchar('\n'); \
@ -74,28 +74,14 @@ extern char ansi_format[];
print_tainted = false; \
}
#define tio_error_printf(format, args...) \
{ \
if (print_tainted) \
putchar('\n'); \
ansi_error_printf("[%s] " format, current_time(), ## args); \
print_tainted = false; \
}
#define error_printf(format, args...) \
snprintf(error[0], 1000, format, ## args);
#define error_printf_silent(format, args...) \
snprintf(error[1], 1000, format, ## args);
#ifdef DEBUG
#define debug_printf(format, args...) \
#define tio_debug_printf(format, args...) \
fprintf (stdout, "[debug] " format, ## args)
#define debug_printf_raw(format, args...) \
#define tio_debug_printf_raw(format, args...) \
fprintf (stdout, "" format, ## args)
#else
#define debug_printf(format, args...)
#define debug_printf_raw(format, args...)
#define tio_debug_printf(format, args...)
#define tio_debug_printf_raw(format, args...)
#endif
void print_hex(char c);

View file

@ -94,13 +94,13 @@ void socket_configure(void)
if (strlen(socket_filename()) == 0)
{
error_printf("Missing socket filename");
tio_error_printf("Missing socket filename");
exit(EXIT_FAILURE);
}
if (strlen(socket_filename()) > sizeof(sockaddr_unix.sun_path) - 1)
{
error_printf("Socket file path %s too long", option.socket);
tio_error_printf("Socket file path %s too long", option.socket);
exit(EXIT_FAILURE);
}
}
@ -113,7 +113,7 @@ void socket_configure(void)
if (port_number < 0)
{
error_printf("Invalid port number: %d", port_number);
tio_error_printf("Invalid port number: %d", port_number);
exit(EXIT_FAILURE);
}
}
@ -126,14 +126,14 @@ void socket_configure(void)
if (port_number < 0)
{
error_printf("Invalid port number: %d", port_number);
tio_error_printf("Invalid port number: %d", port_number);
exit(EXIT_FAILURE);
}
}
if (socket_family == AF_UNSPEC)
{
error_printf("%s: Invalid socket scheme, must be prefixed with 'unix:', 'inet:', or 'inet6:'", option.socket);
tio_error_printf("%s: Invalid socket scheme, must be prefixed with 'unix:', 'inet:', or 'inet6:'", option.socket);
exit(EXIT_FAILURE);
}
@ -165,7 +165,7 @@ void socket_configure(void)
break;
default:
error_printf("Invalid socket family (%d)", socket_family);
tio_error_printf("Invalid socket family (%d)", socket_family);
exit(EXIT_FAILURE);
break;
}
@ -174,21 +174,21 @@ void socket_configure(void)
sockfd = socket(socket_family, SOCK_STREAM, 0);
if (sockfd < 0)
{
error_printf("Failed to create socket (%s)", strerror(errno));
tio_error_printf("Failed to create socket (%s)", strerror(errno));
exit(EXIT_FAILURE);
}
/* Bind */
if (bind(sockfd, sockaddr_p, socklen) < 0)
{
error_printf("Failed to bind to socket (%s)", strerror(errno));
tio_error_printf("Failed to bind to socket (%s)", strerror(errno));
exit(EXIT_FAILURE);
}
/* Listen */
if (listen(sockfd, MAX_SOCKET_CLIENTS) < 0)
{
error_printf("Failed to listen on socket (%s)", strerror(errno));
tio_error_printf("Failed to listen on socket (%s)", strerror(errno));
exit(EXIT_FAILURE);
}
@ -218,7 +218,7 @@ void socket_write(char input_char)
{
if (write(clientfds[i], &input_char, 1) <= 0)
{
error_printf_silent("Failed to write to socket (%s)", strerror(errno));
tio_error_printf_silent("Failed to write to socket (%s)", strerror(errno));
close(clientfds[i]);
clientfds[i] = -1;
}
@ -289,7 +289,7 @@ bool socket_handle_input(fd_set *rdfs, char *output_char)
}
if (status < 0)
{
error_printf_silent("Failed to read from socket (%s)", strerror(errno));
tio_error_printf_silent("Failed to read from socket (%s)", strerror(errno));
close(clientfds[i]);
clientfds[i] = -1;
continue;

View file

@ -132,7 +132,7 @@ void tty_flush(int fd)
if (count < 0)
{
// Error
debug_printf("Write error while flushing tty buffer (%s)", strerror(errno));
tio_debug_printf("Write error while flushing tty buffer (%s)", strerror(errno));
break;
}
tty_buffer_count -= count;
@ -167,7 +167,7 @@ ssize_t tty_write(int fd, const void *buffer, size_t count)
if (retval < 0)
{
// Error
debug_printf("Write error (%s)", strerror(errno));
tio_debug_printf("Write error (%s)", strerror(errno));
break;
}
bytes_written += retval;
@ -217,7 +217,7 @@ static void output_hex(char c)
ssize_t status = tty_write(fd, &hex_value, 1);
if (status < 0)
{
warning_printf("Could not write to tty device");
tio_warning_printf("Could not write to tty device");
}
else
{
@ -232,7 +232,7 @@ static void toggle_line(const char *line_name, int mask)
if (ioctl(fd, TIOCMGET, &state) < 0)
{
warning_printf("Could not get line state (%s)", strerror(errno));
tio_warning_printf("Could not get line state (%s)", strerror(errno));
}
else
{
@ -247,7 +247,7 @@ static void toggle_line(const char *line_name, int mask)
tio_printf("set %s to HIGH", line_name);
}
if (ioctl(fd, TIOCMSET, &state) < 0)
warning_printf("Could not set line state (%s)", strerror(errno));
tio_warning_printf("Could not set line state (%s)", strerror(errno));
}
}
@ -295,7 +295,7 @@ void handle_command_sequence(char input_char, char previous_char, char *output_c
case KEY_SHIFT_L:
if (ioctl(fd, TIOCMGET, &state) < 0)
{
warning_printf("Could not get line state (%s)", strerror(errno));
tio_warning_printf("Could not get line state (%s)", strerror(errno));
break;
}
tio_printf("Line states:");
@ -425,7 +425,7 @@ void stdin_configure(void)
/* Save current stdin settings */
if (tcgetattr(STDIN_FILENO, &stdin_old) < 0)
{
error_printf("Saving current stdin settings failed");
tio_error_printf("Saving current stdin settings failed");
exit(EXIT_FAILURE);
}
@ -443,7 +443,7 @@ void stdin_configure(void)
status = tcsetattr(STDIN_FILENO, TCSANOW, &stdin_new);
if (status == -1)
{
error_printf("Could not apply new stdin settings (%s)", strerror(errno));
tio_error_printf("Could not apply new stdin settings (%s)", strerror(errno));
exit(EXIT_FAILURE);
}
@ -467,7 +467,7 @@ void stdout_configure(void)
/* Save current stdout settings */
if (tcgetattr(STDOUT_FILENO, &stdout_old) < 0)
{
error_printf("Saving current stdio settings failed");
tio_error_printf("Saving current stdio settings failed");
exit(EXIT_FAILURE);
}
@ -485,7 +485,7 @@ void stdout_configure(void)
status = tcsetattr(STDOUT_FILENO, TCSANOW, &stdout_new);
if (status == -1)
{
error_printf("Could not apply new stdout settings (%s)", strerror(errno));
tio_error_printf("Could not apply new stdout settings (%s)", strerror(errno));
exit(EXIT_FAILURE);
}
@ -527,7 +527,7 @@ void tty_configure(void)
standard_baudrate = false;
break;
#else
error_printf("Invalid baud rate");
tio_error_printf("Invalid baud rate");
exit(EXIT_FAILURE);
#endif
}
@ -538,7 +538,7 @@ void tty_configure(void)
status = cfsetispeed(&tio, baudrate);
if (status == -1)
{
error_printf("Could not configure input speed (%s)", strerror(errno));
tio_error_printf("Could not configure input speed (%s)", strerror(errno));
exit(EXIT_FAILURE);
}
@ -546,7 +546,7 @@ void tty_configure(void)
status = cfsetospeed(&tio, baudrate);
if (status == -1)
{
error_printf("Could not configure output speed (%s)", strerror(errno));
tio_error_printf("Could not configure output speed (%s)", strerror(errno));
exit(EXIT_FAILURE);
}
}
@ -568,7 +568,7 @@ void tty_configure(void)
tio.c_cflag |= CS8;
break;
default:
error_printf("Invalid data bits");
tio_error_printf("Invalid data bits");
exit(EXIT_FAILURE);
}
@ -590,7 +590,7 @@ void tty_configure(void)
}
else
{
error_printf("Invalid flow control");
tio_error_printf("Invalid flow control");
exit(EXIT_FAILURE);
}
@ -604,7 +604,7 @@ void tty_configure(void)
tio.c_cflag |= CSTOPB;
break;
default:
error_printf("Invalid stop bits");
tio_error_printf("Invalid stop bits");
exit(EXIT_FAILURE);
}
@ -637,7 +637,7 @@ void tty_configure(void)
}
else
{
error_printf("Invalid parity");
tio_error_printf("Invalid parity");
exit(EXIT_FAILURE);
}
@ -754,7 +754,7 @@ void tty_wait_for_device(void)
status = read(STDIN_FILENO, &input_char, 1);
if (status <= 0)
{
error_printf("Could not read from stdin");
tio_error_printf("Could not read from stdin");
exit(EXIT_FAILURE);
}
@ -767,7 +767,7 @@ void tty_wait_for_device(void)
}
else if (status == -1)
{
error_printf("select() failed (%s)", strerror(errno));
tio_error_printf("select() failed (%s)", strerror(errno));
exit(EXIT_FAILURE);
}
@ -780,7 +780,7 @@ void tty_wait_for_device(void)
}
else if (last_errno != errno)
{
warning_printf("Could not open tty device (%s)", strerror(errno));
tio_warning_printf("Could not open tty device (%s)", strerror(errno));
tio_printf("Waiting for tty device..");
last_errno = errno;
}
@ -832,7 +832,7 @@ void forward_to_tty(int fd, char output_char)
status = tty_write(fd, crlf, 2);
if (status < 0)
{
warning_printf("Could not write to tty device");
tio_warning_printf("Could not write to tty device");
}
tx_total += 2;
@ -850,7 +850,7 @@ void forward_to_tty(int fd, char output_char)
status = tty_write(fd, &output_char, 1);
if (status < 0)
{
warning_printf("Could not write to tty device");
tio_warning_printf("Could not write to tty device");
}
/* Update transmit statistics */
@ -875,14 +875,14 @@ int tty_connect(void)
fd = open(option.tty_device, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd < 0)
{
error_printf_silent("Could not open tty device (%s)", strerror(errno));
tio_error_printf_silent("Could not open tty device (%s)", strerror(errno));
goto error_open;
}
/* Make sure device is of tty type */
if (!isatty(fd))
{
error_printf("Not a tty device");
tio_error_printf("Not a tty device");
exit(EXIT_FAILURE);;
}
@ -890,7 +890,7 @@ int tty_connect(void)
status = flock(fd, LOCK_EX | LOCK_NB);
if ((status == -1) && (errno == EWOULDBLOCK))
{
error_printf("Device file is locked by another process");
tio_error_printf("Device file is locked by another process");
exit(EXIT_FAILURE);
}
@ -945,7 +945,7 @@ int tty_connect(void)
status = tcsetattr(fd, TCSANOW, &tio);
if (status == -1)
{
error_printf_silent("Could not apply port settings (%s)", strerror(errno));
tio_error_printf_silent("Could not apply port settings (%s)", strerror(errno));
goto error_tcsetattr;
}
@ -954,7 +954,7 @@ int tty_connect(void)
{
if (setspeed2(fd, option.baudrate) != 0)
{
error_printf_silent("Could not set baudrate speed (%s)", strerror(errno));
tio_error_printf_silent("Could not set baudrate speed (%s)", strerror(errno));
goto error_setspeed;
}
}
@ -965,7 +965,7 @@ int tty_connect(void)
{
if (iossiospeed(fd, option.baudrate) != 0)
{
error_printf_silent("Could not set baudrate speed (%s)", strerror(errno));
tio_error_printf_silent("Could not set baudrate speed (%s)", strerror(errno));
goto error_setspeed;
}
}
@ -992,7 +992,7 @@ int tty_connect(void)
if (bytes_read <= 0)
{
/* Error reading - device is likely unplugged */
error_printf_silent("Could not read from tty device");
tio_error_printf_silent("Could not read from tty device");
goto error_read;
}
@ -1057,7 +1057,7 @@ int tty_connect(void)
ssize_t bytes_read = read(STDIN_FILENO, input_buffer, BUFSIZ);
if (bytes_read <= 0)
{
error_printf_silent("Could not read from stdin");
tio_error_printf_silent("Could not read from stdin");
goto error_read;
}
@ -1088,7 +1088,7 @@ int tty_connect(void)
{
if (!is_valid_hex(input_char))
{
warning_printf("Invalid hex character: '%d' (0x%02x)", input_char, input_char);
tio_warning_printf("Invalid hex character: '%d' (0x%02x)", input_char, input_char);
forward = false;
}
}
@ -1116,7 +1116,7 @@ int tty_connect(void)
}
else if (status == -1)
{
error_printf("Error: select() failed (%s)", strerror(errno));
tio_error_printf("select() failed (%s)", strerror(errno));
exit(EXIT_FAILURE);
}
}