Mercurial > freewnn
comparison Xwnmo/xwnmo/xcvtkey.c @ 0:bbc77ca4def5
initial import
author | Yoshiki Yazawa <yaz@cc.rim.or.jp> |
---|---|
date | Thu, 13 Dec 2007 04:30:14 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:bbc77ca4def5 |
---|---|
1 /* | |
2 * $Id: xcvtkey.c,v 1.2 2001/06/14 18:16:18 ura Exp $ | |
3 */ | |
4 | |
5 /* | |
6 * FreeWnn is a network-extensible Kana-to-Kanji conversion system. | |
7 * This file is part of FreeWnn. | |
8 * | |
9 * Copyright OMRON Corporation. 1987, 1988, 1989, 1990, 1991, 1992, 1999 | |
10 * Copyright 1991, 1992 by Massachusetts Institute of Technology | |
11 * | |
12 * Author: OMRON SOFTWARE Co., Ltd. <freewnn@rd.kyoto.omronsoft.co.jp> | |
13 * | |
14 * This program is free software; you can redistribute it and/or modify | |
15 * it under the terms of the GNU General Public License as published by | |
16 * the Free Software Foundation; either version 2, or (at your option) | |
17 * any later version. | |
18 * | |
19 * This program is distributed in the hope that it will be useful, | |
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 * GNU General Public License for more details. | |
23 * | |
24 * You should have received a copy of the GNU General Public License | |
25 * along with GNU Emacs; see the file COPYING. If not, write to the | |
26 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
27 * | |
28 * Commentary: | |
29 * | |
30 * Change log: | |
31 * | |
32 * Last modified date: 8,Feb.1999 | |
33 * | |
34 * Code: | |
35 * | |
36 */ | |
37 | |
38 /************************************************************************* | |
39 xcvtkey.c | |
40 */ | |
41 | |
42 #include <stdio.h> | |
43 #include "commonhd.h" | |
44 #include "sdefine.h" | |
45 #ifdef XJUTIL | |
46 #include "xjutil.h" | |
47 #include "sxheader.h" | |
48 #include "xext.h" | |
49 #else /* XJUTIL */ | |
50 #include "xim.h" | |
51 #include "sheader.h" | |
52 #include "ext.h" | |
53 #endif /* XJUTIL */ | |
54 | |
55 char *comment_char_str = "#;"; | |
56 | |
57 int | |
58 comment_char (c) | |
59 register char c; | |
60 { | |
61 return ((index (comment_char_str, c) != NULL) ? 1 : 0); | |
62 } | |
63 | |
64 typedef struct | |
65 { | |
66 char *name; | |
67 unsigned int state; | |
68 } | |
69 StateTbl; | |
70 | |
71 static StateTbl state_tbl[] = { | |
72 {"Shift", ShiftMask}, | |
73 {"Lock", LockMask}, | |
74 {"Ctrl", ControlMask}, | |
75 {"Meta", Mod1Mask}, | |
76 {"Meta1", Mod1Mask}, | |
77 {"Meta2", Mod2Mask}, | |
78 {"Meta3", Mod3Mask}, | |
79 {"Meta4", Mod4Mask}, | |
80 {"Meta5", Mod5Mask}, | |
81 {NULL, 0} | |
82 }; | |
83 | |
84 static unsigned int | |
85 get_one_entry (p) | |
86 char **p; | |
87 { | |
88 register char *s; | |
89 register int size; | |
90 register StateTbl *st = state_tbl; | |
91 char buf[512]; | |
92 | |
93 if (!*p || !**p) | |
94 return (-1); | |
95 if (s = index (*p, '|')) | |
96 { | |
97 size = s - *p; | |
98 bcopy (*p, buf, size); | |
99 buf[size] = '\0'; | |
100 for (; st->name; st++) | |
101 { | |
102 if (!strcmp (st->name, buf)) | |
103 { | |
104 *p += size + 1; | |
105 return (st->state); | |
106 } | |
107 } | |
108 } | |
109 return (0); | |
110 } | |
111 | |
112 int | |
113 cvt_key_setup (filename) | |
114 char *filename; | |
115 { | |
116 register int k, cnt = cvt_key_tbl_cnt, line = 0; | |
117 register unsigned int state, total_state; | |
118 KeySym c; | |
119 char ksname[128], code[32], *p; | |
120 char buf[BUFSIZ]; | |
121 FILE *fp; | |
122 register ConvCode *tbl = cvt_key_tbl; | |
123 extern long strtol (); | |
124 | |
125 if ((fp = fopen (filename, "r")) == NULL) | |
126 { | |
127 return (-1); | |
128 } | |
129 | |
130 CONTI: | |
131 while (fgets (buf, BUFSIZ, fp)) | |
132 { | |
133 line++; | |
134 if (comment_char (*buf) || (k = sscanf (buf, "%s %s", ksname, code)) <= 0) | |
135 continue; | |
136 if (k < 2) | |
137 { | |
138 print_out2 ("In cvt file \"%s\", too few arguments in line %d.", filename, line); | |
139 } | |
140 else | |
141 { | |
142 p = ksname; | |
143 total_state = 0; | |
144 while (1) | |
145 { | |
146 state = get_one_entry (&p); | |
147 if (state <= 0) | |
148 break; | |
149 total_state |= state; | |
150 } | |
151 if (state == -1 || (c = XStringToKeysym (p)) == 0) | |
152 { | |
153 print_out2 ("In cvt file \"%s\", a illegal string in line %d.", ksname, line); | |
154 continue; | |
155 } | |
156 for (k = 0; k < cnt; k++) | |
157 { | |
158 if (tbl[k].fromkey.keysym == c && tbl[k].fromkey.state == total_state) | |
159 { | |
160 print_out2 ("In cvt file \"%s\", duplicate declaration in line %d.", ksname, line); | |
161 goto CONTI; | |
162 } | |
163 } | |
164 tbl[cnt].fromkey.keysym = c; | |
165 tbl[cnt].fromkey.state = total_state; | |
166 tbl[cnt++].tokey = strtol (code, (char **) NULL, 0); | |
167 } | |
168 if (cnt >= MAX_CVT_ENTRY_CNT) | |
169 { | |
170 print_out2 ("In cvt file \"%s\", max line is %d.", ksname, MAX_CVT_ENTRY_CNT); | |
171 break; | |
172 } | |
173 } | |
174 fclose (fp); | |
175 cvt_key_tbl_cnt = cnt; | |
176 return (cnt); | |
177 } | |
178 | |
179 | |
180 /* Function of convert */ | |
181 | |
182 int | |
183 cvt_key_fun (keysym, state) | |
184 KeySym keysym; | |
185 int state; | |
186 { | |
187 register int cnt; | |
188 register ConvCode *tbl = cvt_key_tbl; | |
189 | |
190 for (cnt = 0; cnt < cvt_key_tbl_cnt; cnt++) | |
191 { | |
192 if (tbl[cnt].fromkey.keysym == keysym && tbl[cnt].fromkey.state == state) | |
193 return (tbl[cnt].tokey); | |
194 } | |
195 return (-1); | |
196 } | |
197 | |
198 /* Backward compatibility for cvt_meta and cvt_fun. */ | |
199 int | |
200 cvt_meta_and_fun_setup (filename, tbl, state_name) | |
201 char *filename; | |
202 ConvCode *tbl; | |
203 char *state_name; | |
204 { | |
205 register int k, cnt = cvt_key_tbl_cnt, line = 0; | |
206 register StateTbl *st = state_tbl; | |
207 KeySym c; | |
208 unsigned int state = 0; | |
209 char ksname[32], code[32]; | |
210 char buf[BUFSIZ]; | |
211 FILE *fp; | |
212 extern long strtol (); | |
213 | |
214 if ((fp = fopen (filename, "r")) == NULL) | |
215 { | |
216 return (-1); | |
217 } | |
218 | |
219 if (state_name && *state_name) | |
220 { | |
221 for (; st->name; st++) | |
222 { | |
223 if (!strcmp (st->name, state_name)) | |
224 { | |
225 state = st->state; | |
226 break; | |
227 } | |
228 } | |
229 } | |
230 while (fgets (buf, BUFSIZ, fp)) | |
231 { | |
232 line++; | |
233 if (comment_char (*buf) || (k = sscanf (buf, "%s %s", ksname, code)) <= 0) | |
234 continue; | |
235 if (k < 2) | |
236 { | |
237 print_out2 ("In cvt file \"%s\", too few arguments in line %d.", filename, line); | |
238 } | |
239 else if ((c = XStringToKeysym (ksname)) == 0) | |
240 { | |
241 print_out2 ("In cvt file \"%s\", a illegal Keysym in line %d.", ksname, line); | |
242 } | |
243 else | |
244 { | |
245 tbl[cnt].fromkey.keysym = c; | |
246 tbl[cnt].fromkey.state = state; | |
247 tbl[cnt].tokey = strtol (code, (char **) NULL, 0); | |
248 cnt++; | |
249 } | |
250 } | |
251 fclose (fp); | |
252 return (cnt); | |
253 } |