Statistics
| Revision:

root / src / quote_fmt_parse.y @ 3238

History | View | Annotate | Download (7.48 KB)

1
%{
2

    
3
#include "defs.h"
4

    
5
#include <glib.h>
6
#include <ctype.h>
7

    
8
#include "procmsg.h"
9
#include "procmime.h"
10
#include "utils.h"
11

    
12
#include "quote_fmt.h"
13
#include "quote_fmt_lex.h"
14

    
15
/* decl */
16
/*
17
flex quote_fmt.l
18
bison -p quote_fmt quote_fmt.y
19
*/
20

    
21
int yylex(void);
22

    
23
static MsgInfo *msginfo = NULL;
24
static gboolean *visible = NULL;
25
static gint maxsize = 0;
26
static gint stacksize = 0;
27

    
28
static gchar *buffer = NULL;
29
static gint bufmax = 0;
30
static gint bufsize = 0;
31
static const gchar *quote_str = NULL;
32
static const gchar *body = NULL;
33
static gint error = 0;
34

    
35
static void add_visibility(gboolean val)
36
{
37
	stacksize++;
38
	if (maxsize < stacksize) {
39
		maxsize += 128;
40
		visible = g_realloc(visible, maxsize * sizeof(gboolean));
41
		if (visible == NULL)
42
			maxsize = 0;
43
	}
44

    
45
	visible[stacksize - 1] = val;
46
}
47

    
48
static void remove_visibility(void)
49
{
50
	stacksize--;
51
}
52

    
53
static void add_buffer(const gchar *s)
54
{
55
	gint len;
56

    
57
	len = strlen(s);
58
	if (bufsize + len + 1 > bufmax) {
59
		if (bufmax == 0)
60
			bufmax = 128;
61
		while (bufsize + len + 1 > bufmax)
62
			bufmax *= 2;
63
		buffer = g_realloc(buffer, bufmax);
64
	}
65
	strcpy(buffer + bufsize, s);
66
	bufsize += len;
67
}
68

    
69
#if 0
70
static void flush_buffer(void)
71
{
72
	if (buffer != NULL)
73
		*buffer = '\0';
74
	bufsize = 0;
75
}
76
#endif
77

    
78
gchar *quote_fmt_get_buffer(void)
79
{
80
	if (error != 0)
81
		return NULL;
82
	else
83
		return buffer;
84
}
85

    
86
#define INSERT(buf) \
87
	if (stacksize != 0 && visible[stacksize - 1]) \
88
		add_buffer(buf)
89

    
90
#define INSERT_CHARACTER(chr) \
91
	if (stacksize != 0 && visible[stacksize - 1]) { \
92
		gchar tmp[2]; \
93
		tmp[0] = (chr); \
94
		tmp[1] = '\0'; \
95
		add_buffer(tmp); \
96
	}
97

    
98
void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
99
		    const gchar *my_body)
100
{
101
	quote_str = my_quote_str;
102
	body = my_body;
103
	msginfo = info;
104
	stacksize = 0;
105
	add_visibility(TRUE);
106
	if (buffer != NULL)
107
		*buffer = 0;
108
	bufsize = 0;
109
	error = 0;
110
}
111

    
112
void quote_fmterror(char *str)
113
{
114
	g_warning("Error: %s\n", str);
115
	error = 1;
116
}
117

    
118
int quote_fmtwrap(void)
119
{
120
	return 1;
121
}
122

    
123
static int isseparator(int ch)
124
{
125
	return g_ascii_isspace(ch) || ch == '.' || ch == '-';
126
}
127
%}
128

    
129
%union {
130
	char chr;
131
}
132

    
133
%token SHOW_NEWSGROUPS
134
%token SHOW_DATE SHOW_FROM SHOW_FULLNAME SHOW_FIRST_NAME
135
%token SHOW_SENDER_INITIAL SHOW_SUBJECT SHOW_TO SHOW_MESSAGEID
136
%token SHOW_PERCENT SHOW_CC SHOW_REFERENCES SHOW_MESSAGE
137
%token SHOW_QUOTED_MESSAGE SHOW_BACKSLASH SHOW_TAB
138
%token SHOW_QUOTED_MESSAGE_NO_SIGNATURE SHOW_MESSAGE_NO_SIGNATURE
139
%token SHOW_EOL SHOW_QUESTION_MARK SHOW_OPARENT SHOW_CPARENT
140
%token QUERY_DATE QUERY_FROM
141
%token QUERY_FULLNAME QUERY_SUBJECT QUERY_TO QUERY_NEWSGROUPS
142
%token QUERY_MESSAGEID QUERY_CC QUERY_REFERENCES
143
%token OPARENT CPARENT
144
%token CHARACTER
145

    
146
%start quote_fmt
147

    
148
%token <chr> CHARACTER
149
%type <chr> character
150

    
151
%%
152

    
153
quote_fmt:
154
	character_or_special_or_query_list;
