fcxml.c 47.2 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
 *
Keith Packard's avatar
Keith Packard committed
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
FcTest *
46 47 48 49 50
FcTestCreate (FcMatchKind   kind, 
	      FcQual	    qual,
	      const FcChar8 *field,
	      FcOp	    compare,
	      FcExpr	    *expr)
Keith Packard's avatar
Keith Packard committed
51
{
52
    FcTest	*test = (FcTest *) malloc (sizeof (FcTest));
Keith Packard's avatar
Keith Packard committed
53 54 55

    if (test)
    {
56
	FcMemAlloc (FC_MEM_TEST, sizeof (FcTest));
Keith Packard's avatar
Keith Packard committed
57
	test->next = 0;
58
	test->kind = kind;
Keith Packard's avatar
Keith Packard committed
59
	test->qual = qual;
60
	test->field = (char *) FcStrCopy (field);
Keith Packard's avatar
Keith Packard committed
61 62 63 64 65 66 67 68 69 70 71 72 73
	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);
74
    FcMemFree (FC_MEM_TEST, sizeof (FcTest));
Keith Packard's avatar
Keith Packard committed
75 76 77 78 79 80 81 82 83 84
    free (test);
}

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

    if (e)
    {
85
	FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
Keith Packard's avatar
Keith Packard committed
86 87 88 89 90 91 92 93 94 95 96 97 98
	e->op = FcOpInteger;
	e->u.ival = i;
    }
    return e;
}

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

    if (e)
    {
99
	FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
Keith Packard's avatar
Keith Packard committed
100 101 102 103 104 105 106
	e->op = FcOpDouble;
	e->u.dval = d;
    }
    return e;
}

FcExpr *
107
FcExprCreateString (const FcChar8 *s)
Keith Packard's avatar
Keith Packard committed
108 109 110 111 112
{
    FcExpr *e = (FcExpr *) malloc (sizeof (FcExpr));

    if (e)
    {
113
	FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
Keith Packard's avatar
Keith Packard committed
114 115 116 117 118 119 120 121 122 123 124 125 126
	e->op = FcOpString;
	e->u.sval = FcStrCopy (s);
    }
    return e;
}

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

    if (e)
    {
127
	FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
Keith Packard's avatar
Keith Packard committed
128 129 130 131 132 133 134 135 136 137 138 139 140
	e->op = FcOpMatrix;
	e->u.mval = FcMatrixCopy (m);
    }
    return e;
}

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

    if (e)
    {
141
	FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
Keith Packard's avatar
Keith Packard committed
142 143 144 145 146 147 148 149 150 151 152 153 154
	e->op = FcOpBool;
	e->u.bval = b;
    }
    return e;
}

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

    if (e)
    {
155
	FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
Keith Packard's avatar
Keith Packard committed
156 157 158 159 160 161 162 163 164 165 166 167
	e->op = FcOpNil;
    }
    return e;
}

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

    if (e)
    {
168
	FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
Keith Packard's avatar
Keith Packard committed
169
	e->op = FcOpField;
170
	e->u.field = (char *) FcStrCopy ((FcChar8 *) field);
Keith Packard's avatar
Keith Packard committed
171 172 173 174 175
    }
    return e;
}

FcExpr *
176
FcExprCreateConst (const FcChar8 *constant)
Keith Packard's avatar
Keith Packard committed
177 178 179 180 181
{
    FcExpr *e = (FcExpr *) malloc (sizeof (FcExpr));

    if (e)
    {
182
	FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
Keith Packard's avatar
Keith Packard committed
183 184 185 186 187 188 189 190 191 192 193 194 195
	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)
    {
196
	FcMemAlloc (FC_MEM_EXPR, sizeof (FcExpr));
Keith Packard's avatar
Keith Packard committed
197 198 199 200 201 202 203 204 205 206
	e->op = op;
	e->u.tree.left = left;
	e->u.tree.right = right;
    }
    return e;
}

