File: m17n-core.c

package info (click to toggle)
m17n-lib 1.6.1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,532 kB
  • ctags: 4,388
  • sloc: ansic: 46,837; sh: 11,312; makefile: 646; yacc: 288; xml: 148; sed: 16
file content (1192 lines) | stat: -rw-r--r-- 33,529 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
/* m17n-core.c -- body of the CORE API.
   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
     National Institute of Advanced Industrial Science and Technology (AIST)
     Registration Number H15PRO112

   This file is part of the m17n library.

   The m17n library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public License
   as published by the Free Software Foundation; either version 2.1 of
   the License, or (at your option) any later version.

   The m17n library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the m17n library; if not, write to the Free
   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   02111-1307, USA.  */

/***en
    @addtogroup m17nIntro
    @brief Introduction to the m17n library.

    <em>API LEVELS</em>

    The API of the m17n library is divided into these five.

    <ol>
    <li> CORE API

    It provides basic modules to handle M-texts.  To use this API, an
    application program must include <m17n-core<EM></EM>.h> and be
    linked with -lm17n-core.

    <li> SHELL API

    It provides modules for character properties, character set
    handling, code conversion, etc.  They load various kinds of
    data from the database on demand.  To use this API, an application
    program must include <m17n<EM></EM>.h> and be linked with
    -lm17n-core -lm17n.

    When you use this API, CORE API is also available.

    <li> FLT API

    It provides modules for text shaping using @ref mdbFLT.  To use
    this API, an application program must include <m17n<EM></EM>.h>
    and be linked with -lm17n-core -lm17n-flt.

    When you use this API, CORE API is also available.

    <li> GUI API

    It provides GUI modules such as drawing and inputting M-texts on a
    graphic device.  This API itself is independent of graphic
    devices, but most functions require an argument MFrame that is
    created for a specific type of graphic devices.  The currently
    supported graphic devices are null device, the X Window System,
    and image data (gdImagePtr) of the GD library.

    On a frame of a null device, you cannot draw text nor use input
    methods.  However, functions like mdraw_glyph_list (), etc. are
    available.

    On a frame of the X Window System, you can use the whole GUI API.

    On a frame of the GD library, you can use all drawing API but
    cannot use input methods.

    To use this API, an application program must include
    <m17n-gui<EM></EM>.h> and be linked with -lm17n-core -lm17n
    -lm17n-gui.

    When you use this API, CORE, SHELL, and FLT APIs are also
    available.

    <li> MISC API

    It provides miscellaneous functions to support error handling and
    debugging.  This API cannot be used standalone; it must be used
    with one or more APIs listed above.  To use this API, an
    application program must include <m17n-misc<EM></EM>.h> in
    addition to one of the header files described above.

    </ol>

    See also the section @ref m17n-config "m17n-config(1)".

    <em>ENVIRONMENT VARIABLES</em>

    The m17n library pays attention to the following environment
    variables.

    <ul>
    <li> @c M17NDIR

    The name of the directory that contains data of the m17n database.
    See @ref m17nDatabase for details.

    <li> @c MDEBUG_XXX

    Environment variables whose names start with "MDEBUG_" control
    debug information output.  See @ref m17nDebug for details.

    </ul>

    <em>API NAMING CONVENTION</em>

    The m17n library exports functions, variables, macros, and types.
    All of them start with the letter 'm' or 'M', and are followed by
    an object name (e.g. "symbol", "plist") or a module name
    (e.g. draw, input).  Note that the name of M-text objects start
    with "mtext" and not with "mmtext".

    <ul>

    <li> functions -- mobject () or mobject_xxx ()

    They start with 'm' and are followed by an object name in lower
    case.  Words are separated by '_'.  For example, msymbol (),
    mtext_ref_char (), mdraw_text ().

    <li> non-symbol variables -- mobject, or mobject_xxx
    
    The naming convention is the same as functions (e.g. mface_large).

    <li> symbol variables -- Mname

    Variables of the type MSymbol start with 'M' and are followed by
    their names.  Words are separated by '_'.  For example, Mlanguage
    (the name is "language"), Miso_2022 (the name is "iso-2022").

    <li> macros -- MOBJECT_XXX

    They start with 'M' and are followed by an object name in upper
    case.  Words are separated by '_'.

    <li> types -- MObject or MObjectXxx

    They start with 'M' and are followed by capitalized object names.
    Words are concatenated directly and no '_' are used.  For example,
    MConverter, MInputDriver.

    </ul>

  */

