config_file.l: Move helper functions to user code section.
authorAndre Noll <maan@tuebingen.mpg.de>
Tue, 11 Jul 2017 20:24:10 +0000 (22:24 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Tue, 28 Nov 2017 09:54:24 +0000 (10:54 +0100)
These functions refer to the global variables yytext and yylen.
Reentrant scanners need to use accessor methods instead of accessing
the global variables directly. But these methods are not available
in the definitions section.

As a preparation for making the scanner reentrant, this patch moves
the three helper functions from the definitions section to the user
code section. Since the functions are called from the actions of the
pattern rules, we need to provide forward declarations.

Pure code movement, no actual changes.

config_file.l

index b6eff7cc5bd09ae4f4d257ee76be4a4d9279e99e..f6fff31e171fc5ba0f2b2cfd47fd41f133fe3e29 100644 (file)
@@ -31,100 +31,9 @@ OPTION [a-zA-Z]+[a-zA-Z0-9_-]*
        static char **rargv;
        static const char *subcommand;
 
+       static int add_option(void);
+       static int parse_arg(char **result);
        static int yywrap(void) {return 1;}
-       static int expand_result(void)
-       {
-               int nargc = rargc + 1;
-               char **nrargv = realloc(rargv, (nargc + 1) * sizeof(char *));
-
-               if (!nrargv)
-                       return -E_LLS_NOMEM;
-               rargc = nargc;
-               rargv = nrargv;
-               rargv[rargc] = NULL;
-               return 1;
-       }
-
-       static int add_option(void)
-       {
-               int ret;
-               unsigned n;
-
-               for (n = 0; n < yyleng; n++)
-                       if (!isalnum(yytext[n]) && !(yytext[n] == '-'))
-                               break;
-               assert(n > 0);
-               ret = expand_result();
-               if (ret < 0)
-                       return ret;
-               rargv[rargc - 1] = malloc(n + 2 + 1);
-               if (!rargv[rargc - 1])
-                       return -E_LLS_NOMEM;
-               rargv[rargc - 1][0] = rargv[rargc - 1][1] = '-';
-               memcpy(rargv[rargc - 1] + 2, yytext, n);
-               rargv[rargc - 1][n + 2] = '\0';
-               return 1;
-       }
-
-       static int parse_arg(char **result)
-       {
-               bool backslash = false, quote = false;
-               const char *in;
-               char *out;
-               int ret;
-
-               *result = malloc(yyleng + 1);
-               if (!*result)
-                       return -E_LLS_NOMEM;
-               for (in = yytext, out = *result; *in; in++) {
-                       if (*in == '\\') {
-                               if (!backslash) {
-                                       backslash = true;
-                                       continue;
-                               }
-                       } else if (*in == 'n' || *in == 't') {
-                               if (backslash) { /* \n or \t */
-                                       *out++ = (*in == 'n')? '\n' : '\t';
-                                       backslash = false;
-                                       continue;
-                               }
-                       } else if (*in == '"') {
-                               if (!backslash) {
-                                       quote = !quote;
-                                       continue;
-                               }
-                       } else if (isspace(*in)) {
-                               if (!backslash && !quote)
-                                       break;
-                       }
-                       /* copy the character */
-                       *out++ = *in;
-                       backslash = false;
-               }
-               ret = -E_LLS_TRAILING_BACKSLASH;
-               if (backslash)
-                       goto fail;
-               ret = -E_LLS_UNMATCHED_QUOTE;
-               if (quote)
-                       goto fail;
-               /* look at first non-space character */
-               for (; *in; in++) {
-                       if (isspace(*in))
-                               continue;
-                       if (*in == '#')
-                               break;
-                       ret = -E_LLS_TRAILING_GARBAGE;
-                       goto fail;
-               }
-               /* success */
-               *out = '\0';
-               return out - *result;
-       fail:
-               assert(ret < 0);
-               free(*result);
-               *result = NULL;
-               return ret;
-       }
 %}
 
 %%
@@ -196,6 +105,100 @@ OPTION [a-zA-Z]+[a-zA-Z0-9_-]*
 #include <fcntl.h>
 #include <stdio.h>
 
+static int expand_result(void)
+{
+       int nargc = rargc + 1;
+       char **nrargv = realloc(rargv, (nargc + 1) * sizeof(char *));
+
+       if (!nrargv)
+               return -E_LLS_NOMEM;
+       rargc = nargc;
+       rargv = nrargv;
+       rargv[rargc] = NULL;
+       return 1;
+}
+
+static int add_option(void)
+{
+       int ret;
+       unsigned n;
+
+       for (n = 0; n < yyleng; n++)
+               if (!isalnum(yytext[n]) && !(yytext[n] == '-'))
+                       break;
+       assert(n > 0);
+       ret = expand_result();
+       if (ret < 0)
+               return ret;
+       rargv[rargc - 1] = malloc(n + 2 + 1);
+       if (!rargv[rargc - 1])
+               return -E_LLS_NOMEM;
+       rargv[rargc - 1][0] = rargv[rargc - 1][1] = '-';
+       memcpy(rargv[rargc - 1] + 2, yytext, n);
+       rargv[rargc - 1][n + 2] = '\0';
+       return 1;
+}
+
+static int parse_arg(char **result)
+{
+       bool backslash = false, quote = false;
+       const char *in;
+       char *out;
+       int ret;
+
+       *result = malloc(yyleng + 1);
+       if (!*result)
+               return -E_LLS_NOMEM;
+       for (in = yytext, out = *result; *in; in++) {
+               if (*in == '\\') {
+                       if (!backslash) {
+                               backslash = true;
+                               continue;
+                       }
+               } else if (*in == 'n' || *in == 't') {
+                       if (backslash) { /* \n or \t */
+                               *out++ = (*in == 'n')? '\n' : '\t';
+                               backslash = false;
+                               continue;
+                       }
+               } else if (*in == '"') {
+                       if (!backslash) {
+                               quote = !quote;
+                               continue;
+                       }
+               } else if (isspace(*in)) {
+                       if (!backslash && !quote)
+                               break;
+               }
+               /* copy the character */
+               *out++ = *in;
+               backslash = false;
+       }
+       ret = -E_LLS_TRAILING_BACKSLASH;
+       if (backslash)
+               goto fail;
+       ret = -E_LLS_UNMATCHED_QUOTE;
+       if (quote)
+               goto fail;
+       /* look at first non-space character */
+       for (; *in; in++) {
+               if (isspace(*in))
+                       continue;
+               if (*in == '#')
+                       break;
+               ret = -E_LLS_TRAILING_GARBAGE;
+               goto fail;
+       }
+       /* success */
+       *out = '\0';
+       return out - *result;
+fail:
+       assert(ret < 0);
+       free(*result);
+       *result = NULL;
+       return ret;
+}
+
 int lls_convert_config(const char *buf, size_t nbytes, const char *subcmd,
                 char ***result, char **errctx)
 {