expat_stubs.c 17.4 KB
Newer Older
1
2
3
4
5
6
7
/***********************************************************************/
/* The OcamlExpat library                                              */
/*                                                                     */
/* Copyright 2002, 2003 Maas-Maarten Zeeman. All rights reserved. See  */ 
/* LICENCE for details.                                                */
/***********************************************************************/

8
/* $Id: expat_stubs.c,v 1.6 2003/07/27 12:40:39 cvscast Exp $ */
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

/* Stub code to interface Ocaml with Expat */

#include <stdio.h>
#include <string.h>
#include <expat.h>

#include <caml/mlvalues.h>
#include <caml/custom.h>
#include <caml/alloc.h>
#include <caml/memory.h>
#include <caml/callback.h>
#include <caml/fail.h>

#define XML_Parser_val(v) (*((XML_Parser *) Data_custom_val(v)))

25
26
/* 
 * Define the place where the handlers will be located inside the
27
28
29
 * handler tuple which is registered as global root. Handlers for
 * new functions should go here.
 */
30
#define NUM_HANDLERS 9
31
32
33
34
35
enum expat_handler {
  EXPAT_START_ELEMENT_HANDLER,
  EXPAT_END_ELEMENT_HANDLER,
  EXPAT_CHARACTER_DATA_HANDLER,
  EXPAT_PROCESSING_INSTRUCTION_HANDLER,
36
37
38
  EXPAT_COMMENT_HANDLER,
  EXPAT_START_CDATA_HANDLER,
  EXPAT_END_CDATA_HANDLER,
39
40
  EXPAT_DEFAULT_HANDLER,
  EXPAT_EXTERNAL_ENTITY_REF_HANDLER
41
42
43
44
45
46
47
48
49
50
51
};

static void 
xml_parser_finalize(value parser) 
{ 
  XML_Parser xml_parser = XML_Parser_val(parser);
  value *handlers = XML_GetUserData(xml_parser);

  remove_global_root(handlers);

  XML_ParserFree(xml_parser);  
52
  stat_free(handlers);
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
}

static int 
xml_parser_compare(value v1, value v2) 
{ 
  XML_Parser p1 = XML_Parser_val(v1);
  XML_Parser p2 = XML_Parser_val(v2);
  if(p1 == p2) return 0;
  if(p1 < p2) return -1; 
  return 1;
}

static long 
xml_parser_hash(value v) 
{ 
  return (long) XML_Parser_val(v);
}

static struct custom_operations xml_parser_ops = {
  "Expat_XML_Parser",
  xml_parser_finalize,
  xml_parser_compare,
  xml_parser_hash,
  custom_serialize_default,
  custom_deserialize_default    
};

static XML_Parser
parser_create(char *encoding)
{
  if(encoding[0] == '\0') 
    return XML_ParserCreate(NULL);
  return XML_ParserCreate(encoding);
}

static XML_Parser
parser_create_ns(char *encoding, char sep)
{
  if(encoding[0] == '\0')
    return XML_ParserCreateNS(NULL, sep);
  return XML_ParserCreateNS(encoding, sep);
}

static value
create_ocaml_expat_parser(XML_Parser xml_parser)
{
  CAMLparam0();
100

101
102
103
104
  CAMLlocal1(parser);
  int i;
  value *handlers;

105
106
107
  /* 
   * I don't know how to find out how much memory the parser consumes,
   * so I've set some figures here, which seems to do well.  
108
109
   */
  parser = alloc_custom(&xml_parser_ops, sizeof(XML_Parser), 1, 5000);
110
  XML_Parser_val(parser) = xml_parser;
111
    
112
113
114
  /* 
   * Malloc a value for a tuple which will contain the callback
   * handlers and register it as global root.  
115
116
   */
  handlers = stat_alloc(sizeof *handlers);
117
  *handlers = Val_unit; 
118
  register_global_root(handlers);
119
120
121
122

  /*
   * Create a tuple which will hold the handlers.
   */
123
124
125
126
127
  *handlers = alloc_tuple(NUM_HANDLERS);
  for(i = 0; i < NUM_HANDLERS; i++) {
    Field(*handlers, i) = Val_unit;
  }

128
129
  /* 
   * Associate it as user data with the parser. This is possible because 
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
   * a global root will not be relocated. 
   */
  XML_SetUserData(xml_parser, handlers);
  
  CAMLreturn (parser);
}