/***ja
    @addtogroup m17nIntro
    @brief m17n 饤֥ ȥ.

    @em APIΥ٥

    m17n 饤֥ API ϰʲΣʬवƤ롣

    <ol>
    <li>  API

    M-text 򰷤δŪʥ⥸塼󶡤롣
     API Ѥ뤿ˤϡץꥱץ
    <m17n-core<EM></EM>.h>  include  -lm17n-core
    ǥ󥯤ʤƤϤʤʤ

    <li>  API

    ʸץѥƥʸѴΤΥ⥸塼󶡤롣
    Υ⥸塼ϡǡ١ɬפ˱¿ͤʥǡɤ롣
     API Ѥ뤿ˤϡץꥱץ
    <m17n<EM></EM>.h>  include  -lm17n-core -lm17n
    ǥ󥯤ʤƤϤʤʤ

     API ѤС API ⼫ưŪ˻ѲǽȤʤ롣

    <li> FLT API

    ʸɽ @ref mdbFLT Ѥ⥸塼󶡤롣 API
    Ѥ뤿ˤϡץꥱץ <m17n<EM></EM>.h> 
     include  -lm17n-core -lm17n-flt ǥ󥯤ʤƤϤʤʤ

     API ѤС API ⼫ưŪ˻ѲǽȤʤ롣

    <li> GUI API

    եåǥХ M-text ɽϤꤹ뤿
    GUI ⥸塼󶡤롣 API
    ΤϥեåǥХȤΩǤ뤬
    ¿δؿΥեåǥХѤ˺줿 
    MFrame ˼롣
    ǥݡȤƤ륰եåǥХϡ̥ǥХX
    ɥƥࡢ GD 饤֥Υ᡼ǡ
    (gdImagePtr) Ǥ롣

    ̥ǥХΥե졼ǤɽϤǤʤ
    mdraw_glyph_list () ʤɤδؿϻѲǽǤ롣

    X ɥƥΥե졼ǤϤ٤Ƥ GUI API ѤǤ롣

    GD 饤֥Υե졼ǤϡѤ API
    Ϥ٤ƻѤǤ뤬ϤϤǤʤ

     API Ѥ뤿ˤϡץꥱץ
    <m17n-gui<EM></EM>.h>  include -lm17n-core -lm17n -lm17n-gui
    ǥ󥯤ʤƤϤʤʤ

     API ѤС API API FLT API
    ⼫ưŪ˻ѲǽȤʤ롣

    <li> ¾ API

    顼ǥХåѤΤ¾δؿ󶡤롣 API
    ϤǤϻѤǤ嵭¾ API
    ȶ˻ȤѤ뤿ˤϡ嵭Τ줫include
    ե˲äơ <m17n-misc<EM></EM>.h> include
    ʤƤϤʤʤ

    </ol>

    @ref m17n-config "m17n-config(1)" ⻲ȡ

    @em Ķѿ

    m17n 饤֥ϰʲδĶѿ򻲾Ȥ롣

    <ul>
    <li> @c M17NDIR

    m17n ǡ١ΥǡǼǥ쥯ȥ̾ܺ٤ @ref
    m17nDatabase ȡ

    <li> @c MDEBUG_XXX

    "MDEBUG_" ǻϤޤ̾ĴĶѿϥǥХåνϤ椹롣
    ܺ٤ @ref m17nDebug ȡ

    </ul>

    @em API @em ̿̾§

    m17n 饤֥ϡؿѿޥ export 롣 'm' 
    ޤ 'M' ΤȤ˥֥̾ ("symbol""plist" ʤ)
    ޤϥ⥸塼̾ (draw, input ʤ) ³ΤǤ롣
    M-text ֥Ȥ̾ "mmtext" ǤϤʤ "mtext"
    ǻϤޤ뤳Ȥա
    
    <ul>

    <li> ؿ -- mobject () ޤ mobject_xxx ()

    'm' ΤȤ˾ʸǥ֥̾³ñ֤ '_'
    Ƕڤ롣ȤСmsymbol (),
     mtext_ref_char (), mdraw_text () ʤɡ

    <li> ܥǤʤѿ -- mobject,  ޤ mobject_xxx
    
    ؿƱ̿̾§˽Ȥ  mface_large ʤɡ

    <li> ܥѿ -- Mname

    MSymbol ѿϡ'M' θ̾³ñ֤ '_'
    Ƕڤ롣Ȥ Mlanguage (̾ "language"), Miso_2022
    (̾"iso-2022") ʤɡ

    <li> ޥ -- MOBJECT_XXX

    'M' θʸǥ֥̾³ñ֤ '_' Ƕڤ롣

    <li>  -- MObject ޤ MObjectXxx

    'M' θʸǻϤޤ륪֥̾³ñϢ³ƽ񤫤졢
    '_' ѤʤȤ MConverter, MInputDriver ʤɡ

    </ul>
    
    */
