]> git.wh0rd.org - fontconfig.git/blobdiff - src/fcxml.c
Fix more gcc4 warnings:
[fontconfig.git] / src / fcxml.c
index 19ec419def7e30f5b4e58a8e259938099a40e465..950bc9aa015003d91e8eb6196fa8dc0c28c5fba9 100644 (file)
 #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)
@@ -269,22 +248,6 @@ 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)
 {
@@ -360,72 +323,72 @@ typedef enum _FcElement {
     FcElementUnknown
 } FcElement;
 
+static struct {
+    const char  name[16];
+    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 },
+    { "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 },
-       { "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 },
-       
-       { 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;
@@ -498,9 +461,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);
@@ -528,6 +491,187 @@ 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;
+       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;
+    
+    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 (expr->u.field);
+       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);
+       }
+       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->field = (char *) FcStrCopy (field);
+       test->op = compare;
+       test->expr = expr;
+       o = FcNameGetObjectType (test->field);
+       if (o)
+           FcTypecheckExpr (parse, expr, o->type);
+    }
+    return test;
+}
+
+static FcEdit *
+FcEditCreate (FcConfigParse    *parse,
+             const char        *field,
+             FcOp              op,
+             FcExpr            *expr,
+             FcValueBinding    binding)
+{
+    FcEdit *e = (FcEdit *) malloc (sizeof (FcEdit));
+
+    if (e)
+    {
+       const FcObjectType      *o;
+
+       e->next = 0;
+       e->field = field;   /* already saved in grammar */
+       e->op = op;
+       e->expr = expr;
+       e->binding = binding;
+       o = FcNameGetObjectType (e->field);
+       if (o)
+           FcTypecheckExpr (parse, expr, o->type);
+    }
+    return e;
+}
+
 static void
 FcVStackPush (FcConfigParse *parse, FcVStack *vstack)
 {
@@ -854,7 +998,7 @@ FcConfigCleanup (FcConfigParse      *parse)
 }
 
 static const FcChar8 *
-FcConfigGetAttribute (FcConfigParse *parse, char *attr)
+FcConfigGetAttribute (FcConfigParse *parse, const char *attr)
 {
     FcChar8 **attrs;
     if (!parse->pstack)
@@ -981,7 +1125,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;
@@ -1091,15 +1235,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
@@ -1115,7 +1258,7 @@ FcParseBool (FcConfigParse *parse)
        FcConfigMessage (parse, FcSevereError, "out of memory");
        return;
     }
-    FcVStackPushBool (parse, FcConfigLexBool (s));
+    FcVStackPushBool (parse, FcConfigLexBool (parse, s));
     FcStrFree (s);
 }
 
@@ -1247,7 +1390,8 @@ FcParseAlias (FcConfigParse *parse)
     }
     if (prefer)
     {
-       edit = FcEditCreate (FcConfigSaveField ("family"),
+       edit = FcEditCreate (parse, 
+                            FcConfigSaveField ("family"),
                             FcOpPrepend,
                             prefer,
                             FcValueBindingWeak);
@@ -1259,7 +1403,8 @@ FcParseAlias (FcConfigParse *parse)
     if (accept)
     {
        next = edit;
-       edit = FcEditCreate (FcConfigSaveField ("family"),
+       edit = FcEditCreate (parse,
+                            FcConfigSaveField ("family"),
                             FcOpAppend,
                             accept,
                             FcValueBindingWeak);
@@ -1271,7 +1416,8 @@ FcParseAlias (FcConfigParse *parse)
     if (def)
     {
        next = edit;
-       edit = FcEditCreate (FcConfigSaveField ("family"),
+       edit = FcEditCreate (parse,
+                            FcConfigSaveField ("family"),
                             FcOpAppendLast,
                             def,
                             FcValueBindingWeak);
@@ -1282,7 +1428,7 @@ FcParseAlias (FcConfigParse *parse)
     }
     if (edit)
     {
-       test = FcTestCreate (FcMatchPattern,
+       test = FcTestCreate (parse, FcMatchPattern,
                             FcQualAny,
                             (FcChar8 *) FC_FAMILY,
                             FcOpEqual,
@@ -1437,7 +1583,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;
@@ -1445,7 +1591,7 @@ FcParseInclude (FcConfigParse *parse)
 }
 
 typedef struct _FcOpMap {
-    char    *name;
+    char    name[16];
     FcOp    op;
 } FcOpMap;
 
@@ -1471,7 +1617,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)
@@ -1553,7 +1699,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");
@@ -1571,7 +1717,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)
@@ -1626,7 +1772,7 @@ FcParseEdit (FcConfigParse *parse)
        }
     }
     expr = FcPopBinary (parse, FcOpComma);
-    edit = FcEditCreate ((char *) FcStrCopy (name), mode, expr, binding);
+    edit = FcEditCreate (parse, (char *) FcStrCopy (name), mode, expr, binding);
     if (!edit)
     {
        FcConfigMessage (parse, FcSevereError, "out of memory");
@@ -1780,18 +1926,12 @@ FcParsePatelt (FcConfigParse *parse)
        return;
     }
 
-    name = FcConfigGetAttribute (parse, "name");
+    name = (char *) FcConfigGetAttribute (parse, "name");
     if (!name)
     {
        FcConfigMessage (parse, FcSevereWarning, "missing pattern element name");
        return;
     }
-    name = FcObjectStaticName (name);
-    if (!name)
-    {
-       FcConfigMessage (parse, FcSevereError, "out of memory");
-       return;
-    }
     
     for (;;)
     {
@@ -2124,11 +2264,16 @@ FcConfigParseAndLoadDir (FcConfig       *config,
        
     while (ret && (e = readdir (d)))
     {
+       int d_len;
+#define TAIL       ".conf"
+#define TAIL_LEN    5
        /*
-        * Add all files of the form [0-9]*
+        * Add all files of the form [0-9]*.conf
         */
        if ('0' <= e->d_name[0] && e->d_name[0] <= '9' &&
-           strlen (e->d_name) < FC_MAX_FILE_LEN)
+           (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))
@@ -2235,6 +2380,7 @@ bail2:
     XML_ParserFree (p);
 bail1:
     fclose (f);
+    f = NULL;
 bail0:
     if (error && complain)
     {