]> git.wh0rd.org - fontconfig.git/blobdiff - src/fcxml.c
Change RCS tag
[fontconfig.git] / src / fcxml.c
index 4993eb44f271230de096e53d54e55aea936e3423..033de96794b46e69f8287446dfbccb30d722d7e9 100644 (file)
@@ -1,7 +1,7 @@
 /*
- * $XFree86: xc/lib/fontconfig/src/fcxml.c,v 1.6 2002/02/28 16:51:48 keithp Exp $
+ * $RCSId: xc/lib/fontconfig/src/fcxml.c,v 1.21 2002/08/22 18:53:22 keithp Exp $
  *
- * Copyright © 2002 Keith Packard, member of The XFree86 Project, Inc.
+ * Copyright © 2002 Keith Packard
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
  * documentation for any purpose is hereby granted without fee, provided that
 
 #include <stdarg.h>
 #include "fcint.h"
+
+#ifndef HAVE_XMLPARSE_H
+#define HAVE_XMLPARSE_H 0
+#endif
+
 #if HAVE_XMLPARSE_H
 #include <xmlparse.h>
 #else
 #endif
 
 FcTest *
-FcTestCreate (FcQual qual, const FcChar8 *field, FcOp compare, FcExpr *expr)
+FcTestCreate (FcMatchKind   kind, 
+             FcQual        qual,
+             const FcChar8 *field,
+             FcOp          compare,
+             FcExpr        *expr)
 {
     FcTest     *test = (FcTest *) malloc (sizeof (FcTest));
 
     if (test)
     {
+       FcMemAlloc (FC_MEM_TEST, sizeof (FcTest));
        test->next = 0;
+       test->kind = kind;
        test->qual = qual;
        test->field = (char *) FcStrCopy (field);
        test->op = compare;
@@ -53,6 +64,7 @@ FcTestDestroy (FcTest *test)
        FcTestDestroy (test->next);
     FcExprDestroy (test->expr);
     FcStrFree ((FcChar8 *) test->field);
+    FcMemFree (FC_MEM_TEST, sizeof (FcTest));
     free (test);
 }
 
@@ -63,6 +75,7 @@ FcExprCreateInteger (int i)
 
     if (e)
     {
+       FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
        e->op = FcOpInteger;
        e->u.ival = i;
     }
@@ -76,6 +89,7 @@ FcExprCreateDouble (double d)
 
     if (e)
     {
+       FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
        e->op = FcOpDouble;
        e->u.dval = d;
     }
@@ -89,6 +103,7 @@ FcExprCreateString (const FcChar8 *s)
 
     if (e)
     {
+       FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
        e->op = FcOpString;
        e->u.sval = FcStrCopy (s);
     }
@@ -102,6 +117,7 @@ FcExprCreateMatrix (const FcMatrix *m)
 
     if (e)
     {
+       FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
        e->op = FcOpMatrix;
        e->u.mval = FcMatrixCopy (m);
     }
@@ -115,6 +131,7 @@ FcExprCreateBool (FcBool b)
 
     if (e)
     {
+       FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
        e->op = FcOpBool;
        e->u.bval = b;
     }
@@ -128,6 +145,7 @@ FcExprCreateNil (void)
 
     if (e)
     {
+       FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
        e->op = FcOpNil;
     }
     return e;
@@ -140,6 +158,7 @@ FcExprCreateField (const char *field)
 
     if (e)
     {
+       FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
        e->op = FcOpField;
        e->u.field = (char *) FcStrCopy ((FcChar8 *) field);
     }
@@ -153,6 +172,7 @@ FcExprCreateConst (const FcChar8 *constant)
 
     if (e)
     {
+       FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
        e->op = FcOpConst;
        e->u.constant = FcStrCopy (constant);
     }
@@ -166,6 +186,7 @@ FcExprCreateOp (FcExpr *left, FcOp op, FcExpr *right)
 
     if (e)
     {
+       FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
        e->op = op;
        e->u.tree.left = left;
        e->u.tree.right = right;
@@ -208,12 +229,13 @@ FcExprDestroy (FcExpr *e)
     case FcOpOr:
     case FcOpAnd:
     case FcOpEqual:
-    case FcOpContains:
     case FcOpNotEqual:
     case FcOpLess:
     case FcOpLessEqual:
     case FcOpMore:
     case FcOpMoreEqual:
+    case FcOpContains:
+    case FcOpNotContains:
     case FcOpPlus:
     case FcOpMinus:
     case FcOpTimes:
@@ -229,11 +251,12 @@ FcExprDestroy (FcExpr *e)
     case FcOpInvalid:
        break;
     }
+    FcMemFree (FC_MEM_EXPR, sizeof (FcExpr));
     free (e);
 }
 
 FcEdit *
-FcEditCreate (const char *field, FcOp op, FcExpr *expr)
+FcEditCreate (const char *field, FcOp op, FcExpr *expr, FcValueBinding binding)
 {
     FcEdit *e = (FcEdit *) malloc (sizeof (FcEdit));
 
@@ -243,6 +266,7 @@ FcEditCreate (const char *field, FcOp op, FcExpr *expr)
        e->field = field;   /* already saved in grammar */
        e->op = op;
        e->expr = expr;
