Blame view

libmu_sieve/sieve.y 13.4 KB
1
%{
Wojciech Polak authored
2
/* GNU Mailutils -- a suite of utilities for electronic mail
3 4
   Copyright (C) 1999, 2000, 2001, 2002, 2005, 2006, 2007, 2008, 2009,
   2010 Free Software Foundation, Inc.
5

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

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

16
   You should have received a copy of the GNU Lesser General
17 18
   Public License along with this library.  If not, see
   <http://www.gnu.org/licenses/>. */
19 20 21 22 23

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif  
#include <stdio.h>
24
#include <stdlib.h>
25
#include <assert.h>
Wojciech Polak authored
26
#include <sieve-priv.h>
27

28 29
mu_sieve_machine_t mu_sieve_machine;
int mu_sieve_error_count;
30

31
static void branch_fixup (size_t start, size_t end);
32 33
%}

34 35
%union {
  char *string;
36
  size_t number;
37
  sieve_instr_t instr;
38
  mu_sieve_value_t *value;
39
  mu_list_t list;
40
  size_t pc;
41
  struct {
42 43 44 45
    size_t start;
    size_t end;
  } pclist;
  struct {
46
    char *ident;
47
    mu_list_t args;
48
  } command;
49 50 51 52 53
  struct {
    size_t begin;
    size_t cond;
    size_t branch;
  } branch;
54 55 56 57 58 59 60 61
}

%token <string> IDENT TAG
%token <number> NUMBER
%token <string> STRING MULTILINE
%token REQUIRE IF ELSIF ELSE ANYOF ALLOF NOT

%type <value> arg
62
%type <list> slist stringlist stringorlist arglist maybe_arglist
63
%type <command> command
64
%type <pclist> testlist
65 66
%type <pc> action test statement list elsif else cond begin if block
%type <branch> elsif_branch maybe_elsif else_part
67

68 69 70 71
%%

input        : /* empty */
             | list
72
               { /* to placate bison */ }
73 74 75 76 77 78
             ;

list         : statement
             | list statement
             ;

79
statement    : REQUIRE stringorlist ';'
80
               {
81
		 mu_sieve_require ($2);
82 83
		 /*  All the items in $2 are registered in memory_pool,
		     so we don't free them */
84
		 mu_list_destroy (&$2);
85
		 $$ = mu_sieve_machine->pc;
86
	       }
87
             | action ';'
88 89 90
	     /* 1  2     3       4    */ 
             | if cond block else_part
               {
91
		 mu_sieve_machine->prog[$2].pc = $4.begin - $2 - 1;
92
		 if ($4.branch)
93
		   branch_fixup ($4.branch, mu_sieve_machine->pc);
94 95 96 97
	       }		 
             ;

if           : IF
98
               {
99
		 $$ = mu_sieve_machine->pc;
100
	       }
101 102
             ;

103 104 105
else_part    : maybe_elsif
               {
		 if ($1.begin)
106 107
		   mu_sieve_machine->prog[$1.cond].pc =
		                  mu_sieve_machine->pc - $1.cond - 1;
108 109
		 else
		   {
110
		     $$.begin = mu_sieve_machine->pc;
111 112 113 114 115 116 117
		     $$.branch = 0;
		   }
	       }
             | maybe_elsif else block
               {
		 if ($1.begin)
		   {
118 119
		     mu_sieve_machine->prog[$1.cond].pc = $3 - $1.cond - 1;
		     mu_sieve_machine->prog[$2].pc = $1.branch;
120 121 122 123 124 125 126 127 128 129 130
		     $$.begin = $1.begin;
		     $$.branch = $2;
		   }
		 else
		   {
		     $$.begin = $3;
		     $$.branch = $2;
		   }
	       }
             ;

131
maybe_elsif  : /* empty */
132 133 134 135 136 137 138 139 140 141 142 143 144 145
               {
		 $$.begin = 0;
	       }
             | elsif_branch
             ;

elsif_branch : elsif begin cond block
               {
		 $$.begin = $2; 
		 $$.branch = $1;
		 $$.cond = $3;
	       }
             | elsif_branch elsif begin cond block
               {
146 147
		 mu_sieve_machine->prog[$1.cond].pc = $3 - $1.cond - 1;
		 mu_sieve_machine->prog[$2].pc = $1.branch;
148 149 150 151
		 $$.begin = $1.begin;
		 $$.branch = $2;
		 $$.cond = $4;
	       }
152 153
             ;

154 155
elsif        : ELSIF
               {
156 157 158
		 mu_sv_code_instr (_mu_sv_instr_branch);
		 $$ = mu_sieve_machine->pc;
		 mu_sv_code_number (0);
159
	       }
160 161
             ;

162 163
else         : ELSE
               {
164 165 166
		 mu_sv_code_instr (_mu_sv_instr_branch);
		 $$ = mu_sieve_machine->pc;
		 mu_sv_code_number (0);
167
	       }
168 169 170
             ;

block        : '{' list '}'
171 172 173
               {
		 $$ = $2;
	       }
174 175
             ;

176
testlist     : cond_expr
177
               {
178 179 180
		 $$.start = $$.end = mu_sieve_machine->pc;
		 if (mu_sv_code_instr (_mu_sv_instr_brz)
		     || mu_sv_code_number (0))
181 182
		   YYERROR;
	       }
183
             | testlist ',' cond_expr
184
               {
185 186 187 188
		 mu_sieve_machine->prog[$1.end+1].pc = mu_sieve_machine->pc;
		 $1.end = mu_sieve_machine->pc;
		 if (mu_sv_code_instr (_mu_sv_instr_brz)
		     || mu_sv_code_number (0))
189
		   YYERROR;
190
		 $$ = $1;
191
	       }
192 193
             ;

194 195
cond         : cond_expr
               {
196 197 198
		 mu_sv_code_instr (_mu_sv_instr_brz);
		 $$ = mu_sieve_machine->pc;
		 mu_sv_code_number (0);
199 200 201 202
	       }
             ;

cond_expr    : test
203
               { /* to placate bison */ }
204
             | ANYOF '(' testlist ')'
205
               {
206
		 mu_sv_code_anyof ($3.start);
207
	       }
208
             | ALLOF '(' testlist ')'
209
               {
210
		 mu_sv_code_allof ($3.start);
211
	       }
212
             | NOT cond_expr
213
               {
214
		 if (mu_sv_code_instr (_mu_sv_instr_not))
215 216
		   YYERROR;
	       }
217 218
             ;

219 220
begin        : /* empty */
               {
221
		 $$ = mu_sieve_machine->pc;
222 223 224
	       }
             ; 

225 226
test         : command
               {
227 228 229
		 mu_sieve_register_t *reg = 
		        mu_sieve_test_lookup (mu_sieve_machine, $1.ident);
		 $$ = mu_sieve_machine->pc;
230

231
		 if (!reg)
232
		   mu_sv_compile_error (&mu_sieve_locus,
233 234
					_("unknown test: %s"),
					$1.ident);
235
		 else if (!reg->required)
236
		   mu_sv_compile_error (&mu_sieve_locus,
237 238
					_("test `%s' has not been required"),
					$1.ident);
239
		 else if (mu_sv_code_test (reg, $1.args))
240
		   YYERROR;
241
	       }
242 243 244
             ;

command      : IDENT maybe_arglist
245 246 247 248
               {
		 $$.ident = $1;
		 $$.args = $2;
	       }
249 250 251
             ;

action       : command
252
               {
253 254
		 mu_sieve_register_t *reg = 
		        mu_sieve_action_lookup (mu_sieve_machine, $1.ident);
255
		 
256
		 $$ = mu_sieve_machine->pc;
257
		 if (!reg)
258
		   mu_sv_compile_error (&mu_sieve_locus,
259 260
					_("unknown action: %s"),
					$1.ident);
261
		 else if (!reg->required)
262
		   mu_sv_compile_error (&mu_sieve_locus,
263 264
					_("action `%s' has not been required"),
					$1.ident);
265
		 else if (mu_sv_code_action (reg, $1.args))
266
		   YYERROR;
267
	       }
268 269 270
             ;

maybe_arglist: /* empty */
271 272 273
               {
		 $$ = NULL;
	       }
274 275 276 277
             | arglist
	     ;

arglist      : arg
278
               {
279 280
		 mu_list_create (&$$);
		 mu_list_append ($$, $1);
281
	       }		 
282
             | arglist arg
283
               {
284
		 mu_list_append ($1, $2);
285 286
		 $$ = $1;
	       }
287 288 289
             ;

arg          : stringlist
290
               {
291
		 $$ = mu_sieve_value_create (SVT_STRING_LIST, $1);
292
	       }
293 294
             | STRING
               {
295
		 $$ = mu_sieve_value_create (SVT_STRING, $1);
296
               } 
297
             | MULTILINE
298
               {
299
		 $$ = mu_sieve_value_create (SVT_STRING, $1);
300
	       }
301
             | NUMBER
302
               {
303
		 $$ = mu_sieve_value_create (SVT_NUMBER, &$1);
304
	       }
305
             | TAG
306
               {
307
		 $$ = mu_sieve_value_create (SVT_TAG, $1);
308
	       }
309
             ;
310 311

stringorlist : STRING
312
               {
313 314
		 mu_list_create (&$$);
		 mu_list_append ($$, $1);
315
	       }
316 317 318 319
             | stringlist
             ;

stringlist   : '[' slist ']'
320 321 322
               {
		 $$ = $2;
	       }
323 324 325
             ;

slist        : STRING
326
               {
327 328
		 mu_list_create (&$$);
		 mu_list_append ($$, $1);
329
	       }
330
             | slist ',' STRING
331
               {
332
		 mu_list_append ($1, $3);
333 334
		 $$ = $1;
	       }
335 336 337 338 339
             ;

%%

int
340
yyerror (const char *s)
341
{
342
  mu_sv_compile_error (&mu_sieve_locus, "%s", s);
343
  return 0;
344 345
}

346
int
347 348
mu_sieve_machine_init_ex (mu_sieve_machine_t *pmach,
			  void *data, mu_stream_t errstream)
349
{
350
  int rc;
351
  mu_sieve_machine_t mach;
352 353 354 355
  
  mach = malloc (sizeof (*mach));
  if (!mach)
    return ENOMEM;
356
  memset (mach, 0, sizeof (*mach));
357
  rc = mu_list_create (&mach->memory_pool);
358 359 360
  if (rc)
    {
      free (mach);
361
      return rc;
362
    }
363

364 365 366
  mach->data = data;
  mach->errstream = errstream;
  mu_stream_ref (mu_strerr);
367
  
368 369
  *pmach = mach;
  return 0;
370 371
}

372
int
373 374 375 376 377 378
mu_sieve_machine_init (mu_sieve_machine_t *pmach)
{
  return mu_sieve_machine_init_ex (pmach, NULL, mu_strerr);
}

int
379 380
mu_sieve_machine_inherit (mu_sieve_machine_t const parent,
			  mu_sieve_machine_t *pmach)
381
{
382 383 384
  mu_sieve_machine_t child;
  int rc;
  
385
  rc = mu_sieve_machine_init_ex (&child, parent->data, parent->errstream);
386 387
  if (rc)
    return rc;
388

389 390
  child->logger = parent->logger;
  child->debug_level = parent->debug_level;
391 392
  *pmach = child;
  return 0;
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
}