void
FcExprDestroy (FcExpr *e)
{
207 208
    if (!e)
	return;
Keith Packard's avatar
Keith Packard committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    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:
226
	FcStrFree ((FcChar8 *) e->u.field);
Keith Packard's avatar
Keith Packard committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
	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:
246
    case FcOpContains:
247
    case FcOpListing:
248
    case FcOpNotContains:
Keith Packard's avatar
Keith Packard committed
249 250 251 252 253 254 255 256 257
    case FcOpPlus:
    case FcOpMinus:
    case FcOpTimes:
    case FcOpDivide:
    case FcOpQuest:
    case FcOpComma:
	FcExprDestroy (e->u.tree.right);
	/* fall through */
    case FcOpNot:
258 259 260 261
    case FcOpFloor:
    case FcOpCeil:
    case FcOpRound:
    case FcOpTrunc:
Keith Packard's avatar
Keith Packard committed
262 263 264 265 266 267
	FcExprDestroy (e->u.tree.left);
	break;
    case FcOpNil:
    case FcOpInvalid:
	break;
    }
268
    FcMemFree (FC_MEM_EXPR, sizeof (FcExpr));
Keith Packard's avatar
Keith Packard committed
269 270 271 272
    free (e);
}

FcEdit *
273
FcEditCreate (const char *field, FcOp op, FcExpr *expr, FcValueBinding binding)
Keith Packard's avatar
Keith Packard committed
274 275 276 277 278 279 280 281 282
{
    FcEdit *e = (FcEdit *) malloc (sizeof (FcEdit));

    if (e)
    {
	e->next = 0;
	e->field = field;   /* already saved in grammar */
	e->op = op;
	e->expr = expr;
283
	e->binding = binding;
Keith Packard's avatar
Keith Packard committed
284 285 286 287 288 289 290 291 292 293 294 295
    }
    return e;
}

void
FcEditDestroy (FcEdit *e)
{
    if (e->next)
	FcEditDestroy (e->next);
    FcStrFree ((FcChar8 *) e->field);
    if (e->expr)
	FcExprDestroy (e->expr);
296
    free (e);
Keith Packard's avatar
Keith Packard committed
297 298 299 300 301
}

char *
FcConfigSaveField (const char *field)
{
302
    return (char *) FcStrCopy ((FcChar8 *) field);
Keith Packard's avatar
Keith Packard committed
303 304
}

305 306 307 308 309 310 311 312 313 314 315
typedef enum _FcElement {
    FcElementNone,
    FcElementFontconfig,
    FcElementDir,
    FcElementCache,
    FcElementInclude,
    FcElementConfig,
    FcElementMatch,
    FcElementAlias,
	
    FcElementBlank,
316
    FcElementRescan,
317 318 319 320 321 322

    FcElementPrefer,
    FcElementAccept,
    FcElementDefault,
    FcElementFamily,

323 324 325 326
    FcElementSelectfont,
    FcElementAcceptfont,
    FcElementRejectfont,
    FcElementGlob,
327 328
    FcElementPattern,
    FcElementPatelt,
329

330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
    FcElementTest,
    FcElementEdit,
    FcElementInt,
    FcElementDouble,
    FcElementString,
    FcElementMatrix,
    FcElementBool,
    FcElementCharset,
    FcElementName,
    FcElementConst,
    FcElementOr,
    FcElementAnd,
    FcElementEq,
    FcElementNotEq,
    FcElementLess,
    FcElementLessEq,
    FcElementMore,
    FcElementMoreEq,
348 349
    FcElementContains,
    FcElementNotContains,
350 351 352 353 354 355
    FcElementPlus,
    FcElementMinus,
    FcElementTimes,
    FcElementDivide,
    FcElementNot,
    FcElementIf,
356 357 358 359
    FcElementFloor,
    FcElementCeil,
    FcElementRound,
    FcElementTrunc,
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
    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 },
379
	{ "rescan",	FcElementRescan },
380 381 382 383 384 385

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

386 387 388 389
	{ "selectfont",	FcElementSelectfont },
	{ "acceptfont",	FcElementAcceptfont },
	{ "rejectfont",	FcElementRejectfont },
	{ "glob",	FcElementGlob },
390 391
	{ "pattern",	FcElementPattern },
	{ "patelt",	FcElementPatelt },
392