+       e->binding = binding;
     }
     return e;
 }
@@ -299,6 +323,8 @@ typedef enum _FcElement {
     FcElementLessEq,
     FcElementMore,
     FcElementMoreEq,
+    FcElementContains,
+    FcElementNotContains,
     FcElementPlus,
     FcElementMinus,
     FcElementTimes,
@@ -349,6 +375,8 @@ FcElementMap (const XML_Char *name)
        { "less_eq",    FcElementLessEq },
        { "more",       FcElementMore },
        { "more_eq",    FcElementMoreEq },
+       { "contains",   FcElementContains },
+       { "not_contains",FcElementNotContains },
        { "plus",       FcElementPlus },
        { "minus",      FcElementMinus },
        { "times",      FcElementTimes },
@@ -475,6 +503,7 @@ FcVStackCreate (void)
     new = malloc (sizeof (FcVStack));
     if (!new)
        return 0;
+    FcMemAlloc (FC_MEM_VSTACK, sizeof (FcVStack));
     new->tag = FcVStackNone;
     new->prev = 0;
     return new;
@@ -518,6 +547,7 @@ FcVStackDestroy (FcVStack *vstack)
            FcEditDestroy (vstack->u.edit);
            break;
        }
+       FcMemFree (FC_MEM_VSTACK, sizeof (FcVStack));
        free (vstack);
     }
 }
@@ -688,6 +718,7 @@ FcConfigSaveAttr (const XML_Char **attr)
     new = malloc ((i + 1) * sizeof (FcChar8 *) + slen);
     if (!new)
        return 0;
+    FcMemAlloc (FC_MEM_ATTR, 1);    /* size is too expensive */
     s = (FcChar8 *) (new + (i + 1));
     for (i = 0; attr[i]; i++)
     {
@@ -706,6 +737,7 @@ FcPStackPush (FcConfigParse *parse, FcElement element, const XML_Char **attr)
 
     if (!new)
        return FcFalse;
+    FcMemAlloc (FC_MEM_PSTACK, sizeof (FcPStack));
     new->prev = parse->pstack;
     new->element = element;
     if (attr)
@@ -736,7 +768,11 @@ FcPStackPop (FcConfigParse *parse)
     parse->pstack = old->prev;
     FcStrBufDestroy (&old->str);
     if (old->attr)
+    {
+       FcMemFree (FC_MEM_ATTR, 1); /* size is to expensive */
        free (old->attr);
+    }
+    FcMemFree (FC_MEM_PSTACK, sizeof (FcPStack));
     free (old);
     return FcTrue;
 }
@@ -861,6 +897,61 @@ FcParseInt (FcConfigParse *parse)
     FcStrFree (s);
 }
 
