summaryrefslogtreecommitdiff
path: root/libsieve/runtime.c
blob: d593de0699ea99db50186126db74e5d5aee29106 (plain)
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
/* GNU mailutils - a suite of utilities for electronic mail
   Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.

   This program 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 version 2, 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 Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif  
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sieve.h>

#define SIEVE_ARG(m,n,t) ((m)->prog[(m)->pc+(n)].t)
#define SIEVE_ADJUST(m,n) (m)->pc+=(n)

#define INSTR_DEBUG(m) ((m)->debug_level == 100 && (m)->debug_printer)

static int
instr_run (sieve_machine_t *mach)
{
  sieve_handler_t han = SIEVE_ARG (mach, 0, handler);
  list_t arg_list = SIEVE_ARG (mach, 1, list);
  list_t tag_list = SIEVE_ARG (mach, 2, list);
  int rc;
  
  if (INSTR_DEBUG (mach))
    {
      sieve_debug (mach, "Arguments: ");
      sieve_print_value_list (arg_list, mach->debug_printer, mach->data);
      sieve_debug (mach, "\nTags:");
      sieve_print_tag_list (tag_list, mach->debug_printer, mach->data);
      sieve_debug (mach, "\n");
    }

  rc = han (mach, arg_list, tag_list);
  SIEVE_ADJUST(mach, 4);
  return rc;
}

void
instr_action (sieve_machine_t *mach)
{
  if (INSTR_DEBUG (mach))
    sieve_debug (mach, "ACTION: %s\n", SIEVE_ARG (mach, 3, string));
  instr_run (mach);
}

void
instr_test (sieve_machine_t *mach)
{
  if (INSTR_DEBUG (mach))
    sieve_debug (mach, "TEST: %s\n", SIEVE_ARG (mach, 3, string));
  mach->reg = instr_run (mach);
}

void
instr_push (sieve_machine_t *mach)
{
  if (INSTR_DEBUG (mach))
    sieve_debug (mach, "PUSH\n");
  if (!mach->stack && list_create (&mach->stack))
    {
      sieve_error ("can't create stack");
      sieve_abort (mach);
    }
  list_prepend (mach->stack, (void*) mach->reg);
}

void
instr_pop (sieve_machine_t *mach)
{
  if (INSTR_DEBUG (mach))
    sieve_debug (mach, "POP\n");
  if (!mach->stack || list_is_empty (mach->stack))
    {
      sieve_error ("stack underflow");
      sieve_abort (mach);
    }
  list_get (mach->stack, 0, (void **)&mach->reg);
  list_remove (mach->stack, (void *)mach->reg);
}

void
instr_allof (sieve_machine_t *mach)
{
  int num = SIEVE_ARG (mach, 0, number);
  int val = 1;

  if (INSTR_DEBUG (mach))
    sieve_debug (mach, "ALLOF %d\n", num);
  SIEVE_ADJUST(mach, 1);
  while (num-- > 0)
    {
      instr_pop (mach);
      val &= mach->reg;
    }
  mach->reg = val;
}

void
instr_anyof (sieve_machine_t *mach)
{
  int num = SIEVE_ARG (mach, 0, number);
  int val = 0;

  if (INSTR_DEBUG (mach))
    sieve_debug (mach, "ANYOF %d\n", num);
  SIEVE_ADJUST(mach, 1);
  while (num-- > 0)
    {
      instr_pop (mach);
      val &= mach->reg;
    }
  mach->reg = val;
}

void
instr_not (sieve_machine_t *mach)
{
  if (INSTR_DEBUG (mach))
    sieve_debug (mach, "NOT");
  mach->reg = !mach->reg;
}

void
sieve_abort (sieve_machine_t *mach)
{
  longjmp (mach->errbuf, 1);
}

int
sieve_run (sieve_machine_t *mach)
{
  if (setjmp (mach->errbuf))
    return 1;
  
  for (mach->pc = 1; mach->prog[mach->pc].handler; )
    (*mach->prog[mach->pc++].instr) (mach);

  return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.