Author: iratqq
Date: Sun Mar 2 01:13:20 2008
New Revision: 5264
Added:
trunk/uim/uim-posix.c
- copied, changed from r5263, /trunk/uim/uim-util.c
trunk/uim/uim-posix.h
Modified:
trunk/fep/udsock.c
trunk/fep/udsock.h
trunk/fep/uim-fep.c
trunk/scm/custom-rt.scm
trunk/scm/init.scm
trunk/scm/look-custom.scm
trunk/scm/plugin.scm
trunk/scm/sj3-custom.scm
trunk/scm/skk-custom.scm
trunk/scm/tutcode-custom.scm
trunk/uim/Makefile.am
trunk/uim/prime.c
trunk/uim/uim-custom.c
trunk/uim/uim-helper-client.c
trunk/uim/uim-helper-server.c
trunk/uim/uim-helper.c
trunk/uim/uim-helper.h
trunk/uim/uim-util.c
trunk/uim/uim.c
Log:
* uim-posix.h:
- New header.
* uim-posix.c:
- New file. These functions treates file/directory operation
via posix functions.
(uim_get_user_name, uim_get_home_directory,
uim_check_dir, uim_get_config_path):
- New function for C developer.
(user_name, home_directory):
- New function for scheme developer.
(file_stat_mode, file_readablep, file_writablep, file_executablep)
(file_regularp, file_directoryp, file_mtime, c_getenv, c_setenv)
(c_unsetenv, setugidp):
- Move from uim-util.c.
(uim_init_posix_subrs):
- New function.
* uim/uim.c (uim_init_internal):
- Call uim_init_posix_subrs().
* uim-util.c:
- Remove POSIX functions to uim-posix.c.
* uim-helper.c (uim_helper_get_pathname):
- Change API for safety operation.
* uim/uim-helper.h:
- Sync API change of uim_helper_get_pathname().
* uim/uim-helper-client.c (uim_helper_init_client_fd):
- Ditto.
* uim/uim-helper-server.c (main):
- Ditto.
* uim/prime.c (check_dir):
- Remove.
* uim/prime.c (prime_get_ud_path):
- Change API for safety operation.
- Use uim_get_config_path().
* fep/udsock.c (check_dir):
- Remove.
* fep/udsock.c (get_ud_path):
- Change API for safety operation.
- Use uim_get_config_path().
* fep/udsock.h
- Sync API change of get_ud_path().
* fep/uim-fep.c (main):
- Ditto.
* scm/skk-custom.scm (skk-personal-dic-filename):
(skk-uim-personal-dic-filename):
- Replace (getenv "HOME") with (home-directory (user-name)).
* scm/init.scm (load-user-conf):
- Ditto.
* scm/plugin.scm
(uim-plugin-lib-load-path, uim-plugin-scm-load-path,
load-module-conf, load-enabled-modules):
- Ditto.
* scm/tutcode-custom.scm (tutcode-personal-dic-filename):
- Ditto.
* scm/sj3-custom.scm (sj3-user):
- Ditto.
* scm/look-custom.scm (look-personal-dict-filename):
- Ditto.
* scm/custom-rt.scm (custom-file-path):
- Ditto.
* uim/uim-custom.c (uim_conf_path):
- Ditto.
Modified: trunk/fep/udsock.c
==============================================================================
--- trunk/fep/udsock.c (original)
+++ trunk/fep/udsock.c Sun Mar 2 01:13:20 2008
@@ -62,62 +62,30 @@
#include <uim/uim.h>
#include <uim/uim-helper.h>
+#include "uim-posix.h"
#include "udsock.h"
static int s_send_sockfd = -1;
static int s_recv_sockfd = -1;
static struct sockaddr_un s_servaddr;
-static uim_bool
-check_dir(const char *dir)
+uim_bool
+get_ud_path(char *path, int len)
{
- struct stat st;
-
- if (dir == NULL)
+ if (len <= 0)
return UIM_FALSE;
- if (stat(dir, &st) < 0)
- return (mkdir(dir, 0700) < 0) ? UIM_FALSE : UIM_TRUE;
- else {
- mode_t mode = S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR;
- return ((st.st_mode & mode) == mode) ? UIM_TRUE : UIM_FALSE;
- }
-}
-
-char *
-get_ud_path(void)
-{
- char *path, *home = NULL;
- struct passwd *pw;
-
- pw = getpwuid(getuid());
- if (pw)
- home = pw->pw_dir;
-
- if (!home && !uim_helper_is_setugid())
- home = getenv("HOME");
-
- if (!home)
- return NULL;
-
- if (asprintf(&path, "%s/.uim.d", home) == -1)
- return NULL; /* XXX: fatal */
-
- if (!check_dir(path)) {
- free(path);
- return NULL;
+ if (!uim_get_config_path(path, len, !uim_helper_is_setugid())) {
+ path[0] = '\0';
+ return UIM_FALSE;
}
- free(path);
- if (asprintf(&path, "%s/.uim.d/fep", home) == -1)
- return NULL; /* XXX: fatal */
+ strlcat(path, "/fep", len);
- if (!check_dir(path)) {
- free(path);
- return NULL;
- }
+ if (!uim_check_dir(path))
+ return UIM_FALSE;
- return path;
+ return UIM_TRUE;
}
@@ -125,7 +93,7 @@
{
static char buf[UNIX_PATH_MAX];
char filebuf[UNIX_PATH_MAX];
- char *sock_dir;
+ char sock_dir[UNIX_PATH_MAX];
if (file != NULL && file[0] == '/') {
return file;
@@ -137,8 +105,7 @@
strlcpy(filebuf, file, UNIX_PATH_MAX);
}
- sock_dir = get_ud_path();
- if (!sock_dir) {
+ if (!get_ud_path(sock_dir, sizeof(sock_dir))) {
sendline("uim-fep cannot create directory");
exit(EXIT_FAILURE);
}
Modified: trunk/fep/udsock.h
==============================================================================
--- trunk/fep/udsock.h (original)
+++ trunk/fep/udsock.h Sun Mar 2 01:13:20 2008
@@ -34,6 +34,8 @@
#ifndef UDSOCK_H
#define UDSOCK_H
+#include "uim.h"
+
#ifndef UNIX_PATH_MAX
#define UNIX_PATH_MAX 100
#endif
@@ -44,6 +46,6 @@
void init_recvsocket(const char *sock_path);
int recvline(char *buf, int n);
void close_socket(void);
-char *get_ud_path(void);
+uim_bool get_ud_path(char *, int);
#endif
Modified: trunk/fep/uim-fep.c
==============================================================================
--- trunk/fep/uim-fep.c (original)
+++ trunk/fep/uim-fep.c Sun Mar 2 01:13:20 2008
@@ -243,7 +243,7 @@
};
FILE *fp;
const char *suffix = NULL;
- char *uim_dir;
+ char uim_dir[UNIX_PATH_MAX];
const char *sty_str;
const char *win_str;
struct stat stat_buf;
@@ -415,7 +415,7 @@
tcgetattr(g_win_in, &s_save_tios);
setupterm(NULL, g_win_out, NULL);
- if ((uim_dir = get_ud_path()) == NULL) {
+ if (!get_ud_path(uim_dir, sizeof(uim_dir))) {
sendline("uim-fep cannot make directory");
return EXIT_FAILURE;
}
@@ -475,7 +475,6 @@
}
}
}
- free(uim_dir);
snprintf(pid_str, sizeof(pid_str), "%d", getpid());
setenv("UIM_FEP_PID", pid_str, 1);
Modified: trunk/scm/custom-rt.scm
==============================================================================
--- trunk/scm/custom-rt.scm (original)
+++ trunk/scm/custom-rt.scm Sun Mar 2 01:13:20 2008
@@ -63,7 +63,7 @@
(define custom-file-path
(lambda (gsym)
(let* ((group-name (symbol->string gsym))
- (path (string-append (or (getenv "HOME") "")
+ (path (string-append (or (home-directory (user-name)) "")
"/.uim.d/customs/custom-"
group-name
".scm")))
Modified: trunk/scm/init.scm
==============================================================================
--- trunk/scm/init.scm (original)
+++ trunk/scm/init.scm Sun Mar 2 01:13:20 2008
@@ -68,7 +68,7 @@
(define load-user-conf
(lambda ()
- (let ((home-dir (getenv "HOME")))
+ (let ((home-dir (or (home-directory (user-name)) "")))
(if (or
(setugid?)
(not home-dir))
Modified: trunk/scm/look-custom.scm
==============================================================================
--- trunk/scm/look-custom.scm (original)
+++ trunk/scm/look-custom.scm Sun Mar 2 01:13:20 2008
@@ -47,7 +47,7 @@
(N_ "long description will be here."))
(define-custom 'look-personal-dict-filename
- (string-append (or (getenv "HOME") "") "/.look-uim-dict")
+ (string-append (or (home-directory (user-name)) "") "/.look-uim-dict")
'(look)
'(pathname regular-file)
(N_ "Personal dictionary file")
Modified: trunk/scm/plugin.scm
==============================================================================
--- trunk/scm/plugin.scm (original)
+++ trunk/scm/plugin.scm Sun Mar 2 01:13:20 2008
@@ -37,7 +37,7 @@
(define uim-plugin-lib-load-path
(if (setugid?)
(list (string-append (sys-pkglibdir) "/plugin"))
- (let ((home-dir (getenv "HOME"))
+ (let ((home-dir (or (home-directory (user-name)) ""))
(ld-library-path (getenv "LD_LIBRARY_PATH")))
(filter string?
(append (list (getenv "LIBUIM_PLUGIN_LIB_DIR")
@@ -53,7 +53,7 @@
(define uim-plugin-scm-load-path
(if (setugid?)
(list (sys-pkgdatadir))
- (let ((home-dir (getenv "HOME")))
+ (let ((home-dir (or (home-directory (user-name)) "")))
(filter string?
(list (getenv "LIBUIM_SCM_FILES")
(if home-dir
@@ -128,7 +128,7 @@
;; TODO: write test
(define load-module-conf
(lambda ()
- (let* ((home-dir (getenv "HOME"))
+ (let* ((home-dir (or (home-directory (user-name)) ""))
(user-module-dir (if home-dir
(string-append home-dir "/.uim.d/plugin/")
#f))
@@ -155,7 +155,7 @@
;; TODO: write test
(define load-enabled-modules
(lambda ()
- (let* ((home-dir (getenv "HOME"))
+ (let* ((home-dir (or (home-directory (user-name)) ""))
(user-module-dir (if home-dir
(string-append home-dir "/.uim.d/plugin/")
#f))
Modified: trunk/scm/sj3-custom.scm
==============================================================================
--- trunk/scm/sj3-custom.scm (original)
+++ trunk/scm/sj3-custom.scm Sun Mar 2 01:13:20 2008
@@ -319,7 +319,7 @@
(lambda ()
sj3-use-remote-server?))
-(define-custom 'sj3-user (getenv "USER")
+(define-custom 'sj3-user (or (home-directory (user-name)) "")
'(sj3-advanced sj3server)
'(string ".*")
(N_ "SJ3 user name")
Modified: trunk/scm/skk-custom.scm
==============================================================================
--- trunk/scm/skk-custom.scm (original)
+++ trunk/scm/skk-custom.scm Sun Mar 2 01:13:20 2008
@@ -394,14 +394,14 @@
(N_ "long description will be here."))
(define-custom 'skk-personal-dic-filename
- (string-append (or (getenv "HOME") "") "/.skk-jisyo")
+ (string-append (or (home-directory (user-name)) "") "/.skk-jisyo")
'(skk-dict dict-files)
'(pathname regular-file)
(N_ "Personal dictionary file")
(N_ "long description will be here."))
(define-custom 'skk-uim-personal-dic-filename
- (string-append (or (getenv "HOME") "") "/.skk-uim-jisyo")
+ (string-append (or (home-directory (user-name)) "") "/.skk-uim-jisyo")
'(skk-dict dict-files)
'(pathname regular-file)
(N_ "Personal dictionary file (dedicated to uim)")
Modified: trunk/scm/tutcode-custom.scm
==============================================================================
--- trunk/scm/tutcode-custom.scm (original)
+++ trunk/scm/tutcode-custom.scm Sun Mar 2 01:13:20 2008
@@ -55,7 +55,7 @@
(N_ "long description will be here."))
(define-custom 'tutcode-personal-dic-filename
- (string-append (or (getenv "HOME") "") "/.mazegaki.dic")
+ (string-append (or (home-directory (user-name)) "") "/.mazegaki.dic")
'(tutcode tutcode-dict)
'(pathname regular-file)
(N_ "Personal mazegaki dictionary file")
Modified: trunk/uim/Makefile.am
==============================================================================
--- trunk/uim/Makefile.am (original)
+++ trunk/uim/Makefile.am Sun Mar 2 01:13:20 2008
@@ -37,7 +37,7 @@
$(MAKE) $(AM_MAKEFLAGS) -C $(top_builddir)/sigscheme/src combined
pkginclude_HEADERS = uim-scm.h uim-scm-abbrev.h uim.h \
- uim-util.h plugin.h \
+ uim-util.h uim-posix.h plugin.h \
uim-helper.h uim-im-switcher.h \
uim-custom.h
# Install even if !NOTIFY since plugins will need this to use
@@ -46,7 +46,7 @@
libuim_la_SOURCES = \
uim-internal.h uim-error.c uim.c \
- uim-key.c uim-func.c uim-util.c \
+ uim-key.c uim-func.c uim-util.c uim-posix.c \
iconv.c plugin.c \
uim-ipc.c uim-helper.c uim-helper-client.c \
gettext.h intl.c \
Modified: trunk/uim/prime.c
==============================================================================
--- trunk/uim/prime.c (original)
+++ trunk/uim/prime.c Sun Mar 2 01:13:20 2008
@@ -55,6 +55,7 @@
#include "plugin.h"
#include "uim-helper.h"
#include "uim-util.h"
+#include "uim-posix.h"
#define BUFFER_SIZE (4 * 1024)
@@ -63,7 +64,7 @@
static char *prime_command = "prime";
-static char *prime_ud_path;
+static char prime_ud_path[MAXPATHLEN];
static int prime_fd = -1;
static uim_bool use_unix_domain_socket;
@@ -73,7 +74,7 @@
int fd;
struct sockaddr_un server;
- if (!path)
+ if (path[0] == '\0')
return -1;
memset(&server, 0, sizeof(server));
@@ -109,56 +110,28 @@
}
static uim_bool
-check_dir(const char *dir)
+prime_get_ud_path(char *prime_path, int len)
{
- struct stat st;
+ char socket_path[MAXPATHLEN], ud_path[MAXPATHLEN];
- if (stat(dir, &st) < 0)
- return (mkdir(dir, 0700) < 0) ? UIM_FALSE : UIM_TRUE;
- else {
- mode_t mode = S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR;
- return ((st.st_mode & mode) == mode) ? UIM_TRUE : UIM_FALSE;
- }
-}
-
-static char *
-prime_get_ud_path(void)
-{
- char *path, *home = NULL;
- struct passwd *pw;
- int len;
-
- pw = getpwuid(getuid());
- if (pw)
- home = pw->pw_dir;
-
- if (!home && !uim_helper_is_setugid())
- home = getenv("HOME");
-
- if (!home)
- return NULL;
+ if (len <= 0)
+ return UIM_FALSE;
- len = strlen(home) + strlen("/.uim.d");
- path = uim_malloc(len + 1);
- snprintf(path, len + 1, "%s/.uim.d", home);
- if (!check_dir(path)) {
- free(path);
- return NULL;
+ if (!uim_get_config_path(ud_path,
sizeof(ud_path), !uim_helper_is_setugid())) {
+ prime_path[0] = '\0';
+ return UIM_FALSE;
}
- len += strlen("/socket");
- path = uim_realloc(path, len + 1);
- strlcat(path, "/socket", len + 1);
- if (!check_dir(path)) {
- free(path);
- return NULL;
+ snprintf(socket_path, len, "%s/socket", ud_path);
+
+ if (!uim_check_dir(socket_path)) {
+ prime_path[0] = '\0';
+ return UIM_FALSE;
}
- len += strlen("/uim-prime");
- path = uim_realloc(path, len + 1);
- strlcat(path, "/uim-prime", len + 1);
+ snprintf(prime_path, len, "%s/uim-prime", ud_path);
- return path;
+ return UIM_TRUE;
}
static void
@@ -166,8 +139,7 @@
{
close(prime_fd);
prime_fd = -1;
- free(prime_ud_path);
- prime_ud_path = NULL;
+ prime_ud_path[0] = '\0';
}
static char *
@@ -262,10 +234,9 @@
if (prime_fd != -1)
return uim_scm_t();
- prime_ud_path = prime_get_ud_path();
- if (!prime_ud_path)
+ if (!prime_get_ud_path(prime_ud_path, sizeof(prime_ud_path)))
return uim_scm_f();
-
+
prime_fd = prime_init_ud(prime_ud_path);
if (prime_fd == -1) {
unlink(prime_ud_path);
Modified: trunk/uim/uim-custom.c
==============================================================================
--- trunk/uim/uim-custom.c (original)
+++ trunk/uim/uim-custom.c Sun Mar 2 01:13:20 2008
@@ -1032,7 +1032,7 @@
{
char *dir;
- UIM_EVAL_STRING(NULL, "(string-append (or (getenv \"HOME\") \"\")
\"/.uim.d\")");
+ UIM_EVAL_STRING(NULL, "(string-append (or (home-directory
(user-name)) \"\") \"/.uim.d\")");
dir = uim_scm_c_str(uim_scm_return_value());
if (subpath) {
UIM_EVAL_FSTRING2(NULL, "\"%s/%s\"", dir, subpath);
Modified: trunk/uim/uim-helper-client.c
==============================================================================
--- trunk/uim/uim-helper-client.c (original)
+++ trunk/uim/uim-helper-client.c Sun Mar 2 01:13:20 2008
@@ -74,22 +74,19 @@
int uim_helper_init_client_fd(void (*disconnect_cb)(void))
{
struct sockaddr_un server;
- char *path;
+ char path[MAXPATHLEN];
FILE *serv_r = NULL, *serv_w = NULL;
int fd = -1;
uim_fd = -1;
-
- path = uim_helper_get_pathname();
- if (!path)
+
+ if (!uim_helper_get_pathname(path, sizeof(path)))
goto error;
memset(&server, 0, sizeof(server));
server.sun_family = PF_UNIX;
strlcpy(server.sun_path, path, sizeof(server.sun_path));
- free(path);
-
fd = socket(PF_UNIX, SOCK_STREAM, 0);
if (fd < 0) {
perror("fail to create socket");
Modified: trunk/uim/uim-helper-server.c
==============================================================================
--- trunk/uim/uim-helper-server.c (original)
+++ trunk/uim/uim-helper-server.c Sun Mar 2 01:13:20 2008
@@ -369,13 +369,12 @@
int
main(int argc, char **argv)
{
- char *path;
+ char path[MAXPATHLEN];
int server_fd;
uim_init_error();
- path = uim_helper_get_pathname();
- if (!path)
+ if (!uim_helper_get_pathname(path, sizeof(path)))
return 0;
unlink(path);
@@ -387,7 +386,6 @@
FD_ZERO(&s_fdset_write);
s_max_fd = 0;
server_fd = init_server_fd(path);
- free(path);
printf("waiting\n\n");
fflush(stdout);
Modified: trunk/uim/uim-helper.c
==============================================================================
--- trunk/uim/uim-helper.c (original)
+++ trunk/uim/uim-helper.c Sun Mar 2 01:13:20 2008
@@ -51,6 +51,7 @@
#include "uim-internal.h"
#include "uim-helper.h"
#include "uim-util.h"
+#include "uim-posix.h"
/*
* uim-notify is disabled since I'm not confident about:
@@ -162,51 +163,52 @@
}
}
-char *
-uim_helper_get_pathname(void)
+uim_bool
+uim_helper_get_pathname(char *helper_path, int len)
{
- char *path, *home = NULL;
+ char socket_path[MAXPATHLEN], ud_path[MAXPATHLEN];
struct passwd *pw;
- int len;
-
- if (UIM_CATCH_ERROR_BEGIN())
- return NULL;
- pw = getpwuid(getuid());
- if (pw)
- home = pw->pw_dir;
+ if (len <= 0)
+ return UIM_FALSE;
- if (!home && !uim_issetugid())
- home = getenv("HOME");
+ if (UIM_CATCH_ERROR_BEGIN()) {
+ helper_path[0] = '\0';
+ return UIM_FALSE;
+ }
- if (!home)
+ pw = getpwuid(getuid());
+ if (!pw) {
uim_fatal_error("uim_helper_get_pathname()");
+ endpwent();
+ helper_path[0] = '\0';
+ return UIM_FALSE;
+ }
+
+ snprintf(ud_path, len, "%s/.uim.d", pw->pw_dir);
+ endpwent();
/* check ~/.uim.d/ */
- len = strlen(home) + strlen("/.uim.d");
- path = uim_malloc(len + 1);
- snprintf(path, len + 1, "%s/.uim.d", home);
- if (!check_dir(path)) {
- free(path);
+ if (!check_dir(ud_path)) {
uim_fatal_error("uim_helper_get_pathname()");
+ helper_path[0] = '\0';
+ return UIM_FALSE;
}
/* check ~/.uim.d/socket/ */
- len += strlen("/socket");
- path = uim_realloc(path, len + 1);
- strlcat(path, "/socket", len + 1);
- if (!check_dir(path)) {
- free(path);
+ snprintf(socket_path, sizeof(socket_path), "%s/socket", ud_path);
+
+ if (!check_dir(socket_path)) {
uim_fatal_error("uim_helper_get_pathname()");
+ helper_path[0] = '\0';
+ return UIM_FALSE;
}
- len += strlen("/uim-helper");
- path = uim_realloc(path, len + 1);
- strlcat(path, "/uim-helper", len + 1);
+ snprintf(helper_path, len, "%s/uim-helper", ud_path);
UIM_CATCH_ERROR_END();
- return path;
+ return UIM_TRUE;
}
int
Modified: trunk/uim/uim-helper.h
==============================================================================
--- trunk/uim/uim-helper.h (original)
+++ trunk/uim/uim-helper.h Sun Mar 2 01:13:20 2008
@@ -49,7 +49,7 @@
void uim_helper_send_message(int fd, const char *message);
/* functions for libuim server/client's implementation */
-char *uim_helper_get_pathname(void);
+uim_bool uim_helper_get_pathname(char *, int);
int uim_helper_str_terminated(const char *str);
int uim_helper_check_connection_fd(int fd);
int uim_helper_fd_readable(int fd);
Copied: trunk/uim/uim-posix.c (from r5263, /trunk/uim/uim-util.c)
==============================================================================
--- /trunk/uim/uim-util.c (original)
+++ trunk/uim/uim-posix.c Sun Mar 2 01:13:20 2008
@@ -1,6 +1,6 @@
/*
- Copyright (c) 2003-2008 uim Project http://code.google.com/p/uim/
+ Copyright (c) 2008 uim Project http://code.google.com/p/uim/
All rights reserved.
@@ -33,51 +33,135 @@
#include <config.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/time.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <pwd.h>
#include <assert.h>
-#include <dirent.h>
-#include <dlfcn.h>
#include "uim-internal.h"
#include "uim-scm.h"
#include "uim-scm-abbrev.h"
#include "uim-util.h"
+#include "uim-posix.h"
-static uim_lisp protected;
+uim_bool
+uim_get_user_name(char *name, int len, int uid)
+{
+ struct passwd *pw;
+
+ if (len <= 0)
+ return UIM_FALSE;
+ pw = getpwuid(uid);
+ if (!pw) {
+ name[0] = '\0';
+ return UIM_FALSE;
+ }
+ strlcpy(name, pw->pw_name, len);
+ endpwent();
+ return UIM_TRUE;
+}
-/* define constants as procedure to ensure unmodifiable */
static uim_lisp
-uim_version()
+user_name(void)
{
- return MAKE_STR(PACKAGE_VERSION);
+ char name[BUFSIZ];
+
+ if (!uim_get_user_name(name, sizeof(name), getuid()))
+ return uim_scm_f();
+
+ return MAKE_STR(name);
}
-static uim_lisp
-sys_libdir()
+uim_bool
+uim_get_home_directory(char *home, int len, int uid)
{
- return MAKE_STR(LIBDIR);
+ struct passwd *pw;
+
+ if (len <= 0)
+ return UIM_FALSE;
+ pw = getpwuid(uid);
+ if (!pw) {
+ home[0] = '\0';
+ return UIM_FALSE;
+ }
+ strlcpy(home, pw->pw_dir, len);
+ endpwent();
+ return UIM_TRUE;
}
static uim_lisp
-sys_pkglibdir()
+home_directory(uim_lisp user_)
{
- return MAKE_STR(PKGLIBDIR);
+ int uid;
+ char home[MAXPATHLEN];
+
+ if (INTP(user_)) {
+ uid = C_INT(user_);
+ } else if (STRP(user_)) {
+ struct passwd *pw;
+
+ pw = getpwnam(REFER_C_STR(user_));
+
+ if (!pw)
+ return uim_scm_f();
+
+ uid = pw->pw_uid;
+ endpwent();
+ } else {
+ return uim_scm_f();
+ }
+
+ if (!uim_get_home_directory(home, sizeof(home), uid)) {
+ char *home_env = getenv("HOME");
+ if (home_env)
+ return MAKE_STR(home_env);
+ return uim_scm_f();
+ }
+
+ return MAKE_STR(home);
}
-static uim_lisp
-sys_datadir()
+uim_bool
+uim_check_dir(const char *dir)
{
- return MAKE_STR(DATADIR);
+ struct stat st;
+
+ if (stat(dir, &st) < 0)
+ return (mkdir(dir, 0700) < 0) ? UIM_FALSE : UIM_TRUE;
+ else {
+ mode_t mode = S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR;
+ return ((st.st_mode & mode) == mode) ? UIM_TRUE : UIM_FALSE;
+ }
}
-static uim_lisp
-sys_pkgdatadir()
+uim_bool
+uim_get_config_path(char *path, int len, int is_getenv)
{
- return MAKE_STR(PKGDATADIR);
+ char home[MAXPATHLEN];
+
+ if (len <= 0)
+ return UIM_FALSE;
+
+ if (!uim_get_home_directory(home, sizeof(home), getuid()) &&
is_getenv) {
+ char *home_env = getenv("HOME");
+
+ if (!home_env)
+ return UIM_FALSE;
+
+ strlcpy(home, home_env, sizeof(home));
+ }
+
+ if (snprintf(path, len, "%s/.uim.d", home) == -1)
+ return UIM_FALSE;
+
+ if (!uim_check_dir(path)) {
+ return UIM_FALSE;
+ }
+
+ return UIM_TRUE;
}
static uim_lisp
@@ -166,117 +250,6 @@
return uim_scm_t();
}
-/* Limited version of SRFI-13 string-contains. The number of args are
- * fixed to 3. */
-static uim_lisp
-string_contains(uim_lisp s1_, uim_lisp s2_, uim_lisp start1_)
-{
- const char *s1, *s2, *found;
- long start1;
- size_t s1len;
-
- s1 = REFER_C_STR(s1_);
- s2 = REFER_C_STR(s2_);
- start1 = C_INT(start1_);
- s1len = strlen(s1);
-
- if (start1 < 0 || s1len < (size_t)start1)
- ERROR("string-contains: invalid range");
-
- found = strstr(&s1[start1], s2);
-
- return (found) ? MAKE_INT(found - s1) : uim_scm_f();
-}
-
-static uim_lisp
-string_prefixp_internal(uim_lisp prefix_, uim_lisp str_,
- int (*cmp)(const char *, const char *, size_t))
-{
- const char *prefix, *str;
- size_t len;
-
- prefix = REFER_C_STR(prefix_);
- str = REFER_C_STR(str_);
- len = strlen(prefix);
-
- return MAKE_BOOL((*cmp)(prefix, str, len) == 0);
-}
-
-static uim_lisp
-string_prefixp(uim_lisp prefix_, uim_lisp str_)
-{
- return string_prefixp_internal(prefix_, str_, strncmp);
-}
-
-static uim_lisp
-string_prefix_cip(uim_lisp prefix_, uim_lisp str_)
-{
- return string_prefixp_internal(prefix_, str_, strncasecmp);
-}
-
-/* Limited version of SRFI-43 vector-copy. Only accepts 1st arg. */
-static uim_lisp
-vector_copy(uim_lisp src)
-{
- long len, i;
- uim_lisp elm, copied;
-
- len = uim_scm_vector_length(src);
- copied = uim_scm_callf("make-vector", "l", len);
- for (i = 0; i < len; i++) {
- elm = VECTOR_REF(src, i);
- VECTOR_SET(copied, i, elm);
- }
-
- return copied;
-}
-
-const char *
-uim_get_language_name_from_locale(const char *locale)
-{
- uim_lisp lang_code, lang_name;
- const char *name;
-
- if (UIM_CATCH_ERROR_BEGIN())
- return "-";
-
- assert(uim_scm_gc_any_contextp());
- assert(locale);
-
- /* Performs adhoc "zh_TW:zh_HK" style locale handling as temporary
- * specification of this function for backward compatibility. */
- protected =
- lang_code = uim_scm_callf("langgroup-primary-lang-code", "s", locale);
- protected =
- lang_name = uim_scm_callf("lang-code->lang-name", "o", lang_code);
- name = REFER_C_STR(lang_name);
-
- UIM_CATCH_ERROR_END();
-
- return name;
-}
-
-const char *
-uim_get_language_code_from_language_name(const char *language_name)
-{
- uim_lisp lang_code;
- const char *name;
-
- if (UIM_CATCH_ERROR_BEGIN())
- return "-";
-
- assert(uim_scm_gc_any_contextp());
- assert(language_name);
-
- protected =
- lang_code = uim_scm_callf("lang-name->lang-code", "s", language_name);
- name = REFER_C_STR(lang_code);
-
- UIM_CATCH_ERROR_END();
-
- return name;
-}
-
static uim_lisp
setugidp(void)
{
@@ -286,17 +259,10 @@
}
void
-uim_init_util_subrs(void)
+uim_init_posix_subrs(void)
{
- protected = uim_scm_f();
- uim_scm_gc_protect(&protected);
-
- uim_scm_init_proc0("uim-version", uim_version);
-
- uim_scm_init_proc0("sys-libdir", sys_libdir);
- uim_scm_init_proc0("sys-pkglibdir", sys_pkglibdir);
- uim_scm_init_proc0("sys-datadir", sys_datadir);
- uim_scm_init_proc0("sys-pkgdatadir", sys_pkgdatadir);
+ uim_scm_init_proc0("user-name", user_name);
+ uim_scm_init_proc1("home-directory", home_directory);
uim_scm_init_proc1("file-readable?", file_readablep);
uim_scm_init_proc1("file-writable?", file_writablep);
@@ -310,12 +276,4 @@
uim_scm_init_proc1("getenv", c_getenv);
uim_scm_init_proc3("setenv", c_setenv);
uim_scm_init_proc1("unsetenv", c_unsetenv);
-
- /* SRFI-13 */
- uim_scm_init_proc3("string-contains", string_contains);
- uim_scm_init_proc2("string-prefix?", string_prefixp);
- uim_scm_init_proc2("string-prefix-ci?", string_prefix_cip);
-
- /* SRFI-43 */
- uim_scm_init_proc1("vector-copy", vector_copy);
}
Added: trunk/uim/uim-posix.h
==============================================================================
--- (empty file)
+++ trunk/uim/uim-posix.h Sun Mar 2 01:13:20 2008
@@ -0,0 +1,55 @@
+/*
+
+ Copyright (c) 2008 uim Project http://code.google.com/p/uim/
+
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ 1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+ 3. Neither the name of authors nor the names of its contributors
+ may be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY
+ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ SUCH DAMAGE.
+
+*/
+
+#ifndef UIM_POSIX_H
+#define UIM_POSIX_H
+
+#include "uim.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+uim_bool uim_get_user_name(char *, int, int);
+uim_bool uim_get_home_directory(char *, int, int);
+uim_bool uim_check_dir(const char *);
+uim_bool uim_get_config_path(char *, int, int);
+
+void uim_init_posix_subrs(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+
Modified: trunk/uim/uim-util.c
==============================================================================
--- trunk/uim/uim-util.c (original)
+++ trunk/uim/uim-util.c Sun Mar 2 01:13:20 2008
@@ -33,14 +33,9 @@
#include <config.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/time.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
-#include <dirent.h>
-#include <dlfcn.h>
#include "uim-internal.h"
#include "uim-scm.h"
@@ -80,92 +75,6 @@
return MAKE_STR(PKGDATADIR);
}
-static uim_lisp
-file_stat_mode(uim_lisp filename, mode_t mode)
-{
- struct stat st;
- int err;
-
- err = stat(REFER_C_STR(filename), &st);
- if (err)
- return uim_scm_f(); /* intentionally returns #f instead of error */
-
- return MAKE_BOOL((st.st_mode & mode) == mode);
-}
-
-static uim_lisp
-file_readablep(uim_lisp filename)
-{
- return file_stat_mode(filename, S_IRUSR);
-}
-
-static uim_lisp
-file_writablep(uim_lisp filename)
-{
- return file_stat_mode(filename, S_IWUSR);
-}
-
-static uim_lisp
-file_executablep(uim_lisp filename)
-{
- return file_stat_mode(filename, S_IXUSR);
-}
-
-static uim_lisp
-file_regularp(uim_lisp filename)
-{
- return file_stat_mode(filename, S_IFREG);
-}
-
-static uim_lisp
-file_directoryp(uim_lisp filename)
-{
- return file_stat_mode(filename, S_IFDIR);
-}
-
-static uim_lisp
-file_mtime(uim_lisp filename)
-{
- struct stat st;
- int err;
-
- err = stat(REFER_C_STR(filename), &st);
- if (err)
- ERROR_OBJ("stat failed for file", filename);
-
- return MAKE_INT(st.st_mtime);
-}
-
-static uim_lisp
-c_getenv(uim_lisp str)
-{
- char *val;
-
- ENSURE_TYPE(str, str);
-
- val = getenv(REFER_C_STR(str));
-
- return (val) ? MAKE_STR(val) : uim_scm_f();
-}
-
-static uim_lisp
-c_setenv(uim_lisp name, uim_lisp val, uim_lisp overwrite)
-{
- int err;
-
- err = setenv(REFER_C_STR(name), REFER_C_STR(val), TRUEP(overwrite));
-
- return MAKE_BOOL(!err);
-}
-
-static uim_lisp
-c_unsetenv(uim_lisp name)
-{
- unsetenv(REFER_C_STR(name));
-
- return uim_scm_t();
-}
-
/* Limited version of SRFI-13 string-contains. The number of args are
* fixed to 3. */
static uim_lisp
@@ -277,14 +186,6 @@
return name;
}
-static uim_lisp
-setugidp(void)
-{
- assert(uim_scm_gc_any_contextp());
-
- return MAKE_BOOL(uim_issetugid());
-}
-
void
uim_init_util_subrs(void)
{
@@ -297,19 +198,6 @@
uim_scm_init_proc0("sys-pkglibdir", sys_pkglibdir);
uim_scm_init_proc0("sys-datadir", sys_datadir);
uim_scm_init_proc0("sys-pkgdatadir", sys_pkgdatadir);
-
- uim_scm_init_proc1("file-readable?", file_readablep);
- uim_scm_init_proc1("file-writable?", file_writablep);
- uim_scm_init_proc1("file-executable?", file_executablep);
- uim_scm_init_proc1("file-regular?", file_regularp);
- uim_scm_init_proc1("file-directory?", file_directoryp);
- uim_scm_init_proc1("file-mtime", file_mtime);
-
- uim_scm_init_proc0("setugid?", setugidp);
-
- uim_scm_init_proc1("getenv", c_getenv);
- uim_scm_init_proc3("setenv", c_setenv);
- uim_scm_init_proc1("unsetenv", c_unsetenv);
/* SRFI-13 */
uim_scm_init_proc3("string-contains", string_contains);
Modified: trunk/uim/uim.c
==============================================================================
--- trunk/uim/uim.c (original)
+++ trunk/uim/uim.c Sun Mar 2 01:13:20 2008
@@ -43,6 +43,7 @@
#include "uim.h"
#include "uim-internal.h"
#include "uim-util.h"
+#include "uim-posix.h"
#include "uim-im-switcher.h"
#include "uim-scm.h"
#include "uim-scm-abbrev.h"
@@ -126,6 +127,7 @@
uim_init_im_subrs();
uim_init_intl_subrs();
+ uim_init_posix_subrs();
uim_init_util_subrs();
#if UIM_USE_NOTIFY
uim_notify_init(); /* init uim-notify facility */