[PATCH]: define keyword to substitute colors in syntax files



Hello,

this patch allows new "define" keyword in builtin editor syntax files.
Now only colors can be substituted, but it can be improved on request.
Next construction is legal now:

context default
    define comment brown
    keyword /\* comment
    keyword \*/ comment
    keyword //  comment

ChangeLog:

* syntax.c (defines): New static variable for list of defines.
(mc_defines_destroy): New function to release memory of key pair.
(destroy_defines): New function to destroy list of defines completely.
(this_try_alloc_color_pair): Use strncpy() instead of unsafe strcpy().
Use values from list of defines to substitute given parameters.
(edit_read_syntax_rules): Initialize list of defines and process 
new "define" keyword. Use strncpy() instead of unsafe strcpy().
(edit_free_syntax_rules): Destroy list of defines.

Index: syntax.c
===================================================================
RCS file: /cvs/gnome/mc/edit/syntax.c,v
retrieving revision 1.42
diff -u -p -r1.42 syntax.c
--- syntax.c	15 Dec 2002 18:55:53 -0000	1.42
+++ syntax.c	25 Feb 2003 07:40:49 -0000
@@ -83,6 +83,27 @@ int option_syntax_highlighting = 1;
 
 #define syntax_g_free(x) do {if(x) {g_free(x); (x)=0;}} while (0)
 
+/* List of defines */
+static GTree *defines;
+
+static gint
+mc_defines_destroy (gpointer key, gpointer value, gpointer data)
+{
+    g_free (key);
+    g_free (value);
+
+    return FALSE;
+}
+
+/* Completely destroys the defines tree */
+static void
+destroy_defines (void)
+{
+    g_tree_traverse (defines, mc_defines_destroy, G_POST_ORDER, NULL);
+    g_tree_destroy (defines);
+    defines = 0;
+}
+
 static long compare_word_to_right (WEdit * edit, long i, char *text, char *whole_left, char *whole_right, int line_start)
 {
@@ -534,18 +555,24 @@ this_try_alloc_color_pair (char *fg, cha
 	if (!*fg)
 	    fg = 0;
     if (fg) {
-	strcpy (f, fg);
+	p = g_tree_lookup (defines, fg);
+	if (p)
+	    fg = p;
+	strncpy (f, fg, sizeof (f) - 1);
+	f[sizeof (f) - 1] = 0;
 	p = strchr (f, '/');
 	if (p)
 	    *p = '\0';
-	fg = f;
     }
     if (bg) {
-	strcpy (b, bg);
+	p = g_tree_lookup (defines, bg);
+	if (p)
+	    bg = p;
+	strncpy (b, bg, sizeof (b) - 1);
+	b[sizeof (b) - 1] = 0;
 	p = strchr (b, '/');
 	if (p)
 	    *p = '\0';
-	bg = b;
     }
     return try_alloc_color_pair (fg, bg);
 }
@@ -596,6 +623,8 @@ static int edit_read_syntax_rules (WEdit
 
     r = edit->rules = g_malloc0 (MAX_CONTEXTS * sizeof (struct context_rule *));
 
+    defines = g_tree_new ((GCompareFunc) strcmp);
+
     for (;;) {
 	char **a;
 	line++;
@@ -705,8 +734,10 @@ static int edit_read_syntax_rules (WEdit
 	    bg = *a;
 	    if (*a)
 		a++;
-	    strcpy (last_fg, fg ? fg : "");
-	    strcpy (last_bg, bg ? bg : "");
+	    strncpy (last_fg, fg ? fg : "", sizeof (last_fg) - 1);
+	    last_fg[sizeof (last_fg) - 1] = 0;
+	    strncpy (last_bg, bg ? bg : "", sizeof (last_bg) - 1);
+	    last_bg[sizeof (last_bg) - 1] = 0;
 	    c->keyword[0]->color = this_try_alloc_color_pair (fg, bg);
 	    c->keyword[0]->keyword = g_strdup (" ");
 	    check_not_a;
@@ -762,6 +793,18 @@ static int edit_read_syntax_rules (WEdit
 	    /* do nothing for comment */
 	} else if (!strcmp (args[0], "file")) {
 	    break;
+	} else if (!strcmp (args[0], "define")) {
+	    gpointer t;
+	    char *key = *a++;
+	    char *value = *a;
+	    if (!key || !value)
+		break_a;
+	    if ((t = g_tree_lookup (defines, key))){
+		g_free (t);
+		t = g_strdup (value);
+	    } else {
+		g_tree_insert (defines, g_strdup (key), g_strdup (value));
+	    }
 	} else {		/* anything else is an error */
 	    break_a;
 	}
@@ -806,6 +849,8 @@ void edit_free_syntax_rules (WEdit * edi
 	return;
     if (!edit->rules)
 	return;
+    if (defines)
+	destroy_defines ();
     edit_get_rule (edit, -1);
     syntax_g_free (edit->syntax_type);
     edit->syntax_type = 0;





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]