fcxml.c 50.9 KB
Newer Older
Keith Packard's avatar
Keith Packard committed
1
/*
Keith Packard's avatar
Keith Packard committed
2
 * $RCSId: xc/lib/fontconfig/src/fcxml.c,v 1.21 2002/08/22 18:53:22 keithp Exp $
Keith Packard's avatar
Keith Packard committed
3
 *
4
 * Copyright © 2002 Keith Packard
Keith Packard's avatar
Keith Packard committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * 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>
#include "fcint.h"
27
#include <dirent.h>
28

29 30 31
#ifndef HAVE_XMLPARSE_H
#define HAVE_XMLPARSE_H 0
#endif
32

33 34 35 36 37
#if HAVE_XMLPARSE_H
#include <xmlparse.h>
#else
#include <expat.h>
#endif
Keith Packard's avatar
Keith Packard committed
38

Tor Lillqvist's avatar
Tor Lillqvist committed
39 40 41 42 43 44
#ifdef _WIN32
#define STRICT
#include <windows.h>
#undef STRICT
#endif

Keith Packard's avatar
Keith Packard committed
45 46 47 48 49 50 51 52

void
FcTestDestroy (FcTest *test)
{
    if (test->next)
	FcTestDestroy (test->next);
    FcExprDestroy (test->expr);
    FcStrFree ((FcChar8 *) test->field);
53
    FcMemFree (FC_MEM_TEST, sizeof (FcTest));
Keith Packard's avatar
Keith Packard committed
54 55 56 57 58 59 60 61 62 63
    free (test);
}

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

    if (e)
    {
64
	FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
Keith Packard's avatar
Keith Packard committed
65 66 67 68 69 70 71 72 73 74 75 76 77
	e->op = FcOpInteger;
	e->u.ival = i;
    }
    return e;
}

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

    if (e)
    {
78
	FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
Keith Packard's avatar
Keith Packard committed
79 80 81 82 83 84 85
	e->op = FcOpDouble;
	e->u.dval = d;
    }
    return e;
}

FcExpr *
86
FcExprCreateString (const FcChar8 *s)
Keith Packard's avatar
Keith Packard committed
87 88 89 90 91
{
    FcExpr *e = (FcExpr *) malloc (sizeof (FcExpr));

    if (e)
    {
92
	FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
Keith Packard's avatar
Keith Packard committed
93 94 95 96 97 98 99 100 101 102 103 104 105
	e->op = FcOpString;
	e->u.sval = FcStrCopy (s);
    }
    return e;
}

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

    if (e)
    {
106
	FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
Keith Packard's avatar
Keith Packard committed
107 108 109 110 111 112 113 114 115 116 117 118 119
	e->op = FcOpMatrix;
	e->u.mval = FcMatrixCopy (m);
    }
    return e;
}

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

    if (e)
    {
120
	FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
Keith Packard's avatar
Keith Packard committed
121 122 123 124 125 126 127 128 129 130 131 132 133
	e->op = FcOpBool;
	e->u.bval = b;
    }
    return e;
}

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

    if (e)
    {
134
	FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
Keith Packard's avatar
Keith Packard committed
135 136 137 138 139 140 141 142 143 144 145 146
	e->op = FcOpNil;
    }
    return e;
}

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

    if (e)
    {
147
	FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
Keith Packard's avatar
Keith Packard committed
148
	e->op = FcOpField;
149
	e->u.field = (char *) FcStrCopy ((FcChar8 *) field);
Keith Packard's avatar
Keith Packard committed
150 151 152 153 154
    }
    return e;
}

FcExpr *
155
FcExprCreateConst (const FcChar8 *constant)
Keith Packard's avatar
Keith Packard committed
156 157 158 159 160
{
    FcExpr *e = (FcExpr *) malloc (sizeof (FcExpr));

    if (e)
    {
161
	FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
Keith Packard's avatar
Keith Packard committed
162 163 164 165 166 167 168 169 170 171 172 173 174
	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)
    {
175
	FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
Keith Packard's avatar
Keith Packard committed
176 177 178 179 180 181 182 183 184 185
	e->op = op;
	e->u.tree.left = left;
	e->u.tree.right = right;
    }
    return e;
}

void
FcExprDestroy (FcExpr *e)
{
186 187
    if (!e)
	return;
Keith Packard's avatar
Keith Packard committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
    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:
205
	FcStrFree ((FcChar8 *) e->u.field);
Keith Packard's avatar
Keith Packard committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
	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 FcOpNotEqual:
    case FcOpLess:
    case FcOpLessEqual:
    case FcOpMore:
    case FcOpMoreEqual:
225
    case FcOpContains:
226
    case FcOpListing:
227
    case FcOpNotContains:
Keith Packard's avatar
Keith Packard committed
228 229 230 231 232 233 234 235 236
    case FcOpPlus:
    case FcOpMinus:
    case FcOpTimes:
    case FcOpDivide:
    case FcOpQuest:
    case FcOpComma:
	FcExprDestroy (e->u.tree.right);
	/* fall through */
    case FcOpNot:
