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);
207 strncpy($$, $1, sizeof($$));
208 $$[sizeof($$) - 1] = '\0';
210 if (len + 1 < sizeof($$)) {
219 if (msginfo->newsgroups)
220 INSERT(msginfo->newsgroups);
222 | SHOW_DATE_EXPR OPARENT string CPARENT
225 * ALF - GNU C's strftime() has a nice format specifier
226 * for time zone offset (%z). Non-standard however, so
237 #define RLEFT (sizeof result) - (rptr - result)
238 #define STR_SIZE(x) (sizeof (x) - 1)
242 if (procheader_date_parse_to_tm(msginfo->date, <, zone)) {
244 * break up format string in tiny bits delimited by valid %z's and
245 * feed it to strftime(). don't forget that '%%z' mean literal '%z'.
247 for (rptr = result, fptr = $3; fptr && *fptr && rptr < &result[sizeof result - 1];) {
252 if (NULL != (zptr = strstr(fptr, "%z"))) {
254 * count nr. of prepended percent chars
256 for (perc = 0, p = zptr; p && p >= $3 && *p == '%'; p--, perc++)
261 tmp = g_strndup(fptr, zptr - fptr + (perc % 2 ? 0 : STR_SIZE("%z")));
263 rptr += strftime(rptr, RLEFT, tmp, <);
267 * append time zone offset
269 if (zone[0] && perc % 2)
270 rptr += g_snprintf(rptr, RLEFT, "%s", zone);
271 fptr = zptr + STR_SIZE("%z");
273 rptr += strftime(rptr, RLEFT, fptr, <);
287 INSERT(msginfo->date);
292 INSERT(msginfo->from);
296 if (msginfo->fromname)
297 INSERT(msginfo->fromname);
301 if (msginfo->fromname) {
305 p = strchr(msginfo->fromname, ',');
307 /* fromname is like "Duck, Donald" */
309 while (*p && isspace(*p)) p++;
310 str = alloca(strlen(p) + 1);
316 /* fromname is like "Donald Duck" */
317 str = alloca(strlen(msginfo->fromname) + 1);
319 strcpy(str, msginfo->fromname);
321 while (*p && !isspace(*p)) p++;
330 /* This probably won't work together very well with Middle
331 names and the like - thth */
332 if (msginfo->fromname) {
336 str = alloca(strlen(msginfo->fromname) + 1);
338 strcpy(str, msginfo->fromname);
339 p = strchr(str, ',');
341 /* fromname is like "Duck, Donald" */
345 /* fromname is like "Donald Duck" */
347 while (*p && !isspace(*p)) p++;
349 /* We found a space. Get first
350 none-space char and insert
351 rest of string from there. */
352 while (*p && isspace(*p)) p++;
356 /* If there is no none-space
357 char, just insert whole
362 /* If there is no space, just
363 insert whole fromname. */
370 | SHOW_SENDER_INITIAL
372 #define MAX_SENDER_INITIAL 20
373 if (msginfo->fromname) {
374 gchar tmp[MAX_SENDER_INITIAL];
379 p = msginfo->fromname;
382 if (*p && isalnum(*p)) {
386 if (len >= MAX_SENDER_INITIAL - 1)
390 while (*p && !isseparator(*p)) p++;
391 while (*p && isseparator(*p)) p++;
399 if (msginfo->subject)
400 INSERT(msginfo->subject);
410 INSERT(msginfo->msgid);
423 /* if (msginfo->references)
424 INSERT(msginfo->references); */
428 if (msginfo->folder || body) {
433 fp = str_open_as_stream(body);
435 fp = procmime_get_first_text_content(msginfo);
438 g_warning("Can't get text part\n");
440 while (fgets(buf, sizeof(buf), fp) != NULL) {
448 | SHOW_QUOTED_MESSAGE
450 if (msginfo->folder || body) {
455 fp = str_open_as_stream(body);
457 fp = procmime_get_first_text_content(msginfo);
460 g_warning("Can't get text part\n");
462 while (fgets(buf, sizeof(buf), fp) != NULL) {
472 | SHOW_MESSAGE_NO_SIGNATURE
474 if (msginfo->folder || body) {
479 fp = str_open_as_stream(body);
481 fp = procmime_get_first_text_content(msginfo);
484 g_warning("Can't get text part\n");
486 while (fgets(buf, sizeof(buf), fp) != NULL) {
488 if (strncmp(buf, "-- \n", 4) == 0)
496 | SHOW_QUOTED_MESSAGE_NO_SIGNATURE
498 if (msginfo->folder || body) {
503 fp = str_open_as_stream(body);
505 fp = procmime_get_first_text_content(msginfo);
508 g_warning("Can't get text part\n");
510 while (fgets(buf, sizeof(buf), fp) != NULL) {
512 if (strncmp(buf, "-- \n", 4) == 0)
552 cursor_pos = bufsize;
558 add_visibility(msginfo->date != NULL);
560 OPARENT quote_fmt CPARENT
566 add_visibility(msginfo->from != NULL);
568 OPARENT quote_fmt CPARENT
574 add_visibility(msginfo->fromname != NULL);
576 OPARENT quote_fmt CPARENT
582 add_visibility(msginfo->subject != NULL);
584 OPARENT quote_fmt CPARENT
590 add_visibility(msginfo->to != NULL);
592 OPARENT quote_fmt CPARENT
598 add_visibility(msginfo->newsgroups != NULL);
600 OPARENT quote_fmt CPARENT
606 add_visibility(msginfo->msgid != NULL);
608 OPARENT quote_fmt CPARENT
614 add_visibility(msginfo->cc != NULL);
616 OPARENT quote_fmt CPARENT
622 /* add_visibility(msginfo->references != NULL); */
624 OPARENT quote_fmt CPARENT
630 INSERT_FILE OPARENT string CPARENT
636 if((file = fopen($3, "rb")) != NULL) {
637 while(fgets(buffer, sizeof(buffer), file)) {
644 | INSERT_PROGRAMOUTPUT OPARENT string CPARENT
650 if((file = popen($3, "r")) != NULL) {
651 while(fgets(buffer, sizeof(buffer), file)) {