+ FcStrBufString (buf, new_str);
+ free (new_str);
+ return FcTrue;
+ }
+ else
+ return FcFalse;
+ }
+
+ /* now try our custom formats */
+ if (0) { }
+#define BUILTIN(name, format) \
+ else if (0 == strcmp ((const char *) c->word, name))\
+ ret = FcPatternFormatToBuf (pat, (const FcChar8 *) format, buf)
+ BUILTIN ("fccat", FCCAT_FORMAT);
+ BUILTIN ("fcmatch", FCMATCH_FORMAT);
+ BUILTIN ("fclist", FCLIST_FORMAT);
+ BUILTIN ("pkgkit", PKGKIT_FORMAT);
+#undef BUILTIN
+ else
+ ret = FcFalse;
+
+ if (!ret)
+ message ("unknown builtin \"%s\"",
+ c->word);
+
+ return ret;
+}
+
+static FcBool
+interpret_expr (FcFormatContext *c,
+ FcPattern *pat,
+ FcStrBuf *buf,
+ FcChar8 term);
+
+static FcBool
+interpret_subexpr (FcFormatContext *c,
+ FcPattern *pat,
+ FcStrBuf *buf)
+{
+ return expect_char (c, '{') &&
+ interpret_expr (c, pat, buf, '}') &&
+ expect_char (c, '}');
+}
+
+static FcBool
+maybe_interpret_subexpr (FcFormatContext *c,
+ FcPattern *pat,
+ FcStrBuf *buf)
+{
+ return (*c->format == '{') ?
+ interpret_subexpr (c, pat, buf) :
+ FcTrue;
+}
+
+static FcBool
+skip_subexpr (FcFormatContext *c);
+
+static FcBool
+skip_percent (FcFormatContext *c)
+{
+ int width;
+
+ if (!expect_char (c, '%'))
+ return FcFalse;
+
+ /* skip an optional width specifier */
+ width = strtol ((const char *) c->format, (char **) &c->format, 10);
+
+ if (!expect_char (c, '{'))
+ return FcFalse;
+
+ while(*c->format && *c->format != '}')
+ {
+ switch (*c->format)
+ {
+ case '\\':
+ c->format++; /* skip over '\\' */
+ if (*c->format)
+ c->format++;
+ continue;
+ case '{':
+ if (!skip_subexpr (c))
+ return FcFalse;
+ continue;
+ }
+ c->format++;
+ }
+
+ return expect_char (c, '}');
+}
+
+static FcBool
+skip_expr (FcFormatContext *c)
+{
+ while(*c->format && *c->format != '}')
+ {
+ switch (*c->format)
+ {
+ case '\\':
+ c->format++; /* skip over '\\' */
+ if (*c->format)
+ c->format++;
+ continue;
+ case '%':
+ if (!skip_percent (c))
+ return FcFalse;
+ continue;
+ }
+ c->format++;
+ }
+
+ return FcTrue;
+}
+
+static FcBool
+skip_subexpr (FcFormatContext *c)
+{
+ return expect_char (c, '{') &&
+ skip_expr (c) &&
+ expect_char (c, '}');
+}
+
+static FcBool
+maybe_skip_subexpr (FcFormatContext *c)
+{
+ return (*c->format == '{') ?
+ skip_subexpr (c) :
+ FcTrue;
+}
+
+static FcBool
+interpret_filter_in (FcFormatContext *c,
+ FcPattern *pat,
+ FcStrBuf *buf)
+{
+ FcObjectSet *os;
+ FcPattern *subpat;
+
+ if (!expect_char (c, '+'))
+ return FcFalse;
+
+ os = FcObjectSetCreate ();
+ if (!os)
+ return FcFalse;
+
+ do
+ {
+ /* XXX binding */
+ if (!read_word (c) ||
+ !FcObjectSetAdd (os, (const char *) c->word))
+ {
+ FcObjectSetDestroy (os);
+ return FcFalse;
+ }
+ }
+ while (consume_char (c, ','));
+
+ subpat = FcPatternFilter (pat, os);
+ FcObjectSetDestroy (os);
+
+ if (!subpat ||
+ !interpret_subexpr (c, subpat, buf))
+ return FcFalse;
+
+ FcPatternDestroy (subpat);
+ return FcTrue;
+}
+
+static FcBool
+interpret_filter_out (FcFormatContext *c,
+ FcPattern *pat,
+ FcStrBuf *buf)
+{
+ FcPattern *subpat;
+
+ if (!expect_char (c, '-'))
+ return FcFalse;
+
+ subpat = FcPatternDuplicate (pat);
+ if (!subpat)
+ return FcFalse;
+
+ do
+ {
+ if (!read_word (c))
+ {
+ FcPatternDestroy (subpat);
+ return FcFalse;
+ }
+
+ FcPatternDel (subpat, (const char *) c->word);
+ }
+ while (consume_char (c, ','));
+
+ if (!interpret_subexpr (c, subpat, buf))
+ return FcFalse;
+
+ FcPatternDestroy (subpat);
+ return FcTrue;
+}
+
+static FcBool
+interpret_cond (FcFormatContext *c,
+ FcPattern *pat,
+ FcStrBuf *buf)
+{
+ FcBool pass;
+
+ if (!expect_char (c, '?'))
+ return FcFalse;
+
+ pass = FcTrue;
+
+ do
+ {
+ FcBool negate;
+ FcValue v;
+
+ negate = consume_char (c, '!');
+
+ if (!read_word (c))
+ return FcFalse;
+
+ pass = pass &&
+ (negate ^
+ (FcResultMatch ==
+ FcPatternGet (pat, (const char *) c->word, 0, &v)));
+ }
+ while (consume_char (c, ','));
+
+ if (pass)
+ {
+ if (!interpret_subexpr (c, pat, buf) ||
+ !maybe_skip_subexpr (c))
+ return FcFalse;
+ }
+ else
+ {
+ if (!skip_subexpr (c) ||
+ !maybe_interpret_subexpr (c, pat, buf))
+ return FcFalse;
+ }
+
+ return FcTrue;
+}
+
+static FcBool
+interpret_count (FcFormatContext *c,
+ FcPattern *pat,
+ FcStrBuf *buf)
+{
+ int count;
+ FcPatternElt *e;
+ FcChar8 buf_static[64];
+
+ if (!expect_char (c, '#'))
+ return FcFalse;
+
+ if (!read_word (c))
+ return FcFalse;
+
+ count = 0;
+ e = FcPatternObjectFindElt (pat,
+ FcObjectFromName ((const char *) c->word));
+ if (e)
+ {
+ FcValueListPtr l;
+ count++;
+ for (l = FcPatternEltValues(e);
+ l->next;
+ l = l->next)
+ count++;
+ }
+
+ snprintf ((char *) buf_static, sizeof (buf_static), "%d", count);
+ FcStrBufString (buf, buf_static);
+
+ return FcTrue;
+}
+
+static FcBool
+interpret_enumerate (FcFormatContext *c,
+ FcPattern *pat,
+ FcStrBuf *buf)
+{
+ FcObjectSet *os;
+ FcPattern *subpat;
+ const FcChar8 *format_save;
+ int idx;
+ FcBool ret, done;
+ FcStrList *lang_strs;
+
+ if (!expect_char (c, '[') ||
+ !expect_char (c, ']'))
+ return FcFalse;
+
+ os = FcObjectSetCreate ();
+ if (!os)
+ return FcFalse;
+
+ ret = FcTrue;
+
+ do
+ {
+ if (!read_word (c) ||
+ !FcObjectSetAdd (os, (const char *) c->word))
+ {
+ FcObjectSetDestroy (os);
+ return FcFalse;
+ }
+ }
+ while (consume_char (c, ','));
+
+ /* If we have one element and it's of type FcLangSet, we want
+ * to enumerate the languages in it. */
+ lang_strs = NULL;
+ if (os->nobject == 1)
+ {
+ FcLangSet *langset;
+ if (FcResultMatch ==
+ FcPatternGetLangSet (pat, os->objects[0], 0, &langset))
+ {
+ FcStrSet *ss;
+ if (!(ss = FcLangSetGetLangs (langset)) ||
+ !(lang_strs = FcStrListCreate (ss)))
+ goto bail0;
+ }
+ }
+
+ subpat = FcPatternDuplicate (pat);
+ if (!subpat)
+ goto bail0;
+
+ format_save = c->format;
+ idx = 0;
+ do
+ {
+ int i;
+
+ done = FcTrue;
+
+ if (lang_strs)
+ {
+ FcChar8 *lang;
+
+ FcPatternDel (subpat, os->objects[0]);
+ if ((lang = FcStrListNext (lang_strs)))
+ {
+ /* XXX binding? */
+ FcPatternAddString (subpat, os->objects[0], lang);
+ done = FcFalse;
+ }
+ }
+ else
+ {
+ for (i = 0; i < os->nobject; i++)
+ {
+ FcValue v;
+
+ /* XXX this can be optimized by accessing valuelist linked lists
+ * directly and remembering where we were. Most (all) value lists
+ * in normal uses are pretty short though (language tags are
+ * stored as a LangSet, not separate values.). */
+ FcPatternDel (subpat, os->objects[i]);
+ if (FcResultMatch ==
+ FcPatternGet (pat, os->objects[i], idx, &v))
+ {
+ /* XXX binding */
+ FcPatternAdd (subpat, os->objects[i], v, FcFalse);
+ done = FcFalse;
+ }
+ }
+ }
+
+ if (!done)
+ {
+ c->format = format_save;
+ ret = interpret_subexpr (c, subpat, buf);
+ if (!ret)
+ goto bail;
+ }
+
+ idx++;
+ } while (!done);
+
+ if (c->format == format_save)
+ skip_subexpr (c);
+
+bail:
+ FcPatternDestroy (subpat);
+bail0:
+ if (lang_strs)
+ FcStrListDone (lang_strs);
+ FcObjectSetDestroy (os);
+
+ return ret;
+}
+
+static FcBool
+interpret_simple (FcFormatContext *c,
+ FcPattern *pat,
+ FcStrBuf *buf)
+{
+ FcPatternElt *e;
+ FcBool add_colon = FcFalse;
+ FcBool add_elt_name = FcFalse;
+ int idx;
+ FcChar8 *else_string;
+
+ if (consume_char (c, ':'))
+ add_colon = FcTrue;
+
+ if (!read_word (c))
+ return FcFalse;
+
+ idx = -1;
+ if (consume_char (c, '['))
+ {
+ idx = strtol ((const char *) c->format, (char **) &c->format, 10);
+ if (idx < 0)
+ {
+ message ("expected non-negative number at %d",
+ c->format-1 - c->format_orig + 1);
+ return FcFalse;
+ }
+ if (!expect_char (c, ']'))
+ return FcFalse;
+ }
+
+ if (consume_char (c, '='))
+ add_elt_name = FcTrue;
+
+ /* modifiers */
+ else_string = NULL;
+ if (consume_char (c, ':'))
+ {
+ FcChar8 *orig;
+ /* divert the c->word for now */
+ orig = c->word;
+ c->word = c->word + strlen ((const char *) c->word) + 1;
+ /* for now we just support 'default value' */
+ if (!expect_char (c, '-') ||
+ !read_chars (c, '|'))
+ {
+ c->word = orig;
+ return FcFalse;
+ }
+ else_string = c->word;
+ c->word = orig;
+ }
+
+ e = FcPatternObjectFindElt (pat,
+ FcObjectFromName ((const char *) c->word));
+ if (e || else_string)
+ {
+ FcValueListPtr l = NULL;
+
+ if (add_colon)
+ FcStrBufChar (buf, ':');
+ if (add_elt_name)
+ {
+ FcStrBufString (buf, c->word);
+ FcStrBufChar (buf, '=');
+ }
+
+ if (e)