1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
/* configparser.l
Parser for configuration file
(C) 1999 Stanislav Meduna <stano@eunet.sk>
The format of the file is
[section]
entry = value...
entry[qualif] = value...
The parser is rather primitive and takes all values
as simple strings - no quoting or something like that
is possible.
The caller gets the entries via callback.
*/
string \"[^\n"]+\"
name [A-Za-z/_][A-Za-z0-9/_()]*
combegin "#"|"//"
lbracket "["
rbracket "]"
equal "="
white [[:blank:]]
%s COMMENT ENTRY VALUES
%option prefix="configparser"
%option outfile="lex.yy.c"
%option noyywrap
%option yylineno
%{
#include <config.h>
#include <configuration.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <libintl.h>
#define _(s) dgettext(PACKAGE, s)
static char section[32];
static config_entry_t *entry = 0;
static void reset_entry(void)
{
free_config_entry(entry);
entry = 0;
}
static void got_section(const char *str)
{
str++;
strncpy(section, str, sizeof(section)-1);
section[sizeof(section)-1] = 0;
section[strlen(section)-1] = 0;
}
static void got_entry(const char *str)
{
entry = (config_entry_t *) malloc(sizeof(config_entry_t));
if (entry == NULL)
{
fprintf(stderr, _("Out of memory"));
return;
}
strncpy(entry->section, section, sizeof(entry->section)-1);
entry->section[sizeof(entry->section)-1] = 0;
strncpy(entry->entry, str, sizeof(entry->entry)-1);
entry->entry[sizeof(entry->entry)-1] = 0;
*entry->qualif = 0;
entry->nvalues = 0;
}
static void got_optional(const char *str)
{
if (! entry)
return;
if (*entry->qualif != 0)
{
fprintf(stderr, _("Second qualifier ignored on line %d\n"), yylineno);
return;
}
str++;
strncpy(entry->qualif, str, sizeof(entry->qualif)-1);
entry->qualif[sizeof(entry->qualif)-1] = 0;
entry->qualif[strlen(entry->qualif)-1] = 0;
}
static void got_value(const char *str)
{
int n;
if (! entry)
return;
if (entry->nvalues >= sizeof(entry->values)/sizeof(entry->values[0]))
{
fprintf(stderr, _("Excessive values ignored on line %d\n"), yylineno);
return;
}
if (*str == '\"')
str++; /* skip quote */
n = strlen(str);
if (n)
n--;
entry->values[entry->nvalues] = strdup(str);
if (entry->values[entry->nvalues] == NULL)
{
fprintf(stderr, _("Out of memory"));
return;
}
if (str[n] == '\"')
entry->values[entry->nvalues][n] = 0;
entry->nvalues++;
}
static void got_all(void)
{
if (! entry)
return;
config_entry_cb(entry);
entry = 0;
}
static void syntax_error()
{
fprintf(stderr, _("Syntax error in configuration at line %d\n"), yylineno);
reset_entry();
}
%}
%%
{ BEGIN(INITIAL); entry = 0; *section = 0; }
<INITIAL>{combegin} { BEGIN(COMMENT); }
<INITIAL>{white} ;
<INITIAL>\n ;
<INITIAL>{lbracket}{name}{rbracket} { got_section(yytext); BEGIN(COMMENT); }
<INITIAL>{name} { got_entry(yytext); BEGIN(ENTRY); }
<INITIAL>. { syntax_error(); BEGIN(COMMENT); }
<ENTRY>{lbracket}{name}{rbracket} { got_optional(yytext); }
<ENTRY>{equal} { BEGIN(VALUES); }
<ENTRY>{white} ;
<ENTRY>. { syntax_error(); BEGIN(COMMENT); }
<ENTRY>\n { syntax_error(); BEGIN(INITIAL); }
<VALUES>\n { got_all(); BEGIN(INITIAL); }
<VALUES>{white} ;
<VALUES>{name} { got_value(yytext); }
<VALUES>{string} { got_value(yytext); }
<VALUES>. { syntax_error(); BEGIN(COMMENT); }
<COMMENT>. ;
<COMMENT>\n { reset_entry(); BEGIN(INITIAL); }
%%
|