/*=*/
/*** @{ */
#ifdef FOR_DOXYGEN
/***en
    The #M17NLIB_MAJOR_VERSION macro gives the major version number
    of the m17n library.  */
/***ja
    ޥ #M17NLIB_MAJOR_VERSION  m17n 
    饤֥Υ᥸㡼СֹͿ.  */

#define M17NLIB_MAJOR_VERSION

/*=*/

/***en
    The #M17NLIB_MINOR_VERSION macro gives the minor version number
    of the m17n library.  */

/***ja
    ޥ #M17NLIB_MINOR_VERSION  m17n 
    饤֥ΥޥʡСֹͿ.  */

#define M17NLIB_MINOR_VERSION

/*=*/

/***en
    The #M17NLIB_PATCH_LEVEL macro gives the patch level number
    of the m17n library.  */

/***ja
    ޥ #M17NLIB_PATCH_LEVEL  m17n 
    饤֥Υѥå٥ֹͿ.  */

#define M17NLIB_PATCH_LEVEL

/*=*/

/***en
    The #M17NLIB_VERSION_NAME macro gives the version name of the
    m17n library as a string.  */

/***ja
    ޥ #M17NLIB_VERSION_NAME  m17n 
    饤֥ΥС̾ʸȤͿ.  */

#define M17NLIB_VERSION_NAME

/*=*/

/***en
    @brief Initialize the m17n library.

    The macro M17N_INIT () initializes the m17n library.  This macro
    must be called before any m17n functions are used.

    It is safe to call this macro multiple times, but in that case,
    the macro M17N_FINI () must be called the same times to free the
    memory.

    If the initialization was successful, the external variable
    #merror_code is set to 0.  Otherwise it is set to -1.

    @seealso
    M17N_FINI (), m17n_status ()  */

/***ja
    @brief m17n 饤֥.

    ޥ M17N_INIT ()  m17n 饤֥롣m17n 
    δؿѤˡΥޥޤƤФʤƤϤʤʤ
    
    ΥޥʣƤǤǤ뤬ξ뤿˥ޥ
    M17N_FINI () ƱƤɬפ롣

    ѿ #merror_code ϡ 0 ˡǤʤ 
    -1 ꤵ롣  

    @seealso
    M17N_FINI (), m17n_status ()  */

#define M17N_INIT()

/*=*/

/***en
    @brief Finalize the m17n library.

    The macro M17N_FINI () finalizes the m17n library.  It frees all the
    memory area used by the m17n library.  Once this macro is
    called, no m17n functions should be used until the
    macro M17N_INIT () is called again.

    If the macro M17N_INIT () was called N times, the Nth call of this
    macro actually free the memory. 

    @seealso
    M17N_INIT (), m17n_status ()  */
/***ja
    @brief m17n 饤֥λ. 

    ޥ M17N_FINI ()  m17n 饤֥λ롣m17n 
    饤֥꤬ȤäƤΥΰϲ롣٤ΥޥƤФ줿顢ޥ
    M17N_INIT () ٸƤФޤ m17n ؿϻȤ٤Ǥʤ

    ޥ M17N_INIT ()  N ƤФƤˤϡΥޥ N 
    ƤФƽƥ꤬롣

    @seealso
    M17N_INIT (), m17n_status ()  */

#define M17N_FINI()
#endif /* FOR_DOXYGEN */
/*=*/
/*** @} */ 
/*=*/

#if !defined (FOR_DOXYGEN) || defined (DOXYGEN_INTERNAL_MODULE)
/*** @addtogroup m17nInternal
     @{ */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>

#include "m17n-core.h"
#include "m17n-misc.h"
#include "internal.h"
#include "symbol.h"

static void
default_error_handler (enum MErrorCode err)
{
  exit (err);
}

static struct timeval time_stack[16];
static int time_stack_index;

static M17NObjectArray *object_array_root;

static void
report_object_array ()
{
  fprintf (stderr, "%16s %7s %7s %7s\n",
	   "object", "created", "freed", "alive");
  fprintf (stderr, "%16s %7s %7s %7s\n",
	   "------", "-------", "-----", "-----");
  for (; object_array_root; object_array_root = object_array_root->next)
    {
      M17NObjectArray *array = object_array_root;

      fprintf (stderr, "%16s %7d %7d %7d\n", array->name,
	       array->used, array->used - array->count, array->count);
      if (array->count > 0)
	{
	  int i;
	  for (i = 0; i < array->used && ! array->objects[i]; i++);

	  if (strcmp (array->name, "M-text") == 0)
	    {
	      MText *mt = (MText *) array->objects[i];

	      if (mt->format <= MTEXT_FORMAT_UTF_8)
		fprintf (stderr, "\t\"%s\"\n", (char *) mt->data);
	    }
	  else if (strcmp (array->name, "Plist") == 0)
	    {
	      MPlist *plist = (MPlist *) array->objects[i];

	      mdebug_dump_plist (plist, 8);
	      fprintf (stderr, "\n");
	    }
	}

      if (array->used > 0)
	{
	  free (array->objects);
	  array->count = array->used = 0;
	}
    }
}