155

    
156
character_or_special_or_query_list:
157
	character_or_special_or_query character_or_special_or_query_list
158
	| character_or_special_or_query ;
159

    
160
character_or_special_or_query:
161
	special
162
	| character
163
	{
164
		INSERT_CHARACTER($1);
165
	}
166
	| query ;
167

    
168

    
169
character:
170
	CHARACTER
171
	;
172

    
173
special:
174
	SHOW_NEWSGROUPS
175
	{
176
		if (msginfo->newsgroups)
177
			INSERT(msginfo->newsgroups);
178
	}
179
	| SHOW_DATE
180
	{
181
		if (msginfo->date)
182
			INSERT(msginfo->date);
183
	}
184
	| SHOW_FROM
185
	{
186
		if (msginfo->from)
187
			INSERT(msginfo->from);
188
	}
189
	| SHOW_FULLNAME
190
	{
191
		if (msginfo->fromname)
192
			INSERT(msginfo->fromname);
193
	}
194
	| SHOW_FIRST_NAME
195
	{
196
		if (msginfo->fromname) {
197
			gchar *p;
198
			gchar *str;
199

    
200
			str = alloca(strlen(msginfo->fromname) + 1);
201
			if (str != NULL) {
202
				strcpy(str, msginfo->fromname);
203
				p = str;
204
				while (*p && !g_ascii_isspace(*p)) p++;
205
				*p = '\0';
206
				INSERT(str);
207
			}
208
		}
209
	}
210
	| SHOW_SENDER_INITIAL
211
	{
212
#define MAX_SENDER_INITIAL 20
213
		if (msginfo->fromname) {
214
			gchar tmp[MAX_SENDER_INITIAL];
215
			gchar *p;
216
			gchar *cur;
217
			gint len = 0;
218

    
219
			p = msginfo->fromname;
220
			cur = tmp;
221
			while (*p) {
222
				if (*p && g_ascii_isalnum(*p)) {
223
					*cur = g_ascii_toupper(*p);
224
						cur++;
225
					len++;
226
					if (len >= MAX_SENDER_INITIAL - 1)
227
						break;
228
				} else
229
					break;
230
				while (*p && !isseparator(*p)) p++;
231
				while (*p && isseparator(*p)) p++;
232
			}
233
			*cur = '\0';
234
			INSERT(tmp);
235
		}
236
	}
237
	| SHOW_SUBJECT
238
	{
239
		if (msginfo->subject)
240
			INSERT(msginfo->subject);
241
	}
242
	| SHOW_TO
243
	{
244
		if (msginfo->to)
245
			INSERT(msginfo->to);
246
	}
247
	| SHOW_MESSAGEID
248
	{
249
		if (msginfo->msgid)
250
			INSERT(msginfo->msgid);
251
	}
252
	| SHOW_PERCENT
253
	{
254
		INSERT("%");
255
	}
256
	| SHOW_CC
257
	{
258
		if (msginfo->cc)
259
			INSERT(msginfo->cc);
260
	}
261
	| SHOW_REFERENCES
262
	{
263
		/* if (msginfo->references)
264
			INSERT(msginfo->references); */
265
	}
266
	| SHOW_MESSAGE
267
	{
268
		gchar buf[BUFFSIZE];
269
		FILE *fp = NULL;
270

    
271
		if (body)
272
			fp = str_open_as_stream(body);
273
		else if (msginfo->size > 0) {
274
			fp = procmime_get_first_text_content(msginfo, NULL);
275
			if (fp == NULL)
276
				g_warning("quote_fmt_parse.y: Can't get text part\n");
277
		}
278

    
279
		if (fp) {
280
			while (fgets(buf, sizeof(buf), fp) != NULL) {
281
				strcrchomp(buf);
282
				INSERT(buf);
283
			}
284
			fclose(fp);
285
		}
286
	}
287
	| SHOW_QUOTED_MESSAGE
288
	{
289
		gchar buf[BUFFSIZE];
290
		FILE *fp = NULL;
291

    
292
		if (body)
293
			fp = str_open_as_stream(body);
294
		else if (msginfo->size > 0) {
295
			fp = procmime_get_first_text_content(msginfo, NULL);
296
			if (fp == NULL)
297
				g_warning("quote_fmt_parse.y: Can't get text part\n");
298
		}
299

    
300
		if (fp) {
301
			while (fgets(buf, sizeof(buf), fp) != NULL) {
302
				strcrchomp(buf);
303
				if (quote_str)
304
					INSERT(quote_str);
305
				INSERT(buf);
306
			}
307
			fclose(fp);
308
		}
309
	}
