xkbpath.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /************************************************************
  2. Copyright (c) 1994 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. #include <X11/Xlib.h>
  24. #include <X11/XKBlib.h>
  25. #define DEBUG_VAR debugFlags
  26. #include "utils.h"
  27. #include <stdlib.h>
  28. #include <X11/extensions/XKM.h>
  29. #include "xkbpath.h"
  30. #ifndef DFLT_XKB_CONFIG_ROOT
  31. #define DFLT_XKB_CONFIG_ROOT "/usr/lib/X11/xkb"
  32. #endif
  33. #ifndef PATH_MAX
  34. #define PATH_MAX 1024
  35. #endif
  36. #define PATH_CHUNK 8 /* initial szPath */
  37. static Bool noDefaultPath = False;
  38. static int szPath; /* number of entries allocated for includePath */
  39. static int nPathEntries; /* number of actual entries in includePath */
  40. static char **includePath; /* Holds all directories we might be including data from */
  41. /**
  42. * Extract the first token from an include statement.
  43. * @param str_inout Input statement, modified in-place. Can be passed in
  44. * repeatedly. If str_inout is NULL, the parsing has completed.
  45. * @param file_rtrn Set to the include file to be used.
  46. * @param map_rtrn Set to whatever comes after ), if any.
  47. * @param nextop_rtrn Set to the next operation in the complete statement.
  48. * @param extra_data Set to the string between ( and ), if any.
  49. *
  50. * @return True if parsing was succcessful, False for an illegal string.
  51. *
  52. * Example: "evdev+aliases(qwerty)"
  53. * str_inout = aliases(qwerty)
  54. * nextop_retrn = +
  55. * extra_data = NULL
  56. * file_rtrn = evdev
  57. * map_rtrn = NULL
  58. *
  59. * 2nd run with "aliases(qwerty)"
  60. * str_inout = NULL
  61. * file_rtrn = aliases
  62. * map_rtrn = qwerty
  63. * extra_data = NULL
  64. * nextop_retrn = ""
  65. *
  66. */
  67. Bool
  68. XkbParseIncludeMap(char **str_inout, char **file_rtrn, char **map_rtrn,
  69. char *nextop_rtrn, char **extra_data)
  70. {
  71. char *tmp, *str, *next;
  72. str = *str_inout;
  73. if ((*str == '+') || (*str == '|'))
  74. {
  75. *file_rtrn = *map_rtrn = NULL;
  76. *nextop_rtrn = *str;
  77. next = str + 1;
  78. }
  79. else if (*str == '%')
  80. {
  81. *file_rtrn = *map_rtrn = NULL;
  82. *nextop_rtrn = str[1];
  83. next = str + 2;
  84. }
  85. else
  86. {
  87. /* search for tokens inside the string */
  88. next = strpbrk(str, "|+");
  89. if (next)
  90. {
  91. /* set nextop_rtrn to \0, next to next character */
  92. *nextop_rtrn = *next;
  93. *next++ = '\0';
  94. }
  95. else
  96. {
  97. *nextop_rtrn = '\0';
  98. next = NULL;
  99. }
  100. /* search for :, store result in extra_data */
  101. tmp = strchr(str, ':');
  102. if (tmp != NULL)
  103. {
  104. *tmp++ = '\0';
  105. *extra_data = uStringDup(tmp);
  106. }
  107. else
  108. {
  109. *extra_data = NULL;
  110. }
  111. tmp = strchr(str, '(');
  112. if (tmp == NULL)
  113. {
  114. *file_rtrn = uStringDup(str);
  115. *map_rtrn = NULL;
  116. }
  117. else if (str[0] == '(')
  118. {
  119. uFree(*extra_data);
  120. return False;
  121. }
  122. else
  123. {
  124. *tmp++ = '\0';
  125. *file_rtrn = uStringDup(str);
  126. str = tmp;
  127. tmp = strchr(str, ')');
  128. if ((tmp == NULL) || (tmp[1] != '\0'))
  129. {
  130. uFree(*file_rtrn);
  131. uFree(*extra_data);
  132. return False;
  133. }
  134. *tmp++ = '\0';
  135. *map_rtrn = uStringDup(str);
  136. }
  137. }
  138. if (*nextop_rtrn == '\0')
  139. *str_inout = NULL;
  140. else if ((*nextop_rtrn == '|') || (*nextop_rtrn == '+'))
  141. *str_inout = next;
  142. else
  143. return False;
  144. return True;
  145. }
  146. /**
  147. * Init memory for include paths.
  148. */
  149. Bool
  150. XkbInitIncludePath(void)
  151. {
  152. szPath = PATH_CHUNK;
  153. includePath = (char **) calloc(szPath, sizeof(char *));
  154. if (includePath == NULL)
  155. return False;
  156. return True;
  157. }
  158. void
  159. XkbAddDefaultDirectoriesToPath(void)
  160. {
  161. if (noDefaultPath)
  162. return;
  163. XkbAddDirectoryToPath(DFLT_XKB_CONFIG_ROOT);
  164. }
  165. /**
  166. * Remove all entries from the global includePath.
  167. */
  168. void
  169. XkbClearIncludePath(void)
  170. {
  171. register int i;
  172. if (szPath > 0)
  173. {
  174. for (i = 0; i < nPathEntries; i++)
  175. {
  176. if (includePath[i] != NULL)
  177. {
  178. uFree(includePath[i]);
  179. includePath[i] = NULL;
  180. }
  181. }
  182. nPathEntries = 0;
  183. }
  184. noDefaultPath = True;
  185. return;
  186. }
  187. /**
  188. * Add the given path to the global includePath variable.
  189. * If dir is NULL, the includePath is emptied.
  190. */
  191. Bool
  192. XkbAddDirectoryToPath(const char *dir)
  193. {
  194. int len;
  195. if ((dir == NULL) || (dir[0] == '\0'))
  196. {
  197. XkbClearIncludePath();
  198. return True;
  199. }
  200. len = strlen(dir);
  201. if (len + 2 >= PATH_MAX)
  202. { /* allow for '/' and at least one character */
  203. ERROR2("Path entry (%s) too long (maxiumum length is %d)\n",
  204. dir, PATH_MAX - 3);
  205. return False;
  206. }
  207. if (nPathEntries >= szPath)
  208. {
  209. szPath += PATH_CHUNK;
  210. includePath = (char **) realloc(includePath, szPath * sizeof(char *));
  211. if (includePath == NULL)
  212. {
  213. WSGO("Allocation failed (includePath)\n");
  214. return False;
  215. }
  216. }
  217. includePath[nPathEntries] =
  218. (char *) calloc(strlen(dir) + 1, sizeof(char));
  219. if (includePath[nPathEntries] == NULL)
  220. {
  221. WSGO1("Allocation failed (includePath[%d])\n", nPathEntries);
  222. return False;
  223. }
  224. strcpy(includePath[nPathEntries++], dir);
  225. return True;
  226. }
  227. /***====================================================================***/
  228. /**
  229. * Return the xkb directory based on the type.
  230. * Do not free the memory returned by this function.
  231. */
  232. char *
  233. XkbDirectoryForInclude(unsigned type)
  234. {
  235. static char buf[32];
  236. switch (type)
  237. {
  238. case XkmSemanticsFile:
  239. strcpy(buf, "semantics");
  240. break;
  241. case XkmLayoutFile:
  242. strcpy(buf, "layout");
  243. break;
  244. case XkmKeymapFile:
  245. strcpy(buf, "keymap");
  246. break;
  247. case XkmKeyNamesIndex:
  248. strcpy(buf, "keycodes");
  249. break;
  250. case XkmTypesIndex:
  251. strcpy(buf, "types");
  252. break;
  253. case XkmSymbolsIndex:
  254. strcpy(buf, "symbols");
  255. break;
  256. case XkmCompatMapIndex:
  257. strcpy(buf, "compat");
  258. break;
  259. case XkmGeometryFile:
  260. case XkmGeometryIndex:
  261. strcpy(buf, "geometry");
  262. break;
  263. default:
  264. strcpy(buf, "");
  265. break;
  266. }
  267. return buf;
  268. }
  269. /***====================================================================***/
  270. typedef struct _FileCacheEntry
  271. {
  272. char *name;
  273. unsigned type;
  274. char *path;
  275. void *data;
  276. struct _FileCacheEntry *next;
  277. } FileCacheEntry;
  278. static FileCacheEntry *fileCache;
  279. /**
  280. * Add the file with the given name to the internal cache to avoid opening and
  281. * parsing the file multiple times. If a cache entry for the same name + type
  282. * is already present, the entry is overwritten and the data belonging to the
  283. * previous entry is returned.
  284. *
  285. * @parameter name The name of the file (e.g. evdev).
  286. * @parameter type Type of the file (XkbTypesIdx, ... or XkbSemanticsFile, ...)
  287. * @parameter path The full path to the file.
  288. * @parameter data Already parsed data.
  289. *
  290. * @return The data from the overwritten file or NULL.
  291. */
  292. void *
  293. XkbAddFileToCache(char *name, unsigned type, char *path, void *data)
  294. {
  295. FileCacheEntry *entry;
  296. for (entry = fileCache; entry != NULL; entry = entry->next)
  297. {
  298. if ((type == entry->type) && (uStringEqual(name, entry->name)))
  299. {
  300. void *old = entry->data;
  301. WSGO2("Replacing file cache entry (%s/%d)\n", name, type);
  302. entry->path = path;
  303. entry->data = data;
  304. return old;
  305. }
  306. }
  307. entry = uTypedAlloc(FileCacheEntry);
  308. if (entry != NULL)
  309. {
  310. entry->name = name;
  311. entry->type = type;
  312. entry->path = path;
  313. entry->data = data;
  314. entry->next = fileCache;
  315. fileCache = entry;
  316. }
  317. return NULL;
  318. }
  319. /**
  320. * Search for the given name + type in the cache.
  321. *
  322. * @parameter name The name of the file (e.g. evdev).
  323. * @parameter type Type of the file (XkbTypesIdx, ... or XkbSemanticsFile, ...)
  324. * @parameter pathRtrn Set to the full path of the given entry.
  325. *
  326. * @return the data from the cache entry or NULL if no matching entry was found.
  327. */
  328. void *
  329. XkbFindFileInCache(char *name, unsigned type, char **pathRtrn)
  330. {
  331. FileCacheEntry *entry;
  332. for (entry = fileCache; entry != NULL; entry = entry->next)
  333. {
  334. if ((type == entry->type) && (uStringEqual(name, entry->name)))
  335. {
  336. *pathRtrn = entry->path;
  337. return entry->data;
  338. }
  339. }
  340. return NULL;
  341. }
  342. /***====================================================================***/
  343. /**
  344. * Search for the given file name in the include directories.
  345. *
  346. * @param type one of XkbTypesIndex, XkbCompatMapIndex, ..., or
  347. * XkbSemanticsFile, XkmKeymapFile, ...
  348. * @param pathReturn is set to the full path of the file if found.
  349. *
  350. * @return an FD to the file or NULL. If NULL is returned, the value of
  351. * pathRtrn is undefined.
  352. */
  353. FILE *
  354. XkbFindFileInPath(char *name, unsigned type, char **pathRtrn)
  355. {
  356. register int i;
  357. FILE *file = NULL;
  358. int nameLen, typeLen, pathLen;
  359. char buf[PATH_MAX], *typeDir;
  360. typeDir = XkbDirectoryForInclude(type);
  361. nameLen = strlen(name);
  362. typeLen = strlen(typeDir);
  363. for (i = 0; i < nPathEntries; i++)
  364. {
  365. pathLen = strlen(includePath[i]);
  366. if (typeLen < 1)
  367. continue;
  368. if ((nameLen + typeLen + pathLen + 2) >= PATH_MAX)
  369. {
  370. ERROR3("File name (%s/%s/%s) too long\n", includePath[i],
  371. typeDir, name);
  372. ACTION("Ignored\n");
  373. continue;
  374. }
  375. snprintf(buf, sizeof(buf), "%s/%s/%s", includePath[i], typeDir, name);
  376. file = fopen(buf, "r");
  377. if (file != NULL)
  378. break;
  379. }
  380. if ((file != NULL) && (pathRtrn != NULL))
  381. {
  382. *pathRtrn = (char *) calloc(strlen(buf) + 1, sizeof(char));
  383. if (*pathRtrn != NULL)
  384. strcpy(*pathRtrn, buf);
  385. }
  386. return file;
  387. }