237 238 239 240
    case FcOpFloor:
    case FcOpCeil:
    case FcOpRound:
    case FcOpTrunc:
Keith Packard's avatar
Keith Packard committed
241 242 243 244 245 246
	FcExprDestroy (e->u.tree.left);
	break;
    case FcOpNil:
    case FcOpInvalid:
	break;
    }
247
    FcMemFree (FC_MEM_EXPR, sizeof (FcExpr));
Keith Packard's avatar
Keith Packard committed
248 249 250 251 252 253 254 255 256 257 258
    free (e);
}

void
FcEditDestroy (FcEdit *e)
{
    if (e->next)
	FcEditDestroy (e->next);
    FcStrFree ((FcChar8 *) e->field);
    if (e->expr)
	FcExprDestroy (e->expr);
259
    free (e);
Keith Packard's avatar
Keith Packard committed
260 261 262 263 264
}

char *
FcConfigSaveField (const char *field)
{
265
    return (char *) FcStrCopy ((FcChar8 *) field);
Keith Packard's avatar
Keith Packard committed
266 267
}

268 269 270 271 272 273 274 275 276 277 278
typedef enum _FcElement {
    FcElementNone,
    FcElementFontconfig,
    FcElementDir,
    FcElementCache,
    FcElementInclude,
    FcElementConfig,
    FcElementMatch,
    FcElementAlias,
	
    FcElementBlank,
279
    FcElementRescan,
280 281 282 283 284 285

    FcElementPrefer,
    FcElementAccept,
    FcElementDefault,
    FcElementFamily,

286 287 288 289
    FcElementSelectfont,
    FcElementAcceptfont,
    FcElementRejectfont,
    FcElementGlob,
290 291
    FcElementPattern,
    FcElementPatelt,
292

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
    FcElementTest,
    FcElementEdit,
    FcElementInt,
    FcElementDouble,
    FcElementString,
    FcElementMatrix,
    FcElementBool,
    FcElementCharset,
    FcElementName,
    FcElementConst,
    FcElementOr,
    FcElementAnd,
    FcElementEq,
    FcElementNotEq,
    FcElementLess,
    FcElementLessEq,
    FcElementMore,
    FcElementMoreEq,
311 312
    FcElementContains,
    FcElementNotContains,
313 314 315 316 317 318
    FcElementPlus,
    FcElementMinus,
    FcElementTimes,
    FcElementDivide,
    FcElementNot,
    FcElementIf,
319 320 321 322
    FcElementFloor,
    FcElementCeil,
    FcElementRound,
    FcElementTrunc,
323 324 325
    FcElementUnknown
} FcElement;

Patrick Lam's avatar
Patrick Lam committed
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
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])

386 387 388 389 390
static FcElement
FcElementMap (const XML_Char *name)
{

    int	    i;
Patrick Lam's avatar
Patrick Lam committed
391
    for (i = 0; i < NUM_ELEMENT_MAPS; i++)
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
	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,
411
    FcVStackGlob,
412
    FcVStackPattern,
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
    
    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;
445 446

	FcPattern	*pattern;
447 448 449 450 451 452 453 454 455 456 457 458
    } u;
} FcVStack;

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

