]> git.wh0rd.org - fontconfig.git/blobdiff - src/fcxml.c
Avoid crashes if config files contain junk.
[fontconfig.git] / src / fcxml.c
index 3cdbaa4ca2c0566f8a0da9e55407e2b5061b9e91..3b08fea593eb46db055cdcaf03014a8b549d5604 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * $RCSId: xc/lib/fontconfig/src/fcxml.c,v 1.21 2002/08/22 18:53:22 keithp Exp $
  *
- * Copyright © 2002 Keith Packard
+ * 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
  * PERFORMANCE OF THIS SOFTWARE.
  */
 
-#include <stdarg.h>
 #include "fcint.h"
+#include <fcntl.h>
+#include <stdarg.h>
+#include <dirent.h>
+
+#ifdef ENABLE_LIBXML2
+
+#include <libxml/parser.h>
+
+#define XML_Char                       xmlChar
+#define XML_Parser                     xmlParserCtxtPtr
+#define XML_ParserFree                 xmlFreeParserCtxt
+#define XML_GetCurrentLineNumber       xmlSAX2GetLineNumber
+#define XML_GetErrorCode               xmlCtxtGetLastError
+#define XML_ErrorString(Error)         (Error)->message
+
+#else /* ENABLE_LIBXML2 */
 
 #ifndef HAVE_XMLPARSE_H
 #define HAVE_XMLPARSE_H 0
 #include <expat.h>
 #endif
 
+#endif /* ENABLE_LIBXML2 */
+
 #ifdef _WIN32
 #define STRICT
 #include <windows.h>
 #undef STRICT
 #endif
 
-FcTest *
-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;
-       test->expr = expr;
-    }
-    return test;
-}
 
 void
 FcTestDestroy (FcTest *test)
@@ -69,7 +65,6 @@ FcTestDestroy (FcTest *test)
     if (test->next)
        FcTestDestroy (test->next);
     FcExprDestroy (test->expr);
-    FcStrFree ((FcChar8 *) test->field);
     FcMemFree (FC_MEM_TEST, sizeof (FcTest));
     free (test);
 }
@@ -166,7 +161,7 @@ FcExprCreateField (const char *field)
     {
        FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
        e->op = FcOpField;
-       e->u.field = (char *) FcStrCopy ((FcChar8 *) field);
+       e->u.object = FcObjectFromName (field);
     }
     return e;
 }
@@ -222,7 +217,6 @@ FcExprDestroy (FcExpr *e)
     case FcOpBool:
        break;
     case FcOpField:
-       FcStrFree ((FcChar8 *) e->u.field);
        break;
     case FcOpConst:
        FcStrFree (e->u.constant);
@@ -243,6 +237,7 @@ FcExprDestroy (FcExpr *e)
     case FcOpMore:
     case FcOpMoreEqual:
     case FcOpContains:
+    case FcOpListing:
     case FcOpNotContains:
     case FcOpPlus:
     case FcOpMinus:
@@ -267,30 +262,14 @@ FcExprDestroy (FcExpr *e)
     free (e);
 }
 
-FcEdit *
-FcEditCreate (const char *field, FcOp op, FcExpr *expr, FcValueBinding binding)
-{
-    FcEdit *e = (FcEdit *) malloc (sizeof (FcEdit));
-
-    if (e)
-    {
-       e->next = 0;
-       e->field = field;   /* already saved in grammar */
-       e->op = op;
-       e->expr = expr;
-       e->binding = binding;
-    }
-    return e;
-}
-
 void
 FcEditDestroy (FcEdit *e)
 {
     if (e->next)
        FcEditDestroy (e->next);
-    FcStrFree ((FcChar8 *) e->field);
     if (e->expr)
        FcExprDestroy (e->expr);
+    free (e);
 }
 
 char *
