Author: iratqq
Date: Fri Apr 11 04:01:18 2008
New Revision: 5410

Modified:
  trunk/gtk/compose.c

Log:
* gtk/compose.c
 (TransFileName):
 - Change API.
 - Cleanup parser.
 - Use MAXPATHLEN.
 (get_lang_region):
 - Change API.
 (get_compose_filename):
 - Ditto.


Modified: trunk/gtk/compose.c
==============================================================================
--- trunk/gtk/compose.c (original)
+++ trunk/gtk/compose.c Fri Apr 11 04:01:18 2008
@@ -37,6 +37,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
+#include <sys/param.h>
#include <locale.h>
#include <errno.h>

@@ -62,9 +63,10 @@

static int parse_line(char *line, char **argv, int argsize);
static unsigned int KeySymToUcs4(KeySym keysym);
-static char *get_compose_filename(void);
+static int get_compose_filename(char *, size_t);
static void ParseComposeStringFile(FILE *fp);
-static char *get_lang_region(void);
+static int get_lang_region(char *, size_t);
+static int TransFileName(char *, const char *, size_t);

static DefTree *g_tree;

@@ -380,41 +382,21 @@
    return(mask);
}

-static char *
-TransFileName(char *name)
+static int
+TransFileName(char *transname, const char *name, size_t len)
{
-    char *home = NULL, *lcCompose = NULL;
-    char *i = name, *ret, *j;
-    int l = 0;
+    char *home = NULL;
+    char lcCompose[MAXPATHLEN];
+    const char *i = name;
+    char *j;
+    char ret[MAXPATHLEN];

-    while (*i) {
-       if (*i == '%') {
-           i++;
-           switch (*i) {
-           case '%':
-               l++;
-               break;
-           case 'H':
-               home = getenv("HOME");
-               if (home)
-                   l += strlen(home);
-               break;
-           case 'L':
-               lcCompose = get_compose_filename();
-               if (lcCompose)
-                   l += strlen(lcCompose);
-               break;
-           }
-       } else {
-           l++;
-       }
-       i++;
-    }
+    home = getenv("HOME");
+    get_compose_filename(lcCompose, sizeof(lcCompose));

-    j = ret = (char *)malloc(l + 1);
-    if (ret == NULL)
-       return ret;
+    j = ret;
    i = name;
+    lcCompose[0] = '\0';
    while (*i) {
        if (*i == '%') {
            i++;
@@ -424,15 +406,14 @@
                break;
            case 'H':
                if (home) {
-                   strcpy(j, home);
+                   strlcat(ret, home, sizeof(ret));
                    j += strlen(home);
                }
                break;
            case 'L':
-               if (lcCompose) {
-                   strcpy(j, lcCompose);
+               if (lcCompose[0] != '\0') {
+                   strlcat(ret, lcCompose, sizeof(ret));
                    j += strlen(lcCompose);
-                   free(lcCompose);
                }
                break;
            }
@@ -442,7 +423,8 @@
        }
    }
    *j = '\0';
-    return ret;
+    strlcpy(transname, ret, len);
+    return 1;
}

#ifndef MB_LEN_MAX
@@ -514,16 +496,15 @@
    n = 0;
    do {
        if ((token == KEY) && (strcmp("include", *tokenbuf) == 0)) {
-           char *filename;
+           char filename[MAXPATHLEN];
            FILE *infp;
            token = nexttoken(fp, tokenbuf, &lastch, buflen);
            if (token != KEY && token != STRING)
                goto error;

-           if ((filename = TransFileName(*tokenbuf)) == NULL)
+ if (!TransFileName(filename, *tokenbuf, sizeof(filename)) || filename[0] == '\0')
                goto error;
            infp = fopen(filename, "r");
-           free(filename);
            if (infp == NULL)
                goto error;
            ParseComposeStringFile(infp);
@@ -707,78 +688,62 @@
    }
}

