maprules.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. /************************************************************
  2. Copyright (c) 1996 by Silicon Graphics Computer Systems, Inc.
  3. Permission to use, copy, modify, and distribute this
  4. software and its documentation for any purpose and without
  5. fee is hereby granted, provided that the above copyright
  6. notice appear in all copies and that both that copyright
  7. notice and this permission notice appear in supporting
  8. documentation, and that the name of Silicon Graphics not be
  9. used in advertising or publicity pertaining to distribution
  10. of the software without specific prior written permission.
  11. Silicon Graphics makes no representation about the suitability
  12. of this software for any purpose. It is provided "as is"
  13. without any express or implied warranty.
  14. SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  15. SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  16. AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
  17. GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  18. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  19. DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  20. OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
  21. THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22. ********************************************************/
  23. #ifdef HAVE_DIX_CONFIG_H
  24. #include <dix-config.h>
  25. #endif
  26. #include <stdio.h>
  27. #include <ctype.h>
  28. #include <stdlib.h>
  29. #define X_INCLUDE_STRING_H
  30. #define XOS_USE_NO_LOCKING
  31. #include <X11/Xos_r.h>
  32. #include <X11/Xproto.h>
  33. #include <X11/X.h>
  34. #include <X11/Xos.h>
  35. #include <X11/Xfuncs.h>
  36. #include <X11/Xatom.h>
  37. #include <X11/keysym.h>
  38. #include "misc.h"
  39. #include "inputstr.h"
  40. #include "dix.h"
  41. #include "os.h"
  42. #include "xkbstr.h"
  43. #define XKBSRV_NEED_FILE_FUNCS
  44. #include <xkbsrv.h>
  45. /***====================================================================***/
  46. #define DFLT_LINE_SIZE 128
  47. typedef struct {
  48. int line_num;
  49. int sz_line;
  50. int num_line;
  51. char buf[DFLT_LINE_SIZE];
  52. char *line;
  53. } InputLine;
  54. static void
  55. InitInputLine(InputLine * line)
  56. {
  57. line->line_num = 1;
  58. line->num_line = 0;
  59. line->sz_line = DFLT_LINE_SIZE;
  60. line->line = line->buf;
  61. return;
  62. }
  63. static void
  64. FreeInputLine(InputLine * line)
  65. {
  66. if (line->line != line->buf)
  67. free(line->line);
  68. line->line_num = 1;
  69. line->num_line = 0;
  70. line->sz_line = DFLT_LINE_SIZE;
  71. line->line = line->buf;
  72. return;
  73. }
  74. static int
  75. InputLineAddChar(InputLine * line, int ch)
  76. {
  77. if (line->num_line >= line->sz_line) {
  78. if (line->line == line->buf) {
  79. line->line = malloc(line->sz_line * 2);
  80. memcpy(line->line, line->buf, line->sz_line);
  81. }
  82. else {
  83. line->line = realloc((char *) line->line, line->sz_line * 2);
  84. }
  85. line->sz_line *= 2;
  86. }
  87. line->line[line->num_line++] = ch;
  88. return ch;
  89. }
  90. #define ADD_CHAR(l,c) ((l)->num_line<(l)->sz_line?\
  91. (int)((l)->line[(l)->num_line++]= (c)):\
  92. InputLineAddChar(l,c))
  93. static Bool
  94. GetInputLine(FILE * file, InputLine * line, Bool checkbang)
  95. {
  96. int ch;
  97. Bool endOfFile, spacePending, slashPending, inComment;
  98. endOfFile = FALSE;
  99. while ((!endOfFile) && (line->num_line == 0)) {
  100. spacePending = slashPending = inComment = FALSE;
  101. while (((ch = getc(file)) != '\n') && (ch != EOF)) {
  102. if (ch == '\\') {
  103. if ((ch = getc(file)) == EOF)
  104. break;
  105. if (ch == '\n') {
  106. inComment = FALSE;
  107. ch = ' ';
  108. line->line_num++;
  109. }
  110. }
  111. if (inComment)
  112. continue;
  113. if (ch == '/') {
  114. if (slashPending) {
  115. inComment = TRUE;
  116. slashPending = FALSE;
  117. }
  118. else {
  119. slashPending = TRUE;
  120. }
  121. continue;
  122. }
  123. else if (slashPending) {
  124. if (spacePending) {
  125. ADD_CHAR(line, ' ');
  126. spacePending = FALSE;
  127. }
  128. ADD_CHAR(line, '/');
  129. slashPending = FALSE;
  130. }
  131. if (isspace(ch)) {
  132. while (isspace(ch) && (ch != '\n') && (ch != EOF)) {
  133. ch = getc(file);
  134. }
  135. if (ch == EOF)
  136. break;
  137. if ((ch != '\n') && (line->num_line > 0))
  138. spacePending = TRUE;
  139. ungetc(ch, file);
  140. }
  141. else {
  142. if (spacePending) {
  143. ADD_CHAR(line, ' ');
  144. spacePending = FALSE;
  145. }
  146. if (checkbang && ch == '!') {
  147. if (line->num_line != 0) {
  148. DebugF("The '!' legal only at start of line\n");
  149. DebugF("Line containing '!' ignored\n");
  150. line->num_line = 0;
  151. inComment = 0;
  152. break;
  153. }
  154. }
  155. ADD_CHAR(line, ch);
  156. }
  157. }
  158. if (ch == EOF)
  159. endOfFile = TRUE;
  160. /* else line->num_line++;*/
  161. }
  162. if ((line->num_line == 0) && (endOfFile))
  163. return FALSE;
  164. ADD_CHAR(line, '\0');
  165. return TRUE;
  166. }
  167. /***====================================================================***/
  168. #define MODEL 0
  169. #define LAYOUT 1
  170. #define VARIANT 2
  171. #define OPTION 3
  172. #define KEYCODES 4
  173. #define SYMBOLS 5
  174. #define TYPES 6
  175. #define COMPAT 7
  176. #define GEOMETRY 8
  177. #define MAX_WORDS 9
  178. #define PART_MASK 0x000F
  179. #define COMPONENT_MASK 0x03F0
  180. static const char *cname[MAX_WORDS] = {
  181. "model", "layout", "variant", "option",
  182. "keycodes", "symbols", "types", "compat", "geometry"
  183. };
  184. typedef struct _RemapSpec {
  185. int number;
  186. int num_remap;
  187. struct {
  188. int word;
  189. int index;
  190. } remap[MAX_WORDS];
  191. } RemapSpec;
  192. typedef struct _FileSpec {
  193. char *name[MAX_WORDS];
  194. struct _FileSpec *pending;
  195. } FileSpec;
  196. typedef struct {
  197. const char *model;
  198. const char *layout[XkbNumKbdGroups + 1];
  199. const char *variant[XkbNumKbdGroups + 1];
  200. const char *options;
  201. } XkbRF_MultiDefsRec, *XkbRF_MultiDefsPtr;
  202. #define NDX_BUFF_SIZE 4
  203. /***====================================================================***/
  204. static char *
  205. get_index(char *str, int *ndx)
  206. {
  207. char ndx_buf[NDX_BUFF_SIZE];
  208. char *end;
  209. if (*str != '[') {
  210. *ndx = 0;
  211. return str;
  212. }
  213. str++;
  214. end = strchr(str, ']');
  215. if (end == NULL) {
  216. *ndx = -1;
  217. return str - 1;
  218. }
  219. if ((end - str) >= NDX_BUFF_SIZE) {
  220. *ndx = -1;
  221. return end + 1;
  222. }
  223. strlcpy(ndx_buf, str, 1 + end - str);
  224. *ndx = atoi(ndx_buf);
  225. return end + 1;
  226. }
  227. static void
  228. SetUpRemap(InputLine * line, RemapSpec * remap)
  229. {
  230. char *tok, *str;
  231. unsigned present, l_ndx_present, v_ndx_present;
  232. register int i;
  233. int len, ndx;
  234. _Xstrtokparams strtok_buf;
  235. Bool found;
  236. l_ndx_present = v_ndx_present = present = 0;
  237. str = &line->line[1];
  238. len = remap->number;
  239. memset((char *) remap, 0, sizeof(RemapSpec));
  240. remap->number = len;
  241. while ((tok = _XStrtok(str, " ", strtok_buf)) != NULL) {
  242. found = FALSE;
  243. str = NULL;
  244. if (strcmp(tok, "=") == 0)
  245. continue;
  246. for (i = 0; i < MAX_WORDS; i++) {
  247. len = strlen(cname[i]);
  248. if (strncmp(cname[i], tok, len) == 0) {
  249. if (strlen(tok) > len) {
  250. char *end = get_index(tok + len, &ndx);
  251. if ((i != LAYOUT && i != VARIANT) ||
  252. *end != '\0' || ndx == -1)
  253. break;
  254. if (ndx < 1 || ndx > XkbNumKbdGroups) {
  255. DebugF("Illegal %s index: %d\n", cname[i], ndx);
  256. DebugF("Index must be in range 1..%d\n",
  257. XkbNumKbdGroups);
  258. break;
  259. }
  260. }
  261. else {
  262. ndx = 0;
  263. }
  264. found = TRUE;
  265. if (present & (1 << i)) {
  266. if ((i == LAYOUT && l_ndx_present & (1 << ndx)) ||
  267. (i == VARIANT && v_ndx_present & (1 << ndx))) {
  268. DebugF("Component \"%s\" listed twice\n", tok);
  269. DebugF("Second definition ignored\n");
  270. break;
  271. }
  272. }
  273. present |= (1 << i);
  274. if (i == LAYOUT)
  275. l_ndx_present |= 1 << ndx;
  276. if (i == VARIANT)
  277. v_ndx_present |= 1 << ndx;
  278. remap->remap[remap->num_remap].word = i;
  279. remap->remap[remap->num_remap++].index = ndx;
  280. break;
  281. }
  282. }
  283. if (!found) {
  284. fprintf(stderr, "Unknown component \"%s\" ignored\n", tok);
  285. }
  286. }
  287. if ((present & PART_MASK) == 0) {
  288. unsigned mask = PART_MASK;
  289. ErrorF("Mapping needs at least one of ");
  290. for (i = 0; (i < MAX_WORDS); i++) {
  291. if ((1L << i) & mask) {
  292. mask &= ~(1L << i);
  293. if (mask)
  294. DebugF("\"%s,\" ", cname[i]);
  295. else
  296. DebugF("or \"%s\"\n", cname[i]);
  297. }
  298. }
  299. DebugF("Illegal mapping ignored\n");
  300. remap->num_remap = 0;
  301. return;
  302. }
  303. if ((present & COMPONENT_MASK) == 0) {
  304. DebugF("Mapping needs at least one component\n");
  305. DebugF("Illegal mapping ignored\n");
  306. remap->num_remap = 0;
  307. return;
  308. }
  309. remap->number++;
  310. return;
  311. }
  312. static Bool
  313. MatchOneOf(const char *wanted, const char *vals_defined)
  314. {
  315. const char *str, *next;
  316. int want_len = strlen(wanted);
  317. for (str = vals_defined, next = NULL; str != NULL; str = next) {
  318. int len;
  319. next = strchr(str, ',');
  320. if (next) {
  321. len = next - str;
  322. next++;
  323. }
  324. else {
  325. len = strlen(str);
  326. }
  327. if ((len == want_len) && (strncmp(wanted, str, len) == 0))
  328. return TRUE;
  329. }
  330. return FALSE;
  331. }
  332. /***====================================================================***/
  333. static Bool
  334. CheckLine(InputLine * line,
  335. RemapSpec * remap, XkbRF_RulePtr rule, XkbRF_GroupPtr group)
  336. {
  337. char *str, *tok;
  338. register int nread, i;
  339. FileSpec tmp;
  340. _Xstrtokparams strtok_buf;
  341. Bool append = FALSE;
  342. if (line->line[0] == '!') {
  343. if (line->line[1] == '$' ||
  344. (line->line[1] == ' ' && line->line[2] == '$')) {
  345. char *gname = strchr(line->line, '$');
  346. char *words = strchr(gname, ' ');
  347. if (!words)
  348. return FALSE;
  349. *words++ = '\0';
  350. for (; *words; words++) {
  351. if (*words != '=' && *words != ' ')
  352. break;
  353. }
  354. if (*words == '\0')
  355. return FALSE;
  356. group->name = Xstrdup(gname);
  357. group->words = Xstrdup(words);
  358. for (i = 1, words = group->words; *words; words++) {
  359. if (*words == ' ') {
  360. *words++ = '\0';
  361. i++;
  362. }
  363. }
  364. group->number = i;
  365. return TRUE;
  366. }
  367. else {
  368. SetUpRemap(line, remap);
  369. return FALSE;
  370. }
  371. }
  372. if (remap->num_remap == 0) {
  373. DebugF("Must have a mapping before first line of data\n");
  374. DebugF("Illegal line of data ignored\n");
  375. return FALSE;
  376. }
  377. memset((char *) &tmp, 0, sizeof(FileSpec));
  378. str = line->line;
  379. for (nread = 0; (tok = _XStrtok(str, " ", strtok_buf)) != NULL; nread++) {
  380. str = NULL;
  381. if (strcmp(tok, "=") == 0) {
  382. nread--;
  383. continue;
  384. }
  385. if (nread > remap->num_remap) {
  386. DebugF("Too many words on a line\n");
  387. DebugF("Extra word \"%s\" ignored\n", tok);
  388. continue;
  389. }
  390. tmp.name[remap->remap[nread].word] = tok;
  391. if (*tok == '+' || *tok == '|')
  392. append = TRUE;
  393. }
  394. if (nread < remap->num_remap) {
  395. DebugF("Too few words on a line: %s\n", line->line);
  396. DebugF("line ignored\n");
  397. return FALSE;
  398. }
  399. rule->flags = 0;
  400. rule->number = remap->number;
  401. if (tmp.name[OPTION])
  402. rule->flags |= XkbRF_Option;
  403. else if (append)
  404. rule->flags |= XkbRF_Append;
  405. else
  406. rule->flags |= XkbRF_Normal;
  407. rule->model = Xstrdup(tmp.name[MODEL]);
  408. rule->layout = Xstrdup(tmp.name[LAYOUT]);
  409. rule->variant = Xstrdup(tmp.name[VARIANT]);
  410. rule->option = Xstrdup(tmp.name[OPTION]);
  411. rule->keycodes = Xstrdup(tmp.name[KEYCODES]);
  412. rule->symbols = Xstrdup(tmp.name[SYMBOLS]);
  413. rule->types = Xstrdup(tmp.name[TYPES]);
  414. rule->compat = Xstrdup(tmp.name[COMPAT]);
  415. rule->geometry = Xstrdup(tmp.name[GEOMETRY]);
  416. rule->layout_num = rule->variant_num = 0;
  417. for (i = 0; i < nread; i++) {
  418. if (remap->remap[i].index) {
  419. if (remap->remap[i].word == LAYOUT)
  420. rule->layout_num = remap->remap[i].index;
  421. if (remap->remap[i].word == VARIANT)
  422. rule->variant_num = remap->remap[i].index;
  423. }
  424. }
  425. return TRUE;
  426. }
  427. static char *
  428. _Concat(char *str1, const char *str2)
  429. {
  430. int len;
  431. if ((!str1) || (!str2))
  432. return str1;
  433. len = strlen(str1) + strlen(str2) + 1;
  434. str1 = realloc(str1, len * sizeof(char));
  435. if (str1)
  436. strcat(str1, str2);
  437. return str1;
  438. }
  439. static void
  440. squeeze_spaces(char *p1)
  441. {
  442. char *p2;
  443. for (p2 = p1; *p2; p2++) {
  444. *p1 = *p2;
  445. if (*p1 != ' ')
  446. p1++;
  447. }
  448. *p1 = '\0';
  449. }
  450. static Bool
  451. MakeMultiDefs(XkbRF_MultiDefsPtr mdefs, XkbRF_VarDefsPtr defs)
  452. {
  453. char *options;
  454. memset((char *) mdefs, 0, sizeof(XkbRF_MultiDefsRec));
  455. mdefs->model = defs->model;
  456. options = Xstrdup(defs->options);
  457. if (options)
  458. squeeze_spaces(options);
  459. mdefs->options = options;
  460. if (defs->layout) {
  461. if (!strchr(defs->layout, ',')) {
  462. mdefs->layout[0] = defs->layout;
  463. }
  464. else {
  465. char *p;
  466. char *layout;
  467. int i;
  468. layout = Xstrdup(defs->layout);
  469. if (layout == NULL)
  470. return FALSE;
  471. squeeze_spaces(layout);
  472. mdefs->layout[1] = layout;
  473. p = layout;
  474. for (i = 2; i <= XkbNumKbdGroups; i++) {
  475. if ((p = strchr(p, ','))) {
  476. *p++ = '\0';
  477. mdefs->layout[i] = p;
  478. }
  479. else {
  480. break;
  481. }
  482. }
  483. if (p && (p = strchr(p, ',')))
  484. *p = '\0';
  485. }
  486. }
  487. if (defs->variant) {
  488. if (!strchr(defs->variant, ',')) {
  489. mdefs->variant[0] = defs->variant;
  490. }
  491. else {
  492. char *p;
  493. char *variant;
  494. int i;
  495. variant = Xstrdup(defs->variant);
  496. if (variant == NULL)
  497. return FALSE;
  498. squeeze_spaces(variant);
  499. mdefs->variant[1] = variant;
  500. p = variant;
  501. for (i = 2; i <= XkbNumKbdGroups; i++) {
  502. if ((p = strchr(p, ','))) {
  503. *p++ = '\0';
  504. mdefs->variant[i] = p;
  505. }
  506. else {
  507. break;
  508. }
  509. }
  510. if (p && (p = strchr(p, ',')))
  511. *p = '\0';
  512. }
  513. }
  514. return TRUE;
  515. }
  516. static void
  517. FreeMultiDefs(XkbRF_MultiDefsPtr defs)
  518. {
  519. free((void *) defs->options);
  520. free((void *) defs->layout[1]);
  521. free((void *) defs->variant[1]);
  522. }
  523. static void
  524. Apply(const char *src, char **dst)
  525. {
  526. if (src) {
  527. if (*src == '+' || *src == '!') {
  528. *dst = _Concat(*dst, src);
  529. }
  530. else {
  531. if (*dst == NULL)
  532. *dst = Xstrdup(src);
  533. }
  534. }
  535. }
  536. static void
  537. XkbRF_ApplyRule(XkbRF_RulePtr rule, XkbComponentNamesPtr names)
  538. {
  539. rule->flags &= ~XkbRF_PendingMatch; /* clear the flag because it's applied */
  540. Apply(rule->keycodes, &names->keycodes);
  541. Apply(rule->symbols, &names->symbols);
  542. Apply(rule->types, &names->types);
  543. Apply(rule->compat, &names->compat);
  544. Apply(rule->geometry, &names->geometry);
  545. }
  546. static Bool
  547. CheckGroup(XkbRF_RulesPtr rules, const char *group_name, const char *name)
  548. {
  549. int i;
  550. char *p;
  551. XkbRF_GroupPtr group;
  552. for (i = 0, group = rules->groups; i < rules->num_groups; i++, group++) {
  553. if (!strcmp(group->name, group_name)) {
  554. break;
  555. }
  556. }
  557. if (i == rules->num_groups)
  558. return FALSE;
  559. for (i = 0, p = group->words; i < group->number; i++, p += strlen(p) + 1) {
  560. if (!strcmp(p, name)) {
  561. return TRUE;
  562. }
  563. }
  564. return FALSE;
  565. }
  566. static int
  567. XkbRF_CheckApplyRule(XkbRF_RulePtr rule,
  568. XkbRF_MultiDefsPtr mdefs,
  569. XkbComponentNamesPtr names, XkbRF_RulesPtr rules)
  570. {
  571. Bool pending = FALSE;
  572. if (rule->model != NULL) {
  573. if (mdefs->model == NULL)
  574. return 0;
  575. if (strcmp(rule->model, "*") == 0) {
  576. pending = TRUE;
  577. }
  578. else {
  579. if (rule->model[0] == '$') {
  580. if (!CheckGroup(rules, rule->model, mdefs->model))
  581. return 0;
  582. }
  583. else {
  584. if (strcmp(rule->model, mdefs->model) != 0)
  585. return 0;
  586. }
  587. }
  588. }
  589. if (rule->option != NULL) {
  590. if (mdefs->options == NULL)
  591. return 0;
  592. if ((!MatchOneOf(rule->option, mdefs->options)))
  593. return 0;
  594. }
  595. if (rule->layout != NULL) {
  596. if (mdefs->layout[rule->layout_num] == NULL ||
  597. *mdefs->layout[rule->layout_num] == '\0')
  598. return 0;
  599. if (strcmp(rule->layout, "*") == 0) {
  600. pending = TRUE;
  601. }
  602. else {
  603. if (rule->layout[0] == '$') {
  604. if (!CheckGroup(rules, rule->layout,
  605. mdefs->layout[rule->layout_num]))
  606. return 0;
  607. }
  608. else {
  609. if (strcmp(rule->layout, mdefs->layout[rule->layout_num]) != 0)
  610. return 0;
  611. }
  612. }
  613. }
  614. if (rule->variant != NULL) {
  615. if (mdefs->variant[rule->variant_num] == NULL ||
  616. *mdefs->variant[rule->variant_num] == '\0')
  617. return 0;
  618. if (strcmp(rule->variant, "*") == 0) {
  619. pending = TRUE;
  620. }
  621. else {
  622. if (rule->variant[0] == '$') {
  623. if (!CheckGroup(rules, rule->variant,
  624. mdefs->variant[rule->variant_num]))
  625. return 0;
  626. }
  627. else {
  628. if (strcmp(rule->variant,
  629. mdefs->variant[rule->variant_num]) != 0)
  630. return 0;
  631. }
  632. }
  633. }
  634. if (pending) {
  635. rule->flags |= XkbRF_PendingMatch;
  636. return rule->number;
  637. }
  638. /* exact match, apply it now */
  639. XkbRF_ApplyRule(rule, names);
  640. return rule->number;
  641. }
  642. static void
  643. XkbRF_ClearPartialMatches(XkbRF_RulesPtr rules)
  644. {
  645. register int i;
  646. XkbRF_RulePtr rule;
  647. for (i = 0, rule = rules->rules; i < rules->num_rules; i++, rule++) {
  648. rule->flags &= ~XkbRF_PendingMatch;
  649. }
  650. }
  651. static void
  652. XkbRF_ApplyPartialMatches(XkbRF_RulesPtr rules, XkbComponentNamesPtr names)
  653. {
  654. int i;
  655. XkbRF_RulePtr rule;
  656. for (rule = rules->rules, i = 0; i < rules->num_rules; i++, rule++) {
  657. if ((rule->flags & XkbRF_PendingMatch) == 0)
  658. continue;
  659. XkbRF_ApplyRule(rule, names);
  660. }
  661. }
  662. static void
  663. XkbRF_CheckApplyRules(XkbRF_RulesPtr rules,
  664. XkbRF_MultiDefsPtr mdefs,
  665. XkbComponentNamesPtr names, int flags)
  666. {
  667. int i;
  668. XkbRF_RulePtr rule;
  669. int skip;
  670. for (rule = rules->rules, i = 0; i < rules->num_rules; rule++, i++) {
  671. if ((rule->flags & flags) != flags)
  672. continue;
  673. skip = XkbRF_CheckApplyRule(rule, mdefs, names, rules);
  674. if (skip && !(flags & XkbRF_Option)) {
  675. for (; (i < rules->num_rules) && (rule->number == skip);
  676. rule++, i++);
  677. rule--;
  678. i--;
  679. }
  680. }
  681. }
  682. /***====================================================================***/
  683. static char *
  684. XkbRF_SubstituteVars(char *name, XkbRF_MultiDefsPtr mdefs)
  685. {
  686. char *str, *outstr, *orig, *var;
  687. int len, ndx;
  688. orig = name;
  689. str = index(name, '%');
  690. if (str == NULL)
  691. return name;
  692. len = strlen(name);
  693. while (str != NULL) {
  694. char pfx = str[1];
  695. int extra_len = 0;
  696. if ((pfx == '+') || (pfx == '|') || (pfx == '_') || (pfx == '-')) {
  697. extra_len = 1;
  698. str++;
  699. }
  700. else if (pfx == '(') {
  701. extra_len = 2;
  702. str++;
  703. }
  704. var = str + 1;
  705. str = get_index(var + 1, &ndx);
  706. if (ndx == -1) {
  707. str = index(str, '%');
  708. continue;
  709. }
  710. if ((*var == 'l') && mdefs->layout[ndx] && *mdefs->layout[ndx])
  711. len += strlen(mdefs->layout[ndx]) + extra_len;
  712. else if ((*var == 'm') && mdefs->model)
  713. len += strlen(mdefs->model) + extra_len;
  714. else if ((*var == 'v') && mdefs->variant[ndx] && *mdefs->variant[ndx])
  715. len += strlen(mdefs->variant[ndx]) + extra_len;
  716. if ((pfx == '(') && (*str == ')')) {
  717. str++;
  718. }
  719. str = index(&str[0], '%');
  720. }
  721. name = malloc(len + 1);
  722. str = orig;
  723. outstr = name;
  724. while (*str != '\0') {
  725. if (str[0] == '%') {
  726. char pfx, sfx;
  727. str++;
  728. pfx = str[0];
  729. sfx = '\0';
  730. if ((pfx == '+') || (pfx == '|') || (pfx == '_') || (pfx == '-')) {
  731. str++;
  732. }
  733. else if (pfx == '(') {
  734. sfx = ')';
  735. str++;
  736. }
  737. else
  738. pfx = '\0';
  739. var = str;
  740. str = get_index(var + 1, &ndx);
  741. if (ndx == -1) {
  742. continue;
  743. }
  744. if ((*var == 'l') && mdefs->layout[ndx] && *mdefs->layout[ndx]) {
  745. if (pfx)
  746. *outstr++ = pfx;
  747. strcpy(outstr, mdefs->layout[ndx]);
  748. outstr += strlen(mdefs->layout[ndx]);
  749. if (sfx)
  750. *outstr++ = sfx;
  751. }
  752. else if ((*var == 'm') && (mdefs->model)) {
  753. if (pfx)
  754. *outstr++ = pfx;
  755. strcpy(outstr, mdefs->model);
  756. outstr += strlen(mdefs->model);
  757. if (sfx)
  758. *outstr++ = sfx;
  759. }
  760. else if ((*var == 'v') && mdefs->variant[ndx] &&
  761. *mdefs->variant[ndx]) {
  762. if (pfx)
  763. *outstr++ = pfx;
  764. strcpy(outstr, mdefs->variant[ndx]);
  765. outstr += strlen(mdefs->variant[ndx]);
  766. if (sfx)
  767. *outstr++ = sfx;
  768. }
  769. if ((pfx == '(') && (*str == ')'))
  770. str++;
  771. }
  772. else {
  773. *outstr++ = *str++;
  774. }
  775. }
  776. *outstr++ = '\0';
  777. if (orig != name)
  778. free(orig);
  779. return name;
  780. }
  781. /***====================================================================***/
  782. Bool
  783. XkbRF_GetComponents(XkbRF_RulesPtr rules,
  784. XkbRF_VarDefsPtr defs, XkbComponentNamesPtr names)
  785. {
  786. XkbRF_MultiDefsRec mdefs;
  787. MakeMultiDefs(&mdefs, defs);
  788. memset((char *) names, 0, sizeof(XkbComponentNamesRec));
  789. XkbRF_ClearPartialMatches(rules);
  790. XkbRF_CheckApplyRules(rules, &mdefs, names, XkbRF_Normal);
  791. XkbRF_ApplyPartialMatches(rules, names);
  792. XkbRF_CheckApplyRules(rules, &mdefs, names, XkbRF_Append);
  793. XkbRF_ApplyPartialMatches(rules, names);
  794. XkbRF_CheckApplyRules(rules, &mdefs, names, XkbRF_Option);
  795. XkbRF_ApplyPartialMatches(rules, names);
  796. if (names->keycodes)
  797. names->keycodes = XkbRF_SubstituteVars(names->keycodes, &mdefs);
  798. if (names->symbols)
  799. names->symbols = XkbRF_SubstituteVars(names->symbols, &mdefs);
  800. if (names->types)
  801. names->types = XkbRF_SubstituteVars(names->types, &mdefs);
  802. if (names->compat)
  803. names->compat = XkbRF_SubstituteVars(names->compat, &mdefs);
  804. if (names->geometry)
  805. names->geometry = XkbRF_SubstituteVars(names->geometry, &mdefs);
  806. FreeMultiDefs(&mdefs);
  807. return (names->keycodes && names->symbols && names->types &&
  808. names->compat && names->geometry);
  809. }
  810. static XkbRF_RulePtr
  811. XkbRF_AddRule(XkbRF_RulesPtr rules)
  812. {
  813. if (rules->sz_rules < 1) {
  814. rules->sz_rules = 16;
  815. rules->num_rules = 0;
  816. rules->rules = calloc(rules->sz_rules, sizeof(XkbRF_RuleRec));
  817. }
  818. else if (rules->num_rules >= rules->sz_rules) {
  819. rules->sz_rules *= 2;
  820. rules->rules = realloc(rules->rules,
  821. rules->sz_rules * sizeof(XkbRF_RuleRec));
  822. }
  823. if (!rules->rules) {
  824. rules->sz_rules = rules->num_rules = 0;
  825. DebugF("Allocation failure in XkbRF_AddRule\n");
  826. return NULL;
  827. }
  828. memset((char *) &rules->rules[rules->num_rules], 0, sizeof(XkbRF_RuleRec));
  829. return &rules->rules[rules->num_rules++];
  830. }
  831. static XkbRF_GroupPtr
  832. XkbRF_AddGroup(XkbRF_RulesPtr rules)
  833. {
  834. if (rules->sz_groups < 1) {
  835. rules->sz_groups = 16;
  836. rules->num_groups = 0;
  837. rules->groups = calloc(rules->sz_groups, sizeof(XkbRF_GroupRec));
  838. }
  839. else if (rules->num_groups >= rules->sz_groups) {
  840. rules->sz_groups *= 2;
  841. rules->groups = realloc(rules->groups,
  842. rules->sz_groups * sizeof(XkbRF_GroupRec));
  843. }
  844. if (!rules->groups) {
  845. rules->sz_groups = rules->num_groups = 0;
  846. return NULL;
  847. }
  848. memset((char *) &rules->groups[rules->num_groups], 0,
  849. sizeof(XkbRF_GroupRec));
  850. return &rules->groups[rules->num_groups++];
  851. }
  852. Bool
  853. XkbRF_LoadRules(FILE * file, XkbRF_RulesPtr rules)
  854. {
  855. InputLine line;
  856. RemapSpec remap;
  857. XkbRF_RuleRec trule, *rule;
  858. XkbRF_GroupRec tgroup, *group;
  859. if (!(rules && file))
  860. return FALSE;
  861. memset((char *) &remap, 0, sizeof(RemapSpec));
  862. memset((char *) &tgroup, 0, sizeof(XkbRF_GroupRec));
  863. InitInputLine(&line);
  864. while (GetInputLine(file, &line, TRUE)) {
  865. if (CheckLine(&line, &remap, &trule, &tgroup)) {
  866. if (tgroup.number) {
  867. if ((group = XkbRF_AddGroup(rules)) != NULL) {
  868. *group = tgroup;
  869. memset((char *) &tgroup, 0, sizeof(XkbRF_GroupRec));
  870. }
  871. }
  872. else {
  873. if ((rule = XkbRF_AddRule(rules)) != NULL) {
  874. *rule = trule;
  875. memset((char *) &trule, 0, sizeof(XkbRF_RuleRec));
  876. }
  877. }
  878. }
  879. line.num_line = 0;
  880. }
  881. FreeInputLine(&line);
  882. return TRUE;
  883. }
  884. Bool
  885. XkbRF_LoadRulesByName(char *base, char *locale, XkbRF_RulesPtr rules)
  886. {
  887. FILE *file;
  888. char buf[PATH_MAX];
  889. Bool ok;
  890. if ((!base) || (!rules))
  891. return FALSE;
  892. if (locale) {
  893. if (snprintf(buf, PATH_MAX, "%s-%s", base, locale) >= PATH_MAX)
  894. return FALSE;
  895. }
  896. else {
  897. if (strlen(base) + 1 > PATH_MAX)
  898. return FALSE;
  899. strcpy(buf, base);
  900. }
  901. file = fopen(buf, "r");
  902. if ((!file) && (locale)) { /* fallback if locale was specified */
  903. strcpy(buf, base);
  904. file = fopen(buf, "r");
  905. }
  906. if (!file)
  907. return FALSE;
  908. ok = XkbRF_LoadRules(file, rules);
  909. fclose(file);
  910. return ok;
  911. }
  912. /***====================================================================***/
  913. XkbRF_RulesPtr
  914. XkbRF_Create(void)
  915. {
  916. return calloc(1, sizeof(XkbRF_RulesRec));
  917. }
  918. /***====================================================================***/
  919. void
  920. XkbRF_Free(XkbRF_RulesPtr rules, Bool freeRules)
  921. {
  922. int i;
  923. XkbRF_RulePtr rule;
  924. XkbRF_GroupPtr group;
  925. if (!rules)
  926. return;
  927. if (rules->rules) {
  928. for (i = 0, rule = rules->rules; i < rules->num_rules; i++, rule++) {
  929. free((void *) rule->model);
  930. free((void *) rule->layout);
  931. free((void *) rule->variant);
  932. free((void *) rule->option);
  933. free((void *) rule->keycodes);
  934. free((void *) rule->symbols);
  935. free((void *) rule->types);
  936. free((void *) rule->compat);
  937. free((void *) rule->geometry);
  938. memset((char *) rule, 0, sizeof(XkbRF_RuleRec));
  939. }
  940. free(rules->rules);
  941. rules->num_rules = rules->sz_rules = 0;
  942. rules->rules = NULL;
  943. }
  944. if (rules->groups) {
  945. for (i = 0, group = rules->groups; i < rules->num_groups; i++, group++) {
  946. free((void *) group->name);
  947. free(group->words);
  948. }
  949. free(rules->groups);
  950. rules->num_groups = 0;
  951. rules->groups = NULL;
  952. }
  953. if (freeRules)
  954. free(rules);
  955. return;
  956. }