Blame view

mailbox/cfg_lexer.l 9.53 KB
Sergey Poznyakoff authored
1
%top {
2
/* cfg_lexer.l -- default lexer for Mailutils configuration files
3
   Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

   GNU Mailutils is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 3, or (at
   your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
Sergey Poznyakoff authored
21 22 23
}

%{
24 25 26 27 28 29 30
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
31
#include <mailutils/cctype.h>
32 33 34 35 36 37 38 39
#include <mailutils/errno.h>
#include <mailutils/error.h>
#include <mailutils/debug.h>  
#include <mailutils/argcv.h>
#include <mailutils/alloc.h>  
#include <mailutils/nls.h>
#include <mailutils/cfg.h>
#include <mailutils/list.h>
40 41
#include <mailutils/mutil.h>
  
42 43 44 45 46 47
#include "cfg_parser.h"

void _mu_line_begin (void);
void _mu_line_add (char *text, size_t len);
char *_mu_line_finish (void);

48 49 50 51 52 53 54 55
extern void mu_cfg_set_debug (void);
static void
mu_cfg_set_lex_debug (void)
{
  yy_flex_debug = mu_debug_check_level (mu_cfg_get_debug (),
					MU_DEBUG_TRACE2);
}
 
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
static void _mu_line_add_unescape_last (char *text, size_t len);
static void multiline_begin (char *p);
static char *multiline_strip_tabs (char *text);
static void multiline_add (char *s);
static char *multiline_finish (void);
 
static char *multiline_delimiter;
static size_t multiline_delimiter_len;
static int multiline_unescape;         /* Unescape here-document contents */
static int (*char_to_strip)(char);     /* Strip matching characters of each
					  here-document line */
static int isemptystr(int off);

static mu_opool_t pool;

%}

%x COMMENT ML STR

WS [ \t\f][ \t\f]*
ID [a-zA-Z_][a-zA-Z_0-9-]+
P [1-9][0-9]*

%%
         /* C-style comments */
"/*"         BEGIN(COMMENT);
<COMMENT>[^*\n]*        /* eat anything that's not a '*' */
<COMMENT>"*"+[^*/\n]*   /* eat up '*'s not followed by '/'s */
<COMMENT>\n             ++mu_cfg_locus.line;
<COMMENT>"*"+"/"        BEGIN (INITIAL);
         /* End-of-line comments */
87 88 89 90 91 92 93 94 95 96
#debug=.*\n {
          mu_log_level_t lev;
          mu_debug_t dbg = mu_cfg_get_debug ();
          if (mu_debug_level_from_string (yytext + 7, &lev, dbg) == 0)
            {
	      mu_debug_set_level (dbg, lev);
	      mu_cfg_set_debug ();
	      mu_cfg_set_lex_debug ();
	    }
          }
97 98 99 100 101 102 103 104 105 106 107
#.*\n     { mu_cfg_locus.line++; }
#.*     /* end-of-file comment */;
"//".*\n  { mu_cfg_locus.line++; }
"//".*    /* end-of-file comment */;
        /* Identifiers */
<INITIAL>{ID}         {
                        _mu_line_begin ();
			_mu_line_add (yytext, yyleng);
			yylval.string = _mu_line_finish ();
			return MU_TOK_IDENT; }
         /* Strings */
Sergey Poznyakoff authored
108
[a-zA-Z0-9_\./:\*=-]+ { _mu_line_begin ();
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
                        _mu_line_add (yytext, yyleng);
                        yylval.string = _mu_line_finish ();
                        return MU_TOK_STRING; }
         /* Quoted strings */
\"[^\\"\n]*\"         { _mu_line_begin ();
                        _mu_line_add (yytext + 1, yyleng - 2);
                        yylval.string = _mu_line_finish ();
                        return MU_TOK_QSTRING; }
\"[^\\"\n]*\\. |
\"[^\\"\n]*\\\n        { BEGIN (STR);
                        _mu_line_begin ();
		        _mu_line_add_unescape_last (yytext + 1, yyleng - 1); }
