/* * Copyright (C) 2016 Andre Noll * * Licensed under the LGPL v3, see http://www.gnu.org/licenses/lgpl-3.0.html */ %option stack %option never-interactive %option yylineno %x SC_ARG %s SC_SCANNING IDENTIFIER [a-zA-Z]+[a-zA-Z0-9_-]* EQUALS [[:space:]]*=[[:space:]]* OPTION [a-zA-Z]+[a-zA-Z0-9_-]* %{ #include #include #include #include #include #include "lopsub-internal.h" #include "lopsub.h" static int rargc; static char **rargv; static const char *subcommand; 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; } %} %% /* skip comments and whitespace */ ^[[:space:]]*#.*\n ; [[:space:]]|\n+ ; \[[[:space:]]*{IDENTIFIER}[[:space:]]*\][[:space:]]*\n { int i, j; assert(yytext[0] == '['); if (!subcommand) return 0; for (i = 1; i < yyleng; i++) if (!isspace(yytext[i])) break; for (j = i; j < yyleng; j++) if (yytext[j] == ']' || isspace(yytext[j])) break; assert(j < yyleng); yytext[j] = '\0'; if (strcmp(yytext + i, subcommand)) BEGIN(INITIAL); else BEGIN(SC_SCANNING); } {OPTION}[[:space:]]*\n add_option(); {OPTION}({EQUALS}|[[:space:]]+) { int ret = add_option(); if (ret < 0) return ret; BEGIN(SC_ARG); } .*\n { const char *opt = rargv[rargc - 1]; char *arg, *result; size_t opt_len = strlen(opt), arg_len; int ret = parse_arg(&arg); if (ret < 0) return ret; arg_len = ret; result = malloc(opt_len + arg_len + 2); if (!result) return -E_LLS_NOMEM; strcpy(result, opt); result[opt_len] = '='; strcpy(result + opt_len + 1, arg); free(arg); result[opt_len + arg_len + 1] = '\0'; free(rargv[rargc - 1]); rargv[rargc - 1] = result; BEGIN(SC_SCANNING); } .*\n {} /* This rule runs iff none of the above patterns matched */ . {return -1;} %% #include #include #include #include int lls_convert_config(const char *buf, size_t nbytes, const char *subcmd, char ***result, char **errctx) { int ret; YY_BUFFER_STATE yybs; *result = NULL; if (errctx) *errctx = NULL; subcommand = subcmd; if (!subcmd) BEGIN(SC_SCANNING); else BEGIN(INITIAL); yybs = yy_scan_bytes(buf, nbytes); if (!yybs) return -E_LLS_YY_SCAN; rargc = 1; rargv = malloc((rargc + 1) * sizeof(char *)); if (!rargv) return -E_LLS_NOMEM; rargv[0] = strdup(__FUNCTION__); if (!rargv[0]) { free(rargv); return -E_LLS_NOMEM; } rargv[1] = NULL; ret = yylex(); yy_delete_buffer(yybs); if (ret >= 0) { *result = rargv; return rargc; } if (errctx) { *errctx = malloc(100); if (*errctx) sprintf(*errctx, "error at line %d", yyget_lineno()); } for (; rargc >= 0; rargc--) free(rargv[rargc]); free(rargv); *result = NULL; return -E_LLS_YY_LEX; } void lls_free_argv(char **argv) { int i; if (!argv) return; for (i = 0; argv[i]; i++) free(argv[i]); free(argv); } #if 0 int main(void) { char buf[100 * 1024]; int ret, len, i, argc; char **argv; ret = read(STDIN_FILENO, buf, sizeof(buf)); if (ret <= 0) exit(EXIT_FAILURE); len = ret; ret = lls_convert_config(buf, len, NULL, &argv, NULL); if (ret < 0) exit(EXIT_FAILURE); argc = ret; for (i = 0; i < argc; i++) printf("argv[%d]: %s\n", i, rargv[i]); return EXIT_SUCCESS; } #endif