@@ -303,6 +282,7 @@ typedef enum _FcElement {
     FcElementNone,
     FcElementFontconfig,
     FcElementDir,
+    FcElementCacheDir,
     FcElementCache,
     FcElementInclude,
     FcElementConfig,
@@ -321,6 +301,8 @@ typedef enum _FcElement {
     FcElementAcceptfont,
     FcElementRejectfont,
     FcElementGlob,
+    FcElementPattern,
+    FcElementPatelt,
 
     FcElementTest,
     FcElementEdit,
@@ -355,70 +337,73 @@ typedef enum _FcElement {
     FcElementUnknown
 } FcElement;
 
+static const struct {
+    const char  name[16];
+    FcElement   element;
+} fcElementMap[] = {
+    { "fontconfig",    FcElementFontconfig },
+    { "dir",           FcElementDir },
+    { "cachedir",      FcElementCacheDir },
+    { "cache",         FcElementCache },
+    { "include",       FcElementInclude },
+    { "config",                FcElementConfig },
+    { "match",         FcElementMatch },
+    { "alias",         FcElementAlias },
+    
+    { "blank",         FcElementBlank },
+    { "rescan",                FcElementRescan },
+
+    { "prefer",                FcElementPrefer },
+    { "accept",                FcElementAccept },
+    { "default",       FcElementDefault },
+    { "family",                FcElementFamily },
+
+    { "selectfont",    FcElementSelectfont },
+    { "acceptfont",    FcElementAcceptfont },
+    { "rejectfont",    FcElementRejectfont },
+    { "glob",          FcElementGlob },
+    { "pattern",       FcElementPattern },
+    { "patelt",                FcElementPatelt },
+
+    { "test",          FcElementTest },
+    { "edit",          FcElementEdit },
+    { "int",           FcElementInt },
+    { "double",                FcElementDouble },
+    { "string",                FcElementString },
+    { "matrix",                FcElementMatrix },
+    { "bool",          FcElementBool },
+    { "charset",       FcElementCharset },
+    { "name",          FcElementName },
+    { "const",         FcElementConst },
+    { "or",            FcElementOr },
+    { "and",           FcElementAnd },
+    { "eq",            FcElementEq },
+    { "not_eq",                FcElementNotEq },
+    { "less",          FcElementLess },
+    { "less_eq",       FcElementLessEq },
+    { "more",          FcElementMore },
+    { "more_eq",       FcElementMoreEq },
+    { "contains",      FcElementContains },
+    { "not_contains",  FcElementNotContains },
+    { "plus",          FcElementPlus },
+    { "minus",         FcElementMinus },
+    { "times",         FcElementTimes },
+    { "divide",                FcElementDivide },
+    { "not",           FcElementNot },
+    { "if",            FcElementIf },
+    { "floor",         FcElementFloor },
+    { "ceil",          FcElementCeil },
+    { "round",         FcElementRound },
+    { "trunc",         FcElementTrunc },
+};
+#define NUM_ELEMENT_MAPS (int) (sizeof fcElementMap / sizeof fcElementMap[0])
+
 static FcElement
 FcElementMap (const XML_Char *name)
 {
-    static struct {
-       char        *name;
-       FcElement   element;
-    } fcElementMap[] = {
-       { "fontconfig", FcElementFontconfig },
-       { "dir",        FcElementDir },
-       { "cache",      FcElementCache },
-       { "include",    FcElementInclude },
-       { "config",     FcElementConfig },
-       { "match",      FcElementMatch },
-       { "alias",      FcElementAlias },
-       
-       { "blank",      FcElementBlank },
-       { "rescan",     FcElementRescan },
-
-       { "prefer",     FcElementPrefer },
-       { "accept",     FcElementAccept },
-       { "default",    FcElementDefault },
-       { "family",     FcElementFamily },
-
-       { "selectfont", FcElementSelectfont },
-       { "acceptfont", FcElementAcceptfont },
-       { "rejectfont", FcElementRejectfont },
-       { "glob",       FcElementGlob },
-
-       { "test",       FcElementTest },
-       { "edit",       FcElementEdit },
-       { "int",        FcElementInt },
-       { "double",     FcElementDouble },
-       { "string",     FcElementString },
-       { "matrix",     FcElementMatrix },
-       { "bool",       FcElementBool },
-       { "charset",    FcElementCharset },
-       { "name",       FcElementName },
-       { "const",      FcElementConst },
-       { "or",         FcElementOr },
-       { "and",        FcElementAnd },
-       { "eq",         FcElementEq },
-       { "not_eq",     FcElementNotEq },
-       { "less",       FcElementLess },
-       { "less_eq",    FcElementLessEq },
-       { "more",       FcElementMore },
-       { "more_eq",    FcElementMoreEq },
-       { "contains",   FcElementContains },
-       { "not_contains",FcElementNotContains },
-       { "plus",       FcElementPlus },
-       { "minus",      FcElementMinus },
-       { "times",      FcElementTimes },
-       { "divide",     FcElementDivide },
-       { "not",        FcElementNot },
-       { "if",         FcElementIf },
-       { "floor",      FcElementFloor },
-       { "ceil",       FcElementCeil },
-       { "round",      FcElementRound },
-       { "trunc",      FcElementTrunc },
-       
-       { 0,            0 }
-    };
 
     int            i;
-    for (i = 0; fcElementMap[i].name; i++)
+    for (i = 0; i < NUM_ELEMENT_MAPS; i++)
        if (!strcmp ((char *) name, fcElementMap[i].name))
            return fcElementMap[i].element;
     return FcElementUnknown;
@@ -439,6 +424,7 @@ typedef enum _FcVStackTag {
     FcVStackField,
     FcVStackConstant,
     FcVStackGlob,
+    FcVStackPattern,
     
     FcVStackPrefer,
     FcVStackAccept,
@@ -471,6 +457,8 @@ typedef struct _FcVStack {
        FcOp            op;
        FcExpr          *expr;
        FcEdit          *edit;
+
+       FcPattern       *pattern;
     } u;
 } FcVStack;
 
@@ -488,9 +476,9 @@ typedef enum _FcConfigSeverity {
 } FcConfigSeverity;
 
 static void
-FcConfigMessage (FcConfigParse *parse, FcConfigSeverity severe, char *fmt, ...)
+FcConfigMessage (FcConfigParse *parse, FcConfigSeverity severe, const char *fmt, ...)
 {
-    char       *s = "unknown";
+    const char *s = "unknown";
     va_list    args;
 
     va_start (args, fmt);
@@ -504,10 +492,10 @@ FcConfigMessage (FcConfigParse *parse, FcConfigSeverity severe, char *fmt, ...)
     {
        if (parse->name)
            fprintf (stderr, "Fontconfig %s: \"%s\", line %d: ", s,
-                    parse->name, XML_GetCurrentLineNumber (parse->parser));
+                    parse->name, (int)XML_GetCurrentLineNumber (parse->parser));
        else
            fprintf (stderr, "Fontconfig %s: line %d: ", s,
-                    XML_GetCurrentLineNumber (parse->parser));
+                    (int)XML_GetCurrentLineNumber (parse->parser));
        if (severe >= FcSevereError)
            parse->error = FcTrue;
     }
@@ -518,6 +506,197 @@ FcConfigMessage (FcConfigParse *parse, FcConfigSeverity severe, char *fmt, ...)
     va_end (args);
 }
 
+
+static const char *
+FcTypeName (FcType type)
+{
+    switch (type) {
+    case FcTypeVoid:
+       return "void";
+    case FcTypeInteger:
+    case FcTypeDouble:
+       return "number";
+    case FcTypeString:
+       return "string";
+    case FcTypeBool:
+       return "bool";
+    case FcTypeMatrix:
+       return "matrix";
+    case FcTypeCharSet:
+       return "charset";
+    case FcTypeFTFace:
+       return "FT_Face";
+    case FcTypeLangSet:
+       return "langset";
+    default:
+       return "unknown";
+    }
+}
+
+static void
+FcTypecheckValue (FcConfigParse *parse, FcType value, FcType type)
+{
+    if (value == FcTypeInteger)
+       value = FcTypeDouble;
+    if (type == FcTypeInteger)
+       type = FcTypeDouble;
+    if (value != type)
+    {
+       if ((value == FcTypeLangSet && type == FcTypeString) ||
+           (value == FcTypeString && type == FcTypeLangSet))
+           return;
+       if (type == (FcType) -1)
+           return;
+       FcConfigMessage (parse, FcSevereWarning, "saw %s, expected %s",
+                        FcTypeName (value), FcTypeName (type));
+    }
+}
+
+static void
+FcTypecheckExpr (FcConfigParse *parse, FcExpr *expr, FcType type)
+{
+    const FcObjectType *o;
+    const FcConstant   *c;
+    
+    /* If parsing the expression failed, some nodes may be NULL */
+    if (!expr)
+       return;
+
+    switch (expr->op) {
+    case FcOpInteger:
+    case FcOpDouble:
+       FcTypecheckValue (parse, FcTypeDouble, type);
+       break;
+    case FcOpString:
+       FcTypecheckValue (parse, FcTypeString, type);
+       break;
+    case FcOpMatrix:
+       FcTypecheckValue (parse, FcTypeMatrix, type);
+       break;
+    case FcOpBool:
+       FcTypecheckValue (parse, FcTypeBool, type);
+       break;
+    case FcOpCharSet:
+       FcTypecheckValue (parse, FcTypeCharSet, type);
+       break;
+    case FcOpNil:
+       break;
+    case FcOpField:
+       o = FcNameGetObjectType (FcObjectName (expr->u.object));
+       if (o)
+           FcTypecheckValue (parse, o->type, type);
+       break;
+    case FcOpConst:
+       c = FcNameGetConstant (expr->u.constant);
+       if (c)
+       {
+           o = FcNameGetObjectType (c->object);
+           if (o)
+               FcTypecheckValue (parse, o->type, type);
+       }
+        else 
+            FcConfigMessage (parse, FcSevereWarning, 
+                             "invalid constant used : %s",
+                             expr->u.constant);
+       break;
+    case FcOpQuest:
+       FcTypecheckExpr (parse, expr->u.tree.left, FcTypeBool);
+       FcTypecheckExpr (parse, expr->u.tree.right->u.tree.left, type);
+       FcTypecheckExpr (parse, expr->u.tree.right->u.tree.right, type);
+       break;
+    case FcOpAssign:
+    case FcOpAssignReplace:
+       break;
+    case FcOpEqual:
+    case FcOpNotEqual:
+    case FcOpLess:
+    case FcOpLessEqual:
+    case FcOpMore:
+    case FcOpMoreEqual:
+    case FcOpContains:
+    case FcOpNotContains:
+    case FcOpListing:
+       FcTypecheckValue (parse, FcTypeBool, type);
+       break;
+    case FcOpComma:
+    case FcOpOr:
+    case FcOpAnd:
+    case FcOpPlus:
+    case FcOpMinus:
+    case FcOpTimes:
+    case FcOpDivide:
+       FcTypecheckExpr (parse, expr->u.tree.left, type);
+       FcTypecheckExpr (parse, expr->u.tree.right, type);
+       break;
+    case FcOpNot:
+       FcTypecheckValue (parse, FcTypeBool, type);
+       FcTypecheckExpr (parse, expr->u.tree.left, FcTypeBool);
+       break;
+    case FcOpFloor:
+    case FcOpCeil:
+    case FcOpRound:
+    case FcOpTrunc:
+       FcTypecheckValue (parse, FcTypeDouble, type);
+       FcTypecheckExpr (parse, expr->u.tree.left, FcTypeDouble);
+       break;
+    default:
+       break;
+    }
+}
+
+static FcTest *
+FcTestCreate (FcConfigParse *parse,
+             FcMatchKind   kind, 
+             FcQual        qual,
+             const FcChar8 *field,
+             FcOp          compare,
+             FcExpr        *expr)
+{
+    FcTest     *test = (FcTest *) malloc (sizeof (FcTest));
+
+    if (test)
+    {
+       const FcObjectType      *o;
+       
+       FcMemAlloc (FC_MEM_TEST, sizeof (FcTest));
+       test->next = 0;
+       test->kind = kind;
+       test->qual = qual;
+       test->object = FcObjectFromName ((const char *) field);
+       test->op = compare;
+       test->expr = expr;
+       o = FcNameGetObjectType (FcObjectName (test->object));
+       if (o)
+           FcTypecheckExpr (parse, expr, o->type);
+    }
+    return test;
+}
+
+static FcEdit *
+FcEditCreate (FcConfigParse    *parse,
+             FcObject          object,
+             FcOp              op,
+             FcExpr            *expr,
+             FcValueBinding    binding)
+{
+    FcEdit *e = (FcEdit *) malloc (sizeof (FcEdit));
+
+    if (e)
+    {
+       const FcObjectType      *o;
+
+       e->next = 0;
+       e->object = object;
+       e->op = op;
+       e->expr = expr;
+       e->binding = binding;
+       o = FcNameGetObjectType (FcObjectName (e->object));
+       if (o)
+           FcTypecheckExpr (parse, expr, o->type);
+    }
+    return e;
+}
+
 static void
 FcVStackPush (FcConfigParse *parse, FcVStack *vstack)
 {
@@ -558,6 +737,9 @@ FcVStackDestroy (FcVStack *vstack)
        case FcVStackGlob:
            FcStrFree (vstack->u.string);
            break;
+       case FcVStackPattern:
+           FcPatternDestroy (vstack->u.pattern);
+           break;
        case FcVStackInteger:
        case FcVStackDouble:
            break;
@@ -686,6 +868,18 @@ FcVStackPushEdit (FcConfigParse *parse, FcEdit *edit)
     return FcTrue;
 }
 
+static FcBool
+FcVStackPushPattern (FcConfigParse *parse, FcPattern *pattern)
+{
+    FcVStack    *vstack = FcVStackCreate ();
+    if (!vstack)
+       return FcFalse;
+    vstack->u.pattern = pattern;
+    vstack->tag = FcVStackPattern;
+    FcVStackPush (parse, vstack);
+    return FcTrue;
+}
+
 static FcVStack *
 FcVStackFetch (FcConfigParse *parse, int off)
 {
@@ -735,7 +929,6 @@ FcVStackElements (FcConfigParse *parse)
 static FcChar8 **
 FcConfigSaveAttr (const XML_Char **attr)
 {
-    int                n;
     int                slen;
     int                i;
     FcChar8    **new;
@@ -745,8 +938,7 @@ FcConfigSaveAttr (const XML_Char **attr)
        return 0;
     slen = 0;
     for (i = 0; attr[i]; i++)
-       slen += strlen (attr[i]) + 1;
-    n = i;
+       slen += strlen ((char *) attr[i]) + 1;
     new = malloc ((i + 1) * sizeof (FcChar8 *) + slen);
     if (!new)
        return 0;
@@ -829,13 +1021,16 @@ FcConfigCleanup (FcConfigParse   *parse)
 }
 
 static const FcChar8 *
-FcConfigGetAttribute (FcConfigParse *parse, char *attr)
+FcConfigGetAttribute (FcConfigParse *parse, const char *attr)
 {
     FcChar8 **attrs;
     if (!parse->pstack)
        return 0;
 
     attrs = parse->pstack->attr;
+    if (!attrs)
+        return 0;
+
     while (*attrs)
     {
        if (!strcmp ((char *) *attrs, attr))
@@ -956,7 +1151,7 @@ FcStrtod (char *s, char **end)
        int     slen = strlen (s);
        int     dlen = strlen (locale_data->decimal_point);
        
-       if (slen + dlen > sizeof (buf))
+       if (slen + dlen > (int) sizeof (buf))
        {
            if (end)
                *end = s;
@@ -973,8 +1168,11 @@ FcStrtod (char *s, char **end)
            strcpy (buf + (dot - s) + dlen, dot + 1);
            buf_end = 0;
            v = strtod (buf, &buf_end);
-           if (buf_end)
+           if (buf_end) {
                buf_end = s + (buf_end - buf);
+               if (buf_end > dot)
+                   buf_end -= dlen - 1;
+           }
            if (end)
                *end = buf_end;
        }
@@ -1063,15 +1261,14 @@ FcParseMatrix (FcConfigParse *parse)
 }
 
 static FcBool
-FcConfigLexBool (const FcChar8 *bool)
+FcConfigLexBool (FcConfigParse *parse, const FcChar8 *bool)
 {
-    if (*bool == 't' || *bool == 'T')
-       return FcTrue;
-    if (*bool == 'y' || *bool == 'Y')
-       return FcTrue;
-    if (*bool == '1')
-       return FcTrue;
-    return FcFalse;
+    FcBool  result = FcFalse;
+
+    if (!FcNameBool (bool, &result))
+       FcConfigMessage (parse, FcSevereWarning, "\"%s\" is not known boolean",
+                        bool);
+    return result;
 }
 
 static void
@@ -1087,7 +1284,7 @@ FcParseBool (FcConfigParse *parse)
        FcConfigMessage (parse, FcSevereError, "out of memory");
        return;
     }
-    FcVStackPushBool (parse, FcConfigLexBool (s));
+    FcVStackPushBool (parse, FcConfigLexBool (parse, s));
     FcStrFree (s);
 }
 
@@ -1128,8 +1325,7 @@ FcParseFamilies (FcConfigParse *parse, FcVStackTag tag)
        if (!FcVStackPushExpr (parse, tag, expr))
        {
            FcConfigMessage (parse, FcSevereError, "out of memory");
-           if (expr)
-               FcExprDestroy (expr);
+            FcExprDestroy (expr);
        }
     }
 }
