diff --git a/README.md b/README.md index c9652e1..eca1e2b 100644 --- a/README.md +++ b/README.md @@ -30,22 +30,24 @@ The command-line interface is straightforward as reflected in the output from Usage: tio [] Options: - -b, --baudrate Baud rate (default: 115200) - -d, --databits 5|6|7|8 Data bits (default: 8) - -f, --flow hard|soft|none Flow control (default: none) - -s, --stopbits 1|2 Stop bits (default: 1) - -p, --parity odd|even|none Parity (default: none) - -o, --output-delay Character output delay (default: 0) - -n, --no-autoconnect Disable automatic connect - -e, --local-echo Enable local echo - -t, --timestamp[=] Enable timestamp (default: 24hour) - -L, --list-devices List available serial devices - -l, --log[=] Log to file - -m, --map Map special characters - -c, --color <0..255> Colorize tio text - -S, --socket Listen on socket - -v, --version Display version - -h, --help Display help + -b, --baudrate Baud rate (default: 115200) + -d, --databits 5|6|7|8 Data bits (default: 8) + -f, --flow hard|soft|none Flow control (default: none) + -s, --stopbits 1|2 Stop bits (default: 1) + -p, --parity odd|even|none Parity (default: none) + -o, --output-delay Character output delay (default: 0) + -n, --no-autoconnect Disable automatic connect + -e, --local-echo Enable local echo + -t, --timestamp Enable line timestamp + --timestamp-format Set timestamp format (default: 24hour) + -L, --list-devices List available serial devices + -l, --log Enable log to file + --log-filename Set log filename + -m, --map Map special characters + -c, --color <0..255> Colorize tio text + -S, --socket Listen on socket + -v, --version Display version + -h, --help Display help Options may be set via configuration file. @@ -95,13 +97,13 @@ color = 46 [ftdi] tty = /dev/serial/by-id/usb-FTDI_TTL232R-3V3_FTGQVXBL-if00-port0 baudrate = 9600 -no-autoconnect = 1 +no-autoconnect = enable color = 12 [usb devices] pattern=usb([0-9]*) tty = /dev/ttyUSB%s -log = 1 +log = enable log-filename = usb.log color = 13 ``` diff --git a/TODO b/TODO new file mode 100644 index 0000000..563ceea --- /dev/null +++ b/TODO @@ -0,0 +1,3 @@ + + * Improve error/warning messaging when parsing configuration file + diff --git a/man/tio.1.in b/man/tio.1.in index 20ec02a..5668ccd 100644 --- a/man/tio.1.in +++ b/man/tio.1.in @@ -57,9 +57,14 @@ option is provided, tio will exit if the device is not present or an established Enable local echo. .TP -.BR \-t ", " \-\-timestamp[=\fI\fR\fB] +.BR \-t ", " \-\-timestamp -Enable timestamp. Optionally you can specify any of the following timestamp formats: +Enable line timestamp. + +.TP +.BR " \-\-timestamp-format \fI + +Set timestamp format to any of the following timestamp formats: .RS .TP 16n .IP "\fB24hour" @@ -79,9 +84,14 @@ Default format is List available serial devices. .TP -.BR \-l ", " \-\-log[=\fI\fR\fB] +.BR \-l ", " \-\-log -Log to file. If no filename is provided the filename will be automatically generated. +Enable log to file. If no filename is provided the filename will be automatically generated. + +.TP +.BR " \-\-log-filename \fI + +Set log filename. .TP .BR \-m ", " "\-\-map " \fI @@ -188,6 +198,7 @@ Options without any config section name sets the default options. .TP The following configuration file options are available: +.TP 20n .IP "\fBpattern" pattern matching user input. This pattern can be an extended regular expression with a single group. .IP "\fBtty" @@ -207,13 +218,15 @@ Set output delay .IP "\fBno-autoconnect" Disable automatic connect .IP "\fBlog" -Log to file +Enable log to file .IP "\fBlog-filename" Set log filename .IP "\fBlocal-echo" Enable local echo .IP "\fBtimestamp" -Prefix each new line with a timestamp +Enable line timestamp +.IP "\fBtimestamp-format" +Set timestamp format .IP "\fBmap" Map special characters on input or output .IP "\fBcolor" diff --git a/src/configfile.c b/src/configfile.c index a158972..01cc2c8 100644 --- a/src/configfile.c +++ b/src/configfile.c @@ -144,6 +144,13 @@ static int data_handler(void *user, const char *section, const char *name, } } else if (!strcmp(name, "timestamp")) + { + if (!strcmp(value, "enable")) + { + option.timestamp = TIMESTAMP_24HOUR; + } + } + else if (!strcmp(name, "timestamp-format")) { option.timestamp = timestamp_option_parse(value); } diff --git a/src/options.c b/src/options.c index e02ee52..543509f 100644 --- a/src/options.c +++ b/src/options.c @@ -35,6 +35,13 @@ #include "misc.h" #include "print.h" +enum opt_t +{ + OPT_NONE, + OPT_TIMESTAMP_FORMAT, + OPT_LOG_FILENAME, +}; + /* Default options */ struct option_t option = { @@ -61,22 +68,24 @@ void print_help(char *argv[]) printf("Usage: %s [] \n", argv[0]); printf("\n"); printf("Options:\n"); - printf(" -b, --baudrate Baud rate (default: 115200)\n"); - printf(" -d, --databits 5|6|7|8 Data bits (default: 8)\n"); - printf(" -f, --flow hard|soft|none Flow control (default: none)\n"); - printf(" -s, --stopbits 1|2 Stop bits (default: 1)\n"); - printf(" -p, --parity odd|even|none Parity (default: none)\n"); - printf(" -o, --output-delay Output delay (default: 0)\n"); - printf(" -n, --no-autoconnect Disable automatic connect\n"); - printf(" -e, --local-echo Enable local echo\n"); - printf(" -t, --timestamp[=] Enable timestamp (default: 24hour)\n"); - printf(" -L, --list-devices List available serial devices\n"); - printf(" -l, --log[=] Log to file\n"); - printf(" -m, --map Map special characters\n"); - printf(" -c, --color Colorize tio text\n"); - printf(" -S, --socket Listen on socket\n"); - printf(" -v, --version Display version\n"); - printf(" -h, --help Display help\n"); + printf(" -b, --baudrate Baud rate (default: 115200)\n"); + printf(" -d, --databits 5|6|7|8 Data bits (default: 8)\n"); + printf(" -f, --flow hard|soft|none Flow control (default: none)\n"); + printf(" -s, --stopbits 1|2 Stop bits (default: 1)\n"); + printf(" -p, --parity odd|even|none Parity (default: none)\n"); + printf(" -o, --output-delay Output delay (default: 0)\n"); + printf(" -n, --no-autoconnect Disable automatic connect\n"); + printf(" -e, --local-echo Enable local echo\n"); + printf(" -t, --timestamp Enable line timestamp\n"); + printf(" --timestamp-format Set timestamp format (default: 24hour)\n"); + printf(" -L, --list-devices List available serial devices\n"); + printf(" -l, --log Enable log to file\n"); + printf(" --log-filename Set log filename\n"); + printf(" -m, --map Map special characters\n"); + printf(" -c, --color Colorize tio text\n"); + printf(" -S, --socket Listen on socket\n"); + printf(" -v, --version Display version\n"); + printf(" -h, --help Display help\n"); printf("\n"); printf("Options may be set via configuration file.\n"); printf("\n"); @@ -129,7 +138,7 @@ enum timestamp_t timestamp_option_parse(const char *arg) } else { - printf("Warning: Unknown timestamp type, falling back to '24hour' default format\n"); + warning_printf("Unknown timestamp type, falling back to '24hour' default format"); } } @@ -170,30 +179,32 @@ void options_parse(int argc, char *argv[]) { static struct option long_options[] = { - {"baudrate", required_argument, 0, 'b'}, - {"databits", required_argument, 0, 'd'}, - {"flow", required_argument, 0, 'f'}, - {"stopbits", required_argument, 0, 's'}, - {"parity", required_argument, 0, 'p'}, - {"output-delay", required_argument, 0, 'o'}, - {"no-autoconnect", no_argument, 0, 'n'}, - {"local-echo", no_argument, 0, 'e'}, - {"timestamp", optional_argument, 0, 't'}, - {"list-devices", no_argument, 0, 'L'}, - {"log", optional_argument, 0, 'l'}, - {"socket", required_argument, 0, 'S'}, - {"map", required_argument, 0, 'm'}, - {"color", required_argument, 0, 'c'}, - {"version", no_argument, 0, 'v'}, - {"help", no_argument, 0, 'h'}, - {0, 0, 0, 0 } + {"baudrate", required_argument, 0, 'b' }, + {"databits", required_argument, 0, 'd' }, + {"flow", required_argument, 0, 'f' }, + {"stopbits", required_argument, 0, 's' }, + {"parity", required_argument, 0, 'p' }, + {"output-delay", required_argument, 0, 'o' }, + {"no-autoconnect", no_argument, 0, 'n' }, + {"local-echo", no_argument, 0, 'e' }, + {"timestamp", no_argument, 0, 't' }, + {"timestamp-format", required_argument, 0, OPT_TIMESTAMP_FORMAT }, + {"list-devices", no_argument, 0, 'L' }, + {"log", no_argument, 0, 'l' }, + {"log-filename", required_argument, 0, OPT_LOG_FILENAME }, + {"socket", required_argument, 0, 'S' }, + {"map", required_argument, 0, 'm' }, + {"color", required_argument, 0, 'c' }, + {"version", no_argument, 0, 'v' }, + {"help", no_argument, 0, 'h' }, + {0, 0, 0, 0 } }; /* getopt_long stores the option index here */ int option_index = 0; /* Parse argument using getopt_long */ - c = getopt_long(argc, argv, "b:d:f:s:p:o:net::Ll::S:m:c:vh", long_options, &option_index); + c = getopt_long(argc, argv, "b:d:f:s:p:o:netLlS:m:c:vh", long_options, &option_index); /* Detect the end of the options */ if (c == -1) @@ -244,6 +255,10 @@ void options_parse(int argc, char *argv[]) break; case 't': + option.timestamp = TIMESTAMP_24HOUR; + break; + + case OPT_TIMESTAMP_FORMAT: option.timestamp = timestamp_option_parse(optarg); break; @@ -253,6 +268,9 @@ void options_parse(int argc, char *argv[]) case 'l': option.log = true; + break; + + case OPT_LOG_FILENAME: option.log_filename = optarg; break; diff --git a/src/options.h b/src/options.h index 98bec82..27e9d0c 100644 --- a/src/options.h +++ b/src/options.h @@ -33,6 +33,7 @@ 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);