_fallcFile.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* lcFile.c 1.1 - Fujitsu source for CDEnext 95/11/06 20:32:36 */
  24. /* $XConsortium: _fallcFile.c /main/1 1996/04/08 15:16:48 cde-fuj $ */
  25. /*
  26. *
  27. * Copyright IBM Corporation 1993
  28. *
  29. * All Rights Reserved
  30. *
  31. * License to use, copy, modify, and distribute this software and its
  32. * documentation for any purpose and without fee is hereby granted,
  33. * provided that the above copyright notice appear in all copies and that
  34. * both that copyright notice and this permission notice appear in
  35. * supporting documentation, and that the name of IBM not be
  36. * used in advertising or publicity pertaining to distribution of the
  37. * software without specific, written prior permission.
  38. *
  39. * IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  40. * ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS, AND
  41. * NONINFRINGEMENT OF THIRD PARTY RIGHTS, IN NO EVENT SHALL
  42. * IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  43. * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  44. * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  45. * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  46. * SOFTWARE.
  47. *
  48. */
  49. #include <stdio.h>
  50. #include <ctype.h>
  51. #include "_fallibint.h"
  52. #include "_fallcPubI.h"
  53. #include <X11/Xos.h>
  54. /************************************************************************/
  55. #define iscomment(ch) ((ch) == '#' || (ch) == '\0')
  56. #define isreadable(f) ((access((f), R_OK) != -1) ? 1 : 0)
  57. /*
  58. #define isspace(ch) ((ch) == ' ' || (ch) == '\t' || (ch) == '\n')
  59. */
  60. static int
  61. parse_line(char *line, char **argv, int argsize)
  62. {
  63. int argc = 0;
  64. char *p = line;
  65. while(argc < argsize){
  66. while(isspace(*p)){
  67. ++p;
  68. }
  69. if(*p == '\0'){
  70. break;
  71. }
  72. argv[argc++] = p;
  73. while(! isspace(*p) && *p != '\0'){
  74. ++p;
  75. }
  76. if(*p == '\0'){
  77. break;
  78. }
  79. *p++ = '\0';
  80. }
  81. return argc;
  82. }
  83. #ifndef XLOCALEDIR
  84. #define XLOCALEDIR "/usr/lib/X11/locale"
  85. #endif
  86. static void
  87. xlocaledir(char *path)
  88. {
  89. char *dir, *p = path;
  90. int len;
  91. dir = getenv("XLOCALEDIR");
  92. if(dir != NULL){
  93. len = strlen(dir);
  94. strcpy(p, dir);
  95. p[len++] = ':';
  96. p += len;
  97. }
  98. strcpy(p, XLOCALEDIR);
  99. }
  100. static int
  101. parse_path(char *path, char **argv, int argsize)
  102. {
  103. char *p = path;
  104. int i, n;
  105. while((p = strchr(p, ':')) != NULL){
  106. *p = ' '; /* place space on delimter */
  107. }
  108. n = parse_line(path, argv, argsize);
  109. if(n == 0){
  110. return 0;
  111. }
  112. for(i = 0; i < n; ++i){
  113. int len;
  114. p = argv[i];
  115. len = strlen(p);
  116. if(p[len - 1] == '/'){
  117. /* eliminate slash */
  118. p[len - 1] = '\0';
  119. }
  120. }
  121. return n;
  122. }
  123. enum { LtoR, RtoL };
  124. static char *
  125. _XlcResolveName(char *lc_name, char *file_name, int direction)/*mapping direction*/
  126. {
  127. FILE *fp;
  128. char buf[BUFSIZE], *name = NULL;
  129. fp = fopen(file_name, "r");
  130. if(fp == (FILE *)NULL){
  131. return NULL;
  132. }
  133. while(fgets(buf, BUFSIZE, fp) != NULL){
  134. char *p = buf;
  135. int n;
  136. char *args[2], *from, *to;
  137. while(isspace(*p)){
  138. ++p;
  139. }
  140. if(iscomment(*p)){
  141. continue;
  142. }
  143. n = parse_line(p, args, 2); /* get first 2 fields */
  144. if(n != 2){
  145. continue;
  146. }
  147. if(direction == LtoR){
  148. from = args[0], to = args[1]; /* left to right */
  149. }else{
  150. from = args[1], to = args[0]; /* right to left */
  151. }
  152. if(! strcmp(from, lc_name)){
  153. name = Xmalloc(strlen(to) + 1);
  154. if(name != NULL){
  155. strcpy(name, to);
  156. }
  157. break;
  158. }
  159. }
  160. if(fp != (FILE *)NULL){
  161. fclose(fp);
  162. }
  163. return name;
  164. }
  165. /*
  166. #define isupper(ch) ('A' <= (ch) && (ch) <= 'Z')
  167. #define tolower(ch) ((ch) - 'A' + 'a')
  168. */
  169. static char *
  170. lowercase(char *dst, char *src)
  171. {
  172. char *s, *t;
  173. for(s = src, t = dst; *s; ++s, ++t){
  174. *t = isupper(*s) ? tolower(*s) : *s;
  175. }
  176. *t = '\0';
  177. return dst;
  178. }
  179. /************************************************************************/
  180. char *
  181. _fallcFileName(XLCd lcd, char *category)
  182. {
  183. char lc_name[BUFSIZE];
  184. char cat[BUFSIZE], dir[BUFSIZE];
  185. int i, n;
  186. char *args[256];
  187. char *file_name = NULL;
  188. if(lcd == (XLCd)NULL){
  189. return NULL;
  190. }
  191. if(! _fallcResolveLocaleName(XLC_PUBLIC(lcd, siname), lc_name,
  192. NULL, NULL, NULL)){
  193. return NULL;
  194. }
  195. lowercase(cat, category);
  196. xlocaledir(dir);
  197. n = parse_path(dir, args, 256);
  198. for(i = 0; i < n; ++i){
  199. char buf[BUFSIZE], *name;
  200. sprintf(buf, "%s/%s.dir", args[i], cat);
  201. name = _XlcResolveName(lc_name, buf, RtoL);
  202. if(name == NULL){
  203. continue;
  204. }
  205. if(*name == '/'){
  206. /* supposed to be absolute path name */
  207. file_name = name;
  208. }else{
  209. sprintf(buf, "%s/%s", args[i], name);
  210. Xfree(name);
  211. file_name = Xmalloc(strlen(buf) + 1);
  212. if(file_name == NULL){
  213. break;
  214. }
  215. strcpy(file_name, buf);
  216. }
  217. if(isreadable(file_name)){
  218. break;
  219. }
  220. Xfree(file_name);
  221. file_name = NULL;
  222. /* Then, try with next dir */
  223. }
  224. return file_name;
  225. }
  226. /************************************************************************/
  227. #ifndef LOCALE_ALIAS
  228. #define LOCALE_ALIAS "locale.alias"
  229. #endif
  230. int
  231. _fallcResolveLocaleName(
  232. char *lc_name,
  233. char *full_name,
  234. char *language,
  235. char *territory,
  236. char *codeset)
  237. {
  238. char dir[BUFSIZE], buf[BUFSIZE], *name = NULL;
  239. int i, n;
  240. char *args[256];
  241. xlocaledir(dir);
  242. n = parse_path(dir, args, 256);
  243. for(i = 0; i < n; ++i){
  244. sprintf(buf, "%s/%s", args[i], LOCALE_ALIAS);
  245. name = _XlcResolveName(lc_name, buf, LtoR);
  246. if(name != NULL){
  247. break;
  248. }
  249. }
  250. if(name != NULL){
  251. snprintf(buf, sizeof(buf), "%s", name);
  252. Xfree(name);
  253. }else{
  254. snprintf(buf, sizeof(buf), "%s", lc_name);
  255. }
  256. if(full_name != NULL){
  257. strcpy(full_name, buf);
  258. }
  259. if(language || territory || codeset){
  260. char *ptr, *name_p;
  261. /*
  262. * Decompose locale name
  263. */
  264. if(language) *language = '\0';
  265. if(territory) *territory = '\0';
  266. if(codeset) *codeset = '\0';
  267. name_p = buf;
  268. ptr = language;
  269. while (1) {
  270. if (*name_p == '_') {
  271. if (ptr)
  272. *ptr = '\0';
  273. ptr = territory;
  274. } else if (*name_p == '.') {
  275. if (ptr)
  276. *ptr = '\0';
  277. ptr = codeset;
  278. } else {
  279. if (ptr)
  280. *ptr++ = *name_p;
  281. if (*name_p == '\0')
  282. break;
  283. }
  284. name_p++;
  285. }
  286. }
  287. return (buf[0] != '\0') ? 1 : 0;
  288. }
  289. /************************************************************************/
  290. #ifndef LOCALE_DIR
  291. #define LOCALE_DIR "locale.dir"
  292. #endif
  293. int
  294. _fallcResolveDBName(char *lc_name, char *file_name)
  295. {
  296. char dir[BUFSIZE], buf[BUFSIZE], *name = NULL;
  297. int i, n;
  298. char *args[256];
  299. xlocaledir(dir);
  300. n = parse_path(dir, args, 256);
  301. for(i = 0; i < n; ++i){
  302. sprintf(buf, "%s/%s", args[i], LOCALE_DIR);
  303. name = _XlcResolveName(lc_name, buf, RtoL);
  304. if(name != NULL){
  305. break;
  306. }
  307. }
  308. if(name == NULL){
  309. return 0;
  310. }
  311. strcpy(buf, name);
  312. Xfree(name);
  313. if(file_name != NULL){
  314. strcpy(file_name, buf);
  315. }
  316. return 1;
  317. }
  318. /************************************************************************/
  319. int
  320. _fallcResolveI18NPath(char *path_name)
  321. {
  322. if(path_name != NULL){
  323. xlocaledir(path_name);
  324. }
  325. return 1;
  326. }