@@ -1219,7 +1415,8 @@ FcParseAlias (FcConfigParse *parse)
     }
     if (prefer)
     {
-       edit = FcEditCreate (FcConfigSaveField ("family"),
+       edit = FcEditCreate (parse, 
+                            FC_FAMILY_OBJECT,
                             FcOpPrepend,
                             prefer,
                             FcValueBindingWeak);
@@ -1231,7 +1428,8 @@ FcParseAlias (FcConfigParse *parse)
     if (accept)
     {
        next = edit;
-       edit = FcEditCreate (FcConfigSaveField ("family"),
+       edit = FcEditCreate (parse,
+                            FC_FAMILY_OBJECT,
                             FcOpAppend,
                             accept,
                             FcValueBindingWeak);
@@ -1243,7 +1441,8 @@ FcParseAlias (FcConfigParse *parse)
     if (def)
     {
        next = edit;
-       edit = FcEditCreate (FcConfigSaveField ("family"),
+       edit = FcEditCreate (parse,
+                            FC_FAMILY_OBJECT,
                             FcOpAppendLast,
                             def,
                             FcValueBindingWeak);
@@ -1254,7 +1453,7 @@ FcParseAlias (FcConfigParse *parse)
     }
     if (edit)
     {
-       test = FcTestCreate (FcMatchPattern,
+       test = FcTestCreate (parse, FcMatchPattern,
                             FcQualAny,
                             (FcChar8 *) FC_FAMILY,
                             FcOpEqual,
@@ -1287,6 +1486,9 @@ FcPopExpr (FcConfigParse *parse)
     case FcVStackConstant:
        expr = FcExprCreateConst (vstack->u.string);
        break;
+    case FcVStackGlob:
+       /* XXX: What's the correct action here? (CDW) */
+       break;
     case FcVStackPrefer:
     case FcVStackAccept:
     case FcVStackDefault:
@@ -1313,6 +1515,8 @@ FcPopExpr (FcConfigParse *parse)
        break;
     case FcVStackEdit:
        break;
+    default:
+       break;
     }
     FcVStackDestroy (vstack);
     return expr;
@@ -1342,7 +1546,7 @@ FcPopBinary (FcConfigParse *parse, FcOp op)
                FcConfigMessage (parse, FcSevereError, "out of memory");
                FcExprDestroy (left);
                FcExprDestroy (expr);
-               break;
+               return 0;
            }
            expr = new;
        }
@@ -1404,7 +1608,7 @@ FcParseInclude (FcConfigParse *parse)
        return;
     }
     i = FcConfigGetAttribute (parse, "ignore_missing");
-    if (i && FcConfigLexBool ((FcChar8 *) i) == FcTrue)
+    if (i && FcConfigLexBool (parse, (FcChar8 *) i) == FcTrue)
        ignore_missing = FcTrue;
     if (!FcConfigParseAndLoad (parse->config, s, !ignore_missing))
        parse->error = FcTrue;
@@ -1412,7 +1616,7 @@ FcParseInclude (FcConfigParse *parse)
 }
 
 typedef struct _FcOpMap {
-    char    *name;
+    char    name[16];
     FcOp    op;
 } FcOpMap;
 
@@ -1438,7 +1642,7 @@ static const FcOpMap fcCompareOps[] = {
     { "not_contains",  FcOpNotContains     }
 };
 
-#define NUM_COMPARE_OPS (sizeof fcCompareOps / sizeof fcCompareOps[0])
+#define NUM_COMPARE_OPS        (int) (sizeof fcCompareOps / sizeof fcCompareOps[0])
 
 static FcOp
 FcConfigLexCompare (const FcChar8 *compare)
@@ -1469,6 +1673,8 @@ FcParseTest (FcConfigParse *parse)
            kind = FcMatchPattern;
        else if (!strcmp ((char *) kind_string, "font"))
            kind = FcMatchFont;
+       else if (!strcmp ((char *) kind_string, "scan"))
+           kind = FcMatchScan;
        else if (!strcmp ((char *) kind_string, "default"))
            kind = FcMatchDefault;
        else
@@ -1520,7 +1726,7 @@ FcParseTest (FcConfigParse *parse)
        FcConfigMessage (parse, FcSevereWarning, "missing test expression");
        return;
     }
-    test = FcTestCreate (kind, qual, name, compare, expr);
+    test = FcTestCreate (parse, kind, qual, name, compare, expr);
     if (!test)
     {
        FcConfigMessage (parse, FcSevereError, "out of memory");
@@ -1538,7 +1744,7 @@ static const FcOpMap fcModeOps[] = {
     { "append_last",   FcOpAppendLast      },
 };
 
-#define NUM_MODE_OPS (sizeof fcModeOps / sizeof fcModeOps[0])
+#define NUM_MODE_OPS (int) (sizeof fcModeOps / sizeof fcModeOps[0])
 
 static FcOp
 FcConfigLexMode (const FcChar8 *mode)
@@ -1593,7 +1799,8 @@ FcParseEdit (FcConfigParse *parse)
        }
     }
     expr = FcPopBinary (parse, FcOpComma);
-    edit = FcEditCreate ((char *) FcStrCopy (name), mode, expr, binding);
+    edit = FcEditCreate (parse, FcObjectFromName ((char *) name),
+                        mode, expr, binding);
     if (!edit)
     {
        FcConfigMessage (parse, FcSevereError, "out of memory");
@@ -1622,6 +1829,8 @@ FcParseMatch (FcConfigParse *parse)
            kind = FcMatchPattern;
        else if (!strcmp ((char *) kind_name, "font"))
            kind = FcMatchFont;
+       else if (!strcmp ((char *) kind_name, "scan"))
+           kind = FcMatchScan;
        else
        {
            FcConfigMessage (parse, FcSevereWarning, "invalid match target \"%s\"", kind_name);
@@ -1640,6 +1849,12 @@ FcParseMatch (FcConfigParse *parse)
            vstack->u.edit->next = edit;
            edit = vstack->u.edit;
            vstack->tag = FcVStackNone;
+           if (kind == FcMatchScan && edit->object > FC_MAX_BASE_OBJECT)
+           {
+               FcConfigMessage (parse, FcSevereError, 
+                                "<match target=\"scan\"> cannot edit user-defined object \"%s\"",
+                                FcObjectName(edit->object));
+           }
            break;
        default:
            FcConfigMessage (parse, FcSevereWarning, "invalid match element");
@@ -1667,6 +1882,16 @@ FcParseAcceptRejectFont (FcConfigParse *parse, FcElement element)
                FcConfigMessage (parse, FcSevereError, "out of memory");
            }
            break;
+       case FcVStackPattern:
+           if (!FcConfigPatternsAdd (parse->config,
+                                     vstack->u.pattern,
+                                     element == FcElementAcceptfont))
+           {
+               FcConfigMessage (parse, FcSevereError, "out of memory");
+           }
+           else
+               vstack->tag = FcVStackNone;
+           break;
        default:
            FcConfigMessage (parse, FcSevereWarning, "bad font selector");
            break;
@@ -1675,6 +1900,124 @@ FcParseAcceptRejectFont (FcConfigParse *parse, FcElement element)
     }
 }
 
