2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 Hiroyuki Yamamoto and the Sylpheed-Claws Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #include "procheader.h"
32 #include "quote_fmt.h"
33 #include "quote_fmt_lex.h"
38 bison -p quote_fmt quote_fmt.y
43 static MsgInfo *msginfo = NULL;
44 static gboolean *visible = NULL;
45 static gint maxsize = 0;
46 static gint stacksize = 0;
48 static gchar *buffer = NULL;
49 static gint bufmax = 0;
50 static gint bufsize = 0;
51 static const gchar *quote_str = NULL;
52 static const gchar *body = NULL;
53 static gint error = 0;
55 static gint cursor_pos = 0;
57 static void add_visibility(gboolean val)
60 if (maxsize < stacksize) {
62 visible = g_realloc(visible, maxsize * sizeof(gboolean));
67 visible[stacksize - 1] = val;
70 static void remove_visibility(void)
75 static void add_buffer(const gchar *s)
80 if (bufsize + len + 1 > bufmax) {
83 while (bufsize + len + 1 > bufmax)
85 buffer = g_realloc(buffer, bufmax);
87 strcpy(buffer + bufsize, s);
91 gchar *quote_fmt_get_buffer(void)
99 gint quote_fmt_get_cursor_pos(void)
104 #define INSERT(buf) \
105 if (stacksize != 0 && visible[stacksize - 1]) \
108 #define INSERT_CHARACTER(chr) \
109 if (stacksize != 0 && visible[stacksize - 1]) { \
116 void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
117 const gchar *my_body)
119 quote_str = my_quote_str;
123 add_visibility(TRUE);
131 void quote_fmterror(char *str)
133 g_warning("Error: %s\n", str);
137 int quote_fmtwrap(void)
142 static int isseparator(int ch)
144 return isspace(ch) || ch == '.' || ch == '-';
153 %token SHOW_NEWSGROUPS
154 %token SHOW_DATE SHOW_FROM SHOW_FULLNAME SHOW_FIRST_NAME SHOW_LAST_NAME
155 %token SHOW_SENDER_INITIAL SHOW_SUBJECT SHOW_TO SHOW_MESSAGEID
156 %token SHOW_PERCENT SHOW_CC SHOW_REFERENCES SHOW_MESSAGE
157 %token SHOW_QUOTED_MESSAGE SHOW_BACKSLASH SHOW_TAB
158 %token SHOW_QUOTED_MESSAGE_NO_SIGNATURE SHOW_MESSAGE_NO_SIGNATURE
159 %token SHOW_EOL SHOW_QUESTION_MARK SHOW_PIPE SHOW_OPARENT SHOW_CPARENT
160 %token QUERY_DATE QUERY_FROM
161 %token QUERY_FULLNAME QUERY_SUBJECT QUERY_TO QUERY_NEWSGROUPS
162 %token QUERY_MESSAGEID QUERY_CC QUERY_REFERENCES
163 %token INSERT_FILE INSERT_PROGRAMOUTPUT
164 %token OPARENT CPARENT
166 %token SHOW_DATE_EXPR
167 %token SET_CURSOR_POS
171 %token <chr> CHARACTER
172 %type <chr> character
178 character_or_special_or_insert_or_query_list;
180 character_or_special_or_insert_or_query_list:
181 character_or_special_or_insert_or_query character_or_special_or_insert_or_query_list
182 | character_or_special_or_insert_or_query ;
184 character_or_special_or_insert_or_query:
188 INSERT_CHARACTER($1);
206 $$[strlen($$) + 1] = '\0';
213 if (msginfo->newsgroups)
214 INSERT(msginfo->newsgroups);
216 | SHOW_DATE_EXPR OPARENT string CPARENT
219 * ALF - GNU C's strftime() has a nice format specifier
220 * for time zone offset (%z). Non-standard however, so
231 #define RLEFT (sizeof result) - (rptr - result)
232 #define STR_SIZE(x) (sizeof (x) - 1)
236 if (procheader_date_parse_to_tm(msginfo->date, <, zone)) {
238 * break up format string in tiny bits delimited by valid %z's and
239 * feed it to strftime(). don't forget that '%%z' mean literal '%z'.
241 for (rptr = result, fptr = $3; fptr && *fptr && rptr < &result[sizeof result - 1];) {
246 if (NULL != (zptr = strstr(fptr, "%z"))) {
248 * count nr. of prepended percent chars
250 for (perc = 0, p = zptr; p && p >= $3 && *p == '%'; p--, perc++)
255 tmp = g_strndup(fptr, zptr - fptr + (perc % 2 ? 0 : STR_SIZE("%z")));
257 rptr += strftime(rptr, RLEFT, tmp, <);
261 * append time zone offset
263 if (zone[0] && perc % 2)
264 rptr += g_snprintf(rptr, RLEFT, "%s", zone);
265 fptr = zptr + STR_SIZE("%z");
267 rptr += strftime(rptr, RLEFT, fptr, <);
281 INSERT(msginfo->date);
286 INSERT(msginfo->from);
290 if (msginfo->fromname)
291 INSERT(msginfo->fromname);
295 if (msginfo->fromname) {
299 p = strchr(msginfo->fromname, ',');
301 /* fromname is like "Duck, Donald" */
303 while (*p && isspace(*p)) p++;
304 str = alloca(strlen(p) + 1);
310 /* fromname is like "Donald Duck" */
311 str = alloca(strlen(msginfo->fromname) + 1);
313 strcpy(str, msginfo->fromname);
315 while (*p && !isspace(*p)) p++;
324 /* This probably won't work together very well with Middle
325 names and the like - thth */
326 if (msginfo->fromname) {
330 str = alloca(strlen(msginfo->fromname) + 1);
332 strcpy(str, msginfo->fromname);
333 p = strchr(str, ',');
335 /* fromname is like "Duck, Donald" */
339 /* fromname is like "Donald Duck" */
341 while (*p && !isspace(*p)) p++;
343 /* We found a space. Get first
344 none-space char and insert
345 rest of string from there. */
346 while (*p && isspace(*p)) p++;
350 /* If there is no none-space
351 char, just insert whole
356 /* If there is no space, just
357 insert whole fromname. */
364 | SHOW_SENDER_INITIAL
366 #define MAX_SENDER_INITIAL 20
367 if (msginfo->fromname) {
368 gchar tmp[MAX_SENDER_INITIAL];
373 p = msginfo->fromname;
376 if (*p && isalnum(*p)) {
380 if (len >= MAX_SENDER_INITIAL - 1)
384 while (*p && !isseparator(*p)) p++;
385 while (*p && isseparator(*p)) p++;
393 if (msginfo->subject)
394 INSERT(msginfo->subject);
404 INSERT(msginfo->msgid);
417 /* if (msginfo->references)
418 INSERT(msginfo->references); */
422 if (msginfo->folder || body) {
427 fp = str_open_as_stream(body);
429 fp = procmime_get_first_text_content(msginfo);
432 g_warning("Can't get text part\n");
434 while (fgets(buf, sizeof(buf), fp) != NULL) {
442 | SHOW_QUOTED_MESSAGE
444 if (msginfo->folder || body) {
449 fp = str_open_as_stream(body);
451 fp = procmime_get_first_text_content(msginfo);
454 g_warning("Can't get text part\n");
456 while (fgets(buf, sizeof(buf), fp) != NULL) {
466 | SHOW_MESSAGE_NO_SIGNATURE
468 if (msginfo->folder || body) {
473 fp = str_open_as_stream(body);
475 fp = procmime_get_first_text_content(msginfo);
478 g_warning("Can't get text part\n");
480 while (fgets(buf, sizeof(buf), fp) != NULL) {
482 if (strncmp(buf, "-- \n", 4) == 0)
490 | SHOW_QUOTED_MESSAGE_NO_SIGNATURE
492 if (msginfo->folder || body) {
497 fp = str_open_as_stream(body);
499 fp = procmime_get_first_text_content(msginfo);
502 g_warning("Can't get text part\n");
504 while (fgets(buf, sizeof(buf), fp) != NULL) {
506 if (strncmp(buf, "-- \n", 4) == 0)
546 cursor_pos = bufsize;
552 add_visibility(msginfo->date != NULL);
554 OPARENT quote_fmt CPARENT
560 add_visibility(msginfo->from != NULL);
562 OPARENT quote_fmt CPARENT
568 add_visibility(msginfo->fromname != NULL);
570 OPARENT quote_fmt CPARENT
576 add_visibility(msginfo->subject != NULL);
578 OPARENT quote_fmt CPARENT
584 add_visibility(msginfo->to != NULL);
586 OPARENT quote_fmt CPARENT
592 add_visibility(msginfo->newsgroups != NULL);
594 OPARENT quote_fmt CPARENT
600 add_visibility(msginfo->msgid != NULL);
602 OPARENT quote_fmt CPARENT
608 add_visibility(msginfo->cc != NULL);
610 OPARENT quote_fmt CPARENT
616 /* add_visibility(msginfo->references != NULL); */
618 OPARENT quote_fmt CPARENT
624 INSERT_FILE OPARENT string CPARENT
630 if((file = fopen($3, "rb")) != NULL) {
631 while(fgets(buffer, sizeof(buffer), file)) {
638 | INSERT_PROGRAMOUTPUT OPARENT string CPARENT
644 if((file = popen($3, "r")) != NULL) {
645 while(fgets(buffer, sizeof(buffer), file)) {