]>
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 | * | |
3074a73b | 16 | * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
0c93b91d | 17 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
3074a73b | 18 | * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
0c93b91d BE |
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)); | |
f6d83439 | 740 | if (e || else_string) |
b6a23028 | 741 | { |
f6d83439 | 742 | FcValueListPtr l = NULL; |
b6a23028 BE |
743 | |
744 | if (add_colon) | |
745 | FcStrBufChar (buf, ':'); | |
746 | if (add_elt_name) | |
747 | { | |
748 | FcStrBufString (buf, c->word); | |
749 | FcStrBufChar (buf, '='); | |
750 | } | |
751 | ||
f6d83439 BE |
752 | if (e) |
753 | l = FcPatternEltValues(e); | |
9c83a837 BE |
754 | |
755 | if (idx != -1) | |
756 | { | |
757 | while (l && idx > 0) | |
758 | { | |
759 | l = FcValueListNext(l); | |
760 | idx--; | |
761 | } | |
762 | if (l && idx == 0) | |
763 | { | |
764 | if (!FcNameUnparseValue (buf, &l->value, '\0')) | |
765 | return FcFalse; | |
766 | } | |
767 | else goto notfound; | |
768 | } | |
f6d83439 | 769 | else if (l) |
9c83a837 BE |
770 | { |
771 | FcNameUnparseValueList (buf, l, '\0'); | |
772 | } | |
f6d83439 BE |
773 | else |
774 | { | |
775 | notfound: | |
776 | if (else_string) | |
777 | FcStrBufString (buf, else_string); | |
778 | } | |
b6a23028 BE |
779 | } |
780 | ||
781 | return FcTrue; | |
782 | } | |
783 | ||
85c7fb67 BE |
784 | static FcBool |
785 | cescape (FcFormatContext *c, | |
d04a7507 BE |
786 | const FcChar8 *str, |
787 | FcStrBuf *buf) | |
ced38254 | 788 | { |
ced38254 BE |
789 | while(*str) |
790 | { | |
791 | switch (*str) | |
792 | { | |
793 | case '\\': | |
794 | case '"': | |
85c7fb67 | 795 | FcStrBufChar (buf, '\\'); |
ced38254 BE |
796 | break; |
797 | } | |
85c7fb67 | 798 | FcStrBufChar (buf, *str++); |
ced38254 | 799 | } |
85c7fb67 | 800 | return FcTrue; |
ced38254 BE |
801 | } |
802 | ||
85c7fb67 BE |
803 | static FcBool |
804 | shescape (FcFormatContext *c, | |
d04a7507 BE |
805 | const FcChar8 *str, |
806 | FcStrBuf *buf) | |
ced38254 | 807 | { |
85c7fb67 | 808 | FcStrBufChar (buf, '\''); |
ced38254 BE |
809 | while(*str) |
810 | { | |
811 | if (*str == '\'') | |
85c7fb67 | 812 | FcStrBufString (buf, (const FcChar8 *) "'\\''"); |
ced38254 | 813 | else |
85c7fb67 | 814 | FcStrBufChar (buf, *str); |
ced38254 BE |
815 | str++; |
816 | } | |
85c7fb67 BE |
817 | FcStrBufChar (buf, '\''); |
818 | return FcTrue; | |
ced38254 BE |
819 | } |
820 | ||
85c7fb67 BE |
821 | static FcBool |
822 | xmlescape (FcFormatContext *c, | |
d04a7507 BE |
823 | const FcChar8 *str, |
824 | FcStrBuf *buf) | |
ced38254 | 825 | { |
ced38254 BE |
826 | while(*str) |
827 | { | |
828 | switch (*str) | |
829 | { | |
85c7fb67 BE |
830 | case '&': FcStrBufString (buf, (const FcChar8 *) "&"); break; |
831 | case '<': FcStrBufString (buf, (const FcChar8 *) "<"); break; | |
832 | case '>': FcStrBufString (buf, (const FcChar8 *) ">"); break; | |
833 | default: FcStrBufChar (buf, *str); break; | |
ced38254 BE |
834 | } |
835 | str++; | |
836 | } | |
85c7fb67 | 837 | return FcTrue; |
ced38254 BE |
838 | } |
839 | ||
85c7fb67 | 840 | static FcBool |
c8f5933d | 841 | delete_chars (FcFormatContext *c, |
d04a7507 BE |
842 | const FcChar8 *str, |
843 | FcStrBuf *buf) | |
c8f5933d | 844 | { |
c8f5933d BE |
845 | /* XXX not UTF-8 aware */ |
846 | ||
847 | if (!expect_char (c, '(') || | |
848 | !read_chars (c, ')') || | |
849 | !expect_char (c, ')')) | |
85c7fb67 | 850 | return FcFalse; |
c8f5933d | 851 | |
c8f5933d BE |
852 | while(*str) |
853 | { | |
854 | FcChar8 *p; | |
855 | ||
856 | p = (FcChar8 *) strpbrk ((const char *) str, (const char *) c->word); | |
857 | if (p) | |
858 | { | |
85c7fb67 | 859 | FcStrBufData (buf, str, p - str); |
c8f5933d BE |
860 | str = p + 1; |
861 | } | |
862 | else | |
863 | { | |
85c7fb67 | 864 | FcStrBufString (buf, str); |
c8f5933d BE |
865 | break; |
866 | } | |
867 | ||
868 | } | |
85c7fb67 BE |
869 | |
870 | return FcTrue; | |
c8f5933d BE |
871 | } |
872 | ||
85c7fb67 | 873 | static FcBool |
c8f5933d | 874 | escape_chars (FcFormatContext *c, |
d04a7507 BE |
875 | const FcChar8 *str, |
876 | FcStrBuf *buf) | |
c8f5933d | 877 | { |
c8f5933d BE |
878 | /* XXX not UTF-8 aware */ |
879 | ||
880 | if (!expect_char (c, '(') || | |
881 | !read_chars (c, ')') || | |
882 | !expect_char (c, ')')) | |
85c7fb67 | 883 | return FcFalse; |
c8f5933d | 884 | |
c8f5933d BE |
885 | while(*str) |
886 | { | |
887 | FcChar8 *p; | |
888 | ||
889 | p = (FcChar8 *) strpbrk ((const char *) str, (const char *) c->word); | |
890 | if (p) | |
891 | { | |
85c7fb67 BE |
892 | FcStrBufData (buf, str, p - str); |
893 | FcStrBufChar (buf, c->word[0]); | |
894 | FcStrBufChar (buf, *p); | |
c8f5933d BE |
895 | str = p + 1; |
896 | } | |
897 | else | |
898 | { | |
85c7fb67 | 899 | FcStrBufString (buf, str); |
c8f5933d BE |
900 | break; |
901 | } | |
902 | ||
903 | } | |
85c7fb67 BE |
904 | |
905 | return FcTrue; | |
c8f5933d BE |
906 | } |
907 | ||
85c7fb67 | 908 | static FcBool |
c8f5933d | 909 | translate_chars (FcFormatContext *c, |
d04a7507 BE |
910 | const FcChar8 *str, |
911 | FcStrBuf *buf) | |
c8f5933d | 912 | { |
c8f5933d BE |
913 | char *from, *to, repeat; |
914 | int from_len, to_len; | |
915 | ||
916 | /* XXX not UTF-8 aware */ | |
917 | ||
918 | if (!expect_char (c, '(') || | |
919 | !read_chars (c, ',') || | |
920 | !expect_char (c, ',')) | |
85c7fb67 | 921 | return FcFalse; |
c8f5933d BE |
922 | |
923 | from = (char *) c->word; | |
924 | from_len = strlen (from); | |
925 | to = from + from_len + 1; | |
926 | ||
0673ef38 | 927 | /* hack: we temporarily divert c->word */ |
c8f5933d BE |
928 | c->word = (FcChar8 *) to; |
929 | if (!read_chars (c, ')')) | |
930 | { | |
931 | c->word = (FcChar8 *) from; | |
932 | return FcFalse; | |
933 | } | |
934 | c->word = (FcChar8 *) from; | |
935 | ||
936 | to_len = strlen (to); | |
937 | repeat = to[to_len - 1]; | |
938 | ||
939 | if (!expect_char (c, ')')) | |
940 | return FcFalse; | |
941 | ||
c8f5933d BE |
942 | while(*str) |
943 | { | |
944 | FcChar8 *p; | |
945 | ||
946 | p = (FcChar8 *) strpbrk ((const char *) str, (const char *) from); | |
947 | if (p) | |
948 | { | |
949 | int i; | |
85c7fb67 | 950 | FcStrBufData (buf, str, p - str); |
c8f5933d | 951 | i = strchr (from, *p) - from; |
85c7fb67 | 952 | FcStrBufChar (buf, i < to_len ? to[i] : repeat); |
c8f5933d BE |
953 | str = p + 1; |
954 | } | |
955 | else | |
956 | { | |
85c7fb67 | 957 | FcStrBufString (buf, str); |
c8f5933d BE |
958 | break; |
959 | } | |
960 | ||
961 | } | |
85c7fb67 BE |
962 | |
963 | return FcTrue; | |
c8f5933d BE |
964 | } |
965 | ||
85c7fb67 BE |
966 | static FcBool |
967 | interpret_convert (FcFormatContext *c, | |
968 | FcStrBuf *buf, | |
969 | int start) | |
2017a5eb | 970 | { |
85c7fb67 BE |
971 | const FcChar8 *str; |
972 | FcChar8 *new_str; | |
973 | FcStrBuf new_buf; | |
974 | FcChar8 buf_static[8192]; | |
975 | FcBool ret; | |
976 | ||
d04a7507 BE |
977 | if (!expect_char (c, '|') || |
978 | !read_word (c)) | |
85c7fb67 BE |
979 | return FcFalse; |
980 | ||
d04a7507 | 981 | /* prepare the buffer */ |
85c7fb67 BE |
982 | FcStrBufChar (buf, '\0'); |
983 | if (buf->failed) | |
984 | return FcFalse; | |
985 | str = buf->buf + start; | |
986 | buf->len = start; | |
987 | ||
85c7fb67 BE |
988 | /* try simple converters first */ |
989 | if (0) { } | |
ced38254 BE |
990 | #define CONVERTER(name, func) \ |
991 | else if (0 == strcmp ((const char *) c->word, name))\ | |
85c7fb67 | 992 | do { new_str = func (str); ret = FcTrue; } while (0) |
c8f5933d BE |
993 | CONVERTER ("downcase", FcStrDowncase); |
994 | CONVERTER ("basename", FcStrBasename); | |
995 | CONVERTER ("dirname", FcStrDirname); | |
85c7fb67 BE |
996 | #undef CONVERTER |
997 | else | |
998 | ret = FcFalse; | |
999 | ||
1000 | if (ret) | |
1001 | { | |
1002 | if (new_str) | |
1003 | { | |
85c7fb67 BE |
1004 | FcStrBufString (buf, new_str); |
1005 | free (new_str); | |
1006 | return FcTrue; | |
1007 | } | |
1008 | else | |
1009 | return FcFalse; | |
1010 | } | |
1011 | ||
1012 | FcStrBufInit (&new_buf, buf_static, sizeof (buf_static)); | |
1013 | ||
1014 | /* now try our custom converters */ | |
1015 | if (0) { } | |
1016 | #define CONVERTER(name, func) \ | |
1017 | else if (0 == strcmp ((const char *) c->word, name))\ | |
d04a7507 | 1018 | ret = func (c, str, &new_buf) |
85c7fb67 BE |
1019 | CONVERTER ("cescape", cescape); |
1020 | CONVERTER ("shescape", shescape); | |
1021 | CONVERTER ("xmlescape", xmlescape); | |
1022 | CONVERTER ("delete", delete_chars); | |
1023 | CONVERTER ("escape", escape_chars); | |
1024 | CONVERTER ("translate", translate_chars); | |
1025 | #undef CONVERTER | |
1026 | else | |
1027 | ret = FcFalse; | |
1028 | ||
1029 | if (ret) | |
1030 | { | |
1031 | FcStrBufChar (&new_buf, '\0'); | |
1032 | FcStrBufString (buf, new_buf.buf); | |
1033 | } | |
1034 | else | |
1035 | message ("unknown converter \"%s\"", | |
1036 | c->word); | |
1037 | ||
1038 | FcStrBufDestroy (&new_buf); | |
1039 | ||
1040 | return ret; | |
2017a5eb BE |
1041 | } |
1042 | ||
1043 | static FcBool | |
1044 | maybe_interpret_converts (FcFormatContext *c, | |
1045 | FcStrBuf *buf, | |
1046 | int start) | |
1047 | { | |
85c7fb67 BE |
1048 | while (*c->format == '|') |
1049 | if (!interpret_convert (c, buf, start)) | |
2017a5eb BE |
1050 | return FcFalse; |
1051 | ||
2017a5eb BE |
1052 | return FcTrue; |
1053 | } | |
1054 | ||
1055 | static FcBool | |
1056 | align_to_width (FcStrBuf *buf, | |
1057 | int start, | |
1058 | int width) | |
1059 | { | |
1060 | int len; | |
1061 | ||
1062 | if (buf->failed) | |
1063 | return FcFalse; | |
1064 | ||
1065 | len = buf->len - start; | |
1066 | if (len < -width) | |
1067 | { | |
1068 | /* left align */ | |
1069 | while (len++ < -width) | |
1070 | FcStrBufChar (buf, ' '); | |
1071 | } | |
1072 | else if (len < width) | |
1073 | { | |
1074 | int old_len; | |
1075 | old_len = len; | |
1076 | /* right align */ | |
1077 | while (len++ < width) | |
1078 | FcStrBufChar (buf, ' '); | |
1079 | if (buf->failed) | |
1080 | return FcFalse; | |
1081 | len = old_len; | |
1082 | memmove (buf->buf + buf->len - len, | |
1083 | buf->buf + buf->len - width, | |
1084 | len); | |
1085 | memset (buf->buf + buf->len - width, | |
1086 | ' ', | |
1087 | width - len); | |
1088 | } | |
1089 | ||
1090 | return !buf->failed; | |
1091 | } | |
8c31a243 BE |
1092 | static FcBool |
1093 | interpret_percent (FcFormatContext *c, | |
1094 | FcPattern *pat, | |
1095 | FcStrBuf *buf) | |
1096 | { | |
2017a5eb BE |
1097 | int width, start; |
1098 | FcBool ret; | |
8c31a243 BE |
1099 | |
1100 | if (!expect_char (c, '%')) | |
1101 | return FcFalse; | |
1102 | ||
1103 | if (consume_char (c, '%')) /* "%%" */ | |
1104 | { | |
1105 | FcStrBufChar (buf, '%'); | |
1106 | return FcTrue; | |
1107 | } | |
1108 | ||
1109 | /* parse an optional width specifier */ | |
1110 | width = strtol ((const char *) c->format, (char **) &c->format, 10); | |
1111 | ||
8c31a243 BE |
1112 | if (!expect_char (c, '{')) |
1113 | return FcFalse; | |
1114 | ||
2017a5eb | 1115 | start = buf->len; |
7717b25f | 1116 | |
2017a5eb | 1117 | switch (*c->format) { |
d4f7a4c6 | 1118 | case '=': ret = interpret_builtin (c, pat, buf); break; |
2017a5eb BE |
1119 | case '{': ret = interpret_subexpr (c, pat, buf); break; |
1120 | case '+': ret = interpret_filter (c, pat, buf); break; | |
1121 | case '-': ret = interpret_delete (c, pat, buf); break; | |
1122 | case '?': ret = interpret_cond (c, pat, buf); break; | |
b6a23028 | 1123 | case '#': ret = interpret_count (c, pat, buf); break; |
cdfb7658 | 1124 | case '[': ret = interpret_array (c, pat, buf); break; |
2017a5eb | 1125 | default: ret = interpret_simple (c, pat, buf); break; |
0c93b91d | 1126 | } |
c493c3b7 | 1127 | |
2017a5eb BE |
1128 | return ret && |
1129 | maybe_interpret_converts (c, buf, start) && | |
1130 | align_to_width (buf, start, width) && | |
1131 | expect_char (c, '}'); | |
0c93b91d BE |
1132 | } |
1133 | ||
8c31a243 | 1134 | static FcBool |
7717b25f BE |
1135 | interpret_expr (FcFormatContext *c, |
1136 | FcPattern *pat, | |
1137 | FcStrBuf *buf, | |
1138 | FcChar8 term) | |
0c93b91d | 1139 | { |
7717b25f | 1140 | while (*c->format && *c->format != term) |
0c93b91d | 1141 | { |
27b3e2dd | 1142 | switch (*c->format) |
0c93b91d BE |
1143 | { |
1144 | case '\\': | |
27b3e2dd BE |
1145 | c->format++; /* skip over '\\' */ |
1146 | if (*c->format) | |
1147 | FcStrBufChar (buf, escaped_char (*c->format++)); | |
0c93b91d BE |
1148 | continue; |
1149 | case '%': | |
8c31a243 BE |
1150 | if (!interpret_percent (c, pat, buf)) |
1151 | return FcFalse; | |
0c93b91d BE |
1152 | continue; |
1153 | } | |
27b3e2dd | 1154 | FcStrBufChar (buf, *c->format++); |
0c93b91d | 1155 | } |
8c31a243 | 1156 | return FcTrue; |
0c93b91d BE |
1157 | } |
1158 | ||
d04a7507 BE |
1159 | static FcBool |
1160 | FcPatternFormatToBuf (FcPattern *pat, | |
1161 | const FcChar8 *format, | |
1162 | FcStrBuf *buf) | |
1163 | { | |
1164 | FcFormatContext c; | |
1165 | FcChar8 word_static[1024]; | |
1166 | FcBool ret; | |
1167 | ||
1168 | if (!FcFormatContextInit (&c, format, word_static, sizeof (word_static))) | |
1169 | return FcFalse; | |
1170 | ||
1171 | ret = interpret_expr (&c, pat, buf, '\0'); | |
1172 | ||
1173 | FcFormatContextDone (&c); | |
1174 | ||
1175 | return ret; | |
1176 | } | |
1177 | ||
0c93b91d | 1178 | FcChar8 * |
d04a7507 BE |
1179 | FcPatternFormat (FcPattern *pat, |
1180 | const FcChar8 *format) | |
0c93b91d | 1181 | { |
ced38254 | 1182 | FcStrBuf buf; |
85c7fb67 | 1183 | FcChar8 buf_static[8192 - 1024]; |
ced38254 | 1184 | FcBool ret; |
0c93b91d | 1185 | |
ced38254 | 1186 | FcStrBufInit (&buf, buf_static, sizeof (buf_static)); |
0c93b91d | 1187 | |
d04a7507 | 1188 | ret = FcPatternFormatToBuf (pat, format, &buf); |
0c93b91d | 1189 | |
8c31a243 BE |
1190 | if (ret) |
1191 | return FcStrBufDone (&buf); | |
1192 | else | |
1193 | { | |
1194 | FcStrBufDestroy (&buf); | |
1195 | return NULL; | |
1196 | } | |
0c93b91d BE |
1197 | } |
1198 | ||
1199 | #define __fcformat__ | |
1200 | #include "fcaliastail.h" | |
1201 | #undef __fcformat__ |