CAMLprim value 
expat_XML_ParserCreate(value encoding)
{
  return create_ocaml_expat_parser(parser_create(String_val(encoding)));
}

CAMLprim value
expat_XML_ParserCreateNS(value encoding, value sep)
{ 
  return create_ocaml_expat_parser(parser_create_ns(String_val(encoding),
						    (char) Long_val(sep)));
}

CAMLprim value
expat_XML_GetCurrentByteIndex(value parser)
{
  return Val_long(XML_GetCurrentByteIndex(XML_Parser_val(parser))); 
}

/*
 * Return the current byte count
 */
CAMLprim value
expat_XML_GetCurrentByteCount(value parser)
{
  return Val_long(XML_GetCurrentByteCount(XML_Parser_val(parser))); 
}

/* 
 * Return the current column number
 */
CAMLprim value
expat_XML_GetCurrentColumnNumber(value parser)
{
  return Val_long(XML_GetCurrentColumnNumber(XML_Parser_val(parser)));
}

/* 
 * Returns the current line number
 */
CAMLprim value
expat_XML_GetCurrentLineNumber(value parser)
{
  return Val_long(XML_GetCurrentLineNumber(XML_Parser_val(parser)));
}

/*
 * Return the expat version number as a string
 */
value
expat_XML_ExpatVersion(value unit)
{
  return copy_string(XML_ExpatVersion());
}

192
193
194
195
196
197
198
199
200
201
/*
 * Set the expat parameter for entity parsing
 */
value
expat_XML_SetParamEntityParsing(value parser, value choice) {
  CAMLparam2(parser, choice);
  CAMLreturn (Val_bool(XML_SetParamEntityParsing(XML_Parser_val(parser), 
						 Int_val(choice))));
}

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
/*
 * Return a string with the expat error message.
 */
CAMLprim value
expat_XML_ErrorString(value error_code)
{
  CAMLparam1(error_code);
  const char *error_string = XML_ErrorString(Int_val(error_code));

  /* XML_ErrorString(XML_ERROR_NONE) returns NULL, this check
   * will return an empty string whenever this happens. Note:
   * it checks for NULL, because that is the safest way.
   */
  if (error_string == NULL) 
    CAMLreturn (alloc_string(0));
  CAMLreturn (copy_string(error_string));
}

/* 
 * Raise an expat_error exception
 */
static void
expat_error(int error_code)
{
  static value * expat_error_exn = NULL;

  if(expat_error_exn == NULL) {
    expat_error_exn = caml_named_value("expat_error");
    if(expat_error_exn == NULL) {
      invalid_argument("Exception Expat_error not initialized");
    }
  }

  raise_with_arg(*expat_error_exn, Val_long(error_code));
}

/*
 * The parse call
 */
CAMLprim value
expat_XML_Parse(value parser, value string)
{
  CAMLparam2(parser, string);
  XML_Parser xml_parser =  XML_Parser_val(parser);

  if(!XML_Parse(xml_parser, String_val(string), string_length(string), 0)) {
    expat_error(XML_GetErrorCode(xml_parser));
  }
  
  CAMLreturn (Val_unit);
}

254
255
256
/*
 * A sub-string variant of the parse call
 */
257
CAMLprim value
258
expat_XML_ParseSub(value parser, value string, value pos, value len)
259
{
260
  CAMLparam4(parser, string, pos, len);
261
  XML_Parser xml_parser =  XML_Parser_val(parser);
262
263
264
  
  if(!XML_Parse(xml_parser, 
		String_val(string) + Int_val(pos), Int_val(len), 0)) {
265
266
    expat_error(XML_GetErrorCode(xml_parser));
  }
267

268
269
270
271
272
273
274
275
276
  CAMLreturn (Val_unit);
}

/*
 * The final call
 */
CAMLprim value
expat_XML_Final(value parser)
{
277
  CAMLparam1(parser); 
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
  XML_Parser xml_parser =  XML_Parser_val(parser);

  if(!XML_Parse(xml_parser, NULL, 0, 1)) {
    expat_error(XML_GetErrorCode(xml_parser));
  }
  
  CAMLreturn (Val_unit);
}

/*
 * Start element handling, setting and resetting.
 */
