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