initial message templates support
[claws.git] / src / quote_fmt_parse.y
blob9cde46d3018f1a1f93cc1c07346ffdfcbc1c6255
1 %{
3 #include "defs.h"
5 #include <ctype.h>
6 #include <gtk/gtk.h>
7 #include <glib.h>
9 #include "procmsg.h"
10 #include "procmime.h"
11 #include "utils.h"
12 #include "intl.h"
14 #include "quote_fmt.h"
15 #include "quote_fmt_lex.h"
17 /* decl */
19 flex quote_fmt.l
20 bison -p quote_fmt quote_fmt.y
23 static MsgInfo * msginfo = NULL;
24 static gboolean * visible = NULL;
25 static gint maxsize = 0;
26 static gint stacksize = 0;
28 static gchar * buffer = NULL;
29 static gint bufmax = 0;
30 static gint bufsize = 0;
31 static gchar * quote_str = NULL;
32 static gint error = 0;
34 static void add_visibility(gboolean val)
36 stacksize ++;
37 if (maxsize < stacksize) {
38 maxsize += 128;
39 visible = g_realloc(visible, maxsize * sizeof(gboolean));
40 if (visible == NULL)
41 maxsize = 0;
44 visible[stacksize - 1] = val;
47 static void remove_visibility()
49 stacksize --;
53 static void add_buffer(gchar * s)
55 gint len = strlen(s);
57 if (bufsize + len + 1 > bufmax) {
58 if (bufmax == 0)
59 bufmax = 128;
60 while (bufsize + len + 1 > bufmax)
61 bufmax *= 2;
62 buffer = g_realloc(buffer, bufmax);
64 strcpy(buffer + bufsize, s);
65 bufsize += len;
68 static void flush_buffer()
70 if (buffer != NULL)
71 *buffer = 0;
72 bufsize = 0;
75 gchar * quote_fmt_get_buffer()
77 if (error != 0)
78 return NULL;
79 else
80 return buffer;
83 #define INSERT(buf) \
84 if (stacksize != 0 && visible[stacksize - 1]) \
85 add_buffer(buf)
87 #define INSERT_CHARACTER(chr) \
88 if (stacksize != 0 && visible[stacksize - 1]) { \
89 gchar tmp[2]; \
90 tmp[0] = (chr); \
91 tmp[1] = '\0'; \
92 add_buffer(tmp); \
95 void quote_fmt_init(MsgInfo * info, gchar * my_quote_str)
97 quote_str = my_quote_str;
98 msginfo = info;
99 stacksize = 0;
100 add_visibility(TRUE);
101 if (buffer != NULL)
102 *buffer = 0;
103 bufsize = 0;
104 error = 0;
107 void quote_fmterror(char * str)
109 g_warning(_("Error %s\n"), str);
110 error = 1;
113 int quote_fmtwrap(void)
115 return 1;
118 static int isseparator(char ch)
120 return isspace(ch) || ch == '.' || ch == '-';
124 %union {
125 char chr;
128 %token SHOW_NEWSGROUPS
129 %token SHOW_DATE SHOW_FROM SHOW_FULLNAME SHOW_FIRST_NAME
130 %token SHOW_SENDER_INITIAL SHOW_SUBJECT SHOW_TO SHOW_MESSAGEID
131 %token SHOW_PERCENT SHOW_CC SHOW_REFERENCES SHOW_MESSAGE
132 %token SHOW_QUOTED_MESSAGE SHOW_BACKSLASH SHOW_TAB
133 %token SHOW_QUOTED_MESSAGE_NO_SIGNATURE SHOW_MESSAGE_NO_SIGNATURE
134 %token SHOW_EOL SHOW_QUESTION_MARK SHOW_OPARENT SHOW_CPARENT
135 %token QUERY_DATE QUERY_FROM
136 %token QUERY_FULLNAME QUERY_SUBJECT QUERY_TO QUERY_NEWSGROUPS
137 %token QUERY_MESSAGEID QUERY_CC QUERY_REFERENCES
138 %token OPARENT CPARENT
139 %token CHARACTER
141 %start quote_fmt
143 %token <chr> CHARACTER
144 %type <chr> character
148 quote_fmt:
149 character_or_special_or_query_list;
151 character_or_special_or_query_list:
152 character_or_special_or_query character_or_special_or_query_list
153 | character_or_special_or_query ;
155 character_or_special_or_query:
156 special ;
157 | character
159 INSERT_CHARACTER($1);
161 | query ;
164 character:
165 CHARACTER
168 special:
169 SHOW_NEWSGROUPS
171 if (msginfo->newsgroups)
172 INSERT(msginfo->newsgroups);
174 | SHOW_DATE
176 if (msginfo->date)
177 INSERT(msginfo->date);
179 | SHOW_FROM
181 if (msginfo->from)
182 INSERT(msginfo->from);
184 | SHOW_FULLNAME
186 if (msginfo->fromname)
187 INSERT(msginfo->fromname);
189 | SHOW_FIRST_NAME
191 if (msginfo->fromname) {
192 gchar * p;
193 gchar * str;
195 str = alloca(strlen(msginfo->fromname) + 1);
196 if (str != NULL) {
197 strcpy(str, msginfo->fromname);
198 p = str;
199 while (*p && !isspace(*p)) p++;
200 *p = '\0';
201 INSERT(str);
205 | SHOW_SENDER_INITIAL
207 #define MAX_SENDER_INITIAL 20
208 if (msginfo->fromname) {
209 gchar tmp[MAX_SENDER_INITIAL];
210 gchar * p;
211 gchar * cur;
212 gint len = 0;
214 p = msginfo->fromname;
215 cur = tmp;
216 while (*p) {
217 if (*p && isalnum(*p)) {
218 *cur = toupper(*p);
219 cur ++;
220 len ++;
221 if (len >= MAX_SENDER_INITIAL - 1)
222 break;
224 else
225 break;
226 while (*p && !isseparator(*p)) p++;
227 while (*p && isseparator(*p)) p++;
229 *cur = '\0';
230 INSERT(tmp);
233 | SHOW_SUBJECT
235 if (msginfo->subject)
236 INSERT(msginfo->subject);
238 | SHOW_TO
240 if (msginfo->to)
241 INSERT(msginfo->to);
243 | SHOW_MESSAGEID
245 if (msginfo->msgid)
246 INSERT(msginfo->msgid);
248 | SHOW_PERCENT
250 INSERT("%");
252 | SHOW_CC
254 if (msginfo->cc)
255 INSERT(msginfo->cc);
257 | SHOW_REFERENCES
259 if (msginfo->references)
260 INSERT(msginfo->references);
262 | SHOW_MESSAGE
264 gchar buf[BUFFSIZE];
265 FILE * fp;
267 if ((fp = procmime_get_text_part(msginfo)) == NULL)
268 g_warning(_("Can't get text part\n"));
269 while (fgets(buf, sizeof(buf), fp) != NULL) {
270 INSERT(buf);
272 fclose(fp);
274 | SHOW_QUOTED_MESSAGE
276 gchar buf[BUFFSIZE];
277 FILE * fp;
279 if ((fp = procmime_get_text_part(msginfo)) == NULL)
280 g_warning(_("Can't get text part\n"));
281 while (fgets(buf, sizeof(buf), fp) != NULL) {
282 if (quote_str)
283 INSERT(quote_str);
284 INSERT(buf);
286 fclose(fp);
288 | SHOW_MESSAGE_NO_SIGNATURE
290 gchar buf[BUFFSIZE];
291 FILE * fp;
293 if ((fp = procmime_get_text_part(msginfo)) == NULL)
294 g_warning(_("Can't get text part\n"));
295 while (fgets(buf, sizeof(buf), fp) != NULL) {
296 if (strncmp(buf, "-- ", 3) == 0)
297 break;
298 INSERT(buf);
300 fclose(fp);
302 | SHOW_QUOTED_MESSAGE_NO_SIGNATURE
304 gchar buf[BUFFSIZE];
305 FILE * fp;
307 if ((fp = procmime_get_text_part(msginfo)) == NULL)
308 g_warning(_("Can't get text part\n"));
309 while (fgets(buf, sizeof(buf), fp) != NULL) {
310 if (strncmp(buf, "-- ", 3) == 0)
311 break;
312 if (quote_str)
313 INSERT(quote_str);
314 INSERT(buf);
316 fclose(fp);
318 | SHOW_BACKSLASH
320 INSERT("\\");
322 | SHOW_TAB
324 INSERT("\t");
326 | SHOW_EOL
328 INSERT("\n");
330 | SHOW_QUESTION_MARK
332 INSERT("?");
334 | SHOW_OPARENT
336 INSERT("(");
338 | SHOW_CPARENT
340 INSERT(")");
343 query:
344 QUERY_DATE
346 add_visibility(msginfo->date != NULL);
348 OPARENT quote_fmt CPARENT
350 remove_visibility();
352 | QUERY_FROM
354 add_visibility(msginfo->from != NULL);
356 OPARENT quote_fmt CPARENT
358 remove_visibility();
360 | QUERY_FULLNAME
362 add_visibility(msginfo->fromname != NULL);
364 OPARENT quote_fmt CPARENT
366 remove_visibility();
368 | QUERY_SUBJECT
370 add_visibility(msginfo->subject != NULL);
372 OPARENT quote_fmt CPARENT
374 remove_visibility();
376 | QUERY_TO
378 add_visibility(msginfo->to != NULL);
380 OPARENT quote_fmt CPARENT
382 remove_visibility();
384 | QUERY_NEWSGROUPS
386 add_visibility(msginfo->newsgroups != NULL);
388 OPARENT quote_fmt CPARENT
390 remove_visibility();
392 | QUERY_MESSAGEID
394 add_visibility(msginfo->msgid != NULL);
396 OPARENT quote_fmt CPARENT
398 remove_visibility();
400 | QUERY_CC
402 add_visibility(msginfo->cc != NULL);
404 OPARENT quote_fmt CPARENT
406 remove_visibility();
408 | QUERY_REFERENCES
410 add_visibility(msginfo->references != NULL);
412 OPARENT quote_fmt CPARENT
414 remove_visibility();