393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
	{ "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 },
411 412
	{ "contains",	FcElementContains },
	{ "not_contains",FcElementNotContains },
413 414 415 416 417 418
	{ "plus",	FcElementPlus },
	{ "minus",	FcElementMinus },
	{ "times",	FcElementTimes },
	{ "divide",	FcElementDivide },
	{ "not",	FcElementNot },
	{ "if",		FcElementIf },
419 420 421 422
	{ "floor",	FcElementFloor },
	{ "ceil",	FcElementCeil },
	{ "round",	FcElementRound },
	{ "trunc",	FcElementTrunc },
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
	
	{ 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,
448
    FcVStackGlob,
449
    FcVStackPattern,
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
    
    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;
482 483

	FcPattern	*pattern;
484 485 486 487 488 489 490 491 492 493 494 495
    } u;
} FcVStack;

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

496 497 498 499
typedef enum _FcConfigSeverity {
    FcSevereInfo, FcSevereWarning, FcSevereError
} FcConfigSeverity;

Keith Packard's avatar
Keith Packard committed
500
static void
501
FcConfigMessage (FcConfigParse *parse, FcConfigSeverity severe, char *fmt, ...)
Keith Packard's avatar
Keith Packard committed
502
{
503
    char	*s = "unknown";
Keith Packard's avatar
Keith Packard committed
504 505 506
    va_list	args;

    va_start (args, fmt);
507 508 509 510 511 512

    switch (severe) {
    case FcSevereInfo: s = "info"; break;
    case FcSevereWarning: s = "warning"; break;
    case FcSevereError: s = "error"; break;
    }
513 514 515
    if (parse)
    {
	if (parse->name)
516
	    fprintf (stderr, "Fontconfig %s: \"%s\", line %d: ", s,
517 518
		     parse->name, XML_GetCurrentLineNumber (parse->parser));
	else
519
	    fprintf (stderr, "Fontconfig %s: line %d: ", s,
520
		     XML_GetCurrentLineNumber (parse->parser));
521 522
	if (severe >= FcSevereError)
	    parse->error = FcTrue;
523 524
    }
    else
525
	fprintf (stderr, "Fontconfig %s: ", s);
Keith Packard's avatar
Keith Packard committed
526 527 528 529 530
    vfprintf (stderr, fmt, args);
    fprintf (stderr, "\n");
    va_end (args);
}

531 532
static void
FcVStackPush (FcConfigParse *parse, FcVStack *vstack)
Keith Packard's avatar
Keith Packard committed
533
{
534 535 536
    vstack->prev = parse->vstack;
    vstack->pstack = parse->pstack ? parse->pstack->prev : 0;
    parse->vstack = vstack;
Keith Packard's avatar
Keith Packard committed
537 538
}

539 540
static FcVStack *
FcVStackCreate (void)
Keith Packard's avatar
Keith Packard committed
541
{
542
    FcVStack    *new;
Keith Packard's avatar
Keith Packard committed
543

544 545 546
    new = malloc (sizeof (FcVStack));
    if (!new)
	return 0;
547
    FcMemAlloc (FC_MEM_VSTACK, sizeof (FcVStack));
548 549 550 551
    new->tag = FcVStackNone;
    new->prev = 0;
    return new;
}
Keith Packard's avatar
Keith Packard committed
552

553 554
static void
FcVStackDestroy (FcVStack *vstack)
Keith Packard's avatar
Keith Packard committed
555
{
556
    FcVStack    *prev;
Keith Packard's avatar
Keith Packard committed
557

558 559 560 561 562 563 564 565 566 567
    for (; vstack; vstack = prev)
    {
	prev = vstack->prev;
	switch (vstack->tag) {
	case FcVStackNone:
	    break;
	case FcVStackString:
	case FcVStackFamily:
	case FcVStackField:
	case FcVStackConstant:
568
	case FcVStackGlob:
569 570
	    FcStrFree (vstack->u.string);
	    break;
571 572 573
	case FcVStackPattern:
	    FcPatternDestroy (vstack->u.pattern);
	    break;
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
	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;
	}
595
	FcMemFree (FC_MEM_VSTACK, sizeof (FcVStack));
596 597
	free (vstack);
    }
Keith Packard's avatar
Keith Packard committed
598 599 600
}