310
	| SHOW_MESSAGE_NO_SIGNATURE
311
	{
312
		gchar buf[BUFFSIZE];
313
		FILE *fp = NULL;
314

    
315
		if (body)
316
			fp = str_open_as_stream(body);
317
		else if (msginfo->size > 0) {
318
			fp = procmime_get_first_text_content(msginfo, NULL);
319
			if (fp == NULL)
320
				g_warning("quote_fmt_parse.y: Can't get text part\n");
321
		}
322

    
323
		if (fp) {
324
			while (fgets(buf, sizeof(buf), fp) != NULL) {
325
				strcrchomp(buf);
326
				if (strncmp(buf, "-- \n", 4) == 0)
327
					break;
328
				INSERT(buf);
329
			}
330
			fclose(fp);
331
		}
332
	}
333
	| SHOW_QUOTED_MESSAGE_NO_SIGNATURE
334
	{
335
		gchar buf[BUFFSIZE];
336
		FILE *fp = NULL;
337

    
338
		if (body)
339
			fp = str_open_as_stream(body);
340
		else if (msginfo->size > 0) {
341
			fp = procmime_get_first_text_content(msginfo, NULL);
342
			if (fp == NULL)
343
				g_warning("Can't get text part\n");
344
		}
345

    
346
		if (fp) {
347
			while (fgets(buf, sizeof(buf), fp) != NULL) {
348
				strcrchomp(buf);
349
				if (strncmp(buf, "-- \n", 4) == 0)
350
					break;
351
				if (quote_str)
352
					INSERT(quote_str);
353
				INSERT(buf);
354
			}
355
			fclose(fp);
356
		}
357
	}
358
	| SHOW_BACKSLASH
359
	{
360
		INSERT("\\");
361
	}
362
	| SHOW_TAB
363
	{
364
		INSERT("\t");
365
	}
366
	| SHOW_EOL
367
	{
368
		INSERT("\n");
369
	}
370
	| SHOW_QUESTION_MARK
371
	{
372
		INSERT("?");
373
	}
374
	| SHOW_OPARENT
375
	{
376
		INSERT("{");
377
	}
378
	| SHOW_CPARENT
379
	{
380
		INSERT("}");
381
	};
382

    
383
query:
384
	QUERY_DATE
385
	{
386
		add_visibility(msginfo->date != NULL);
387
	}
388
	OPARENT quote_fmt CPARENT
389
	{
390
		remove_visibility();
391
	}
392
	| QUERY_FROM
393
	{
394
		add_visibility(msginfo->from != NULL);
395
	}
396
	OPARENT quote_fmt CPARENT
397
	{
398
		remove_visibility();
399
	}
400
	| QUERY_FULLNAME
401
	{
402
		add_visibility(msginfo->fromname != NULL);
403
	}
404
	OPARENT quote_fmt CPARENT
405
	{
406
		remove_visibility();
407
	}
408
	| QUERY_SUBJECT
409
	{
410
		add_visibility(msginfo->subject != NULL);
411
	}
412
	OPARENT quote_fmt CPARENT
413
	{
414
		remove_visibility();
415
	}
416
	| QUERY_TO
417
	{
418
		add_visibility(msginfo->to != NULL);
419
	}
420
	OPARENT quote_fmt CPARENT
421
	{
422
		remove_visibility();
423
	}
424
	| QUERY_NEWSGROUPS
425
	{
426
		add_visibility(msginfo->newsgroups != NULL);
427
	}
428
	OPARENT quote_fmt CPARENT
429
	{
430
		remove_visibility();
431
	}
432
	| QUERY_MESSAGEID
433
	{
434
		add_visibility(msginfo->msgid != NULL);
435
	}
436
	OPARENT quote_fmt CPARENT
437
	{
438
		remove_visibility();
439
	}
440
	| QUERY_CC
441
	{
442
		add_visibility(msginfo->cc != NULL);
443
	}
444
	OPARENT quote_fmt CPARENT
445
	{
446
		remove_visibility();
447
	}
448
	| QUERY_REFERENCES
449
	{
450
		/* add_visibility(msginfo->references != NULL); */
451
	}
452
	OPARENT quote_fmt CPARENT
453
	{
454
		remove_visibility();
455
	};