summaryrefslogtreecommitdiff
path: root/application/spidlib
diff options
context:
space:
mode:
authorjerome lefranc2008-11-24 23:03:31 +0100
committerjerome lefranc2008-11-24 23:03:31 +0100
commited3c2985d5d1e6065d515ee2ecfbd0b87cfc51de (patch)
tree044b845a14c3e6554d3f6cebf3b7960531f652ab /application/spidlib
parenta235bed1c0ffceea3f7cf3f9239e71cc2efedd28 (diff)
[spidlib] check file for spidlib_read_config_param
Diffstat (limited to 'application/spidlib')
-rw-r--r--application/spidlib/Makefile8
-rw-r--r--application/spidlib/check/Makefile2
-rw-r--r--application/spidlib/check/check.c2
-rw-r--r--application/spidlib/check/check_config.c85
-rw-r--r--application/spidlib/check/testfiles/config_file_113
-rw-r--r--application/spidlib/src/config/functions.c (renamed from application/spidlib/src/varfunc/functions.c)0
-rw-r--r--application/spidlib/src/config/read_config_param.c (renamed from application/spidlib/src/varfunc/config_handlers.c)0
-rw-r--r--application/spidlib/src/config/write_config_param.c268
8 files changed, 372 insertions, 6 deletions
diff --git a/application/spidlib/Makefile b/application/spidlib/Makefile
index 59bc6cc0b0..5804367c22 100644
--- a/application/spidlib/Makefile
+++ b/application/spidlib/Makefile
@@ -1,5 +1,5 @@
TOPDIR=../..
-SRCDIR=src:src/system:src/plc:src/ethernet:src/network:src/services:src/varfunc
+SRCDIR=src:src/system:src/plc:src/ethernet:src/network:src/services:src/config
INCDIR=inc
OBJDIR=obj
SPIDLIB=spidlib.a
@@ -9,11 +9,11 @@ INCLUDES=spidlib.h ethernet.h network.h plc.h services.h system.h
OBJS = 8021x_supplicant_state.o 8021x_supplicant_identity.o 8021x_supplicant_password.o \
8021x_auth_server_addr.o 8021x_auth_server_port.o 8021x_auth_server_secret.o 8021x_auth_mode.o 8021x_auth_state.o \
afe_version.o bands.o bridge.o board_version.o boot_version.o bootlog_messages.o bssid.o\
- config_handlers.o cpuinfo.o default_gain.o duplex.o functions.o ftp_state.o host.o hostname.o http_state.o interrupts.o ip_addr.o \
+ cpuinfo.o default_gain.o duplex.o functions.o ftp_state.o host.o hostname.o http_state.o interrupts.o ip_addr.o \
ip_netmask.o ip_mode.o mac.o mac_limit.o mac_list.o meminfo.o \
- mode.o nb_hosts.o processes.o route_default.o snmp_state.o software_version.o serial_state.o \
+ mode.o nb_hosts.o processes.o read_config_param.o route_default.o snmp_state.o software_version.o serial_state.o \
serial_number.o speed.o static_spy.o system_version.o telnet_state.o tei.o uptime.o \
- vlan.o white_list.o reboot.o ip_gateway.o nvram.o image_desc.o \
+ vlan.o white_list.o write_config_param.o reboot.o ip_gateway.o nvram.o image_desc.o \
snmp_analog_alarm.o snmp_discrete_alarm.o \
admin_logical_id.o manufactory_info.o model_number.o vendor_info.o output_level.o
diff --git a/application/spidlib/check/Makefile b/application/spidlib/check/Makefile
index 871a3a5ccd..2e28a59f47 100644
--- a/application/spidlib/check/Makefile
+++ b/application/spidlib/check/Makefile
@@ -1,6 +1,6 @@
PROGS = check
-FILE = ethernet system #network plc services system varfunc
+FILE = ethernet system varfunc #network plc services
CHECK_OBJS = $(FILE:%=check_%.o)
VPATH = ../src:../src/system:../src/plc:../src/ethernet:../src/network:../src/services:../src/varfunc
diff --git a/application/spidlib/check/check.c b/application/spidlib/check/check.c
index bde8a7ff2d..e583825619 100644
--- a/application/spidlib/check/check.c
+++ b/application/spidlib/check/check.c
@@ -15,12 +15,12 @@ int main(void)
Suite *s = suite_create("spidlib");
SRunner *sr = srunner_create(s);
+ srunner_add_suite(sr, varfunc_suite());
srunner_add_suite(sr, ethernet_suite());
/*srunner_add_suite(sr, network_suite());
srunner_add_suite(sr, plc_suite());
srunner_add_suite(sr, services_suite());*/
srunner_add_suite(sr, system_suite());
- /*srunner_add_suite(sr, varfunc_suite());*/
srunner_run_all(sr, CK_NORMAL);
nf = srunner_ntests_failed(sr);
srunner_free(sr);
diff --git a/application/spidlib/check/check_config.c b/application/spidlib/check/check_config.c
new file mode 100644
index 0000000000..6a754e9c4a
--- /dev/null
+++ b/application/spidlib/check/check_config.c
@@ -0,0 +1,85 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <check.h>
+
+START_TEST (test_read_param)
+{
+ char value[64];
+ fail_unless(((spidlib_read_config_param(NULL, "test", value, 16) < 0)
+ && (errno == EINVAL)),
+ "filename = NULL");
+
+ fail_unless(((spidlib_read_config_param("config_file_1", NULL, value, 16) < 0)
+ && (errno == EINVAL)),
+ "label = NULL");
+
+ fail_unless(((spidlib_read_config_param("config_file_1", "test", NULL, 16) < 0)
+ && (errno == EINVAL)),
+ "value = NULL");
+
+ fail_unless(((spidlib_read_config_param("config_file_1", "test", value, 0) < 0)
+ && (errno == EINVAL)),
+ "value_len <= 0");
+
+ fail_unless(((spidlib_read_config_param("noname_file", "test", value, 16) < 0)
+ && (errno == ENOENT)),
+ "file doesn't exist");
+
+ fail_unless(((spidlib_read_config_param("config_file_1", "nolabel", value, 16) < 0)
+ && (errno == ENODATA)),
+ "label doesn't exist");
+
+ fail_unless(((spidlib_read_config_param("config_file_1", "label2", value, 5) < 0)
+ && (errno == ENOSPC)),
+ "value buffer too short");
+
+ fail_unless(((spidlib_read_config_param("config_file_1", "label2", value, 6) >= 0)
+ && !strcmp(value, "value1")),
+ "label2 = value1");
+
+ fail_unless(((spidlib_read_config_param("config_file_1", "label3", value, 32) >= 0)
+ && !strcmp(value, "multiple value string")),
+ "multiple value string");
+
+ fail_unless(((spidlib_read_config_param("config_file_1", "label4", value, 32) >= 0)
+ && !strcmp(value, "no space between delimiter")),
+ "no space between delimiter");
+
+ fail_unless(((spidlib_read_config_param("config_file_1", "label5", value, 32) >= 0)
+ && !strcmp(value, "multi spaced")),
+ "multi spaced");
+
+ fail_unless(((spidlib_read_config_param("config_file_1", "label6", value, 32) >= 0)
+ && !strcmp(value, "")),
+ "empty value");
+
+ fail_unless(((spidlib_read_config_param("config_file_1", "label7", value, 32) >= 0)
+ && !strcmp(value, "after bad entry")),
+ "label after bad entry");
+}
+END_TEST
+
+START_TEST (test_read_param)
+{
+
+}
+END_TEST
+
+TCase *
+config_param_tcase (void)
+{
+ TCase *tc = tcase_create ("config_param");
+ tcase_add_test (tc, test_read_param);
+ tcase_add_test (tc, test_write_param);
+ return tc;
+}
+
+Suite *
+config_suite (void)
+{
+ Suite *s = suite_create ("config");
+ suite_add_tcase (s, config_param_tcase ());
+ return s;
+}
+
diff --git a/application/spidlib/check/testfiles/config_file_1 b/application/spidlib/check/testfiles/config_file_1
new file mode 100644
index 0000000000..92873f6e39
--- /dev/null
+++ b/application/spidlib/check/testfiles/config_file_1
@@ -0,0 +1,13 @@
+# this is example config file
+label1 = value1
+label2 = value1
+label3 = multiple value string
+# comment here
+
+label4=no space between delimiter
+label5 = multi spaced
+label6 =
+# oups, bad entries
+ = no label
+# can we reach this label ?
+label7 = after bad entry
diff --git a/application/spidlib/src/varfunc/functions.c b/application/spidlib/src/config/functions.c
index 749c9dd086..749c9dd086 100644
--- a/application/spidlib/src/varfunc/functions.c
+++ b/application/spidlib/src/config/functions.c
diff --git a/application/spidlib/src/varfunc/config_handlers.c b/application/spidlib/src/config/read_config_param.c
index 5e873b5b3a..5e873b5b3a 100644
--- a/application/spidlib/src/varfunc/config_handlers.c
+++ b/application/spidlib/src/config/read_config_param.c
diff --git a/application/spidlib/src/config/write_config_param.c b/application/spidlib/src/config/write_config_param.c
new file mode 100644
index 0000000000..5e873b5b3a
--- /dev/null
+++ b/application/spidlib/src/config/write_config_param.c
@@ -0,0 +1,268 @@
+/*-----------------------------------------------------------------------------
+ File: config_handlers.c
+ Description: Functions to handle read/write for configuration file.
+ Project: SPiDLIB
+ Target: SPiDCOM modem w/ SPC200e
+ Version: 0.1
+ Revision list: -
+ Company: SPiDCOM Eastern Europe
+ Author: Slobodan Stanisic
+ Date: June 2008.
+-----------------------------------------------------------------------------*/
+
+/*---------------------- Include files --------------------------------------*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#include "spidlib.h"
+
+/*-----------------------------------------------------------------------------
+ Function: read_config_param
+ Input: char *filename - full filename of a configuration file
+ char *param - pointer to beginning of string containing parameter name
+ char *output_value - pointer to buffer where output is expected
+ int output_buffer_len - max. length of output buffer
+ Output: 0 - Function returned successfully
+ -errno - There were errors
+ Error handling: Look for output value. In case of error check errno variable:
+ EINVAL - inappropriate buffer length requested
+ ENOTDIR - missing configuration file
+ EFAULT - buffer overrun or no parameter
+ Note: -
+-----------------------------------------------------------------------------*/
+int
+read_config_param(char *filename, char *param, char *output_value,
+ int output_buffer_len)
+{
+ char buffer[MAX_FILE_LINE_LENGTH];
+ FILE *fp;
+ int param_correct = 0;
+ int buffer_index = 0;
+ int parameter_extracted = 0;
+ int buffer_overrun_flag = -1;
+ int x;
+
+/* Test max. output length */
+ if ( (output_buffer_len <= 0) ||
+ (output_buffer_len > MAX_OUTPUT_BUFFER_LENGTH) )
+ {
+ return (-(errno = EINVAL));
+ }
+
+/* Try to open file for reading */
+ if ( (fp = fopen(filename, "r") ) == NULL )
+ return (-(errno = ENOTDIR));
+
+/* Reset output string */
+ strcpy(output_value, "");
+
+/* Browse through all lines */
+ while ( fgets(buffer,MAX_FILE_LINE_LENGTH,fp) )
+ {
+/* Reset buffer index */
+ buffer_index = 0;
+
+/* Reset flag for correctly detected parameter */
+ param_correct = 0;
+
+/* Ignore spaces at the beginning of a line */
+ while ( buffer[buffer_index] == ' ' )
+ {
+ buffer_index++;
+ }
+
+/* '#' at beginning of a line means comment - ignore it */
+ if ( buffer[buffer_index] == '#' )
+ continue;
+
+/* Now check for correct parameter name */
+ x = 0;
+ while ( *(param+x) != '\0' )
+ {
+ if ( buffer[buffer_index++] != *(param+x) )
+ {
+ param_correct = -1;
+ continue;
+ }
+ x++;
+ }
+
+/* Check if compare succeded */
+ if ( param_correct )
+ continue;
+
+/* Dump spaces after parameter name */
+ while ( buffer[buffer_index] == ' ' )
+ {
+ buffer_index++;
+ }
+
+/* Check for '=' char */
+ if ( buffer[buffer_index] != '=' )
+ continue;
+
+/* Skip '=' sign */
+ buffer_index++;
+
+/* Dump spaces after '=' sign */
+ while ( buffer[buffer_index] == ' ' )
+ {
+ buffer_index++;
+ }
+
+/* Pull rest of the line in the output buffer */
+/* x is counter of elements in output string */
+ x = 0;
+/* Set parameter_extracted flag to note that parameter is found. If there are
+ no errors while reading parameter value, this flag will indicate that
+ requested parameter is found. */
+ parameter_extracted = -1;
+
+ while ( (buffer[buffer_index] != '\0') &&
+ (buffer[buffer_index] != '\n') )
+ {
+ *(output_value++) = buffer[buffer_index++];
+ x++;
+/* Check for buffer overrun, in case of error terminate string */
+ if ( x > output_buffer_len )
+ {
+ buffer_overrun_flag = 0;
+ *(--output_value) = '\0';
+ continue;
+ }
+ }
+ }
+
+ fclose(fp);
+
+/* Check if buffer overrun occured */
+ if ( !buffer_overrun_flag )
+ {
+ return (-(errno = EFAULT));
+ }
+
+/* Check if parameter extracted */
+ if ( !parameter_extracted )
+ {
+ return (-(errno = EFAULT));
+ }
+
+ return 0;
+}
+
+/*-----------------------------------------------------------------------------
+ Function: write_config_param
+ Input: char *filename - full filename of a configuration file
+ char *param - pointer to beginning of string containing parameter name
+ char *value - pointer to buffer where parameter value is located
+ Output: 0 - Function returned successfully
+ -errno - There were errors
+ Error handling: Look for output value. In case of error check errno variable:
+ EINVAL - inappropriate buffer length requested
+ ENOTDIR - missing configuration file
+ EFAULT - buffer overrun or no parameter
+ Note: -
+-----------------------------------------------------------------------------*/
+int
+write_config_param(char *filename, char *param, char *value)
+{
+ char buffer[MAX_FILE_LINE_LENGTH];
+ char temp_buffer[MAX_FILE_LINE_LENGTH];
+ FILE *fp, *fp_memo;
+ int param_correct = 0;
+ int buffer_index = 0;
+ int parameter_found = -1;
+ int line_length;
+ int c;
+ int x;
+
+/* Test max. parameter length
+ if ( strlen(value) > MAX_PARAMETER_VALUE_LENGTH )
+ return (-(errno = EINVAL)); */
+
+/* Try to open file for reading and writing */
+ if ( (fp = fopen(filename, "rw") ) == NULL )
+ return (-(errno = ENOTDIR));
+
+/* Browse through all lines */
+ while ( fgets(buffer,MAX_FILE_LINE_LENGTH,fp) && parameter_found )
+ {
+/* Reset buffer index */
+ buffer_index = 0;
+
+/* Reset flag for correctly detected parameter */
+ param_correct = 0;
+
+/* Get read line length */
+ line_length = strlen ( buffer );
+
+/* Ignore spaces at the beginning of a line */
+ while ( buffer[buffer_index] == ' ' )
+ {
+ buffer_index++;
+ }
+
+/* '#' at beginning of a line means comment - ignore it */
+ if ( buffer[buffer_index] == '#' )
+ continue;
+
+/* Now check for correct parameter name */
+ x = 0;
+ while ( *(param+x) != '\0' )
+ {
+ if ( buffer[buffer_index++] != *(param+x) )
+ {
+ param_correct = -1;
+ continue;
+ }
+ x++;
+ }
+
+/* Check if compare succeded */
+ if ( param_correct )
+ continue;
+ else
+/* Leave the loop by flagging parameter_found */
+ parameter_found = 0;
+ }
+
+/* Make a backup of current position */
+ *fp_memo = *fp;
+
+ if ( !parameter_found )
+ {
+/* Read rest of the file and put it in the buffer */
+ x = 0;
+ while ( (c = fgetc(fp)) != EOF )
+ {
+ temp_buffer[x++] = (char)c;
+ }
+
+/* Write new value to output file */
+/* First, calculate file pointer position */
+ fp_memo -= buffer_index;
+
+/* Write new value to file */
+ fwrite ( value, 1, strlen(value), fp_memo );
+/* Add NULL and end of line */
+ fputc ( '\0', fp_memo );
+ fputc ( '\n', fp_memo );
+
+/* Add rest of the file */
+/* TODO!!! */
+ }
+ else
+ {
+
+ }
+
+
+/* Unfinished part */
+
+ fclose(fp);
+
+ return 0;
+}