/* Internal API */

int m17n__core_initialized;
int m17n__shell_initialized;
int m17n__gui_initialized;

int mdebug__flags[MDEBUG_MAX];
FILE *mdebug__output;

void
mdebug__push_time ()
{
  struct timezone tz;

  gettimeofday (time_stack + time_stack_index++, &tz);
}

void
mdebug__pop_time ()
{
  time_stack_index--;
}

void
mdebug__print_time ()
{
  struct timeval tv;
  struct timezone tz;
  long diff;

  gettimeofday (&tv, &tz);
  diff = ((tv.tv_sec - time_stack[time_stack_index - 1].tv_sec) * 1000000
	  + (tv.tv_usec - time_stack[time_stack_index - 1].tv_usec));
  fprintf (stderr, "%8ld ms.", diff);
  time_stack[time_stack_index - 1] = tv;
}

static void
SET_DEBUG_FLAG (char *env_name, enum MDebugFlag flag)
{
  char *env_value = getenv (env_name);

  if (env_value)
    {
      int int_value = atoi (env_value);

      if (flag == MDEBUG_ALL)
	{
	  int i;
	  for (i = 0; i < MDEBUG_MAX; i++)
	    mdebug__flags[i] = int_value;
	}
      else
	mdebug__flags[flag] = int_value;
    }
}

void
mdebug__add_object_array (M17NObjectArray *array, char *name)
{
  array->name = name;
  array->count = 0;
  array->next = object_array_root;
  object_array_root = array;
}


void
mdebug__register_object (M17NObjectArray *array, void *object)
{
  if (array->used == 0)
    MLIST_INIT1 (array, objects, 256);
  array->count++;
  MLIST_APPEND1 (array, objects, object, MERROR_OBJECT);
}

void
mdebug__unregister_object (M17NObjectArray *array, void *object)
{
  array->count--;
  if (array->count >= 0)
    {
      int i;

      for (i = array->used - 1; i >= 0 && array->objects[i] != object; i--);
      if (i >= 0)
	{
	  if (i == array->used - 1)
	    array->used--;
	  array->objects[i] = NULL;
	}
      else
	mdebug_hook ();
    }
  else									\
    mdebug_hook ();
}


/* External API */

/* The following two are actually not exposed to a user but concealed
   by the macro M17N_INIT (). */

void
m17n_init_core (void)
{
  int mdebug_flag = MDEBUG_INIT;

  merror_code = MERROR_NONE;
  if (m17n__core_initialized++)
    return;

  m17n_memory_full_handler = default_error_handler;

  SET_DEBUG_FLAG ("MDEBUG_ALL", MDEBUG_ALL);
  SET_DEBUG_FLAG ("MDEBUG_INIT", MDEBUG_INIT);
  SET_DEBUG_FLAG ("MDEBUG_FINI", MDEBUG_FINI);
  SET_DEBUG_FLAG ("MDEBUG_CHARSET", MDEBUG_CHARSET);
  SET_DEBUG_FLAG ("MDEBUG_CODING", MDEBUG_CODING);
  SET_DEBUG_FLAG ("MDEBUG_DATABASE", MDEBUG_DATABASE);
  SET_DEBUG_FLAG ("MDEBUG_FONT", MDEBUG_FONT); 
  SET_DEBUG_FLAG ("MDEBUG_FLT", MDEBUG_FLT);
  SET_DEBUG_FLAG ("MDEBUG_FONTSET", MDEBUG_FONTSET);
  SET_DEBUG_FLAG ("MDEBUG_INPUT", MDEBUG_INPUT);
  /* for backward compatibility... */
  SET_DEBUG_FLAG ("MDEBUG_FONT_FLT", MDEBUG_FLT);
  SET_DEBUG_FLAG ("MDEBUG_FONT_OTF", MDEBUG_FLT);
  {
    char *env_value = getenv ("MDEBUG_OUTPUT_FILE");

    mdebug__output = NULL;
    if (env_value)
      {
	if (strcmp (env_value, "stdout"))
	  mdebug__output = stdout;
	else
	  mdebug__output = fopen (env_value, "a");
      }
    if (! mdebug__output)
      mdebug__output = stderr;
  }

  MDEBUG_PUSH_TIME ();
  MDEBUG_PUSH_TIME ();
  if (msymbol__init () < 0)
    goto err;
  MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize symbol module."));
  if  (mplist__init () < 0)
    goto err;
  MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize plist module."));
  if (mchar__init () < 0)
    goto err;
  MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize character module."));
  if  (mchartable__init () < 0)
    goto err;
  MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize chartable module."));
  if (mtext__init () < 0 || mtext__prop_init () < 0)
    goto err;
  MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize mtext module."));
  if (mdatabase__init () < 0)
    goto err;
  MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize database module."));

#if ENABLE_NLS
  bindtextdomain ("m17n-lib", GETTEXTDIR);
  bindtextdomain ("m17n-db", GETTEXTDIR);
  bindtextdomain ("m17n-contrib", GETTEXTDIR);
  bind_textdomain_codeset ("m17n-lib", "UTF-8");
  bind_textdomain_codeset ("m17n-db", "UTF-8");
  bind_textdomain_codeset ("m17n-contrib", "UTF-8");
#endif

 err:
  MDEBUG_POP_TIME ();
  MDEBUG_PRINT_TIME ("INIT", (stderr, " to initialize the core modules."));
  MDEBUG_POP_TIME ();
}