static FcBool
601
FcVStackPushString (FcConfigParse *parse, FcVStackTag tag, FcChar8 *string)
Keith Packard's avatar
Keith Packard committed
602
{
603 604 605 606 607 608 609
    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
610 611 612
}

static FcBool
613
FcVStackPushInteger (FcConfigParse *parse, int integer)
Keith Packard's avatar
Keith Packard committed
614
{
615 616
    FcVStack    *vstack = FcVStackCreate ();
    if (!vstack)
Keith Packard's avatar
Keith Packard committed
617
	return FcFalse;
618 619 620 621
    vstack->u.integer = integer;
    vstack->tag = FcVStackInteger;
    FcVStackPush (parse, vstack);
    return FcTrue;
Keith Packard's avatar
Keith Packard committed
622 623 624
}

static FcBool
625
FcVStackPushDouble (FcConfigParse *parse, double _double)
Keith Packard's avatar
Keith Packard committed
626
{
627 628
    FcVStack    *vstack = FcVStackCreate ();
    if (!vstack)
Keith Packard's avatar
Keith Packard committed
629
	return FcFalse;
630 631 632 633
    vstack->u._double = _double;
    vstack->tag = FcVStackDouble;
    FcVStackPush (parse, vstack);
    return FcTrue;
Keith Packard's avatar
Keith Packard committed
634 635 636
}

static FcBool
637
FcVStackPushMatrix (FcConfigParse *parse, FcMatrix *matrix)
Keith Packard's avatar
Keith Packard committed
638
{
639 640
    FcVStack    *vstack = FcVStackCreate ();
    if (!vstack)
Keith Packard's avatar
Keith Packard committed
641
	return FcFalse;
642 643
    matrix = FcMatrixCopy (matrix);
    if (!matrix)
Keith Packard's avatar
Keith Packard committed
644
    {
645 646
	FcVStackDestroy (vstack);
	return FcFalse;
Keith Packard's avatar
Keith Packard committed
647
    }
648 649 650 651
    vstack->u.matrix = matrix;
    vstack->tag = FcVStackMatrix;
    FcVStackPush (parse, vstack);
    return FcTrue;
Keith Packard's avatar
Keith Packard committed
652 653 654
}

static FcBool
655
FcVStackPushBool (FcConfigParse *parse, FcBool bool)
Keith Packard's avatar
Keith Packard committed
656
{
657 658 659 660 661 662 663 664
    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
665

666 667 668 669 670
static FcBool
FcVStackPushTest (FcConfigParse *parse, FcTest *test)
{
    FcVStack    *vstack = FcVStackCreate ();
    if (!vstack)
Keith Packard's avatar
Keith Packard committed
671
	return FcFalse;
672 673 674
    vstack->u.test = test;
    vstack->tag = FcVStackTest;
    FcVStackPush (parse, vstack);
Keith Packard's avatar
Keith Packard committed
675 676 677 678
    return FcTrue;
}

static FcBool
679
FcVStackPushExpr (FcConfigParse *parse, FcVStackTag tag, FcExpr *expr)
Keith Packard's avatar
Keith Packard committed
680
{
681 682 683 684 685 686 687 688
    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
689

690 691 692 693 694
static FcBool
FcVStackPushEdit (FcConfigParse *parse, FcEdit *edit)
{
    FcVStack    *vstack = FcVStackCreate ();
    if (!vstack)
Keith Packard's avatar
Keith Packard committed
695
	return FcFalse;
696 697 698
    vstack->u.edit = edit;
    vstack->tag = FcVStackEdit;
    FcVStackPush (parse, vstack);
Keith Packard's avatar
Keith Packard committed
699 700 701
    return FcTrue;
}

702 703 704 705 706 707 708 709 710 711 712 713
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;
}

714 715
static FcVStack *
FcVStackFetch (FcConfigParse *parse, int off)
Keith Packard's avatar
Keith Packard committed
716
{
717
    FcVStack    *vstack;
Keith Packard's avatar
Keith Packard committed
718

719 720 721 722 723 724 725 726
    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
727
    {
728 729 730 731
	FcVStack    *vstack = parse->vstack;
	parse->vstack = vstack->prev;
	vstack->prev = 0;
	FcVStackDestroy (vstack);
Keith Packard's avatar
Keith Packard committed
732 733 734
    }
}

735 736
static FcVStack *
FcVStackPop (FcConfigParse *parse)
Keith Packard's avatar
Keith Packard committed
737
{
738
    FcVStack	*vstack = parse->vstack;
Keith Packard's avatar
Keith Packard committed
739
    
740
    if (!vstack || vstack->pstack != parse->pstack)
Keith Packard's avatar
Keith Packard committed
741
	return 0;
742 743 744 745 746 747 748 749 750 751 752 753 754 755
    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
756
    }
757
    return h;
Keith Packard's avatar
Keith Packard committed
758 759
}