459 460 461 462
typedef enum _FcConfigSeverity {
    FcSevereInfo, FcSevereWarning, FcSevereError
} FcConfigSeverity;

Keith Packard's avatar
Keith Packard committed
463
static void
Patrick Lam's avatar
Patrick Lam committed
464
FcConfigMessage (FcConfigParse *parse, FcConfigSeverity severe, const char *fmt, ...)
Keith Packard's avatar
Keith Packard committed
465
{
Patrick Lam's avatar
Patrick Lam committed
466
    const char	*s = "unknown";
Keith Packard's avatar
Keith Packard committed
467 468 469
    va_list	args;

    va_start (args, fmt);
470 471 472 473 474 475

    switch (severe) {
    case FcSevereInfo: s = "info"; break;
    case FcSevereWarning: s = "warning"; break;
    case FcSevereError: s = "error"; break;
    }
476 477 478
    if (parse)
    {
	if (parse->name)
479
	    fprintf (stderr, "Fontconfig %s: \"%s\", line %d: ", s,
480 481
		     parse->name, XML_GetCurrentLineNumber (parse->parser));
	else
482
	    fprintf (stderr, "Fontconfig %s: line %d: ", s,
483
		     XML_GetCurrentLineNumber (parse->parser));
484 485
	if (severe >= FcSevereError)
	    parse->error = FcTrue;
486 487
    }
    else
488
	fprintf (stderr, "Fontconfig %s: ", s);
Keith Packard's avatar
Keith Packard committed
489 490 491 492 493
    vfprintf (stderr, fmt, args);
    fprintf (stderr, "\n");
    va_end (args);
}

494

Patrick Lam's avatar
Patrick Lam committed
495
static const char *
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
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;
}

675 676
static void
FcVStackPush (FcConfigParse *parse, FcVStack *vstack)
Keith Packard's avatar
Keith Packard committed
677
{
678 679 680
    vstack->prev = parse->vstack;
    vstack->pstack = parse->pstack ? parse->pstack->prev : 0;
    parse->vstack = vstack;
Keith Packard's avatar
Keith Packard committed
681 682
}

683 684
static FcVStack *
FcVStackCreate (void)
Keith Packard's avatar
Keith Packard committed
685
{
686
    FcVStack    *new;
Keith Packard's avatar
Keith Packard committed
687

688 689 690
    new = malloc (sizeof (FcVStack));
    if (!new)
	return 0;
691
    FcMemAlloc (FC_MEM_VSTACK, sizeof (FcVStack));
692 693 694 695
    new->tag = FcVStackNone;
    new->prev = 0;
    return new;
}
Keith Packard's avatar
Keith Packard committed
696

697 698
static void
FcVStackDestroy (FcVStack *vstack)
Keith Packard's avatar
Keith Packard committed
699
{
700
    FcVStack    *prev;
Keith Packard's avatar
Keith Packard committed
701

702 703 704 705 706 707 708 709 710 711
    for (; vstack; vstack = prev)
    {
	prev = vstack->prev;
	switch (vstack->tag) {
	case FcVStackNone:
	    break;
	case FcVStackString:
	case FcVStackFamily:
	case FcVStackField:
	case FcVStackConstant:
712
	case FcVStackGlob:
713 714
	    FcStrFree (vstack->u.string);
	    break;
715 716 717
	case FcVStackPattern:
	    FcPatternDestroy (vstack->u.pattern);
	    break;
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
	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;
	}
739
	FcMemFree (FC_MEM_VSTACK, sizeof (FcVStack));
740 741
	free (vstack);
    }
Keith Packard's avatar
Keith Packard committed
742 743 744
}

static FcBool
745
FcVStackPushString (FcConfigParse *parse, FcVStackTag tag, FcChar8 *string)
Keith Packard's avatar
Keith Packard committed
746
{
747 748 749 750 751 752 753
    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
754 755 756
}

