compose: add xkbcommon-compose - implementation Signed-off-by: Ran Benita <ran234@gmail.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
diff --git a/Makefile.am b/Makefile.am
index 0bae4ea..0f713ee 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -14,6 +14,7 @@ EXTRA_DIST = \
AM_CPPFLAGS = \
-DDFLT_XKB_CONFIG_ROOT='"$(XKBCONFIGROOT)"' \
+ -DXLOCALEDIR='"$(XLOCALEDIR)"' \
-I$(top_srcdir)/src \
-I$(top_srcdir)/src/xkbcomp \
-I$(top_builddir)/src/xkbcomp \
@@ -32,11 +33,19 @@ xkbcommonincludedir = $(includedir)/xkbcommon
xkbcommoninclude_HEADERS = \
xkbcommon/xkbcommon.h \
xkbcommon/xkbcommon-compat.h \
+ xkbcommon/xkbcommon-compose.h \
xkbcommon/xkbcommon-keysyms.h \
xkbcommon/xkbcommon-names.h
lib_LTLIBRARIES = libxkbcommon.la
libxkbcommon_la_SOURCES = \
+ src/compose/parser.c \
+ src/compose/parser.h \
+ src/compose/paths.c \
+ src/compose/paths.h \
+ src/compose/state.c \
+ src/compose/table.c \
+ src/compose/table.h \
src/xkbcomp/action.c \
src/xkbcomp/action.h \
src/xkbcomp/ast.h \
diff --git a/configure.ac b/configure.ac
index 791e158..7449954 100644
--- a/configure.ac
+++ b/configure.ac
@@ -107,6 +107,14 @@ AC_ARG_WITH([xkb_config_root],
[XKBCONFIGROOT="$xkb_base"])
AC_SUBST([XKBCONFIGROOT])
+# Define a configuration option for the X locale directory for compose
+AC_ARG_WITH([x_locale_root],
+ [AS_HELP_STRING([--with-x-locale-root=<path>],
+ [Set X locale root (default: $datadir/X11/locale)])],
+ [XLOCALEDIR="$withval"],
+ [XLOCALEDIR="$datadir/X11/locale"])
+AC_SUBST([XLOCALEDIR])
+
AC_ARG_WITH([default_rules],
[AS_HELP_STRING([--with-default-rules=<path>],
[Default XKB ruleset (default: evdev)])],
@@ -189,4 +197,5 @@ AC_MSG_RESULT([
includedir: ${includedir}
lib dir: ${libdir}
XKB config root: ${XKBCONFIGROOT}
+ X11 locale root: ${XLOCALEDIR}
])
diff --git a/src/compose/parser.c b/src/compose/parser.c
new file mode 100644
index 0000000..29229f3
--- /dev/null
+++ b/src/compose/parser.c
@@ -0,0 +1,634 @@
+/*
+ * Copyright © 2013 Ran Benita <ran234@gmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include <errno.h>
+
+#include "utils.h"
+#include "scanner-utils.h"
+#include "table.h"
+#include "paths.h"
+#include "utf8.h"
+#include "parser.h"
+
+#define MAX_LHS_LEN 10
+#define MAX_INCLUDE_DEPTH 5
+
+#define KEYSYM_FROM_NAME_CACHE_SIZE 8
+
+/*
+ * xkb_keysym_from_name() is fairly slow, because for internal reasons
+ * it must use strcasecmp().
+ * A small cache reduces about 20% from the compilation time of
+ * en_US.UTF-8/Compose.
+ */
+struct keysym_from_name_cache {
+ struct {
+ char name[64];
+ xkb_keysym_t keysym;
+ } cache[KEYSYM_FROM_NAME_CACHE_SIZE];
+ unsigned next;
+};
+
+static xkb_keysym_t
+cached_keysym_from_name(struct keysym_from_name_cache *cache,
+ const char *name, size_t len)
+{
+ xkb_keysym_t keysym;
+
+ if (len >= sizeof(cache->cache[0].name))
+ return XKB_KEY_NoSymbol;
+
+ for (unsigned i = 0; i < KEYSYM_FROM_NAME_CACHE_SIZE; i++)
+ if (streq(cache->cache[i].name, name))
+ return cache->cache[i].keysym;
+
+ keysym = xkb_keysym_from_name(name, XKB_KEYSYM_NO_FLAGS);
+ strcpy(cache->cache[cache->next].name, name);
+ cache->cache[cache->next].keysym = keysym;
+ cache->next = (cache->next + 1) % KEYSYM_FROM_NAME_CACHE_SIZE;
+ return keysym;
+}
+
+/*
+ * Grammar adapted from libX11/modules/im/ximcp/imLcPrs.c.
+ * See also the XCompose(5) manpage.
+ *
+ * We don't support the MODIFIER rules, which are commented out.
+ *
+ * FILE ::= { [PRODUCTION] [COMMENT] "\n" | INCLUDE }
+ * INCLUDE ::= "include" '"' INCLUDE_STRING '"'
+ * PRODUCTION ::= LHS ":" RHS [ COMMENT ]
+ * COMMENT ::= "#" {<any character except null or newline>}
+ * LHS ::= EVENT { EVENT }
+ * EVENT ::= "<" keysym ">"
+ * # EVENT ::= [MODIFIER_LIST] "<" keysym ">"
+ * # MODIFIER_LIST ::= ("!" {MODIFIER} ) | "None"
+ * # MODIFIER ::= ["~"] modifier_name
+ * RHS ::= ( STRING | keysym | STRING keysym )
+ * STRING ::= '"' { CHAR } '"'
+ * CHAR ::= GRAPHIC_CHAR | ESCAPED_CHAR
+ * GRAPHIC_CHAR ::= locale (codeset) dependent code
+ * ESCAPED_CHAR ::= ('\\' | '\"' | OCTAL | HEX )
+ * OCTAL ::= '\' OCTAL_CHAR [OCTAL_CHAR [OCTAL_CHAR]]
+ * OCTAL_CHAR ::= (0|1|2|3|4|5|6|7)
+ * HEX ::= '\' (x|X) HEX_CHAR [HEX_CHAR]]
+ * HEX_CHAR ::= (0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F|a|b|c|d|e|f)
+ *
+ * INCLUDE_STRING is a filesystem path, with the following %-expansions:
+ * %% - '%'.
+ * %H - The user's home directory (the $HOME environment variable).
+ * %L - The name of the locale specific Compose file (e.g.,
+ * "/usr/share/X11/locale/<localename>/Compose").
+ * %S - The name of the system directory for Compose files (e.g.,
+ * "/usr/share/X11/locale").
+ */
+
+enum rules_token {
+ TOK_END_OF_FILE = 0,
+ TOK_END_OF_LINE,
+ TOK_INCLUDE,
+ TOK_INCLUDE_STRING,
+ TOK_LHS_KEYSYM,
+ TOK_COLON,
+ TOK_STRING,
+ TOK_RHS_KEYSYM,
+ TOK_ERROR
+};
+
+/* Values returned with some tokens, like yylval. */
+union lvalue {
+ const char *string;
+ xkb_keysym_t keysym;
+};
+
+static enum rules_token
+lex(struct scanner *s, union lvalue *val)
+{
+ struct keysym_from_name_cache *cache = s->priv;
+
+skip_more_whitespace_and_comments:
+ /* Skip spaces. */
+ while (is_space(peek(s)))
+ if (next(s) == '\n')
+ return TOK_END_OF_LINE;
+
+ /* Skip comments. */
+ if (chr(s, '#')) {
+ while (!eof(s) && !eol(s)) next(s);
+ goto skip_more_whitespace_and_comments;
+ }
+
+ /* See if we're done. */
+ if (eof(s)) return TOK_END_OF_FILE;
+
+ /* New token. */
+ s->token_line = s->line;
+ s->token_column = s->column;
+ s->buf_pos = 0;
+
+ /* LHS Keysym. */
+ if (chr(s, '<')) {
+ while (peek(s) != '>' && !eol(s))
+ buf_append(s, next(s));
+ if (!chr(s, '>')) {
+ scanner_err(s, "unterminated keysym literal");
+ return TOK_ERROR;
+ }
+ if (!buf_append(s, '\0')) {
+ scanner_err(s, "keysym literal is too long");
+ return TOK_ERROR;
+ }
+ val->keysym = cached_keysym_from_name(cache, s->buf, s->buf_pos);
+ if (val->keysym == XKB_KEY_NoSymbol) {
+ scanner_err(s, "unrecognized keysym \"%s\" on left-hand side", s->buf);
+ return TOK_ERROR;
+ }
+ return TOK_LHS_KEYSYM;
+ }
+
+ /* Colon. */
+ if (chr(s, ':'))
+ return TOK_COLON;
+
+ /* String literal. */
+ if (chr(s, '\"')) {
+ while (!eof(s) && !eol(s) && peek(s) != '\"') {
+ if (chr(s, '\\')) {
+ uint8_t o;
+ if (chr(s, '\\')) {
+ buf_append(s, '\\');
+ }
+ else if (chr(s, '"')) {
+ buf_append(s, '"');
+ }
+ else if (chr(s, 'x') || chr(s, 'X')) {
+ if (hex(s, &o))
+ buf_append(s, (char) o);
+ else
+ scanner_warn(s, "illegal hexadecimal escape sequence in string literal");
+ }
+ else if (oct(s, &o)) {
+ buf_append(s, (char) o);
+ }
+ else {
+ scanner_warn(s, "unknown escape sequence (%c) in string literal", peek(s));
+ /* Ignore. */
+ }
+ } else {
+ buf_append(s, next(s));
+ }
+ }
+ if (!chr(s, '\"')) {
+ scanner_err(s, "unterminated string literal");
+ return TOK_ERROR;
+ }
+ if (!buf_append(s, '\0')) {
+ scanner_err(s, "string literal is too long");
+ return TOK_ERROR;
+ }
+ if (!is_valid_utf8(s->buf, s->buf_pos - 1)) {
+ scanner_err(s, "string literal is not a valid UTF-8 string");
+ return TOK_ERROR;
+ }
+ val->string = s->buf;
+ return TOK_STRING;
+ }
+
+ /* RHS keysym or include. */
+ if (is_alpha(peek(s)) || peek(s) == '_') {
+ s->buf_pos = 0;
+ while (is_alnum(peek(s)) || peek(s) == '_')
+ buf_append(s, next(s));
+ if (!buf_append(s, '\0')) {
+ scanner_err(s, "identifier is too long");
+ return TOK_ERROR;
+ }
+
+ if (streq(s->buf, "include"))
+ return TOK_INCLUDE;
+
+ val->keysym = cached_keysym_from_name(cache, s->buf, s->buf_pos);
+ if (val->keysym == XKB_KEY_NoSymbol) {
+ scanner_err(s, "unrecognized keysym \"%s\" on right-hand side", s->buf);
+ return TOK_ERROR;
+ }
+ return TOK_RHS_KEYSYM;
+ }
+
+ /* Skip line. */
+ while (!eof(s) && !eol(s))
+ next(s);
+
+ scanner_err(s, "unrecognized token");
+ return TOK_ERROR;
+}
+
+static enum rules_token
+lex_include_string(struct scanner *s, struct xkb_compose_table *table,
+ union lvalue *val_out)
+{
+ while (is_space(peek(s)))
+ if (next(s) == '\n')
+ return TOK_END_OF_LINE;
+
+ s->token_line = s->line;
+ s->token_column = s->column;
+ s->buf_pos = 0;
+
+ if (!chr(s, '\"')) {
+ scanner_err(s, "include statement must be followed by a path");
+ return TOK_ERROR;
+ }
+
+ while (!eof(s) && !eol(s) && peek(s) != '\"') {
+ if (chr(s, '%')) {
+ if (chr(s, '%')) {
+ buf_append(s, '%');
+ }
+ else if (chr(s, 'H')) {
+ const char *home = secure_getenv("HOME");
+ if (!home) {
+ scanner_err(s, "%%H was used in an include statement, but the HOME environment variable is not set");
+ return TOK_ERROR;
+ }
+ if (!buf_appends(s, home)) {
+ scanner_err(s, "include path after expanding %%H is too long");
+ return TOK_ERROR;
+ }
+ }
+ else if (chr(s, 'L')) {
+ char *path = get_locale_compose_file_path(table->locale);
+ if (!path) {
+ scanner_err(s, "failed to expand %%L to the locale Compose file");
+ return TOK_ERROR;
+ }
+ if (!buf_appends(s, path)) {
+ free(path);
+ scanner_err(s, "include path after expanding %%L is too long");
+ return TOK_ERROR;
+ }
+ free(path);
+ }
+ else if (chr(s, 'S')) {
+ const char *xlocaledir = get_xlocaledir_path();
+ if (!buf_appends(s, xlocaledir)) {
+ scanner_err(s, "include path after expanding %%S is too long");
+ return TOK_ERROR;
+ }
+ }
+ else {
+ scanner_err(s, "unknown %% format (%c) in include statement", peek(s));
+ return TOK_ERROR;
+ }
+ } else {
+ buf_append(s, next(s));
+ }
+ }
+ if (!chr(s, '\"')) {
+ scanner_err(s, "unterminated include statement");
+ return TOK_ERROR;
+ }
+ if (!buf_append(s, '\0')) {
+ scanner_err(s, "include path is too long");
+ return TOK_ERROR;
+ }
+ val_out->string = s->buf;
+ return TOK_INCLUDE_STRING;
+}
+
+struct production {
+ xkb_keysym_t lhs[MAX_LHS_LEN];
+ unsigned int len;
+ xkb_keysym_t keysym;
+ char string[256];
+ bool has_keysym;
+ bool has_string;
+};
+
+static uint32_t
+add_node(struct xkb_compose_table *table, xkb_keysym_t keysym)
+{
+ struct compose_node new = {
+ .keysym = keysym,
+ .next = 0,
+ .is_leaf = true,
+ };
+ darray_append(table->nodes, new);
+ return darray_size(table->nodes) - 1;
+}
+
+static void
+add_production(struct xkb_compose_table *table, struct scanner *s,
+ const struct production *production)
+{
+ unsigned lhs_pos;
+ uint32_t curr;
+ struct compose_node *node;
+
+ curr = 0;
+ node = &darray_item(table->nodes, curr);
+
+ /*
+ * Insert the sequence to the trie, creating new nodes as needed.
+ *
+ * TODO: This can be sped up a bit by first trying the path that the
+ * previous production took, and only then doing the linear search
+ * through the trie levels. This will work because sequences in the
+ * Compose files are often clustered by a common prefix; especially
+ * in the 1st and 2nd keysyms, which is where the largest variation
+ * (thus, longest search) is.
+ */
+ for (lhs_pos = 0; lhs_pos < production->len; lhs_pos++) {
+ while (production->lhs[lhs_pos] != node->keysym) {
+ if (node->next == 0) {
+ uint32_t next = add_node(table, production->lhs[lhs_pos]);
+ /* Refetch since add_node could have realloc()ed. */
+ node = &darray_item(table->nodes, curr);
+ node->next = next;
+ }
+
+ curr = node->next;
+ node = &darray_item(table->nodes, curr);
+ }
+
+ if (lhs_pos + 1 == production->len)
+ break;
+
+ if (node->is_leaf) {
+ if (node->u.leaf.utf8 != 0 ||
+ node->u.leaf.keysym != XKB_KEY_NoSymbol) {
+ scanner_warn(s, "a sequence already exists which is a prefix of this sequence; overriding");
+ node->u.leaf.utf8 = 0;
+ node->u.leaf.keysym = XKB_KEY_NoSymbol;
+ }
+
+ {
+ uint32_t successor = add_node(table, production->lhs[lhs_pos + 1]);
+ /* Refetch since add_node could have realloc()ed. */
+ node = &darray_item(table->nodes, curr);
+ node->is_leaf = false;
+ node->u.successor = successor;
+ }
+ }
+
+ curr = node->u.successor;
+ node = &darray_item(table->nodes, curr);
+ }
+
+ if (!node->is_leaf) {
+ scanner_warn(s, "this compose sequence is a prefix of another; skipping line");
+ return;
+ }
+
+ if (node->u.leaf.utf8 != 0 || node->u.leaf.keysym != XKB_KEY_NoSymbol) {
+ if (streq(&darray_item(table->utf8, node->u.leaf.utf8),
+ production->string) &&
+ node->u.leaf.keysym == production->keysym) {
+ scanner_warn(s, "this compose sequence is a duplicate of another; skipping line");
+ return;
+ }
+ scanner_warn(s, "this compose sequence already exists; overriding");
+ }
+
+ if (production->has_string) {
+ node->u.leaf.utf8 = darray_size(table->utf8);
+ darray_append_items(table->utf8, production->string,
+ strlen(production->string) + 1);
+ }
+ if (production->has_keysym) {
+ node->u.leaf.keysym = production->keysym;
+ }
+}
+
+static bool
+parse(struct xkb_compose_table *table, struct scanner *s,
+ unsigned include_depth);
+
+static bool
+do_include(struct xkb_compose_table *table, struct scanner *s,
+ const char *path, unsigned include_depth)
+{
+ FILE *file;
+ bool ok;
+ const char *string;
+ size_t size;
+ struct scanner new_s;
+
+ if (include_depth >= MAX_INCLUDE_DEPTH) {
+ scanner_err(s, "maximum include depth (%d) exceeded; maybe there is an include loop?",
+ MAX_INCLUDE_DEPTH);
+ return false;
+ }
+
+ file = fopen(s->buf, "r");
+ if (!file) {
+ scanner_err(s, "failed to open included Compose file \"%s\": %s",
+ path, strerror(errno));
+ return false;
+ }
+
+ ok = map_file(file, &string, &size);
+ if (!ok) {
+ scanner_err(s, "failed to read included Compose file \"%s\": %s",
+ path, strerror(errno));
+ goto err_file;
+ }
+
+ scanner_init(&new_s, table->ctx, string, size, path);
+
+ ok = parse(table, &new_s, include_depth + 1);
+ if (!ok)
+ goto err_unmap;
+
+err_unmap:
+ unmap_file(string, size);
+err_file:
+ fclose(file);
+ return ok;
+}
+
+static bool
+parse(struct xkb_compose_table *table, struct scanner *s,
+ unsigned include_depth)
+{
+ enum rules_token tok;
+ union lvalue val;
+ struct production production;
+ enum { MAX_ERRORS = 10 };
+ int num_errors = 0;
+
+initial:
+ production.len = 0;
+ production.has_keysym = false;
+ production.has_string = false;
+
+ /* fallthrough */
+
+initial_eol:
+ switch (tok = lex(s, &val)) {
+ case TOK_END_OF_LINE:
+ goto initial_eol;
+ case TOK_END_OF_FILE:
+ goto finished;
+ case TOK_INCLUDE:
+ goto include;
+ case TOK_LHS_KEYSYM:
+ production.lhs[production.len++] = val.keysym;
+ goto lhs;
+ default:
+ goto unexpected;
+ }
+
+include:
+ switch (tok = lex_include_string(s, table, &val)) {
+ case TOK_INCLUDE_STRING:
+ goto include_eol;
+ default:
+ goto unexpected;
+ }
+
+include_eol:
+ switch (tok = lex(s, &val)) {
+ case TOK_END_OF_LINE:
+ if (!do_include(table, s, val.string, include_depth))
+ goto fail;
+ goto initial;
+ default:
+ goto unexpected;
+ }
+
+lhs:
+ switch (tok = lex(s, &val)) {
+ case TOK_LHS_KEYSYM:
+ if (production.len + 1 > MAX_LHS_LEN) {
+ scanner_warn(s, "too many keysyms (%d) on left-hand side; skipping line",
+ MAX_LHS_LEN + 1);
+ goto skip;
+ }
+ production.lhs[production.len++] = val.keysym;
+ goto lhs;
+ case TOK_COLON:
+ if (production.len <= 0) {
+ scanner_warn(s, "expected at least one keysym on left-hand side; skipping line");
+ goto skip;
+ }
+ goto rhs;
+ default:
+ goto unexpected;
+ }
+
+rhs:
+ switch (tok = lex(s, &val)) {
+ case TOK_STRING:
+ if (production.has_string) {
+ scanner_warn(s, "right-hand side can have at most one string; skipping line");
+ goto skip;
+ }
+ if (*val.string == '\0') {
+ scanner_warn(s, "right-hand side string must not be empty; skipping line");
+ goto skip;
+ }
+ if (strlen(val.string) >= sizeof(production.string)) {
+ scanner_warn(s, "right-hand side string is too long; skipping line");
+ goto skip;
+ }
+ strcpy(production.string, val.string);
+ production.has_string = true;
+ goto rhs;
+ case TOK_RHS_KEYSYM:
+ if (production.has_keysym) {
+ scanner_warn(s, "right-hand side can have at most one keysym; skipping line");
+ goto skip;
+ }
+ production.keysym = val.keysym;
+ production.has_keysym = true;
+ case TOK_END_OF_LINE:
+ if (!production.has_string && !production.has_keysym) {
+ scanner_warn(s, "right-hand side must have at least one of string or keysym; skipping line");
+ goto skip;
+ }
+ add_production(table, s, &production);
+ goto initial;
+ default:
+ goto unexpected;
+ }
+
+unexpected:
+ if (tok != TOK_ERROR)
+ scanner_err(s, "unexpected token");
+
+ num_errors++;
+ if (num_errors <= MAX_ERRORS)
+ goto skip;
+
+ scanner_err(s, "too many errors");
+ goto fail;
+
+fail:
+ scanner_err(s, "failed to parse file");
+ return false;
+
+skip:
+ while (tok != TOK_END_OF_LINE && tok != TOK_END_OF_FILE)
+ tok = lex(s, &val);
+ goto initial;
+
+finished:
+ return true;
+}
+
+bool
+parse_string(struct xkb_compose_table *table, const char *string, size_t len,
+ const char *file_name)
+{
+ struct scanner s;
+ struct keysym_from_name_cache cache;
+ scanner_init(&s, table->ctx, string, len, file_name);
+ memset(&cache, 0, sizeof(cache));
+ s.priv = &cache;
+ if (!parse(table, &s, 0))
+ return false;
+ /* Maybe the allocator can use the excess space. */
+ darray_shrink(table->nodes);
+ darray_shrink(table->utf8);
+ return true;
+}
+
+bool
+parse_file(struct xkb_compose_table *table, FILE *file, const char *file_name)
+{
+ bool ok;
+ const char *string;
+ size_t size;
+
+ ok = map_file(file, &string, &size);
+ if (!ok) {
+ log_err(table->ctx, "Couldn't read Compose file %s: %s\n",
+ file_name, strerror(errno));
+ return false;
+ }
+
+ ok = parse_string(table, string, size, file_name);
+ unmap_file(string, size);
+ return ok;
+}
diff --git a/src/compose/parser.h b/src/compose/parser.h
new file mode 100644
index 0000000..3f64a07
--- /dev/null
+++ b/src/compose/parser.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright © 2013 Ran Benita <ran234@gmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef COMPOSE_PARSER_H
+#define COMPOSE_PARSER_H
+
+bool
+parse_string(struct xkb_compose_table *table,
+ const char *string, size_t len,
+ const char *file_name);
+
+bool
+parse_file(struct xkb_compose_table *table,
+ FILE *file, const char *file_name);
+
+#endif
diff --git a/src/compose/paths.c b/src/compose/paths.c
new file mode 100644
index 0000000..c96d7d2
--- /dev/null
+++ b/src/compose/paths.c
@@ -0,0 +1,204 @@
+/*
+ * Copyright © 2014 Ran Benita <ran234@gmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "utils.h"
+#include "paths.h"
+
+enum resolve_name_direction {
+ LEFT_TO_RIGHT,
+ RIGHT_TO_LEFT,
+};
+
+const char *
+get_xlocaledir_path(void)
+{
+ const char *dir = secure_getenv("XLOCALEDIR");
+ if (!dir)
+ dir = XLOCALEDIR;
+ return dir;
+}
+
+/*
+ * Files like compose.dir have the format LEFT: RIGHT. Lookup @name in
+ * such a file and return its matching value, according to @direction.
+ * @filename is relative to the xlocaledir.
+ */
+static char *
+resolve_name(const char *filename, enum resolve_name_direction direction,
+ const char *name)
+{
+ int ret;
+ bool ok;
+ const char *xlocaledir;
+ char path[512];
+ FILE *file;
+ const char *string, *end;
+ size_t string_size;
+ const char *s, *left, *right;
+ char *match;
+ size_t left_len, right_len, name_len;
+
+ xlocaledir = get_xlocaledir_path();
+
+ ret = snprintf(path, sizeof(path), "%s/%s", xlocaledir, filename);
+ if (ret < 0 || (size_t) ret >= sizeof(path))
+ return false;
+
+ file = fopen(path, "r");
+ if (!file)
+ return false;
+
+ ok = map_file(file, &string, &string_size);
+ fclose(file);
+ if (!ok)
+ return false;
+
+ s = string;
+ end = string + string_size;
+ name_len = strlen(name);
+ match = NULL;
+
+ while (s < end) {
+ /* Skip spaces. */
+ while (s < end && is_space(*s))
+ s++;
+
+ /* Skip comments. */
+ if (s < end && *s == '#') {
+ while (s < end && *s != '\n')
+ s++;
+ continue;
+ }
+
+ /* Get the left value. */
+ left = s;
+ while (s < end && !is_space(*s) && *s != ':')
+ s++;
+ left_len = s - left;
+
+ /* There's an optional colon between left and right. */
+ if (s < end && *s == ':')
+ s++;
+
+ /* Skip spaces. */
+ while (s < end && is_space(*s))
+ s++;
+
+ /* Get the right value. */
+ right = s;
+ while (s < end && !is_space(*s))
+ s++;
+ right_len = s - right;
+
+ /* Discard rest of line. */
+ while (s < end && *s != '\n')
+ s++;
+
+ if (direction == LEFT_TO_RIGHT) {
+ if (left_len == name_len && strncmp(left, name, left_len) == 0) {
+ match = strndup(right, right_len);
+ break;
+ }
+ }
+ else if (direction == RIGHT_TO_LEFT) {
+ if (right_len == name_len && strncmp(right, name, right_len) == 0) {
+ match = strndup(left, left_len);
+ break;
+ }
+ }
+ }
+
+ unmap_file(string, string_size);
+ return match;
+}
+
+char *
+resolve_locale(const char *locale)
+{
+ char *alias = resolve_name("locale.alias", LEFT_TO_RIGHT, locale);
+ return alias ? alias : strdup(locale);
+}
+
+const char *
+get_xcomposefile_path(void)
+{
+ return secure_getenv("XCOMPOSEFILE");
+}
+
+char *
+get_home_xcompose_file_path(void)
+{
+ int ret;
+ const char *home;
+ char *path;
+
+ home = secure_getenv("HOME");
+ if (!home)
+ return NULL;
+
+ ret = asprintf(&path, "%s/.XCompose", home);
+ if (ret <0)
+ return NULL;
+
+ return path;
+}
+
+char *
+get_locale_compose_file_path(const char *locale)
+{
+ int ret;
+ const char *xlocaledir;
+ char *resolved;
+ char *path;
+
+ /*
+ * WARNING: Random workaround ahead.
+ *
+ * We currently do not support non-UTF-8 Compose files. The C/POSIX
+ * locale is specified to be the default fallback locale with an
+ * ASCII charset. But for some reason the compose.dir points the C
+ * locale to the iso8859-1/Compose file, which is not ASCII but
+ * ISO8859-1. Since this is bound to happen a lot, and since our API
+ * is UTF-8 based, and since 99% of the time a C locale is really just
+ * a misconfiguration for UTF-8, let's do the most helpful thing.
+ */
+ if (streq(locale, "C"))
+ locale = "en_US.UTF-8";
+
+ resolved = resolve_name("compose.dir", RIGHT_TO_LEFT, locale);
+ if (!resolved)
+ return NULL;
+
+ if (resolved[0] == '/') {
+ path = resolved;
+ }
+ else {
+ xlocaledir = get_xlocaledir_path();
+ ret = asprintf(&path, "%s/%s", xlocaledir, resolved);
+ free(resolved);
+ if (ret < 0)
+ return NULL;
+ }
+
+ return path;
+}
diff --git a/src/compose/paths.h b/src/compose/paths.h
new file mode 100644
index 0000000..1d719af
--- /dev/null
+++ b/src/compose/paths.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright © 2014 Ran Benita <ran234@gmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef COMPOSE_RESOLVE_H
+#define COMPOSE_RESOLVE_H
+
+char *
+resolve_locale(const char *locale);
+
+const char *
+get_xlocaledir_path(void);
+
+const char *
+get_xcomposefile_path(void);
+
+char *
+get_home_xcompose_file_path(void);
+
+char *
+get_locale_compose_file_path(const char *locale);
+
+#endif
diff --git a/src/compose/state.c b/src/compose/state.c
new file mode 100644
index 0000000..8657ff7
--- /dev/null
+++ b/src/compose/state.c
@@ -0,0 +1,196 @@
+/*
+ * Copyright © 2013 Ran Benita <ran234@gmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "table.h"
+#include "utils.h"
+#include "keysym.h"
+
+struct xkb_compose_state {
+ int refcnt;
+ enum xkb_compose_state_flags flags;
+ struct xkb_compose_table *table;
+
+ /*
+ * Offsets into xkb_compose_table::nodes.
+ *
+ * They maintain the current and previous position in the trie; see
+ * xkb_compose_state_feed().
+ *
+ * This is also sufficient for inferring the current status; see
+ * xkb_compose_state_get_status().
+ */
+ uint32_t prev_context;
+ uint32_t context;
+};
+
+XKB_EXPORT struct xkb_compose_state *
+xkb_compose_state_new(struct xkb_compose_table *table,
+ enum xkb_compose_state_flags flags)
+{
+ struct xkb_compose_state *state;
+
+ state = calloc(1, sizeof(*state));
+ if (!state)
+ return NULL;
+
+ state->refcnt = 1;
+ state->table = xkb_compose_table_ref(table);
+
+ state->flags = flags;
+ state->prev_context = 0;
+ state->context = 0;
+
+ return state;
+}
+
+XKB_EXPORT struct xkb_compose_state *
+xkb_compose_state_ref(struct xkb_compose_state *state)
+{
+ state->refcnt++;
+ return state;
+}
+
+XKB_EXPORT void
+xkb_compose_state_unref(struct xkb_compose_state *state)
+{
+ if (!state || --state->refcnt > 0)
+ return;
+
+ xkb_compose_table_unref(state->table);
+ free(state);
+}
+
+XKB_EXPORT struct xkb_compose_table *
+xkb_compose_state_get_compose_table(struct xkb_compose_state *state)
+{
+ return state->table;
+}
+
+XKB_EXPORT enum xkb_compose_feed_result
+xkb_compose_state_feed(struct xkb_compose_state *state, xkb_keysym_t keysym)
+{
+ uint32_t context;
+ const struct compose_node *node;
+
+ /*
+ * Modifiers do not affect the sequence directly. In particular,
+ * they do not cancel a sequence; otherwise it'd be impossible to
+ * have a sequence like <dead_acute><A> (needs Shift in the middle).
+ *
+ * The following test is not really accurate - in order to test if
+ * a key is "modifier key", we really need the keymap, but we don't
+ * have it here. However, this is (approximately) what libX11 does
+ * as well.
+ */
+ if (xkb_keysym_is_modifier(keysym))
+ return XKB_COMPOSE_FEED_IGNORED;
+
+ node = &darray_item(state->table->nodes, state->context);
+
+ context = (node->is_leaf ? 0 : node->u.successor);
+ node = &darray_item(state->table->nodes, context);
+
+ while (node->keysym != keysym && node->next != 0) {
+ context = node->next;
+ node = &darray_item(state->table->nodes, context);
+ }
+
+ if (node->keysym != keysym)
+ context = 0;
+
+ state->prev_context = state->context;
+ state->context = context;
+ return XKB_COMPOSE_FEED_ACCEPTED;
+}
+
+XKB_EXPORT void
+xkb_compose_state_reset(struct xkb_compose_state *state)
+{
+ state->prev_context = 0;
+ state->context = 0;
+}
+
+XKB_EXPORT enum xkb_compose_status
+xkb_compose_state_get_status(struct xkb_compose_state *state)
+{
+ const struct compose_node *prev_node, *node;
+
+ prev_node = &darray_item(state->table->nodes, state->prev_context);
+ node = &darray_item(state->table->nodes, state->context);
+
+ if (state->context == 0 && !prev_node->is_leaf)
+ return XKB_COMPOSE_CANCELLED;
+
+ if (state->context == 0)
+ return XKB_COMPOSE_NOTHING;
+
+ if (!node->is_leaf)
+ return XKB_COMPOSE_COMPOSING;
+
+ return XKB_COMPOSE_COMPOSED;
+}
+
+XKB_EXPORT int
+xkb_compose_state_get_utf8(struct xkb_compose_state *state,
+ char *buffer, size_t size)
+{
+ const struct compose_node *node =
+ &darray_item(state->table->nodes, state->context);
+
+ if (!node->is_leaf)
+ goto fail;
+
+ /* If there's no string specified, but only a keysym, try to do the
+ * most helpful thing. */
+ if (node->u.leaf.utf8 == 0 && node->u.leaf.keysym != XKB_KEY_NoSymbol) {
+ char name[64];
+ int ret;
+
+ ret = xkb_keysym_to_utf8(node->u.leaf.keysym, name, sizeof(name));
+ if (ret < 0 || ret == 0) {
+ /* ret < 0 is impossible.
+ * ret == 0 means the keysym has no string representation. */
+ goto fail;
+ }
+
+ return snprintf(buffer, size, "%s", name);
+ }
+
+ return snprintf(buffer, size, "%s",
+ &darray_item(state->table->utf8, node->u.leaf.utf8));
+
+fail:
+ if (size > 0)
+ buffer[0] = '\0';
+ return 0;
+}
+
+XKB_EXPORT xkb_keysym_t
+xkb_compose_state_get_one_sym(struct xkb_compose_state *state)
+{
+ const struct compose_node *node =
+ &darray_item(state->table->nodes, state->context);
+ if (!node->is_leaf)
+ return XKB_KEY_NoSymbol;
+ return node->u.leaf.keysym;
+}
diff --git a/src/compose/table.c b/src/compose/table.c
new file mode 100644
index 0000000..5cd8415
--- /dev/null
+++ b/src/compose/table.c
@@ -0,0 +1,219 @@
+/*
+ * Copyright © 2013 Ran Benita <ran234@gmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "utils.h"
+#include "table.h"
+#include "parser.h"
+#include "paths.h"
+
+static struct xkb_compose_table *
+xkb_compose_table_new(struct xkb_context *ctx,
+ const char *locale,
+ enum xkb_compose_format format,
+ enum xkb_compose_compile_flags flags)
+{
+ char *resolved_locale;
+ struct xkb_compose_table *table;
+ struct compose_node root;
+
+ resolved_locale = resolve_locale(locale);
+ if (!resolved_locale)
+ return NULL;
+
+ table = calloc(1, sizeof(*table));
+ if (!table) {
+ free(resolved_locale);
+ return NULL;
+ }
+
+ table->refcnt = 1;
+ table->ctx = xkb_context_ref(ctx);
+
+ table->locale = resolved_locale;
+ table->format = format;
+ table->flags = flags;
+
+ darray_init(table->nodes);
+ darray_init(table->utf8);
+
+ root.keysym = XKB_KEY_NoSymbol;
+ root.next = 0;
+ root.is_leaf = true;
+ root.u.leaf.utf8 = 0;
+ root.u.leaf.keysym = XKB_KEY_NoSymbol;
+ darray_append(table->nodes, root);
+
+ darray_append(table->utf8, '\0');
+
+ return table;
+}
+
+XKB_EXPORT struct xkb_compose_table *
+xkb_compose_table_ref(struct xkb_compose_table *table)
+{
+ table->refcnt++;
+ return table;
+}
+
+XKB_EXPORT void
+xkb_compose_table_unref(struct xkb_compose_table *table)
+{
+ if (!table || --table->refcnt > 0)
+ return;
+ free(table->locale);
+ darray_free(table->nodes);
+ darray_free(table->utf8);
+ xkb_context_unref(table->ctx);
+ free(table);
+}
+
+XKB_EXPORT struct xkb_compose_table *
+xkb_compose_table_new_from_file(struct xkb_context *ctx,
+ FILE *file,
+ const char *locale,
+ enum xkb_compose_format format,
+ enum xkb_compose_compile_flags flags)
+{
+ struct xkb_compose_table *table;
+ bool ok;
+
+ if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
+ log_err_func(ctx, "unrecognized flags: %#x\n", flags);
+ return NULL;
+ }
+
+ if (format != XKB_COMPOSE_FORMAT_TEXT_V1) {
+ log_err_func(ctx, "unsupported compose format: %d\n", format);
+ return NULL;
+ }
+
+ table = xkb_compose_table_new(ctx, locale, format, flags);
+ if (!table)
+ return NULL;
+
+ ok = parse_file(table, file, "(unknown file)");
+ if (!ok) {
+ xkb_compose_table_unref(table);
+ return NULL;
+ }
+
+ return table;
+}
+
+XKB_EXPORT struct xkb_compose_table *
+xkb_compose_table_new_from_buffer(struct xkb_context *ctx,
+ const char *buffer, size_t length,
+ const char *locale,
+ enum xkb_compose_format format,
+ enum xkb_compose_compile_flags flags)
+{
+ struct xkb_compose_table *table;
+ bool ok;
+
+ if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
+ log_err_func(ctx, "unrecognized flags: %#x\n", flags);
+ return NULL;
+ }
+
+ if (format != XKB_COMPOSE_FORMAT_TEXT_V1) {
+ log_err_func(ctx, "unsupported compose format: %d\n", format);
+ return NULL;
+ }
+
+ table = xkb_compose_table_new(ctx, locale, format, flags);
+ if (!table)
+ return NULL;
+
+ ok = parse_string(table, buffer, length, "(input string)");
+ if (!ok) {
+ xkb_compose_table_unref(table);
+ return NULL;
+ }
+
+ return table;
+}
+
+XKB_EXPORT struct xkb_compose_table *
+xkb_compose_table_new_from_locale(struct xkb_context *ctx,
+ const char *locale,
+ enum xkb_compose_compile_flags flags)
+{
+ struct xkb_compose_table *table;
+ char *path = NULL;
+ const char *cpath;
+ FILE *file;
+ bool ok;
+
+ if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
+ log_err_func(ctx, "unrecognized flags: %#x\n", flags);
+ return NULL;
+ }
+
+ table = xkb_compose_table_new(ctx, locale, XKB_COMPOSE_FORMAT_TEXT_V1,
+ flags);
+ if (!table)
+ return NULL;
+
+ cpath = get_xcomposefile_path();
+ if (cpath) {
+ file = fopen(cpath, "r");
+ if (file)
+ goto found_path;
+ }
+
+ cpath = path = get_home_xcompose_file_path();
+ if (path) {
+ file = fopen(path, "r");
+ if (file)
+ goto found_path;
+ }
+ free(path);
+ path = NULL;
+
+ cpath = path = get_locale_compose_file_path(table->locale);
+ if (path) {
+ file = fopen(path, "r");
+ if (file)
+ goto found_path;
+ }
+ free(path);
+ path = NULL;
+
+ log_err(ctx, "couldn't find a Compose file for locale \"%s\"\n", locale);
+ xkb_compose_table_unref(table);
+ return NULL;
+
+found_path:
+ ok = parse_file(table, file, cpath);
+ fclose(file);
+ if (!ok) {
+ xkb_compose_table_unref(table);
+ return NULL;
+ }
+
+ log_dbg(ctx, "created compose table from locale %s with path %s\n",
+ table->locale, path);
+
+ free(path);
+ return table;
+}
diff --git a/src/compose/table.h b/src/compose/table.h
new file mode 100644
index 0000000..05a415f
--- /dev/null
+++ b/src/compose/table.h
@@ -0,0 +1,100 @@
+/*
+ * Copyright © 2013 Ran Benita <ran234@gmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef COMPOSE_COMPOSE_H
+#define COMPOSE_COMPOSE_H
+
+#include "xkbcommon/xkbcommon-compose.h"
+#include "utils.h"
+#include "context.h"
+
+/*
+ * The compose table data structure is a simple trie. An example will
+ * help. Given these sequences:
+ *
+ * <A> <B> : "first" dead_a
+ * <A> <C> <D> : "second" dead_b
+ * <E> <F> : "third" dead_c
+ *
+ * the trie would look like:
+ *
+ * [root] ---> [<A>] -----------------> [<E>] -#
+ * | | |
+ * # v v
+ * [<B>] ---> [<C>] -# [<F>] -#
+ * | | -
+ * # v #
+ * [<D>] -#
+ * |
+ * #
+ * where:
+ * - [root] is a special empty root node.
+ * - [<X>] is a node for a sequence keysym <X>.
+ * - right arrows are `next` pointers.
+ * - down arrows are `successor` pointers.
+ * - # is a nil pointer.
+ *
+ * The nodes are all kept in a contiguous array. Pointers are represented
+ * as integer offsets into this array. A nil pointer is represented as 0
+ * (which, helpfully, is the offset of the empty root node).
+ *
+ * Nodes without a successor are leaf nodes. Since a sequence cannot be a
+ * prefix of another, these are exactly the nodes which terminate the
+ * sequences (in a bijective manner).
+ *
+ * A leaf contains the result data of its sequence. The result keysym is
+ * contained in the node struct itself; the result UTF-8 string is a byte
+ * offset into an array of the form "\0first\0second\0third" (the initial
+ * \0 is so offset 0 points to an empty string).
+ */
+
+struct compose_node {
+ xkb_keysym_t keysym;
+ /* Offset into xkb_compose_table::nodes. */
+ unsigned int next:31;
+ bool is_leaf:1;
+
+ union {
+ /* Offset into xkb_compose_table::nodes. */
+ uint32_t successor;
+ struct {
+ /* Offset into xkb_compose_table::utf8. */
+ uint32_t utf8;
+ xkb_keysym_t keysym;
+ } leaf;
+ } u;
+};
+
+struct xkb_compose_table {
+ int refcnt;
+ enum xkb_compose_format format;
+ enum xkb_compose_compile_flags flags;
+ struct xkb_context *ctx;
+
+ char *locale;
+
+ darray_char utf8;
+ darray(struct compose_node) nodes;
+};
+
+#endif
diff --git a/xkbcommon.map b/xkbcommon.map
index 28122b4..3bbb13e 100644
--- a/xkbcommon.map
+++ b/xkbcommon.map
@@ -68,6 +68,20 @@ global:
xkb_state_layout_index_is_active;
xkb_state_led_name_is_active;
xkb_state_led_index_is_active;
+ xkb_compose_table_new_from_locale;
+ xkb_compose_table_new_from_file;
+ xkb_compose_table_new_from_buffer;
+ xkb_compose_table_ref;
+ xkb_compose_table_unref;
+ xkb_compose_state_new;
+ xkb_compose_state_ref;
+ xkb_compose_state_unref;
+ xkb_compose_state_get_compose_table;
+ xkb_compose_state_feed;
+ xkb_compose_state_reset;
+ xkb_compose_state_get_status;
+ xkb_compose_state_get_utf8;
+ xkb_compose_state_get_one_sym;
local:
*;
};