uim_eb * uim_eb_new (const char *bookpath) { uim_eb *ueb; EB_Error_Code err; ueb = uim_malloc(sizeof(uim_eb)); eb_initialize_book(&ueb->book); err = eb_bind(&ueb->book, bookpath); if (err != EB_SUCCESS) { uim_notify_fatal(N_("eb: wrong bookpath")); free(ueb); return NULL; } err = eb_subbook_list(&ueb->book, ueb->subCodes, &ueb->subCount); if (err != EB_SUCCESS) { uim_notify_fatal(N_("eb: eb_subbook_list() failed\n")); free(ueb); return NULL; } return ueb; }
static void * dynlib_bind_internal(uim_lisp name) { void *library; void (*dynlib_instance_init)(void); void (*dynlib_instance_quit)(void); DPRINTFN(UIM_VLEVEL_DYNLIB, (stderr, "Loading %s", REFER_C_STR(name))); library = dlopen(REFER_C_STR(name), RTLD_NOW); if (library == NULL) { uim_notify_fatal(_("dynlib: %s: Load failed."), dlerror()); return uim_scm_f(); } dynlib_instance_init = (void (*)(void))dlfunc(library, "uim_dynlib_instance_init"); dynlib_instance_quit = (void (*)(void))dlfunc(library, "uim_dynlib_instance_quit"); if (!dynlib_instance_init) { uim_notify_fatal(_("dynlib: %s: Initialization failed."), REFER_C_STR(name)); return uim_scm_f(); } DPRINTFN(UIM_VLEVEL_DYNLIB, (stderr, "Calling dynlib_instance_init() for %s.\n", REFER_C_STR(name))); (*dynlib_instance_init)(); return LIST3(MAKE_PTR(library), MAKE_FPTR(dynlib_instance_init), MAKE_FPTR(dynlib_instance_quit)); }
static uim_lisp c_getaddrinfo(uim_lisp hostname_, uim_lisp servname_, uim_lisp hint_) { const char *hostname; char *servname = NULL; struct addrinfo *hints = C_PTR(hint_); struct addrinfo *res, *res0; uim_lisp ret_ = uim_scm_null(); int error; if (INTP(servname_)) { uim_asprintf(&servname, "%d", C_INT(servname_)); } else { servname = C_STR(servname_); } if (FALSEP(hostname_)) hostname = NULL; else hostname = REFER_C_STR(hostname_); error = getaddrinfo(hostname, servname, hints, &res0); if (error) { const char *errstr = gai_strerror(error); uim_notify_fatal("getaddrinfo: %s", errstr); free(servname); return uim_scm_f(); } free(servname); for (res = res0; res; res = res->ai_next) { ret_ = CONS(MAKE_PTR(res) , ret_); } return uim_scm_callf("reverse", "o", ret_); }
static uim_lisp notify_fatal(uim_lisp msg_) { const char *msg = REFER_C_STR(msg_); return MAKE_BOOL(uim_notify_fatal("%s", msg)); }
void uim_eb_open () { EB_Error_Code err; err = eb_initialize_library(); if (err != EB_SUCCESS) uim_notify_fatal(_("eb: failed to initialize EB library : error = %s\n"), eb_error_message(err)); }
static void go_text_eb (uim_eb *ueb, EB_Position position, char **str, const char *enc) { EB_Hookset hookset; char text[MAX_TEXT + 1]; ssize_t text_length; ssize_t bytes; int i; if (eb_seek_text(&ueb->book, &position) != EB_SUCCESS) { uim_notify_fatal(N_("eb: eb_seek_text error occurs")); return; } eb_initialize_hookset(&hookset); for (i = 0; i < 1; i++) { char *local; iconv_t cd; if (eb_read_text(&ueb->book, NULL, &hookset, NULL, MAX_TEXT, text, &text_length) != EB_SUCCESS) { bytes = 0; uim_notify_fatal(N_("eb_read_text : an error occurs")); return; } bytes += text_length; if (text_length < 1) break; /* FIXME! check return value */ cd = (iconv_t)uim_iconv->create(enc, "EUC-JP"); local = uim_iconv->convert(cd, text); uim_iconv->release(cd); uim_eb_strappend(str, local, strlen(local)); free(local); } eb_finalize_hookset(&hookset); }
int uim_helper_check_connection_fd(int fd) { uid_t euid; gid_t egid; if (getpeereid(fd, &euid, &egid) < 0) { #if USE_UIM_NOTIFY && !UIM_NON_LIBUIM_PROG uim_notify_fatal("uim_helper: %s", strerror(errno)); #else perror("getpeereid failed"); #endif return -1; } if ((euid != 0) && (euid != getuid())) { #if USE_UIM_NOTIFY && !UIM_NON_LIBUIM_PROG uim_notify_fatal("uim_helper: uid mismatch"); #else fprintf(stderr, "uid mismatch\n"); #endif return -1; } return 0; }
char * uim_eb_search_text (uim_eb *ueb, const char *key, const char *enc) { char *text; int i; char *str = NULL; iconv_t cd; /* FIXME! check return value */ cd = (iconv_t)uim_iconv->create("EUC-JP", enc); text = uim_iconv->convert(cd, key); uim_iconv->release(cd); if (!text) return NULL; for (i = 0; i < ueb->subCount; i++) { EB_Hit hits[MAX_HITS]; int hitCount; int j; /* specify subbook */ if (eb_set_subbook(&ueb->book, ueb->subCodes[i]) != EB_SUCCESS) { uim_notify_fatal(N_("eb: eb_set_subbook() failed")); continue; } eb_search_word(&ueb->book, text); eb_hit_list(&ueb->book, MAX_HITS, hits, &hitCount); for (j = 0; j < hitCount; j++) { /*EB_Position headp = hits[j].heading;*/ EB_Position textp = hits[j].text; go_text_eb(ueb, textp, &str, enc); uim_eb_strappend(&str, "\n", sizeof("\n")); } } free(text); return str; }
static uim_lisp mana_init(void) { char buf[100]; int fd; int fl; if (mana_pid == 0) mana_pid = uim_ipc_open_command(0, &mana_r, &mana_w, MANA_COMMAND); if (mana_pid == 0) return uim_scm_f(); fd = fileno(mana_r); fl = fcntl(fd, F_GETFL); fcntl(fd, F_SETFL, fl | O_NONBLOCK); fgets(buf, sizeof(buf), mana_r); fcntl(fd, F_SETFL, fl); if (feof(mana_r)) { mana_pid = 0; fclose(mana_r); fclose(mana_w); mana_r = mana_w = NULL; uim_notify_fatal(N_("uim-mana: Command 'mana' not found.")); return uim_scm_f(); } if (ferror(mana_r)) clearerr(mana_r); #ifdef DEBUG log = fopen("mana.log", "w"); #endif return uim_scm_t(); }
static char * mana_ipc_send_command(pid_t *pid, FILE **read_fp, FILE **write_fp, const char *str) { char *tmp = uim_strdup(""); char buf[8192]; struct sigaction act, oact; act.sa_handler = SIG_IGN; sigemptyset(&act.sa_mask); act.sa_flags = 0; sigaction(SIGPIPE, &act, &oact); fputs(str, *write_fp); again: if (fflush(*write_fp) != 0) { switch (errno) { case EINTR: goto again; case EPIPE: while (!feof(*read_fp)) { fgets(buf, sizeof(buf), *read_fp); if (buf != NULL) { if (strcmp(buf, "err") == 0) uim_notify_fatal(N_("uim-mana: Command 'mana' not found.")); else uim_notify_fatal("uim-mana: %s", buf); } } *pid = 0; fclose(*read_fp); fclose(*write_fp); *read_fp = NULL; *write_fp = NULL; sigaction(SIGPIPE, &oact, NULL); free(tmp); return NULL; default: sigaction(SIGPIPE, &oact, NULL); free(tmp); return NULL; } } sigaction(SIGPIPE, &oact, NULL); if (feof(*read_fp)) { *pid = 0; fclose(*read_fp); fclose(*write_fp); *read_fp = NULL; *write_fp = NULL; free(tmp); return NULL; } while (fgets (buf, sizeof(buf), *read_fp) != NULL) { tmp = uim_realloc(tmp, strlen(tmp) + strlen(buf) + 1); strcat(tmp, buf); if (strchr( buf, '\n' )) { break; } } return tmp; }
uim_bool uim_helper_get_pathname(char *helper_path, int len) { struct passwd *pw; char *runtimedir; if (len <= 0) return UIM_FALSE; if (UIM_CATCH_ERROR_BEGIN()) return UIM_FALSE; runtimedir = getenv("XDG_RUNTIME_DIR"); if (runtimedir && runtimedir[0]) { if (strlcpy(helper_path, runtimedir, len) >= (size_t)len) goto path_error; if (strlcat(helper_path, "/uim", len) >= (size_t)len) goto path_error; } else { pw = getpwuid(getuid()); if (!pw) { endpwent(); goto path_error; } if (strlcpy(helper_path, pw->pw_dir, len) >= (size_t)len) { endpwent(); goto path_error; } if (strlcat(helper_path, "/.uim.d", len) >= (size_t)len) { endpwent(); goto path_error; } endpwent(); } /* check $XDG_RUNTIME_DIR/uim/ if $XDG_RUNTIME_DIR is available. * otherwise ~/.uim.d/ */ if (!check_dir(helper_path)) goto path_error; /* check $XDG_RUNTIME_DIR/uim/socket/ if $XDG_RUNTIME_DIR is available. * otherwise ~/.uim.d/socket/ */ if (strlcat(helper_path, "/socket", len) >= (size_t)len) goto path_error; if (!check_dir(helper_path)) goto path_error; if (strlcat(helper_path, "/uim-helper", len) >= (size_t)len) goto path_error; UIM_CATCH_ERROR_END(); return UIM_TRUE; path_error: #if USE_UIM_NOTIFY && !UIM_NON_LIBUIM_PROG uim_notify_fatal("uim_helper_get_pathname() failed"); #else fprintf(stderr, "uim_helper_get_pathname() failed\n"); #endif helper_path[0] = '\0'; UIM_CATCH_ERROR_END(); return UIM_FALSE; }