]> git.wh0rd.org - fontconfig.git/blob - src/fcformat.c
delete unused variables
[fontconfig.git] / src / fcformat.c
1 /*
2 * Copyright © 2008,2009 Red Hat, Inc.
3 *
4 * Red Hat Author(s): Behdad Esfahbod
5 *
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.
15 *
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.
23 */
24
25 #include "fcint.h"
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdarg.h>
29
30
31 /* The language is documented in doc/fcformat.fncs
32 * These are the features implemented:
33 *
34 * simple %{elt}
35 * width %width{elt}
36 * index %{elt[idx]}
37 * name= %{elt=}
38 * :name= %{:elt}
39 * default %{elt:-word}
40 * count %{#elt}
41 * subexpr %{{expr}}
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
47 * builtin %{=blt}
48 * convert %{elt|conv1|conv2|conv3}
49 *
50 * converters:
51 * basename FcStrBasename
52 * dirname FcStrDirname
53 * downcase FcStrDowncase
54 * shescape
55 * cescape
56 * xmlescape
57 * delete delete chars
58 * escape escape chars
59 * translate translate chars
60 *
61 * builtins:
62 * unparse FcNameUnparse
63 * fcmatch fc-match default
64 * fclist fc-list default
65 * fccat fc-cat default
66 * pkgkit PackageKit package tag format
67 *
68 *
69 * Some ideas for future syntax extensions:
70 *
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)
75 */
76
77
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}}"
82
83
84 static void
85 message (const char *fmt, ...)
86 {
87 va_list args;
88 va_start (args, fmt);
89 fprintf (stderr, "Fontconfig: Pattern format error: ");
90 vfprintf (stderr, fmt, args);
91 fprintf (stderr, ".\n");
92 va_end (args);
93 }
94
95
96 typedef struct _FcFormatContext
97 {
98 const FcChar8 *format_orig;
99 const FcChar8 *format;
100 int format_len;
101 FcChar8 *word;
102 FcBool word_allocated;
103 } FcFormatContext;
104
105 static FcBool
106 FcFormatContextInit (FcFormatContext *c,
107 const FcChar8 *format,
108 FcChar8 *scratch,
109 int scratch_len)
110 {
111 c->format_orig = c->format = format;
112 c->format_len = strlen ((const char *) format);
113
114 if (c->format_len < scratch_len)
115 {
116 c->word = scratch;
117 c->word_allocated = FcFalse;
118 }
119 else
120 {
121 c->word = malloc (c->format_len + 1);
122 c->word_allocated = FcTrue;
123 }
124
125 return c->word != NULL;
126 }
127
128 static void
129 FcFormatContextDone (FcFormatContext *c)
130 {
131 if (c && c->word_allocated)
132 {
133 free (c->word);
134 }
135 }
136
137 static FcBool
138 consume_char (FcFormatContext *c,
139 FcChar8 term)
140 {
141 if (*c->format != term)
142 return FcFalse;
143
144 c->format++;
145 return FcTrue;
146 }
147
148 static FcBool
149 expect_char (FcFormatContext *c,
150 FcChar8 term)
151 {
152 FcBool res = consume_char (c, term);
153 if (!res)
154 {
155 if (c->format == c->format_orig + c->format_len)
156 message ("format ended while expecting '%c'",
157 term);
158 else
159 message ("expected '%c' at %d",
160 term, c->format - c->format_orig + 1);
161 }
162 return res;
163 }
164
165 static FcBool
166 FcCharIsPunct (const FcChar8 c)
167 {
168 if (c < '0')
169 return FcTrue;
170 if (c <= '9')
171 return FcFalse;
172 if (c < 'A')
173 return FcTrue;
174 if (c <= 'Z')
175 return FcFalse;
176 if (c < 'a')
177 return FcTrue;
178 if (c <= 'z')
179 return FcFalse;
180 if (c <= '~')
181 return FcTrue;
182 return FcFalse;
183 }
184
185 static char escaped_char(const char ch)
186 {
187 switch (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';
195 default: return ch;
196 }
197 }
198
199 static FcBool
200 read_word (FcFormatContext *c)
201 {
202 FcChar8 *p;
203
204 p = c->word;
205
206 while (*c->format)
207 {
208 if (*c->format == '\\')
209 {
210 c->format++;
211 if (*c->format)
212 *p++ = escaped_char (*c->format++);
213 continue;
214 }
215 else if (FcCharIsPunct (*c->format))
216 break;
217
218 *p++ = *c->format++;
219 }
220 *p = '\0';
221
222 if (p == c->word)
223 {
224 message ("expected identifier at %d",
225 c->format - c->format_orig + 1);
226 return FcFalse;
227 }
228
229 return FcTrue;
230 }
231
232 static FcBool
233 read_chars (FcFormatContext *c,
234 FcChar8 term)
235 {
236 FcChar8 *p;
237
238 p = c->word;
239
240 while (*c->format && *c->format != '}' && *c->format != term)
241 {
242 if (*c->format == '\\')
243 {
244 c->format++;
245 if (*c->format)
246 *p++ = escaped_char (*c->format++);
247 continue;
248 }
249
250 *p++ = *c->format++;
251 }
252 *p = '\0';
253
254 if (p == c->word)
255 {
256 message ("expected character data at %d",
257 c->format - c->format_orig + 1);
258 return FcFalse;
259 }
260
261 return FcTrue;
262 }
263
264 static FcBool
265 FcPatternFormatToBuf (FcPattern *pat,
266 const FcChar8 *format,
267 FcStrBuf *buf);
268
269 static FcBool
270 interpret_builtin (FcFormatContext *c,
271 FcPattern *pat,
272 FcStrBuf *buf)
273 {
274 FcChar8 *new_str;
275 FcBool ret;
276
277 if (!expect_char (c, '=') ||
278 !read_word (c))
279 return FcFalse;
280
281 /* try simple builtins first */
282 if (0) { }
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 */
288 #undef BUILTIN
289 else
290 ret = FcFalse;
291
292 if (ret)
293 {
294 if (new_str)
295 {
296 FcStrBufString (buf, new_str);
297 free (new_str);
298 return FcTrue;
299 }
300 else
301 return FcFalse;
302 }
303
304 /* now try our custom formats */
305 if (0) { }
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);
313 #undef BUILTIN
314 else
315 ret = FcFalse;
316
317 if (!ret)
318 message ("unknown builtin \"%s\"",
319 c->word);
320
321 return ret;
322 }
323
324 static FcBool
325 interpret_expr (FcFormatContext *c,
326 FcPattern *pat,
327 FcStrBuf *buf,
328 FcChar8 term);
329
330 static FcBool
331 interpret_subexpr (FcFormatContext *c,
332 FcPattern *pat,
333 FcStrBuf *buf)
334 {
335 return expect_char (c, '{') &&
336 interpret_expr (c, pat, buf, '}') &&
337 expect_char (c, '}');
338 }
339
340 static FcBool
341 maybe_interpret_subexpr (FcFormatContext *c,
342 FcPattern *pat,
343 FcStrBuf *buf)
344 {
345 return (*c->format == '{') ?
346 interpret_subexpr (c, pat, buf) :
347 FcTrue;
348 }
349
350 static FcBool
351 skip_subexpr (FcFormatContext *c);
352
353 static FcBool
354 skip_percent (FcFormatContext *c)
355 {
356 if (!expect_char (c, '%'))
357 return FcFalse;
358
359 /* skip an optional width specifier */
360 strtol ((const char *) c->format, (char **) &c->format, 10);
361
362 if (!expect_char (c, '{'))
363 return FcFalse;
364
365 while(*c->format && *c->format != '}')
366 {
367 switch (*c->format)
368 {
369 case '\\':
370 c->format++; /* skip over '\\' */
371 if (*c->format)
372 c->format++;
373 continue;
374 case '{':
375 if (!skip_subexpr (c))
376 return FcFalse;
377 continue;
378 }
379 c->format++;
380 }
381
382 return expect_char (c, '}');
383 }
384
385 static FcBool
386 skip_expr (FcFormatContext *c)
387 {
388 while(*c->format && *c->format != '}')
389 {
390 switch (*c->format)
391 {
392 case '\\':
393 c->format++; /* skip over '\\' */
394 if (*c->format)
395 c->format++;
396 continue;
397 case '%':
398 if (!skip_percent (c))
399 return FcFalse;
400 continue;
401 }
402 c->format++;
403 }
404
405 return FcTrue;
406 }
407
408 static FcBool
409 skip_subexpr (FcFormatContext *c)
410 {
411 return expect_char (c, '{') &&
412 skip_expr (c) &&
413 expect_char (c, '}');
414 }
415
416 static FcBool
417 maybe_skip_subexpr (FcFormatContext *c)
418 {
419 return (*c->format == '{') ?
420 skip_subexpr (c) :
421 FcTrue;
422 }
423
424 static FcBool
425 interpret_filter_in (FcFormatContext *c,
426 FcPattern *pat,
427 FcStrBuf *buf)
428 {
429 FcObjectSet *os;
430 FcPattern *subpat;
431
432 if (!expect_char (c, '+'))
433 return FcFalse;
434
435 os = FcObjectSetCreate ();
436 if (!os)
437 return FcFalse;
438
439 do
440 {
441 /* XXX binding */
442 if (!read_word (c) ||
443 !FcObjectSetAdd (os, (const char *) c->word))
444 {
445 FcObjectSetDestroy (os);
446 return FcFalse;
447 }
448 }
449 while (consume_char (c, ','));
450
451 subpat = FcPatternFilter (pat, os);
452 FcObjectSetDestroy (os);
453
454 if (!subpat ||
455 !interpret_subexpr (c, subpat, buf))
456 return FcFalse;
457
458 FcPatternDestroy (subpat);
459 return FcTrue;
460 }
461
462 static FcBool
463 interpret_filter_out (FcFormatContext *c,
464 FcPattern *pat,
465 FcStrBuf *buf)
466 {
467 FcPattern *subpat;
468
469 if (!expect_char (c, '-'))
470 return FcFalse;
471
472 subpat = FcPatternDuplicate (pat);
473 if (!subpat)
474 return FcFalse;
475
476 do
477 {
478 if (!read_word (c))
479 {
480 FcPatternDestroy (subpat);
481 return FcFalse;
482 }
483
484 FcPatternDel (subpat, (const char *) c->word);
485 }
486 while (consume_char (c, ','));
487
488 if (!interpret_subexpr (c, subpat, buf))
489 return FcFalse;
490
491 FcPatternDestroy (subpat);
492 return FcTrue;
493 }
494
495 static FcBool
496 interpret_cond (FcFormatContext *c,
497 FcPattern *pat,
498 FcStrBuf *buf)
499 {
500 FcBool pass;
501
502 if (!expect_char (c, '?'))
503 return FcFalse;
504
505 pass = FcTrue;
506
507 do
508 {
509 FcBool negate;
510 FcValue v;
511
512 negate = consume_char (c, '!');
513
514 if (!read_word (c))
515 return FcFalse;
516
517 pass = pass &&
518 (negate ^
519 (FcResultMatch ==
520 FcPatternGet (pat, (const char *) c->word, 0, &v)));
521 }
522 while (consume_char (c, ','));
523
524 if (pass)
525 {
526 if (!interpret_subexpr (c, pat, buf) ||
527 !maybe_skip_subexpr (c))
528 return FcFalse;
529 }
530 else
531 {
532 if (!skip_subexpr (c) ||
533 !maybe_interpret_subexpr (c, pat, buf))
534 return FcFalse;
535 }
536
537 return FcTrue;
538 }
539
540 static FcBool
541 interpret_count (FcFormatContext *c,
542 FcPattern *pat,
543 FcStrBuf *buf)
544 {
545 int count;
546 FcPatternElt *e;
547 FcChar8 buf_static[64];
548
549 if (!expect_char (c, '#'))
550 return FcFalse;
551
552 if (!read_word (c))
553 return FcFalse;
554
555 count = 0;
556 e = FcPatternObjectFindElt (pat,
557 FcObjectFromName ((const char *) c->word));
558 if (e)
559 {
560 FcValueListPtr l;
561 count++;
562 for (l = FcPatternEltValues(e);
563 l->next;
564 l = l->next)
565 count++;
566 }
567
568 snprintf ((char *) buf_static, sizeof (buf_static), "%d", count);
569 FcStrBufString (buf, buf_static);
570
571 return FcTrue;
572 }
573
574 static FcBool
575 interpret_enumerate (FcFormatContext *c,
576 FcPattern *pat,
577 FcStrBuf *buf)
578 {
579 FcObjectSet *os;
580 FcPattern *subpat;
581 const FcChar8 *format_save;
582 int idx;
583 FcBool ret, done;
584 FcStrList *lang_strs;
585
586 if (!expect_char (c, '[') ||
587 !expect_char (c, ']'))
588 return FcFalse;
589
590 os = FcObjectSetCreate ();
591 if (!os)
592 return FcFalse;
593
594 ret = FcTrue;
595
596 do
597 {
598 if (!read_word (c) ||
599 !FcObjectSetAdd (os, (const char *) c->word))
600 {
601 FcObjectSetDestroy (os);
602 return FcFalse;
603 }
604 }
605 while (consume_char (c, ','));
606
607 /* If we have one element and it's of type FcLangSet, we want
608 * to enumerate the languages in it. */
609 lang_strs = NULL;
610 if (os->nobject == 1)
611 {
612 FcLangSet *langset;
613 if (FcResultMatch ==
614 FcPatternGetLangSet (pat, os->objects[0], 0, &langset))
615 {
616 FcStrSet *ss;
617 if (!(ss = FcLangSetGetLangs (langset)) ||
618 !(lang_strs = FcStrListCreate (ss)))
619 goto bail0;
620 }
621 }
622
623 subpat = FcPatternDuplicate (pat);
624 if (!subpat)
625 goto bail0;
626
627 format_save = c->format;
628 idx = 0;
629 do
630 {
631 int i;
632
633 done = FcTrue;
634
635 if (lang_strs)
636 {
637 FcChar8 *lang;
638
639 FcPatternDel (subpat, os->objects[0]);
640 if ((lang = FcStrListNext (lang_strs)))
641 {
642 /* XXX binding? */
643 FcPatternAddString (subpat, os->objects[0], lang);
644 done = FcFalse;
645 }
646 }
647 else
648 {
649 for (i = 0; i < os->nobject; i++)
650 {
651 FcValue v;
652
653 /* XXX this can be optimized by accessing valuelist linked lists
654 * directly and remembering where we were. Most (all) value lists
655 * in normal uses are pretty short though (language tags are
656 * stored as a LangSet, not separate values.). */
657 FcPatternDel (subpat, os->objects[i]);
658 if (FcResultMatch ==
659 FcPatternGet (pat, os->objects[i], idx, &v))
660 {
661 /* XXX binding */
662 FcPatternAdd (subpat, os->objects[i], v, FcFalse);
663 done = FcFalse;
664 }
665 }
666 }
667
668 if (!done)
669 {
670 c->format = format_save;
671 ret = interpret_subexpr (c, subpat, buf);
672 if (!ret)
673 goto bail;
674 }
675
676 idx++;
677 } while (!done);
678
679 if (c->format == format_save)
680 skip_subexpr (c);
681
682 bail:
683 FcPatternDestroy (subpat);
684 bail0:
685 if (lang_strs)
686 FcStrListDone (lang_strs);
687 FcObjectSetDestroy (os);
688
689 return ret;
690 }
691
692 static FcBool
693 interpret_simple (FcFormatContext *c,
694 FcPattern *pat,
695 FcStrBuf *buf)
696 {
697 FcPatternElt *e;
698 FcBool add_colon = FcFalse;
699 FcBool add_elt_name = FcFalse;
700 int idx;
701 FcChar8 *else_string;
702
703 if (consume_char (c, ':'))
704 add_colon = FcTrue;
705
706 if (!read_word (c))
707 return FcFalse;
708
709 idx = -1;
710 if (consume_char (c, '['))
711 {
712 idx = strtol ((const char *) c->format, (char **) &c->format, 10);
713 if (idx < 0)
714 {
715 message ("expected non-negative number at %d",
716 c->format-1 - c->format_orig + 1);
717 return FcFalse;
718 }
719 if (!expect_char (c, ']'))
720 return FcFalse;
721 }
722
723 if (consume_char (c, '='))
724 add_elt_name = FcTrue;
725
726 /* modifiers */
727 else_string = NULL;
728 if (consume_char (c, ':'))
729 {
730 FcChar8 *orig;
731 /* divert the c->word for now */
732 orig = c->word;
733 c->word = c->word + strlen ((const char *) c->word) + 1;
734 /* for now we just support 'default value' */
735 if (!expect_char (c, '-') ||
736 !read_chars (c, '|'))
737 {
738 c->word = orig;
739 return FcFalse;
740 }
741 else_string = c->word;
742 c->word = orig;
743 }
744
745 e = FcPatternObjectFindElt (pat,
746 FcObjectFromName ((const char *) c->word));
747 if (e || else_string)
748 {
749 FcValueListPtr l = NULL;
750
751 if (add_colon)
752 FcStrBufChar (buf, ':');
753 if (add_elt_name)
754 {
755 FcStrBufString (buf, c->word);
756 FcStrBufChar (buf, '=');
757 }
758
759 if (e)
760 l = FcPatternEltValues(e);
761
762 if (idx != -1)
763 {
764 while (l && idx > 0)
765 {
766 l = FcValueListNext(l);
767 idx--;
768 }
769 if (l && idx == 0)
770 {
771 if (!FcNameUnparseValue (buf, &l->value, '\0'))
772 return FcFalse;
773 }
774 else goto notfound;
775 }
776 else if (l)
777 {
778 FcNameUnparseValueList (buf, l, '\0');
779 }
780 else
781 {
782 notfound:
783 if (else_string)
784 FcStrBufString (buf, else_string);
785 }
786 }
787
788 return FcTrue;
789 }
790
791 static FcBool
792 cescape (FcFormatContext *c,
793 const FcChar8 *str,
794 FcStrBuf *buf)
795 {
796 /* XXX escape \n etc? */
797
798 while(*str)
799 {
800 switch (*str)
801 {
802 case '\\':
803 case '"':
804 FcStrBufChar (buf, '\\');
805 break;
806 }
807 FcStrBufChar (buf, *str++);
808 }
809 return FcTrue;
810 }
811
812 static FcBool
813 shescape (FcFormatContext *c,
814 const FcChar8 *str,
815 FcStrBuf *buf)
816 {
817 FcStrBufChar (buf, '\'');
818 while(*str)
819 {
820 if (*str == '\'')
821 FcStrBufString (buf, (const FcChar8 *) "'\\''");
822 else
823 FcStrBufChar (buf, *str);
824 str++;
825 }
826 FcStrBufChar (buf, '\'');
827 return FcTrue;
828 }
829
830 static FcBool
831 xmlescape (FcFormatContext *c,
832 const FcChar8 *str,
833 FcStrBuf *buf)
834 {
835 /* XXX escape \n etc? */
836
837 while(*str)
838 {
839 switch (*str)
840 {
841 case '&': FcStrBufString (buf, (const FcChar8 *) "&amp;"); break;
842 case '<': FcStrBufString (buf, (const FcChar8 *) "&lt;"); break;
843 case '>': FcStrBufString (buf, (const FcChar8 *) "&gt;"); break;
844 default: FcStrBufChar (buf, *str); break;
845 }
846 str++;
847 }
848 return FcTrue;
849 }
850
851 static FcBool
852 delete_chars (FcFormatContext *c,
853 const FcChar8 *str,
854 FcStrBuf *buf)
855 {
856 /* XXX not UTF-8 aware */
857
858 if (!expect_char (c, '(') ||
859 !read_chars (c, ')') ||
860 !expect_char (c, ')'))
861 return FcFalse;
862
863 while(*str)
864 {
865 FcChar8 *p;
866
867 p = (FcChar8 *) strpbrk ((const char *) str, (const char *) c->word);
868 if (p)
869 {
870 FcStrBufData (buf, str, p - str);
871 str = p + 1;
872 }
873 else
874 {
875 FcStrBufString (buf, str);
876 break;
877 }
878
879 }
880
881 return FcTrue;
882 }
883
884 static FcBool
885 escape_chars (FcFormatContext *c,
886 const FcChar8 *str,
887 FcStrBuf *buf)
888 {
889 /* XXX not UTF-8 aware */
890
891 if (!expect_char (c, '(') ||
892 !read_chars (c, ')') ||
893 !expect_char (c, ')'))
894 return FcFalse;
895
896 while(*str)
897 {
898 FcChar8 *p;
899
900 p = (FcChar8 *) strpbrk ((const char *) str, (const char *) c->word);
901 if (p)
902 {
903 FcStrBufData (buf, str, p - str);
904 FcStrBufChar (buf, c->word[0]);
905 FcStrBufChar (buf, *p);
906 str = p + 1;
907 }
908 else
909 {
910 FcStrBufString (buf, str);
911 break;
912 }
913
914 }
915
916 return FcTrue;
917 }
918
919 static FcBool
920 translate_chars (FcFormatContext *c,
921 const FcChar8 *str,
922 FcStrBuf *buf)
923 {
924 char *from, *to, repeat;
925 int from_len, to_len;
926
927 /* XXX not UTF-8 aware */
928
929 if (!expect_char (c, '(') ||
930 !read_chars (c, ',') ||
931 !expect_char (c, ','))
932 return FcFalse;
933
934 from = (char *) c->word;
935 from_len = strlen (from);
936 to = from + from_len + 1;
937
938 /* hack: we temporarily divert c->word */
939 c->word = (FcChar8 *) to;
940 if (!read_chars (c, ')'))
941 {
942 c->word = (FcChar8 *) from;
943 return FcFalse;
944 }
945 c->word = (FcChar8 *) from;
946
947 to_len = strlen (to);
948 repeat = to[to_len - 1];
949
950 if (!expect_char (c, ')'))
951 return FcFalse;
952
953 while(*str)
954 {
955 FcChar8 *p;
956
957 p = (FcChar8 *) strpbrk ((const char *) str, (const char *) from);
958 if (p)
959 {
960 int i;
961 FcStrBufData (buf, str, p - str);
962 i = strchr (from, *p) - from;
963 FcStrBufChar (buf, i < to_len ? to[i] : repeat);
964 str = p + 1;
965 }
966 else
967 {
968 FcStrBufString (buf, str);
969 break;
970 }
971
972 }
973
974 return FcTrue;
975 }
976
977 static FcBool
978 interpret_convert (FcFormatContext *c,
979 FcStrBuf *buf,
980 int start)
981 {
982 const FcChar8 *str;
983 FcChar8 *new_str;
984 FcStrBuf new_buf;
985 FcChar8 buf_static[8192];
986 FcBool ret;
987
988 if (!expect_char (c, '|') ||
989 !read_word (c))
990 return FcFalse;
991
992 /* prepare the buffer */
993 FcStrBufChar (buf, '\0');
994 if (buf->failed)
995 return FcFalse;
996 str = buf->buf + start;
997 buf->len = start;
998
999 /* try simple converters first */
1000 if (0) { }
1001 #define CONVERTER(name, func) \
1002 else if (0 == strcmp ((const char *) c->word, name))\
1003 do { new_str = func (str); ret = FcTrue; } while (0)
1004 CONVERTER ("downcase", FcStrDowncase);
1005 CONVERTER ("basename", FcStrBasename);
1006 CONVERTER ("dirname", FcStrDirname);
1007 #undef CONVERTER
1008 else
1009 ret = FcFalse;
1010
1011 if (ret)
1012 {
1013 if (new_str)
1014 {
1015 FcStrBufString (buf, new_str);
1016 free (new_str);
1017 return FcTrue;
1018 }
1019 else
1020 return FcFalse;
1021 }
1022
1023 FcStrBufInit (&new_buf, buf_static, sizeof (buf_static));
1024
1025 /* now try our custom converters */
1026 if (0) { }
1027 #define CONVERTER(name, func) \
1028 else if (0 == strcmp ((const char *) c->word, name))\
1029 ret = func (c, str, &new_buf)
1030 CONVERTER ("cescape", cescape);
1031 CONVERTER ("shescape", shescape);
1032 CONVERTER ("xmlescape", xmlescape);
1033 CONVERTER ("delete", delete_chars);
1034 CONVERTER ("escape", escape_chars);
1035 CONVERTER ("translate", translate_chars);
1036 #undef CONVERTER
1037 else
1038 ret = FcFalse;
1039
1040 if (ret)
1041 {
1042 FcStrBufChar (&new_buf, '\0');
1043 FcStrBufString (buf, new_buf.buf);
1044 }
1045 else
1046 message ("unknown converter \"%s\"",
1047 c->word);
1048
1049 FcStrBufDestroy (&new_buf);
1050
1051 return ret;
1052 }
1053
1054 static FcBool
1055 maybe_interpret_converts (FcFormatContext *c,
1056 FcStrBuf *buf,
1057 int start)
1058 {
1059 while (*c->format == '|')
1060 if (!interpret_convert (c, buf, start))
1061 return FcFalse;
1062
1063 return FcTrue;
1064 }
1065
1066 static FcBool
1067 align_to_width (FcStrBuf *buf,
1068 int start,
1069 int width)
1070 {
1071 int len;
1072
1073 if (buf->failed)
1074 return FcFalse;
1075
1076 len = buf->len - start;
1077 if (len < -width)
1078 {
1079 /* left align */
1080 while (len++ < -width)
1081 FcStrBufChar (buf, ' ');
1082 }
1083 else if (len < width)
1084 {
1085 int old_len;
1086 old_len = len;
1087 /* right align */
1088 while (len++ < width)
1089 FcStrBufChar (buf, ' ');
1090 if (buf->failed)
1091 return FcFalse;
1092 len = old_len;
1093 memmove (buf->buf + buf->len - len,
1094 buf->buf + buf->len - width,
1095 len);
1096 memset (buf->buf + buf->len - width,
1097 ' ',
1098 width - len);
1099 }
1100
1101 return !buf->failed;
1102 }
1103 static FcBool
1104 interpret_percent (FcFormatContext *c,
1105 FcPattern *pat,
1106 FcStrBuf *buf)
1107 {
1108 int width, start;
1109 FcBool ret;
1110
1111 if (!expect_char (c, '%'))
1112 return FcFalse;
1113
1114 if (consume_char (c, '%')) /* "%%" */
1115 {
1116 FcStrBufChar (buf, '%');
1117 return FcTrue;
1118 }
1119
1120 /* parse an optional width specifier */
1121 width = strtol ((const char *) c->format, (char **) &c->format, 10);
1122
1123 if (!expect_char (c, '{'))
1124 return FcFalse;
1125
1126 start = buf->len;
1127
1128 switch (*c->format) {
1129 case '=': ret = interpret_builtin (c, pat, buf); break;
1130 case '{': ret = interpret_subexpr (c, pat, buf); break;
1131 case '+': ret = interpret_filter_in (c, pat, buf); break;
1132 case '-': ret = interpret_filter_out (c, pat, buf); break;
1133 case '?': ret = interpret_cond (c, pat, buf); break;
1134 case '#': ret = interpret_count (c, pat, buf); break;
1135 case '[': ret = interpret_enumerate (c, pat, buf); break;
1136 default: ret = interpret_simple (c, pat, buf); break;
1137 }
1138
1139 return ret &&
1140 maybe_interpret_converts (c, buf, start) &&
1141 align_to_width (buf, start, width) &&
1142 expect_char (c, '}');
1143 }
1144
1145 static FcBool
1146 interpret_expr (FcFormatContext *c,
1147 FcPattern *pat,
1148 FcStrBuf *buf,
1149 FcChar8 term)
1150 {
1151 while (*c->format && *c->format != term)
1152 {
1153 switch (*c->format)
1154 {
1155 case '\\':
1156 c->format++; /* skip over '\\' */
1157 if (*c->format)
1158 FcStrBufChar (buf, escaped_char (*c->format++));
1159 continue;
1160 case '%':
1161 if (!interpret_percent (c, pat, buf))
1162 return FcFalse;
1163 continue;
1164 }
1165 FcStrBufChar (buf, *c->format++);
1166 }
1167 return FcTrue;
1168 }
1169
1170 static FcBool
1171 FcPatternFormatToBuf (FcPattern *pat,
1172 const FcChar8 *format,
1173 FcStrBuf *buf)
1174 {
1175 FcFormatContext c;
1176 FcChar8 word_static[1024];
1177 FcBool ret;
1178
1179 if (!FcFormatContextInit (&c, format, word_static, sizeof (word_static)))
1180 return FcFalse;
1181
1182 ret = interpret_expr (&c, pat, buf, '\0');
1183
1184 FcFormatContextDone (&c);
1185
1186 return ret;
1187 }
1188
1189 FcChar8 *
1190 FcPatternFormat (FcPattern *pat,
1191 const FcChar8 *format)
1192 {
1193 FcStrBuf buf;
1194 FcChar8 buf_static[8192 - 1024];
1195 FcBool ret;
1196
1197 FcStrBufInit (&buf, buf_static, sizeof (buf_static));
1198
1199 ret = FcPatternFormatToBuf (pat, format, &buf);
1200
1201 if (ret)
1202 return FcStrBufDone (&buf);
1203 else
1204 {
1205 FcStrBufDestroy (&buf);
1206 return NULL;
1207 }
1208 }
1209
1210 #define __fcformat__
1211 #include "fcaliastail.h"
1212 #undef __fcformat__