41bb939358b119e09f569f8c0eea4b41989cfae4
[m17n/m17n-lib.git] / src / m17n-core.c
1 /* m17n-core.c -- body of the CORE API.
2    Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
3      National Institute of Advanced Industrial Science and Technology (AIST)
4      Registration Number H15PRO112
5
6    This file is part of the m17n library.
7
8    The m17n library is free software; you can redistribute it and/or
9    modify it under the terms of the GNU Lesser General Public License
10    as published by the Free Software Foundation; either version 2.1 of
11    the License, or (at your option) any later version.
12
13    The m17n library is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    Lesser General Public License for more details.
17
18    You should have received a copy of the GNU Lesser General Public
19    License along with the m17n library; if not, write to the Free
20    Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21    02111-1307, USA.  */
22
23 /***en
24     @addtogroup m17nIntro
25     @brief Introduction to the m17n library.
26
27     <em>API LEVELS</em>
28
29     The API of the m17n library is divided into these five.
30
31     <ol>
32     <li> CORE API
33
34     It provides basic modules to handle M-texts.  To use this API, an
35     application program must include <m17n-core<EM></EM>.h> and be
36     linked with -lm17n-core.
37
38     <li> SHELL API
39
40     It provides modules for character properties, character set
41     handling, code conversion, etc.  They load various kinds of
42     data from the database on demand.  To use this API, an application
43     program must include <m17n<EM></EM>.h> and be linked with
44     -lm17n-core -lm17n.
45
46     When you use this API, CORE API is also available.
47
48     <li> FLT API
49
50     It provides modules for text shaping using @ref mdbFLT.  To use
51     this API, an application program must include <m17n<EM></EM>.h>
52     and be linked with -lm17n-core -lm17n-flt.
53
54     When you use this API, CORE API is also available.
55
56     <li> GUI API
57
58     It provides GUI modules such as drawing and inputting M-texts on a
59     graphic device.  This API itself is independent of graphic
60     devices, but most functions require an argument MFrame that is
61     created for a specific type of graphic devices.  The currently
62     supported graphic devices are null device, the X Window System,
63     and image data (gdImagePtr) of the GD library.
64
65     On a frame of a null device, you cannot draw text nor use input
66     methods.  However, functions like mdraw_glyph_list (), etc. are
67     available.
68
69     On a frame of the X Window System, you can use the whole GUI API.
70
71     On a frame of the GD library, you can use all drawing API but
72     cannot use input methods.
73
74     To use this API, an application program must include
75     <m17n-gui<EM></EM>.h> and be linked with -lm17n-core -lm17n
76     -lm17n-gui.
77
78     When you use this API, CORE, SHELL, and FLT APIs are also
79     available.
80
81     <li> MISC API
82
83     It provides miscellaneous functions to support error handling and
84     debugging.  This API cannot be used standalone; it must be used
85     with one or more APIs listed above.  To use this API, an
86     application program must include <m17n-misc<EM></EM>.h> in
87     addition to one of the header files described above.
88
89     </ol>
90
91     See also the section @ref m17n-config "m17n-config(1)".
92
93     <em>ENVIRONMENT VARIABLES</em>
94
95     The m17n library pays attention to the following environment
96     variables.
97
98     <ul>
99     <li> @c M17NDIR
100
101     The name of the directory that contains data of the m17n database.
102     See @ref m17nDatabase for details.
103
104     <li> @c MDEBUG_XXX
105
106     Environment variables whose names start with "MDEBUG_" control
107     debug information output.  See @ref m17nDebug for details.
108
109     </ul>
110
111     <em>API NAMING CONVENTION</em>
112
113     The m17n library exports functions, variables, macros, and types.
114     All of them start with the letter 'm' or 'M', and are followed by
115     an object name (e.g. "symbol", "plist") or a module name
116     (e.g. draw, input).  Note that the name of M-text objects start
117     with "mtext" and not with "mmtext".
118
119     <ul>
120
121     <li> functions -- mobject () or mobject_xxx ()
122
123     They start with 'm' and are followed by an object name in lower
124     case.  Words are separated by '_'.  For example, msymbol (),
125     mtext_ref_char (), mdraw_text ().
126
127     <li> non-symbol variables -- mobject, or mobject_xxx
128     
129     The naming convention is the same as functions (e.g. mface_large).
130
131     <li> symbol variables -- Mname
132
133     Variables of the type MSymbol start with 'M' and are followed by
134     their names.  Words are separated by '_'.  For example, Mlanguage
135     (the name is "language"), Miso_2022 (the name is "iso-2022").
136
137     <li> macros -- MOBJECT_XXX
138
139     They start with 'M' and are followed by an object name in upper
140     case.  Words are separated by '_'.
141
142     <li> types -- MObject or MObjectXxx
143
144     They start with 'M' and are followed by capitalized object names.
145     Words are concatenated directly and no '_' are used.  For example,
146     MConverter, MInputDriver.
147
148     </ul>
149
150   */
151
152 /***ja
153     @addtogroup m17nIntro
154     @brief m17n ¥é¥¤¥Ö¥é¥ê ¥¤¥ó¥È¥í¥À¥¯¥·¥ç¥ó.
155
156     @em API¤Î¥ì¥Ù¥ë
157
158     m17n ¥é¥¤¥Ö¥é¥ê¤Î API ¤Ï°Ê²¼¤Î£´¼ï¤ËʬÎव¤ì¤Æ¤¤¤ë¡£
159
160     <ol>
161     <li> ¥³¥¢ API
162
163     M-text ¤ò°·¤¦¤¿¤á¤Î´ðËÜŪ¤Ê¥â¥¸¥å¡¼¥ë¤òÄ󶡤¹¤ë¡£
164     ¤³¤Î API ¤òÍøÍѤ¹¤ë¤¿¤á¤Ë¤Ï¡¢¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¥×¥í¥°¥é¥à¤Ï
165     <m17n-core<EM></EM>.h> ¤ò include ¤·¡¢ -lm17n-core
166     ¤Ç¥ê¥ó¥¯¤µ¤ì¤Ê¤¯¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£
167
168     <li> ¥·¥§¥ë API
169
170     Ê¸»ú¥×¥í¥Ñ¥Æ¥£¡¢Ê¸»ú½¸¹çÁàºî¡¢¥³¡¼¥ÉÊÑ´¹Åù¤Î¤¿¤á¤Î¥â¥¸¥å¡¼¥ë¤òÄ󶡤¹¤ë¡£
171     ¤³¤ì¤é¤Î¥â¥¸¥å¡¼¥ë¤Ï¡¢¥Ç¡¼¥¿¥Ù¡¼¥¹¤«¤éɬÍפ˱þ¤¸¤Æ¿Íͤʥǡ¼¥¿¤ò¥í¡¼¥É¤¹¤ë¡£
172     ¤³¤Î API ¤òÍøÍѤ¹¤ë¤¿¤á¤Ë¤Ï¡¢¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¥×¥í¥°¥é¥à¤Ï
173     <m17n<EM></EM>.h> ¤ò include ¤·¡¢ -lm17n-core -lm17n
174     ¤Ç¥ê¥ó¥¯¤µ¤ì¤Ê¤¯¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£
175
176     ¤³¤Î API ¤ò»ÈÍѤ¹¤ì¤Ð¡¢¥³¥¢ API ¤â¼«Æ°Åª¤Ë»ÈÍѲÄǽ¤È¤Ê¤ë¡£
177
178     <li> FLT API
179
180     Ê¸»úÎóɽ¼¨¤Ë @ref mdbFLT ¤òÍѤ¤¤ë¥â¥¸¥å¡¼¥ë¤òÄ󶡤¹¤ë¡£¤³¤Î API
181     ¤òÍøÍѤ¹¤ë¤¿¤á¤Ë¤Ï¡¢¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¥×¥í¥°¥é¥à¤Ï <m17n<EM></EM>.h> 
182     ¤ò include ¤·¡¢ -lm17n-core -lm17n-flt ¤Ç¥ê¥ó¥¯¤µ¤ì¤Ê¤¯¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£
183
184     ¤³¤Î API ¤ò»ÈÍѤ¹¤ì¤Ð¡¢¥³¥¢ API ¤â¼«Æ°Åª¤Ë»ÈÍѲÄǽ¤È¤Ê¤ë¡£
185
186     <li> GUI API
187
188     ¥°¥é¥Õ¥£¥Ã¥¯¥Ç¥Ð¥¤¥¹¾å¤Ç M-text ¤òɽ¼¨¤·¤¿¤êÆþÎϤ·¤¿¤ê¤¹¤ë¤¿¤á¤Î
189     GUI ¥â¥¸¥å¡¼¥ë¤òÄ󶡤¹¤ë¡£¤³¤Î API
190     ¼«ÂΤϥ°¥é¥Õ¥£¥Ã¥¯¥Ç¥Ð¥¤¥¹¤È¤ÏÆÈΩ¤Ç¤¢¤ë¤¬¡¢
191     Â¿¤¯¤Î´Ø¿ô¤ÏÆÃÄê¤Î¥°¥é¥Õ¥£¥Ã¥¯¥Ç¥Ð¥¤¥¹ÍѤ˺îÀ®¤µ¤ì¤¿ 
192     MFrame ¤ò°ú¿ô¤Ë¼è¤ë¡£
193     ¸½»þÅÀ¤Ç¥µ¥Ý¡¼¥È¤µ¤ì¤Æ¤¤¤ë¥°¥é¥Õ¥£¥Ã¥¯¥Ç¥Ð¥¤¥¹¤Ï¡¢¥Ì¥ë¥Ç¥Ð¥¤¥¹¡¢X
194     ¥¦¥£¥ó¥É¥¦¥·¥¹¥Æ¥à¡¢¤ª¤è¤Ó GD ¥é¥¤¥Ö¥é¥ê¤Î¥¤¥á¡¼¥¸¥Ç¡¼¥¿
195     (gdImagePtr) ¤Ç¤¢¤ë¡£
196
197     ¥Ì¥ë¥Ç¥Ð¥¤¥¹¤Î¥Õ¥ì¡¼¥à¾å¤Ç¤Ïɽ¼¨¤âÆþÎϤâ¤Ç¤­¤Ê¤¤¡£¤¿¤À¤·
198     mdraw_glyph_list () ¤Ê¤É¤Î´Ø¿ô¤Ï»ÈÍѲÄǽ¤Ç¤¢¤ë¡£
199
200     X ¥¦¥£¥ó¥É¥¦¥·¥¹¥Æ¥à¤Î¥Õ¥ì¡¼¥à¾å¤Ç¤Ï¤¹¤Ù¤Æ¤Î GUI API ¤¬»ÈÍѤǤ­¤ë¡£
201
202     GD ¥é¥¤¥Ö¥é¥ê¤Î¥Õ¥ì¡¼¥à¾å¤Ç¤Ï¡¢ÉÁ²èÍѤΠAPI
203     ¤Ï¤¹¤Ù¤Æ»ÈÍѤǤ­¤ë¤¬¡¢ÆþÎϤϤǤ­¤Ê¤¤¡£
204
205     ¤³¤Î API ¤ò»ÈÍѤ¹¤ë¤¿¤á¤Ë¤Ï¡¢¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¥×¥í¥°¥é¥à¤Ï
206     <m17n-gui<EM></EM>.h> ¤ò include ¤·¡¢-lm17n-core -lm17n -lm17n-gui
207     ¤Ç¥ê¥ó¥¯¤µ¤ì¤Ê¤¯¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£
208
209     ¤³¤Î API ¤ò»ÈÍѤ¹¤ì¤Ð¡¢¥³¥¢ API¡¢¥·¥§¥ë API¡¢¤ª¤è¤Ó FLT API
210     ¤â¼«Æ°Åª¤Ë»ÈÍѲÄǽ¤È¤Ê¤ë¡£
211
212     <li> ¤½¤Î¾¤Î API
213
214     ¥¨¥é¡¼½èÍý¡¢¥Ç¥Ð¥Ã¥°ÍѤΤ½¤Î¾¤Î´Ø¿ô¤òÄ󶡤¹¤ë¡£¤³¤Î API
215     ¤Ï¤½¤ì¤À¤±¤Ç¤Ï»ÈÍѤǤ­¤º¡¢¾åµ­¤Î¾¤Î API
216     ¤È¶¦¤Ë»È¤¦¡£ÍøÍѤ¹¤ë¤¿¤á¤Ë¤Ï¡¢¾åµ­¤Î¤¤¤º¤ì¤«¤Îinclude
217     ¥Õ¥¡¥¤¥ë¤Ë²Ã¤¨¤Æ¡¢ <m17n-misc<EM></EM>.h> ¤òinclude
218     ¤·¤Ê¤¯¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£
219
220     </ol>
221
222     @ref m17n-config "m17n-config(1)" Àá¤â»²¾È¡£
223
224     @em ´Ä¶­ÊÑ¿ô
225
226     m17n ¥é¥¤¥Ö¥é¥ê¤Ï°Ê²¼¤Î´Ä¶­ÊÑ¿ô¤ò»²¾È¤¹¤ë¡£
227
228     <ul>
229     <li> @c M17NDIR
230
231     m17n ¥Ç¡¼¥¿¥Ù¡¼¥¹¤Î¥Ç¡¼¥¿¤ò³ÊǼ¤·¤¿¥Ç¥£¥ì¥¯¥È¥ê¤Î̾Á°¡£¾ÜºÙ¤Ï @ref
232     m17nDatabase »²¾È¡£
233
234     <li> @c MDEBUG_XXX
235
236     "MDEBUG_" ¤Ç»Ï¤Þ¤ë̾Á°¤ò»ý¤Ä´Ä¶­ÊÑ¿ô¤Ï¥Ç¥Ð¥Ã¥°¾ðÊó¤Î½ÐÎϤòÀ©¸æ¤¹¤ë¡£
237     ¾ÜºÙ¤Ï @ref m17nDebug »²¾È¡£
238
239     </ul>
240
241     @em API @em ¤Î̿̾µ¬Â§
242
243     m17n ¥é¥¤¥Ö¥é¥ê¤Ï¡¢´Ø¿ô¡¢ÊÑ¿ô¡¢¥Þ¥¯¥í¡¢·¿¤ò export ¤¹¤ë¡£¤½¤ì¤é¤Ï 'm' 
244     ¤Þ¤¿¤Ï 'M' ¤Î¤¢¤È¤Ë¥ª¥Ö¥¸¥§¥¯¥È̾ ("symbol"¡¢"plist" ¤Ê¤É)
245     ¤Þ¤¿¤Ï¥â¥¸¥å¡¼¥ë̾ (draw, input ¤Ê¤É) ¤ò³¤±¤¿¤â¤Î¤Ç¤¢¤ë¡£
246     M-text ¥ª¥Ö¥¸¥§¥¯¥È¤Î̾Á°¤Ï "mmtext" ¤Ç¤Ï¤Ê¤¯¤Æ "mtext"
247     ¤Ç»Ï¤Þ¤ë¤³¤È¤ËÃí°Õ¡£
248     
249     <ul>
250
251     <li> ´Ø¿ô -- mobject () ¤Þ¤¿¤Ï mobject_xxx ()
252
253     'm' ¤Î¤¢¤È¤Ë¾®Ê¸»ú¤Ç¥ª¥Ö¥¸¥§¥¯¥È̾¤¬Â³¤¯¡£Ã±¸ì´Ö¤Ï '_'
254     ¤Ç¶èÀÚ¤é¤ì¤ë¡£¤¿¤È¤¨¤Ð¡¢msymbol (),
255      mtext_ref_char (), mdraw_text () ¤Ê¤É¡£
256
257     <li> ¥·¥ó¥Ü¥ë¤Ç¤Ê¤¤ÊÑ¿ô -- mobject,  ¤Þ¤¿¤Ï mobject_xxx
258     
259     ´Ø¿ô¤ÈƱ¤¸Ì¿Ì¾µ¬Â§¤Ë½¾¤¦¡£¤¿¤È¤¨¤Ð  mface_large ¤Ê¤É¡£
260
261     <li> ¥·¥ó¥Ü¥ëÊÑ¿ô -- Mname
262
263     MSymbol ·¿ÊÑ¿ô¤Ï¡¢'M' ¤Î¸å¤Ë̾Á°¤¬Â³¤¯¡£Ã±¸ì´Ö¤Ï '_'
264     ¤Ç¶èÀÚ¤é¤ì¤ë¡£¤¿¤È¤¨¤Ð Mlanguage (̾Á°¤Ï "language"), Miso_2022
265     (̾Á°¤Ï"iso-2022") ¤Ê¤É¡£
266
267     <li> ¥Þ¥¯¥í -- MOBJECT_XXX
268
269     'M' ¤Î¸å¤ËÂçʸ»ú¤Ç¥ª¥Ö¥¸¥§¥¯¥È̾¤¬Â³¤¯¡£Ã±¸ì´Ö¤Ï '_' ¤Ç¶èÀÚ¤é¤ì¤ë¡£
270
271     <li> ¥¿¥¤¥× -- MObject ¤Þ¤¿¤Ï MObjectXxx
272
273     'M' ¤Î¸å¤ËÂçʸ»ú¤Ç»Ï¤Þ¤ë¥ª¥Ö¥¸¥§¥¯¥È̾¤¬Â³¤¯¡£Ã±¸ì¤ÏϢ³¤·¤Æ½ñ¤«¤ì¡¢
274     '_' ¤ÏÍѤ¤¤é¤ì¤Ê¤¤¡£¤¿¤È¤¨¤Ð MConverter, MInputDriver ¤Ê¤É¡£
275
276     </ul>
277     
278     */
279 /*=*/
280 /*** @{ */
281 #ifdef FOR_DOXYGEN
282 /***en
283     The #M17NLIB_MAJOR_VERSION macro gives the major version number
284     of the m17n library.  */
285 /***ja
286     ¥Þ¥¯¥í #M17NLIB_MAJOR_VERSION ¤Ï m17n 
287     ¥é¥¤¥Ö¥é¥ê¤Î¥á¥¸¥ã¡¼¥Ð¡¼¥¸¥ç¥óÈÖ¹æ¤òÍ¿¤¨¤ë.  */
288
289 #define M17NLIB_MAJOR_VERSION
290
291 /*=*/
292
293 /***en
294     The #M17NLIB_MINOR_VERSION macro gives the minor version number
295     of the m17n library.  */
296
297 /***ja
298     ¥Þ¥¯¥í #M17NLIB_MINOR_VERSION ¤Ï m17n 
299     ¥é¥¤¥Ö¥é¥ê¤Î¥Þ¥¤¥Ê¡¼¥Ð¡¼¥¸¥ç¥óÈÖ¹æ¤òÍ¿¤¨¤ë.  */
300
301 #define M17NLIB_MINOR_VERSION
302
303 /*=*/
304
305 /***en
306     The #M17NLIB_PATCH_LEVEL macro gives the patch level number
307     of the m17n library.  */
308
309 /***ja
310     ¥Þ¥¯¥í #M17NLIB_PATCH_LEVEL ¤Ï m17n 
311     ¥é¥¤¥Ö¥é¥ê¤Î¥Ñ¥Ã¥Á¥ì¥Ù¥ëÈÖ¹æ¤òÍ¿¤¨¤ë.  */
312
313 #define M17NLIB_PATCH_LEVEL
314
315 /*=*/
316
317 /***en
318     The #M17NLIB_VERSION_NAME macro gives the version name of the
319     m17n library as a string.  */
320
321 /***ja
322     ¥Þ¥¯¥í #M17NLIB_VERSION_NAME ¤Ï m17n 
323     ¥é¥¤¥Ö¥é¥ê¤Î¥Ð¡¼¥¸¥ç¥ó̾¤òʸ»úÎó¤È¤·¤ÆÍ¿¤¨¤ë.  */
324
325 #define M17NLIB_VERSION_NAME
326
327 /*=*/
328
329 /***en
330     @brief Initialize the m17n library.
331
332     The macro M17N_INIT () initializes the m17n library.  This macro
333     must be called before any m17n functions are used.
334
335     It is safe to call this macro multiple times, but in that case,
336     the macro M17N_FINI () must be called the same times to free the
337     memory.
338
339     If the initialization was successful, the external variable
340     #merror_code is set to 0.  Otherwise it is set to -1.
341
342     @seealso
343     M17N_FINI (), m17n_status ()  */
344
345 /***ja
346     @brief m17n ¥é¥¤¥Ö¥é¥ê¤ò½é´ü²½¤¹¤ë.
347
348     ¥Þ¥¯¥í M17N_INIT () ¤Ï m17n ¥é¥¤¥Ö¥é¥ê¤ò½é´ü²½¤¹¤ë¡£m17n 
349     ¤Î´Ø¿ô¤òÍøÍѤ¹¤ëÁ°¤Ë¡¢¤³¤Î¥Þ¥¯¥í¤ò¤Þ¤º¸Æ¤Ð¤Ê¤¯¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£
350     
351     ¤³¤Î¥Þ¥¯¥í¤òÊ£¿ô²ó¸Æ¤ó¤Ç¤â°ÂÁ´¤Ç¤¢¤ë¤¬¡¢¤½¤Î¾ì¹ç¥á¥â¥ê¤ò²òÊü¤¹¤ë¤¿¤á¤Ë¥Þ¥¯¥í
352     M17N_FINI () ¤òƱ¤¸²ó¿ô¸Æ¤ÖɬÍפ¬¤¢¤ë¡£
353
354     ³°ÉôÊÑ¿ô #merror_code ¤Ï¡¢½é´ü²½¤¬À®¸ù¤¹¤ì¤Ð 0 ¤Ë¡¢¤½¤¦¤Ç¤Ê¤±¤ì¤Ð 
355     -1 ¤ËÀßÄꤵ¤ì¤ë¡£  
356
357     @seealso
358     M17N_FINI (), m17n_status ()  */
359
360 #define M17N_INIT()
361
362 /*=*/
363
364 /***en
365     @brief Finalize the m17n library.
366
367     The macro M17N_FINI () finalizes the m17n library.  It frees all the
368     memory area used by the m17n library.  Once this macro is
369     called, no m17n functions should be used until the
370     macro M17N_INIT () is called again.
371
372     If the macro M17N_INIT () was called N times, the Nth call of this
373     macro actually free the memory. 
374
375     @seealso
376     M17N_INIT (), m17n_status ()  */
377 /***ja
378     @brief m17n ¥é¥¤¥Ö¥é¥ê¤ò½ªÎ»¤¹¤ë. 
379
380     ¥Þ¥¯¥í M17N_FINI () ¤Ï m17n ¥é¥¤¥Ö¥é¥ê¤ò½ªÎ»¤¹¤ë¡£m17n 
381     ¥é¥¤¥Ö¥é¥ê¤¬»È¤Ã¤¿Á´¤Æ¤Î¥á¥â¥êÎΰè¤Ï²òÊü¤µ¤ì¤ë¡£°ìÅÙ¤³¤Î¥Þ¥¯¥í¤¬¸Æ¤Ð¤ì¤¿¤é¡¢¥Þ¥¯¥í
382     M17N_INIT () ¤¬ºÆÅٸƤФì¤ë¤Þ¤Ç m17n ´Ø¿ô¤Ï»È¤¦¤Ù¤­¤Ç¤Ê¤¤¡£
383
384     ¥Þ¥¯¥í M17N_INIT () ¤¬ N ²ó¸Æ¤Ð¤ì¤Æ¤¤¤¿¾ì¹ç¤Ë¤Ï¡¢¤³¤Î¥Þ¥¯¥í¤¬ N 
385     ²ó¸Æ¤Ð¤ì¤Æ½é¤á¤Æ¥á¥â¥ê¤¬²òÊü¤µ¤ì¤ë¡£
386
387     @seealso
388     M17N_INIT (), m17n_status ()  */
389
390 #define M17N_FINI()
391 #endif /* FOR_DOXYGEN */
392 /*=*/
393 /*** @} */ 
394 /*=*/
395
396 #if !defined (FOR_DOXYGEN) || defined (DOXYGEN_INTERNAL_MODULE)
397 /*** @addtogroup m17nInternal
398      @{ */
399
400 #include <config.h>
401 #include <stdio.h>
402 #include <stdlib.h>
403 #include <string.h>
404 #include <sys/time.h>
405 #include <sys/resource.h>
406 #include <unistd.h>
407
408 #include "m17n-core.h"
409 #include "m17n-misc.h"
410 #include "internal.h"
411 #include "symbol.h"
412
413 static void
414 default_error_handler (enum MErrorCode err)
415 {
416   exit (err);
417 }
418
419 static struct timeval time_stack[16];
420 static int time_stack_index;
421
422 static M17NObjectArray *object_array_root;
423
424 static void
425 report_object_array ()
426 {
427   fprintf (stderr, "%16s %7s %7s %7s\n",
428            "object", "created", "freed", "alive");
429   fprintf (stderr, "%16s %7s %7s %7s\n",
430            "------", "-------", "-----", "-----");
431   for (; object_array_root; object_array_root = object_array_root->next)
432     {
433       M17NObjectArray *array = object_array_root;
434
435       fprintf (stderr, "%16s %7d %7d %7d\n", array->name,
436                array->used, array->used - array->count, array->count);
437       if (array->count > 0)
438         {
439           int i;
440           for (i = 0; i < array->used && ! array->objects[i]; i++);
441
442           if (strcmp (array->name, "M-text") == 0)
443             {
444               MText *mt = (MText *) array->objects[i];
445
446               if (mt->format <= MTEXT_FORMAT_UTF_8)
447                 fprintf (stderr, "\t\"%s\"\n", (char *) mt->data);
448             }
449           else if (strcmp (array->name, "Plist") == 0)
450             {
451               MPlist *plist = (MPlist *) array->objects[i];
452
453               mdebug_dump_plist (plist, 8);
454               fprintf (stderr, "\n");
455             }
456         }
457
458       if (array->used > 0)
459         {
460           free (array->objects);
461           array->count = array->used = 0;
462         }
463     }
464 }
465
466
467 \f
468 /* Internal API */
469
470 int m17n__core_initialized;
471 int m17n__shell_initialized;
472 int m17n__gui_initialized;
473
474 int mdebug__flags[MDEBUG_MAX];
475 FILE *mdebug__output;
476
477 void
478 mdebug__push_time ()
479 {
480   struct timezone tz;
481
482   gettimeofday (time_stack + time_stack_index++, &tz);
483 }
484
485 void
486 mdebug__pop_time ()
487 {
488   time_stack_index--;
489 }
490
491 void
492 mdebug__print_time ()
493 {
494   struct timeval tv;
495   struct timezone tz;
496   long diff;
497
498   gettimeofday (&tv, &tz);
499   diff = ((tv.tv_sec - time_stack[time_stack_index - 1].tv_sec) * 1000000
500           + (tv.tv_usec - time_stack[time_stack_index - 1].tv_usec));
501   fprintf (stderr, "%8ld ms.", diff);
502   time_stack[time_stack_index - 1] = tv;
503 }
504
505 static void
506 SET_DEBUG_FLAG (char *env_name, enum MDebugFlag flag)
507 {
508   char *env_value = getenv (env_name);
509
510   if (env_value)
511     {
512       int int_value = atoi (env_value);
513
514       if (flag == MDEBUG_ALL)
515         {
516           int i;
517           for (i = 0; i < MDEBUG_MAX; i++)
518             mdebug__flags[i] = int_value;
519         }
520       else
521         mdebug__flags[flag] = int_value;
522     }
523 }
524
525 void
526 mdebug__add_object_array (M17NObjectArray *array, char *name)
527 {
528   array->name = name;
529   array->count = 0;
530   array->next = object_array_root;
531   object_array_root = array;
532 }
533
534
535 void
536 mdebug__register_object (M17NObjectArray *array, void *object)
537 {
538   if (array->used == 0)
539     MLIST_INIT1 (array, objects, 256);
540   array->count++;
541   MLIST_APPEND1 (array, objects, object, MERROR_OBJECT);
542 }
543
544 void
545 mdebug__unregister_object (M17NObjectArray *array, void *object)
546 {
547   array->count--;
548   if (array->count >= 0)
549     {
550       int i;
551
552       for (i = array->used - 1; i >= 0 && array->objects[i] != object; i--);
553       if (i >= 0)
554         {
555           if (i == array->used - 1)
556             array->used--;
557           array->objects[i] = NULL;
558         }
559       else
560         mdebug_hook ();
561     }
562   else                                                                  \
563     mdebug_hook ();
564 }
565
566 \f
567 /* External API */
568
569 /* The following two are actually not exposed to a user but concealed
570    by the macro M17N_INIT (). */
571
572 void
573 m17n_init_core (void)
574 {
575   int mdebug_flag = MDEBUG_INIT;
576
577   merror_code = MERROR_NONE;
578   if (m17n__core_initialized++)
579     return;
580
581   m17n_memory_full_handler = default_error_handler;
582
583   SET_DEBUG_FLAG ("MDEBUG_ALL", MDEBUG_ALL);
584   SET_DEBUG_FLAG ("MDEBUG_INIT", MDEBUG_INIT);
585   SET_DEBUG_FLAG ("MDEBUG_FINI", MDEBUG_FINI);
586   SET_DEBUG_FLAG ("MDEBUG_CHARSET", MDEBUG_CHARSET);
587   SET_DEBUG_FLAG ("MDEBUG_CODING", MDEBUG_CODING);
588   SET_DEBUG_FLAG ("MDEBUG_DATABASE", MDEBUG_DATABASE);
589   SET_DEBUG_FLAG ("MDEBUG_FONT", MDEBUG_FONT); 
590   SET_DEBUG_FLAG ("MDEBUG_FLT", MDEBUG_FLT);
591   SET_DEBUG_FLAG ("MDEBUG_FONTSET", MDEBUG_FONTSET);
592   SET_DEBUG_FLAG ("MDEBUG_INPUT", MDEBUG_INPUT);
593   /* for backward compatibility... */
594   SET_DEBUG_FLAG ("MDEBUG_FONT_FLT", MDEBUG_FLT);
595   SET_DEBUG_FLAG ("MDEBUG_FONT_OTF", MDEBUG_FLT);
596   {
597     char *env_value = getenv ("MDEBUG_OUTPUT_FILE");
598
599     mdebug__output = NULL;
600     if (env_value)
601       {
602         if (strcmp (env_value, "stdout"))
603           mdebug__output = stdout;
604         else
605           mdebug__output = fopen (env_value, "a");
606       }
607     if (! mdebug__output)
608       mdebug__output = stderr;
609   }
610
611   MDEBUG_PUSH_TIME ();
612   MDEBUG_PUSH_TIME ();
613   if (msymbol__init () < 0)
614     goto err;
615   MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize symbol module."));
616   if  (mplist__init () < 0)
617     goto err;
618   MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize plist module."));
619   if (mchar__init () < 0)
620     goto err;
621   MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize character module."));
622   if  (mchartable__init () < 0)
623     goto err;
624   MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize chartable module."));
625   if (mtext__init () < 0 || mtext__prop_init () < 0)
626     goto err;
627   MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize mtext module."));
628   if (mdatabase__init () < 0)
629     goto err;
630   MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize database module."));
631
632 #if ENABLE_NLS
633   bindtextdomain ("m17n-lib", GETTEXTDIR);
634   bindtextdomain ("m17n-db", GETTEXTDIR);
635   bindtextdomain ("m17n-contrib", GETTEXTDIR);
636   bind_textdomain_codeset ("m17n-lib", "UTF-8");
637   bind_textdomain_codeset ("m17n-db", "UTF-8");
638   bind_textdomain_codeset ("m17n-contrib", "UTF-8");
639 #endif
640
641  err:
642   MDEBUG_POP_TIME ();
643   MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize the core modules."));
644   MDEBUG_POP_TIME ();
645 }
646
647 void
648 m17n_fini_core (void)
649 {
650   int mdebug_flag = MDEBUG_FINI;
651
652   if (m17n__core_initialized == 0
653       || --m17n__core_initialized > 0)
654     return;
655
656   MDEBUG_PUSH_TIME ();
657   MDEBUG_PUSH_TIME ();
658   mchartable__fini ();
659   MDEBUG_PRINT_TIME ("FINI", (stderr, " to finalize chartable module."));
660   mtext__fini ();
661   MDEBUG_PRINT_TIME ("FINI", (stderr, " to finalize mtext module."));
662   msymbol__fini ();
663   MDEBUG_PRINT_TIME ("FINI", (stderr, " to finalize symbol module."));
664   mplist__fini ();
665   MDEBUG_PRINT_TIME ("FINI", (stderr, " to finalize plist module."));
666   /* We must call this after the aboves because it frees interval
667      pools.  */
668   mtext__prop_fini ();
669   MDEBUG_PRINT_TIME ("FINI", (stderr, " to finalize textprop module."));
670   MDEBUG_POP_TIME ();
671   MDEBUG_PRINT_TIME ("FINI", (stderr, " to finalize the core modules."));
672   MDEBUG_POP_TIME ();
673   if (mdebug__flags[MDEBUG_FINI])
674     report_object_array ();
675   msymbol__free_table ();
676   if (mdebug__output != stderr)
677     fclose (mdebug__output);
678 }
679
680 /*** @} */
681 #endif /* !FOR_DOXYGEN || DOXYGEN_INTERNAL_MODULE */
682 /*=*/
683
684 /*** @addtogroup m17nIntro */
685
686 /*** @{  */
687 /*=*/
688
689 /***en
690     @brief Report which part of the m17n library is initialized.
691
692     The m17n_status () function returns one of these values depending
693     on which part of the m17n library is initialized:
694
695         #M17N_NOT_INITIALIZED, #M17N_CORE_INITIALIZED,
696         #M17N_SHELL_INITIALIZED, #M17N_GUI_INITIALIZED  */
697
698 /***ja
699     @brief m17n ¥é¥¤¥Ö¥é¥ê¤Î¤É¤ÎÉôʬ¤¬½é´ü²½¤µ¤ì¤¿¤«Êó¹ð¤¹¤ë.
700
701     ´Ø¿ô m17n_status () ¤Ï 
702     m17n ¥é¥¤¥Ö¥é¥ê¤Î¤É¤ÎÉôʬ¤¬½é´ü²½¤µ¤ì¤¿¤«¤Ë±þ¤¸¤Æ¡¢°Ê²¼¤ÎÃͤΤ¤¤º¤ì¤«¤òÊÖ¤¹¡£
703
704         #M17N_NOT_INITIALIZED, #M17N_CORE_INITIALIZED,
705         #M17N_SHELL_INITIALIZED, #M17N_GUI_INITIALIZED  */
706
707 enum M17NStatus
708 m17n_status (void)
709 {
710   return (m17n__gui_initialized ? M17N_GUI_INITIALIZED
711           : m17n__shell_initialized ? M17N_SHELL_INITIALIZED
712           : m17n__core_initialized ? M17N_CORE_INITIALIZED
713           : M17N_NOT_INITIALIZED);
714 }
715
716 /*** @} */
717
718 /*=*/
719 /***en
720     @addtogroup m17nObject
721     @brief Managed objects are objects managed by the reference count.
722
723     There are some types of m17n objects that are managed by their
724     reference count.  Those objects are called @e managed @e objects.
725     When created, the reference count of a managed object is
726     initialized to one.  The m17n_object_ref () function increments
727     the reference count of a managed object by one, and the
728     m17n_object_unref () function decrements by one.  A managed
729     object is automatically freed when its reference count becomes
730     zero.
731
732     A property whose key is a managing key can have only a managed
733     object as its value.  Some functions, for instance msymbol_put ()
734     and mplist_put (), pay special attention to such a property.
735
736     In addition to the predefined managed object types, users can
737     define their own managed object types.  See the documentation of
738     the m17n_object () for more details.  */
739 /***ja
740     @addtogroup m17nObject
741     @brief ´ÉÍý²¼¥ª¥Ö¥¸¥§¥¯¥È¤È¤Ï»²¾È¿ô¤Ë¤è¤Ã¤Æ´ÉÍý¤µ¤ì¤Æ¤¤¤ë¥ª¥Ö¥¸¥§¥¯¥È¤Ç¤¢¤ë.
742
743     m17n ¥ª¥Ö¥¸¥§¥¯¥È¤Î¤¢¤ë·¿¤Î¤â¤Î¤Ï¡¢»²¾È¿ô¤Ë¤è¤Ã¤Æ´ÉÍý¤µ¤ì¤Æ¤¤¤ë¡£
744     ¤½¤ì¤é¤Î¥ª¥Ö¥¸¥§¥¯¥È¤Ï @e ´ÉÍý²¼¥ª¥Ö¥¸¥§¥¯¥È ¤È¸Æ¤Ð¤ì¤ë¡£À¸À®¤µ¤ì¤¿»þÅÀ¤Ç¤Î»²¾È¿ô¤Ï
745     1 ¤Ë½é´ü²½¤µ¤ì¤Æ¤¤¤ë¡£´Ø¿ô m17n_object_ref () ¤Ï´ÉÍý²¼¥ª¥Ö¥¸¥§¥¯¥È¤Î»²¾È¿ô¤ò
746     1 Áý¤ä¤·¡¢´Ø¿ôm17n_object_unref () ¤Ï 1 ¸º¤é¤¹¡£»²¾È¿ô¤¬
747     0 ¤Ë¤Ê¤Ã¤¿´ÉÍý²¼¥ª¥Ö¥¸¥§¥¯¥È¤Ï¼«Æ°Åª¤Ë²òÊü¤µ¤ì¤ë¡£
748
749     ¥­¡¼¤¬´ÉÍý¥­¡¼¤Ç¤¢¤ë¥×¥í¥Ñ¥Æ¥£¤Ï¡¢ÃͤȤ·¤Æ´ÉÍý²¼¥ª¥Ö¥¸¥§¥¯¥È¤À¤±¤ò¼è¤ë¡£
750     ´Ø¿ô msymbol_put () ¤ä mplist_put () ¤Ê¤É¤Ï¤½¤ì¤é¤Î¥×¥í¥Ñ¥Æ¥£¤òÆÃÊÌ°·¤¤¤¹¤ë¡£
751
752     ÄêµÁºÑ¤ß´ÉÍý²¼¥ª¥Ö¥¸¥§¥¯¥È¥¿¥¤¥×¤Î¾¤Ë¡¢¥æ¡¼¥¶¤ÏɬÍפʴÉÍý²¼¥ª¥Ö¥¸¥§¥¯¥È¥¿¥¤¥×¤ò¼«Ê¬¤ÇÄêµÁ¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë¡£¾ÜºÙ¤Ï
753     m17n_object () ¤ÎÀâÌÀ¤ò»²¾È¡£  */
754
755 /*** @{  */
756 /*=*/
757 /***en
758     @brief Allocate a managed object.
759
760     The m17n_object () function allocates a new managed object of
761     $SIZE bytes and sets its reference count to 1.  $FREER is the
762     function that is used to free the object when the reference count
763     becomes 0.  If $FREER is NULL, the object is freed by the free ()
764     function.
765
766     The heading bytes of the allocated object is occupied by
767     #M17NObjectHead.  That area is reserved for the m17n library and
768     application programs should never touch it.
769
770     @return
771     This function returns a newly allocated object.
772
773     @errors
774     This function never fails.  */
775
776 /***ja
777     @brief ´ÉÍý²¼¥ª¥Ö¥¸¥§¥¯¥È¤ò³ä¤êÅö¤Æ¤ë.
778
779     ´Ø¿ô m17n_object () ¤Ï$SIZE ¥Ð¥¤¥È¤Î¿·¤·¤¤´ÉÍý²¼¥ª¥Ö¥¸¥§¥¯¥È¤ò³ä¤êÅö¤Æ¡¢¤½¤Î»²¾È¿ô¤ò
780     1 ¤È¤¹¤ë¡£ $FREER ¤Ï»²¾È¿ô¤¬ 0 
781     ¤Ë¤Ê¤Ã¤¿ºÝ¤Ë¤½¤Î¥ª¥Ö¥¸¥§¥¯¥È¤ò²òÊü¤¹¤ë¤¿¤á¤ËÍѤ¤¤é¤ì¤ë´Ø¿ô¤Ç¤¢¤ë¡£$FREER
782     ¤¬ NULL¤Ê¤é¤Ð¡¢¥ª¥Ö¥¸¥§¥¯¥È¤Ï´Ø¿ô free () ¤Ë¤è¤Ã¤Æ²òÊü¤µ¤ì¤ë¡£
783
784     ³ä¤êÅö¤Æ¤é¤ì¤¿¥ª¥Ö¥¸¥§¥¯¥ÈËÁƬ¤Î¥Ð¥¤¥È¤Ï¡¢#M17NObjectHead 
785     ¤¬Àê¤á¤ë¡£¤³¤ÎÎΰè¤Ï m17n ¥é¥¤¥Ö¥é¥ê¤¬»ÈÍѤ¹¤ë¤Î¤Ç¡¢¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¥×¥í¥°¥é¥à¤Ï¿¨¤ì¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£
786
787     @return
788     ¤³¤Î´Ø¿ô¤Ï¿·¤·¤¯³ä¤êÅö¤Æ¤é¤ì¤¿¥ª¥Ö¥¸¥§¥¯¥È¤òÊÖ¤¹¡£
789
790     @errors
791     ¤³¤Î´Ø¿ô¤Ï¼ºÇÔ¤·¤Ê¤¤¡£    */
792
793 #if EXAMPLE_CODE
794 typedef struct
795 {
796   M17NObjectHead head;
797   int mem1;
798   char *mem2;
799 } MYStruct;
800
801 void
802 my_freer (void *obj)
803 {
804   free (((MYStruct *) obj)->mem2);
805   free (obj);
806 }
807
808 void
809 my_func (MText *mt, MSymbol key, int num, char *str)
810 {
811   MYStruct *st = m17n_object (sizeof (MYStruct), my_freer);
812
813   st->mem1 = num;
814   st->mem2 = strdup (str);
815   /* KEY must be a managing key.   */
816   mtext_put_prop (mt, 0, mtext_len (mt), key, st);
817   /* This sets the reference count of ST back to 1.  */
818   m17n_object_unref (st);
819 }
820 #endif
821
822 void *
823 m17n_object (int size, void (*freer) (void *))
824 {
825   M17NObject *obj = malloc (size);
826
827   obj->ref_count = 1;
828   obj->ref_count_extended = 0;
829   obj->flag = 0;
830   obj->u.freer = freer;
831   return obj;
832 }
833
834 /*=*/
835
836 /***en
837     @brief Increment the reference count of a managed object.
838
839     The m17n_object_ref () function increments the reference count of
840     the managed object pointed to by $OBJECT.
841
842     @return
843     This function returns the resulting reference count if it fits in
844     a 16-bit unsigned integer (i.e. less than 0x10000).  Otherwise, it
845     return -1.
846
847     @errors
848     This function never fails.  */
849 /***ja
850     @brief ´ÉÍý²¼¥ª¥Ö¥¸¥§¥¯¥È¤Î»²¾È¿ô¤ò 1 Áý¤ä¤¹.
851
852     ´Ø¿ô m17n_object_ref () ¤Ï $OBJECT 
853     ¤Ç»Ø¤µ¤ì¤ë´ÉÍý²¼¥ª¥Ö¥¸¥§¥¯¥È¤Î»²¾È¿ô¤ò 1 Áý¤ä¤¹¡£
854
855     @return 
856     ¤³¤Î´Ø¿ô¤Ï¡¢Áý¤ä¤·¤¿»²¾È¿ô¤¬ 16 ¥Ó¥Ã¥È¤ÎÉä¹æ̵¤·À°¿ôÃÍ(¤¹¤Ê¤ï¤Á 
857     0x10000 Ì¤Ëþ)¤Ë¤ª¤µ¤Þ¤ì¤Ð¡¢¤½¤ì¤òÊÖ¤¹¡£¤½¤¦¤Ç¤Ê¤±¤ì¤Ð -1 ¤òÊÖ¤¹¡£
858
859     @errors
860     ¤³¤Î´Ø¿ô¤Ï¼ºÇÔ¤·¤Ê¤¤¡£    */
861
862 int
863 m17n_object_ref (void *object)
864 {
865   M17NObject *obj = (M17NObject *) object;
866   M17NObjectRecord *record;
867   unsigned *count;
868
869   if (! obj->ref_count_extended)
870     {
871       if (++obj->ref_count)
872         return (int) obj->ref_count;
873       MSTRUCT_MALLOC (record, MERROR_OBJECT);
874       record->freer = obj->u.freer;
875       MLIST_INIT1 (record, counts, 1);
876       MLIST_APPEND1 (record, counts, 0, MERROR_OBJECT);
877       obj->u.record = record;
878       obj->ref_count_extended = 1;
879     }
880   else
881     record = obj->u.record;
882
883   count = record->counts;
884   while (*count == 0xFFFFFFFF)
885     *(count++) = 0;
886   (*count)++;
887   if (*count == 0xFFFFFFFF)
888     MLIST_APPEND1 (record, counts, 0, MERROR_OBJECT);
889   return -1;
890 }
891
892 /*=*/
893
894 /***en
895     @brief Decrement the reference count of a managed object.
896
897     The m17n_object_unref () function decrements the reference count
898     of the managed object pointed to by $OBJECT.  When the reference
899     count becomes zero, the object is freed by its freer function.
900
901     @return
902     This function returns the resulting reference count if it fits in
903     a 16-bit unsigned integer (i.e. less than 0x10000).  Otherwise, it
904     returns -1.  Thus, the return value zero means that $OBJECT is
905     freed.
906
907     @errors
908     This function never fails.  */
909 /***ja
910     @brief ´ÉÍý²¼¥ª¥Ö¥¸¥§¥¯¥È¤Î»²¾È¿ô¤ò 1 ¸º¤é¤¹.
911
912     ´Ø¿ô m17n_object_unref () ¤Ï $OBJECT ¤Ç»Ø¤µ¤ì¤ë´ÉÍý²¼¥ª¥Ö¥¸¥§¥¯¥È¤Î»²¾È¿ô¤ò
913     1 ¸º¤é¤¹¡£»²¾È¿ô¤¬ 0 ¤Ë¤Ê¤ì¤Ð¡¢¥ª¥Ö¥¸¥§¥¯¥È¤Ï²òÊü´Ø¿ô¤Ë¤è¤Ã¤Æ²òÊü¤µ¤ì¤ë¡£
914
915     @return 
916     ¤³¤Î´Ø¿ô¤Ï¡¢¸º¤é¤·¤¿»²¾È¿ô¤¬ 16 ¥Ó¥Ã¥È¤ÎÉä¹æ̵¤·À°¿ôÃÍ(¤¹¤Ê¤ï¤Á 
917     0x10000 Ì¤Ëþ)¤Ë¤ª¤µ¤Þ¤ì¤Ð¡¢¤½¤ì¤òÊÖ¤¹¡£¤½¤¦¤Ç¤Ê¤±¤ì¤Ð -1 
918     ¤òÊÖ¤¹¡£¤Ä¤Þ¤ê¡¢0 ¤¬Ê֤äÆÍ褿¾ì¹ç¤Ï$OBJECT ¤Ï²òÊü¤µ¤ì¤Æ¤¤¤ë¡£
919
920     @errors
921     ¤³¤Î´Ø¿ô¤Ï¼ºÇÔ¤·¤Ê¤¤¡£    */
922 int
923 m17n_object_unref (void *object)
924 {
925   M17NObject *obj = (M17NObject *) object;
926   M17NObjectRecord *record;
927   unsigned *count;
928
929   if (! obj->ref_count_extended)
930     {
931       if (! --obj->ref_count)
932         {
933           if (obj->u.freer)
934             (obj->u.freer) (object);
935           else
936             free (object);
937           return 0;
938         }
939       return (int) obj->ref_count;
940     }
941
942   record = obj->u.record;
943   count = record->counts;
944   while (! *count)
945     *(count++) = 0xFFFFFFFF;
946   (*count)--;
947   if (! record->counts[0])
948     {
949       obj->ref_count_extended = 0;
950       obj->ref_count--;
951       obj->u.freer = record->freer;
952       MLIST_FREE1 (record, counts);
953       free (record);
954     }
955   return -1;
956 }
957
958 /*=*/
959
960 /*** @} */
961
962 /***en
963     @addtogroup m17nError Error Handling
964     @brief Error handling of the m17n library.
965
966     There are two types of errors that may happen in a function of
967     the m17n library.
968
969     The first type is argument errors.  When a library function is
970     called with invalid arguments, it returns a value that indicates
971     error and at the same time sets the external variable #merror_code
972     to a non-zero integer.
973
974     The second type is memory allocation errors.  When the required
975     amount of memory is not available on the system, m17n library
976     functions call a function pointed to by the external variable @c
977     m17n_memory_full_handler.  The default value of the variable is a
978     pointer to the default_error_handle () function, which just calls
979     <tt> exit ()</tt>.  */
980
981 /***ja
982     @addtogroup m17nError ¥¨¥é¡¼½èÍý
983     @brief m17n ¥é¥¤¥Ö¥é¥ê¤Î¥¨¥é¡¼½èÍý.
984
985     m17n ¥é¥¤¥Ö¥é¥ê¤Î´Ø¿ô¤Ç¤Ï¡¢£²¤Ä¤Î¼ïÎà¤Î¥¨¥é¡¼¤¬µ¯¤³¤êÆÀ¤ë¡£
986
987     °ì¤Ä¤Ï°ú¿ô¤Î¥¨¥é¡¼¤Ç¤¢¤ë¡£
988     ¥é¥¤¥Ö¥é¥ê¤Î´Ø¿ô¤¬ÂÅÅö¤Ç¤Ê¤¤°ú¿ô¤È¤È¤â¤Ë¸Æ¤Ð¤ì¤¿¾ì¹ç¡¢¤½¤Î´Ø¿ô¤Ï¥¨¥é¡¼¤ò°ÕÌ£¤¹¤ëÃͤòÊÖ¤·¡¢Æ±»þ¤Ë³°ÉôÊÑ¿ô 
989     #merror_code ¤Ë¥¼¥í¤Ç¤Ê¤¤À°¿ô¤ò¥»¥Ã¥È¤¹¤ë¡£
990
991     ¤â¤¦°ì¤Ä¤Î¼ïÎà¤Ï¥á¥â¥ê³äÅö¤Æ¥¨¥é¡¼¤Ç¤¢¤ë¡£
992     ¥·¥¹¥Æ¥à¤¬É¬ÍפÊÎ̤Υá¥â¥ê¤ò³äÅö¤Æ¤ë¤³¤È¤¬¤Ç¤­¤Ê¤¤¾ì¹ç¡¢¥é¥¤¥Ö¥é¥ê´Ø¿ô¤Ï³°ÉôÊÑ¿ô 
993     @c m17n_memory_full_handler ¤¬»Ø¤¹´Ø¿ô¤ò¸Æ¤Ö¡£¥Ç¥Õ¥©¥ë¥È¤Ç¤Ï¡¢´Ø¿ô 
994     default_error_handle () ¤ò»Ø¤·¤Æ¤ª¤ê¡¢¤³¤Î´Ø¿ô¤Ïñ¤Ë <tt>exit
995     ()</tt> ¤ò¸Æ¤Ö¡£
996 */
997
998 /*** @{ */
999
1000 /*=*/
1001
1002 /***en 
1003     @brief External variable to hold error code of the m17n library.
1004
1005     The external variable #merror_code holds an error code of the
1006     m17n library.  When a library function is called with an invalid
1007     argument, it sets this variable to one of @c enum #MErrorCode.
1008
1009     This variable initially has the value 0.  */
1010
1011 /***ja 
1012     @brief m17n ¥é¥¤¥Ö¥é¥ê¤Î¥¨¥é¡¼¥³¡¼¥É¤òÊÝ»ý¤¹¤ë³°ÉôÊÑ¿ô.
1013
1014     ³°ÉôÊÑ¿ô #merror_code ¤Ï¡¢m17n ¥é¥¤¥Ö¥é¥ê¤Î¥¨¥é¡¼¥³¡¼¥É¤òÊÝ»ý¤¹¤ë¡£
1015     ¥é¥¤¥Ö¥é¥ê´Ø¿ô¤¬ÂÅÅö¤Ç¤Ê¤¤°ú¿ô¤È¤È¤â¤Ë¸Æ¤Ð¤ì¤¿ºÝ¤Ë¤Ï¡¢¤³¤ÎÊÑ¿ô¤ò 
1016     @c enum #MErrorCode ¤Î°ì¤Ä¤Ë¥»¥Ã¥È¤¹¤ë¡£
1017
1018     ¤³¤ÎÊÑ¿ô¤Î½é´üÃͤϠ0 ¤Ç¤¢¤ë¡£  */
1019
1020 int merror_code;
1021
1022 /*=*/
1023
1024 /***en 
1025     @brief Memory allocation error handler.
1026
1027     The external variable #m17n_memory_full_handler holds a pointer
1028     to the function to call when a library function failed to allocate
1029     memory.  $ERR is one of @c enum #MErrorCode indicating in which
1030     function the error occurred.
1031
1032     This variable initially points a function that simply calls the
1033     <tt>exit </tt>() function with $ERR as an argument.
1034
1035     An application program that needs a different error handling can
1036     change this variable to point a proper function.  */
1037
1038 /***ja 
1039     @brief ¥á¥â¥ê³äÅö¤Æ¥¨¥é¡¼¥Ï¥ó¥É¥é.
1040
1041     ÊÑ¿ô #m17n_memory_full_handler 
1042     ¤Ï¡¢¥é¥¤¥Ö¥é¥ê´Ø¿ô¤¬¥á¥â¥ê³äÅö¤Æ¤Ë¼ºÇÔ¤·¤¿ºÝ¤Ë¸Æ¤Ö¤Ù¤­´Ø¿ô¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¡£
1043     $ERR ¤Ï @c enum #MErrorCode 
1044     ¤Î¤¦¤Á¤Î¤¤¤º¤ì¤«¤Ç¤¢¤ê¡¢¤É¤Î¥é¥¤¥Ö¥é¥ê´Ø¿ô¤Ç¥¨¥é¡¼¤¬µ¯¤Ã¤¿¤«¤ò¼¨¤¹¡£
1045
1046     @anchor test
1047
1048     ½é´üÀßÄê¤Ç¤Ï¡¢¤³¤ÎÊÑ¿ô¤Ïñ¤Ë <tt>exit ()</tt> ¤ò $ERR 
1049     ¤ò°ú¿ô¤È¤·¤Æ¸Æ¤Ö´Ø¿ô¤ò»Ø¤·¤Æ¤¤¤ë¡£
1050
1051     ¤³¤ì¤È¤Ï°Û¤Ê¤ë¥¨¥é¡¼½èÍý¤òɬÍפȤ¹¤ë¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ï¡¢¤³¤ÎÊÑ¿ô¤òŬÅö¤Ê´Ø¿ô¤ËÀßÄꤹ¤ë¤³¤È¤Ç¡¢ÌÜŪ¤òãÀ®¤Ç¤­¤ë¡£  */
1052
1053 void (*m17n_memory_full_handler) (enum MErrorCode err);
1054
1055 /*** @} */
1056
1057 /*=*/
1058
1059 /***en
1060     @addtogroup m17nDebug
1061     @brief Support for m17n library users to debug their programs.
1062
1063     The m17n library provides the following facilities to support the
1064     library users to debug their programs.
1065
1066     <ul>
1067
1068     <li> Environment variables to control printing of various
1069     information.
1070
1071     <ul>
1072
1073     <li> MDEBUG_INIT -- If set to 1, print information about the
1074     library initialization on the call of M17N_INIT ().
1075
1076     <li> MDEBUG_FINI -- If set to 1, print counts of objects that are
1077     not yet freed on the call of M17N_FINI ().
1078
1079     <li> MDEBUG_CHARSET -- If set to 1, print information about
1080     charsets being loaded from the m17n database.
1081
1082     <li> MDEBUG_CODING -- If set to 1, print information about coding
1083     systems being loaded from the m17n database.
1084
1085     <li> MDEBUG_DATABASE -- If set to 1, print information about
1086     data being loaded from the m17n database.
1087
1088     <li> MDEBUG_FONT -- If set to 1, print information about fonts
1089     being selected and opened.
1090
1091     <li> MDEBUG_FLT -- If set to 1, 2, or 3, print information about
1092     which command of Font Layout Table are being executed.  The bigger
1093     number prints the more detailed information.
1094
1095     <li> MDEBUG_INPUT -- If set to 1, print information about how an
1096     input method is running.
1097
1098     <li> MDEBUG_ALL -- Setting this variable to 1 is equivalent to
1099     setting all the above variables to 1.
1100
1101     </ul>
1102
1103     <li> Functions to print various objects in a human readable way.
1104     See the documentation of mdebug_dump_XXXX () functions.
1105
1106     <li> The hook function called on an error.  See the documentation
1107     of mdebug_hook ().
1108
1109     </ul>
1110 */
1111 /***ja
1112     @addtogroup m17nDebug
1113     @brief m17n ¥é¥¤¥Ö¥é¥ê¥æ¡¼¥¶¤Î¤¿¤á¤Î¥×¥í¥°¥é¥à¥Ç¥Ð¥Ã¥°¥µ¥Ý¡¼¥È.
1114
1115     m17n ¥é¥¤¥Ö¥é¥ê¤Ï¡¢¤½¤Î¥æ¡¼¥¶¤¬¼«Ê¬¤Î¥×¥í¥°¥é¥à¤ò¥Ç¥Ð¥Ã¥°¤¹¤ë¤¿¤á¤Ë¡¢°Ê²¼¤Îµ¡Ç½¤ò¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤ë¡£
1116
1117     <ul>
1118
1119     <li> ¤µ¤Þ¤¶¤Þ¤Ê¾ðÊó¤Î¥×¥ê¥ó¥È¤òÀ©¸æ¤¹¤ë´Ä¶­ÊÑ¿ô¡£
1120
1121     <ul>
1122
1123     <li> MDEBUG_INIT -- 1 ¤Ê¤é¤Ð¡¢M17N_INIT () 
1124     ¤¬¸Æ¤Ð¤ì¤¿»þÅÀ¤Ç¡¢¥é¥¤¥Ö¥é¥ê¤Î½é´ü²½¤Ë´Ø¤¹¤ë¾ðÊó¤ò¥×¥ê¥ó¥È¤¹¤ë¡£
1125
1126     <li> MDEBUG_FINI -- 1 ¤Ê¤é¤Ð¡¢M17N_FINI () 
1127     ¤¬¸Æ¤Ð¤ì¤¿»þÅÀ¤Ç¡¢¤Þ¤À²òÊü¤µ¤ì¤Æ¤¤¤Ê¤¤¥ª¥Ö¥¸¥§¥¯¥È¤Î»²¾È¿ô¤ò¥×¥ê¥ó¥È¤¹¤ë¡£
1128
1129     <li> MDEBUG_CHARSET -- 1 ¤Ê¤é¤Ð¡¢m17n
1130     ¥Ç¡¼¥¿¥Ù¡¼¥¹¤«¤é¥í¡¼¥É¤µ¤ì¤¿Ê¸»ú¥»¥Ã¥È¤Ë¤Ä¤¤¤Æ¤Î¾ðÊó¤ò¥×¥ê¥ó¥È¤¹¤ë¡£
1131
1132     <li> MDEBUG_CODING --  1 ¤Ê¤é¤Ð¡¢m17n 
1133     ¥Ç¡¼¥¿¥Ù¡¼¥¹¤«¤é¥í¡¼¥É¤µ¤ì¤¿¥³¡¼¥É·Ï¤Ë¤Ä¤¤¤Æ¤Î¾ðÊó¤ò¥×¥ê¥ó¥È¤¹¤ë¡£
1134
1135     <li> MDEBUG_DATABASE -- 1 ¤Ê¤é¤Ð¡¢m17n
1136     ¥Ç¡¼¥¿¥Ù¡¼¥¹¤«¤é¥í¡¼¥É¤µ¤ì¤¿¥Ç¡¼¥¿¤Ë¤Ä¤¤¤Æ¤Î¾ðÊó¤ò¥×¥ê¥ó¥È¤¹¤ë¡£
1137
1138     <li> MDEBUG_FONT -- 1 ¤Ê¤é¤Ð¡¢ÁªÂò¤µ¤ì¤Æ¥ª¡¼¥×¥ó¤µ¤ì¤¿¥Õ¥©¥ó¥È¤Ë¤Ä
1139     ¤¤¤Æ¤Î¾ðÊó¤ò¥×¥ê¥ó¥È¤¹¤ë¡£
1140
1141     <li> MDEBUG_FLT -- 1¡¢2¡¢¤â¤·¤¯¤Ï 3 ¤Ê¤é¤Ð¡¢Font Layout Table ¤Î¤É
1142     ¤Î¥³¥Þ¥ó¥É¤¬¼Â¹ÔÃ椫¤Ë¤Ä¤¤¤Æ¤Î¤ò¥×¥ê¥ó¥È¤¹¤ë¡£¤è¤êÂ礭¤ÊÃÍÄø¤è¤ê¾Ü
1143     ¤·¤¤¾ðÊó¤ò¥×¥ê¥ó¥È¤¹¤ë¡£
1144
1145     <li> MDEBUG_INPUT -- 1 ¤Ê¤é¤Ð¡¢¼Â¹ÔÃæ¤ÎÆþÎϥ᥽¥Ã¥É¤Î¾õÂÖ¤ËÉÕ¤¤¤Æ¤Î
1146     ¾ðÊó¤ò¥×¥ê¥ó¥È¤¹¤ë¡£
1147
1148     <li> MDEBUG_ALL -- 1 ¤Ê¤é¤Ð¡¢¾åµ­¤¹¤Ù¤Æ¤ÎÊÑ¿ô¤ò 1 
1149     ¤Ë¤·¤¿¤Î¤ÈƱ¤¸¸ú²Ì¤ò»ý¤Ä¡£
1150
1151     </ul>
1152
1153     <li> ¼ï¡¹¤Î¥ª¥Ö¥¸¥§¥¯¥È¤ò¿Í´Ö¤Ë²ÄÆɤʷÁ¤Ç¥×¥ê¥ó¥È¤¹¤ë´Ø¿ô¡£¾ÜºÙ¤Ï´Ø¿ô
1154     mdebug_dump_XXXX () ¤ÎÀâÌÀ»²¾È¡£
1155
1156     <li> ¥¨¥é¡¼È¯À¸»þ¤Ë¸Æ¤Ð¤ì¤ë¥Õ¥Ã¥¯´Ø¿ô¡£mdebug_hook () ¤ÎÀâÌÀ»²¾È¡£
1157
1158     </ul>
1159 */
1160
1161 /*=*/
1162 /*** @{ */
1163 /*=*/
1164
1165 /***en
1166     @brief Hook function called on an error.
1167
1168     The mdebug_hook () function is called when an error happens.  It
1169     returns -1 without doing anything.  It is useful to set a break
1170     point on this function in a debugger.  */ 
1171 /***ja
1172     @brief ¥¨¥é¡¼¤ÎºÝ¤Ë¸Æ¤Ð¤ì¤ë¥Õ¥Ã¥¯´Ø¿ô.
1173
1174     ´Ø¿ô mdebug_hook () ¤Ï¥¨¥é¡¼¤¬µ¯¤³¤Ã¤¿ºÝ¤Ë¸Æ¤Ð¤ì¡¢²¿¤â¤»¤º¤Ë-1 
1175     ¤òÊÖ¤¹¡£¥Ç¥Ð¥Ã¥¬Æâ¤Ç¥Ö¥ì¡¼¥¯¥Ý¥¤¥ó¥È¤òÀßÄꤹ¤ë¤¿¤á¤ËÍѤ¤¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
1176     */ 
1177
1178 int
1179 mdebug_hook ()
1180 {
1181   return -1;
1182 }
1183
1184 /*=*/
1185
1186 /*** @} */ 
1187
1188 /*
1189   Local Variables:
1190   coding: euc-japan
1191   End:
1192 */