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 void add_visibility(gboolean val)
58 if (maxsize < stacksize) {
60 visible = g_realloc(visible, maxsize * sizeof(gboolean));
65 visible[stacksize - 1] = val;
68 static void remove_visibility(void)
73 static void add_buffer(const gchar *s)
78 if (bufsize + len + 1 > bufmax) {
81 while (bufsize + len + 1 > bufmax)
83 buffer = g_realloc(buffer, bufmax);
85 strcpy(buffer + bufsize, s);
89 gchar *quote_fmt_get_buffer(void)
98 if (stacksize != 0 && visible[stacksize - 1]) \
101 #define INSERT_CHARACTER(chr) \
102 if (stacksize != 0 && visible[stacksize - 1]) { \
109 void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
110 const gchar *my_body)
112 quote_str = my_quote_str;
116 add_visibility(TRUE);
123 void quote_fmterror(char *str)
125 g_warning("Error: %s\n", str);
129 int quote_fmtwrap(void)
134 static int isseparator(char ch)
136 return isspace(ch) || ch == '.' || ch == '-';
145 %token SHOW_NEWSGROUPS
146 %token SHOW_DATE SHOW_FROM SHOW_FULLNAME SHOW_FIRST_NAME SHOW_LAST_NAME
147 %token SHOW_SENDER_INITIAL SHOW_SUBJECT SHOW_TO SHOW_MESSAGEID
148 %token SHOW_PERCENT SHOW_CC SHOW_REFERENCES SHOW_MESSAGE
149 %token SHOW_QUOTED_MESSAGE SHOW_BACKSLASH SHOW_TAB
150 %token SHOW_QUOTED_MESSAGE_NO_SIGNATURE SHOW_MESSAGE_NO_SIGNATURE
151 %token SHOW_EOL SHOW_QUESTION_MARK SHOW_PIPE SHOW_OPARENT SHOW_CPARENT
152 %token QUERY_DATE QUERY_FROM
153 %token QUERY_FULLNAME QUERY_SUBJECT QUERY_TO QUERY_NEWSGROUPS
154 %token QUERY_MESSAGEID QUERY_CC QUERY_REFERENCES
155 %token INSERT_FILE INSERT_PROGRAMOUTPUT
156 %token OPARENT CPARENT
158 %token SHOW_DATE_EXPR
162 %token <chr> CHARACTER
163 %type <chr> character
169 character_or_special_or_insert_or_query_list;
171 character_or_special_or_insert_or_query_list:
172 character_or_special_or_insert_or_query character_or_special_or_insert_or_query_list
173 | character_or_special_or_insert_or_query ;
175 character_or_special_or_insert_or_query:
179 INSERT_CHARACTER($1);
197 $$[strlen($$) + 1] = '\0';
204 if (msginfo->newsgroups)
205 INSERT(msginfo->newsgroups);
207 | SHOW_DATE_EXPR OPARENT string CPARENT
210 * ALF - GNU C's strftime() has a nice format specifier
211 * for time zone offset (%z). Non-standard however, so
222 #define RLEFT (sizeof result) - (rptr - result)
223 #define STR_SIZE(x) (sizeof (x) - 1)
227 if (procheader_date_parse_to_tm(msginfo->date, <, zone)) {
229 * break up format string in tiny bits delimited by valid %z's and
230 * feed it to strftime(). don't forget that '%%z' mean literal '%z'.
232 for (rptr = result, fptr = $3; fptr && *fptr && rptr < &result[sizeof result - 1];) {
237 if (NULL != (zptr = strstr(fptr, "%z"))) {
239 * count nr. of prepended percent chars
241 for (perc = 0, p = zptr; p && p >= $3 && *p == '%'; p--, perc++)
246 tmp = g_strndup(fptr, zptr - fptr + (perc % 2 ? 0 : STR_SIZE("%z")));
248 rptr += strftime(rptr, RLEFT, tmp, <);
252 * append time zone offset
254 if (zone[0] && perc % 2)
255 rptr += g_snprintf(rptr, RLEFT, "%s", zone);
256 fptr = zptr + STR_SIZE("%z");
258 rptr += strftime(rptr, RLEFT, fptr, <);
272 INSERT(msginfo->date);
277 INSERT(msginfo->from);
281 if (msginfo->fromname)
282 INSERT(msginfo->fromname);
286 if (msginfo->fromname) {
290 str = alloca(strlen(msginfo->fromname) + 1);
292 strcpy(str, msginfo->fromname);
294 while (*p && !isspace(*p)) p++;
302 /* This probably won't work together very well with Middle
303 names and the like - thth */
304 if (msginfo->fromname) {
308 str = alloca(strlen(msginfo->fromname) + 1);
310 strcpy(str, msginfo->fromname);
312 while (*p && !isspace(*p)) p++;
314 /* We found a space. Get first none-space char and
315 insert rest of string from there. */
316 while (*p && isspace(*p)) p++;
320 /* If there is no none-space char, just insert
325 /* If there is no space, just insert whole fromname. */
331 | SHOW_SENDER_INITIAL
333 #define MAX_SENDER_INITIAL 20
334 if (msginfo->fromname) {
335 gchar tmp[MAX_SENDER_INITIAL];
340 p = msginfo->fromname;
343 if (*p && isalnum(*p)) {
347 if (len >= MAX_SENDER_INITIAL - 1)
351 while (*p && !isseparator(*p)) p++;
352 while (*p && isseparator(*p)) p++;
360 if (msginfo->subject)
361 INSERT(msginfo->subject);
371 INSERT(msginfo->msgid);
384 /* if (msginfo->references)
385 INSERT(msginfo->references); */
389 if (msginfo->folder || body) {
394 fp = str_open_as_stream(body);
396 fp = procmime_get_first_text_content(msginfo);
399 g_warning("Can't get text part\n");
401 while (fgets(buf, sizeof(buf), fp) != NULL) {
409 | SHOW_QUOTED_MESSAGE
411 if (msginfo->folder || body) {
416 fp = str_open_as_stream(body);
418 fp = procmime_get_first_text_content(msginfo);
421 g_warning("Can't get text part\n");
423 while (fgets(buf, sizeof(buf), fp) != NULL) {
433 | SHOW_MESSAGE_NO_SIGNATURE
435 if (msginfo->folder || body) {
440 fp = str_open_as_stream(body);
442 fp = procmime_get_first_text_content(msginfo);
445 g_warning("Can't get text part\n");
447 while (fgets(buf, sizeof(buf), fp) != NULL) {
449 if (strncmp(buf, "-- \n", 4) == 0)
457 | SHOW_QUOTED_MESSAGE_NO_SIGNATURE
459 if (msginfo->folder || body) {
464 fp = str_open_as_stream(body);
466 fp = procmime_get_first_text_content(msginfo);
469 g_warning("Can't get text part\n");
471 while (fgets(buf, sizeof(buf), fp) != NULL) {
473 if (strncmp(buf, "-- \n", 4) == 0)
515 add_visibility(msginfo->date != NULL);
517 OPARENT quote_fmt CPARENT
523 add_visibility(msginfo->from != NULL);
525 OPARENT quote_fmt CPARENT
531 add_visibility(msginfo->fromname != NULL);
533 OPARENT quote_fmt CPARENT
539 add_visibility(msginfo->subject != NULL);
541 OPARENT quote_fmt CPARENT
547 add_visibility(msginfo->to != NULL);
549 OPARENT quote_fmt CPARENT
555 add_visibility(msginfo->newsgroups != NULL);
557 OPARENT quote_fmt CPARENT
563 add_visibility(msginfo->msgid != NULL);
565 OPARENT quote_fmt CPARENT
571 add_visibility(msginfo->cc != NULL);
573 OPARENT quote_fmt CPARENT
579 /* add_visibility(msginfo->references != NULL); */
581 OPARENT quote_fmt CPARENT
587 INSERT_FILE OPARENT string CPARENT
593 if((file = fopen($3, "rb")) != NULL) {
594 while(fgets(buffer, sizeof(buffer), file)) {
601 | INSERT_PROGRAMOUTPUT OPARENT string CPARENT
607 if((file = popen($3, "r")) != NULL) {
608 while(fgets(buffer, sizeof(buffer), file)) {