2 * Copyright © 2008,2009 Red Hat, Inc.
4 * Red Hat Author(s): Behdad Esfahbod
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. The authors make no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
31 /* The language is documented in doc/fcformat.fncs
32 * These are the features implemented:
39 * default %{elt:-word}
42 * filter-out %{-elt1,elt2,elt3{expr}}
43 * filter-in %{+elt1,elt2,elt3{expr}}
44 * conditional %{?elt1,elt2,!elt3{}{}}
45 * enumerate %{[]elt1,elt2{expr}}
46 * langset langset enumeration using the same syntax
48 * convert %{elt|conv1|conv2|conv3}
51 * basename FcStrBasename
52 * dirname FcStrDirname
53 * downcase FcStrDowncase
59 * translate translate chars
62 * unparse FcNameUnparse
63 * fcmatch fc-match default
64 * fclist fc-list default
65 * fccat fc-cat default
66 * pkgkit PackageKit package tag format
69 * Some ideas for future syntax extensions:
71 * - verbose builtin that is like FcPatternPrint
72 * - allow indexing subexprs using '%{[idx]elt1,elt2{subexpr}}'
73 * - allow indexing in +, -, ? filtering?
74 * - conditional/filtering/deletion on binding (using '(w)'/'(s)'/'(=)' notation)
78 #define FCCAT_FORMAT "\"%{file|basename|cescape}\" %{index} \"%{-file{%{=unparse|cescape}}}\""
79 #define FCMATCH_FORMAT "%{file:-<unknown filename>|basename}: \"%{family[0]:-<unknown family>}\" \"%{style[0]:-<unknown style>}\""
80 #define FCLIST_FORMAT "%{?file{%{file}: }}%{-file{%{=unparse}}}"
81 #define PKGKIT_FORMAT "%{[]family{font(%{family|downcase|delete( )})\n}}%{[]lang{font(:lang=%{lang|downcase|translate(_,-)})\n}}"
85 message (const char *fmt, ...)
89 fprintf (stderr, "Fontconfig: Pattern format error: ");
90 vfprintf (stderr, fmt, args);
91 fprintf (stderr, ".\n");
96 typedef struct _FcFormatContext
98 const FcChar8 *format_orig;
99 const FcChar8 *format;
102 FcBool word_allocated;
106 FcFormatContextInit (FcFormatContext *c,
107 const FcChar8 *format,
111 c->format_orig = c->format = format;
112 c->format_len = strlen ((const char *) format);
114 if (c->format_len < scratch_len)
117 c->word_allocated = FcFalse;
121 c->word = malloc (c->format_len + 1);
122 c->word_allocated = FcTrue;
125 return c->word != NULL;
129 FcFormatContextDone (FcFormatContext *c)
131 if (c && c->word_allocated)
138 consume_char (FcFormatContext *c,
141 if (*c->format != term)
149 expect_char (FcFormatContext *c,
152 FcBool res = consume_char (c, term);
155 if (c->format == c->format_orig + c->format_len)
156 message ("format ended while expecting '%c'",
159 message ("expected '%c' at %d",
160 term, c->format - c->format_orig + 1);
166 FcCharIsPunct (const FcChar8 c)
185 static char escaped_char(const char ch)
188 case 'a': return '\a';
189 case 'b': return '\b';
190 case 'f': return '\f';
191 case 'n': return '\n';
192 case 'r': return '\r';
193 case 't': return '\t';
194 case 'v': return '\v';
200 read_word (FcFormatContext *c)
208 if (*c->format == '\\')
212 *p++ = escaped_char (*c->format++);
215 else if (FcCharIsPunct (*c->format))
224 message ("expected identifier at %d",
225 c->format - c->format_orig + 1);
233 read_chars (FcFormatContext *c,
240 while (*c->format && *c->format != '}' && *c->format != term)
242 if (*c->format == '\\')
246 *p++ = escaped_char (*c->format++);
256 message ("expected character data at %d",
257 c->format - c->format_orig + 1);
265 FcPatternFormatToBuf (FcPattern *pat,
266 const FcChar8 *format,
270 interpret_builtin (FcFormatContext *c,
277 if (!expect_char (c, '=') ||
281 /* try simple builtins first */
283 #define BUILTIN(name, func) \
284 else if (0 == strcmp ((const char *) c->word, name))\
285 do { new_str = func (pat); ret = FcTrue; } while (0)
286 BUILTIN ("unparse", FcNameUnparse);
287 /* BUILTIN ("verbose", FcPatternPrint); XXX */
296 FcStrBufString (buf, new_str);
304 /* now try our custom formats */
306 #define BUILTIN(name, format) \
307 else if (0 == strcmp ((const char *) c->word, name))\
308 ret = FcPatternFormatToBuf (pat, (const FcChar8 *) format, buf)
309 BUILTIN ("fccat", FCCAT_FORMAT);
310 BUILTIN ("fcmatch", FCMATCH_FORMAT);
311 BUILTIN ("fclist", FCLIST_FORMAT);
312 BUILTIN ("pkgkit", PKGKIT_FORMAT);
318 message ("unknown builtin \"%s\"",
325 interpret_expr (FcFormatContext *c,
331 interpret_subexpr (FcFormatContext *c,
335 return expect_char (c, '{') &&
336 interpret_expr (c, pat, buf, '}') &&
337 expect_char (c, '}');
341 maybe_interpret_subexpr (FcFormatContext *c,
345 return (*c->format == '{') ?
346 interpret_subexpr (c, pat, buf) :
351 skip_subexpr (FcFormatContext *c);
354 skip_percent (FcFormatContext *c)
356 if (!expect_char (c, '%'))
359 /* skip an optional width specifier */
360 if (strtol ((const char *) c->format, (char **) &c->format, 10))
363 if (!expect_char (c, '{'))
366 while(*c->format && *c->format != '}')
371 c->format++; /* skip over '\\' */
376 if (!skip_subexpr (c))
383 return expect_char (c, '}');
387 skip_expr (FcFormatContext *c)
389 while(*c->format && *c->format != '}')
394 c->format++; /* skip over '\\' */
399 if (!skip_percent (c))
410 skip_subexpr (FcFormatContext *c)
412 return expect_char (c, '{') &&
414 expect_char (c, '}');
418 maybe_skip_subexpr (FcFormatContext *c)
420 return (*c->format == '{') ?
426 interpret_filter_in (FcFormatContext *c,
433 if (!expect_char (c, '+'))
436 os = FcObjectSetCreate ();
443 if (!read_word (c) ||
444 !FcObjectSetAdd (os, (const char *) c->word))
446 FcObjectSetDestroy (os);
450 while (consume_char (c, ','));
452 subpat = FcPatternFilter (pat, os);
453 FcObjectSetDestroy (os);
456 !interpret_subexpr (c, subpat, buf))
459 FcPatternDestroy (subpat);
464 interpret_filter_out (FcFormatContext *c,
470 if (!expect_char (c, '-'))
473 subpat = FcPatternDuplicate (pat);
481 FcPatternDestroy (subpat);
485 FcPatternDel (subpat, (const char *) c->word);
487 while (consume_char (c, ','));
489 if (!interpret_subexpr (c, subpat, buf))
492 FcPatternDestroy (subpat);
497 interpret_cond (FcFormatContext *c,
503 if (!expect_char (c, '?'))
513 negate = consume_char (c, '!');
521 FcPatternGet (pat, (const char *) c->word, 0, &v)));
523 while (consume_char (c, ','));
527 if (!interpret_subexpr (c, pat, buf) ||
528 !maybe_skip_subexpr (c))
533 if (!skip_subexpr (c) ||
534 !maybe_interpret_subexpr (c, pat, buf))
542 interpret_count (FcFormatContext *c,
548 FcChar8 buf_static[64];
550 if (!expect_char (c, '#'))
557 if (FcPatternFindIter (pat, &iter, (const char *) c->word))
559 count = FcPatternIterValueCount (pat, &iter);
562 snprintf ((char *) buf_static, sizeof (buf_static), "%d", count);
563 FcStrBufString (buf, buf_static);
569 interpret_enumerate (FcFormatContext *c,
575 const FcChar8 *format_save;
578 FcStrList *lang_strs;
580 if (!expect_char (c, '[') ||
581 !expect_char (c, ']'))
584 os = FcObjectSetCreate ();
592 if (!read_word (c) ||
593 !FcObjectSetAdd (os, (const char *) c->word))
595 FcObjectSetDestroy (os);
599 while (consume_char (c, ','));
601 /* If we have one element and it's of type FcLangSet, we want
602 * to enumerate the languages in it. */
604 if (os->nobject == 1)
608 FcPatternGetLangSet (pat, os->objects[0], 0, &langset))
611 if (!(ss = FcLangSetGetLangs (langset)) ||
612 !(lang_strs = FcStrListCreate (ss)))
617 subpat = FcPatternDuplicate (pat);
621 format_save = c->format;
633 FcPatternDel (subpat, os->objects[0]);
634 if ((lang = FcStrListNext (lang_strs)))
637 FcPatternAddString (subpat, os->objects[0], lang);
643 for (i = 0; i < os->nobject; i++)
647 /* XXX this can be optimized by accessing valuelist linked lists
648 * directly and remembering where we were. Most (all) value lists
649 * in normal uses are pretty short though (language tags are
650 * stored as a LangSet, not separate values.). */
651 FcPatternDel (subpat, os->objects[i]);
653 FcPatternGet (pat, os->objects[i], idx, &v))
656 FcPatternAdd (subpat, os->objects[i], v, FcFalse);
664 c->format = format_save;
665 ret = interpret_subexpr (c, subpat, buf);
673 if (c->format == format_save)
677 FcPatternDestroy (subpat);
680 FcStrListDone (lang_strs);
681 FcObjectSetDestroy (os);
687 interpret_simple (FcFormatContext *c,
692 FcBool add_colon = FcFalse;
693 FcBool add_elt_name = FcFalse;
695 FcChar8 *else_string;
697 if (consume_char (c, ':'))
704 if (consume_char (c, '['))
706 idx = strtol ((const char *) c->format, (char **) &c->format, 10);
709 message ("expected non-negative number at %d",
710 c->format-1 - c->format_orig + 1);
713 if (!expect_char (c, ']'))
717 if (consume_char (c, '='))
718 add_elt_name = FcTrue;
722 if (consume_char (c, ':'))
725 /* divert the c->word for now */
727 c->word = c->word + strlen ((const char *) c->word) + 1;
728 /* for now we just support 'default value' */
729 if (!expect_char (c, '-') ||
730 !read_chars (c, '|'))
735 else_string = c->word;
739 if (FcPatternFindIter (pat, &iter, (const char *) c->word) || else_string)
741 FcValueListPtr l = NULL;
744 FcStrBufChar (buf, ':');
747 FcStrBufString (buf, c->word);
748 FcStrBufChar (buf, '=');
751 l = FcPatternIterGetValues (pat, &iter);
757 l = FcValueListNext(l);
762 if (!FcNameUnparseValue (buf, &l->value, '\0'))
769 FcNameUnparseValueList (buf, l, '\0');
775 FcStrBufString (buf, else_string);
783 cescape (FcFormatContext *c FC_UNUSED,
787 /* XXX escape \n etc? */
795 FcStrBufChar (buf, '\\');
798 FcStrBufChar (buf, *str++);
804 shescape (FcFormatContext *c FC_UNUSED,
808 FcStrBufChar (buf, '\'');
812 FcStrBufString (buf, (const FcChar8 *) "'\\''");
814 FcStrBufChar (buf, *str);
817 FcStrBufChar (buf, '\'');
822 xmlescape (FcFormatContext *c FC_UNUSED,
826 /* XXX escape \n etc? */
832 case '&': FcStrBufString (buf, (const FcChar8 *) "&"); break;
833 case '<': FcStrBufString (buf, (const FcChar8 *) "<"); break;
834 case '>': FcStrBufString (buf, (const FcChar8 *) ">"); break;
835 default: FcStrBufChar (buf, *str); break;
843 delete_chars (FcFormatContext *c,
847 /* XXX not UTF-8 aware */
849 if (!expect_char (c, '(') ||
850 !read_chars (c, ')') ||
851 !expect_char (c, ')'))
858 p = (FcChar8 *) strpbrk ((const char *) str, (const char *) c->word);
861 FcStrBufData (buf, str, p - str);
866 FcStrBufString (buf, str);
876 escape_chars (FcFormatContext *c,
880 /* XXX not UTF-8 aware */
882 if (!expect_char (c, '(') ||
883 !read_chars (c, ')') ||
884 !expect_char (c, ')'))
891 p = (FcChar8 *) strpbrk ((const char *) str, (const char *) c->word);
894 FcStrBufData (buf, str, p - str);
895 FcStrBufChar (buf, c->word[0]);
896 FcStrBufChar (buf, *p);
901 FcStrBufString (buf, str);
911 translate_chars (FcFormatContext *c,
915 char *from, *to, repeat;
916 int from_len, to_len;
918 /* XXX not UTF-8 aware */
920 if (!expect_char (c, '(') ||
921 !read_chars (c, ',') ||
922 !expect_char (c, ','))
925 from = (char *) c->word;
926 from_len = strlen (from);
927 to = from + from_len + 1;
929 /* hack: we temporarily divert c->word */
930 c->word = (FcChar8 *) to;
931 if (!read_chars (c, ')'))
933 c->word = (FcChar8 *) from;
936 c->word = (FcChar8 *) from;
938 to_len = strlen (to);
939 repeat = to[to_len - 1];
941 if (!expect_char (c, ')'))
948 p = (FcChar8 *) strpbrk ((const char *) str, (const char *) from);
952 FcStrBufData (buf, str, p - str);
953 i = strchr (from, *p) - from;
954 FcStrBufChar (buf, i < to_len ? to[i] : repeat);
959 FcStrBufString (buf, str);
969 interpret_convert (FcFormatContext *c,
976 FcChar8 buf_static[8192];
979 if (!expect_char (c, '|') ||
983 /* prepare the buffer */
984 FcStrBufChar (buf, '\0');
987 str = buf->buf + start;
990 /* try simple converters first */
992 #define CONVERTER(name, func) \
993 else if (0 == strcmp ((const char *) c->word, name))\
994 do { new_str = func (str); ret = FcTrue; } while (0)
995 CONVERTER ("downcase", FcStrDowncase);
996 CONVERTER ("basename", FcStrBasename);
997 CONVERTER ("dirname", FcStrDirname);
1006 FcStrBufString (buf, new_str);
1007 FcStrFree (new_str);
1014 FcStrBufInit (&new_buf, buf_static, sizeof (buf_static));
1016 /* now try our custom converters */
1018 #define CONVERTER(name, func) \
1019 else if (0 == strcmp ((const char *) c->word, name))\
1020 ret = func (c, str, &new_buf)
1021 CONVERTER ("cescape", cescape);
1022 CONVERTER ("shescape", shescape);
1023 CONVERTER ("xmlescape", xmlescape);
1024 CONVERTER ("delete", delete_chars);
1025 CONVERTER ("escape", escape_chars);
1026 CONVERTER ("translate", translate_chars);
1033 FcStrBufChar (&new_buf, '\0');
1034 FcStrBufString (buf, new_buf.buf);
1037 message ("unknown converter \"%s\"",
1040 FcStrBufDestroy (&new_buf);
1046 maybe_interpret_converts (FcFormatContext *c,
1050 while (*c->format == '|')
1051 if (!interpret_convert (c, buf, start))
1058 align_to_width (FcStrBuf *buf,
1067 len = buf->len - start;
1071 while (len++ < -width)
1072 FcStrBufChar (buf, ' ');
1074 else if (len < width)
1079 while (len++ < width)
1080 FcStrBufChar (buf, ' ');
1084 memmove (buf->buf + buf->len - len,
1085 buf->buf + buf->len - width,
1087 memset (buf->buf + buf->len - width,
1092 return !buf->failed;
1095 interpret_percent (FcFormatContext *c,
1102 if (!expect_char (c, '%'))
1105 if (consume_char (c, '%')) /* "%%" */
1107 FcStrBufChar (buf, '%');
1111 /* parse an optional width specifier */
1112 width = strtol ((const char *) c->format, (char **) &c->format, 10);
1114 if (!expect_char (c, '{'))
1119 switch (*c->format) {
1120 case '=': ret = interpret_builtin (c, pat, buf); break;
1121 case '{': ret = interpret_subexpr (c, pat, buf); break;
1122 case '+': ret = interpret_filter_in (c, pat, buf); break;
1123 case '-': ret = interpret_filter_out (c, pat, buf); break;
1124 case '?': ret = interpret_cond (c, pat, buf); break;
1125 case '#': ret = interpret_count (c, pat, buf); break;
1126 case '[': ret = interpret_enumerate (c, pat, buf); break;
1127 default: ret = interpret_simple (c, pat, buf); break;
1131 maybe_interpret_converts (c, buf, start) &&
1132 align_to_width (buf, start, width) &&
1133 expect_char (c, '}');
1137 interpret_expr (FcFormatContext *c,
1142 while (*c->format && *c->format != term)
1147 c->format++; /* skip over '\\' */
1149 FcStrBufChar (buf, escaped_char (*c->format++));
1152 if (!interpret_percent (c, pat, buf))
1156 FcStrBufChar (buf, *c->format++);
1162 FcPatternFormatToBuf (FcPattern *pat,
1163 const FcChar8 *format,
1167 FcChar8 word_static[1024];
1170 if (!FcFormatContextInit (&c, format, word_static, sizeof (word_static)))
1173 ret = interpret_expr (&c, pat, buf, '\0');
1175 FcFormatContextDone (&c);
1181 FcPatternFormat (FcPattern *pat,
1182 const FcChar8 *format)
1185 FcChar8 buf_static[8192 - 1024];
1186 FcPattern *alloced = NULL;
1190 alloced = pat = FcPatternCreate ();
1192 FcStrBufInit (&buf, buf_static, sizeof (buf_static));
1194 ret = FcPatternFormatToBuf (pat, format, &buf);
1197 FcPatternDestroy (alloced);
1200 return FcStrBufDone (&buf);
1203 FcStrBufDestroy (&buf);
1208 #define __fcformat__
1209 #include "fcaliastail.h"