void
m17n_fini_core (void)
{
  int mdebug_flag = MDEBUG_FINI;

  if (m17n__core_initialized == 0
      || --m17n__core_initialized > 0)
    return;

  MDEBUG_PUSH_TIME ();
  MDEBUG_PUSH_TIME ();
  mchartable__fini ();
  MDEBUG_PRINT_TIME ("FINI", (stderr, " to finalize chartable module."));
  mtext__fini ();
  MDEBUG_PRINT_TIME ("FINI", (stderr, " to finalize mtext module."));
  msymbol__fini ();
  MDEBUG_PRINT_TIME ("FINI", (stderr, " to finalize symbol module."));
  mplist__fini ();
  MDEBUG_PRINT_TIME ("FINI", (stderr, " to finalize plist module."));
  /* We must call this after the aboves because it frees interval
     pools.  */
  mtext__prop_fini ();
  MDEBUG_PRINT_TIME ("FINI", (stderr, " to finalize textprop module."));
  MDEBUG_POP_TIME ();
  MDEBUG_PRINT_TIME ("FINI", (stderr, " to finalize the core modules."));
  MDEBUG_POP_TIME ();
  if (mdebug__flags[MDEBUG_FINI])
    report_object_array ();
  msymbol__free_table ();
  if (mdebug__output != stderr)
    fclose (mdebug__output);
}

/*** @} */
#endif /* !FOR_DOXYGEN || DOXYGEN_INTERNAL_MODULE */
/*=*/

/*** @addtogroup m17nIntro */

/*** @{  */
/*=*/

/***en
    @brief Report which part of the m17n library is initialized.

    The m17n_status () function returns one of these values depending
    on which part of the m17n library is initialized:

	#M17N_NOT_INITIALIZED, #M17N_CORE_INITIALIZED,
	#M17N_SHELL_INITIALIZED, #M17N_GUI_INITIALIZED  */

/***ja
    @brief m17n 饤֥Τɤʬ줿𤹤.

    ؿ m17n_status ()  
    m17n 饤֥Τɤʬ줿˱ơʲͤΤ줫֤

	#M17N_NOT_INITIALIZED, #M17N_CORE_INITIALIZED,
	#M17N_SHELL_INITIALIZED, #M17N_GUI_INITIALIZED  */

enum M17NStatus
m17n_status (void)
{
  return (m17n__gui_initialized ? M17N_GUI_INITIALIZED
	  : m17n__shell_initialized ? M17N_SHELL_INITIALIZED
	  : m17n__core_initialized ? M17N_CORE_INITIALIZED
	  : M17N_NOT_INITIALIZED);
}

/*** @} */

/*=*/
/***en
    @addtogroup m17nObject
    @brief Managed objects are objects managed by the reference count.

    There are some types of m17n objects that are managed by their
    reference count.  Those objects are called @e managed @e objects.
    When created, the reference count of a managed object is
    initialized to one.  The m17n_object_ref () function increments
    the reference count of a managed object by one, and the
    m17n_object_unref () function decrements by one.  A managed
    object is automatically freed when its reference count becomes
    zero.

    A property whose key is a managing key can have only a managed
    object as its value.  Some functions, for instance msymbol_put ()
    and mplist_put (), pay special attention to such a property.

    In addition to the predefined managed object types, users can
    define their own managed object types.  See the documentation of
    the m17n_object () for more details.  */