static void 
start_element_handler(void *user_data, const char *name, const char **attr)
{
  CAMLparam0();
  CAMLlocal5(list, cons, prev, att, tag);
  value *handlers = user_data;
  int i;

  list = Val_unit;
  prev = Val_unit;

  /* Create an assoc list with the attributes */
  for(i = 0; attr[i]; i += 2) {
    /* Create a tuple */
    att = alloc_tuple(2);
    Store_field(att, 0, copy_string(attr[i]));
    Store_field(att, 1, copy_string(attr[i + 1]));
    
    /* Create a cons */
    cons = alloc_tuple(2);
    Store_field(cons, 0, att);
    Store_field(cons, 1, Val_unit);
    if(prev != Val_unit) {
      Store_field(prev, 1, cons);      
    } 
    prev = cons;
    if(list == Val_unit) {
      list = cons;
    }
  }
  tag = copy_string(name);
  callback2(Field(*handlers, EXPAT_START_ELEMENT_HANDLER), tag, list);
  
  CAMLreturn0;
}

static value set_start_handler(value parser,
			       XML_StartElementHandler c_handler,
			       value ocaml_handler)
{
  CAMLparam2(parser, ocaml_handler);
  XML_Parser xml_parser = XML_Parser_val(parser);
  value *handlers = XML_GetUserData(xml_parser);

  Store_field(*handlers, EXPAT_START_ELEMENT_HANDLER, ocaml_handler);
  XML_SetStartElementHandler(xml_parser, c_handler); 

  CAMLreturn (Val_unit);
}

CAMLprim value
expat_XML_SetStartElementHandler(value parser, value handler)
{
  CAMLparam2(parser, handler);
  CAMLreturn (set_start_handler(parser, start_element_handler, handler));
}

CAMLprim value
expat_XML_ResetStartElementHandler(value parser)
{
  CAMLparam1(parser);
  CAMLreturn (set_start_handler(parser, NULL, Val_unit));
}

/*
 * End element handling, setting and resetting.
 */
static void
end_element_handler(void *user_data, const char *name)
{
  value tag;
  value *handlers = user_data;

  tag = copy_string(name);
  callback(Field(*handlers, EXPAT_END_ELEMENT_HANDLER), tag);
}

static value 
set_end_handler(value parser, XML_EndElementHandler c_handler, 
		value ocaml_handler)
{
  CAMLparam2(parser, ocaml_handler);
  XML_Parser xml_parser = XML_Parser_val(parser);
  value *handlers = XML_GetUserData(xml_parser);

  Store_field(*handlers, EXPAT_END_ELEMENT_HANDLER, ocaml_handler);
  XML_SetEndElementHandler(xml_parser, c_handler); 

  CAMLreturn (Val_unit);
}

CAMLprim value
expat_XML_SetEndElementHandler(value parser, value handler)
{
  CAMLparam2(parser, handler);
  CAMLreturn (set_end_handler(parser, end_element_handler, handler));
}

CAMLprim value
expat_XML_ResetEndElementHandler(value parser)
{
  CAMLparam1(parser);
  CAMLreturn (set_end_handler(parser, NULL, Val_unit));
}

/*
 * Character data handling, setting, and resetting
 */
static void
character_data_handler(void *user_data, const char *data, int len)
{
  CAMLparam0();
  CAMLlocal1(str);
  value *handlers = user_data;

  str = alloc_string(len);
  memcpy(String_val(str), data, len);
  callback(Field(*handlers, EXPAT_CHARACTER_DATA_HANDLER), str);

  CAMLreturn0;
}

static value set_character_data_handler(value parser,
					XML_CharacterDataHandler c_handler,
					value ocaml_handler)
{
  CAMLparam2(parser, ocaml_handler);
  XML_Parser xml_parser = XML_Parser_val(parser);
  value *handlers = XML_GetUserData(xml_parser);

  Store_field(*handlers, EXPAT_CHARACTER_DATA_HANDLER, ocaml_handler);
  XML_SetCharacterDataHandler(xml_parser, c_handler);

  CAMLreturn (Val_unit);
}

CAMLprim value
expat_XML_SetCharacterDataHandler(value parser, value handler)
{
  CAMLparam2(parser, handler);
  CAMLreturn (set_character_data_handler(parser, character_data_handler, 
					 handler));
}

CAMLprim value
expat_XML_ResetCharacterDataHandler(value parser)
{
  CAMLparam1(parser);
  CAMLreturn (set_character_data_handler(parser, NULL, Val_unit));
}

/*
 * Process instruction, setting and resetting
 */
static void
processing_instruction_handler(void *user_data,  const char *target,  
			       const char *data)
{
  CAMLparam0();
  CAMLlocal2(t, d);
  value *handlers = user_data;

  t = copy_string(target);
  d = copy_string(data);
  callback2(Field(*handlers, EXPAT_PROCESSING_INSTRUCTION_HANDLER), t, d);

  CAMLreturn0;
}

