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