/***ja
    @addtogroup m17nObject
    @brief ֥ȤȤϻȿˤäƴƤ륪֥ȤǤ.

    m17n ֥ȤΤ뷿ΤΤϡȿˤäƴƤ롣
    Υ֥Ȥ @e ֥ ȸƤФ롣줿Ǥλȿ
    1 ˽Ƥ롣ؿ m17n_object_ref () ϴ֥Ȥλȿ
    1 䤷ؿm17n_object_unref ()  1 餹ȿ
    0 ˤʤä֥ȤϼưŪ˲롣

    ǤץѥƥϡͤȤƴ֥Ȥ롣
    ؿ msymbol_put ()  mplist_put () ʤɤϤΥץѥƥ̰롣

    Ѥߴ֥ȥפ¾ˡ桼ɬפʴ֥ȥפʬ뤳ȤǤ롣ܺ٤
    m17n_object () 򻲾ȡ  */

/*** @{  */
/*=*/
/***en
    @brief Allocate a managed object.

    The m17n_object () function allocates a new managed object of
    $SIZE bytes and sets its reference count to 1.  $FREER is the
    function that is used to free the object when the reference count
    becomes 0.  If $FREER is NULL, the object is freed by the free ()
    function.

    The heading bytes of the allocated object is occupied by
    #M17NObjectHead.  That area is reserved for the m17n library and
    application programs should never touch it.

    @return
    This function returns a newly allocated object.

    @errors
    This function never fails.  */

/***ja
    @brief ֥ȤƤ.

    ؿ m17n_object () $SIZE ХȤο֥Ȥơλȿ
    1 Ȥ롣 $FREER ϻȿ 0 
    ˤʤäݤˤΥ֥Ȥ뤿ѤؿǤ롣$FREER
     NULLʤС֥Ȥϴؿ free () ˤäƲ롣

    Ƥ줿֥ƬΥХȤϡ#M17NObjectHead 
    롣ΰ m17n 饤֥꤬ѤΤǡץꥱץϿƤϤʤʤ

    @return
    δؿϿƤ줿֥Ȥ֤

    @errors
    δؿϼԤʤ    */

#if EXAMPLE_CODE
typedef struct
{
  M17NObjectHead head;
  int mem1;
  char *mem2;
} MYStruct;

void
my_freer (void *obj)
{
  free (((MYStruct *) obj)->mem2);
  free (obj);
}

void
my_func (MText *mt, MSymbol key, int num, char *str)
{
  MYStruct *st = m17n_object (sizeof (MYStruct), my_freer);

  st->mem1 = num;
  st->mem2 = strdup (str);
  /* KEY must be a managing key.   */
  mtext_put_prop (mt, 0, mtext_len (mt), key, st);
  /* This sets the reference count of ST back to 1.  */
  m17n_object_unref (st);
}
#endif

void *
m17n_object (int size, void (*freer) (void *))
{
  M17NObject *obj = malloc (size);

  obj->ref_count = 1;
  obj->ref_count_extended = 0;
  obj->flag = 0;
  obj->u.freer = freer;
  return obj;
}

/*=*/

/***en
    @brief Increment the reference count of a managed object.

    The m17n_object_ref () function increments the reference count of
    the managed object pointed to by $OBJECT.

    @return
    This function returns the resulting reference count if it fits in
    a 16-bit unsigned integer (i.e. less than 0x10000).  Otherwise, it
    return -1.

    @errors
    This function never fails.  */
/***ja
    @brief ֥Ȥλȿ 1 䤹.

    ؿ m17n_object_ref ()  $OBJECT 
    ǻؤ֥Ȥλȿ 1 䤹

    @return 
    δؿϡ䤷ȿ 16 ӥåȤ̵(ʤ 
    0x10000 ̤)ˤޤС֤Ǥʤ -1 ֤

    @errors
    δؿϼԤʤ    */

int
m17n_object_ref (void *object)
{
  M17NObject *obj = (M17NObject *) object;
  M17NObjectRecord *record;
  unsigned *count;

  if (! obj->ref_count_extended)
    {
      if (++obj->ref_count)
	return (int) obj->ref_count;
      MSTRUCT_MALLOC (record, MERROR_OBJECT);
      record->freer = obj->u.freer;
      MLIST_INIT1 (record, counts, 1);
      MLIST_APPEND1 (record, counts, 0, MERROR_OBJECT);
      obj->u.record = record;
      obj->ref_count_extended = 1;
    }
  else
    record = obj->u.record;

  count = record->counts;
  while (*count == 0xFFFFFFFF)
    *(count++) = 0;
  (*count)++;
  if (*count == 0xFFFFFFFF)
    MLIST_APPEND1 (record, counts, 0, MERROR_OBJECT);
  return -1;
}

/*=*/

