Merge pull request #142 from sly74fr/issue/ini_timestamp_parsing

Fix timestamp parsing in INI conf
This commit is contained in:
Martin Lund 2022-03-31 14:05:36 +02:00 committed by GitHub
commit 11df828f68
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 109 additions and 40 deletions

View file

@ -122,7 +122,10 @@ Display program version.
.BR \-h ", " \-\-help
Display help.
.TP
.BR \-V ", " \-\-verbose
Enable verbose output.
.SH "KEYS"
.PP
.TP 16n

View file

@ -23,7 +23,8 @@ _tio()
-t --timestamp \
-L --list-devices \
-c --color \
-h --help"
-h --help" \
-V --verbose \
# Complete the arguments to the options.
case "${prev}" in
@ -88,6 +89,10 @@ _tio()
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
;;
-V | --verbose)
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
;;
*)
;;
esac

View file

@ -34,6 +34,7 @@
#include <unistd.h>
#include <regex.h>
#include <ini.h>
#include "options.h"
#include "configfile.h"
#include "misc.h"
#include "options.h"
@ -135,7 +136,7 @@ static int data_handler(void *user, const char *section, const char *name,
}
else if (!strcmp(name, "timestamp"))
{
option.timestamp = atoi(value);
option.timestamp = timestamp_option_parse(value);
}
else if (!strcmp(name, "log-filename"))
{
@ -260,3 +261,11 @@ void config_exit(void)
free(c);
}
void config_file_print()
{
tio_printf("INI:");
tio_printf(" path: %s", c->path);
tio_printf(" searched section: %s", c->user);
tio_printf(" found section: %s", c->section_name);
}

View file

@ -37,5 +37,6 @@ struct config_t
char *map;
};
void config_file_print();
void config_file_parse(const int argc, char *argv[]);
void config_exit(void);

View file

@ -77,6 +77,11 @@ int main(int argc, char *argv[])
/* Print launch hints */
tio_printf("tio v%s", VERSION);
tio_printf("Press ctrl-t q to quit");
if (option.debug)
{
config_file_print();
options_print();
}
/* Connect to tty device */
if (option.no_autoconnect)

View file

