File: bc_dump.c

package info (click to toggle)
kolab-cyrus-imapd 2.2.13-2
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 14,028 kB
  • ctags: 8,058
  • sloc: ansic: 83,921; sh: 36,867; perl: 3,994; makefile: 1,416; yacc: 949; awk: 302; lex: 249; asm: 214
file content (304 lines) | stat: -rw-r--r-- 7,590 bytes parent folder | download | duplicates (8)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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
228
229
230
231
232
233
234
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
/* bc_generate.c -- sieve bytecode- almost flattened bytecode
 * Rob Siemborski
 * $Id: bc_dump.c,v 1.2 2003/10/22 18:03:23 rjs3 Exp $
 */
/***********************************************************
        Copyright 2001 by Carnegie Mellon University

                      All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of Carnegie Mellon
University not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
permission.

CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR
ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
******************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
 
#include "sieve_interface.h"
#include "bytecode.h"

 
struct bytecode_info 
{
    bytecode_t *data;/* pointer to almost-flat bytecode */
    size_t scriptend; /* used by emit code to know final length of bytecode */
    size_t reallen; /* allocated length of 'data' */
};

/*this would work a lot better if we actually could tell how many levels deep in if statements we were.  currently it doesn't know*/

void print_spaces(int n)
{
    int temp_n=0;
    while(temp_n++ < (n))
	putchar(' ');
}


#if DUMPCODE

/* Dump a stringlist.  Return the last address used by the list */
static int dump_sl(bytecode_info_t *d, int ip, int level) 
{
    int numstr = d->data[ip].listlen;
    int i;
    
    for(i=0; i<numstr; i++) {
	print_spaces(level*4);
	printf(" {%d}",d->data[++ip].len);
	printf("%s\n",d->data[++ip].str);
    }
    
    return ip;
}

static int dump_test(bytecode_info_t *d, int ip, int level);

/* Dump a testlist.  Return the last address used by the list */
static int dump_tl(bytecode_info_t *d, int ip, int level) 
{
    int numtest = d->data[ip].listlen;
    int i;
    
    for(i=0; i<numtest; i++) {
	print_spaces(level*4);
	printf(" (until %d)\n", d->data[++ip].jump);
	ip = dump_test(d, ++ip, level);
    }
    
    return ip;
}

/* Dump a test, return the last address used by the test */
static int dump_test(bytecode_info_t *d, int ip, int level ) {

    print_spaces(level*4);
    switch(d->data[ip].op) {
    case BC_TRUE:
	printf("%d: TRUE\n",ip);
	break;

    case BC_FALSE:
	printf("%d: FALSE\n",ip);
	break;

    case BC_NOT:
	printf("%d: NOT TEST(\n",ip++);
	/*   printf("  (until %d)\n", d->data[ip++].jump);*/
	ip = dump_test(d,ip, level);
	print_spaces(level*4);
	printf("    )\n");
	break;

    case BC_SIZE:
	printf("%d: SIZE TAG(%d) NUM(%d)\n",ip,
	       d->data[ip+1].value, d->data[ip+2].value);
	ip+=2;
	break;

    case BC_EXISTS:
	printf("%d: EXISTS\n",ip++);
	ip = dump_sl(d,ip,level);
	break;

    case BC_ALLOF:
	printf("%d: ALLOF (\n",ip++);
	ip = dump_tl(d,ip,level);
	print_spaces(level*4);
	printf(")\n");
	break;

    case BC_ANYOF:
	printf("%d: ANYOF (\n",ip++);
	ip = dump_tl(d,ip, level);
	  print_spaces(level*4);
	printf(")\n");
	break;
	    
    case BC_HEADER:
	printf("%d: HEADER (\n",ip++);
	print_spaces(level*4);
	if (d->data[ip].value == B_COUNT || d->data[ip].value == B_VALUE)
	{
	    printf("      MATCH:%d  RELATION:%d  COMP:%d HEADERS:\n", 
		   d->data[ip].value, d->data[ip+1].value,d->data[ip+2].value);
	} else {
	    printf("      MATCH:%d COMP:%d HEADERS:\n",d->data[ip].value, d->data[ip+2].value);
	}
	ip+=3;
	ip = dump_sl(d,ip,level);
	ip++;
	print_spaces(level*4);
	printf("      DATA:\n");
	ip = dump_sl(d,ip,level);
	break;
	
    case BC_ADDRESS:
    case BC_ENVELOPE:
	printf("%d: %s (\n",ip++,
	       d->data[ip].op == BC_ADDRESS ? "ADDRESS" : "ENVELOPE");
	print_spaces(level*4);
	if (d->data[ip].value == B_COUNT || d->data[ip].value == B_VALUE)
	{
	    printf("      MATCH:%d RELATION: %d COMP: %d TYPE: %d HEADERS:\n", 
		   d->data[ip].value, d->data[ip+1].value, d->data[ip+2].value, d->data[ip+3].value);
	} else {
	    printf("      MATCH:%d COMP:%d TYPE:%d HEADERS:\n",
		   d->data[ip].value,d->data[ip+1].value,d->data[ip+3].value);
	}
	ip+=4;
	ip = dump_sl(d,ip,level); ip++;
	print_spaces(level*4);
	printf("      DATA:\n");
	ip = dump_sl(d,ip,level);
	break;

    default:
	printf("%d: TEST(%d)\n",ip,d->data[ip].op);
	break;
    }

    return ip;
}