-static char *
-get_lang_region()
+static int
+get_lang_region(char *locale, size_t len)
{
-    char *locale, *p;
+    char *p;

-    locale = setlocale(LC_CTYPE, NULL);
-    if (locale) {
-       locale = strdup(locale);
-    } else {
-       return NULL;
+    strlcpy(locale, setlocale(LC_CTYPE, NULL), len);
+    if (locale[0] == '\0') {
+       return 0;
    }

    p = strrchr(locale, '.');
    if (p)
        *p = '\0';

-    return locale;
+    return 1;
}

void im_uim_create_compose_tree()
{
    FILE *fp = NULL;
-    char *name, *tmpname = NULL;
-    char *lang_region;
+    char name[MAXPATHLEN];
+    char lang_region[BUFSIZ];
    const char *encoding;
+    char *compose_env;

-    name = getenv("XCOMPOSEFILE");
+    name[0] = '\0';
+    compose_env = getenv("XCOMPOSEFILE");

-    if (name == NULL) {
+    if (compose_env != NULL) {
+       strlcpy(name, compose_env, sizeof(name));
+    } else {
        char *home = getenv("HOME");
        if (home != NULL) {
-           int hl = strlen(home);
-           tmpname = name = (char *)malloc(hl + 10 + 1);
-           if (name != NULL) {
-               strcpy(name, home);
-               strcpy(name + hl, "/.XCompose");
-               fp = fopen(name, "r");
-               if (fp == NULL) {
-                   free(name);
-                   name = tmpname = NULL;
-               }
-           }
+           snprintf(name, sizeof(name), "%s/.XCompose", home);
+           fp = fopen(name, "r");
+           if (fp == NULL)
+               name[0] = '\0';
        }
    }

-    if (name == NULL) {
-       tmpname = name = get_compose_filename();
-    }
-
-    if (name == NULL)
+    if (name[0] == '\0' && !get_compose_filename(name, sizeof(name)))
        return;
-    if (fp == NULL) {
-       fp = fopen(name, "r");
-    }
-    if (tmpname != NULL) {
-       free(tmpname);
-    }
-    if (fp == NULL)
+
+    if (fp == NULL && ((fp = fopen(name, "r")) == NULL))
        return;

-    lang_region = get_lang_region();
+    get_lang_region(lang_region, sizeof(lang_region));
    g_get_charset(&encoding);
    if (lang_region == NULL || encoding == NULL) {
        fprintf(stderr, "Warning: locale name is NULL\n");
-       free(lang_region);
        fclose(fp);
        return;
    }

    ParseComposeStringFile(fp);
    fclose(fp);
-    free(lang_region);
}

static void
@@ -802,68 +767,43 @@
    FreeComposeTree(g_tree);
}

-static char *
-get_compose_filename()
+static int
+get_compose_filename(char *filename, size_t len)
{
-    char *compose_dir_file;
-    char *locale;
+    char compose_dir_file[MAXPATHLEN], name[MAXPATHLEN];
+    char locale[BUFSIZ];
    const char *encoding;
-    char *lang_region;
+    char lang_region[BUFSIZ];
+    int ret;

    FILE *fp;
-    char buf[XLC_BUFSIZE], *name = NULL, *filename = NULL;
+    char buf[XLC_BUFSIZE];
    const char *xlib_dir = XLIB_DIR ;

-    lang_region = get_lang_region();
+    ret = get_lang_region(lang_region, sizeof(lang_region));
    g_get_charset(&encoding);

-    if (lang_region == NULL || encoding == NULL) {
-       free(lang_region);
-       return NULL;
-    }
-
-    locale = (char *)malloc(strlen(lang_region) + strlen(encoding) + 2);
-    if (locale == NULL) {
-       free(lang_region);
-       return NULL;
-    }
-    sprintf(locale, "%s.%s", lang_region, encoding);
-    free(lang_region);
+    if (!ret || encoding == NULL)
+       return 0;

- compose_dir_file = (char *)malloc(strlen(XLIB_DIR) + strlen(COMPOSE_DIR_FILE) + 2);
-    if (compose_dir_file == NULL) {
-       free(locale);
-       return NULL;
-    }
-    sprintf(compose_dir_file, "%s/%s", XLIB_DIR, COMPOSE_DIR_FILE);
+    snprintf(locale, sizeof(locale), "%s.%s", lang_region, encoding);
+ snprintf(compose_dir_file, sizeof(compose_dir_file), "%s/%s", XLIB_DIR, COMPOSE_DIR_FILE);

    fp = fopen(compose_dir_file, "r");
    if (fp == NULL) {
        /* retry with fallback file */
        if (strcmp(FALLBACK_XLIB_DIR, XLIB_DIR)) {
-           compose_dir_file = (char *)realloc(compose_dir_file,
-                           strlen(FALLBACK_XLIB_DIR) +
-                           strlen(COMPOSE_DIR_FILE) + 2);
-           if (compose_dir_file == NULL) {
-               free(locale);
-               return NULL;
-           }
-           sprintf(compose_dir_file, "%s/%s",
+           snprintf(compose_dir_file, sizeof(compose_dir_file), "%s/%s",
                            FALLBACK_XLIB_DIR, COMPOSE_DIR_FILE);
            fp = fopen(compose_dir_file, "r");
-           if (fp == NULL) {
-               free(locale);
-               free(compose_dir_file);
-               return NULL;
-           }
+           if (fp == NULL)
+             return 0;
            xlib_dir = FALLBACK_XLIB_DIR;
-       } else {
-           free(locale);
-           free(compose_dir_file);
-           return NULL;
-       }
+       } else
+           return 0;
    }

+    name[0] = '\0';
    while (fgets(buf, XLC_BUFSIZE, fp) != NULL) {
        char *p = buf;
        int n;
@@ -880,28 +820,17 @@
        }
        from = args[1], to = args[0];
        if (!strcmp(from, locale)) {
-           name = (char *)malloc(strlen(to) + 1);
-           if (name != NULL) {
-               strcpy(name, to);
-           }
+           strlcpy(name, to, sizeof(name));
            break;
        }
    }
    fclose(fp);
-    free(locale);
-    free(compose_dir_file);

-    if (name == NULL)
-       return NULL;
-
-    filename = (char *)malloc(strlen(xlib_dir) + strlen(XLOCALE_DIR) +
-                   strlen(name) + 3);
-    if (filename == NULL)
-       return NULL;
-    sprintf(filename, "%s/%s/%s", xlib_dir, XLOCALE_DIR, name);
-    free(name);
+    if (name[0] == '\0')
+       return 0;

-    return filename;
+    snprintf(filename, len, "%s/%s/%s", xlib_dir, XLOCALE_DIR, name);
+    return 1;
}

static int

Reply via email to