int
mu_sieve_machine_dup (mu_sieve_machine_t const in, mu_sieve_machine_t *out)
{
  int rc;
  mu_sieve_machine_t mach;
  
  mach = malloc (sizeof (*mach));
  if (!mach)
    return ENOMEM;
  memset (mach, 0, sizeof (*mach));
  rc = mu_list_create (&mach->memory_pool);
  if (rc)
    {
      free (mach);
      return rc;
    }
  mach->destr_list = NULL;
  mach->test_list = NULL;
  mach->action_list = NULL;
  mach->comp_list = NULL;

  mach->progsize = in->progsize;
  mach->prog = in->prog;

  mach->pc = 0;
  mach->reg = 0;
  mach->stack = NULL;

  mach->debug_level = in->debug_level;
  
425 426 427
  mach->errstream = in->errstream;
  mu_stream_ref (mach->errstream);
  
428 429 430 431 432 433 434 435 436
  mach->data = in->data;
  mach->logger = in->logger;
  mach->daemon_email = in->daemon_email;

  *out = mach;
  return 0;
}

void
437
mu_sieve_get_diag_stream (mu_sieve_machine_t mach, mu_stream_t *pstr)
438
{
439 440
  *pstr = mach->errstream;
  mu_stream_ref (*pstr);
441 442 443
}

void
444
mu_sieve_set_diag_stream (mu_sieve_machine_t mach, mu_stream_t str)
445
{
446 447 448
  mu_stream_unref (mach->errstream);
  mach->errstream = str;
  mu_stream_ref (mach->errstream);
449 450 451 452 453
}

void
mu_sieve_set_debug_level (mu_sieve_machine_t mach, int level)
{
454 455 456
  mach->debug_level = level;
}

457
void
458
mu_sieve_set_logger (mu_sieve_machine_t mach, mu_sieve_action_log_t logger)
459 460 461 462
{
  mach->logger = logger;
}

463
mu_mailer_t
464
mu_sieve_get_mailer (mu_sieve_machine_t mach)
465 466
{
  if (!mach->mailer)
467
    mu_mailer_create (&mach->mailer, NULL);
468 469 470 471
  return mach->mailer;
}

void
472
mu_sieve_set_mailer (mu_sieve_machine_t mach, mu_mailer_t mailer)
473
{
474
  mu_mailer_destroy (&mach->mailer);
475 476 477 478 479 480
  mach->mailer = mailer;
}

#define MAILER_DAEMON_PFX "MAILER-DAEMON@"

char *
481
mu_sieve_get_daemon_email (mu_sieve_machine_t mach)
482 483 484 485 486 487
{
  if (!mach->daemon_email)
    {
      const char *domain = NULL;
      
      mu_get_user_email_domain (&domain);
488
      mach->daemon_email = mu_sieve_malloc (mach,
489 490
					    sizeof(MAILER_DAEMON_PFX) +
					    strlen (domain));
491 492 493 494 495 496
      sprintf (mach->daemon_email, "%s%s", MAILER_DAEMON_PFX, domain);
    }
  return mach->daemon_email;
}

void
497
mu_sieve_set_daemon_email (mu_sieve_machine_t mach, const char *email)
498
{
499 500
  mu_sieve_mfree (mach, (void *)mach->daemon_email);
  mach->daemon_email = mu_sieve_mstrdup (mach, email);
501 502
}

503 504
struct sieve_destr_record
{
505
  mu_sieve_destructor_t destr;
506 507 508 509
  void *ptr;
};

int
510 511 512
mu_sieve_machine_add_destructor (mu_sieve_machine_t mach,
				 mu_sieve_destructor_t destr,
				 void *ptr)
513 514
{
  struct sieve_destr_record *p;
515

516
  if (!mach->destr_list && mu_list_create (&mach->destr_list))
517
    return 1;
518
  p = mu_sieve_malloc (mach, sizeof (*p));
519 520 521 522
  if (!p)
    return 1;
  p->destr = destr;
  p->ptr = ptr;
523
  return mu_list_prepend (mach->destr_list, p);
524 525 526 527 528 529 530 531 532 533 534
}