static FcBool
757
FcVStackPushInteger (FcConfigParse *parse, int integer)
Keith Packard's avatar
Keith Packard committed
758
{
759 760
    FcVStack    *vstack = FcVStackCreate ();
    if (!vstack)
Keith Packard's avatar
Keith Packard committed
761
	return FcFalse;
762 763 764 765
    vstack->u.integer = integer;
    vstack->tag = FcVStackInteger;
    FcVStackPush (parse, vstack);
    return FcTrue;
Keith Packard's avatar
Keith Packard committed
766 767 768
}

static FcBool
769
FcVStackPushDouble (FcConfigParse *parse, double _double)
Keith Packard's avatar
Keith Packard committed
770
{
771 772
    FcVStack    *vstack = FcVStackCreate ();
    if (!vstack)
Keith Packard's avatar
Keith Packard committed
773
	return FcFalse;
774 775 776 777
    vstack->u._double = _double;
    vstack->tag = FcVStackDouble;
    FcVStackPush (parse, vstack);
    return FcTrue;
Keith Packard's avatar
Keith Packard committed
778 779 780
}

static FcBool
781
FcVStackPushMatrix (FcConfigParse *parse, FcMatrix *matrix)
Keith Packard's avatar
Keith Packard committed
782
{
783 784
    FcVStack    *vstack = FcVStackCreate ();
    if (!vstack)
Keith Packard's avatar
Keith Packard committed
785
	return FcFalse;
786 787
    matrix = FcMatrixCopy (matrix);
    if (!matrix)
Keith Packard's avatar
Keith Packard committed
788
    {
789 790
	FcVStackDestroy (vstack);
	return FcFalse;
Keith Packard's avatar
Keith Packard committed
791
    }
792 793 794 795
    vstack->u.matrix = matrix;
    vstack->tag = FcVStackMatrix;
    FcVStackPush (parse, vstack);
    return FcTrue;
Keith Packard's avatar
Keith Packard committed
796 797 798
}

static FcBool
799
FcVStackPushBool (FcConfigParse *parse, FcBool bool)
Keith Packard's avatar
Keith Packard committed
800
{
801 802 803 804 805 806 807 808
    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
809

810 811 812 813 814
static FcBool
FcVStackPushTest (FcConfigParse *parse, FcTest *test)
{
    FcVStack    *vstack = FcVStackCreate ();
    if (!vstack)
Keith Packard's avatar
Keith Packard committed
815
	return FcFalse;
816 817 818
    vstack->u.test = test;
    vstack->tag = FcVStackTest;
    FcVStackPush (parse, vstack);
Keith Packard's avatar
Keith Packard committed
819 820 821 822
    return FcTrue;
}

static FcBool
823
FcVStackPushExpr (FcConfigParse *parse, FcVStackTag tag, FcExpr *expr)
Keith Packard's avatar
Keith Packard committed
824
{
825 826 827 828 829 830 831 832
    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
833

834 835 836 837 838
static FcBool
FcVStackPushEdit (FcConfigParse *parse, FcEdit *edit)
{
    FcVStack    *vstack = FcVStackCreate ();
    if (!vstack)
Keith Packard's avatar
Keith Packard committed
839
	return FcFalse;
840 841 842
    vstack->u.edit = edit;
    vstack->tag = FcVStackEdit;
    FcVStackPush (parse, vstack);
Keith Packard's avatar
Keith Packard committed
843 844 845
    return FcTrue;
}

846 847 848 849 850 851 852 853 854 855 856 857
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;
}

858 859
static FcVStack *
FcVStackFetch (FcConfigParse *parse, int off)
Keith Packard's avatar
Keith Packard committed
860
{
861
    FcVStack    *vstack;
Keith Packard's avatar
Keith Packard committed
862

863 864 865 866 867 868 869 870
    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
871
    {
872 873 874 875
	FcVStack    *vstack = parse->vstack;
	parse->vstack = vstack->prev;
	vstack->prev = 0;
	FcVStackDestroy (vstack);
Keith Packard's avatar
Keith Packard committed
876 877 878
    }
}

879 880
static FcVStack *
FcVStackPop (FcConfigParse *parse)
Keith Packard's avatar
Keith Packard committed
881
{
882
    FcVStack	*vstack = parse->vstack;
Keith Packard's avatar
Keith Packard committed
883
    
884
    if (!vstack || vstack->pstack != parse->pstack)
Keith Packard's avatar
Keith Packard committed
885
	return 0;
886 887 888 889 890 891 892 893 894 895 896 897 898 899
    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
900
    }
901
    return h;
Keith Packard's avatar
Keith Packard committed
902 903
}