760 761
static FcChar8 **
FcConfigSaveAttr (const XML_Char **attr)
Keith Packard's avatar
Keith Packard committed
762
{
763 764 765 766 767
    int		n;
    int		slen;
    int		i;
    FcChar8	**new;
    FcChar8	*s;
Keith Packard's avatar
Keith Packard committed
768

769 770 771 772 773 774 775 776 777
    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;
778
    FcMemAlloc (FC_MEM_ATTR, 1);    /* size is too expensive */
779 780
    s = (FcChar8 *) (new + (i + 1));
    for (i = 0; attr[i]; i++)
Keith Packard's avatar
Keith Packard committed
781
    {
782 783 784
	new[i] = s;
	strcpy ((char *) s, (char *) attr[i]);
	s += strlen ((char *) s) + 1;
Keith Packard's avatar
Keith Packard committed
785
    }
786 787 788
    new[i] = 0;
    return new;
}
Keith Packard's avatar
Keith Packard committed
789

790 791 792 793 794 795 796
static FcBool
FcPStackPush (FcConfigParse *parse, FcElement element, const XML_Char **attr)
{
    FcPStack   *new = malloc (sizeof (FcPStack));

    if (!new)
	return FcFalse;
797
    FcMemAlloc (FC_MEM_PSTACK, sizeof (FcPStack));
798 799 800
    new->prev = parse->pstack;
    new->element = element;
    if (attr)
Keith Packard's avatar
Keith Packard committed
801
    {
802 803
	new->attr = FcConfigSaveAttr (attr);
	if (!new->attr)
804
	    FcConfigMessage (parse, FcSevereError, "out of memory");
Keith Packard's avatar
Keith Packard committed
805
    }
806 807 808 809 810 811
    else
	new->attr = 0;
    FcStrBufInit (&new->str, 0, 0);
    parse->pstack = new;
    return FcTrue;
}
Keith Packard's avatar
Keith Packard committed
812

813 814 815 816 817 818
static FcBool
FcPStackPop (FcConfigParse *parse)
{
    FcPStack   *old;
    
    if (!parse->pstack) 
Keith Packard's avatar
Keith Packard committed
819
    {
820
	FcConfigMessage (parse, FcSevereError, "mismatching element");
821
	return FcFalse;
Keith Packard's avatar
Keith Packard committed
822
    }
823 824 825 826 827
    FcVStackClear (parse);
    old = parse->pstack;
    parse->pstack = old->prev;
    FcStrBufDestroy (&old->str);
    if (old->attr)
828 829
    {
	FcMemFree (FC_MEM_ATTR, 1); /* size is to expensive */
830
	free (old->attr);
831 832
    }
    FcMemFree (FC_MEM_PSTACK, sizeof (FcPStack));
833 834
    free (old);
    return FcTrue;
Keith Packard's avatar
Keith Packard committed
835 836
}

837 838
static FcBool
FcConfigInit (FcConfigParse *parse, const FcChar8 *name, FcConfig *config, XML_Parser parser)
Keith Packard's avatar
Keith Packard committed
839
{
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
    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
861 862
	return 0;

863 864
    attrs = parse->pstack->attr;
    while (*attrs)
Keith Packard's avatar
Keith Packard committed
865
    {
866 867 868
	if (!strcmp ((char *) *attrs, attr))
	    return attrs[1];
	attrs += 2;
Keith Packard's avatar
Keith Packard committed
869 870 871 872
    }
    return 0;
}

