File: canna.c

package info (click to toggle)
sumika 0.11-2.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 2,780 kB
  • ctags: 1,977
  • sloc: ansic: 14,857; sh: 9,050; makefile: 550; yacc: 316; sed: 16
file content (217 lines) | stat: -rw-r--r-- 5,450 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 *  canna.c
 *  Copyright(C) 2003- Masahito Omote <omote@utyuuzin.net>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

#include <stdlib.h>
#include <dlfcn.h>
#include <pwd.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include "canna.h"

static struct _canna_api RkFunc;
static void   *canna_lib;
static int     context_num;
static void    canna_get_privatedicname(unsigned char **);

int canna_init(char *cannahost) {
    if(canna_lib) {
	dlclose(canna_lib);
    }

    canna_lib = dlopen("libcanna.so.1", RTLD_LAZY);

    if(canna_lib != NULL) {
	/* 全部使う瘢雹とは思えないので必要なものだけ対応づける */
	RkFunc.Initialize = dlsym(canna_lib, "RkInitialize");
	RkFunc.Finalize = dlsym(canna_lib, "RkFinalize");
	RkFunc.GetWordTextDic = dlsym(canna_lib, "RkGetWordTextDic");
	RkFunc.ListDic = dlsym(canna_lib, "RkListDic");
	RkFunc.GetMountList = dlsym(canna_lib, "RkGetMountList");
	RkFunc.MountDic = dlsym(canna_lib, "RkMountDic");
	RkFunc.UnmountDic = dlsym(canna_lib, "RkUnmountDic");
	RkFunc.DefineDic = dlsym(canna_lib, "RkDefineDic");
	RkFunc.DeleteDic = dlsym(canna_lib, "RkDeleteDic");
	RkFunc.SyncDic = dlsym(canna_lib, "RkSync");

	if(RkFunc.Initialize == NULL || RkFunc.Finalize == NULL ||
	   RkFunc.MountDic == NULL || RkFunc.UnmountDic == NULL ||
	   RkFunc.DefineDic == NULL || RkFunc.DeleteDic == NULL ||
	   RkFunc.GetWordTextDic == NULL || RkFunc.ListDic == NULL ||
	   RkFunc.GetMountList == NULL || RkFunc.SyncDic == NULL)
	{
	    dlclose(canna_lib);
	    return -1;
	}

	context_num = RkFunc.Initialize(cannahost);

	if(context_num >= 0) {
	    return 0;
	}
	else {
	    return -1;
	}
    } else {
	return -1;
    }
}

void canna_close(void) {
    if(RkFunc.Finalize != NULL) {
	RkFunc.Finalize();
    }

    if(canna_lib) {
	dlclose(canna_lib);
    }
}

int canna_get_word_text_dic(unsigned char *dirname,
			    unsigned char *dicname,
			    word **head)
{
    unsigned char buf[1024];
    unsigned char dicname_bk[256];
    int ret = 0;

    if(RkFunc.GetWordTextDic != NULL) {
#ifndef HAVE_STRLCPY
	strncpy(dicname_bk, dicname, 256);
	dicname_bk[255] = '\0';
#else
	strlcpy(dicname_bk, dicname, 256);
#endif
	do {
	    ret = RkFunc.GetWordTextDic(context_num, dirname,
					dicname_bk, buf, 1024);
	    if(ret <= 0)
		break;

	    dicname_bk[0] = '\0';
	    cannadic_parse_line(buf, head);
	} while(ret >= 0);
    }
    if(ret < 0)
	return -1;
    else
	return 0;
}

word *canna_get_word_text_priv_dic(void) {
    struct passwd *pw;
    char *username, *dirname;
    char dir[] = ":user/";
    word *list = NULL;

    pw = getpwuid(getuid());
    if(pw != NULL) {
	username = strdup(pw->pw_name);

	if(username != NULL) {
	    int len;
	    /* dirname := ":user/username" */
	    len = strlen(dir) + strlen(username) + 1;
	    dirname = malloc(sizeof(char) * len);
	    if(dirname != NULL) {
		snprintf(dirname, len, "%s%s", dir, username);
		canna_get_word_text_dic(dirname, "user", &list);
		free(dirname);
	    }
	    free(username);
	}
    }

    return list;
}

int canna_define_dic(unsigned char *dicname, unsigned char *buf) {
    /* buf := phon cclass desc */
    int ret;

    if(RkFunc.MountDic(context_num, (char *)dicname, 0) < 0 )
	return -1;

    ret = RkFunc.DefineDic(context_num, (char *)dicname, buf);

    RkFunc.UnmountDic(context_num, (char *)dicname);

    return ret;
}

int canna_delete_dic(unsigned char *dicname, unsigned char *buf) {
    /* buf := phon cclass desc */
    int ret;

    if(RkFunc.MountDic(context_num, (char *)dicname, 0) < 0 )
	return -1;

    ret = RkFunc.DeleteDic(context_num, (char *)dicname, buf);

    RkFunc.UnmountDic(context_num, (char *)dicname);

    return ret;
}

GList *canna_read_privatediclist(void) {
    struct passwd *pw;
    char *username, *dirname;
    char dir[] = ":user/";
    char dicname = "user"; /* XXX */
    GList *list = NULL;
    unsigned char buf[1024];
    unsigned char dicname_bk[256];
    int ret = 0;

    pw = getpwuid(getuid());
    if(pw != NULL) {
	username = strdup(pw->pw_name);

	if(username != NULL) {
	    int len;
	    /* dirname := ":user/username" */
	    len = strlen(dir) + strlen(username) + 1;
	    dirname = malloc(sizeof(char) * len);
	    if(dirname != NULL) {
		snprintf(dirname, len, "%s%s", dir, username);
		if(RkFunc.GetWordTextDic != NULL) {
#ifndef HAVE_STRLCPY
		    strncpy(dicname_bk, dicname, 256);
		    dicname_bk[255] = '\0';
#else
		    strlcpy(dicname_bk, dicname, 256);
#endif
		    do {
			ret = RkFunc.GetWordTextDic(context_num, dirname,
						    dicname_bk, buf, 1024);
			if(ret <= 0)
			    break;
			
			dicname_bk[0] = '\0';
			list = cannadic_parse_line_glist(buf, list);
		    } while(ret >= 0);
		}
		free(dirname);
	    }
	    free(username);
	}
    }
}