<STR>[^\\"\n]*\\. |
<STR>\"[^\\"\n]*\\\n  { _mu_line_add_unescape_last (yytext, yyleng); }
<STR>[^\\"\n]*\"      { BEGIN (INITIAL);
                        if (yyleng > 1) 
                          _mu_line_add (yytext, yyleng - 1); 
                        yylval.string = _mu_line_finish ();
		        return MU_TOK_QSTRING; }
         /* Multiline strings */
"<<"(-" "?)?\\?{ID}[ \t]*#.*\n |
"<<"(-" "?)?\\?{ID}[ \t]*"//".*\n |
"<<"(-" "?)?\\?{ID}[ \t]*\n |
"<<"(-" "?)?\"{ID}\"[ \t]*#.*\n |
"<<"(-" "?)?\"{ID}\"[ \t]*"//".*\n |
"<<"(-" "?)?\"{ID}\"[ \t]*\n {
                        BEGIN (ML);
			multiline_begin (yytext+2);
			mu_cfg_locus.line++;
		  }
<ML>.*\n { char *p = multiline_strip_tabs (yytext);
	   
           if (!strncmp (p, multiline_delimiter, multiline_delimiter_len)
	       && isemptystr (p + multiline_delimiter_len - yytext))
	     {
	       free (multiline_delimiter);
	       multiline_delimiter = NULL;
	       BEGIN (INITIAL);
	       yylval.string = multiline_finish ();
	       return MU_TOK_MSTRING;
	     }
           mu_cfg_locus.line++;
	   multiline_add (p); } 
{WS}     ;
         /* Other tokens */
\n       { mu_cfg_locus.line++; } 
[,;{}()] return yytext[0];
156
.        { if (mu_isprint (yytext[0]))
157
              mu_cfg_parse_error (_("stray character %c"), yytext[0]);
Sergey Poznyakoff authored
158
           else 
159 160
              mu_cfg_parse_error (_("stray character \\%03o"),
				  (unsigned char) yytext[0]);
Sergey Poznyakoff authored
161
         }
162 163 164 165 166 167 168 169 170 171 172 173 174 175
%%

int
yywrap ()
{
  return 1;
}

static void
unescape_to_line (int c)
{
  if (c != '\n')
    {
      char t = mu_argcv_unquote_char (c);
Sergey Poznyakoff authored
176
      if (t == c && t != '\\' && t != '\"')
177
	mu_cfg_parse_error (_("unknown escape sequence '\\%c'"), c);
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
      mu_opool_append_char (pool, t);
    }
}

void
_mu_line_add (char *text, size_t len)
{
  mu_opool_append (pool, text, len);
}

void
_mu_line_add_unescape_last (char *text, size_t len)
{
  mu_opool_append (pool, text, len - 2);
  unescape_to_line (text[len - 1]);
}

void
_mu_line_begin ()
{
  if (!pool)
    mu_opool_create (&pool, 1);
  else
    mu_opool_clear (pool);
}

char *
_mu_line_finish ()
{
  mu_opool_append_char (pool, 0);
  return mu_opool_finish (pool, NULL);
}



static int
is_tab (char c)
{
    return c == '\t';
}
 
static int
is_ws (char c)
{
    return c == '\t' || c == ' ';
}

static int
isemptystr (int off)
{
228
  for (; yytext[off] && mu_isspace (yytext[off]); off++)
229 230 231 232 233
    ;
  if (yytext[off] == ';')
    {
      int i;
      for (i = off + 1; yytext[i]; i++) 
234
	if (!mu_isspace (yytext[i]))
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
	  return 0;
      yyless (off);
      return 1;
    }
  return yytext[off] == 0;
}

static void
multiline_begin (char *p)
{
  if (*p == '-')
    {
      if (*++p == ' ')
	{
	  char_to_strip = is_ws;
	  p++;
	}
      else
	char_to_strip = is_tab;
    }
  else
    char_to_strip = NULL;
  if (*p == '\\')
    {
      p++;
      multiline_unescape = 0;
    }
  else if (*p == '"')
    {
      char *q;
      
      p++;
      multiline_unescape = 0;
      q = strchr (p, '"');
      multiline_delimiter_len = q - p;
    }
  else
    {
	multiline_delimiter_len = strcspn (p, " \t");
	multiline_unescape = 1;
    }

  /* Remove trailing newline */
  multiline_delimiter_len--;
  multiline_delimiter = mu_alloc (multiline_delimiter_len + 1);
  memcpy (multiline_delimiter, p, multiline_delimiter_len);
  multiline_delimiter[multiline_delimiter_len] = 0;
  _mu_line_begin ();
}