@ -33,6 +33,7 @@
#include "options.h"
#include "error.h"
#include "misc.h"
#include "print.h"
/* Default options */
struct option_t option =
@ -52,6 +53,7 @@ struct option_t option =
.log_filename = "",
.map = "",
.color = -1,
.debug = false,
};
void print_help(char *argv[])
@ -74,6 +76,7 @@ void print_help(char *argv[])
printf(" -c, --color <code> Colorize tio text\n");
printf(" -v, --version Display version\n");
printf(" -h, --help Display help\n");
printf(" -V, --verbose Enable verbose output\n");
printf("\n");
printf("See the man page for more details.\n");
printf("\n");
@ -81,6 +84,74 @@ void print_help(char *argv[])
printf("\n");
}
const char* timestamp_token(enum timestamp_t timestamp)
{
switch (timestamp)
{
case TIMESTAMP_NONE:
return "none";
break;
case TIMESTAMP_24HOUR:
return "24hour";
break;
case TIMESTAMP_24HOUR_START:
return "24hour-start";
break;
case TIMESTAMP_ISO8601:
return "iso8601";
break;
default:
return "unknown";
break;
}
}
enum timestamp_t timestamp_option_parse(const char *arg)
{
enum timestamp_t timestamp = TIMESTAMP_24HOUR; // Default
if (arg != NULL)
{
if (strcmp(arg, "24hour-start") == 0)
{
return TIMESTAMP_24HOUR_START;
}
else if (strcmp(arg, "iso8601") == 0)
{
return TIMESTAMP_ISO8601;
}
else
{
printf("Warning: Unknown timestamp type, falling back to '24hour' default format\n");
}
}
return timestamp;
}
void options_print()
{
tio_printf("Configuration:");
tio_printf(" TTY device: %s", option.tty_device);
tio_printf(" Baudrate: %u", option.baudrate);
tio_printf(" Databits: %d", option.databits);
tio_printf(" Flow: %s", option.flow);
tio_printf(" Stopbits: %d", option.stopbits);
tio_printf(" Parity: %s", option.parity);
tio_printf(" Local echo: %s", option.local_echo ? "enabled" : "disabled");
tio_printf(" Timestamps: %s", timestamp_token(option.timestamp));
tio_printf(" Output delay: %d", option.output_delay);
tio_printf(" Auto connect: %s", option.no_autoconnect ? "disabled" : "enabled");
if (option.map[0] != 0)
tio_printf(" Map flags: %s", option.map);
if (option.log)
tio_printf(" Log file: %s", option.log_filename);
}
void options_parse(int argc, char *argv[])
{
int c;
@ -110,6 +181,7 @@ void options_parse(int argc, char *argv[])
{"color", required_argument, 0, 'c'},
{"version", no_argument, 0, 'v'},
{"help", no_argument, 0, 'h'},
{"verbose", no_argument, 0, 'V'},
{0, 0, 0, 0 }
};
@ -117,7 +189,7 @@ void options_parse(int argc, char *argv[])
int option_index = 0;
/* Parse argument using getopt_long */
c = getopt_long(argc, argv, "b:d:f:s:p:o:net::Ll::m:c:vh", long_options, &option_index);
c = getopt_long(argc, argv, "b:d:f:s:p:o:net::Ll::m:c:vhV", long_options, &option_index);
/* Detect the end of the options */
if (c == -1)
@ -168,27 +240,7 @@ void options_parse(int argc, char *argv[])
break;
case 't':
option.timestamp = TIMESTAMP_24HOUR; // Default
if (optarg != NULL)
{
if (strcmp(optarg, "24hour") == 0)
{
option.timestamp = TIMESTAMP_24HOUR;
}
else if (strcmp(optarg, "24hour-start") == 0)
{
option.timestamp = TIMESTAMP_24HOUR_START;
}
else if (strcmp(optarg, "iso8601") == 0)
{
option.timestamp = TIMESTAMP_ISO8601;
}
else
{
printf("Error: Unknown timestamp type\n");
exit(EXIT_FAILURE);
}
}
option.timestamp = timestamp_option_parse(optarg);
break;
case 'L':
@ -238,6 +290,10 @@ void options_parse(int argc, char *argv[])
exit(EXIT_SUCCESS);
break;
case 'V':
option.debug = true;
break;
case '?':
/* getopt_long already printed an error message */
exit(EXIT_FAILURE);
@ -268,7 +324,7 @@ void options_parse(int argc, char *argv[])
/* Print any remaining command line arguments (unknown options) */
if (optind < argc)
{
printf("Error: unknown arguments: ");
printf("Error: Unknown argument ");
while (optind < argc)
printf("%s ", argv[optind++]);
printf("\n");

View file

@ -33,6 +33,8 @@ enum timestamp_t
TIMESTAMP_24HOUR_START,
TIMESTAMP_ISO8601,
};
const char* timestamp_token(enum timestamp_t timestamp);
enum timestamp_t timestamp_option_parse(const char *arg);
/* Options */
struct option_t
@ -52,8 +54,10 @@ struct option_t
const char *log_filename;
const char *map;
int color;
bool debug;
};
extern struct option_t option;
void options_print();
void options_parse(int argc, char *argv[]);

View file

@ -163,21 +163,7 @@ void handle_command_sequence(char input_char, char previous_char, char *output_c
break;
case KEY_C:
tio_printf("Configuration:");
tio_printf(" TTY device: %s", option.tty_device);
tio_printf(" Baudrate: %u", option.baudrate);
tio_printf(" Databits: %d", option.databits);
tio_printf(" Flow: %s", option.flow);
tio_printf(" Stopbits: %d", option.stopbits);
tio_printf(" Parity: %s", option.parity);
tio_printf(" Local echo: %s", option.local_echo ? "enabled" : "disabled");
tio_printf(" Timestamps: %s", option.timestamp ? "enabled" : "disabled");
tio_printf(" Output delay: %d", option.output_delay);
tio_printf(" Auto connect: %s", option.no_autoconnect ? "disabled" : "enabled");
if (option.map[0] != 0)
tio_printf(" Map flags: %s", option.map);
if (option.log)
tio_printf(" Log file: %s", option.log_filename);
options_print();
break;
case KEY_E: