changeset 555:36423638841a

Clean up the parser a bit. Also return valid error value when an error occurs, instead of a bogus value.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 04 Jan 2020 16:49:25 +0200
parents c9b6fea48c8d
children 46f0bea942e4
files th_config.c
diffstat 1 files changed, 60 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- a/th_config.c	Fri Jan 03 10:54:39 2020 +0200
+++ b/th_config.c	Sat Jan 04 16:49:25 2020 +0200
@@ -255,7 +255,7 @@
 }
 
 
-static void th_cfg_set_item(th_cfgparserctx_t *ctx, th_cfgitem_t *item, const char *str)
+static int th_cfg_set_item(th_cfgparserctx_t *ctx, th_cfgitem_t *item, const char *str)
 {
     BOOL res = TRUE;
 
@@ -278,7 +278,7 @@
                     th_cfg_set_next_parsemode(ctx, PM_LIST);
 
                     // Early exit as we set the parsemode here
-                    return;
+                    return THERR_OK;
                 }
                 else
                     res = FALSE;
@@ -306,7 +306,9 @@
             break;
     }
 
-    th_cfg_set_parsemode(ctx, res ? PM_IDLE : PM_ERROR);
+    th_cfg_set_parsemode(ctx, PM_IDLE);
+
+    return res ? THERR_OK : THERR_INVALID_DATA;
 }
 
 
@@ -316,13 +318,14 @@
     th_cfgitem_t *item = NULL;
     char *tmpStr = NULL;
     size_t strPos;
-    BOOL isEscaped, isStart, isError, fpSet;
+    BOOL isEscaped, isStart, fpSet;
+    int ret = THERR_OK;
 
     // Initialize values
     memset(&ctx, 0, sizeof(ctx));
     ctx.ch = -1;
     ctx.nextMode = ctx.prevMode = ctx.parseMode = PM_IDLE;
-    isEscaped = fpSet = isStart = isError = FALSE;
+    isEscaped = fpSet = isStart = FALSE;
     strPos = 0;
 
     if ((tmpStr = th_malloc(SET_MAX_BUF + 1)) == NULL)
@@ -339,11 +342,12 @@
             case EOF:
                 if (ctx.parseMode != PM_IDLE)
                 {
-                    th_io_error(fh, THERR_OUT_OF_DATA, "Unexpected end of file.\n");
-                    ctx.parseMode = PM_ERROR;
+                    ret = th_io_error(fh, THERR_OUT_OF_DATA,
+                        "Unexpected end of file.\n");
+                    goto out;
                 }
-                else
-                    ctx.parseMode = PM_EOF;
+
+                ctx.parseMode = PM_EOF;
                 break;
 
             case '\n':
@@ -385,9 +389,9 @@
                 }
                 else
                 {
-                    th_io_error(fh, THERR_INVALID_DATA,
+                    ret = th_io_error(fh, THERR_INVALID_DATA,
                         "Invalid nesting sequence encountered.\n");
-                    ctx.parseMode = PM_ERROR;
+                    goto out;
                 }
             }
             else
@@ -400,9 +404,9 @@
             else
             {
                 // Error! Invalid character found
-                th_io_error(fh, THERR_INVALID_DATA,
+                ret = th_io_error(fh, THERR_INVALID_DATA,
                     "Unexpected character '%c'.\n", ctx.ch);
-                ctx.parseMode = PM_ERROR;
+                goto out;
             }
             break;
 
@@ -428,8 +432,9 @@
                 else
                 {
                     // Error! Key name string too long!
-                    th_io_error(fh, THERR_INVALID_DATA, "Config key name too long!");
-                    ctx.parseMode = PM_ERROR;
+                    ret = th_io_error(fh, THERR_INVALID_DATA,
+                        "Config key name too long!");
+                    goto out;
                 }
                 ctx.ch = -1;
                 tmpStr[strPos] = 0;
@@ -438,10 +443,10 @@
             {
                 // Error! Invalid character found
                 tmpStr[strPos] = 0;
-                th_io_error(fh, THERR_INVALID_DATA,
+                ret = th_io_error(fh, THERR_INVALID_DATA,
                     "Unexpected character '%c' in key name '%s'.\n",
                     ctx.ch, tmpStr);
-                ctx.parseMode = PM_ERROR;
+                goto out;
             }
             break;
 
@@ -474,10 +479,10 @@
                 else
                 {
                     // Error! No configuration key by this name found
-                    th_io_error(fh, THERR_INVALID_DATA,
+                    ret = th_io_error(fh, THERR_INVALID_DATA,
                         "No such configuration setting ('%s')\n",
                         tmpStr);
-                    ctx.parseMode = PM_ERROR;
+                    goto out;
                 }
 
                 ctx.ch = -1;
@@ -485,10 +490,10 @@
             else
             {
                 // Error! '=' expected!
-                th_io_error(fh, THERR_INVALID_DATA,
+                ret = th_io_error(fh, THERR_INVALID_DATA,
                     "Unexpected character '%c', assignation '=' was expected.\n",
                     ctx.ch);
-                ctx.parseMode = PM_ERROR;
+                goto out;
             }
             break;
 
@@ -535,19 +540,17 @@
             if (ctx.ch != '{')
             {
                 // Error! Section start '{' expected!
-                th_io_error(fh, THERR_INVALID_DATA,
+                ret = th_io_error(fh, THERR_INVALID_DATA,
                     "Unexpected character '%c', section start '{' was expected.\n",
                     ctx.ch);
-                ctx.parseMode = PM_ERROR;
+                goto out;
             }
             else
             {
-                int res = th_cfg_read_sect(fh, item->v.section, nesting + 1);
-                if (res == THERR_OK)
-                    th_cfg_set_parsemode(&ctx, PM_IDLE);
-                else
-                    ctx.parseMode = PM_ERROR;
+                if ((ret = th_cfg_read_sect(fh, item->v.section, nesting + 1)) != THERR_OK)
+                    goto out;
 
+                th_cfg_set_parsemode(&ctx, PM_IDLE);
                 ctx.ch = -1;
             }
             break;
@@ -567,7 +570,8 @@
             {
                 // End of string, set the value
                 tmpStr[strPos] = 0;
-                th_cfg_set_item(&ctx, item, tmpStr);
+                if ((ret = th_cfg_set_item(&ctx, item, tmpStr)) != THERR_OK)
+                    goto out;
             }
             else
             if (!isEscaped && ctx.ch == '\\')
@@ -582,10 +586,10 @@
                 else
                 {
                     // Error! String too long!
-                    th_io_error(fh, THERR_INVALID_DATA,
+                    ret = th_io_error(fh, THERR_INVALID_DATA,
                         "String too long! Maximum is %d characters.",
                         SET_MAX_BUF);
-                    ctx.parseMode = PM_ERROR;
+                    goto out;
                 }
                 isEscaped = FALSE;
             }
@@ -598,17 +602,17 @@
             if (isStart && item->type == ITEM_UINT && ctx.ch == '-')
             {
                 // Error! Negative values not allowed for unsigned ints
-                th_io_error(fh, THERR_INVALID_DATA,
+                ret = th_io_error(fh, THERR_INVALID_DATA,
                     "Negative value specified for %s, unsigned value expected.",
                     item->name);
-                ctx.parseMode = PM_ERROR;
+                goto out;
             }
             else
             if (isStart && (ctx.ch == '-' || ctx.ch == '+'))
             {
                 VADDCH(ctx.ch)
                 else
-                isError = TRUE;
+                ret = THERR_INVALID_DATA;
             }
             else
             if (isStart && item->type == ITEM_FLOAT && ctx.ch == '.')
@@ -616,11 +620,11 @@
                 fpSet = TRUE;
                 VADDCH('0')
                 else
-                isError = TRUE;
+                ret = THERR_INVALID_DATA;
 
                 VADDCH(ctx.ch)
                 else
-                isError = TRUE;
+                ret = THERR_INVALID_DATA;
             }
             else
             if (item->type == ITEM_FLOAT && ctx.ch == '.' && !fpSet)
@@ -628,39 +632,42 @@
                 fpSet = TRUE;
                 VADDCH(ctx.ch)
                 else
-                isError = TRUE;
+                ret = THERR_INVALID_DATA;
             }
             else
             if (th_isdigit(ctx.ch))
             {
                 VADDCH(ctx.ch)
                 else
-                isError = TRUE;
+                ret = THERR_INVALID_DATA;
             }
             else
             if (th_cfg_is_end(ctx.ch))
             {
                 // End of integer parsing mode
                 tmpStr[strPos] = 0;
-                th_cfg_set_item(&ctx, item, tmpStr);
+
+                if ((ret = th_cfg_set_item(&ctx, item, tmpStr)) != THERR_OK)
+                    goto out;
+
                 th_cfg_set_parsemode(&ctx, PM_IDLE);
             }
             else
             {
                 // Error! Unexpected character.
-                th_io_error(fh, THERR_INVALID_DATA,
+                ret = th_io_error(fh, THERR_INVALID_DATA,
                     "Unexpected character '%c' for numeric setting '%s'.",
                     ctx.ch, item->name);
-                ctx.parseMode = PM_ERROR;
+                goto out;
             }
 
-            if (isError)
+            if (ret != THERR_OK)
             {
                 // Error! String too long!
-                th_io_error(fh, THERR_INVALID_DATA,
+                ret = th_io_error(fh, ret,
                     "String too long! Maximum is %d characters.",
                     SET_MAX_BUF);
-                ctx.parseMode = PM_ERROR;
+                goto out;
             }
 
             isStart = FALSE;
@@ -679,21 +686,21 @@
             {
                 VADDCH(ctx.ch)
                 else
-                isError = TRUE;
+                ret = THERR_INVALID_DATA;
             }
             else
             if (th_cfg_is_end(ctx.ch))
             {
                 tmpStr[strPos] = 0;
-                th_cfg_set_item(&ctx, item, tmpStr);
+                ret = th_cfg_set_item(&ctx, item, tmpStr);
             }
 
-            if (isError || ctx.parseMode == PM_ERROR)
+            if (ret != THERR_OK)
             {
-                th_io_error(fh, THERR_INVALID_DATA,
+                ret = th_io_error(fh, ret,
                     "Invalid boolean value for '%s'.\n",
                     item->name);
-                ctx.parseMode = PM_ERROR;
+                goto out;
             }
             ctx.ch = -1;
             break;
@@ -704,10 +711,10 @@
     th_free(tmpStr);
 
     // Return result
-    if (ctx.parseMode == PM_ERROR)
-        return fh->status;
-    else
-        return THERR_OK;
+    if (ret == THERR_OK && ctx.parseMode == PM_ERROR)
+        ret = THERR_INVALID_DATA;
+
+    return ret;
 }