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