static char *
multiline_strip_tabs (char *text)
{
  if (char_to_strip)
    for (; *text && char_to_strip (*text); text++)
      ;
  return text;
}

static void
multiline_add (char *s)
{
  if (multiline_unescape)
    {
      for (; *s; s++)
	{
	  if (*s == '\\')
	    {
	      unescape_to_line (s[1]);
	      ++s;
	    }
	  else
	    _mu_line_add (s, 1);
	}
    }
  else
    _mu_line_add (s, strlen (s));
}

static char *
multiline_finish ()
{
  return _mu_line_finish ();
}


int
322
mu_cfg_parse_file (mu_cfg_tree_t **return_tree, const char *file, int flags)
323 324 325 326
{
  struct stat st;
  FILE *fp;
  int rc;
327 328 329
  char *full_name = mu_tilde_expansion (file, "/", NULL);

  if (stat (full_name, &st))
330 331
    {
      if (errno != ENOENT)
332 333
	mu_error (_("cannot stat `%s': %s"), full_name, mu_strerror (errno));
      free (full_name);
334
      return ENOENT;
335
    }
Sergey Poznyakoff authored
336 337 338 339 340 341 342 343
  else if (!S_ISREG (st.st_mode))
    {
      if (flags & MU_PARSE_CONFIG_VERBOSE)
	mu_diag_output (MU_DIAG_INFO, _("%s: not a regular file"), full_name);
      free (full_name);
      return ENOENT;
    } 
      
344
  fp = fopen (full_name, "r");
345 346
  if (!fp)
    {
347
      mu_error (_("cannot open config file `%s': %s"), full_name,
348
		mu_strerror (errno));
349
      free (full_name);
350
      return errno;
351 352 353
    }

  if (flags & MU_PARSE_CONFIG_VERBOSE)
354
    mu_diag_output (MU_DIAG_INFO, _("parsing file `%s'"), full_name);
355

356
  mu_cfg_set_lex_debug ();
357 358 359 360 361 362 363 364 365 366

  /* Initialize locus: */
  /* 1. Save file name in the lexer object pool and point `file' member
     to this copy. Free full_name: it is not used after that. */
  _mu_line_begin ();
  _mu_line_add (full_name, strlen (full_name));
  mu_cfg_locus.file = _mu_line_finish ();
  free (full_name); 
  /* 2. Initialize line number */
  mu_cfg_locus.line = 1;
367 368 369
  
  /* Parse configuration */
  yyrestart (fp);
370
  rc = mu_cfg_parse (return_tree);
Sergey Poznyakoff authored
371
  fclose (fp);
372
  if (flags & MU_PARSE_CONFIG_VERBOSE)
373 374 375
    mu_diag_output (MU_DIAG_INFO, _("finished parsing file `%s'"),
		    mu_cfg_locus.file);

376 377 378 379 380 381 382 383 384 385
  return rc == 0 ? 0 : MU_ERR_FAILURE;
}

/* FIXME: Deprecated interface */
int
mu_get_config (const char *file, const char *progname,
               struct mu_cfg_param *progparam, int flags, void *target_ptr)
{
  mu_cfg_tree_t *parse_tree;
  int rc = mu_cfg_parse_file (&parse_tree, file, flags);
386
  if (rc == 0)
Sergey Poznyakoff authored
387
    {
388
      rc = mu_cfg_tree_postprocess (parse_tree, flags);
Sergey Poznyakoff authored
389 390 391
      if (rc == 0)
	rc = mu_cfg_tree_reduce (parse_tree, progname, progparam, flags,
				 target_ptr);
392
      mu_cfg_destroy_tree (&parse_tree);
Sergey Poznyakoff authored
393
    }
394

395
  return rc == 0 ? 0 : MU_ERR_FAILURE;
396 397
}

398

399 400 401 402 403 404 405
mu_opool_t 
mu_cfg_lexer_pool ()
{
  mu_opool_t p = pool;
  pool = NULL;
  return p;
}