/***en
    @brief Decrement the reference count of a managed object.

    The m17n_object_unref () function decrements the reference count
    of the managed object pointed to by $OBJECT.  When the reference
    count becomes zero, the object is freed by its freer function.

    @return
    This function returns the resulting reference count if it fits in
    a 16-bit unsigned integer (i.e. less than 0x10000).  Otherwise, it
    returns -1.  Thus, the return value zero means that $OBJECT is
    freed.

    @errors
    This function never fails.  */
/***ja
    @brief ֥Ȥλȿ 1 餹.

    ؿ m17n_object_unref ()  $OBJECT ǻؤ֥Ȥλȿ
    1 餹ȿ 0 ˤʤС֥ȤϲؿˤäƲ롣

    @return 
    δؿϡ餷ȿ 16 ӥåȤ̵(ʤ 
    0x10000 ̤)ˤޤС֤Ǥʤ -1 
    ֤Ĥޤꡢ0 ֤ä褿$OBJECT ϲƤ롣

    @errors
    δؿϼԤʤ    */
int
m17n_object_unref (void *object)
{
  M17NObject *obj = (M17NObject *) object;
  M17NObjectRecord *record;
  unsigned *count;

  if (! obj->ref_count_extended)
    {
      if (! --obj->ref_count)
	{
	  if (obj->u.freer)
	    (obj->u.freer) (object);
	  else
	    free (object);
	  return 0;
	}
      return (int) obj->ref_count;
    }

  record = obj->u.record;
  count = record->counts;
  while (! *count)
    *(count++) = 0xFFFFFFFF;
  (*count)--;
  if (! record->counts[0])
    {
      obj->ref_count_extended = 0;
      obj->ref_count--;
      obj->u.freer = record->freer;
      MLIST_FREE1 (record, counts);
      free (record);
    }
  return -1;
}

/*=*/

/*** @} */

/***en
    @addtogroup m17nError Error Handling
    @brief Error handling of the m17n library.

    There are two types of errors that may happen in a function of
    the m17n library.

    The first type is argument errors.  When a library function is
    called with invalid arguments, it returns a value that indicates
    error and at the same time sets the external variable #merror_code
    to a non-zero integer.

    The second type is memory allocation errors.  When the required
    amount of memory is not available on the system, m17n library
    functions call a function pointed to by the external variable @c
    m17n_memory_full_handler.  The default value of the variable is a
    pointer to the default_error_handle () function, which just calls
    <tt> exit ()</tt>.  */

/***ja
    @addtogroup m17nError 顼
    @brief m17n 饤֥Υ顼.

    m17n 饤֥δؿǤϡĤμΥ顼롣

    ĤϰΥ顼Ǥ롣
    饤֥δؿǤʤȤȤ˸ƤФ줿硢δؿϥ顼̣֤ͤƱ˳ѿ 
    #merror_code ˥Ǥʤ򥻥åȤ롣

    ⤦Ĥμϥƥ顼Ǥ롣
    ƥबɬפ̤ΥƤ뤳ȤǤʤ硢饤֥ؿϳѿ 
    @c m17n_memory_full_handler ؤؿƤ֡ǥեȤǤϡؿ 
    default_error_handle () ؤƤꡢδؿñ <tt>exit
    ()</tt> Ƥ֡
*/

/*** @{ */

/*=*/

/***en 
    @brief External variable to hold error code of the m17n library.

    The external variable #merror_code holds an error code of the
    m17n library.  When a library function is called with an invalid
    argument, it sets this variable to one of @c enum #MErrorCode.

    This variable initially has the value 0.  */

/***ja 
    @brief m17n 饤֥Υ顼ɤݻ볰ѿ.

    ѿ #merror_code ϡm17n 饤֥Υ顼ɤݻ롣
    饤֥ؿǤʤȤȤ˸ƤФ줿ݤˤϡѿ 
    @c enum #MErrorCode ΰĤ˥åȤ롣

    ѿνͤ 0 Ǥ롣  */

int merror_code;

/*=*/

/***en 
    @brief Memory allocation error handler.

    The external variable #m17n_memory_full_handler holds a pointer
    to the function to call when a library function failed to allocate
    memory.  $ERR is one of @c enum #MErrorCode indicating in which
    function the error occurred.

    This variable initially points a function that simply calls the
    <tt>exit </tt>() function with $ERR as an argument.

    An application program that needs a different error handling can
    change this variable to point a proper function.  */

