From a4841776b6e1751232d46482731836a7c17b896f Mon Sep 17 00:00:00 2001 From: Nathanael Sensfelder Date: Fri, 3 Jan 2020 03:50:24 +0100 Subject: Still working on it... --- include/relabsd/config.h | 5 + include/relabsd/config/parameters.h | 2 +- include/relabsd/config/parameters_types.h | 3 +- include/relabsd/device/axis.h | 13 + include/relabsd/util/string.h | 7 + src/config/config_file.c | 716 --------------------------- src/config/parameters/parameters.c | 44 +- src/config/parameters/parameters_accessors.c | 6 +- src/config/parameters/parse_config_file.c | 348 +++++++++++++ src/device/axis/axis.c | 15 +- src/device/axis/axis_filter.c | 20 +- src/device/axis/axis_option.c | 71 +++ src/util/string.c | 35 ++ 13 files changed, 538 insertions(+), 747 deletions(-) delete mode 100644 src/config/config_file.c create mode 100644 src/config/parameters/parse_config_file.c create mode 100644 src/device/axis/axis_option.c diff --git a/include/relabsd/config.h b/include/relabsd/config.h index e69de29..6558ebb 100644 --- a/include/relabsd/config.h +++ b/include/relabsd/config.h @@ -0,0 +1,5 @@ +#pragma once + +#ifndef RELABSD_CONF_AXIS_CODE_SIZE +#define RELABSD_CONF_AXIS_CODE_SIZE 2 +#endif diff --git a/include/relabsd/config/parameters.h b/include/relabsd/config/parameters.h index 5a12be4..e1c67a4 100644 --- a/include/relabsd/config/parameters.h +++ b/include/relabsd/config/parameters.h @@ -28,7 +28,7 @@ void relabsd_parameters_print_usage (const char exec [const restrict static 1]); /**** Accessors ***************************************************************/ void relabsd_parameters_initialize_options ( - const struct relabsd_parameters parameters [const restrict static 1] + struct relabsd_parameters parameters [const restrict static 1] ); int relabsd_parameters_get_run_as_daemon diff --git a/include/relabsd/config/parameters_types.h b/include/relabsd/config/parameters_types.h index ab07bfa..e6c4009 100644 --- a/include/relabsd/config/parameters_types.h +++ b/include/relabsd/config/parameters_types.h @@ -18,5 +18,6 @@ struct relabsd_parameters const char * device_name; const char * physical_device_file_name; const char * configuration_file; - struct relabsd_axis axes[RELABSD_AXIS_VALID_AXES_COUNT] + int timeout; /* TODO: use time structure + enabled flag */ + struct relabsd_axis axes[RELABSD_AXIS_VALID_AXES_COUNT]; }; diff --git a/include/relabsd/device/axis.h b/include/relabsd/device/axis.h index fee8522..815bbb8 100644 --- a/include/relabsd/device/axis.h +++ b/include/relabsd/device/axis.h @@ -52,3 +52,16 @@ enum relabsd_axis_name relabsd_axis_parse_name * Returned values should be coherent with the configuration file syntax. */ const char * relabsd_axis_name_to_string (const enum relabsd_axis_name e); + +/* + * Returns -1 if the option was discarded (an error has been reported), + * 0 if the option was successfully parsed. + */ +int relabsd_axis_enable_option_from_name +( + const char option_name [const restrict static 1], + const char axis_name [const restrict static 1], + struct relabsd_axis axis [const restrict static 1] +); + +void relabsd_axis_enable (struct relabsd_axis axis [const restrict static 1]); diff --git a/include/relabsd/util/string.h b/include/relabsd/util/string.h index d632720..a0ffde3 100644 --- a/include/relabsd/util/string.h +++ b/include/relabsd/util/string.h @@ -16,3 +16,10 @@ int relabsd_util_parse_int const int max, int output [const restrict static 1] ); + +/* + * Returns -1 on error, + * 0 on EOF, + * 1 on newline. + */ +int relabsd_util_reach_next_line_or_eof (FILE f [const restrict static 1]); diff --git a/src/config/config_file.c b/src/config/config_file.c deleted file mode 100644 index 2e5ef0c..0000000 --- a/src/config/config_file.c +++ /dev/null @@ -1,716 +0,0 @@ -/**** POSIX *******************************************************************/ -#include -#include -#include -#include -#include - -#include - -/**** RELABSD *****************************************************************/ -#include - -#include - -#ifndef RELABSD_OPTION_MAX_SIZE - #define RELABSD_OPTION_MAX_SIZE 64 -#endif - -/******************************************************************************/ -/**** LOCAL FUNCTIONS *********************************************************/ -/******************************************************************************/ -/* - * Returns -1 on (fatal) error, - * 0 on EOF, - * 1 on newline. - */ -static int reach_next_line_or_eof (FILE f [const restrict static 1]) -{ - char c; - - c = (char) getc(f); - - while ((c != '\n') && c != EOF) - { - c = (char) getc(f); - } - - if (ferror(f)) - { - /* - * The 'ferror' function's manual specifically states that it does not - * sets errno. There is no mention of errno in the 'getc' function's - * either, so I am assuming that errno cannot be used to indicate the - * error. - */ - RELABSD_S_FATAL - ( - "[CONFIG] Error while attempting to reach EOF or next line: %s." - ); - - return -1; - } - - if (c == EOF) - { - return 0; - } - - return 1; -} - -/* - * Returns -1 if the option was discarded (an error has been reported), - * 0 if the option was successfully parsed. - * - * ('length' - 1) is the number of relevant characters in 'name'. - * 'name' must support 'length' characters. - * name[length] will be set to \0, so it does not need to be when calling - * this function. - */ -static int parse_option -( - struct relabsd_config * const conf, - enum relabsd_axis const axis, - char * const name, - int const length -) -{ - name[length] = '\0'; - - if (strcmp(name, "direct") == 0) - { - conf->axis[axis].option[RELABSD_DIRECT_OPTION] = 1; - - RELABSD_DEBUG - ( - RELABSD_DEBUG_CONFIG, - "Axis '%s' enabled option 'direct'.", - relabsd_axis_to_name(axis) - ); - - if (conf->axis[axis].option[RELABSD_FRAMED_OPTION]) - { - RELABSD_WARNING - ( - "[CONFIG] Axis '%s': using option 'direct' discards option" - "'framed'.", - relabsd_axis_to_name(axis) - ); - } - } - else if (strcmp(name, "real_fuzz") == 0) - { - conf->axis[axis].option[RELABSD_REAL_FUZZ_OPTION] = 1; - - RELABSD_DEBUG - ( - RELABSD_DEBUG_CONFIG, - "Axis '%s' enabled option 'real_fuzz'.", - relabsd_axis_to_name(axis) - ); - } - else if (strcmp(name, "framed") == 0) - { - conf->axis[axis].option[RELABSD_FRAMED_OPTION] = 1; - - RELABSD_DEBUG - ( - RELABSD_DEBUG_CONFIG, - "Axis '%s' enabled option 'framed'.", - relabsd_axis_to_name(axis) - ); - - if (conf->axis[axis].option[RELABSD_DIRECT_OPTION]) - { - RELABSD_WARNING - ( - "[CONFIG] Axis '%s': using option 'direct' discards option" - "'framed'.", - relabsd_axis_to_name(axis) - ); - } - } - else - { - RELABSD_ERROR - ( - "[CONFIG] Unknown option '%s' for axis '%s'.", - name, - relabsd_axis_to_name(axis) - ); - - return -1; - } - - return 0; -} - -/* - * Returns -1 on error, - * 0 on EOF, - * 1 on newline. - */ -static int read_axis_options -( - struct relabsd_config * const conf, - FILE * const f, - enum relabsd_axis const axis -) -{ - char option[(RELABSD_OPTION_MAX_SIZE + 1)]; - int i, prev_errno; - char c; - - option[RELABSD_OPTION_MAX_SIZE] = '\0'; - - prev_errno = errno; - - errno = 0; - - memset(conf->axis[axis].option, 0, RELABSD_OPTIONS_COUNT * sizeof(int)); - - i = 0; - - while (i <= RELABSD_OPTION_MAX_SIZE) - { - c = (char) getc(f); - - if ((errno != 0) && (c == EOF)) - { - RELABSD_FATAL - ( - "[CONFIG] Reading error while parsing option name (axis '%s'): %s.", - relabsd_axis_to_name(axis), - strerror(errno) - ); - - errno = prev_errno; - - return -1; - } - - switch (c) - { - case ' ': - case '\t': - break; - - case ',': - /* We parsed a new option and there is a least another. */ - parse_option(conf, axis, option, i); - - i = 0; - - break; - - case '\n': - parse_option(conf, axis, option, i); - errno = prev_errno; - - return 1; - - case EOF: - parse_option(conf, axis, option, i); - errno = prev_errno; - - return 0; - - default: - option[i] = c; - i++; - - break; - } - } - - RELABSD_FATAL - ( - "[CONFIG] Option name '%s[...]' (axis '%s') is too long (%d chars max).", - option, - relabsd_axis_to_name(axis), - RELABSD_OPTION_MAX_SIZE - ); - - return -1; -} - -static int parse_timeout_option -( - struct relabsd_config * const conf, - const char * const param -) -{ - int timeout_msec; - const int prev_errno = errno; - - conf->enable_timeout = 1; - - errno = 0; - - timeout_msec = atoi(param); - - if (timeout_msec <= 0) - { - RELABSD_FATAL - ( - "Illegal value for timeout \"%d\": accepted range is [1, %d].", - timeout_msec, - INT_MAX - ); - - return -1; - } - - memset((void *) &(conf->timeout), 0, sizeof(struct timeval)); - - conf->timeout.tv_sec = (time_t) (timeout_msec / 1000); - - conf->timeout.tv_usec = - ( - ((suseconds_t) timeout_msec) - * ((suseconds_t) 1000) - ); - - return 0; -} - -/* - * Returns -1 on (fatal) error, - * 0 on succes. - */ -static int parse_axis_configuration_line -( - struct relabsd_config * const conf, - FILE * const f, - const char * const buffer -) -{ - int valc, prev_errno; - enum relabsd_axis axis; - - axis = relabsd_axis_from_name(buffer); - - if (axis == RELABSD_UNKNOWN) - { - RELABSD_FATAL - ( - "[CONFIG] Unknown axis '%s'.", - buffer - ); - - return -1; - } - - prev_errno = errno; - errno = 0; - - valc = - fscanf - ( - f, - "%d %d %d %d %d", - &(conf->axis[axis].min), - &(conf->axis[axis].max), - &(conf->axis[axis].fuzz), - &(conf->axis[axis].flat), - &(conf->axis[axis].resolution) - ); - - if (valc == EOF) - { - if (errno == 0) - { - RELABSD_FATAL - ( - "[CONFIG] Unexpected end of file while reading axis '%s'.", - buffer - ); - } - else - { - RELABSD_FATAL - ( - "[CONFIG] An error occured while reading axis '%s': %s.", - buffer, - strerror(errno) - ); - } - - errno = prev_errno; - - return -1; - } - else if (valc < 5) - { - RELABSD_FATAL - ( - "[CONFIG] Invalid parameter count for axis '%s'.", - buffer - ); - - errno = prev_errno; - - return -1; - } - - RELABSD_DEBUG - ( - RELABSD_DEBUG_CONFIG, - "Axis '%s': {min = %d; max = %d; fuzz = %d; flat = %d; resolution = %d}", - buffer, - conf->axis[axis].min, - conf->axis[axis].max, - conf->axis[axis].fuzz, - conf->axis[axis].flat, - conf->axis[axis].resolution - ); - - errno = prev_errno; - - conf->axis[axis].enabled = 1; - conf->axis[axis].previous_value = 0; - - return read_axis_options(conf, f, axis); -} - -/* - * Returns -1 on (fatal) error, - * 0 on EOF, - * 1 on newline. - */ -static int read_config_line -( - struct relabsd_config * const conf, - FILE * const f, - const char * const prefix -) -{ - if (!RELABSD_IS_PREFIX("#", prefix)) - { - return parse_axis_configuration_line(conf, f, prefix); - } - - return reach_next_line_or_eof(f); -} - -/* - * Returns -1 on (fatal) error, - * 0 on success. - */ -static int read_config_file -( - struct relabsd_config * const conf, - char * const filename -) -{ - FILE * f; - char buffer[(RELABSD_CONF_AXIS_CODE_SIZE + 1)]; - int continue_reading, prev_errno; - - buffer[RELABSD_CONF_AXIS_CODE_SIZE] = '\0'; - - f = fopen(filename, "r"); - - if (f == (FILE *) NULL) - { - RELABSD_FATAL - ( - "[CONFIG] Could not open file: %s.", - strerror(errno) - ); - - return -1; - } - - - prev_errno = errno; - errno = 0; - - continue_reading = 1; - - while - ( - (continue_reading == 1) - && - ( - fscanf - ( - f, - "%" RELABSD_TO_STRING(RELABSD_CONF_AXIS_CODE_SIZE) "s", - buffer - ) - != EOF - ) - ) - { - switch (read_config_line(conf, f, buffer)) - { - case 1: - /* Everything is going well. */ - break; - - case 0: - /* EOF reached. */ - continue_reading = 0; - break; - - case -1: - /* A fatal error occured. */ - errno = prev_errno; - - fclose(f); - return -1; - } - } - - if (errno != 0) - { - /* An error happened in the while loop condition. */ - RELABSD_FATAL - ( - "[CONFIG] Error while reading file: %s, last read '%s'.", - strerror(errno), - buffer - ); - - errno = prev_errno; - - fclose(f); - - return -1; - } - - errno = prev_errno; - - fclose(f); - - return 0; -} - -static void print_usage -( - const char * const exec -) -{ - RELABSD_FATAL - ( - "USAGE: %s input_device config_file [