873 874
static void
FcStartElement(void *userData, const XML_Char *name, const XML_Char **attr)
Keith Packard's avatar
Keith Packard committed
875
{
876 877 878 879 880
    FcConfigParse   *parse = userData;
    FcElement	    element;
    
    element = FcElementMap (name);
    if (element == FcElementUnknown)
881
	FcConfigMessage (parse, FcSevereWarning, "unknown element \"%s\"", name);
882 883
    
    if (!FcPStackPush (parse, element, attr))
Keith Packard's avatar
Keith Packard committed
884
    {
885
	FcConfigMessage (parse, FcSevereError, "out of memory");
886
	return;
Keith Packard's avatar
Keith Packard committed
887
    }
888 889
    return;
}
Keith Packard's avatar
Keith Packard committed
890

891 892 893 894 895
static void
FcParseBlank (FcConfigParse *parse)
{
    int	    n = FcVStackElements (parse);
    while (n-- > 0)
Keith Packard's avatar
Keith Packard committed
896
    {
897 898
	FcVStack    *v = FcVStackFetch (parse, n);
	if (v->tag != FcVStackInteger)
899
	    FcConfigMessage (parse, FcSevereError, "non-integer blank");
900
	else
Keith Packard's avatar
Keith Packard committed
901
	{
902
	    if (!parse->config->blanks)
Keith Packard's avatar
Keith Packard committed
903
	    {
904 905 906
		parse->config->blanks = FcBlanksCreate ();
		if (!parse->config->blanks)
		{
907
		    FcConfigMessage (parse, FcSevereError, "out of memory");
908 909
		    break;
		}
Keith Packard's avatar
Keith Packard committed
910
	    }
911
	    if (!FcBlanksAdd (parse->config->blanks, v->u.integer))
Keith Packard's avatar
Keith Packard committed
912
	    {
913
		FcConfigMessage (parse, FcSevereError, "out of memory");
914
		break;
Keith Packard's avatar
Keith Packard committed
915 916 917
	    }
	}
    }
918
}
Keith Packard's avatar
Keith Packard committed
919

920 921 922 923 924 925 926 927 928 929 930 931 932 933
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;
    }
}

934 935 936 937 938 939 940 941 942 943
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
944
    {
945
	FcConfigMessage (parse, FcSevereError, "out of memory");
946
	return;
Keith Packard's avatar
Keith Packard committed
947
    }
948 949 950
    end = 0;
    l = (int) strtol ((char *) s, (char **)&end, 0);
    if (end != s + strlen ((char *) s))
951
	FcConfigMessage (parse, FcSevereError, "\"%s\": not a valid integer", s);
952 953 954
    else
	FcVStackPushInteger (parse, l);
    FcStrFree (s);
Keith Packard's avatar
Keith Packard committed
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 1000
/*
 * idea copied from glib g_ascii_strtod with 
 * permission of the author (Alexander Larsson) 
 */

#include <locale.h>

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

    /*
     * Have to swap the decimal point to match the current locale
     * if that locale doesn't use 0x2e
     */
    if ((dot = strchr (s, 0x2e)) &&
	(locale_data = localeconv ()) &&
	(locale_data->decimal_point[0] != 0x2e ||
	 locale_data->decimal_point[1] != 0))
    {
	char	buf[128];
	int	slen = strlen (s);
	int	dlen = strlen (locale_data->decimal_point);
	
	if (slen + dlen > sizeof (buf))
	{
	    if (end)
		*end = s;
	    v = 0;
	}
	else
	{
	    char	*buf_end;
	    /* mantissa */
	    strncpy (buf, s, dot - s);
	    /* decimal point */
	    strcpy (buf + (dot - s), locale_data->decimal_point);
	    /* rest of number */
	    strcpy (buf + (dot - s) + dlen, dot + 1);
	    buf_end = 0;
	    v = strtod (buf, &buf_end);
1001 1002 1003 1004 1005
	    if (buf_end) {
		buf_end = s + (buf_end - buf);
		if (buf_end > dot)
		    buf_end -= dlen - 1;
	    }
1006 1007 1008 1009 1010 1011 1012 1013 1014
	    if (end)
		*end = buf_end;
	}
    }
    else
	v = strtod (s, end);
    return v;
}

