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
|
/*
* Parses unix mail boxes into headers and bodies.
*/
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
#define BUFSIZE 2048
/* A growable buffer for collecting headers. */
struct Buffer
{
Buffer() : data(0), allocated(0), length(0) { }
~Buffer() { empty(); }
void append( char p ) {
if ( ++length > allocated )
upAllocate( length*2 );
data[length-1] = p;
}
void clear() { length = 0; }
void upAllocate( int len );
void empty();
char *data;
int allocated;
int length;
};
struct MailboxScanner
{
Buffer headName;
Buffer headContent;
int cs, top, stack[1];
int init( );
int execute( const char *data, int len, bool isEof );
int finish( );
};
%%{
machine MailboxScanner;
# Buffer the header names.
action bufHeadName { headName.append(fc); }
# Prints a blank line after the end of the headers of each message.
action blankLine { cout << endl; }
# Helpers we will use in matching the date section of the from line.
day = /[A-Z][a-z][a-z]/;
month = /[A-Z][a-z][a-z]/;
year = /[0-9][0-9][0-9][0-9]/;
time = /[0-9][0-9]:[0-9][0-9]/ . ( /:[0-9][0-9]/ | '' );
letterZone = /[A-Z][A-Z][A-Z]/;
numZone = /[+\-][0-9][0-9][0-9][0-9]/;
zone = letterZone | numZone;
dayNum = /[0-9 ][0-9]/;
# These are the different formats of the date minus an obscure
# type that has a funny string 'remote from xxx' on the end. Taken
# from c-client in the imap-2000 distribution.
date = day . ' ' . month . ' ' . dayNum . ' ' . time . ' ' .
( year | year . ' ' . zone | zone . ' ' . year );
# From lines separate messages. We will exclude fromLine from a message
# body line. This will cause us to stay in message line up until an
# entirely correct from line is matched.
fromLine = 'From ' . (any-'\n')* . ' ' . date . '\n';
# The types of characters that can be used as a header name.
hchar = print - [ :];
# Simply eat up an uninteresting header. Return at the first non-ws
# character following a newline.
consumeHeader := (
[^\n] |
'\n' [ \t] |
'\n' [^ \t] @{fhold; fret;}
)*;
action hchar {headContent.append(fc);}
action hspace {headContent.append(' ');}
action hfinish {
headContent.append(0);
cout << headContent.data << endl;
headContent.clear();
fhold;
fret;
}
# Display the contents of a header as it is consumed. Collapses line
# continuations to a single space.
printHeader := (
[^\n] @hchar |
( '\n' ( [ \t]+ '\n' )* [ \t]+ ) %hspace
)** $!hfinish;
action onHeader
{
headName.append(0);
if ( strcmp( headName.data, "From" ) == 0 ||
strcmp( headName.data, "To" ) == 0 ||
strcmp( headName.data, "Subject" ) == 0 )
{
/* Print the header name, then jump to a machine the will display
* the contents. */
cout << headName.data << ":";
headName.clear();
fcall printHeader;
}
headName.clear();
fcall consumeHeader;
}
header = hchar+ $bufHeadName ':' @onHeader;
# Exclude fromLine from a messageLine, otherwise when encountering a
# fromLine we will be simultaneously matching the old message and a new
# message.
messageLine = ( [^\n]* '\n' - fromLine );
# An entire message.
message = ( fromLine . header* . '\n' @blankLine . messageLine* );
# File is a series of messages.
main := message*;
}%%
%% write data;
int MailboxScanner::init( )
{
%% write init;
return 1;
}
int MailboxScanner::execute( const char *data, int len, bool isEof )
{
const char *p = data;
const char *pe = data + len;
const char *eof = isEof ? pe : 0;
%% write exec;
if ( cs == MailboxScanner_error )
return -1;
if ( cs >= MailboxScanner_first_final )
return 1;
return 0;
}
int MailboxScanner::finish( )
{
if ( cs == MailboxScanner_error )
return -1;
if ( cs >= MailboxScanner_first_final )
return 1;
return 0;
}
void Buffer::empty()
{
if ( data != 0 ) {
free( data );
data = 0;
length = 0;
allocated = 0;
}
}
void Buffer::upAllocate( int len )
{
if ( data == 0 )
data = (char*) malloc( len );
else
data = (char*) realloc( data, len );
allocated = len;
}
MailboxScanner mailbox;
char buf[BUFSIZE];
int main()
{
mailbox.init();
while ( 1 ) {
int len = fread( buf, 1, BUFSIZE, stdin );
mailbox.execute( buf, len, len != BUFSIZE );
if ( len != BUFSIZE )
break;
}
if ( mailbox.finish() <= 0 )
cerr << "mailbox: error parsing input" << endl;
return 0;
}
|