remove reference to 'headerwindow.c'
[claws.git] / src / quote_fmt_parse.y
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 #include "intl.h"
12
13 #include "quote_fmt.h"
14 #include "quote_fmt_lex.h"
15
16 /* decl */
17 /*
18 flex quote_fmt.l
19 bison -p quote_fmt quote_fmt.y
20 */
21
22 int yylex(void);
23
24 static MsgInfo *msginfo = NULL;
25 static gboolean *visible = NULL;
26 static gint maxsize = 0;
27 static gint stacksize = 0;
28
29 static gchar *buffer = NULL;
30 static gint bufmax = 0;
31 static gint bufsize = 0;
32 static const gchar *quote_str = NULL;
33 static const gchar *body = NULL;
34 static gint error = 0;
35
36 static void add_visibility(gboolean val)
37 {
38         stacksize++;
39         if (maxsize < stacksize) {
40                 maxsize += 128;
41                 visible = g_realloc(visible, maxsize * sizeof(gboolean));
42                 if (visible == NULL)
43                         maxsize = 0;
44         }
45
46         visible[stacksize - 1] = val;
47 }
48
49 static void remove_visibility(void)
50 {
51         stacksize--;
52 }
53
54 static void add_buffer(const gchar *s)
55 {
56         gint len;
57
58         len = strlen(s);
59         if (bufsize + len + 1 > bufmax) {
60                 if (bufmax == 0)
61                         bufmax = 128;
62                 while (bufsize + len + 1 > bufmax)
63                         bufmax *= 2;
64                 buffer = g_realloc(buffer, bufmax);
65         }
66         strcpy(buffer + bufsize, s);
67         bufsize += len;
68 }
69
70 static void flush_buffer(void)
71 {
72         if (buffer != NULL)
73                 *buffer = '\0';
74         bufsize = 0;
75 }
76
77 gchar *quote_fmt_get_buffer(void)
78 {
79         if (error != 0)
80                 return NULL;
81         else
82                 return buffer;
83 }
84
85 #define INSERT(buf) \
86         if (stacksize != 0 && visible[stacksize - 1]) \
87                 add_buffer(buf)
88
89 #define INSERT_CHARACTER(chr) \
90         if (stacksize != 0 && visible[stacksize - 1]) { \
91                 gchar tmp[2]; \
92                 tmp[0] = (chr); \
93                 tmp[1] = '\0'; \
94                 add_buffer(tmp); \
95         }
96
97 void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
98                     const gchar *my_body)
99 {
100         quote_str = my_quote_str;
101         body = my_body;
102         msginfo = info;
103         stacksize = 0;
104         add_visibility(TRUE);
105         if (buffer != NULL)
106                 *buffer = 0;
107         bufsize = 0;
108         error = 0;
109 }
110
111 void quote_fmterror(char *str)
112 {
113         g_warning(_("Error: %s\n"), str);
114         error = 1;
115 }
116
117 int quote_fmtwrap(void)
118 {
119         return 1;
120 }
121
122 static int isseparator(char ch)
123 {
124         return isspace(ch) || ch == '.' || ch == '-';
125 }
126 %}
127
128 %union {
129         char chr;
130 }
131
132 %token SHOW_NEWSGROUPS
133 %token SHOW_DATE SHOW_FROM SHOW_FULLNAME SHOW_FIRST_NAME
134 %token SHOW_SENDER_INITIAL SHOW_SUBJECT SHOW_TO SHOW_MESSAGEID
135 %token SHOW_PERCENT SHOW_CC SHOW_REFERENCES SHOW_MESSAGE
136 %token SHOW_QUOTED_MESSAGE SHOW_BACKSLASH SHOW_TAB
137 %token SHOW_QUOTED_MESSAGE_NO_SIGNATURE SHOW_MESSAGE_NO_SIGNATURE
138 %token SHOW_EOL SHOW_QUESTION_MARK SHOW_OPARENT SHOW_CPARENT
139 %token QUERY_DATE QUERY_FROM
140 %token QUERY_FULLNAME QUERY_SUBJECT QUERY_TO QUERY_NEWSGROUPS
141 %token QUERY_MESSAGEID QUERY_CC QUERY_REFERENCES
142 %token OPARENT CPARENT
143 %token CHARACTER
144
145 %start quote_fmt
146
147 %token <chr> CHARACTER
148 %type <chr> character
149
150 %%
151
152 quote_fmt:
153         character_or_special_or_query_list;
154
155 character_or_special_or_query_list:
156         character_or_special_or_query character_or_special_or_query_list
157         | character_or_special_or_query ;
158
159 character_or_special_or_query:
160         special ;
161         | character
162         {
163                 INSERT_CHARACTER($1);
164         }
165         | query ;
166
167
168 character:
169         CHARACTER
170         ;
171
172 special:
173         SHOW_NEWSGROUPS
174         {
175                 if (msginfo->newsgroups)
176                         INSERT(msginfo->newsgroups);
177         }
178         | SHOW_DATE
179         {
180                 if (msginfo->date)
181                         INSERT(msginfo->date);
182         }
183         | SHOW_FROM
184         {
185                 if (msginfo->from)
186                         INSERT(msginfo->from);
187         }
188         | SHOW_FULLNAME
189         {
190                 if (msginfo->fromname)
191                         INSERT(msginfo->fromname);
192         }
193         | SHOW_FIRST_NAME
194         {
195                 if (msginfo->fromname) {
196                         gchar *p;
197                         gchar *str;
198
199                         str = alloca(strlen(msginfo->fromname) + 1);
200                         if (str != NULL) {
201                                 strcpy(str, msginfo->fromname);
202                                 p = str;
203                                 while (*p && !isspace(*p)) p++;
204                                 *p = '\0';
205                                 INSERT(str);
206                         }
207                 }
208         }
209         | SHOW_SENDER_INITIAL
210         {
211 #define MAX_SENDER_INITIAL 20
212                 if (msginfo->fromname) {
213                         gchar tmp[MAX_SENDER_INITIAL];
214                         gchar *p;
215                         gchar *cur;
216                         gint len = 0;
217
218                         p = msginfo->fromname;
219                         cur = tmp;
220                         while (*p) {
221                                 if (*p && isalnum(*p)) {
222                                         *cur = toupper(*p);
223                                                 cur++;
224                                         len++;
225                                         if (len >= MAX_SENDER_INITIAL - 1)
226                                                 break;
227                                 } else
228                                         break;
229                                 while (*p && !isseparator(*p)) p++;
230                                 while (*p && isseparator(*p)) p++;
231                         }
232                         *cur = '\0';
233                         INSERT(tmp);
234                 }
235         }
236         | SHOW_SUBJECT
237         {
238                 if (msginfo->subject)
239                         INSERT(msginfo->subject);
240         }
241         | SHOW_TO
242         {
243                 if (msginfo->to)
244                         INSERT(msginfo->to);
245         }
246         | SHOW_MESSAGEID
247         {
248                 if (msginfo->msgid)
249                         INSERT(msginfo->msgid);
250         }
251         | SHOW_PERCENT
252         {
253                 INSERT("%");
254         }
255         | SHOW_CC
256         {
257                 if (msginfo->cc)
258                         INSERT(msginfo->cc);
259         }
260         | SHOW_REFERENCES
261         {
262                 /* if (msginfo->references)
263                         INSERT(msginfo->references); */
264         }
265         | SHOW_MESSAGE
266         {
267                 if (msginfo->folder) {
268                         gchar buf[BUFFSIZE];
269                         FILE *fp;
270
271                         if (body)
272                                 fp = str_open_as_stream(body);
273                         else
274                                 fp = procmime_get_first_text_content(msginfo);
275
276                         if (fp == NULL)
277                                 g_warning(_("Can't get text part\n"));
278                         else {
279                                 while (fgets(buf, sizeof(buf), fp) != NULL) {
280                                         strcrchomp(buf);
281                                         INSERT(buf);
282                                 }
283                                 fclose(fp);
284                         }
285                 }
286         }
287         | SHOW_QUOTED_MESSAGE
288         {
289                 if (msginfo->folder) {
290                         gchar buf[BUFFSIZE];
291                         FILE *fp;
292
293                         if (body)
294                                 fp = str_open_as_stream(body);
295                         else
296                                 fp = procmime_get_first_text_content(msginfo);
297
298                         if (fp == NULL)
299                                 g_warning(_("Can't get text part\n"));
300                         else {
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         }
311         | SHOW_MESSAGE_NO_SIGNATURE
312         {
313                 if (msginfo->folder) {
314                         gchar buf[BUFFSIZE];
315                         FILE *fp;
316
317                         if (body)
318                                 fp = str_open_as_stream(body);
319                         else
320                                 fp = procmime_get_first_text_content(msginfo);
321
322                         if (fp == NULL)
323                                 g_warning(_("Can't get text part\n"));
324                         else {
325                                 while (fgets(buf, sizeof(buf), fp) != NULL) {
326                                         strcrchomp(buf);
327                                         if (strncmp(buf, "-- ", 3) == 0)
328                                                 break;
329                                         INSERT(buf);
330                                 }
331                                 fclose(fp);
332                         }
333                 }
334         }
335         | SHOW_QUOTED_MESSAGE_NO_SIGNATURE
336         {
337                 if (msginfo->folder) {
338                         gchar buf[BUFFSIZE];
339                         FILE *fp;
340
341                         if (body)
342                                 fp = str_open_as_stream(body);
343                         else
344                                 fp = procmime_get_first_text_content(msginfo);
345
346                         if (fp == NULL)
347                                 g_warning(_("Can't get text part\n"));
348                         else {
349                                 while (fgets(buf, sizeof(buf), fp) != NULL) {
350                                         strcrchomp(buf);
351                                         if (strncmp(buf, "-- ", 3) == 0)
352                                                 break;
353                                         if (quote_str)
354                                                 INSERT(quote_str);
355                                         INSERT(buf);
356                                 }
357                                 fclose(fp);
358                         }
359                 }
360         }
361         | SHOW_BACKSLASH
362         {
363                 INSERT("\\");
364         }
365         | SHOW_TAB
366         {
367                 INSERT("\t");
368         }
369         | SHOW_EOL
370         {
371                 INSERT("\n");
372         }
373         | SHOW_QUESTION_MARK
374         {
375                 INSERT("?");
376         }
377         | SHOW_OPARENT
378         {
379                 INSERT("{");
380         }
381         | SHOW_CPARENT
382         {
383                 INSERT("}");
384         };
385
386 query:
387         QUERY_DATE
388         {
389                 add_visibility(msginfo->date != NULL);
390         }
391         OPARENT quote_fmt CPARENT
392         {
393                 remove_visibility();
394         }
395         | QUERY_FROM
396         {
397                 add_visibility(msginfo->from != NULL);
398         }
399         OPARENT quote_fmt CPARENT
400         {
401                 remove_visibility();
402         }
403         | QUERY_FULLNAME
404         {
405                 add_visibility(msginfo->fromname != NULL);
406         }
407         OPARENT quote_fmt CPARENT
408         {
409                 remove_visibility();
410         }
411         | QUERY_SUBJECT
412         {
413                 add_visibility(msginfo->subject != NULL);
414         }
415         OPARENT quote_fmt CPARENT
416         {
417                 remove_visibility();
418         }
419         | QUERY_TO
420         {
421                 add_visibility(msginfo->to != NULL);
422         }
423         OPARENT quote_fmt CPARENT
424         {
425                 remove_visibility();
426         }
427         | QUERY_NEWSGROUPS
428         {
429                 add_visibility(msginfo->newsgroups != NULL);
430         }
431         OPARENT quote_fmt CPARENT
432         {
433                 remove_visibility();
434         }
435         | QUERY_MESSAGEID
436         {
437                 add_visibility(msginfo->msgid != NULL);
438         }
439         OPARENT quote_fmt CPARENT
440         {
441                 remove_visibility();
442         }
443         | QUERY_CC
444         {
445                 add_visibility(msginfo->cc != NULL);
446         }
447         OPARENT quote_fmt CPARENT
448         {
449                 remove_visibility();
450         }
451         | QUERY_REFERENCES
452         {
453                 /* add_visibility(msginfo->references != NULL); */
454         }
455         OPARENT quote_fmt CPARENT
456         {
457                 remove_visibility();
458         };