+/*
+ * idea copied from glib g_ascii_strtod with 
+ * permission of the author (Alexander Larsson) 
+ */
+
+#include <locale.h>
+
+static double 
+FcStrtod (char *s, char **end)
+{
+    struct lconv    *locale_data;
+    char           *dot;
+    double         v;
+
+    /*
+     * Have to swap the decimal point to match the current locale
+     * if that locale doesn't use 0x2e
+     */
+    if ((dot = strchr (s, 0x2e)) &&
+       (locale_data = localeconv ()) &&
+       (locale_data->decimal_point[0] != 0x2e ||
+        locale_data->decimal_point[1] != 0))
+    {
+       char    buf[128];
+       int     slen = strlen (s);
+       int     dlen = strlen (locale_data->decimal_point);
+       
+       if (slen + dlen > sizeof (buf))
+       {
+           if (end)
+               *end = s;
+           v = 0;
+       }
+       else
+       {
+           char        *buf_end;
+           /* mantissa */
+           strncpy (buf, s, dot - s);
+           /* decimal point */
+           strcpy (buf + (dot - s), locale_data->decimal_point);
+           /* rest of number */
+           strcpy (buf + (dot - s) + dlen, dot + 1);
+           buf_end = 0;
+           v = strtod (buf, &buf_end);
+           if (buf_end)
+               buf_end = s + (buf_end - buf);
+           if (end)
+               *end = buf_end;
+       }
+    }
+    else
+       v = strtod (s, end);
+    return v;
+}
+
 static void
 FcParseDouble (FcConfigParse *parse)
 {
@@ -876,7 +967,7 @@ FcParseDouble (FcConfigParse *parse)
        return;
     }
     end = 0;
-    d = strtod ((char *) s, (char **)&end);
+    d = FcStrtod ((char *) s, (char **)&end);
     if (end != s + strlen ((char *) s))
        FcConfigMessage (parse, FcSevereError, "\"%s\": not a valid double", s);
     else
@@ -930,6 +1021,7 @@ FcParseMatrix (FcConfigParse *parse)
        case m_yy: m.yy = v; break;
        default: break;
        }
+       FcVStackDestroy (vstack);
        matrix_state--;
     }
     if (matrix_state != m_done)
@@ -1097,7 +1189,8 @@ FcParseAlias (FcConfigParse *parse)
     {
        edit = FcEditCreate (FcConfigSaveField ("family"),
                             FcOpPrepend,
-                            prefer);
+                            prefer,
+                            FcValueBindingWeak);
        if (edit)
            edit->next = 0;
        else
@@ -1108,7 +1201,8 @@ FcParseAlias (FcConfigParse *parse)
        next = edit;
        edit = FcEditCreate (FcConfigSaveField ("family"),
                             FcOpAppend,
-                            accept);
+                            accept,
+                            FcValueBindingWeak);
        if (edit)
            edit->next = next;
        else
@@ -1118,8 +1212,9 @@ FcParseAlias (FcConfigParse *parse)
     {
        next = edit;
        edit = FcEditCreate (FcConfigSaveField ("family"),
-                             FcOpAppendLast,
-                             def);
+                            FcOpAppendLast,
+                            def,
+                            FcValueBindingWeak);
        if (edit)
            edit->next = next;
        else