/***ja 
    @brief ƥ顼ϥɥ.

    ѿ #m17n_memory_full_handler 
    ϡ饤֥ؿƤ˼Ԥݤ˸Ƥ֤٤ؿؤΥݥ󥿤Ǥ롣
    $ERR  @c enum #MErrorCode 
    ΤΤ줫ǤꡢɤΥ饤֥ؿǥ顼ä򼨤

    @anchor test

    Ǥϡѿñ <tt>exit ()</tt>  $ERR 
    ȤƸƤִؿؤƤ롣

    Ȥϰۤʤ륨顼ɬפȤ륢ץꥱϡѿŬʴؿꤹ뤳ȤǡŪãǤ롣  */

void (*m17n_memory_full_handler) (enum MErrorCode err);

/*** @} */

/*=*/

/***en
    @addtogroup m17nDebug
    @brief Support for m17n library users to debug their programs.

    The m17n library provides the following facilities to support the
    library users to debug their programs.

    <ul>

    <li> Environment variables to control printing of various
    information.

    <ul>

    <li> MDEBUG_INIT -- If set to 1, print information about the
    library initialization on the call of M17N_INIT ().

    <li> MDEBUG_FINI -- If set to 1, print counts of objects that are
    not yet freed on the call of M17N_FINI ().

    <li> MDEBUG_CHARSET -- If set to 1, print information about
    charsets being loaded from the m17n database.

    <li> MDEBUG_CODING -- If set to 1, print information about coding
    systems being loaded from the m17n database.

    <li> MDEBUG_DATABASE -- If set to 1, print information about
    data being loaded from the m17n database.

    <li> MDEBUG_FONT -- If set to 1, print information about fonts
    being selected and opened.

    <li> MDEBUG_FLT -- If set to 1, 2, or 3, print information about
    which command of Font Layout Table are being executed.  The bigger
    number prints the more detailed information.

    <li> MDEBUG_INPUT -- If set to 1, print information about how an
    input method is running.

    <li> MDEBUG_ALL -- Setting this variable to 1 is equivalent to
    setting all the above variables to 1.

    </ul>

    <li> Functions to print various objects in a human readable way.
    See the documentation of mdebug_dump_XXXX () functions.

    <li> The hook function called on an error.  See the documentation
    of mdebug_hook ().

    </ul>
*/
/***ja
    @addtogroup m17nDebug
    @brief m17n 饤֥桼ΤΥץǥХåݡ.

    m17n 饤֥ϡΥ桼ʬΥץǥХå뤿ˡʲεǽ򥵥ݡȤƤ롣

    <ul>

    <li> ޤޤʾΥץȤ椹Ķѿ

    <ul>

    <li> MDEBUG_INIT -- 1 ʤСM17N_INIT () 
    ƤФ줿ǡ饤֥ν˴ؤץȤ롣

    <li> MDEBUG_FINI -- 1 ʤСM17N_FINI () 
    ƤФ줿ǡޤƤʤ֥ȤλȿץȤ롣

    <li> MDEBUG_CHARSET -- 1 ʤСm17n
    ǡ١ɤ줿ʸåȤˤĤƤξץȤ롣

    <li> MDEBUG_CODING --  1 ʤСm17n 
    ǡ١ɤ줿ɷϤˤĤƤξץȤ롣

    <li> MDEBUG_DATABASE -- 1 ʤСm17n
    ǡ١ɤ줿ǡˤĤƤξץȤ롣

    <li> MDEBUG_FONT -- 1 ʤС򤵤ƥץ󤵤줿եȤˤ
    ƤξץȤ롣

    <li> MDEBUG_FLT -- 12⤷ 3 ʤСFont Layout Table Τ
    Υޥɤ¹椫ˤĤƤΤץȤ롣礭
    ץȤ롣

    <li> MDEBUG_INPUT -- 1 ʤС¹ϥ᥽åɤξ֤դƤ
    ץȤ롣

    <li> MDEBUG_ALL -- 1 ʤС嵭٤Ƥѿ 1 
    ˤΤƱ̤ġ

    </ul>

    <li> Υ֥Ȥʹ֤˲ɤʷǥץȤؿܺ٤ϴؿ
    mdebug_dump_XXXX () ȡ

    <li> 顼ȯ˸ƤФեåؿmdebug_hook () ȡ

    </ul>
*/

/*=*/
/*** @{ */
/*=*/

/***en
    @brief Hook function called on an error.

    The mdebug_hook () function is called when an error happens.  It
    returns -1 without doing anything.  It is useful to set a break
    point on this function in a debugger.  */ 
/***ja
    @brief 顼κݤ˸ƤФեåؿ.

    ؿ mdebug_hook () ϥ顼äݤ˸ƤФ졢⤻-1 
    ֤ǥХåǥ֥졼ݥȤꤹ뤿Ѥ뤳ȤǤ롣
    */ 

int
mdebug_hook ()
{
  return -1;
}

/*=*/

/*** @} */ 

/*
  Local Variables:
  coding: euc-japan
  End:
*/