filter.c 3.71 KB
Newer Older
Cedric Roux's avatar
Cedric Roux committed
1
2
3
4
5
6
7
8
9
#include "filter.h"
#include "event.h"
#include "database.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct filter {
  union {
Cedric Roux's avatar
Cedric Roux committed
10
    struct { struct filter *a, *b; } op2;
Cedric Roux's avatar
Cedric Roux committed
11
12
13
14
15
    int v;
    struct { int event_type; int arg_index; } evarg;
  } v;

  int (*eval)(struct filter *this, event e);
16
  void (*free)(struct filter *this);
Cedric Roux's avatar
Cedric Roux committed
17
18
19
20
21
22
};

/****************************************************************************/
/*                     evaluation functions                                 */
/****************************************************************************/

Cedric Roux's avatar
Cedric Roux committed
23
24
25
26
27
28
int eval_and(struct filter *f, event e)
{
  if (f->v.op2.a->eval(f->v.op2.a, e) == 0) return 0;
  return f->v.op2.b->eval(f->v.op2.b, e);
}

Cedric Roux's avatar
Cedric Roux committed
29
30
int eval_eq(struct filter *f, event e)
{
Cedric Roux's avatar
Cedric Roux committed
31
32
  int a = f->v.op2.a->eval(f->v.op2.a, e);
  int b = f->v.op2.b->eval(f->v.op2.b, e);
Cedric Roux's avatar
Cedric Roux committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  return a == b;
}

int eval_int(struct filter *f, event e)
{
  return f->v.v;
}

int eval_evarg(struct filter *f, event e)
{
  if (e.type != f->v.evarg.event_type) {
    printf("%s:%d:%s: bad event type\n", __FILE__, __LINE__, __FUNCTION__);
    abort();
  }
  if (e.e[f->v.evarg.arg_index].type != EVENT_INT) {
    printf("%s:%d:%s: bad event argtype; has to be 'int'\n",
        __FILE__, __LINE__, __FUNCTION__);
    abort();
  }
  return e.e[f->v.evarg.arg_index].i;
}

/****************************************************************************/
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*                     free memory functions                                */
/****************************************************************************/

void free_op2(struct filter *f)
{
  free_filter(f->v.op2.a);
  free_filter(f->v.op2.b);
  free(f);
}

void free_noop(struct filter *f)
{
  free(f);
}

/****************************************************************************/
/*                     filter construction/destruction functions            */
Cedric Roux's avatar
Cedric Roux committed
73
74
/****************************************************************************/

Cedric Roux's avatar
Cedric Roux committed
75
76
77
78
79
filter *filter_and(filter *a, filter *b)
{
  struct filter *ret = calloc(1, sizeof(struct filter));
  if (ret == NULL) abort();
  ret->eval = eval_and;
80
  ret->free = free_op2;
Cedric Roux's avatar
Cedric Roux committed
81
82
83
84
85
  ret->v.op2.a = a;
  ret->v.op2.b = b;
  return ret;
}

Cedric Roux's avatar
Cedric Roux committed
86
87
88
89
90
filter *filter_eq(filter *a, filter *b)
{
  struct filter *ret = calloc(1, sizeof(struct filter));
  if (ret == NULL) abort();
  ret->eval = eval_eq;
91
  ret->free = free_op2;
Cedric Roux's avatar
Cedric Roux committed
92
93
  ret->v.op2.a = a;
  ret->v.op2.b = b;
Cedric Roux's avatar
Cedric Roux committed
94
95
96
97
98
99
100
101
  return ret;
}

filter *filter_int(int v)
{
  struct filter *ret = calloc(1, sizeof(struct filter));
  if (ret == NULL) abort();
  ret->eval = eval_int;
102
  ret->free = free_noop;
Cedric Roux's avatar
Cedric Roux committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  ret->v.v = v;
  return ret;
}

filter *filter_evarg(void *database, char *event_name, char *varname)
{
  struct filter *ret;
  int event_id;
  database_event_format f;
  int i;

  ret = calloc(1, sizeof(struct filter)); if (ret == NULL) abort();

  event_id = event_id_from_name(database, event_name);
  f = get_format(database, event_id);

  ret->eval = eval_evarg;
120
  ret->free = free_noop;
Cedric Roux's avatar
Cedric Roux committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  ret->v.evarg.event_type = event_id;
  ret->v.evarg.arg_index = -1;

  for (i = 0; i < f.count; i++) {
    if (strcmp(f.name[i], varname) != 0) continue;
    ret->v.evarg.arg_index = i;
    break;
  }
  if (ret->v.evarg.arg_index == -1) {
    printf("%s:%d:%s: event '%s' has no argument '%s'\n",
        __FILE__, __LINE__, __FUNCTION__, event_name, varname);
    abort();
  }

  return ret;
}

138
139
140
141
142
143
144
145
void free_filter(filter *_f)
{
  struct filter *f;
  if (_f == NULL) return;
  f = _f;
  f->free(f);
}

Cedric Roux's avatar
Cedric Roux committed
146
147
148
149
150
151
152
153
154
/****************************************************************************/
/*                     eval function                                        */
/****************************************************************************/

int filter_eval(filter *_f, event e)
{
  struct filter *f = _f;
  return f->eval(f, e);
}