static int
_run_destructor (void *data, void *unused)
{
  struct sieve_destr_record *p = data;
  p->destr (p->ptr);
  return 0;
}

void
535
mu_sieve_machine_destroy (mu_sieve_machine_t *pmach)
536
{
537
  mu_sieve_machine_t mach = *pmach;
538
  /* FIXME: Restore stream state (locus & mode) */
539 540
  mu_stream_ioctl (mach->errstream, MU_IOCTL_LOGSTREAM,
                   MU_IOCTL_LOGSTREAM_SET_LOCUS, NULL);
541
  mu_stream_destroy (&mach->errstream);
542
  mu_mailer_destroy (&mach->mailer);
543 544 545 546 547 548 549
  mu_list_do (mach->destr_list, _run_destructor, NULL);
  mu_list_destroy (&mach->destr_list);
  mu_list_destroy (&mach->action_list);
  mu_list_destroy (&mach->test_list);
  mu_list_destroy (&mach->comp_list);
  mu_list_destroy (&mach->source_list);
  mu_sieve_slist_destroy (&mach->memory_pool);
550 551 552 553
  free (mach);
  *pmach = NULL;
}

554 555 556 557 558 559
static int
string_comp (const void *item, const void *value)
{
  return strcmp (item, value);
}

560
void
561
mu_sieve_machine_begin (mu_sieve_machine_t mach, const char *file)
562
{
563 564 565
  mu_sieve_machine = mach;
  mu_sieve_error_count = 0;
  mu_sv_code_instr (NULL);
566

567 568
  mu_list_create (&mach->source_list);
  mu_list_set_comparator (mach->source_list, string_comp);
569
  
570 571 572
  mu_sv_register_standard_actions (mach);
  mu_sv_register_standard_tests (mach);
  mu_sv_register_standard_comparators (mach);
573 574 575
}

void
576
mu_sieve_machine_finish (mu_sieve_machine_t mach)
577
{
578
  mu_sv_code_instr (NULL);
579 580
}

581
int
582
mu_sieve_compile (mu_sieve_machine_t mach, const char *name)
583
{
584 585
  int rc;
  
586
  mu_sieve_machine_begin (mach, name);
587

588
  if (mu_sv_lex_begin (name) == 0)
589
    {
590
      rc = yyparse ();
591
      if (mu_sieve_error_count)
592
	rc = 1;
593
      mu_sv_lex_finish ();
594 595 596
    }
  else
    rc = 1;
597
  
598
  mu_sieve_machine_finish (mach);
599
  return rc;
600 601
}

602 603
int
mu_sieve_compile_buffer (mu_sieve_machine_t mach,
604 605
			 const char *buf, int bufsize,
			 const char *fname, int line)
606 607 608 609 610
{
  int rc;
  
  mu_sieve_machine_begin (mach, fname);

611
  if (mu_sv_lex_begin_string (buf, bufsize, fname, line) == 0)
612 613
    {
      rc = yyparse ();
614
      if (mu_sieve_error_count)
615
	rc = 1;
616
      mu_sv_lex_finish ();
617 618 619 620 621 622 623 624
    }
  else
    rc = 1;
  
  mu_sieve_machine_finish (mach);
  return rc;
}

625 626 627
static void
_branch_fixup (size_t start, size_t end)
{
628
  size_t prev = mu_sieve_machine->prog[start].pc;
629 630 631
  if (!prev)
    return;
  branch_fixup (prev, end);
632
  mu_sieve_machine->prog[prev].pc = end - prev - 1;
633 634
}

635
static void
636 637 638
branch_fixup (size_t start, size_t end)
{
  _branch_fixup (start, end);
639 640 641 642 643
  mu_sieve_machine->prog[start].pc = end - start - 1;
}