fcxml.c 32.4 KB
Newer Older
Keith Packard's avatar
Keith Packard committed
1
/*
2
 * $XFree86: xc/lib/fontconfig/src/fcxml.c,v 1.3 2002/02/18 22:29:28 keithp Exp $
Keith Packard's avatar
Keith Packard committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * Copyright  2002 Keith Packard, member of The XFree86 Project, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Keith Packard not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Keith Packard makes no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

#include <stdarg.h>
26
#include <expat.h>
Keith Packard's avatar
Keith Packard committed
27 28 29
#include "fcint.h"

FcTest *
30
FcTestCreate (FcQual qual, const FcChar8 *field, FcOp compare, FcExpr *expr)
Keith Packard's avatar
Keith Packard committed
31
{
32
    FcTest	*test = (FcTest *) malloc (sizeof (FcTest));
Keith Packard's avatar
Keith Packard committed
33 34 35 36 37

    if (test)
    {
	test->next = 0;
	test->qual = qual;
38
	test->field = (char *) FcStrCopy (field);
Keith Packard's avatar
Keith Packard committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
	test->op = compare;
	test->expr = expr;
    }
    return test;
}

void
FcTestDestroy (FcTest *test)
{
    if (test->next)
	FcTestDestroy (test->next);
    FcExprDestroy (test->expr);
    FcStrFree ((FcChar8 *) test->field);
    free (test);
}

FcExpr *
FcExprCreateInteger (int i)
{
    FcExpr *e = (FcExpr *) malloc (sizeof (FcExpr));

    if (e)
    {
	e->op = FcOpInteger;
	e->u.ival = i;
    }
    return e;
}

FcExpr *
FcExprCreateDouble (double d)
{
    FcExpr *e = (FcExpr *) malloc (sizeof (FcExpr));

    if (e)
    {
	e->op = FcOpDouble;
	e->u.dval = d;
    }
    return e;
}

FcExpr *
82
FcExprCreateString (const FcChar8 *s)
Keith Packard's avatar
Keith Packard committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
{
    FcExpr *e = (FcExpr *) malloc (sizeof (FcExpr));

    if (e)
    {
	e->op = FcOpString;
	e->u.sval = FcStrCopy (s);
    }
    return e;
}

FcExpr *
FcExprCreateMatrix (const FcMatrix *m)
{
    FcExpr *e = (FcExpr *) malloc (sizeof (FcExpr));

    if (e)
    {
	e->op = FcOpMatrix;
	e->u.mval = FcMatrixCopy (m);
    }
    return e;
}

FcExpr *
FcExprCreateBool (FcBool b)
{
    FcExpr *e = (FcExpr *) malloc (sizeof (FcExpr));

    if (e)
    {
	e->op = FcOpBool;
	e->u.bval = b;
    }
    return e;
}

FcExpr *
FcExprCreateNil (void)
{
    FcExpr *e = (FcExpr *) malloc (sizeof (FcExpr));

    if (e)
    {
	e->op = FcOpNil;
    }
    return e;
}

FcExpr *
FcExprCreateField (const char *field)
{
    FcExpr *e = (FcExpr *) malloc (sizeof (FcExpr));

    if (e)
    {
	e->op = FcOpField;
140
	e->u.field = (char *) FcStrCopy ((FcChar8 *) field);
Keith Packard's avatar
Keith Packard committed
141 142 143 144 145
    }
    return e;
}

FcExpr *
146
FcExprCreateConst (const FcChar8 *constant)
Keith Packard's avatar
Keith Packard committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
{
    FcExpr *e = (FcExpr *) malloc (sizeof (FcExpr));

    if (e)
    {
	e->op = FcOpConst;
	e->u.constant = FcStrCopy (constant);
    }
    return e;
}

FcExpr *
FcExprCreateOp (FcExpr *left, FcOp op, FcExpr *right)
{
    FcExpr *e = (FcExpr *) malloc (sizeof (FcExpr));

    if (e)
    {
	e->op = op;
	e->u.tree.left = left;
	e->u.tree.right = right;
    }
    return e;
}

void
FcExprDestroy (FcExpr *e)
{
    switch (e->op) {
    case FcOpInteger:
	break;
    case FcOpDouble:
	break;
    case FcOpString:
	FcStrFree (e->u.sval);
	break;
    case FcOpMatrix:
	FcMatrixFree (e->u.mval);
	break;
    case FcOpCharSet:
	FcCharSetDestroy (e->u.cval);
	break;
    case FcOpBool:
	break;
    case FcOpField:
192
	FcStrFree ((FcChar8 *) e->u.field);
Keith Packard's avatar
Keith Packard committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
	break;
    case FcOpConst:
	FcStrFree (e->u.constant);
	break;
    case FcOpAssign:
    case FcOpAssignReplace:
    case FcOpPrepend:
    case FcOpPrependFirst:
    case FcOpAppend:
    case FcOpAppendLast:
	break;
    case FcOpOr:
    case FcOpAnd:
    case FcOpEqual:
    case FcOpContains:
    case FcOpNotEqual:
    case FcOpLess:
    case FcOpLessEqual:
    case FcOpMore:
    case FcOpMoreEqual:
    case FcOpPlus:
    case FcOpMinus:
    case FcOpTimes:
    case FcOpDivide:
    case FcOpQuest:
    case FcOpComma:
	FcExprDestroy (e->u.tree.right);
	/* fall through */
    case FcOpNot:
	FcExprDestroy (e->u.tree.left);
	break;
    case FcOpNil:
    case FcOpInvalid:
	break;
    }
    free (e);
}

FcEdit *
FcEditCreate (const char *field, FcOp op, FcExpr *expr)
{
    FcEdit *e = (FcEdit *) malloc (sizeof (FcEdit));

    if (e)
    {
	e->next = 0;
	e->field = field;   /* already saved in grammar */
	e->op = op;
	e->expr = expr;
    }
    return e;
}

void
FcEditDestroy (FcEdit *e)
{
    if (e->next)
	FcEditDestroy (e->next);
    FcStrFree ((FcChar8 *) e->field);
    if (e->expr)
	FcExprDestroy (e->expr);
}