+
+static FcValue
+FcPopValue (FcConfigParse *parse)
+{
+    FcVStack   *vstack = FcVStackPop (parse);
+    FcValue    value;
+    
+    value.type = FcTypeVoid;
+    
+    if (!vstack)
+       return value;
+    
+    switch (vstack->tag) {
+    case FcVStackString:
+       value.u.s = FcStrCopy (vstack->u.string);
+       if (value.u.s)
+           value.type = FcTypeString;
+       break;
+    case FcVStackConstant:
+       if (FcNameConstant (vstack->u.string, &value.u.i))
+           value.type = FcTypeInteger;
+       break;
+    case FcVStackInteger:
+       value.u.i = vstack->u.integer;
+       value.type = FcTypeInteger;
+       break;
+    case FcVStackDouble:
+       value.u.d = vstack->u._double;
+       value.type = FcTypeInteger;
+       break;
+    case FcVStackMatrix:
+       value.u.m = FcMatrixCopy (vstack->u.matrix);
+       if (value.u.m)
+           value.type = FcTypeMatrix;
+       break;
+    case FcVStackBool:
+       value.u.b = vstack->u.bool;
+       value.type = FcTypeBool;
+       break;
+    default:
+       FcConfigMessage (parse, FcSevereWarning, "unknown pattern element %d", 
+                        vstack->tag);
+       break;
+    }
+    FcVStackDestroy (vstack);
+    
+    return value;
+}
+
+static void
+FcParsePatelt (FcConfigParse *parse)
+{
+    FcValue    value;
+    FcPattern  *pattern = FcPatternCreate ();
+    const char *name;
+
+    if (!pattern)
+    {
+       FcConfigMessage (parse, FcSevereError, "out of memory");
+       return;
+    }
+
+    name = (char *) FcConfigGetAttribute (parse, "name");
+    if (!name)
+    {
+       FcConfigMessage (parse, FcSevereWarning, "missing pattern element name");
+       FcPatternDestroy (pattern);
+       return;
+    }
+    
+    for (;;)
+    {
+       value = FcPopValue (parse);
+       if (value.type == FcTypeVoid)
+           break;
+       if (!FcPatternAdd (pattern, name, value, FcTrue))
+       {
+           FcConfigMessage (parse, FcSevereError, "out of memory");
+           break;
+       }
+    }
+
+    FcVStackPushPattern (parse, pattern);
+}
+
+static void
+FcParsePattern (FcConfigParse *parse)
+{
+    FcVStack   *vstack;
+    FcPattern  *pattern = FcPatternCreate ();
+
+    if (!pattern)
+    {
+       FcConfigMessage (parse, FcSevereError, "out of memory");
+       return;
+    }
+       
+    while ((vstack = FcVStackPop (parse)))
+    {
+       switch (vstack->tag) {
+       case FcVStackPattern:
+           if (!FcPatternAppend (pattern, vstack->u.pattern))
+           {
+               FcConfigMessage (parse, FcSevereError, "out of memory");
+               FcPatternDestroy (pattern);
+               return;
+           }
+           break;
+       default:
+           FcConfigMessage (parse, FcSevereWarning, "unknown pattern element");
+           break;
+       }
+       FcVStackDestroy (vstack);
+    }
+
+    FcVStackPushPattern (parse, pattern);
+}
+
 static void
 FcEndElement(void *userData, const XML_Char *name)
 {
@@ -1722,11 +2065,11 @@ FcEndElement(void *userData, const XML_Char *name)
        if (!FcStrUsesHome (data) || FcConfigHome ())
        {
            if (!FcConfigAddDir (parse->config, data))
-               FcConfigMessage (parse, FcSevereError, "out of memory");
+               FcConfigMessage (parse, FcSevereError, "out of memory; cannot add directory %s", data);
        }
        FcStrFree (data);
        break;
-    case FcElementCache:
+    case FcElementCacheDir:
        data = FcStrBufDone (&parse->pstack->str);
        if (!data)
        {
@@ -1735,11 +2078,22 @@ FcEndElement(void *userData, const XML_Char *name)
        }
        if (!FcStrUsesHome (data) || FcConfigHome ())
        {
-           if (!FcConfigSetCache (parse->config, data))
-               FcConfigMessage (parse, FcSevereError, "out of memory");
+           if (!FcConfigAddCacheDir (parse->config, data))
+               FcConfigMessage (parse, FcSevereError, "out of memory; cannot add cache directory %s", data);
        }
        FcStrFree (data);
        break;
+       
+    case FcElementCache:
+       data = FcStrBufDone (&parse->pstack->str);
+       if (!data)
+       {
+           FcConfigMessage (parse, FcSevereError, "out of memory");
+           break;
+       }
+       /* discard this data; no longer used */
+       FcStrFree (data);
+       break;
     case FcElementInclude:
        FcParseInclude (parse);
        break;
@@ -1806,6 +2160,12 @@ FcEndElement(void *userData, const XML_Char *name)
     case FcElementGlob:
        FcParseString (parse, FcVStackGlob);
        break;
+    case FcElementPattern:
+       FcParsePattern (parse);
+       break;
+    case FcElementPatelt:
+       FcParsePatelt (parse);
+       break;
     case FcElementName:
        FcParseString (parse, FcVStackField);
        break;
@@ -1902,11 +2262,126 @@ FcStartDoctypeDecl (void           *userData,
        FcConfigMessage (parse, FcSevereError, "invalid doctype \"%s\"", doctypeName);
 }
 
+#ifdef ENABLE_LIBXML2
+
+static void
+FcInternalSubsetDecl (void            *userData,
+                     const XML_Char  *doctypeName,
+                     const XML_Char  *sysid,
+                     const XML_Char  *pubid)
+{
+    FcStartDoctypeDecl (userData, doctypeName, sysid, pubid, 1);
+}
+
+static void
+FcExternalSubsetDecl (void            *userData,
+                     const XML_Char  *doctypeName,
+                     const XML_Char  *sysid,
+                     const XML_Char  *pubid)
+{
+    FcStartDoctypeDecl (userData, doctypeName, sysid, pubid, 0);
+}
+
+#else /* ENABLE_LIBXML2 */
+
 static void
 FcEndDoctypeDecl (void *userData)
 {
 }
 
+#endif /* ENABLE_LIBXML2 */
+
+static int
+FcSortCmpStr (const void *a, const void *b)
+{
+    const FcChar8    *as = *((FcChar8 **) a);
+    const FcChar8    *bs = *((FcChar8 **) b);
+    return FcStrCmp (as, bs);
+}
+
+static FcBool
+FcConfigParseAndLoadDir (FcConfig      *config,
+                        const FcChar8  *name,
+                        const FcChar8  *dir,
+                        FcBool         complain)
+{
+    DIR                    *d;
+    struct dirent   *e;
+    FcBool         ret = FcTrue;
+    FcChar8        *file;
+    FcChar8        *base;
+    FcStrSet       *files;
+
+    d = opendir ((char *) dir);
+    if (!d)
+    {
+       if (complain)
+           FcConfigMessage (0, FcSevereError, "Cannot open config dir \"%s\"",
+                            name);
+       ret = FcFalse;
+       goto bail0;
+    }
+    /* freed below */
+    file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
+    if (!file)
+    {
+       ret = FcFalse;
+       goto bail1;
+    }
+    
+    strcpy ((char *) file, (char *) dir);
+    strcat ((char *) file, "/");
+    base = file + strlen ((char *) file);
+    
+    files = FcStrSetCreate ();
+    if (!files)
+    {
+       ret = FcFalse;
+       goto bail2;
+    }
+    
+    if (FcDebug () & FC_DBG_CONFIG)
+       printf ("\tScanning config dir %s\n", dir);
+       
+    while (ret && (e = readdir (d)))
+    {
+       int d_len;
+#define TAIL       ".conf"
+#define TAIL_LEN    5
+       /*
+        * Add all files of the form [0-9]*.conf
+        */
+       if ('0' <= e->d_name[0] && e->d_name[0] <= '9' &&
+           (d_len = strlen (e->d_name)) < FC_MAX_FILE_LEN &&
+           d_len > TAIL_LEN &&
+           strcmp (e->d_name + d_len - TAIL_LEN, TAIL) == 0)
+       {
+           strcpy ((char *) base, (char *) e->d_name);
+           if (!FcStrSetAdd (files, file))
+           {
+               ret = FcFalse;
+               goto bail3;
+           }
+       }
+    }
+    if (ret)
+    {
+       int i;
+       qsort (files->strs, files->num, sizeof (FcChar8 *), 
+              (int (*)(const void *, const void *)) FcSortCmpStr);
+       for (i = 0; ret && i < files->num; i++)
+           ret = FcConfigParseAndLoad (config, files->strs[i], complain);
+    }
+bail3:
+    FcStrSetDestroy (files);
+bail2:
+    free (file);
+bail1:
+    closedir (d);
+bail0:
+    return ret || !complain;
+}
+
 FcBool
 FcConfigParseAndLoad (FcConfig     *config,
                      const FcChar8 *name,
@@ -1915,54 +2390,102 @@ FcConfigParseAndLoad (FcConfig     *config,
 
     XML_Parser     p;
     FcChar8        *filename;
-    FILE           *f;
+    int                    fd;
     int                    len;
-    void           *buf;
     FcConfigParse   parse;
     FcBool         error = FcTrue;
     
+#ifdef ENABLE_LIBXML2
+    xmlSAXHandler   sax;
+    char            buf[BUFSIZ];
+#else
+    void           *buf;
+#endif
+    
     filename = FcConfigFilename (name);
     if (!filename)
        goto bail0;
     
+    if (FcStrSetMember (config->configFiles, filename))
+    {
+        FcStrFree (filename);
+        return FcTrue;
+    }
+
     if (!FcStrSetAdd (config->configFiles, filename))
     {
        FcStrFree (filename);
        goto bail0;
     }
 
-    f = fopen ((char *) filename, "r");
-    FcStrFree (filename);
-    if (!f)
+    if (FcFileIsDir (filename))
+    {
+       FcBool ret = FcConfigParseAndLoadDir (config, name, filename, complain);
+       FcStrFree (filename);
+       return ret;
+    }
+
+    if (FcDebug () & FC_DBG_CONFIG)
+       printf ("\tLoading config file %s\n", filename);
+
+    fd = open ((char *) filename, O_RDONLY);
+    if (fd == -1) { 
+       FcStrFree (filename);
        goto bail0;
+    }
     
+#ifdef ENABLE_LIBXML2
+    memset(&sax, 0, sizeof(sax));
+
+    sax.internalSubset = FcInternalSubsetDecl;
+    sax.externalSubset = FcExternalSubsetDecl;
+    sax.startElement = FcStartElement;
+    sax.endElement = FcEndElement;
+    sax.characters = FcCharacterData;
+
+    p = xmlCreatePushParserCtxt (&sax, &parse, NULL, 0, (const char *) filename);
+#else
     p = XML_ParserCreate ("UTF-8");
+#endif
+    FcStrFree (filename);
+
     if (!p)
        goto bail1;
 
     if (!FcConfigInit (&parse, name, config, p))
        goto bail2;
 
+#ifndef ENABLE_LIBXML2
+
     XML_SetUserData (p, &parse);
     
     XML_SetDoctypeDeclHandler (p, FcStartDoctypeDecl, FcEndDoctypeDecl);
     XML_SetElementHandler (p, FcStartElement, FcEndElement);
     XML_SetCharacterDataHandler (p, FcCharacterData);
        
+#endif /* ENABLE_LIBXML2 */
+
     do {
+#ifndef ENABLE_LIBXML2
        buf = XML_GetBuffer (p, BUFSIZ);
        if (!buf)
        {
            FcConfigMessage (&parse, FcSevereError, "cannot get parse buffer");
            goto bail3;
        }
-       len = fread (buf, 1, BUFSIZ, f);
+#endif
+       len = read (fd, buf, BUFSIZ);
        if (len < 0)
        {
            FcConfigMessage (&parse, FcSevereError, "failed reading config file");
            goto bail3;
        }
+
+#ifdef ENABLE_LIBXML2
+       if (xmlParseChunk (p, buf, len, len == 0))
+#else
        if (!XML_ParseBuffer (p, len, len == 0))
+#endif
        {
            FcConfigMessage (&parse, FcSevereError, "%s", 
                           XML_ErrorString (XML_GetErrorCode (p)));
@@ -1975,7 +2498,8 @@ bail3:
 bail2:
     XML_ParserFree (p);
 bail1:
-    fclose (f);
+    close (fd);
+    fd = -1;
 bail0:
     if (error && complain)
     {
@@ -1987,3 +2511,6 @@ bail0:
     }
     return FcTrue;
 }
+#define __fcxml__
+#include "fcaliastail.h"
+#undef __fcxml__