Mostly cosmetic updates

This commit is contained in:
Martin Lund 2022-03-11 15:01:40 +01:00
parent ea0dd3e602
commit a2b164519f
8 changed files with 130 additions and 86 deletions

View file

@ -1,6 +1,6 @@
option('bashcompletiondir', option('bashcompletiondir',
type : 'string', type : 'string',
description : 'Directory for bash completion scripts ["no" disables]') description : 'Directory for bash completion scripts ["no" disables]')
option('conffile', option('configfile',
type: 'boolean', value: true, type: 'boolean', value: true,
description: 'Enable configuration file support') description: 'Enable configuration file support')

View file

@ -1,8 +1,8 @@
// vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4:
/* /*
* tio - a simple TTY terminal I/O application * tio - a simple serial terminal I/O application
* *
* Copyright (c) 2020-2022 Liam Beguin * Copyright (c) 2020-2022 Liam Beguin
* Copyright (c) 2022 Martin Lund
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
@ -34,13 +34,13 @@
#include <unistd.h> #include <unistd.h>
#include <regex.h> #include <regex.h>
#include <ini.h> #include <ini.h>
#include "conffile.h" #include "configfile.h"
#include "misc.h" #include "misc.h"
#include "options.h" #include "options.h"
#include "error.h" #include "error.h"
#include "print.h" #include "print.h"
static struct conf_data *d; static struct config_t *c;
static int get_match(const char *input, const char *pattern, char **match) static int get_match(const char *input, const char *pattern, char **match)
{ {
@ -52,7 +52,8 @@ static int get_match(const char *input, const char *pattern, char **match)
/* compile a regex with the pattern */ /* compile a regex with the pattern */
ret = regcomp(&re, pattern, REG_EXTENDED); ret = regcomp(&re, pattern, REG_EXTENDED);
if (ret) { if (ret)
{
regerror(ret, &re, err, sizeof(err)); regerror(ret, &re, err, sizeof(err));
printf("reg error: %s\n", err); printf("reg error: %s\n", err);
return ret; return ret;
@ -61,12 +62,16 @@ static int get_match(const char *input, const char *pattern, char **match)
/* try to match on input */ /* try to match on input */
ret = regexec(&re, input, 2, m, 0); ret = regexec(&re, input, 2, m, 0);
if (!ret) if (!ret)
{
len = m[1].rm_eo - m[1].rm_so; len = m[1].rm_eo - m[1].rm_so;
}
regfree(&re); regfree(&re);
if (len) if (len)
{
asprintf(match, "%s", &input[m[1].rm_so]); asprintf(match, "%s", &input[m[1].rm_so]);
}
return len; return len;
} }
@ -79,42 +84,67 @@ static int get_match(const char *input, const char *pattern, char **match)
static int data_handler(void *user, const char *section, const char *name, static int data_handler(void *user, const char *section, const char *name,
const char *value) const char *value)
{ {
_unused(user); UNUSED(user);
if (strcmp(section, d->section_name)) if (strcmp(section, c->section_name))
return 0; return 0;
if (!strcmp(name, "tty")) { if (!strcmp(name, "tty"))
asprintf(&d->tty, value, d->match); {
option.tty_device = d->tty; asprintf(&c->tty, value, c->match);
} else if (!strcmp(name, "baudrate")) { option.tty_device = c->tty;
}
else if (!strcmp(name, "baudrate"))
{
option.baudrate = string_to_long((char *)value); option.baudrate = string_to_long((char *)value);
} else if (!strcmp(name, "databits")) { }
else if (!strcmp(name, "databits"))
{
option.databits = atoi(value); option.databits = atoi(value);
} else if (!strcmp(name, "flow")) { }
asprintf(&d->flow, "%s", value); else if (!strcmp(name, "flow"))
option.flow = d->flow; {
} else if (!strcmp(name, "stopbits")) { asprintf(&c->flow, "%s", value);
option.flow = c->flow;
}
else if (!strcmp(name, "stopbits"))
{
option.stopbits = atoi(value); option.stopbits = atoi(value);
} else if (!strcmp(name, "parity")) { }
asprintf(&d->parity, "%s", value); else if (!strcmp(name, "parity"))
option.parity = d->parity; {
} else if (!strcmp(name, "output-delay")) { asprintf(&c->parity, "%s", value);
option.parity = c->parity;
}
else if (!strcmp(name, "output-delay"))
{
option.output_delay = atoi(value); option.output_delay = atoi(value);
} else if (!strcmp(name, "no-autoconnect")) { }
else if (!strcmp(name, "no-autoconnect"))
{
option.no_autoconnect = atoi(value); option.no_autoconnect = atoi(value);
} else if (!strcmp(name, "log")) { }
else if (!strcmp(name, "log"))
{
option.log = atoi(value); option.log = atoi(value);
} else if (!strcmp(name, "local-echo")) { }
else if (!strcmp(name, "local-echo"))
{
option.local_echo = atoi(value); option.local_echo = atoi(value);
} else if (!strcmp(name, "timestamp")) { }
else if (!strcmp(name, "timestamp"))
{
option.timestamp = atoi(value); option.timestamp = atoi(value);
} else if (!strcmp(name, "log-filename")) { }
asprintf(&d->log_filename, "%s", value); else if (!strcmp(name, "log-filename"))
option.log_filename = d->log_filename; {
} else if (!strcmp(name, "map")) { asprintf(&c->log_filename, "%s", value);
asprintf(&d->map, "%s", value); option.log_filename = c->log_filename;
option.map = d->map; }
else if (!strcmp(name, "map"))
{
asprintf(&c->map, "%s", value);
option.map = c->map;
} }
return 0; return 0;
@ -130,83 +160,98 @@ static int data_handler(void *user, const char *section, const char *name,
static int section_search_handler(void *user, const char *section, const char static int section_search_handler(void *user, const char *section, const char
*varname, const char *varval) *varname, const char *varval)
{ {
_unused(user); UNUSED(user);
if (strcmp(varname, "pattern")) if (strcmp(varname, "pattern"))
return 0; return 0;
if (!strcmp(varval, d->user)) { if (!strcmp(varval, c->user))
{
/* pattern matches as plain text */ /* pattern matches as plain text */
asprintf(&d->section_name, "%s", section); asprintf(&c->section_name, "%s", section);
} else if (get_match(d->user, varval, &d->match) > 0) { }
else if (get_match(c->user, varval, &c->match) > 0)
{
/* pattern matches as regex */ /* pattern matches as regex */
asprintf(&d->section_name, "%s", section); asprintf(&c->section_name, "%s", section);
} }
return 0; return 0;
} }
int resolve_conf_file(void) static int resolve_config_file(void)
{ {
asprintf(&d->path, "%s/tio/tiorc", getenv("XDG_CONFIG_HOME")); asprintf(&c->path, "%s/tio/tiorc", getenv("XDG_CONFIG_HOME"));
if (!access(d->path, F_OK)) if (!access(c->path, F_OK))
{
return 0; return 0;
}
asprintf(&d->path, "%s/.config/tio/tiorc", getenv("HOME")); asprintf(&c->path, "%s/.config/tio/tiorc", getenv("HOME"));
if (!access(d->path, F_OK)) if (!access(c->path, F_OK))
{
return 0; return 0;
}
asprintf(&d->path, "%s/.tiorc", getenv("HOME")); asprintf(&c->path, "%s/.tiorc", getenv("HOME"));
if (!access(d->path, F_OK)) if (!access(c->path, F_OK))
{
return 0; return 0;
}
return -EINVAL; return -EINVAL;
} }
void conf_parse_file(const int argc, char *argv[]) void config_file_parse(const int argc, char *argv[])
{ {
int ret; int ret;
int i; int i;
d = malloc(sizeof(struct conf_data)); c = malloc(sizeof(struct config_t));
memset(d, 0, sizeof(struct conf_data)); memset(c, 0, sizeof(struct config_t));
resolve_conf_file(); resolve_config_file();
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++)
if (argv[i][0] != '-') { {
d->user = argv[i]; if (argv[i][0] != '-')
{
c->user = argv[i];
break; break;
} }
} }
if (!d->user) if (!c->user)
return; {
return;
}
ret = ini_parse(d->path, section_search_handler, NULL); ret = ini_parse(c->path, section_search_handler, NULL);
if (!d->section_name) { if (!c->section_name)
{
debug_printf("unable to match user input to configuration section (%d)\n", ret); debug_printf("unable to match user input to configuration section (%d)\n", ret);
return; return;
} }
ret = ini_parse(d->path, data_handler, NULL); ret = ini_parse(c->path, data_handler, NULL);
if (ret < 0) { if (ret < 0)
{
fprintf(stderr, "Error: unable to parse configuration file (%d)\n", ret); fprintf(stderr, "Error: unable to parse configuration file (%d)\n", ret);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
void conf_exit(void) void config_exit(void)
{ {
free(d->tty); free(c->tty);
free(d->flow); free(c->flow);
free(d->parity); free(c->parity);
free(d->log_filename); free(c->log_filename);
free(d->map); free(c->map);
free(d->match); free(c->match);
free(d->section_name); free(c->section_name);
free(d->path); free(c->path);
free(d); free(c);
} }

View file

@ -1,7 +1,8 @@
/* /*
* tio - a simple TTY terminal I/O application * tio - a simple serial terminal I/O application
* *
* Copyright (c) 2020 Liam Beguin * Copyright (c) 2020 Liam Beguin
* Copyright (c) 2022 Martin Lund
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
@ -19,10 +20,10 @@
* 02110-1301, USA. * 02110-1301, USA.
*/ */
#ifndef CONFFILE_H #pragma once
#define CONFFILE_H
struct conf_data { struct config_t
{
const char *user; const char *user;
char *path; char *path;
@ -36,7 +37,5 @@ struct conf_data {
char *map; char *map;
}; };
void conf_parse_file(const int argc, char *argv[]); void config_file_parse(const int argc, char *argv[]);
void conf_exit(void); void config_exit(void);
#endif /* CONFFILE_H */

View file

@ -24,7 +24,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "options.h" #include "options.h"
#include "conffile.h" #include "configfile.h"
#include "tty.h" #include "tty.h"
#include "log.h" #include "log.h"
#include "error.h" #include "error.h"
@ -38,11 +38,11 @@ int main(int argc, char *argv[])
atexit(&error_exit); atexit(&error_exit);
/* Parse configuration file */ /* Parse configuration file */
conf_parse_file(argc, argv); config_file_parse(argc, argv);
atexit(&conf_exit); atexit(&config_exit);
/* Parse options */ /* Parse command-line options */
parse_options(argc, argv); options_parse(argc, argv);
/* List available serial devices */ /* List available serial devices */
if (option.list_devices) if (option.list_devices)

View file

@ -14,9 +14,9 @@ tio_sources = [
] ]
# dependencies # dependencies
if get_option('conffile') if get_option('configfile')
inih = meson.get_compiler('c').find_library('inih') inih = meson.get_compiler('c').find_library('inih')
tio_sources += 'conffile.c' tio_sources += 'configfile.c'
else else
inih = dependency() inih = dependency()
endif endif

View file

@ -20,7 +20,7 @@
*/ */
#pragma once #pragma once
#define _unused(x) (void)(x) #define UNUSED(expr) do { (void)(expr); } while (0)
char * current_time(void); char * current_time(void);
void delay(long ms); void delay(long ms);

View file

@ -1,5 +1,5 @@
/* /*
* tio - a simple TTY terminal I/O tool * tio - a simple serial terminal I/O tool
* *
* Copyright (c) 2014-2022 Martin Lund * Copyright (c) 2014-2022 Martin Lund
* *
@ -81,7 +81,7 @@ void print_help(char *argv[])
printf("\n"); printf("\n");
} }
void parse_options(int argc, char *argv[]) void options_parse(int argc, char *argv[])
{ {
int c; int c;

View file

@ -56,4 +56,4 @@ struct option_t
extern struct option_t option; extern struct option_t option;
void parse_options(int argc, char *argv[]); void options_parse(int argc, char *argv[]);