@@ -1127,8 +1222,9 @@ FcParseAlias (FcConfigParse *parse)
     }
     if (edit)
     {
-       test = FcTestCreate (FcQualAny,
-                            FcStrCopy ((FcChar8 *) "family"),
+       test = FcTestCreate (FcMatchPattern,
+                            FcQualAny,
+                            (FcChar8 *) FC_FAMILY,
                             FcOpEqual,
                             family);
        if (test)
@@ -1181,6 +1277,7 @@ FcPopExpr (FcConfigParse *parse)
        break;
     case FcVStackExpr:
        expr = vstack->u.expr;
+       vstack->tag = FcVStackNone;
        break;
     case FcVStackEdit:
        break;
@@ -1240,7 +1337,7 @@ FcParseInclude (FcConfigParse *parse)
        ignore_missing = FcTrue;
     if (!FcConfigParseAndLoad (parse->config, s, !ignore_missing))
        parse->error = FcTrue;
-    free (s);
+    FcStrFree (s);
 }
 
 typedef struct _FcOpMap {
@@ -1265,7 +1362,9 @@ static const FcOpMap fcCompareOps[] = {
     { "less",          FcOpLess            },
     { "less_eq",       FcOpLessEqual       },
     { "more",          FcOpMore            },
-    { "more_eq",       FcOpMoreEqual       }
+    { "more_eq",       FcOpMoreEqual       },
+    { "contains",      FcOpContains        },
+    { "not_contains",  FcOpNotContains     }
 };
 
 #define NUM_COMPARE_OPS (sizeof fcCompareOps / sizeof fcCompareOps[0])
@@ -1280,6 +1379,8 @@ FcConfigLexCompare (const FcChar8 *compare)
 static void
 FcParseTest (FcConfigParse *parse)
 {
+    const FcChar8   *kind_string;
+    FcMatchKind            kind;
     const FcChar8   *qual_string;
     FcQual         qual;
     const FcChar8   *name;
@@ -1288,6 +1389,23 @@ FcParseTest (FcConfigParse *parse)
     FcExpr         *expr;
     FcTest         *test;
 
+    kind_string = FcConfigGetAttribute (parse, "target");
+    if (!kind_string)
+       kind = FcMatchDefault;
+    else
+    {
+       if (!strcmp ((char *) kind_string, "pattern"))
+           kind = FcMatchPattern;
+       else if (!strcmp ((char *) kind_string, "font"))
+           kind = FcMatchFont;
+       else if (!strcmp ((char *) kind_string, "default"))
+           kind = FcMatchDefault;
+       else
+       {
+           FcConfigMessage (parse, FcSevereWarning, "invalid test target \"%s\"", kind_string);
+           return;
+       }
+    }
     qual_string = FcConfigGetAttribute (parse, "qual");
     if (!qual_string)
        qual = FcQualAny;
@@ -1297,17 +1415,21 @@ FcParseTest (FcConfigParse *parse)
            qual = FcQualAny;
        else if (!strcmp ((char *) qual_string, "all"))
            qual = FcQualAll;
+       else if (!strcmp ((char *) qual_string, "first"))
+           qual = FcQualFirst;
+       else if (!strcmp ((char *) qual_string, "not_first"))
+           qual = FcQualNotFirst;
        else
        {
            FcConfigMessage (parse, FcSevereWarning, "invalid test qual \"%s\"", qual_string);
-           qual = FcQualAny;
+           return;
        }
     }
     name = FcConfigGetAttribute (parse, "name");
     if (!name)
     {
        FcConfigMessage (parse, FcSevereWarning, "missing test name");
-       name = (FcChar8 *) FC_FAMILY;
+       return;
     }
     compare_string = FcConfigGetAttribute (parse, "compare");
     if (!compare_string)
@@ -1318,7 +1440,7 @@ FcParseTest (FcConfigParse *parse)
        if (compare == FcOpInvalid)
        {
            FcConfigMessage (parse, FcSevereWarning, "invalid test compare \"%s\"", compare_string);
-           compare = FcOpEqual;
+           return;
        }
     }
     expr = FcPopExprs (parse, FcOpComma);
@@ -1327,7 +1449,7 @@ FcParseTest (FcConfigParse *parse)
        FcConfigMessage (parse, FcSevereWarning, "missing test expression");
        return;
     }