1015 1016
static void
FcParseDouble (FcConfigParse *parse)
Keith Packard's avatar
Keith Packard committed
1017
{
1018 1019
    FcChar8 *s, *end;
    double  d;
Keith Packard's avatar
Keith Packard committed
1020
    
1021 1022 1023 1024
    if (!parse->pstack)
	return;
    s = FcStrBufDone (&parse->pstack->str);
    if (!s)
Keith Packard's avatar
Keith Packard committed
1025
    {
1026
	FcConfigMessage (parse, FcSevereError, "out of memory");
1027
	return;
Keith Packard's avatar
Keith Packard committed
1028
    }
1029
    end = 0;
1030
    d = FcStrtod ((char *) s, (char **)&end);
1031
    if (end != s + strlen ((char *) s))
1032
	FcConfigMessage (parse, FcSevereError, "\"%s\": not a valid double", s);
Keith Packard's avatar
Keith Packard committed
1033
    else
1034 1035 1036
	FcVStackPushDouble (parse, d);
    FcStrFree (s);
}
Keith Packard's avatar
Keith Packard committed
1037

1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
static void
FcParseString (FcConfigParse *parse, FcVStackTag tag)
{
    FcChar8 *s;
    
    if (!parse->pstack)
	return;
    s = FcStrBufDone (&parse->pstack->str);
    if (!s)
    {
1048
	FcConfigMessage (parse, FcSevereError, "out of memory");
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
	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)))
    {
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
	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;
1083
	}
1084
	FcVStackDestroy (vstack);
1085
	matrix_state--;
1086 1087
    }
    if (matrix_state != m_done)
1088
	FcConfigMessage (parse, FcSevereError, "wrong number of matrix elements");
1089 1090
    else
	FcVStackPushMatrix (parse, &m);
Keith Packard's avatar
Keith Packard committed
1091 1092 1093
}

static FcBool
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
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)
    {
1115
	FcConfigMessage (parse, FcSevereError, "out of memory");
1116 1117 1118 1119 1120 1121 1122 1123
	return;
    }
    FcVStackPushBool (parse, FcConfigLexBool (s));
    FcStrFree (s);
}

static void
FcParseFamilies (FcConfigParse *parse, FcVStackTag tag)
Keith Packard's avatar
Keith Packard committed
1124
{
1125 1126 1127 1128 1129 1130 1131
    FcVStack	*vstack;
    FcExpr	*left, *expr = 0, *new;

    while ((vstack = FcVStackPop (parse)))
    {
	if (vstack->tag != FcVStackFamily)
	{
1132 1133 1134
	    FcConfigMessage (parse, FcSevereWarning, "non-family");
	    FcVStackDestroy (vstack);
	    continue;
1135 1136 1137 1138 1139 1140 1141 1142 1143
	}
	left = vstack->u.expr;
	vstack->tag = FcVStackNone;
	FcVStackDestroy (vstack);
	if (expr)
	{
	    new = FcExprCreateOp (left, FcOpComma, expr);
	    if (!new)
	    {
1144
		FcConfigMessage (parse, FcSevereError, "out of memory");
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
		FcExprDestroy (left);
		FcExprDestroy (expr);
		break;
	    }
	    expr = new;
	}
	else
	    expr = left;
    }
    if (expr)
    {
	if (!FcVStackPushExpr (parse, tag, expr))
	{
1158
	    FcConfigMessage (parse, FcSevereError, "out of memory");
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
	    if (expr)
		FcExprDestroy (expr);
	}
    }
}

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

    if (!parse->pstack)
	return;
    s = FcStrBufDone (&parse->pstack->str);
    if (!s)
    {
1176
	FcConfigMessage (parse, FcSevereError, "out of memory");
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
	return;
    }
    expr = FcExprCreateString (s);
    FcStrFree (s);
    if (expr)
	FcVStackPushExpr (parse, FcVStackFamily, expr);
}

static void
FcParseAlias (FcConfigParse *parse)
{
1188
    FcExpr	*family = 0, *accept = 0, *prefer = 0, *def = 0, *new = 0;
Keith Packard's avatar
Keith Packard committed
1189
    FcEdit	*edit = 0, *next;
1190
    FcVStack	*vstack;
Keith Packard's avatar
Keith Packard committed
1191 1192
    FcTest	*test;

1193
    while ((vstack = FcVStackPop (parse))) 
Keith Packard's avatar
Keith Packard committed
1194
    {
1195 1196 1197
	switch (vstack->tag) {
	case FcVStackFamily:
	    if (family)
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
	    {
		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;
	    }
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
	    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:
1232
	    FcConfigMessage (parse, FcSevereWarning, "bad alias");
1233 1234 1235
	    break;
	}
	FcVStackDestroy (vstack);
Keith Packard's avatar
Keith Packard committed
1236
    }
1237
    if (!family)
1238
    {
1239 1240 1241 1242 1243 1244 1245
	FcConfigMessage (parse, FcSevereError, "missing family in alias");
	if (prefer)
	    FcExprDestroy (prefer);
	if (accept)
	    FcExprDestroy (accept);
	if (def)
	    FcExprDestroy (def);
1246 1247
	return;
    }
Keith Packard's avatar
Keith Packard committed
1248 1249 1250 1251
    if (prefer)
    {
	edit = FcEditCreate (FcConfigSaveField ("family"),
			     FcOpPrepend,
1252 1253
			     prefer,
			     FcValueBindingWeak);
Keith Packard's avatar
Keith Packard committed
1254 1255
	if (edit)
	    edit->next = 0;
1256 1257
	else
	    FcExprDestroy (prefer);
Keith Packard's avatar
Keith Packard committed
1258 1259 1260 1261 1262 1263
    }
    if (accept)
    {
	next = edit;
	edit = FcEditCreate (FcConfigSaveField ("family"),
			     FcOpAppend,
1264 1265
			     accept,
			     FcValueBindingWeak);
Keith Packard's avatar
Keith Packard committed
1266 1267
	if (edit)
	    edit->next = next;
1268 1269
	else
	    FcExprDestroy (accept);
Keith Packard's avatar
Keith Packard committed
1270 1271 1272 1273 1274
    }
    if (def)
    {
	next = edit;
	edit = FcEditCreate (FcConfigSaveField ("family"),
1275 1276 1277
			     FcOpAppendLast,
			     def,
			     FcValueBindingWeak);
Keith Packard's avatar
Keith Packard committed
1278 1279
	if (edit)
	    edit->next = next;
1280 1281
	else
	    FcExprDestroy (def);
Keith Packard's avatar
Keith Packard committed
1282 1283 1284
    }
    if (edit)
    {
1285 1286
	test = FcTestCreate (FcMatchPattern,
			     FcQualAny,
Keith Packard's avatar
Keith Packard committed
1287
			     (FcChar8 *) FC_FAMILY,
Keith Packard's avatar
Keith Packard committed
1288 1289 1290
			     FcOpEqual,
			     family);
	if (test)
1291 1292
	    if (!FcConfigAddEdit (parse->config, test, edit, FcMatchPattern))
		FcTestDestroy (test);
Keith Packard's avatar
Keith Packard committed
1293
    }
1294 1295
    else
	FcExprDestroy (family);
Keith Packard's avatar
Keith Packard committed
1296 1297
}

1298 1299
static FcExpr *
FcPopExpr (FcConfigParse *parse)
Keith Packard's avatar
Keith Packard committed
1300
{
1301 1302 1303 1304 1305 1306 1307 1308 1309
    FcVStack	*vstack = FcVStackPop (parse);
    FcExpr	*expr = 0;
    if (!vstack)
	return 0;
    switch (vstack->tag) {
    case FcVStackNone:
	break;
    case FcVStackString:
    case FcVStackFamily:
1310 1311
	expr = FcExprCreateString (vstack->u.string);
	break;
1312
    case FcVStackField:
1313 1314
	expr = FcExprCreateField ((char *) vstack->u.string);
	break;
1315
    case FcVStackConstant:
1316
	expr = FcExprCreateConst (vstack->u.string);
1317
	break;
1318 1319 1320
    case FcVStackGlob:
	/* XXX: What's the correct action here? (CDW) */
	break;
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337