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 str = alloca(strlen(msginfo->fromname) + 1);
301 strcpy(str, msginfo->fromname);
303 while (*p && !isspace(*p)) p++;
311 /* This probably won't work together very well with Middle
312 names and the like - thth */
313 if (msginfo->fromname) {
317 str = alloca(strlen(msginfo->fromname) + 1);
319 strcpy(str, msginfo->fromname);
321 while (*p && !isspace(*p)) p++;
323 /* We found a space. Get first none-space char and
324 insert rest of string from there. */
325 while (*p && isspace(*p)) p++;
329 /* If there is no none-space char, just insert
334 /* If there is no space, just insert whole fromname. */
340 | SHOW_SENDER_INITIAL
342 #define MAX_SENDER_INITIAL 20
343 if (msginfo->fromname) {
344 gchar tmp[MAX_SENDER_INITIAL];
349 p = msginfo->fromname;
352 if (*p && isalnum(*p)) {
356 if (len >= MAX_SENDER_INITIAL - 1)
360 while (*p && !isseparator(*p)) p++;
361 while (*p && isseparator(*p)) p++;
369 if (msginfo->subject)
370 INSERT(msginfo->subject);
380 INSERT(msginfo->msgid);
393 /* if (msginfo->references)
394 INSERT(msginfo->references); */
398 if (msginfo->folder || body) {
403 fp = str_open_as_stream(body);
405 fp = procmime_get_first_text_content(msginfo);
408 g_warning("Can't get text part\n");
410 while (fgets(buf, sizeof(buf), fp) != NULL) {
418 | SHOW_QUOTED_MESSAGE
420 if (msginfo->folder || body) {
425 fp = str_open_as_stream(body);
427 fp = procmime_get_first_text_content(msginfo);
430 g_warning("Can't get text part\n");
432 while (fgets(buf, sizeof(buf), fp) != NULL) {
442 | SHOW_MESSAGE_NO_SIGNATURE
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) {
458 if (strncmp(buf, "-- \n", 4) == 0)
466 | SHOW_QUOTED_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)
522 cursor_pos = bufsize;
528 add_visibility(msginfo->date != NULL);
530 OPARENT quote_fmt CPARENT
536 add_visibility(msginfo->from != NULL);
538 OPARENT quote_fmt CPARENT
544 add_visibility(msginfo->fromname != NULL);
546 OPARENT quote_fmt CPARENT
552 add_visibility(msginfo->subject != NULL);
554 OPARENT quote_fmt CPARENT
560 add_visibility(msginfo->to != NULL);
562 OPARENT quote_fmt CPARENT
568 add_visibility(msginfo->newsgroups != NULL);
570 OPARENT quote_fmt CPARENT
576 add_visibility(msginfo->msgid != NULL);
578 OPARENT quote_fmt CPARENT
584 add_visibility(msginfo->cc != NULL);
586 OPARENT quote_fmt CPARENT
592 /* add_visibility(msginfo->references != NULL); */
594 OPARENT quote_fmt CPARENT
600 INSERT_FILE OPARENT string CPARENT
606 if((file = fopen($3, "rb")) != NULL) {
607 while(fgets(buffer, sizeof(buffer), file)) {
614 | INSERT_PROGRAMOUTPUT OPARENT string CPARENT
620 if((file = popen($3, "r")) != NULL) {
621 while(fgets(buffer, sizeof(buffer), file)) {