904 905
static FcChar8 **
FcConfigSaveAttr (const XML_Char **attr)
Keith Packard's avatar
Keith Packard committed
906
{
907 908 909 910 911
    int		n;
    int		slen;
    int		i;
    FcChar8	**new;
    FcChar8	*s;
Keith Packard's avatar
Keith Packard committed
912

913 914 915 916 917 918 919 920 921
    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;
922
    FcMemAlloc (FC_MEM_ATTR, 1);    /* size is too expensive */
923 924
    s = (FcChar8 *) (new + (i + 1));
    for (i = 0; attr[i]; i++)
Keith Packard's avatar
Keith Packard committed
925
    {
926 927 928
	new[i] = s;
	strcpy ((char *) s, (char *) attr[i]);
	s += strlen ((char *) s) + 1;
Keith Packard's avatar
Keith Packard committed
929
    }
930 931 932
    new[i] = 0;
    return new;
}
Keith Packard's avatar
Keith Packard committed
933

934 935 936 937 938 939 940
static FcBool
FcPStackPush (FcConfigParse *parse, FcElement element, const XML_Char **attr)
{
    FcPStack   *new = malloc (sizeof (FcPStack));

    if (!new)
	return FcFalse;
941
    FcMemAlloc (FC_MEM_PSTACK, sizeof (FcPStack));
942 943 944
    new->prev = parse->pstack;
    new->element = element;
    if (attr)
Keith Packard's avatar
Keith Packard committed
945
    {
946 947
	new->attr = FcConfigSaveAttr (attr);
	if (!new->attr)
948
	    FcConfigMessage (parse, FcSevereError, "out of memory");
Keith Packard's avatar
Keith Packard committed
949
    }
950 951 952 953 954 955
    else
	new->attr = 0;
    FcStrBufInit (&new->str, 0, 0);
    parse->pstack = new;
    return FcTrue;
}
Keith Packard's avatar
Keith Packard committed
956

957 958 959 960 961 962
static FcBool
FcPStackPop (FcConfigParse *parse)
{
    FcPStack   *old;
    
    if (!parse->pstack) 
Keith Packard's avatar
Keith Packard committed
963
    {
964
	FcConfigMessage (parse, FcSevereError, "mismatching element");
965
	return FcFalse;
Keith Packard's avatar
Keith Packard committed
966
    }
967 968 969 970 971
    FcVStackClear (parse);
    old = parse->pstack;
    parse->pstack = old->prev;
    FcStrBufDestroy (&old->str);
    if (old->attr)
972 973
    {
	FcMemFree (FC_MEM_ATTR, 1); /* size is to expensive */
974
	free (old->attr);
975 976
    }
    FcMemFree (FC_MEM_PSTACK, sizeof (FcPStack));
977 978
    free (old);
    return FcTrue;
Keith Packard's avatar
Keith Packard committed
979 980
}