static value 
set_processing_instruction_handler(value parser,
				   XML_ProcessingInstructionHandler c_handler,
				   value ocaml_handler)
{
  CAMLparam2(parser, ocaml_handler);
  XML_Parser xml_parser = XML_Parser_val(parser);
  value *handlers = XML_GetUserData(xml_parser);

  Store_field(*handlers, EXPAT_PROCESSING_INSTRUCTION_HANDLER, ocaml_handler);
  XML_SetProcessingInstructionHandler(xml_parser, c_handler);

  CAMLreturn (Val_unit);
}

CAMLprim value
expat_XML_SetProcessingInstructionHandler(value parser, value handler)
{
  CAMLparam2(parser, handler);
  CAMLreturn (set_processing_instruction_handler(parser, 
					       processing_instruction_handler, 
					       handler));
}

CAMLprim value
expat_XML_ResetProcessingInstructionHandler(value parser)
{
  CAMLparam1(parser);
  CAMLreturn (set_processing_instruction_handler(parser, NULL, Val_unit));
}

/*
 * Comment handler, setting and resetting
 */
static void
comment_handler(void *user_data, const char *data)
{
  CAMLparam0();
  CAMLlocal1(d);

  value *handlers = user_data;
  d = copy_string(data);
  callback(Field(*handlers, EXPAT_COMMENT_HANDLER), d);

  CAMLreturn0;
}

static value 
set_comment_handler(value parser, 
		    XML_CommentHandler c_handler, value ocaml_handler)
{
  CAMLparam2(parser, ocaml_handler);
  XML_Parser xml_parser = XML_Parser_val(parser);
  value *handlers = XML_GetUserData(xml_parser);
  
  Store_field(*handlers, EXPAT_COMMENT_HANDLER, ocaml_handler);
  XML_SetCommentHandler(xml_parser, c_handler);

  CAMLreturn (Val_unit);
}

CAMLprim value
expat_XML_SetCommentHandler(value parser, value handler)
{
  CAMLparam2(parser, handler);
  CAMLreturn (set_comment_handler(parser, comment_handler, handler));
}

CAMLprim value
expat_XML_ResetCommentHandler(value parser)
{
  CAMLparam1(parser);
  CAMLreturn (set_comment_handler(parser, NULL, Val_unit));
}
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

/*
 * Start CData handler, setting and resetting
 */
static void
start_cdata_handler(void *user_data) 
{
  CAMLparam0();
  value *handlers = user_data;
  
  callback(Field(*handlers, EXPAT_START_CDATA_HANDLER), Val_unit);

  CAMLreturn0;
}

static value
set_start_cdata_handler(value parser,
			XML_StartCdataSectionHandler c_handler, 
			value ocaml_handler)
{
  CAMLparam2(parser, ocaml_handler);
  XML_Parser xml_parser = XML_Parser_val(parser);
  value *handlers = XML_GetUserData(xml_parser);
  
  Store_field(*handlers, EXPAT_START_CDATA_HANDLER, ocaml_handler);
  XML_SetStartCdataSectionHandler(xml_parser, c_handler);
  
  CAMLreturn (Val_unit);
}

CAMLprim value
expat_XML_SetStartCDataHandler(value parser, value handler)
{
  CAMLparam2(parser, handler);
  CAMLreturn (set_start_cdata_handler(parser, start_cdata_handler, handler));
}

CAMLprim value
expat_XML_ResetStartCDataHandler(value parser)
{
  CAMLparam1(parser);
  CAMLreturn (set_start_cdata_handler(parser, NULL, Val_unit));
}

/*
 * End CData handler, setting and resetting
 */
static void
end_cdata_handler(void *user_data) 
{
  CAMLparam0();
  value *handlers = user_data;

  callback(Field(*handlers, EXPAT_END_CDATA_HANDLER), Val_unit);

  CAMLreturn0;
}

static value
set_end_cdata_handler(value parser,
		      XML_EndCdataSectionHandler c_handler, 
		      value ocaml_handler)
{
  CAMLparam2(parser, ocaml_handler);
  XML_Parser xml_parser = XML_Parser_val(parser);
  value *handlers = XML_GetUserData(xml_parser);

  Store_field(*handlers, EXPAT_END_CDATA_HANDLER, ocaml_handler);
  XML_SetEndCdataSectionHandler(xml_parser, c_handler);
  
  CAMLreturn (Val_unit);
}

