Blame view

mh/pick.y 8.16 KB
1 2
%{
/* GNU Mailutils -- a suite of utilities for electronic mail
3 4
   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009, 2010 Free Software
   Foundation, Inc.
5 6 7

   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
8
   the Free Software Foundation; either version 3, or (at your option)
9 10 11 12 13 14 15 16
   any later version.

   GNU Mailutils 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
17
   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
18 19 20 21

#include <mh.h>
#include <regex.h>  
#include <pick.h>
22
  
23 24 25 26 27 28 29 30 31 32 33
static node_t *pick_node_create (node_type type, void *a, void *b);
static void set_cflags (char *str);
 
static regex_t *
regex_dup (regex_t *re)
{
  regex_t *rp = xmalloc (sizeof (*rp));
  *rp = *re;
  return rp;
}

34 35 36
int yyerror (const char *s);
int yylex (void); 
 
37 38 39 40 41 42
static node_t *parse_tree;
static int nesting_level;
static int reg_flags = REG_EXTENDED|REG_ICASE;
%}

%token <string> T_COMP T_DATEFIELD  T_STRING T_CFLAGS
43
%token T_LBRACE T_RBRACE T_BEFORE T_AFTER 
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
%left T_OR
%left T_AND
%left T_NOT

%union {
  char *string;
  node_t *node;
  regex_t regex;
};

%type <node> expr exprlist
%type <regex> regex

%%

input    : /* empty */
           {
	     parse_tree = NULL;
	   }
         | exprlist
           {
	     parse_tree = $1;
	   }
         ;

exprlist : expr
         | exprlist expr
           {
	     $$ = pick_node_create (node_and, $1, $2);
	   }
         ;

cflags   : /* empty */
         | T_CFLAGS
           {
	     set_cflags ($1);
	   }
         ;

regex    : cflags T_STRING
           {
	     int rc = regcomp (&$$, $2, reg_flags|REG_NOSUB);
	     if (rc)
	       {
		 char errbuf[512];
		 regerror (rc, &$$, errbuf, sizeof (errbuf));
90
		 mu_error ("error compiling regex \"%s\": %s",
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
			   $2, errbuf);
		 YYERROR;
	       }
	   }
         ;

expr     : lbrace exprlist rbrace
           {
	     $$ = $2;
	   }
         | cflags T_COMP regex
           {
	     $$ = pick_node_create (node_regex, $2, regex_dup (&$3));
	   }		      
         | regex
           {
	     $$ = pick_node_create (node_regex, NULL, regex_dup (&$1));
	   }		      
         | T_DATEFIELD
           {
	     $$ = pick_node_create (node_datefield, $1, NULL);
	   }
         | T_BEFORE T_STRING
           {
115 116 117
	     time_t t;
	     if (mu_parse_date ($2, &t, NULL))
	       {
118
		 mu_error (_("bad date format: %s"), $2);
119 120 121 122
		 exit (1);
	       }
	     $$ = pick_node_create (node_before, NULL, NULL);
	     $$->v.time = t;
123 124 125
	   }
         | T_AFTER T_STRING
           {
126 127 128
	     time_t t;
	     if (mu_parse_date ($2, &t, NULL))
	       {
129
		 mu_error (_("bad date format: %s"), $2);
130 131 132 133
		 exit (1);
	       }
	     $$ = pick_node_create (node_after, NULL, NULL);
	     $$->v.time = t;
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
	   }
         | expr T_AND expr
           {
	     $$ = pick_node_create (node_and, $1, $3);
	   }
         | expr T_OR expr
           {
	     $$ = pick_node_create (node_or, $1, $3);
	   }
	 | T_NOT expr
           {
	     $$ = pick_node_create (node_not, $2, NULL);
	   }
         ;

lbrace   : T_LBRACE
           {
	     nesting_level++;
	   }
         ;

rbrace   : T_RBRACE
           {
	     nesting_level--;
	   }
         ;

%%

/* Lexical analizer */

struct token
{
  int tok;
  char *val;
};

171
static mu_iterator_t iterator;
172 173 174 175 176 177

int
yylex ()
{
  struct token *tok;
  
178
  if (mu_iterator_is_done (iterator))
179
    return 0;
180 181
  mu_iterator_current (iterator, (void **)&tok);
  mu_iterator_next (iterator);
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
  yylval.string = tok->val;
  return tok->tok;
}

static char *
tokname (int tok)
{
  switch (tok)
    {
    case T_DATEFIELD:
      return "--datefield";
      
    case T_BEFORE:
      return "--before";
      
    case T_AFTER:
      return "--after";
      
    case T_LBRACE:
      return "--lbrace";
      
    case T_RBRACE:
      return "--rbrace";
      
    case T_OR:
      return "--or";
      
    case T_AND:
      return "--and";
      
    case T_NOT:
      return "--not";
    }
  return NULL;
}

int
219
yyerror (const char *s)
220 221
{
  int tok = yylex ();
222
  const char *str;
223 224 225 226 227 228 229 230 231
  
  if (!tok)
    str = _("end of input");
  else if (yylval.string)
    str = yylval.string;
  else
    str = tokname (tok);

  if (nesting_level)
232
    mu_error (_("%s near %s (missing closing brace?)"), s, str);
233
  else
234
    mu_error (_("%s near %s"), s, str);
235 236 237 238
  return 0;
}
  
void
239
pick_add_token (mu_list_t *list, int tok, char *val)
240 241 242 243
{
  struct token *tp;
  int rc;
  
244
  if (!*list && (rc = mu_list_create (list)))
245
    {
246
      mu_error(_("cannot create list: %s"), mu_strerror (rc));
247 248 249 250 251
      exit (1);
    }
  tp = xmalloc (sizeof (*tp));
  tp->tok = tok;
  tp->val = val;
252
  mu_list_append (*list, tp);
253 254 255 256
}

/* Main entry point */
int
257
pick_parse (mu_list_t toklist)
258 259 260 261 262 263 264 265 266
{
  int rc;
  
  if (!toklist)
    {
      parse_tree = NULL;
      return 0;
    }

267
  if (mu_list_get_iterator (toklist, &iterator))
268
    return -1;
269
  mu_iterator_first (iterator);
270
  rc = yyparse ();
271
  mu_iterator_destroy (&iterator);
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
  return rc;
}


/* Parse tree functions */

node_t *
pick_node_create (node_type type, void *a, void *b)
{
  node_t *node;

  node = xmalloc (sizeof (*node));
  node->type = type;
  node->v.gen.a = a;
  node->v.gen.b = b;
  return node;
}

struct eval_env
{
292
  mu_message_t msg;
293 294 295 296
  char *datefield;
};

static int
297
match_header (mu_message_t msg, char *comp, regex_t *regex)
298
{
Sergey Poznyakoff authored
299
  int rc;
300
  size_t i, count;
301
  mu_header_t hdr = NULL;
Sergey Poznyakoff authored
302
  const char *buf;
303
  
Sergey Poznyakoff authored
304 305 306 307 308 309
  rc = mu_message_get_header (msg, &hdr);
  if (rc)
    {
      mu_error (_("cannot get header: %s"), mu_strerror (rc));
      return 0;
    }
310
  mu_header_get_field_count (hdr, &count);
311 312
  for (i = 1; i <= count; i++)
    {
Sergey Poznyakoff authored
313
      mu_header_sget_field_name (hdr, i, &buf);
314
      if (mu_c_strcasecmp (buf, comp) == 0)
315
	{
Sergey Poznyakoff authored
316
	  mu_header_sget_field_value (hdr, i, &buf);
317 318 319 320 321 322 323 324
	  if (regexec (regex, buf, 0, NULL, 0) == 0)
	    return 1;
	}
    }
  return 0;
}

static int
325
match_message (mu_message_t msg, regex_t *regex)
326
{
327
  mu_stream_t str = NULL;
328 329 330
  char buf[128];
  size_t n;
  
331
  mu_message_get_streamref (msg, &str);
332
  while (mu_stream_readline (str, buf, sizeof buf, &n) == 0
333 334 335 336 337 338
	 && n > 0)
    {
      buf[n] = 0;
      if (regexec (regex, buf, 0, NULL, 0) == 0)
	return 1;
    }
339
  mu_stream_destroy (&str);
340 341 342 343
  return 0;
}

static int
344 345
get_date_field (struct eval_env *env, time_t *t)
{
346
  mu_header_t hdr;
347 348
  char buf[128];
  
349
  if (mu_message_get_header (env->msg, &hdr))
350
    return 1;
351
  if (mu_header_get_value (hdr, env->datefield, buf, sizeof buf, NULL))
352 353 354 355 356
    return 1;
  return mu_parse_date (buf, t, NULL);
}

static int
357 358
pick_eval_node (node_t *node, struct eval_env *env)
{
359 360
  time_t t;
  
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
  switch (node->type)
    {
    case node_and:
      if (!pick_eval_node (node->v.op.larg, env))
	return 0;
      return pick_eval_node (node->v.op.rarg, env);
	
    case node_or:
      if (pick_eval_node (node->v.op.larg, env))
	return 1;
      return pick_eval_node (node->v.op.rarg, env);

    case node_not:
      return !pick_eval_node (node->v.op.larg, env);
      
    case node_regex:
      if (node->v.re.comp)
	return match_header (env->msg, node->v.re.comp, node->v.re.regex);
      else
	return match_message (env->msg, node->v.re.regex);
      
    case node_datefield:
      env->datefield = node->v.df.datefield;
      return 1;
385 386 387 388 389 390 391 392 393 394

    case node_before:
      if (get_date_field (env, &t))
	break;
      return t < node->v.time;
      
    case node_after:
      if (get_date_field (env, &t))
	break;
      return t > node->v.time;
395
    }
396

397 398 399 400
  return 0;
}

int
401
pick_eval (mu_message_t msg)
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
{
  struct eval_env env;
  
  if (!parse_tree)
    return 1;
  env.msg = msg;
  env.datefield = "date";
  return pick_eval_node (parse_tree, &env);
}

void
set_cflags (char *str)
{
  reg_flags = 0;
  for (; *str; str++)
    {
      switch (*str)
	{
	case 'b':
	case 'B':
	  reg_flags &= ~REG_EXTENDED;
	  break;

	case 'e':
	case 'E':
	  reg_flags |= REG_EXTENDED;
	  break;

	case 'c':
	case 'C':
	  reg_flags &= ~REG_ICASE;
	  break;
	  
	case 'i':
	case 'I':
	  reg_flags |= REG_ICASE;
	  break;

	default:
441
	  mu_error (_("Invalid regular expression flag: %c"), *str);
442 443 444 445
	  exit (1);
	}
    }
}