mirror of
https://github.com/tio/tio.git
synced 2026-05-01 14:57:59 +02:00
Factorize timestamp parsing to be coherent with command line format in configuration file.
This commit is contained in:
parent
1ec39d1c7b
commit
a543fbd7ef
6 changed files with 99 additions and 39 deletions
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
101
src/options.c
101
src/options.c
|
|
@ -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[])
|
||||
|
|
@ -81,6 +83,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 +180,7 @@ void options_parse(int argc, char *argv[])
|
|||
{"color", required_argument, 0, 'c'},
|
||||
{"version", no_argument, 0, 'v'},
|
||||
{"help", no_argument, 0, 'h'},
|
||||
{"V", no_argument, 0, 'V'},
|
||||
{0, 0, 0, 0 }
|
||||
};
|
||||
|
||||
|
|
@ -117,7 +188,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 +239,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 +289,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 +323,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");
|
||||
|
|
|
|||
|
|
@ -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[]);
|
||||
|
|
|
|||
16
src/tty.c
16
src/tty.c
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue