home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / evbl0627.zip / everblue_20010627.zip / x11 / Xlc_File.c < prev    next >
C/C++ Source or Header  |  1999-11-02  |  9KB  |  400 lines

  1. /* $TOG: lcFile.c /main/9 1997/06/03 15:52:47 kaleb $ */
  2. /*
  3.  *
  4.  * Copyright IBM Corporation 1993
  5.  *
  6.  * All Rights Reserved
  7.  *
  8.  * License to use, copy, modify, and distribute this software and its
  9.  * documentation for any purpose and without fee is hereby granted,
  10.  * provided that the above copyright notice appear in all copies and that
  11.  * both that copyright notice and this permission notice appear in
  12.  * supporting documentation, and that the name of IBM not be
  13.  * used in advertising or publicity pertaining to distribution of the
  14.  * software without specific, written prior permission.
  15.  *
  16.  * IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  17.  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS, AND 
  18.  * NONINFRINGEMENT OF THIRD PARTY RIGHTS, IN NO EVENT SHALL
  19.  * IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  20.  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21.  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  22.  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  23.  * SOFTWARE.
  24.  *
  25. */
  26. /* $XFree86: xc/lib/X11/lcFile.c,v 3.6.2.8 1998/10/04 13:36:25 hohndel Exp $ */
  27.  
  28. #include <stdio.h>
  29. #include <ctype.h>
  30. #include "Xlib_private.h"
  31. #include "XlcPubI.h"
  32. #include <X11/Xos.h>
  33. #ifdef X_NOT_STDC_ENV
  34. extern char *getenv();
  35. #endif
  36.  
  37. /************************************************************************/
  38.  
  39. #define    iscomment(ch)    ((ch) == '#' || (ch) == '\0')
  40. #define isreadable(f)    ((access((f), R_OK) != -1) ? 1 : 0)
  41.  
  42. #ifndef __EMX__
  43. #define LC_PATHDELIM ':'
  44. #else
  45. #define LC_PATHDELIM ';'
  46. #endif
  47.  
  48. #define XLC_BUFSIZE 256
  49.  
  50. #ifndef X_NOT_POSIX
  51. #ifdef _POSIX_SOURCE
  52. #include <limits.h>
  53. #else
  54. #define _POSIX_SOURCE
  55. #include <limits.h>
  56. #undef _POSIX_SOURCE
  57. #endif
  58. #endif
  59. #ifndef PATH_MAX
  60. #ifdef WIN32
  61. #define PATH_MAX 512
  62. #else
  63. #include <sys/param.h>
  64. #endif
  65. #ifndef PATH_MAX
  66. #ifdef MAXPATHLEN
  67. #define PATH_MAX MAXPATHLEN
  68. #else
  69. #define PATH_MAX 1024
  70. #endif
  71. #endif
  72. #endif
  73.  
  74. #define NUM_LOCALEDIR    64
  75.  
  76. static int
  77. parse_line(line, argv, argsize)
  78.     char *line;
  79.     char **argv;
  80.     int argsize;
  81. {
  82.     DBUG_ENTER("parse_line")
  83.     int argc = 0;
  84.     char *p = line;
  85.  
  86.     while(argc < argsize){
  87.     while(isspace(*p)){
  88.         ++p;
  89.     }
  90.     if(*p == '\0'){
  91.         break;
  92.     }
  93.     argv[argc++] = p;
  94.     while(! isspace(*p) && *p != '\0'){
  95.         ++p;
  96.     }
  97.     if(*p == '\0'){
  98.         break;
  99.     }
  100.     *p++ = '\0';
  101.     }
  102.  
  103.     DBUG_RETURN(argc);
  104. }
  105.  
  106. /* parse the colon separated list in path into argv */
  107. int
  108. _XlcParsePath(path, argv, argsize)
  109.     char *path;
  110.     char **argv;
  111.     int argsize;
  112. {
  113.     DBUG_ENTER("_XlcParsePath")
  114.     char *p = path;
  115.     int i, n;
  116.  
  117.     while((p = strchr(p, LC_PATHDELIM)) != NULL){
  118.     *p = ' ';    /* place space on delimter */
  119.     }
  120.     n = parse_line(path, argv, argsize);
  121.     if(n == 0){
  122.     DBUG_RETURN(0);
  123.     }
  124.     for(i = 0; i < n; ++i){
  125.     int len;
  126.     p = argv[i];
  127.     len = strlen(p);
  128.     if(p[len - 1] == '/'){
  129.         /* eliminate slash */
  130.         p[len - 1] = '\0';
  131.     }
  132.     }
  133.     DBUG_RETURN(n);
  134. }
  135.  
  136. #ifndef XLOCALEDIR
  137. #define XLOCALEDIR "/usr/lib/X11/locale"
  138. #endif
  139.  
  140. static void
  141. xlocaledir(buf, buf_len)
  142.     char *buf;
  143.     int buf_len;
  144. {
  145.     DBUG_ENTER("xlocaledir")
  146.     char *dir, *p = buf;
  147.     int len = 0;
  148.  
  149.     dir = getenv("XLOCALEDIR");
  150.     if(dir != NULL){
  151.     len = strlen(dir);
  152.     strncpy(p, dir, buf_len);
  153.     if (len < buf_len) {
  154.         p[len++] = LC_PATHDELIM;
  155.         p += len;
  156.     }
  157.     }
  158.     if (len < buf_len)
  159. #ifndef __EMX__
  160.       strncpy(p, XLOCALEDIR, buf_len - len);
  161. #else
  162.       strncpy(p,__XOS2RedirRoot(XLOCALEDIR), buf_len - len);
  163. #endif
  164.     buf[buf_len-1] = '\0';
  165.     DBUG_VOID_RETURN;
  166. }
  167.  
  168. enum { LtoR, RtoL };
  169.  
  170. static char *
  171. resolve_name(lc_name, file_name, direction)
  172.     char *lc_name;
  173.     char *file_name;
  174.     int direction;    /* mapping direction */
  175. {
  176.     DBUG_ENTER("resolve_name")
  177.     FILE *fp;
  178.     char buf[XLC_BUFSIZE], *name = NULL;
  179.  
  180.     fp = fopen(file_name, "r");
  181.     if(fp == (FILE *)NULL){
  182.     DBUG_RETURN(NULL);
  183.     }
  184.  
  185.     while(fgets(buf, XLC_BUFSIZE, fp) != NULL){
  186.     char *p = buf;
  187.     int n;
  188.     char *args[2], *from, *to;
  189. #ifdef __EMX__  /* Take out CR under OS/2 */
  190.     int len;
  191.  
  192.     len=strlen(p);
  193.     if (len>1) {
  194.       if (*(p+len-2) == '\r' && *(p+len-1) == '\n') {
  195.         *(p+len-2) = '\n';
  196.         *(p+len-1) = '\0';
  197.       }
  198.     }
  199. #endif
  200.     while(isspace(*p)){
  201.         ++p;
  202.     }
  203.     if(iscomment(*p)){
  204.         continue;
  205.     }
  206.     n = parse_line(p, args, 2);        /* get first 2 fields */
  207.     if(n != 2){
  208.         continue;
  209.     }
  210.     if(direction == LtoR){
  211.         from = args[0], to = args[1];    /* left to right */
  212.     }else{
  213.         from = args[1], to = args[0];    /* right to left */
  214.     }
  215.     if(! strcmp(from, lc_name)){
  216.         name = Xmalloc(strlen(to) + 1);
  217.         if(name != NULL){
  218.         strcpy(name, to);
  219.         }
  220.         break;
  221.     }
  222.     }
  223.     if(fp != (FILE *)NULL){
  224.     fclose(fp);
  225.     }
  226.     DBUG_RETURN(name);
  227. }
  228.  
  229. /*
  230. #define    isupper(ch)    ('A' <= (ch) && (ch) <= 'Z')
  231. #define    tolower(ch)    ((ch) - 'A' + 'a')
  232. */
  233. static char *
  234. lowercase(dst, src)
  235.     char *dst;
  236.     char *src;
  237. {
  238.     DBUG_ENTER("lowercase")
  239.     char *s, *t;
  240.  
  241.     for(s = src, t = dst; *s; ++s, ++t){
  242.     *t = isupper(*s) ? tolower(*s) : *s;
  243.     }
  244.     *t = '\0';
  245.     DBUG_RETURN(dst);
  246. }
  247.  
  248. /************************************************************************/
  249. char *
  250. _XlcFileName(lcd, category)
  251.     XLCd lcd;
  252.     char *category;
  253. {
  254.     DBUG_ENTER("_XlcFileName")
  255.     char *siname;
  256.     char cat[XLC_BUFSIZE], dir[XLC_BUFSIZE];
  257.     int i, n;
  258.     char *args[NUM_LOCALEDIR];
  259.     char *file_name = NULL;
  260.  
  261.     if(lcd == (XLCd)NULL){
  262. #if defined(DEBUG) && defined(VERBOSE)
  263.     fprintf(stderr,"_XlcFileName: lcd == NULL\n");
  264. #endif
  265.     DBUG_RETURN(NULL);
  266.     }
  267.  
  268.     siname = XLC_PUBLIC(lcd, siname);
  269.  
  270.     lowercase(cat, category);
  271.     xlocaledir(dir,XLC_BUFSIZE);
  272.     n = _XlcParsePath(dir, args, NUM_LOCALEDIR);
  273.     for(i = 0; i < n; ++i){
  274.     char buf[PATH_MAX], *name;
  275.  
  276.     name = NULL;
  277.     if ((5 + (args[i] ? strlen (args[i]) : 0) +
  278.         (cat ? strlen (cat) : 0)) < PATH_MAX) {
  279.         sprintf(buf, "%s/%s.dir", args[i], cat);
  280.         name = resolve_name(siname, buf, RtoL);
  281.     }
  282.     if(name == NULL){
  283.         continue;
  284.     }
  285.     if(*name == '/'){
  286.         /* supposed to be absolute path name */
  287.         file_name = name;
  288.     }else{
  289.         file_name = Xmalloc(2 + (args[i] ? strlen (args[i]) : 0) +
  290.                 (name ? strlen (name) : 0));
  291.         if (file_name != NULL)
  292.         sprintf(file_name, "%s/%s", args[i], name);
  293.         Xfree(name);
  294.     }
  295.     if(isreadable(file_name)){
  296.         break;
  297.     }
  298.     Xfree(file_name);
  299.     file_name = NULL;
  300.     /* Then, try with next dir */
  301.     }
  302. #if defined(DEBUG) && defined(VERBOSE)
  303.     if (!file_name) {
  304.     fprintf(stderr,"_XlcFileName: failed for category '%s'\n",cat);
  305.     }
  306. #endif
  307.     DBUG_RETURN(file_name);
  308. }
  309.  
  310. /************************************************************************/
  311. #ifndef LOCALE_ALIAS
  312. #define LOCALE_ALIAS    "locale.alias"
  313. #endif
  314.  
  315. int
  316. _XlcResolveLocaleName(lc_name, pub)
  317.     char* lc_name;
  318.     XLCdPublicPart* pub;
  319. {
  320.     DBUG_ENTER("_XlcResolveLocaleName")
  321.     char dir[PATH_MAX], buf[PATH_MAX], *name = NULL;
  322.     char *dst;
  323.     int i, n, len, sinamelen;
  324.     char *args[NUM_LOCALEDIR];
  325.     static char locale_alias[] = LOCALE_ALIAS;
  326.     char *tmp_siname;
  327.  
  328.     xlocaledir (dir, PATH_MAX);
  329.     n = _XlcParsePath(dir, args, NUM_LOCALEDIR);
  330.     for(i = 0; i < n; ++i){
  331.     if ((2 + (args[i] ? strlen (args[i]) : 0) + 
  332.         strlen (locale_alias)) < PATH_MAX) {
  333.         sprintf (buf, "%s/%s", args[i], locale_alias);
  334.         name = resolve_name (lc_name, buf, LtoR);
  335.     }
  336.     if(name != NULL){
  337.         break;
  338.     }
  339.     }
  340.  
  341.     if (name == NULL) {
  342.     /* vendor locale name == Xlocale name, no expansion of alias */
  343.     pub->siname = Xmalloc (strlen (lc_name) + 1);
  344.     strcpy (pub->siname, lc_name);
  345.     } else {
  346.     pub->siname = name;
  347.     }
  348.  
  349.     sinamelen = strlen (pub->siname);
  350.     if (sinamelen == 1 && pub->siname[0] == 'C') {
  351.     pub->language = pub->siname;
  352.     pub->territory = pub->codeset = NULL;
  353.     DBUG_RETURN(1);
  354.     }
  355.  
  356.     /* 
  357.      * pub->siname is in the format <lang>_<terr>.<codeset>, typical would
  358.      * be "en_US.ISO8859-1", "en_US.utf8", or "ru_RU.KOI-8"
  359.      */
  360.     tmp_siname = Xrealloc (pub->siname, 2 * (sinamelen + 1));
  361.     if (tmp_siname == NULL) {
  362.     DBUG_RETURN(0);
  363.     } 
  364.     pub->siname = tmp_siname;
  365.  
  366.     /* language */
  367.     dst = &pub->siname[sinamelen + 1];
  368.     strcpy (dst, pub->siname);
  369.     pub->language = dst;
  370.  
  371.     /* territory */
  372.     dst = strchr (dst, '_');
  373.     if (dst) {
  374.     *dst = '\0';
  375.     pub->territory = ++dst;
  376.  
  377.     /* codeset */
  378.     dst = strchr (dst, '.');
  379.     if (dst) {
  380.         *dst = '\0';
  381.         pub->codeset = ++dst;
  382.     }
  383.     }
  384.  
  385.     DBUG_RETURN((pub->siname[0] != '\0') ? 1 : 0);
  386. }
  387.  
  388. /************************************************************************/
  389. int
  390. _XlcResolveI18NPath(buf, buf_len)
  391.     char *buf;
  392.     int buf_len;
  393. {
  394.     DBUG_ENTER("_XlcResolveI18NPath")
  395.     if(buf != NULL){
  396.     xlocaledir(buf, buf_len);
  397.     }
  398.     DBUG_RETURN(1);
  399. }
  400.