char *
FcConfigSaveField (const char *field)
{
259
    return (char *) FcStrCopy ((FcChar8 *) field);
Keith Packard's avatar
Keith Packard committed
260 261
}

262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
typedef enum _FcElement {
    FcElementNone,
    FcElementFontconfig,
    FcElementDir,
    FcElementCache,
    FcElementInclude,
    FcElementConfig,
    FcElementMatch,
    FcElementAlias,
	
    FcElementBlank,

    FcElementPrefer,
    FcElementAccept,
    FcElementDefault,
    FcElementFamily,

    FcElementTest,
    FcElementEdit,
    FcElementInt,
    FcElementDouble,
    FcElementString,
    FcElementMatrix,
    FcElementBool,
    FcElementCharset,
    FcElementName,
    FcElementConst,
    FcElementOr,
    FcElementAnd,
    FcElementEq,
    FcElementNotEq,
    FcElementLess,
    FcElementLessEq,
    FcElementMore,
    FcElementMoreEq,
    FcElementPlus,
    FcElementMinus,
    FcElementTimes,
    FcElementDivide,
    FcElementNot,
    FcElementIf,
    FcElementUnknown
} FcElement;

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 },

	{ "prefer",	FcElementPrefer },
	{ "accept",	FcElementAccept },
	{ "default",	FcElementDefault },
	{ "family",	FcElementFamily },

	{ "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 },
	{ "plus",	FcElementPlus },
	{ "minus",	FcElementMinus },
	{ "times",	FcElementTimes },
	{ "divide",	FcElementDivide },
	{ "not",	FcElementNot },
	{ "if",		FcElementIf },
	
	{ 0,		0 }
    };

    int	    i;
    for (i = 0; fcElementMap[i].name; i++)
	if (!strcmp ((char *) name, fcElementMap[i].name))
	    return fcElementMap[i].element;
    return FcElementUnknown;
}

typedef struct _FcPStack {
    struct _FcPStack   *prev;
    FcElement		element;
    FcChar8		**attr;
    FcStrBuf		str;
} FcPStack;
    
typedef enum _FcVStackTag {
    FcVStackNone,

    FcVStackString,
    FcVStackFamily,
    FcVStackField,
    FcVStackConstant,
    
    FcVStackPrefer,
    FcVStackAccept,
    FcVStackDefault,
    
    FcVStackInteger,
    FcVStackDouble,
    FcVStackMatrix,
    FcVStackBool,
    
    FcVStackTest,
    FcVStackExpr,
    FcVStackEdit
} FcVStackTag;

typedef struct _FcVStack {
    struct _FcVStack	*prev;
    FcPStack		*pstack;	/* related parse element */
    FcVStackTag		tag;
    union {
	FcChar8		*string;

	int		integer;
	double		_double;
	FcMatrix	*matrix;
	FcBool		bool;

	FcTest		*test;
	FcQual		qual;
	FcOp		op;
	FcExpr		*expr;
	FcEdit		*edit;
    } u;
} FcVStack;

typedef struct _FcConfigParse {
    FcPStack	    *pstack;
    FcVStack	    *vstack;
    FcBool	    error;
    const FcChar8   *name;
    FcConfig	    *config;
    XML_Parser	    parser;
} FcConfigParse;

Keith Packard's avatar
Keith Packard committed
421
static void
422
FcConfigError (FcConfigParse *parse, char *fmt, ...)
Keith Packard's avatar
Keith Packard committed
423 424 425 426
{
    va_list	args;

    va_start (args, fmt);
427 428 429 430 431 432 433 434 435 436 437 438
    if (parse)
    {
	if (parse->name)
	    fprintf (stderr, "Fontconfig error: \"%s\", line %d: ",
		     parse->name, XML_GetCurrentLineNumber (parse->parser));
	else
	    fprintf (stderr, "Fontconfig error: line %d: ",
		     XML_GetCurrentLineNumber (parse->parser));
	parse->error = FcTrue;
    }
    else
	fprintf (stderr, "Fontconfig error: ");
Keith Packard's avatar
Keith Packard committed
439 440 441 442 443
    vfprintf (stderr, fmt, args);
    fprintf (stderr, "\n");
    va_end (args);
}

444 445
static void
FcVStackPush (FcConfigParse *parse, FcVStack *vstack)
Keith Packard's avatar
Keith Packard committed
446
{
447 448 449
    vstack->prev = parse->vstack;
    vstack->pstack = parse->pstack ? parse->pstack->prev : 0;
    parse->vstack = vstack;
Keith Packard's avatar
Keith Packard committed
450 451
}

452 453
static FcVStack *
FcVStackCreate (void)
Keith Packard's avatar
Keith Packard committed
454
{
455
    FcVStack    *new;
Keith Packard's avatar
Keith Packard committed
456

457 458 459 460 461 462 463
    new = malloc (sizeof (FcVStack));
    if (!new)
	return 0;
    new->tag = FcVStackNone;
    new->prev = 0;
    return new;
}
Keith Packard's avatar
Keith Packard committed
464

465 466
static void
FcVStackDestroy (FcVStack *vstack)
Keith Packard's avatar
Keith Packard committed
467
{
468
    FcVStack    *prev;
Keith Packard's avatar
Keith Packard committed
469

470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
    for (; vstack; vstack = prev)
    {
	prev = vstack->prev;
	switch (vstack->tag) {
	case FcVStackNone:
	    break;
	case FcVStackString:
	case FcVStackFamily:
	case FcVStackField:
	case FcVStackConstant:
	    FcStrFree (vstack->u.string);
	    break;
	case FcVStackInteger:
	case FcVStackDouble:
	    break;
	case FcVStackMatrix:
	    FcMatrixFree (vstack->u.matrix);
	    break;
	case FcVStackBool:
	    break;
	case FcVStackTest:
	    FcTestDestroy (vstack->u.test);
	    break;
	case FcVStackExpr:
	case FcVStackPrefer:
	case FcVStackAccept:
	case FcVStackDefault:
	    FcExprDestroy (vstack->u.expr);
	    break;
	case FcVStackEdit:
	    FcEditDestroy (vstack->u.edit);
	    break;
	}
	free (vstack);
    }
Keith Packard's avatar
Keith Packard committed
505 506 507
}

static FcBool
508
FcVStackPushString (FcConfigParse *parse, FcVStackTag tag, FcChar8 *string)
Keith Packard's avatar
Keith Packard committed
509
{
510 511 512 513 514 515 516
    FcVStack    *vstack = FcVStackCreate ();
    if (!vstack)
	return FcFalse;
    vstack->u.string = string;
    vstack->tag = tag;
    FcVStackPush (parse, vstack);
    return FcTrue;
Keith Packard's avatar
Keith Packard committed
517 518 519
}

static FcBool
520
FcVStackPushInteger (FcConfigParse *parse, int integer)
Keith Packard's avatar
Keith Packard committed
521
{
522 523
    FcVStack    *vstack = FcVStackCreate ();
    if (!vstack)
Keith Packard's avatar
Keith Packard committed
524
	return FcFalse;
525 526 527 528
    vstack->u.integer = integer;
    vstack->tag = FcVStackInteger;
    FcVStackPush (parse, vstack);
    return FcTrue;
Keith Packard's avatar
Keith Packard committed
529 530 531
}

static FcBool
532
FcVStackPushDouble (FcConfigParse *parse, double _double)
Keith Packard's avatar
Keith Packard committed
533
{
534 535
    FcVStack    *vstack = FcVStackCreate ();
    if (!vstack)
Keith Packard's avatar
Keith Packard committed
536
	return FcFalse;
537 538 539 540
    vstack->u._double = _double;
    vstack->tag = FcVStackDouble;
    FcVStackPush (parse, vstack);
    return FcTrue;
Keith Packard's avatar
Keith Packard committed
541 542 543
}

static FcBool
544
FcVStackPushMatrix (FcConfigParse *parse, FcMatrix *matrix)
Keith Packard's avatar
Keith Packard committed
545
{
546 547
    FcVStack    *vstack = FcVStackCreate ();
    if (!vstack)
Keith Packard's avatar
Keith Packard committed
548
	return FcFalse;
549 550
    matrix = FcMatrixCopy (matrix);
    if (!matrix)
Keith Packard's avatar
Keith Packard committed
551
    {
552 553
	FcVStackDestroy (vstack);
	return FcFalse;
Keith Packard's avatar
Keith Packard committed
554
    }
555 556 557 558
    vstack->u.matrix = matrix;
    vstack->tag = FcVStackMatrix;
    FcVStackPush (parse, vstack);
    return FcTrue;
Keith Packard's avatar
Keith Packard committed
559 560 561
}

static FcBool
562
FcVStackPushBool (FcConfigParse *parse, FcBool bool)
Keith Packard's avatar
Keith Packard committed
563
{
564 565 566 567 568 569 570 571
    FcVStack    *vstack = FcVStackCreate ();
    if (!vstack)
	return FcFalse;
    vstack->u.bool = bool;
    vstack->tag = FcVStackBool;
    FcVStackPush (parse, vstack);
    return FcTrue;
}
Keith Packard's avatar
Keith Packard committed
572

573 574 575 576 577
static FcBool
FcVStackPushTest (FcConfigParse *parse, FcTest *test)
{
    FcVStack    *vstack = FcVStackCreate ();
    if (!vstack)
Keith Packard's avatar
Keith Packard committed
578
	return FcFalse;
579 580 581
    vstack->u.test = test;
    vstack->tag = FcVStackTest;
    FcVStackPush (parse, vstack);
Keith Packard's avatar
Keith Packard committed
582 583 584 585
    return FcTrue;
}

static FcBool
586
FcVStackPushExpr (FcConfigParse *parse, FcVStackTag tag, FcExpr *expr)
Keith Packard's avatar
Keith Packard committed
587
{
588 589 590 591 592 593 594 595
    FcVStack    *vstack = FcVStackCreate ();
    if (!vstack)
	return FcFalse;
    vstack->u.expr = expr;
    vstack->tag = tag;
    FcVStackPush (parse, vstack);
    return FcTrue;
}
Keith Packard's avatar
Keith Packard committed
596

597 598 599 600 601
static FcBool
FcVStackPushEdit (FcConfigParse *parse, FcEdit *edit)
{
    FcVStack    *vstack = FcVStackCreate ();
    if (!vstack)
Keith Packard's avatar
Keith Packard committed
602
	return FcFalse;
603 604 605
    vstack->u.edit = edit;
    vstack->tag = FcVStackEdit;
    FcVStackPush (parse, vstack);
Keith Packard's avatar
Keith Packard committed
606 607 608
    return FcTrue;
}

609 610
static FcVStack *
FcVStackFetch (FcConfigParse *parse, int off)
Keith Packard's avatar
Keith Packard committed
611
{
612
    FcVStack    *vstack;
Keith Packard's avatar
Keith Packard committed
613

614 615 616 617 618 619 620 621
    for (vstack = parse->vstack; vstack && off-- > 0; vstack = vstack->prev);
    return vstack;
}

static void
FcVStackClear (FcConfigParse *parse)
{
    while (parse->vstack && parse->vstack->pstack == parse->pstack)
Keith Packard's avatar
Keith Packard committed
622
    {
623 624 625 626
	FcVStack    *vstack = parse->vstack;
	parse->vstack = vstack->prev;
	vstack->prev = 0;
	FcVStackDestroy (vstack);
Keith Packard's avatar
Keith Packard committed
627 628 629
    }
}

630 631
static FcVStack *
FcVStackPop (FcConfigParse *parse)
Keith Packard's avatar
Keith Packard committed
632
{
633
    FcVStack	*vstack = parse->vstack;
Keith Packard's avatar
Keith Packard committed
634
    
635
    if (!vstack || vstack->pstack != parse->pstack)
Keith Packard's avatar
Keith Packard committed
636
	return 0;
637 638 639 640 641 642 643 644 645 646 647 648 649 650
    parse->vstack = vstack->prev;
    vstack->prev = 0;
    return vstack;
}

static int
FcVStackElements (FcConfigParse *parse)
{
    int		h = 0;
    FcVStack	*vstack = parse->vstack;
    while (vstack && vstack->pstack == parse->pstack)
    {
	h++;
	vstack = vstack->prev;
Keith Packard's avatar
Keith Packard committed
651
    }
652
    return h;
Keith Packard's avatar
Keith Packard committed
653 654
}

655 656
static FcChar8 **
FcConfigSaveAttr (const XML_Char **attr)
Keith Packard's avatar
Keith Packard committed
657
{
658 659 660 661 662
    int		n;
    int		slen;
    int		i;
    FcChar8	**new;
    FcChar8	*s;
Keith Packard's avatar
Keith Packard committed
663

664 665 666 667 668 669 670 671 672 673 674
    if (!attr)
	return 0;
    slen = 0;
    for (i = 0; attr[i]; i++)
	slen += strlen (attr[i]) + 1;
    n = i;
    new = malloc ((i + 1) * sizeof (FcChar8 *) + slen);
    if (!new)
	return 0;
    s = (FcChar8 *) (new + (i + 1));
    for (i = 0; attr[i]; i++)
Keith Packard's avatar
Keith Packard committed
675
    {
676 677 678
	new[i] = s;
	strcpy ((char *) s, (char *) attr[i]);
	s += strlen ((char *) s) + 1;
Keith Packard's avatar
Keith Packard committed
679
    }
680 681 682
    new[i] = 0;
    return new;
}
Keith Packard's avatar
Keith Packard committed
683

684 685 686 687 688 689 690 691 692 693
static FcBool
FcPStackPush (FcConfigParse *parse, FcElement element, const XML_Char **attr)
{
    FcPStack   *new = malloc (sizeof (FcPStack));

    if (!new)
	return FcFalse;
    new->prev = parse->pstack;
    new->element = element;
    if (attr)
Keith Packard's avatar
Keith Packard committed
694
    {
695 696 697
	new->attr = FcConfigSaveAttr (attr);
	if (!new->attr)
	    FcConfigError (parse, "out of memory");
Keith Packard's avatar
Keith Packard committed
698
    }
699 700 701 702 703 704
    else
	new->attr = 0;
    FcStrBufInit (&new->str, 0, 0);
    parse->pstack = new;
    return FcTrue;
}
Keith Packard's avatar
Keith Packard committed
705

706 707 708 709 710 711
static FcBool
FcPStackPop (FcConfigParse *parse)
{
    FcPStack   *old;
    
    if (!parse->pstack) 
Keith Packard's avatar
Keith Packard committed
712
    {
713 714
	FcConfigError (parse, "mismatching element");
	return FcFalse;
Keith Packard's avatar
Keith Packard committed
715
    }
716 717 718 719 720 721 722 723
    FcVStackClear (parse);
    old = parse->pstack;
    parse->pstack = old->prev;
    FcStrBufDestroy (&old->str);
    if (old->attr)
	free (old->attr);
    free (old);
    return FcTrue;
Keith Packard's avatar
Keith Packard committed
724 725
}

726 727
static FcBool
FcConfigInit (FcConfigParse *parse, const FcChar8 *name, FcConfig *config, XML_Parser parser)
Keith Packard's avatar
Keith Packard committed
728
{
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
    parse->pstack = 0;
    parse->vstack = 0;
    parse->error = FcFalse;
    parse->name = name;
    parse->config = config;
    parse->parser = parser;
    return FcTrue;
}

static void
FcConfigCleanup (FcConfigParse	*parse)
{
    while (parse->pstack)
	FcPStackPop (parse);
}

static const FcChar8 *
FcConfigGetAttribute (FcConfigParse *parse, char *attr)
{
    FcChar8 **attrs;
    if (!parse->pstack)
Keith Packard's avatar
Keith Packard committed
750 751
	return 0;

752 753
    attrs = parse->pstack->attr;
    while (*attrs)
Keith Packard's avatar
Keith Packard committed
754
    {
755 756 757
	if (!strcmp ((char *) *attrs, attr))
	    return attrs[1];
	attrs += 2;
Keith Packard's avatar
Keith Packard committed
758 759 760 761
    }
    return 0;
}

762 763
static void
FcStartElement(void *userData, const XML_Char *name, const XML_Char **attr)
Keith Packard's avatar
Keith Packard committed
764
{
765 766 767 768 769
    FcConfigParse   *parse = userData;
    FcElement	    element;
    
    element = FcElementMap (name);
    if (element == FcElementUnknown)
Keith Packard's avatar
Keith Packard committed
770
    {
771 772
	FcConfigError (parse, "unknown element \"%s\"", name);
	return;
Keith Packard's avatar
Keith Packard committed
773
    }
774 775
    
    if (!FcPStackPush (parse, element, attr))
Keith Packard's avatar
Keith Packard committed
776
    {
777 778
	FcConfigError (parse, "out of memory");
	return;
Keith Packard's avatar
Keith Packard committed
779
    }
780 781
    return;
}
Keith Packard's avatar
Keith Packard committed
782

783 784 785 786 787
static void
FcParseBlank (FcConfigParse *parse)
{
    int	    n = FcVStackElements (parse);
    while (n-- > 0)
Keith Packard's avatar
Keith Packard committed
788
    {
789 790 791 792
	FcVStack    *v = FcVStackFetch (parse, n);
	if (v->tag != FcVStackInteger)
	    FcConfigError (parse, "non-integer blank");
	else
Keith Packard's avatar
Keith Packard committed
793
	{
794
	    if (!parse->config->blanks)
Keith Packard's avatar
Keith Packard committed
795
	    {
796 797 798 799 800 801
		parse->config->blanks = FcBlanksCreate ();
		if (!parse->config->blanks)
		{
		    FcConfigError (parse, "out of memory");
		    break;
		}
Keith Packard's avatar
Keith Packard committed
802
	    }
803
	    if (!FcBlanksAdd (parse->config->blanks, v->u.integer))
Keith Packard's avatar
Keith Packard committed
804
	    {
805 806
		FcConfigError (parse, "out of memory");
		break;
Keith Packard's avatar
Keith Packard committed
807 808 809
	    }
	}
    }
810
}
Keith Packard's avatar
Keith Packard committed
811

812 813 814 815 816 817 818 819 820 821
static void
FcParseInt (FcConfigParse *parse)
{
    FcChar8 *s, *end;
    int	    l;
    
    if (!parse->pstack)
	return;
    s = FcStrBufDone (&parse->pstack->str);
    if (!s)
Keith Packard's avatar
Keith Packard committed
822
    {
823 824
	FcConfigError (parse, "out of memory");
	return;
Keith Packard's avatar
Keith Packard committed
825
    }
826 827 828 829 830 831 832
    end = 0;
    l = (int) strtol ((char *) s, (char **)&end, 0);
    if (end != s + strlen ((char *) s))
	FcConfigError (parse, "\"%s\": not a valid integer", s);
    else
	FcVStackPushInteger (parse, l);
    FcStrFree (s);
Keith Packard's avatar
Keith Packard committed
833 834
}

835 836
static void
FcParseDouble (FcConfigParse *parse)
Keith Packard's avatar
Keith Packard committed
837
{
838 839
    FcChar8 *s, *end;
    double  d;
Keith Packard's avatar
Keith Packard committed
840
    
841 842 843 844
    if (!parse->pstack)
	return;
    s = FcStrBufDone (&parse->pstack->str);
    if (!s)
Keith Packard's avatar
Keith Packard committed
845
    {
846 847
	FcConfigError (parse, "out of memory");
	return;
Keith Packard's avatar
Keith Packard committed
848
    }
849 850 851 852
    end = 0;
    d = strtod ((char *) s, (char **)&end);
    if (end != s + strlen ((char *) s))
	FcConfigError (parse, "\"%s\": not a valid double", s);
Keith Packard's avatar
Keith Packard committed
853
    else
854 855 856
	FcVStackPushDouble (parse, d);
    FcStrFree (s);
}
Keith Packard's avatar
Keith Packard committed
857

858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
static void
FcParseString (FcConfigParse *parse, FcVStackTag tag)
{
    FcChar8 *s;
    
    if (!parse->pstack)
	return;
    s = FcStrBufDone (&parse->pstack->str);
    if (!s)
    {
	FcConfigError (parse, "out of memory");
	return;
    }
    if (!FcVStackPushString (parse, tag, s))
	FcStrFree (s);
}

static void
FcParseMatrix (FcConfigParse *parse)
{
    FcVStack	*vstack;
    enum { m_done, m_xx, m_xy, m_yx, m_yy } matrix_state = m_yy;
    FcMatrix	m;
    
    while ((vstack = FcVStackPop (parse)))
    {
	if (vstack->tag != FcVStackDouble)
	    FcConfigError (parse, "non-double matrix element");
	else
	{
	    double v = vstack->u._double;
	    switch (matrix_state) {
	    case m_xx: m.xx = v; break;
	    case m_xy: m.xy = v; break;
	    case m_yx: m.yx = v; break;
	    case m_yy: m.yy = v; break;
	    default: break;
	    }
	    matrix_state--;
	}
    }
    if (matrix_state != m_done)
	FcConfigError (parse, "wrong number of matrix elements");
    else
	FcVStackPushMatrix (parse, &m);
Keith Packard's avatar
Keith Packard committed
903 904 905
}

static FcBool
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
FcConfigLexBool (const FcChar8 *bool)
{
    if (*bool == 't' || *bool == 'T')
	return FcTrue;
    if (*bool == 'y' || *bool == 'Y')
	return FcTrue;
    if (*bool == '1')
	return FcTrue;
    return FcFalse;
}

static void
FcParseBool (FcConfigParse *parse)
{
    FcChar8 *s;

    if (!parse->pstack)
	return;
    s = FcStrBufDone (&parse->pstack->str);
    if (!s)
    {
	FcConfigError (parse, "out of memory");
	return;
    }
    FcVStackPushBool (parse, FcConfigLexBool (s));
    FcStrFree (s);
}

static void
FcParseFamilies (FcConfigParse *parse, FcVStackTag tag)
Keith Packard's avatar
Keith Packard committed
936
{
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
    FcVStack	*vstack;
    FcExpr	*left, *expr = 0, *new;

    while ((vstack = FcVStackPop (parse)))
    {
	if (vstack->tag != FcVStackFamily)
	{
	    FcConfigError (parse, "non-family");
	    break;
	}
	left = vstack->u.expr;
	vstack->tag = FcVStackNone;
	FcVStackDestroy (vstack);
	if (expr)
	{
	    new = FcExprCreateOp (left, FcOpComma, expr);
	    if (!new)
	    {
		FcConfigError (parse, "out of memory");
		FcExprDestroy (left);
		FcExprDestroy (expr);
		break;
	    }
	    expr = new;
	}
	else
	    expr = left;
    }
    if (expr)
    {
	if (!FcVStackPushExpr (parse, tag, expr))
	{
	    FcConfigError (parse, "out of memory");
	    if (expr)
		FcExprDestroy (expr);
	}
    }
}

static void
FcParseFamily (FcConfigParse *parse)
{
    FcChar8 *s;
    FcExpr  *expr;

    if (!parse->pstack)
	return;
    s = FcStrBufDone (&parse->pstack->str);
    if (!s)
    {
	FcConfigError (parse, "out of memory");
	return;
    }
    expr = FcExprCreateString (s);
    FcStrFree (s);
    if (expr)
	FcVStackPushExpr (parse, FcVStackFamily, expr);
}

static void
FcParseAlias (FcConfigParse *parse)
{
    FcExpr	*family = 0, *accept = 0, *prefer = 0, *def = 0;
Keith Packard's avatar
Keith Packard committed
1000
    FcEdit	*edit = 0, *next;
1001
    FcVStack	*vstack;
Keith Packard's avatar
Keith Packard committed
1002 1003
    FcTest	*test;

1004
    while ((vstack = FcVStackPop (parse))) 
Keith Packard's avatar
Keith Packard committed
1005
    {
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
	switch (vstack->tag) {
	case FcVStackFamily:
	    if (family)
		FcExprDestroy (family);
	    family = vstack->u.expr;
	    vstack->tag = FcVStackNone;
	    break;
	case FcVStackPrefer:
	    if (prefer)
		FcExprDestroy (prefer);
	    prefer = vstack->u.expr;
	    vstack->tag = FcVStackNone;
	    break;
	case FcVStackAccept:
	    if (accept)
		FcExprDestroy (accept);
	    accept = vstack->u.expr;
	    vstack->tag = FcVStackNone;
	    break;
	case FcVStackDefault:
	    if (def)
		FcExprDestroy (def);
	    def = vstack->u.expr;
	    vstack->tag = FcVStackNone;
	    break;
	default:
	    FcConfigError (parse, "bad alias");
	    break;
	}
	FcVStackDestroy (vstack);
Keith Packard's avatar
Keith Packard committed
1036
    }
1037
    if (!family)
1038 1039 1040 1041
    {
	FcConfigError (parse, "missing family in alias");
	return;
    }
Keith Packard's avatar
Keith Packard committed
1042 1043 1044 1045 1046 1047 1048
    if (prefer)
    {
	edit = FcEditCreate (FcConfigSaveField ("family"),
			     FcOpPrepend,
			     prefer);
	if (edit)
	    edit->next = 0;
1049 1050
	else
	    FcExprDestroy (prefer);
Keith Packard's avatar
Keith Packard committed
1051 1052 1053 1054 1055 1056 1057 1058 1059
    }
    if (accept)
    {
	next = edit;
	edit = FcEditCreate (FcConfigSaveField ("family"),
			     FcOpAppend,
			     accept);
	if (edit)
	    edit->next = next;
1060 1061
	else
	    FcExprDestroy (accept);
Keith Packard's avatar
Keith Packard committed
1062 1063 1064 1065 1066 1067 1068 1069 1070
    }
    if (def)
    {
	next = edit;
	edit = FcEditCreate (FcConfigSaveField ("family"),
			      FcOpAppendLast,
			      def);
	if (edit)
	    edit->next = next;
1071 1072
	else
	    FcExprDestroy (def);
Keith Packard's avatar
Keith Packard committed
1073 1074 1075 1076
    }
    if (edit)
    {
	test = FcTestCreate (FcQualAny,
1077
			     FcStrCopy ((FcChar8 *) "family"),
Keith Packard's avatar
Keith Packard committed
1078 1079 1080
			     FcOpEqual,
			     family);
	if (test)
1081 1082
	    if (!FcConfigAddEdit (parse->config, test, edit, FcMatchPattern))
		FcTestDestroy (test);
Keith Packard's avatar
Keith Packard committed
1083
    }
1084 1085
    else
	FcExprDestroy (family);
Keith Packard's avatar
Keith Packard committed
1086 1087
}

1088 1089
static FcExpr *
FcPopExpr (FcConfigParse *parse)
Keith Packard's avatar
Keith Packard committed
1090
{
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
    FcVStack	*vstack = FcVStackPop (parse);
    FcExpr	*expr = 0;
    if (!vstack)
	return 0;
    switch (vstack->tag) {
    case FcVStackNone:
	break;
    case FcVStackString:
    case FcVStackFamily:
    case FcVStackField:
    case FcVStackConstant:
	expr = FcExprCreateString (vstack->u.string);
	break;
    case FcVStackPrefer:
    case FcVStackAccept:
    case FcVStackDefault:
	expr = vstack->u.expr;
	vstack->tag = FcVStackNone;
	break;
    case FcVStackInteger:
	expr = FcExprCreateInteger (vstack->u.integer);
	break;
    case FcVStackDouble:
	expr = FcExprCreateDouble (vstack->u._double);
	break;
    case FcVStackMatrix:
	expr = FcExprCreateMatrix (vstack->u.matrix);
	break;
    case FcVStackBool:
	expr = FcExprCreateBool (vstack->u.bool);
	break;
    case FcVStackTest:
	break;
    case FcVStackExpr:
	expr = vstack->u.expr;
	break;
    case FcVStackEdit:
	break;
    }
    FcVStackDestroy (vstack);
    return expr;
}

static FcExpr *
FcPopExprs (FcConfigParse *parse, FcOp op)
{
    FcExpr  *left, *expr = 0, *new;
Keith Packard's avatar
Keith Packard committed
1138

1139
    while ((left = FcPopExpr (parse)))
Keith Packard's avatar
Keith Packard committed
1140
    {
1141
	if (expr)
Keith Packard's avatar
Keith Packard committed
1142
	{
1143 1144 1145 1146 1147 1148
	    new = FcExprCreateOp (left, op, expr);
	    if (!new)
	    {
		FcConfigError (parse, "out of memory");
		FcExprDestroy (left);
		FcExprDestroy (expr);
Keith Packard's avatar
Keith Packard committed
1149
		break;
1150 1151
	    }
	    expr = new;
Keith Packard's avatar
Keith Packard committed
1152
	}
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
	else
	    expr = left;
    }
    return expr;
}

static void
FcParseExpr (FcConfigParse *parse, FcOp op)
{
    FcExpr  *expr = FcPopExprs (parse, op);
    if (expr)
	FcVStackPushExpr (parse, FcVStackExpr, expr);
}

static void
FcParseInclude (FcConfigParse *parse)
{
    FcChar8	    *s;
    const FcChar8   *i;
    FcBool	    ignore_missing = FcFalse;
    
    s = FcStrBufDone (&parse->pstack->str);
    if (!s)
    {
	FcConfigError (parse, "out of memory");
	return;
    }
    i = FcConfigGetAttribute (parse, "ignore_missing");
    if (i && FcConfigLexBool ((FcChar8 *) i) == FcTrue)
	ignore_missing = FcTrue;
1183
    if (!FcConfigParseAndLoad (parse->config, s, !ignore_missing))
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
	parse->error = FcTrue;
    free (s);
}

typedef struct _FcOpMap {
    char    *name;
    FcOp    op;
} FcOpMap;

static FcOp
FcConfigLexOp (const FcChar8 *op, const FcOpMap	*map, int nmap)
{
    int	i;

    for (i = 0; i < nmap; i++)
	if (!strcmp ((char *) op, map[i].name)) 
	    return map[i].op;
    return FcOpInvalid;
}

static const FcOpMap fcCompareOps[] = {
    { "eq",		FcOpEqual	    },
    { "not_eq",		FcOpNotEqual	    },
    { "less",		FcOpLess	    },
    { "less_eq",	FcOpLessEqual	    },
    { "more",		FcOpMore	    },
    { "more_eq",	FcOpMoreEqual	    }
};

#define NUM_COMPARE_OPS (sizeof fcCompareOps / sizeof fcCompareOps[0])

static FcOp
FcConfigLexCompare (const FcChar8 *compare)
{
    return FcConfigLexOp (compare, fcCompareOps, NUM_COMPARE_OPS);
}


static void
FcParseTest (FcConfigParse *parse)
{
    const FcChar8   *qual_string;
    FcQual	    qual;
    const FcChar8   *name;
    const FcChar8   *compare_string;
    FcOp	    compare;
    FcExpr	    *expr;
    FcTest	    *test;

    qual_string = FcConfigGetAttribute (parse, "qual");
    if (!qual_string)
	qual = FcQualAny;
    else
    {
	if (!strcmp ((char *) qual_string, "any"))
	    qual = FcQualAny;
	else if (!strcmp ((char *) qual_string, "all"))
	    qual = FcQualAll;
	else
Keith Packard's avatar
Keith Packard committed
1243
	{
1244 1245
	    FcConfigError (parse, "invalid test qual \"%s\"", qual_string);
	    return;
Keith Packard's avatar
Keith Packard committed
1246
	}
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
    }
    name = FcConfigGetAttribute (parse, "name");
    if (!name)
    {
	FcConfigError (parse, "missing test name");
	return;
    }
    compare_string = FcConfigGetAttribute (parse, "compare");
    if (!compare_string)
	compare = FcOpEqual;
    else
    {
	compare = FcConfigLexCompare (compare_string);
	if (compare == FcOpInvalid)
Keith Packard's avatar
Keith Packard committed
1261
	{
1262 1263
	    FcConfigError (parse, "invalid test compare \"%s\"", compare_string);
	    return;
Keith Packard's avatar
Keith Packard committed
1264
	}
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
    }
    expr = FcPopExpr (parse);
    if (!expr)
    {
	FcConfigError (parse, "missing test expression");
	return;
    }
    test = FcTestCreate (qual, name, compare, expr);
    if (!test)
    {
	FcConfigError (parse, "out of memory");
	return;
    }
    FcVStackPushTest (parse, test);
}

static const FcOpMap fcModeOps[] = {
    { "assign",		FcOpAssign	    },
    { "assign_replace",	FcOpAssignReplace   },
    { "prepend",	FcOpPrepend	    },
    { "prepend_first",	FcOpPrependFirst    },
    { "append",		FcOpAppend	    },
    { "append_last",	FcOpAppendLast	    },
};

#define NUM_MODE_OPS (sizeof fcModeOps / sizeof fcModeOps[0])

static FcOp
FcConfigLexMode (const FcChar8 *mode)
{
    return FcConfigLexOp (mode, fcModeOps, NUM_MODE_OPS);
}

static void
FcParseEdit (FcConfigParse *parse)
{
    const FcChar8   *name;
    const FcChar8   *mode_string;
    FcOp	    mode;
    FcExpr	    *expr;
    FcEdit	    *edit;

    name = FcConfigGetAttribute (parse, "name");
    if (!name)
    {
	FcConfigError (parse, "missing edit name");
	return;
    }
    mode_string = FcConfigGetAttribute (parse, "mode");
    if (!mode_string)
	mode = FcOpEqual;
    else
    {
	mode = FcConfigLexMode (mode_string);
	if (mode == FcOpInvalid)
Keith Packard's avatar
Keith Packard committed
1320
	{
1321 1322
	    FcConfigError (parse, "invalid edit mode \"%s\"", mode_string);
	    return;
Keith Packard's avatar
Keith Packard committed
1323
	}
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
    }
    expr = FcPopExprs (parse, FcOpComma);
    edit = FcEditCreate ((char *) FcStrCopy (name), mode, expr);
    if (!edit)
    {
	FcConfigError (parse, "out of memory");
	FcExprDestroy (expr);
	return;
    }
    if (!FcVStackPushEdit (parse, edit))
	FcEditDestroy (edit);
}

static void
FcParseMatch (FcConfigParse *parse)
{
    const FcChar8   *kind_name;
    FcMatchKind	    kind;
    FcTest	    *test = 0;
    FcEdit	    *edit = 0;
    FcVStack	    *vstack;

    kind_name = FcConfigGetAttribute (parse, "target");
    if (!kind_name)
	kind = FcMatchPattern;
    else
    {
	if (!strcmp ((char *) kind_name, "pattern"))
	    kind = FcMatchPattern;
	else if (!strcmp ((char *) kind_name, "font"))
	    kind = FcMatchFont;
	else
Keith Packard's avatar
Keith Packard committed
1356
	{
1357 1358
	    FcConfigError (parse, "invalid match target \"%s\"", kind_name);
	    return;
Keith Packard's avatar
Keith Packard committed
1359
	}
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
    }
    while ((vstack = FcVStackPop (parse)))
    {
	switch (vstack->tag) {
	case FcVStackTest:
	    vstack->u.test->next = test;
	    test = vstack->u.test;
	    vstack->tag = FcVStackNone;
	    break;
	case FcVStackEdit:
	    vstack->u.edit->next = edit;
	    edit = vstack->u.edit;
	    vstack->tag = FcVStackNone;
	    break;
	default:
	    FcConfigError (parse, "invalid match element");
	    break;
	}
	FcVStackDestroy (vstack);
    }
    if (!FcConfigAddEdit (parse->config, test, edit, kind))
	FcConfigError (parse, "out of memory");
}

static void
FcEndElement(void *userData, const XML_Char *name)
{
    FcConfigParse   *parse = userData;
    FcChar8	    *data;
    
    if (!parse->pstack)
	return;
    switch (parse->pstack->element) {
    case FcElementNone:
	break;
    case FcElementFontconfig:
	break;
    case FcElementDir:
	data = FcStrBufDone (&parse->pstack->str);
	if (!data)
Keith Packard's avatar
Keith Packard committed
1400
	{
1401 1402
	    FcConfigError (parse, "out of memory");
	    break;
Keith Packard's avatar
Keith Packard committed
1403
	}
1404 1405 1406 1407 1408 1409 1410
	if (!FcConfigAddDir (parse->config, data))
	    FcConfigError (parse, "out of memory");
	free (data);
	break;
    case FcElementCache:
	data = FcStrBufDone (&parse->pstack->str);
	if (!data)
Keith Packard's avatar
Keith Packard committed
1411
	{
1412
	    FcConfigError (parse, "out of memory");
Keith Packard's avatar
Keith Packard committed
1413
	    break;
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
	}
	if (!FcConfigSetCache (parse->config, data))
	    FcConfigError (parse, "out of memory");
	free (data);
	break;
    case FcElementInclude:
	FcParseInclude (parse);
	break;
    case FcElementConfig:
	break;
    case FcElementMatch:
	FcParseMatch (parse);
	break;
    case FcElementAlias:
	FcParseAlias (parse);
	break;

    case FcElementBlank:
	FcParseBlank (parse);
	break;
	
    case FcElementPrefer:
	FcParseFamilies (parse, FcVStackPrefer);
	break;
    case FcElementAccept:
	FcParseFamilies (parse, FcVStackAccept);
	break;
    case FcElementDefault:
	FcParseFamilies (parse, FcVStackDefault);
	break;
    case FcElementFamily:
	FcParseFamily (parse);
	break;

    case FcElementTest:
	FcParseTest (parse);
	break;
    case FcElementEdit:
	FcParseEdit (parse);
	break;

    case FcElementInt:
	FcParseInt (parse);
	break;
    case FcElementDouble:
	FcParseDouble (parse);
	break;
    case FcElementString:
	FcParseString (parse, FcVStackString);
	break;
    case FcElementMatrix:
	FcParseMatrix (parse);
	break;
    case FcElementBool:
	FcParseBool (parse);
	break;
    case FcElementCharset:
/*	FcParseCharset (parse); */
	break;

    case FcElementName:
	FcParseString (parse, FcVStackField);
	break;
    case FcElementConst:
	FcParseString (parse, FcVStackConstant);
	break;
    case FcElementOr:
	FcParseExpr (parse, FcOpOr);
	break;
    case FcElementAnd:
	FcParseExpr (parse, FcOpAnd);
	break;
    case FcElementEq:
	FcParseExpr (parse, FcOpEqual);
	break;
    case FcElementNotEq:
	FcParseExpr (parse, FcOpNotEqual);
	break;
    case FcElementLess:
	FcParseExpr (parse, FcOpLess);
	break;
    case FcElementLessEq:
	FcParseExpr (parse, FcOpLessEqual);
	break;
    case FcElementMore:
	FcParseExpr (parse, FcOpMore);
	break;
    case FcElementMoreEq:
	FcParseExpr (parse, FcOpMoreEqual);
	break;
    case FcElementPlus:
	FcParseExpr (parse, FcOpPlus);
	break;
    case FcElementMinus:
	FcParseExpr (parse, FcOpMinus);
	break;
    case FcElementTimes:
	FcParseExpr (parse, FcOpTimes);
	break;
    case FcElementDivide:
	FcParseExpr (parse, FcOpDivide);
	break;
    case FcElementNot:
	FcParseExpr (parse, FcOpNot);
	break;
    case FcElementIf:
	FcParseExpr (parse, FcOpQuest);
	break;
    case FcElementUnknown:
	break;
Keith Packard's avatar
Keith Packard committed
1524
    }
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
    (void) FcPStackPop (parse);
}

static void
FcCharacterData (void *userData, const XML_Char *s, int len)
{
    FcConfigParse   *parse = userData;
    
    if (!parse->pstack)
	return;
    if (!FcStrBufData (&parse->pstack->str, (FcChar8 *) s, len))
	FcConfigError (parse, "out of memory");
}

static void
FcStartDoctypeDecl (void	    *userData,
		    const XML_Char  *doctypeName,
		    const XML_Char  *sysid,
		    const XML_Char  *pubid,
		    int		    has_internal_subset)
{
    FcConfigParse   *parse = userData;

    if (strcmp ((char *) doctypeName, "fontconfig") != 0)
	FcConfigError (parse, "invalid doctype \"%s\"", doctypeName);
}

static void
FcEndDoctypeDecl (void *userData)
{
Keith Packard's avatar
Keith Packard committed
1555 1556 1557 1558
}

FcBool
FcConfigParseAndLoad (FcConfig	    *config,
1559
		      const FcChar8 *name,
Keith Packard's avatar
Keith Packard committed
1560 1561 1562
		      FcBool	    complain)
{

1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
    XML_Parser	    p;
    FcChar8	    *filename;
    FILE	    *f;
    int		    len;
    void	    *buf;
    FcConfigParse   parse;
    FcBool	    error = FcTrue;
    
    filename = FcConfigFilename (name);
    if (!filename)
	goto bail0;
    f = fopen ((char *) filename, "r");
    free (filename);
    if (!f)
	goto bail0;
    
    p = XML_ParserCreate ("UTF-8");
    if (!p)
	goto bail1;

    if (!FcConfigInit (&parse, name, config, p))
	goto bail2;

    XML_SetUserData (p, &parse);
    
    XML_SetDoctypeDeclHandler (p, FcStartDoctypeDecl, FcEndDoctypeDecl);
    XML_SetElementHandler (p, FcStartElement, FcEndElement);
    XML_SetCharacterDataHandler (p, FcCharacterData);
	
    do {
	buf = XML_GetBuffer (p, BUFSIZ);
	if (!buf)
	    goto bail3;
	len = fread (buf, 1, BUFSIZ, f);
	if (len < 0)
	    goto bail3;
	if (!XML_ParseBuffer (p, len, len == 0))
	    goto bail3;
    } while (len != 0);
    error = parse.error;
bail3:
    FcConfigCleanup (&parse);
bail2:
    XML_ParserFree (p);
bail1:
    fclose (f);
bail0:
    if (error && complain)
Keith Packard's avatar
Keith Packard committed
1611
    {
1612 1613
	if (name)
	    FcConfigError (0, "Cannot load config file \"%s\"", name);
Keith Packard's avatar
Keith Packard committed
1614
	else
1615
	    FcConfigError (0, "Cannot load default config file");
Keith Packard's avatar
Keith Packard committed
1616 1617 1618 1619
	return FcFalse;
    }
    return FcTrue;
}