CAMLprim value
expat_XML_SetEndCDataHandler(value parser, value handler)
{
  CAMLparam2(parser, handler);
  CAMLreturn (set_end_cdata_handler(parser, end_cdata_handler, handler));
}

CAMLprim value
expat_XML_ResetEndCDataHandler(value parser)
{
  CAMLparam1(parser);
  CAMLreturn (set_end_cdata_handler(parser, NULL, Val_unit));
}


/*
 * Default handler, setting and resetting
 */
static void
default_handler(void *user_data, const char *data, int len) 
{
  CAMLparam0();
  CAMLlocal1(d);
  value *handlers = user_data;

  d = alloc_string(len);
  memmove(String_val(d), data, len);
  callback(Field(*handlers, EXPAT_DEFAULT_HANDLER), d);

  CAMLreturn0;
}

static value
set_default_handler(value parser,
		    XML_DefaultHandler c_handler, 
		    value ocaml_handler)
{
  CAMLparam2(parser, ocaml_handler);
  XML_Parser xml_parser = XML_Parser_val(parser);
  value *handlers = XML_GetUserData(xml_parser);

  Store_field(*handlers, EXPAT_DEFAULT_HANDLER, ocaml_handler);
  XML_SetDefaultHandler(xml_parser, c_handler);
  
  CAMLreturn (Val_unit);
}

CAMLprim value
expat_XML_SetDefaultHandler(value parser, value handler)
{
  CAMLparam2(parser, handler);
  CAMLreturn (set_default_handler(parser, default_handler, handler));
}

CAMLprim value
expat_XML_ResetDefaultHandler(value parser)
{
  CAMLparam1(parser);
  CAMLreturn (set_default_handler(parser, NULL, Val_unit));
}
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


/* 
 * Return None if a null string is passed as a parameter, and Some str
 * if a string is used.  
 */
static value
Val_option_string(const char *str) {
  CAMLparam0();
  CAMLlocal2(some, some_str);

  if(str == NULL) {
    CAMLreturn (Val_int(0));
  } else {
    some = alloc(1, 1);
    some_str = copy_string(str);
    Store_field(some, 0, some_str);
    CAMLreturn (some);
  }
}

/*
 * External Entity Ref handler, setting and resetting
 */
static int
external_entity_ref_handler(XML_Parser xml_parser, 
			    const char *context, const char *base,
			    const char *systemId, const char *publicId) 
{
  CAMLparam0();
  CAMLlocal4(caml_context, caml_base, caml_systemId, caml_publicId);
  value *handlers = XML_GetUserData(xml_parser);
  value arg[4];

  /* 
   * Now put the strings into ocaml values. The parameters context,
   * base, and publicId are optional systemId is never optional.  
   */
  caml_context = Val_option_string(context);
  caml_base = Val_option_string(base);
  caml_systemId = copy_string(systemId); 
  caml_publicId = Val_option_string(publicId);

  /* Call the callback which has more than 3 parameters */
  arg[0] = caml_context;
  arg[1] = caml_base;
  arg[2] = caml_systemId;
  arg[3] = caml_publicId;  
  callbackN(Field(*handlers, EXPAT_EXTERNAL_ENTITY_REF_HANDLER), 4, arg);

  CAMLreturn (XML_STATUS_OK);
}

static value
set_external_entity_ref_handler(value parser,
				XML_ExternalEntityRefHandler c_handler, 
				value ocaml_handler)
{
  CAMLparam2(parser, ocaml_handler);
  XML_Parser xml_parser = XML_Parser_val(parser);
  value *handlers = XML_GetUserData(xml_parser);

  Store_field(*handlers, EXPAT_EXTERNAL_ENTITY_REF_HANDLER, ocaml_handler);
  XML_SetExternalEntityRefHandler(xml_parser, c_handler);
  
  CAMLreturn (Val_unit);
}

CAMLprim value
expat_XML_SetExternalEntityRefHandler(value parser, value handler)
{
  CAMLparam2(parser, handler);
  CAMLreturn (set_external_entity_ref_handler(parser, 
					      external_entity_ref_handler, 
					      handler));
}

CAMLprim value
expat_XML_ResetExternalEntityRefHandler(value parser)
{
  CAMLparam1(parser);
  CAMLreturn (set_external_entity_ref_handler(parser, NULL, Val_unit));
}