void dump(bytecode_info_t *d, int level) 
{
    int i;
    printf("Dumping almost flattened bytecode\n\n");
    
    if(!d) return;
    
    for(i=0; i<d->scriptend; i++) {
	print_spaces(level*4);
	switch(d->data[i].op) {
	case B_REJECT:
	    printf("%d: REJECT {%d}%s\n",i,
		   d->data[i+1].len,d->data[i+2].str);
	    i+=2;
	    break;
	case B_IF:
	    if (d->data[i+3].jump== -1)
	    {
		printf("%d: IF THEN(%d) POST(%d) TEST(\n",i,
		       d->data[i+1].jump,d->data[i+2].jump);
	    }
	    else
	    {
		printf("%d: IF THEN(%d) ELSE(%d) POST(%d) TEST(\n",i,
		       d->data[i+1].jump,d->data[i+2].jump,
		       d->data[i+3].jump);
	    }
	    i = dump_test(d,i+4, level+1);
	    printf(")\n");
	    break;

	case B_STOP:
	    printf("%d: STOP\n",i);
	    break;

	case B_DISCARD:
	    printf("%d: DISCARD\n",i);
	    break;
	    
	case B_KEEP:
	    printf("%d: KEEP\n",i);
	    break;

	case B_MARK:
	    printf("%d: MARK\n",i);
	    break;

	case B_UNMARK:
	    printf("%d: UNMARK\n",i);
	    break;

	case B_FILEINTO:
	    printf("%d: FILEINTO {%d}%s\n",i,
		   d->data[i+1].len,d->data[i+2].str);
	    i+=2;
	    break;

	case B_REDIRECT:
	    printf("%d: REDIRECT {%d}%s\n",i,
		   d->data[i+1].len,d->data[i+2].str);
	    i+=2;
	    break;

	case B_SETFLAG:
	    printf("%d: SETFLAG\n",i);
	    i=dump_sl(d,++i, level);
	    break;

	case B_ADDFLAG:
	    printf("%d: ADDFLAG\n",i);
	    i=dump_sl(d,++i,level);
	    break;

	case B_REMOVEFLAG:
	    printf("%d: REMOVEFLAG\n",i);
	    i=dump_sl(d,++i,level);
	    break;

	case B_DENOTIFY:
	    printf("%d: DENOTIFY priority %d,comp %d %d  %s\n", 
		   i,
		   d->data[i+1].value,
		   d->data[i+2].value,
		   d->data[i+3].value,
		   (d->data[i+4].len == -1 ? "[nil]" : d->data[i+5].str));
	    i+=5;
	    break;

	case B_NOTIFY: 
	    printf("%d: NOTIFY\n   METHOD(%s),\n   ID(%s),\n   OPTIONS",
		   i,
		   d->data[i+2].str,
		   (d->data[i+3].len == -1 ? "[nil]" : d->data[i+4].str));
	    i+=5;
	    i=dump_sl(d,i,level);
	    printf("   PRIORITY(%d),\n   MESSAGE({%d}%s)\n", 
		   d->data[i+1].value, d->data[i+2].len,d->data[i+3].str);
	    i+=3;
	    break;

	case B_VACATION:
	    printf("%d:VACATION\n",i);
	    i++;
	    i=dump_sl(d,i,level);
	    printf("SUBJ({%d}%s) MESG({%d}%s)\n DAYS(%d) MIME(%d)\n", 
		   d->data[i+1].len, (d->data[i+1].len == -1 ? "[nil]" : d->data[i+2].str),
		   d->data[i+3].len, (d->data[i+3].len == -1 ? "[nil]" : d->data[i+4].str),
		   d->data[i+5].value, d->data[i+6].value);
	    i+=6;
	
	    break;
	case B_JUMP:
	    printf("%d: JUMP HUH?  this shouldn't be here>?!",i);
	    break;
	case B_NULL:
	    printf("%d: NULL\n",i);
	    break;
	default:
	    printf("%d: %d\n",i,d->data[i].op);
	    break;
	}
    }
    printf("full len is: %d\n", d->scriptend);
}
#endif