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