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.
31 #include "procheader.h"
33 #include "quote_fmt.h"
34 #include "quote_fmt_lex.h"
39 bison -p quote_fmt quote_fmt.y
44 static MsgInfo *msginfo = NULL;
45 static gboolean *visible = NULL;
46 static gint maxsize = 0;
47 static gint stacksize = 0;
49 static gchar *buffer = NULL;
50 static gint bufmax = 0;
51 static gint bufsize = 0;
52 static const gchar *quote_str = NULL;
53 static const gchar *body = NULL;
54 static gint error = 0;
56 static void add_visibility(gboolean val)
59 if (maxsize < stacksize) {
61 visible = g_realloc(visible, maxsize * sizeof(gboolean));
66 visible[stacksize - 1] = val;
69 static void remove_visibility(void)
74 static void add_buffer(const gchar *s)
79 if (bufsize + len + 1 > bufmax) {
82 while (bufsize + len + 1 > bufmax)
84 buffer = g_realloc(buffer, bufmax);
86 strcpy(buffer + bufsize, s);
90 gchar *quote_fmt_get_buffer(void)
99 if (stacksize != 0 && visible[stacksize - 1]) \
102 #define INSERT_CHARACTER(chr) \
103 if (stacksize != 0 && visible[stacksize - 1]) { \
110 void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
111 const gchar *my_body)
113 quote_str = my_quote_str;
117 add_visibility(TRUE);
124 void quote_fmterror(char *str)
126 g_warning("Error: %s\n", str);
130 int quote_fmtwrap(void)
135 static int isseparator(char ch)
137 return isspace(ch) || ch == '.' || ch == '-';
146 %token SHOW_NEWSGROUPS
147 %token SHOW_DATE SHOW_FROM SHOW_FULLNAME SHOW_FIRST_NAME SHOW_LAST_NAME
148 %token SHOW_SENDER_INITIAL SHOW_SUBJECT SHOW_TO SHOW_MESSAGEID
149 %token SHOW_PERCENT SHOW_CC SHOW_REFERENCES SHOW_MESSAGE
150 %token SHOW_QUOTED_MESSAGE SHOW_BACKSLASH SHOW_TAB
151 %token SHOW_QUOTED_MESSAGE_NO_SIGNATURE SHOW_MESSAGE_NO_SIGNATURE
152 %token SHOW_EOL SHOW_QUESTION_MARK SHOW_PIPE SHOW_OPARENT SHOW_CPARENT
153 %token QUERY_DATE QUERY_FROM
154 %token QUERY_FULLNAME QUERY_SUBJECT QUERY_TO QUERY_NEWSGROUPS
155 %token QUERY_MESSAGEID QUERY_CC QUERY_REFERENCES
156 %token INSERT_FILE INSERT_PROGRAMOUTPUT
157 %token OPARENT CPARENT
159 %token SHOW_DATE_EXPR
163 %token <chr> CHARACTER
164 %type <chr> character
170 character_or_special_or_insert_or_query_list;
172 character_or_special_or_insert_or_query_list:
173 character_or_special_or_insert_or_query character_or_special_or_insert_or_query_list
174 | character_or_special_or_insert_or_query ;
176 character_or_special_or_insert_or_query:
180 INSERT_CHARACTER($1);
198 $$[strlen($$) + 1] = '\0';
205 if (msginfo->newsgroups)
206 INSERT(msginfo->newsgroups);
208 | SHOW_DATE_EXPR OPARENT string CPARENT
211 * ALF - GNU C's strftime() has a nice format specifier
212 * for time zone offset (%z). Non-standard however, so
223 #define RLEFT (sizeof result) - (rptr - result)
224 #define STR_SIZE(x) (sizeof (x) - 1)
228 if (procheader_date_parse_to_tm(msginfo->date, <, zone)) {
230 * break up format string in tiny bits delimited by valid %z's and
231 * feed it to strftime(). don't forget that '%%z' mean literal '%z'.
233 for (rptr = result, fptr = $3; fptr && *fptr && rptr < &result[sizeof result - 1];) {
238 if (NULL != (zptr = strstr(fptr, "%z"))) {
240 * count nr. of prepended percent chars
242 for (perc = 0, p = zptr; p && p >= $3 && *p == '%'; p--, perc++)
247 tmp = g_strndup(fptr, zptr - fptr + (perc % 2 ? 0 : STR_SIZE("%z")));
249 rptr += strftime(rptr, RLEFT, tmp, <);
253 * append time zone offset
255 if (zone[0] && perc % 2)
256 rptr += g_snprintf(rptr, RLEFT, "%s", zone);
257 fptr = zptr + STR_SIZE("%z");
259 rptr += strftime(rptr, RLEFT, fptr, <);
273 INSERT(msginfo->date);
278 INSERT(msginfo->from);
282 if (msginfo->fromname)
283 INSERT(msginfo->fromname);
287 if (msginfo->fromname) {
291 str = alloca(strlen(msginfo->fromname) + 1);
293 strcpy(str, msginfo->fromname);
295 while (*p && !isspace(*p)) p++;
303 /* This probably won't work together very well with Middle
304 names and the like - thth */
305 if (msginfo->fromname) {
309 str = alloca(strlen(msginfo->fromname) + 1);
311 strcpy(str, msginfo->fromname);
313 while (*p && !isspace(*p)) p++;
315 /* We found a space. Get first none-space char and
316 insert rest of string from there. */
317 while (*p && isspace(*p)) p++;
321 /* If there is no none-space char, just insert
326 /* If there is no space, just insert whole fromname. */
332 | SHOW_SENDER_INITIAL
334 #define MAX_SENDER_INITIAL 20
335 if (msginfo->fromname) {
336 gchar tmp[MAX_SENDER_INITIAL];
341 p = msginfo->fromname;
344 if (*p && isalnum(*p)) {
348 if (len >= MAX_SENDER_INITIAL - 1)
352 while (*p && !isseparator(*p)) p++;
353 while (*p && isseparator(*p)) p++;
361 if (msginfo->subject)
362 INSERT(msginfo->subject);
372 INSERT(msginfo->msgid);
385 /* if (msginfo->references)
386 INSERT(msginfo->references); */
390 if (msginfo->folder || body) {
395 fp = str_open_as_stream(body);
397 fp = procmime_get_first_text_content(msginfo);
400 g_warning("Can't get text part\n");
402 while (fgets(buf, sizeof(buf), fp) != NULL) {
410 | SHOW_QUOTED_MESSAGE
412 if (msginfo->folder || body) {
417 fp = str_open_as_stream(body);
419 fp = procmime_get_first_text_content(msginfo);
422 g_warning("Can't get text part\n");
424 while (fgets(buf, sizeof(buf), fp) != NULL) {
434 | SHOW_MESSAGE_NO_SIGNATURE
436 if (msginfo->folder || body) {
441 fp = str_open_as_stream(body);
443 fp = procmime_get_first_text_content(msginfo);
446 g_warning("Can't get text part\n");
448 while (fgets(buf, sizeof(buf), fp) != NULL) {
450 if (strncmp(buf, "-- \n", 4) == 0)
458 | SHOW_QUOTED_MESSAGE_NO_SIGNATURE
460 if (msginfo->folder || body) {
465 fp = str_open_as_stream(body);
467 fp = procmime_get_first_text_content(msginfo);
470 g_warning("Can't get text part\n");
472 while (fgets(buf, sizeof(buf), fp) != NULL) {
474 if (strncmp(buf, "-- \n", 4) == 0)
516 add_visibility(msginfo->date != NULL);
518 OPARENT quote_fmt CPARENT
524 add_visibility(msginfo->from != NULL);
526 OPARENT quote_fmt CPARENT
532 add_visibility(msginfo->fromname != NULL);
534 OPARENT quote_fmt CPARENT
540 add_visibility(msginfo->subject != NULL);
542 OPARENT quote_fmt CPARENT
548 add_visibility(msginfo->to != NULL);
550 OPARENT quote_fmt CPARENT
556 add_visibility(msginfo->newsgroups != NULL);
558 OPARENT quote_fmt CPARENT
564 add_visibility(msginfo->msgid != NULL);
566 OPARENT quote_fmt CPARENT
572 add_visibility(msginfo->cc != NULL);
574 OPARENT quote_fmt CPARENT
580 /* add_visibility(msginfo->references != NULL); */
582 OPARENT quote_fmt CPARENT
588 INSERT_FILE OPARENT string CPARENT
594 if((file = fopen($3, "rb")) != NULL) {
595 while(fgets(buffer, sizeof(buffer), file)) {
602 | INSERT_PROGRAMOUTPUT OPARENT string CPARENT
608 if((file = popen($3, "r")) != NULL) {
609 while(fgets(buffer, sizeof(buffer), file)) {