refixed buffer overflow
[claws.git] / src / quote_fmt_parse.y
index 495617c805ff991ea69f23a0dfbb05d31ab15b1f..a28647fb5e948278eebc07d9d1a61fc0c5b84546 100644 (file)
@@ -1,3 +1,22 @@
+/*
+ * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2002 Hiroyuki Yamamoto and the Sylpheed-Claws Team
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
 %{
 
 #include "defs.h"
@@ -8,7 +27,6 @@
 #include "procmsg.h"
 #include "procmime.h"
 #include "utils.h"
-#include "intl.h"
 #include "procheader.h"
 
 #include "quote_fmt.h"
@@ -34,6 +52,8 @@ static const gchar *quote_str = NULL;
 static const gchar *body = NULL;
 static gint error = 0;
 
+static gint cursor_pos  = 0;
+
 static void add_visibility(gboolean val)
 {
        stacksize++;
@@ -76,6 +96,11 @@ gchar *quote_fmt_get_buffer(void)
                return buffer;
 }
 
+gint quote_fmt_get_cursor_pos(void)
+{
+       return cursor_pos;      
+}
+
 #define INSERT(buf) \
        if (stacksize != 0 && visible[stacksize - 1]) \
                add_buffer(buf)
@@ -100,6 +125,7 @@ void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
                *buffer = 0;
        bufsize = 0;
        error = 0;
+       cursor_pos = 0;
 }
 
 void quote_fmterror(char *str)
@@ -113,7 +139,7 @@ int quote_fmtwrap(void)
        return 1;
 }
 
-static int isseparator(char ch)
+static int isseparator(int ch)
 {
        return isspace(ch) || ch == '.' || ch == '-';
 }
@@ -138,6 +164,7 @@ static int isseparator(char ch)
 %token OPARENT CPARENT
 %token CHARACTER
 %token SHOW_DATE_EXPR
+%token SET_CURSOR_POS
 
 %start quote_fmt
 
@@ -175,9 +202,15 @@ string:
        }
        | string CHARACTER
        {
-               strcpy($$, $1);
-               $$[strlen($$) + 1] = '\0';
-               $$[strlen($$)] = $2;
+               int len;
+               
+               strncpy($$, $1, sizeof($$));
+               $$[sizeof($$) - 1] = '\0';
+               len = strlen($$);
+               if (len + 1 < sizeof($$)) {
+                       $$[len + 1] = '\0';
+                       $$[len] = $2;
+               }
        };
 
 special:
@@ -266,16 +299,29 @@ special:
        | SHOW_FIRST_NAME
        {
                if (msginfo->fromname) {
-                       gchar *p;
+                       guchar *p;
                        gchar *str;
 
-                       str = alloca(strlen(msginfo->fromname) + 1);
-                       if (str != NULL) {
-                               strcpy(str, msginfo->fromname);
-                               p = str;
-                               while (*p && !isspace(*p)) p++;
-                               *p = '\0';
-                               INSERT(str);
+                       p = strchr(msginfo->fromname, ',');
+                       if (p != NULL) {
+                               /* fromname is like "Duck, Donald" */
+                               p++;
+                               while (*p && isspace(*p)) p++;
+                               str = alloca(strlen(p) + 1);
+                               if (str != NULL) {
+                                       strcpy(str, p);
+                                       INSERT(str);
+                               }
+                       } else {
+                               /* fromname is like "Donald Duck" */
+                               str = alloca(strlen(msginfo->fromname) + 1);
+                               if (str != NULL) {
+                                       strcpy(str, msginfo->fromname);
+                                       p = str;
+                                       while (*p && !isspace(*p)) p++;
+                                       *p = '\0';
+                                       INSERT(str);
+                               }
                        }
                }
        }
@@ -290,22 +336,33 @@ special:
                        str = alloca(strlen(msginfo->fromname) + 1);
                        if (str != NULL) {
                                strcpy(str, msginfo->fromname);
-                                p = str;
-                                while (*p && !isspace(*p)) p++;
-                                if (*p) {
-                                   /* We found a space. Get first none-space char and
-                                    insert rest of string from there. */
-                                   while (*p && isspace(*p)) p++;
-                                    if (*p) {
-                                       INSERT(p);
-                                   } else {
-                                       /* If there is no none-space char, just insert
-                                        whole fromname. */
+                               p = strchr(str, ',');
+                               if (p != NULL) {
+                                       /* fromname is like "Duck, Donald" */
+                                       *p = '\0';
                                        INSERT(str);
-                                   }
                                } else {
-                                   /* If there is no space, just insert whole fromname. */
-                                   INSERT(str);
+                                       /* fromname is like "Donald Duck" */
+                                       p = str;
+                                       while (*p && !isspace(*p)) p++;
+                                       if (*p) {
+                                           /* We found a space. Get first 
+                                            none-space char and insert
+                                            rest of string from there. */
+                                           while (*p && isspace(*p)) p++;
+                                           if (*p) {
+                                               INSERT(p);
+                                           } else {
+                                               /* If there is no none-space 
+                                                char, just insert whole 
+                                                fromname. */
+                                               INSERT(str);
+                                           }
+                                       } else {
+                                           /* If there is no space, just 
+                                            insert whole fromname. */
+                                           INSERT(str);
+                                       }
                                }
                        }
                }
@@ -315,7 +372,7 @@ special:
 #define MAX_SENDER_INITIAL 20
                if (msginfo->fromname) {
                        gchar tmp[MAX_SENDER_INITIAL];
-                       gchar *p;
+                       guchar *p;
                        gchar *cur;
                        gint len = 0;
 
@@ -428,7 +485,7 @@ special:
                        else {
                                while (fgets(buf, sizeof(buf), fp) != NULL) {
                                        strcrchomp(buf);
-                                       if (strncmp(buf, "-- ", 3) == 0)
+                                       if (strncmp(buf, "-- \n", 4) == 0)
                                                break;
                                        INSERT(buf);
                                }
@@ -452,7 +509,7 @@ special:
                        else {
                                while (fgets(buf, sizeof(buf), fp) != NULL) {
                                        strcrchomp(buf);
-                                       if (strncmp(buf, "-- ", 3) == 0)
+                                       if (strncmp(buf, "-- \n", 4) == 0)
                                                break;
                                        if (quote_str)
                                                INSERT(quote_str);
@@ -489,6 +546,10 @@ special:
        | SHOW_CPARENT
        {
                INSERT("}");
+       }
+       | SET_CURSOR_POS
+       {
+               cursor_pos = bufsize;
        };
 
 query: