[1154] in BarnOwl Developers

home help back first fref pref prev next nref lref last post

[D-O-H] r1102 - trunk/owl

daemon@ATHENA.MIT.EDU (nelhage@MIT.EDU)
Thu Oct 29 18:13:34 2009

Resent-From: nelhage@mit.edu
Resent-To: barnowl-dev-mtg@charon.mit.edu
X-Original-To: nelhage@nelhage.com
To: dirty-owl-hackers@MIT.EDU
From: nelhage@MIT.EDU
Reply-to: dirty-owl-hackers@MIT.EDU
Date: Sun, 10 Aug 2008 17:38:44 -0400 (EDT)

Author: nelhage
Date: 2008-08-10 17:38:44 -0400 (Sun, 10 Aug 2008)
New Revision: 1102

Modified:
   trunk/owl/filter.c
   trunk/owl/filterelement.c
   trunk/owl/functions.c
   trunk/owl/owl.h
Log:
Rewrite owl_filter_print to use GString.

barnowl will no longer segfault on `show filter' with filters over
5000 characters or so.


Modified: trunk/owl/filter.c
===================================================================
--- trunk/owl/filter.c	2008-08-10 14:53:16 UTC (rev 1101)
+++ trunk/owl/filter.c	2008-08-10 21:38:44 UTC (rev 1102)
@@ -211,50 +211,55 @@
 }
 
 
-void owl_filter_print(owl_filter *f, char *out)
+char* owl_filter_print(owl_filter *f)
 {
-  strcpy(out, owl_filter_get_name(f));
-  strcat(out, ": ");
+  GString *out = g_string_new(owl_filter_get_name(f));
 
+  g_string_append(out, ": ");
+
   if (f->fgcolor!=OWL_COLOR_DEFAULT) {
-    strcat(out, "-c ");
+    g_string_append(out, "-c ");
     if (f->fgcolor < 8) {
-      strcat(out, owl_util_color_to_string(f->fgcolor));
+      g_string_append(out, owl_util_color_to_string(f->fgcolor));
     }
     else {
-      char* c = owl_sprintf("%i",f->fgcolor);
-      strcat(out, c);
-      owl_free(c);
+      g_string_append_printf(out, "%i",f->fgcolor);
     }
-    strcat(out, " ");
+    g_string_append(out, " ");
   }
   if (f->bgcolor!=OWL_COLOR_DEFAULT) {
-    strcat(out, "-b ");
+    g_string_append(out, "-b ");
     if (f->bgcolor < 8) {
-      strcat(out, owl_util_color_to_string(f->bgcolor));
+      g_string_append(out, owl_util_color_to_string(f->bgcolor));
     }
     else {
-      char* c = owl_sprintf("%i",f->bgcolor);
-      strcat(out, c);
-      owl_free(c);
+      g_string_append_printf(out, "%i",f->fgcolor);
     }
-    strcat(out, " ");
+    g_string_append(out, " ");
   }
-  if(!f->root) return;
-  owl_filterelement_print(f->root, out);
-  strcat(out, "\n");
+  if(f->root) {
+    owl_filterelement_print(f->root, out);
+    g_string_append(out, "\n");
+  }
+
+  return g_string_free(out, 0);
 }
 
 /* Return 1 if the filters 'a' and 'b' are equivalent, 0 otherwise */
 int owl_filter_equiv(owl_filter *a, owl_filter *b)
 {
-  char buff[LINE], buff2[LINE];
+  char *buffa, *buffb;
+  int ret;
 
-  owl_filter_print(a, buff);
-  owl_filter_print(b, buff2);
+  buffa = owl_filter_print(a);
+  buffb = owl_filter_print(b);
 
-  if (!strcmp(buff, buff2)) return(1);
-  return(0);
+  ret = !strcmp(buffa, buffb);
+
+  owl_free(buffa);
+  owl_free(buffb);
+
+  return ret;
 }
 
 

Modified: trunk/owl/filterelement.c
===================================================================
--- trunk/owl/filterelement.c	2008-08-10 14:53:16 UTC (rev 1101)
+++ trunk/owl/filterelement.c	2008-08-10 21:38:44 UTC (rev 1102)
@@ -125,65 +125,65 @@
 
 /* Print methods */
 
-static void owl_filterelement_print_true(owl_filterelement *fe, char *buf)
+static void owl_filterelement_print_true(owl_filterelement *fe, GString *buf)
 {
-  strcat(buf, "true");
+  g_string_append(buf, "true");
 }
 
-static void owl_filterelement_print_false(owl_filterelement *fe, char *buf)
+static void owl_filterelement_print_false(owl_filterelement *fe, GString *buf)
 {
-  strcat(buf, "false");
+  g_string_append(buf, "false");
 }
 
-static void owl_filterelement_print_re(owl_filterelement *fe, char *buf)
+static void owl_filterelement_print_re(owl_filterelement *fe, GString *buf)
 {
   char *re, *q;
-  strcat(buf, fe->field);
-  strcat(buf, " ");
+  g_string_append(buf, fe->field);
+  g_string_append(buf, " ");
 
   re = owl_regex_get_string(&(fe->re));
   q = owl_getquoting(re);
-  strcat(buf, q);
-  strcat(buf, re); 
-  strcat(buf, q);
+  g_string_append(buf, q);
+  g_string_append(buf, re);
+  g_string_append(buf, q);
 }
 
-static void owl_filterelement_print_filter(owl_filterelement *fe, char *buf)
+static void owl_filterelement_print_filter(owl_filterelement *fe, GString *buf)
 {
-  strcat(buf, "filter ");
-  strcat(buf, fe->field);
+  g_string_append(buf, "filter ");
+  g_string_append(buf, fe->field);
 }
 
-static void owl_filterelement_print_perl(owl_filterelement *fe, char *buf)
+static void owl_filterelement_print_perl(owl_filterelement *fe, GString *buf)
 {
-  strcat(buf, "perl ");
-  strcat(buf, fe->field);
+  g_string_append(buf, "perl ");
+  g_string_append(buf, fe->field);
 }
 
-static void owl_filterelement_print_group(owl_filterelement *fe, char *buf)
+static void owl_filterelement_print_group(owl_filterelement *fe, GString *buf)
 {
-  strcat(buf, "( ");
+  g_string_append(buf, "( ");
   owl_filterelement_print(fe->left, buf) ;
-  strcat(buf, " )");
+  g_string_append(buf, " )");
 }
 
-static void owl_filterelement_print_or(owl_filterelement *fe, char *buf)
+static void owl_filterelement_print_or(owl_filterelement *fe, GString *buf)
 {
   owl_filterelement_print(fe->left, buf);
-  strcat(buf, " or ");
+  g_string_append(buf, " or ");
   owl_filterelement_print(fe->right, buf);
 }
 
-static void owl_filterelement_print_and(owl_filterelement *fe, char *buf)
+static void owl_filterelement_print_and(owl_filterelement *fe, GString *buf)
 {
   owl_filterelement_print(fe->left, buf);
-  strcat(buf, " and ");
+  g_string_append(buf, " and ");
   owl_filterelement_print(fe->right, buf);
 }
 
-static void owl_filterelement_print_not(owl_filterelement *fe, char *buf)
+static void owl_filterelement_print_not(owl_filterelement *fe, GString *buf)
 {
-  strcat(buf, " not ");
+  g_string_append(buf, " not ");
   owl_filterelement_print(fe->left, buf);
 }
 
@@ -320,7 +320,7 @@
   owl_regex_free(&(fe->re));
 }
 
-void owl_filterelement_print(owl_filterelement *fe, char *buf)
+void owl_filterelement_print(owl_filterelement *fe, GString *buf)
 {
   if(!fe || !fe->print_elt) return;
   fe->print_elt(fe, buf);

Modified: trunk/owl/functions.c
===================================================================
--- trunk/owl/functions.c	2008-08-10 14:53:16 UTC (rev 1101)
+++ trunk/owl/functions.c	2008-08-10 21:38:44 UTC (rev 1102)
@@ -2514,15 +2514,16 @@
 void owl_function_show_filter(char *name)
 {
   owl_filter *f;
-  char buff[5000];
+  char *buff;
 
   f=owl_global_get_filter(&g, name);
   if (!f) {
     owl_function_error("There is no filter named %s", name);
     return;
   }
-  owl_filter_print(f, buff);
+  buff = owl_filter_print(f);
   owl_function_popless_text(buff);
+  owl_free(buff);
 }
 
 void owl_function_show_zpunts()
@@ -2530,6 +2531,7 @@
   owl_filter *f;
   owl_list *fl;
   char buff[5000];
+  char *tmp;
   owl_fmtext fm;
   int i, j;
 
@@ -2543,7 +2545,9 @@
     f=owl_list_get_element(fl, i);
     snprintf(buff, sizeof(buff), "[% 2d] ", i+1);
     owl_fmtext_append_normal(&fm, buff);
-    owl_filter_print(f, buff);
+    tmp = owl_filter_print(f);
+    owl_fmtext_append_normal(&fm, tmp);
+    owl_free(tmp);
     owl_fmtext_append_normal(&fm, buff);
   }
   owl_function_popless_fmtext(&fm);

Modified: trunk/owl/owl.h
===================================================================
--- trunk/owl/owl.h	2008-08-10 14:53:16 UTC (rev 1101)
+++ trunk/owl/owl.h	2008-08-10 21:38:44 UTC (rev 1102)
@@ -405,7 +405,7 @@
 typedef struct _owl_filterelement {
   int (*match_message)(struct _owl_filterelement *fe, owl_message *m);
   /* Append a string representation of the filterelement onto buf*/
-  void (*print_elt)(struct _owl_filterelement *fe, char * buf);
+  void (*print_elt)(struct _owl_filterelement *fe, GString *buf);
   /* Operands for and,or,not*/
   struct _owl_filterelement *left, *right;
   /* For regex filters*/


home help back first fref pref prev next nref lref last post