981 982
static FcBool
FcConfigInit (FcConfigParse *parse, const FcChar8 *name, FcConfig *config, XML_Parser parser)
Keith Packard's avatar
Keith Packard committed
983
{
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
    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 *
Patrick Lam's avatar
Patrick Lam committed
1001
FcConfigGetAttribute (FcConfigParse *parse, const char *attr)
1002 1003 1004
{
    FcChar8 **attrs;
    if (!parse->pstack)
Keith Packard's avatar
Keith Packard committed
1005 1006
	return 0;

1007 1008
    attrs = parse->pstack->attr;
    while (*attrs)
Keith Packard's avatar
Keith Packard committed
1009
    {
1010 1011 1012
	if (!strcmp ((char *) *attrs, attr))
	    return attrs[1];
	attrs += 2;
Keith Packard's avatar
Keith Packard committed
1013 1014 1015 1016
    }
    return 0;
}

1017 1018
static void
FcStartElement(void *userData, const XML_Char *name, const XML_Char **attr)
Keith Packard's avatar
Keith Packard committed
1019
{
1020 1021 1022 1023 1024
    FcConfigParse   *parse = userData;
    FcElement	    element;
    
    element = FcElementMap (name);
    if (element == FcElementUnknown)
1025
	FcConfigMessage (parse, FcSevereWarning, "unknown element \"%s\"", name);
1026 1027
    
    if (!FcPStackPush (parse, element, attr))
Keith Packard's avatar
Keith Packard committed
1028
    {
1029
	FcConfigMessage (parse, FcSevereError, "out of memory");
1030
	return;
Keith Packard's avatar
Keith Packard committed
1031
    }
1032 1033
    return;
}
Keith Packard's avatar
Keith Packard committed
1034

1035 1036 1037 1038 1039
static void
FcParseBlank (FcConfigParse *parse)
{
    int	    n = FcVStackElements (parse);
    while (n-- > 0)
Keith Packard's avatar
Keith Packard committed
1040
    {
1041 1042
	FcVStack    *v = FcVStackFetch (parse, n);
	if (v->tag != FcVStackInteger)
1043
	    FcConfigMessage (parse, FcSevereError, "non-integer blank");
1044
	else
Keith Packard's avatar
Keith Packard committed
1045
	{
1046
	    if (!parse->config->blanks)
Keith Packard's avatar
Keith Packard committed
1047
	    {
1048 1049 1050
		parse->config->blanks = FcBlanksCreate ();
		if (!parse->config->blanks)
		{
1051
		    FcConfigMessage (parse, FcSevereError, "out of memory");
1052 1053
		    break;
		}
Keith Packard's avatar
Keith Packard committed
1054
	    }
1055
	    if (!FcBlanksAdd (parse->config->blanks, v->u.integer))
Keith Packard's avatar
Keith Packard committed
1056
	    {
1057
		FcConfigMessage (parse, FcSevereError, "out of memory");
1058
		break;
Keith Packard's avatar
Keith Packard committed
1059 1060 1061
	    }
	}
    }
1062
}
Keith Packard's avatar
Keith Packard committed
1063

1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
static void
FcParseRescan (FcConfigParse *parse)
{
    int	    n = FcVStackElements (parse);
    while (n-- > 0)
    {
	FcVStack    *v = FcVStackFetch (parse, n);
	if (v->tag != FcVStackInteger)
	    FcConfigMessage (parse, FcSevereWarning, "non-integer rescan");
	else
	    parse->config->rescanInterval = v->u.integer;
    }
}

1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
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
1088
    {
1089
	FcConfigMessage (parse, FcSevereError, "out of memory");
1090
	return;
Keith Packard's avatar
Keith Packard committed
1091
    }
1092 1093 1094
    end = 0;
    l = (int) strtol ((char *) s, (char **)&end, 0);
    if (end != s + strlen ((char *) s))
1095
	FcConfigMessage (parse, FcSevereError, "\"%s\": not a valid integer", s);
1096 1097 1098
    else
	FcVStackPushInteger (parse, l);
    FcStrFree (s);
Keith Packard's avatar
Keith Packard committed
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
/*
 * idea copied from glib g_ascii_strtod with 
 * permission of the author (Alexander Larsson) 
 */

#include <locale.h>

static double 
FcStrtod (char *s, char **end)
{
    struct lconv    *locale_data;
    char	    *dot;
    double	    v;

    /*
     * Have to swap the decimal point to match the current locale
     * if that locale doesn't use 0x2e
     */
    if ((dot = strchr (s, 0x2e)) &&
	(locale_data = localeconv ()) &&
	(locale_data->decimal_point[0] != 0x2e ||
	 locale_data->decimal_point[1] != 0))
    {
	char	buf[128];
	int	slen = strlen (s);
	int	dlen = strlen (locale_data->decimal_point);
	
Patrick Lam's avatar
Patrick Lam committed
1128
	if (slen + dlen > (int) sizeof (buf))
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
	{
	    if (end)
		*end = s;
	    v = 0;
	}
	else
	{
	    char	*buf_end;
	    /* mantissa */
	    strncpy (buf, s, dot - s);
	    /* decimal point */
	    strcpy (buf + (dot - s), locale_data->decimal_point);
	    /* rest of number */
	    strcpy (buf + (dot - s) + dlen, dot + 1);
	    buf_end = 0;
	    v = strtod (buf, &buf_end);
1145 1146 1147 1148 1149
	    if (buf_end) {
		buf_end = s + (buf_end - buf);
		if (buf_end > dot)
		    buf_end -= dlen - 1;
	    }
1150 1151 1152 1153 1154 1155 1156 1157 1158
	    if (end)
		*end = buf_end;
	}
    }
    else
	v = strtod (s, end);
    return v;
}

1159 1160
static void
FcParseDouble (FcConfigParse *parse)
Keith Packard's avatar
Keith Packard committed
1161
{
1162 1163
    FcChar8 *s, *end;
    double  d;
Keith Packard's avatar
Keith Packard committed
1164
    
1165 1166 1167 1168
    if (!parse->pstack)
	return;
    s = FcStrBufDone (&parse->pstack->str);
    if (!s)
Keith Packard's avatar
Keith Packard committed
1169
    {
1170
	FcConfigMessage (parse, FcSevereError, "out of memory");
1171
	return;
Keith Packard's avatar
Keith Packard committed
1172
    }
1173
    end = 0;
1174
    d = FcStrtod ((char *) s, (char **)&end);
1175
    if (end != s + strlen ((char *) s))
1176
	FcConfigMessage (parse, FcSevereError, "\"%s\": not a valid double", s);
Keith Packard's avatar
Keith Packard committed
1177
    else
1178 1179 1180
	FcVStackPushDouble (parse, d);
    FcStrFree (s);
}
Keith Packard's avatar
Keith Packard committed
1181

1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
static void
FcParseString (FcConfigParse *parse, FcVStackTag tag)
{
    FcChar8 *s;
    
    if (!parse->pstack)
	return;
    s = FcStrBufDone (&parse->pstack->str);
    if (!s)
    {
1192
	FcConfigMessage (parse, FcSevereError, "out of memory");
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
	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)))
    {
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
	double	v;
	switch (vstack->tag) {
	case FcVStackInteger:
	    v = vstack->u.integer;
	    break;
	case FcVStackDouble:
	    v = vstack->u._double;
	    break;
	default:
	    FcConfigMessage (parse, FcSevereError, "non-double matrix element");
	    v = 1.0;
	    break;
	}
	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;
1227
	}
1228
	FcVStackDestroy (vstack);
1229
	matrix_state--;
1230 1231
    }
    if (matrix_state != m_done)
1232
	FcConfigMessage (parse, FcSevereError, "wrong number of matrix elements");
1233 1234
    else
	FcVStackPushMatrix (parse, &m);
Keith Packard's avatar
Keith Packard committed
1235 1236 1237
}

static FcBool
1238
FcConfigLexBool (FcConfigParse *parse, const FcChar8 *bool)
1239
{
1240 1241 1242 1243 1244 1245
    FcBool  result = FcFalse;

    if (!FcNameBool (bool, &result))
	FcConfigMessage (parse, FcSevereWarning, "\"%s\" is not known boolean",
			 bool);
    return result;
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
}

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

    if (!parse->pstack)
	return;
    s = FcStrBufDone (&parse->pstack->str);
    if (!s)
    {
1258
	FcConfigMessage (parse, FcSevereError, "out of memory");
1259 1260
	return;
    }
1261
    FcVStackPushBool (parse, FcConfigLexBool (parse, s));
1262 1263 1264 1265 1266
    FcStrFree (s);
}

static void
FcParseFamilies (FcConfigParse *parse, FcVStackTag tag)
Keith Packard's avatar
Keith Packard committed
1267
{
1268 1269 1270 1271 1272 1273 1274
    FcVStack	*vstack;
    FcExpr	*left, *expr = 0, *new;

    while ((vstack = FcVStackPop (parse)))
    {
	if (vstack->tag != FcVStackFamily)
	{
1275 1276 1277
	    FcConfigMessage (parse, FcSevereWarning, "non-family");
	    FcVStackDestroy (vstack);
	    continue;
1278 1279 1280 1281 1282 1283 1284 1285 1286
	}
	left = vstack->u.expr;
	vstack->tag = FcVStackNone;
	FcVStackDestroy (vstack);
	if (expr)
	{
	    new = FcExprCreateOp (left, FcOpComma, expr);
	    if (!new)
	    {
1287
		FcConfigMessage (parse, FcSevereError, "out of memory");
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
		FcExprDestroy (left);
		FcExprDestroy (expr);
		break;
	    }
	    expr = new;
	}
	else
	    expr = left;
    }
    if (expr)
    {
	if (!FcVStackPushExpr (parse, tag, expr))
	{
1301
	    FcConfigMessage (parse, FcSevereError, "out of memory");
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
	    if (expr)
		FcExprDestroy (expr);
	}
    }
}

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

    if (!parse->pstack)
	return;
    s = FcStrBufDone (&parse->pstack->str);
    if (!s)
    {
1319
	FcConfigMessage (parse, FcSevereError, "out of memory");
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
	return;
    }
    expr = FcExprCreateString (s);
    FcStrFree (s);
    if (expr)
	FcVStackPushExpr (parse, FcVStackFamily, expr);
}

static void
FcParseAlias (FcConfigParse *parse)
{
1331
    FcExpr	*family = 0, *accept = 0, *prefer = 0, *def = 0, *new = 0;
Keith Packard's avatar
Keith Packard committed
1332
    FcEdit	*edit = 0, *next;
1333
    FcVStack	*vstack;
Keith Packard's avatar
Keith Packard committed
1334 1335
    FcTest	*test;

1336
    while ((vstack = FcVStackPop (parse))) 
Keith Packard's avatar
Keith Packard committed
1337
    {
1338 1339 1340
	switch (vstack->tag) {
	case FcVStackFamily:
	    if (family)
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
	    {
		new = FcExprCreateOp (vstack->u.expr, FcOpComma, family);
		if (!new)
		    FcConfigMessage (parse, FcSevereError, "out of memory");
		else
		    family = new;
	    }
	    else
		new = vstack->u.expr;
	    if (new)
	    {
		family = new;
		vstack->tag = FcVStackNone;
	    }
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
	    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:
1375
	    FcConfigMessage (parse, FcSevereWarning, "bad alias");
1376 1377 1378
	    break;
	}
	FcVStackDestroy (vstack);
Keith Packard's avatar
Keith Packard committed
1379
    }
1380
    if (!family)
1381
    {
1382 1383 1384 1385 1386 1387 1388
	FcConfigMessage (parse, FcSevereError, "missing family in alias");
	if (prefer)
	    FcExprDestroy (prefer);
	if (accept)
	    FcExprDestroy (accept);
	if (def)
	    FcExprDestroy (def);
1389 1390
	return;
    }
Keith Packard's avatar
Keith Packard committed
1391 1392
    if (prefer)
    {
1393 1394
	edit = FcEditCreate (parse, 
			     FcConfigSaveField ("family"),
Keith Packard's avatar
Keith Packard committed
1395
			     FcOpPrepend,
1396 1397
			     prefer,
			     FcValueBindingWeak);
Keith Packard's avatar
Keith Packard committed
1398 1399
	if (edit)
	    edit->next = 0;
1400 1401
	else
	    FcExprDestroy (prefer);
Keith Packard's avatar
Keith Packard committed
1402 1403 1404 1405
    }
    if (accept)
    {
	next = edit;
1406 1407
	edit = FcEditCreate (parse,
			     FcConfigSaveField ("family"),
Keith Packard's avatar
Keith Packard committed
1408
			     FcOpAppend,
1409 1410
			     accept,
			     FcValueBindingWeak);
Keith Packard's avatar
Keith Packard committed
1411 1412
	if (edit)
	    edit->next = next;