-    test = FcTestCreate (qual, name, compare, expr);
+    test = FcTestCreate (kind, qual, name, compare, expr);
     if (!test)
     {
        FcConfigMessage (parse, FcSevereError, "out of memory");
@@ -1358,7 +1480,9 @@ FcParseEdit (FcConfigParse *parse)
 {
     const FcChar8   *name;
     const FcChar8   *mode_string;
+    const FcChar8   *binding_string;
     FcOp           mode;
+    FcValueBinding  binding;
     FcExpr         *expr;
     FcEdit         *edit;
 
@@ -1366,22 +1490,39 @@ FcParseEdit (FcConfigParse *parse)
     if (!name)
     {
        FcConfigMessage (parse, FcSevereWarning, "missing edit name");
-       name = (FcChar8 *) FC_FAMILY;
+       return;
     }
     mode_string = FcConfigGetAttribute (parse, "mode");
     if (!mode_string)
-       mode = FcOpEqual;
+       mode = FcOpAssign;
     else
     {
        mode = FcConfigLexMode (mode_string);
        if (mode == FcOpInvalid)
        {
            FcConfigMessage (parse, FcSevereWarning, "invalid edit mode \"%s\"", mode_string);
-           mode = FcOpAssign;
+           return;
+       }
+    }
+    binding_string = FcConfigGetAttribute (parse, "binding");
+    if (!binding_string)
+       binding = FcValueBindingWeak;
+    else
+    {
+       if (!strcmp ((char *) binding_string, "weak"))
+           binding = FcValueBindingWeak;
+       else if (!strcmp ((char *) binding_string, "strong"))
+           binding = FcValueBindingStrong;
+       else if (!strcmp ((char *) binding_string, "same"))
+           binding = FcValueBindingSame;
+       else
+       {
+           FcConfigMessage (parse, FcSevereWarning, "invalid edit binding \"%s\"", binding_string);
+           return;
        }
     }
     expr = FcPopExprs (parse, FcOpComma);
-    edit = FcEditCreate ((char *) FcStrCopy (name), mode, expr);
+    edit = FcEditCreate ((char *) FcStrCopy (name), mode, expr, binding);
     if (!edit)
     {
        FcConfigMessage (parse, FcSevereError, "out of memory");
@@ -1413,7 +1554,7 @@ FcParseMatch (FcConfigParse *parse)
        else
        {
            FcConfigMessage (parse, FcSevereWarning, "invalid match target \"%s\"", kind_name);
-           kind = FcMatchPattern;
+           return;
        }
     }
     while ((vstack = FcVStackPop (parse)))
@@ -1459,9 +1600,12 @@ FcEndElement(void *userData, const XML_Char *name)
            FcConfigMessage (parse, FcSevereError, "out of memory");
            break;
        }
-       if (!FcConfigAddDir (parse->config, data))
-           FcConfigMessage (parse, FcSevereError, "out of memory");
-       free (data);
+       if (!FcStrUsesHome (data) || FcConfigHome ())
+       {
+           if (!FcConfigAddDir (parse->config, data))
+               FcConfigMessage (parse, FcSevereError, "out of memory");
+       }
+       FcStrFree (data);
        break;
     case FcElementCache:
        data = FcStrBufDone (&parse->pstack->str);
@@ -1470,9 +1614,12 @@ FcEndElement(void *userData, const XML_Char *name)
            FcConfigMessage (parse, FcSevereError, "out of memory");
            break;
        }
-       if (!FcConfigSetCache (parse->config, data))
-           FcConfigMessage (parse, FcSevereError, "out of memory");
-       free (data);
+       if (!FcStrUsesHome (data) || FcConfigHome ())
+       {
+           if (!FcConfigSetCache (parse->config, data))
+               FcConfigMessage (parse, FcSevereError, "out of memory");
+       }
+       FcStrFree (data);
        break;
     case FcElementInclude:
        FcParseInclude (parse);
@@ -1562,6 +1709,12 @@ FcEndElement(void *userData, const XML_Char *name)
     case FcElementMoreEq:
        FcParseExpr (parse, FcOpMoreEqual);
        break;
+    case FcElementContains:
+       FcParseExpr (parse, FcOpContains);
+       break;
+    case FcElementNotContains:
+       FcParseExpr (parse, FcOpNotContains);
+       break;
     case FcElementPlus:
        FcParseExpr (parse, FcOpPlus);
        break;
@@ -1632,8 +1785,15 @@ FcConfigParseAndLoad (FcConfig       *config,
     filename = FcConfigFilename (name);
     if (!filename)
        goto bail0;
+    
+    if (!FcStrSetAdd (config->configFiles, filename))
+    {
+       FcStrFree (filename);
+       goto bail0;
+    }
+
     f = fopen ((char *) filename, "r");
-    free (filename);
+    FcStrFree (filename);
     if (!f)
        goto bail0;