summaryrefslogtreecommitdiff
path: root/ext/xslt/sablot.c
blob: 340e3d39f4a116cd28b161067e07d05b3fa9cc61 (plain)
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
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
/*
   +----------------------------------------------------------------------+
   | PHP version 4.0                                                      |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group             |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.02 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available at through the world-wide-web at                           |
   | http://www.php.net/license/2_02.txt.                                 |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Authors: Sterling Hughes <sterling@php.net>                          |
   +----------------------------------------------------------------------+
 */

/* $Id$ */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_xslt.h"
#include "php_sablot.h"
#include "ext/standard/info.h"

#if HAVE_SABLOT_BACKEND

#include <sablot.h>

#include <string.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

/* functions relating to handlers */
static void register_sax_handler_pair(zval *, zval *, zval **);

/* Free processor */
static void free_processor(zend_rsrc_list_entry *);

/* Scheme handler functions */
static int  scheme_getall(void *, SablotHandle, const char *, const char *, char **, int *);
static int  scheme_freememory(void *, SablotHandle, char *);
static int  scheme_open(void *, SablotHandle, const char *, const char *, int *);
static int  scheme_get(void *, SablotHandle, int, char *, int *);
static int  scheme_put(void *, SablotHandle, int, const char *, int *);
static int  scheme_close(void *, SablotHandle, int);

/* Sax handler functions */
static SAX_RETURN sax_startdoc(void *);
static SAX_RETURN sax_startelement(void *, const char *, const char **);
static SAX_RETURN sax_endelement(void *, const char *);
static SAX_RETURN sax_startnamespace(void *, const char *, const char *);
static SAX_RETURN sax_endnamespace(void *, const char *);
static SAX_RETURN sax_comment(void *, const char *);
static SAX_RETURN sax_pi(void *, const char *, const char *);
static SAX_RETURN sax_characters(void *, const char *, int);
static SAX_RETURN sax_enddoc(void *);

/* Error handlers */
static MH_ERROR error_makecode(void *, SablotHandle, int, unsigned short, unsigned short);
static MH_ERROR error_log(void *, SablotHandle, MH_ERROR, MH_LEVEL, char **);
static MH_ERROR error_print(void *, SablotHandle, MH_ERROR, MH_LEVEL, char **);

/* Resource related */
#define le_xslt_name "XSLT Processor"
static int  le_xslt;

/* {{{ xslt_functions[]
 */
function_entry xslt_functions[] = {
	PHP_FE(xslt_create,              NULL)
	PHP_FE(xslt_set_sax_handlers,    NULL)
	PHP_FE(xslt_set_scheme_handlers, NULL)
	PHP_FE(xslt_set_error_handler,   NULL)
	PHP_FE(xslt_set_base,		 NULL)
#ifdef HAVE_SABLOT_SET_ENCODING
	PHP_FE(xslt_set_encoding,        NULL)
#else
	PHP_FALIAS(xslt_set_encoding, warn_not_available, NULL)
#endif
	PHP_FE(xslt_set_log,             NULL)
	PHP_FE(xslt_process,             NULL)
	PHP_FE(xslt_error,               NULL)
	PHP_FE(xslt_errno,               NULL)
	PHP_FE(xslt_free,                NULL)
};
/* }}} */

/* {{{ xslt_module_entry
 */
zend_module_entry xslt_module_entry = {
	"xslt",
	xslt_functions,
	PHP_MINIT(xslt),
	NULL,
	NULL,	
	NULL,
	PHP_MINFO(xslt),
	STANDARD_MODULE_PROPERTIES
};
/* }}} */

#ifdef COMPILE_DL_XSLT
ZEND_GET_MODULE(xslt)
#endif

/* {{{ handler structs
   A structure containing the sax handlers, automatically 
   registered whether the user defines them or not */
static SAXHandler sax_handlers = 
{
	sax_startdoc,
	sax_startelement,
	sax_endelement,
	sax_startnamespace,
	sax_endnamespace,
	sax_comment,
	sax_pi,
	sax_characters,
	sax_enddoc
};

/* Error handlers, automatically registered */
static MessageHandler message_handler = {
	error_makecode,
	error_log,
	error_print
};

/* Scheme handlers automatically registered */
static SchemeHandler scheme_handler = {
	scheme_getall,
	scheme_freememory,
	scheme_open,
	scheme_get,
	scheme_put,
	scheme_close
};
/* }}} */

/* {{{ PHP_MINIT_FUNCTION
 */
PHP_MINIT_FUNCTION(xslt)
{
	le_xslt = zend_register_list_destructors_ex(free_processor, NULL, le_xslt_name, module_number);

	return SUCCESS;
}
/* }}} */

/* {{{ PHP_MINFO_FUNCTION
 */
PHP_MINFO_FUNCTION(xslt)
{
	php_info_print_table_start();
	php_info_print_table_header(2, "XSLT support", "enabled");
	php_info_print_table_end();
}
/* }}} */

/* {{{ proto resource xslt_create(void) 
   Create a new XSLT processor */
PHP_FUNCTION(xslt_create)
{
	php_xslt     *handle;      /* The php -> sablotron handle */
	SablotHandle  processor;   /* The sablotron processor */
	int           error;       /* The error container */

	/* Allocate the php-sablotron handle */
	handle                   = emalloc(sizeof(php_xslt));
	handle->handlers         = emalloc(sizeof(struct xslt_handlers));
	handle->err              = emalloc(sizeof(struct xslt_error));

	XSLT_LOG(handle).path = NULL;

	/* Allocate the actual processor itself, via sablotron */
	error = SablotCreateProcessor(&processor);
	if (error) {
		XSLT_ERRNO(handle) = error;
		RETURN_FALSE;
	}

	/* Save the processor and set the default handlers */
	XSLT_PROCESSOR(handle) = processor;
	SablotRegHandler(XSLT_PROCESSOR(handle), HLR_SAX,     (void *) &sax_handlers,    (void *) handle);
	SablotRegHandler(XSLT_PROCESSOR(handle), HLR_MESSAGE, (void *) &message_handler, (void *) handle);
	SablotRegHandler(XSLT_PROCESSOR(handle), HLR_SCHEME,  (void *) &scheme_handler,  (void *) handle);

	/* Register the processor as a resource and return it to the user */
	ZEND_REGISTER_RESOURCE(return_value, handle, le_xslt);
	
	/* The resource index, save it for later use */
	handle->processor.idx = Z_LVAL_P(return_value);
}
/* }}} */

/* {{{ proto void xslt_set_sax_handlers(resource processor, array handlers)
   Set the SAX handlers to be called when the XML document gets processed */
PHP_FUNCTION(xslt_set_sax_handlers)
{
	zval       **processor_p,      /* Resource pointer to the php->sablotron handle */
	           **sax_handlers_p,   /* Pointer to the sax handlers php array */
	           **handler;          /* Individual handler, or handler pair */
	HashTable   *sax_handlers;     /* PHP array of sax handlers */
	php_xslt    *handle;           /* Pointer to a php_xslt handle */
	char        *string_key;       /* String key for the hash */
	ulong        num_key;          /* (unused) hash's numerical key */
	int          key_type;         /* The type of the current hash key */

	if (ZEND_NUM_ARGS() != 2 ||
	    zend_get_parameters_ex(2, &processor_p, &sax_handlers_p) == FAILURE) {
		WRONG_PARAM_COUNT;
	}
	ZEND_FETCH_RESOURCE(handle, php_xslt *, processor_p, -1, le_xslt_name, le_xslt);
	
	/* Convert the sax_handlers_p zval ** to a hash table we can process */
	sax_handlers = HASH_OF(*sax_handlers_p);
	if (!sax_handlers) {
		php_error(E_WARNING, "Expecting an array as the second argument to xslt_set_sax_handlers()");
		return;
	}

	/* Loop through the HashTable containing the SAX handlers */
	for (zend_hash_internal_pointer_reset(sax_handlers);
	     zend_hash_get_current_data(sax_handlers, (void **) &handler) == SUCCESS;
		 zend_hash_move_forward(sax_handlers)) {

		/* Allocate the handler */
		SEPARATE_ZVAL(handler);

		key_type = zend_hash_get_current_key(sax_handlers, &string_key, &num_key, 0);
		if (key_type == HASH_KEY_IS_LONG) {
			convert_to_string_ex(handler);
			php_error(E_NOTICE, "Skipping numerical index %d (with value %s) in xslt_set_sax_handlers()",
			          num_key, Z_STRVAL_PP(handler));
			continue;
		}

		/* Document handlers (document start, document end) */
		if (strcasecmp(string_key, "document") == 0) {
			register_sax_handler_pair(XSLT_SAX(handle).doc_start, 
			                          XSLT_SAX(handle).doc_end, 
			                          handler);
		}
		/* Element handlers, start of an element, and end of an element */
		else if (strcasecmp(string_key, "element") == 0) {
			register_sax_handler_pair(XSLT_SAX(handle).element_start, 
			                          XSLT_SAX(handle).element_end, 
			                          handler);
		}
		/* Namespace handlers, start of a namespace, end of a namespace */
		else if (strcasecmp(string_key, "namespace") == 0) {
			register_sax_handler_pair(XSLT_SAX(handle).namespace_start, 
			                          XSLT_SAX(handle).namespace_end, 
			                          handler);
		}
		/* Comment handlers, called when a comment is reached */
		else if (strcasecmp(string_key, "comment") == 0) {
			XSLT_SAX(handle).comment = *handler;
			zval_add_ref(&XSLT_SAX(handle).comment);
		}
		/* Processing instructions handler called when processing instructions
		   (<? ?>) */
		else if (strcasecmp(string_key, "pi") == 0) {
			XSLT_SAX(handle).pi = *handler;
			zval_add_ref(&XSLT_SAX(handle).pi);
		}
		/* Character handler, called when data is found */
		else if (strcasecmp(string_key, "character") == 0) {
			XSLT_SAX(handle).characters = *handler;
			zval_add_ref(XSLT_SAX(handle).characters);
		}
		/* Invalid handler name, tsk, tsk, tsk :) */
		else {
			php_error(E_WARNING, "Invalid option to xslt_set_sax_handlers(): %s", string_key);
		}
	}
}
/* }}} *

/* {{{ proto void xslt_set_scheme_handlers(resource processor, array handlers)
   Set the scheme handlers for the XSLT processor */
PHP_FUNCTION(xslt_set_scheme_handlers)
{
	zval                   **processor_p,       /* Resource pointer to the php->sablotron handle */
	                       **scheme_handlers_p, /* Pointer to the scheme handler array */
	                       **handler;           /* Individual scheme handler */
	zval                    *assign_handle;     /* The handler to assign to */
	HashTable               *scheme_handlers;   /* Scheme handler array */
	php_xslt                *handle;            /* php->sablotron handle */
	char                    *string_key;        /* Hash key (string) */
	ulong                    num_key;           /* (unused) Hash key (number) */
	int                      key_type;          /* The type of the current key */

	if (ZEND_NUM_ARGS() != 2 ||
	    zend_get_parameters_ex(2, &processor_p, &scheme_handlers_p) == FAILURE) {
		WRONG_PARAM_COUNT;
	}
	ZEND_FETCH_RESOURCE(handle, php_xslt *, processor_p, -1, le_xslt_name, le_xslt);

	scheme_handlers = HASH_OF(*scheme_handlers_p);
	if (!scheme_handlers) {
		php_error(E_WARNING, "2nd argument to xslt_set_scheme_handlers() must be an array");
		return;
	}

	/* Loop through the scheme handlers array, setting the given
	   scheme handlers */
	for (zend_hash_internal_pointer_reset(scheme_handlers);
	     zend_hash_get_current_data(scheme_handlers, (void **) &handler) == SUCCESS;
	     zend_hash_move_forward(scheme_handlers)) {

		SEPARATE_ZVAL(handler);

		key_type = zend_hash_get_current_key(scheme_handlers, &string_key, &num_key, 0);
		if (key_type == HASH_KEY_IS_LONG) {
			php_error(E_NOTICE, "Numerical key %d (with value %s) being ignored in xslt_set_scheme_handlers()",
					  num_key, Z_STRVAL_PP(handler));
			continue;
		}

		/* Open the URI and return the whole string */
		if (strcasecmp(string_key, "get_all") == 0) {
			assign_handle = XSLT_SCHEME(handle).get_all;
		}
		/* Open the URI and return a handle */
		else if (strcasecmp(string_key, "open") == 0) {
			assign_handle = XSLT_SCHEME(handle).open;
		}
		/* Retrieve data from the URI */
		else if (strcasecmp(string_key, "get") == 0) {
			assign_handle = XSLT_SCHEME(handle).get;
		}
		/* Save data to the URI */
		else if (strcasecmp(string_key, "put") == 0) {
			assign_handle = XSLT_SCHEME(handle).put;
		}
		/* Close the URI */
		else if (strcasecmp(string_key, "close") == 0) {
			assign_handle = XSLT_SCHEME(handle).close;
		}
		/* Invalid handler name */
		else {
			php_error(E_WARNING, "Invalid option to xslt_set_scheme_handlers(): %s", string_key);
		}

		assign_handle = *handler;
		zval_add_ref(assign_handle);
	}
}
/* }}} */

/* {{{ proto void xslt_set_error_handler(resource processor, mixed error_func)
   Set the error handler, to be called when an XSLT error happens */
PHP_FUNCTION(xslt_set_error_handler)
{
	zval      **processor_p,   /* Resource Pointer to a PHP-XSLT processor */
	          **error_func;    /* Name of the user defined error function */
	php_xslt   *handle;        /* A PHP-XSLT processor */
	
	if (ZEND_NUM_ARGS() != 2 ||
	    zend_get_parameters_ex(2, &processor_p, &error_func) == FAILURE) {
		WRONG_PARAM_COUNT;
	}
	ZEND_FETCH_RESOURCE(handle, php_xslt *, processor_p, -1, le_xslt_name, le_xslt);

	XSLT_ERROR(handle) = *error_func;
	zval_add_ref(XSLT_ERROR(handle));
}
/* }}} */

/* {{{ proto void xslt_set_base(resource processor, string base)
   Sets the base URI for all XSLT transformations */
PHP_FUNCTION(xslt_set_base)
{
	zval     **processor_p,  /* Resource Pointer to a PHP-XSLT processor */
	         **base;         /* The base URI for the transformation */
	php_xslt  *handle;       /* A PHP-XSLT processor */

	if (ZEND_NUM_ARGS() != 2 ||
	    zend_get_parameters_ex(2, &processor_p, &base) == FAILURE) {
		WRONG_PARAM_COUNT;
	}
	ZEND_FETCH_RESOURCE(handle, php_xslt *, processor_p, -1, le_xslt_name, le_xslt);
	convert_to_string_ex(base);

	/* Set the base */
	SablotSetBase(XSLT_PROCESSOR(handle), Z_STRVAL_PP(base));
}
/* }}} */

/* {{{ proto void xslt_set_encoding(resource processor, string encoding)
   Set the output encoding for the current stylesheet */
PHP_FUNCTION(xslt_set_encoding)
{
/* The user has to explicitly compile sablotron with sablotron 
   encoding functions in order for SablotSetEncoding to be 
   enabled.  config.m4 automatically checks for this... */

#ifdef HAVE_SABLOT_SET_ENCODING
	zval       **processor_p,  /* Resource Pointer to a PHP-XSLT processor */
	           **encoding;     /* The encoding to use for the output */
	php_xslt    *handle;       /* A PHP-XSLT processor */

	if (ZEND_NUM_ARGS() != 2 ||
	    zend_get_parameters_ex(2, &processor_p, &encoding) == FAILURE) {
		WRONG_PARAM_COUNT;
	}
	ZEND_FETCH_RESOURCE(handle, php_xslt *, processor_p, -1, le_xslt_name, le_xslt);
	convert_to_string_ex(encoding);

	/* Set the encoding */
	SablotSetEncoding(XSLT_PROCESSOR(handle), Z_STRVAL_PP(encoding));
#endif

}
/* }}} */

/* {{{ proto void xslt_set_log(resource processor, string logfile)
   Set the log file to write the errors to (defaults to stderr) */
PHP_FUNCTION(xslt_set_log)
{
	zval      **processor_p,             /* Resource pointer to a PHP-XSLT processor */
	          **logfile;                 /* Path to the logfile */
	php_xslt   *handle;                  /* A PHP-XSLT processor */
	int         argc = ZEND_NUM_ARGS();  /* Argument count */

	if (argc < 1 || argc > 2 ||
	    zend_get_parameters_ex(argc, &processor_p, &logfile) == FAILURE) {
		WRONG_PARAM_COUNT;
	}
	ZEND_FETCH_RESOURCE(handle, php_xslt *, processor_p, -1, le_xslt_name, le_xslt);
	convert_to_string_ex(logfile);
	
	/* If the log file already exists, free it */
	if (XSLT_LOG(handle).path) {
		efree(XSLT_LOG(handle).path);
	}
	
	/* Copy the path */
	XSLT_LOG(handle).path = estrndup(Z_STRVAL_PP(logfile),
	                                 Z_STRLEN_PP(logfile));
}
/* }}} */

/* {{{ proto string xslt_process(resource processor, string xml, string xslt[, mixed result[, array args[, array params]]])
   Perform the xslt transformation */
PHP_FUNCTION(xslt_process)
{
	zval       **processor_p,             /* Resource Pointer to a PHP-XSLT processor */
	           **xml_p,                   /* A zval pointer to the XML data */
	           **xslt_p,                  /* A zval pointer to the XSLT data */
	           **result_p,                /* A zval pointer to the transformation results */
	           **params_p,                /* A zval pointer to the XSLT parameters array */
	           **args_p;                  /* A zval pointer to the XSLT arguments array */
	php_xslt    *handle;                  /* A PHP-XSLT processor */
	char       **params = NULL;           /* A Sablotron parameter array (derived from the zval parameter array) */
	char       **args   = NULL;           /* A Sablotron argument array (derived from the zval argument array) */
	char        *xslt;                    /* The XSLT stylesheet or argument buffer (pointer to xslt_p) */
	char        *xml;                     /* The XML stylesheet or argument buffer (pointer to xml_p) */
	char        *result;                  /* The result file or argument buffer */
	int          argc = ZEND_NUM_ARGS();  /* The number of arguments given */
	int          error;                   /* Our error container */

	if (argc < 3 || argc > 6 ||
	    zend_get_parameters_ex(argc, &processor_p, &xml_p, &xslt_p, &result_p, &args_p, &params_p) == FAILURE) {
		WRONG_PARAM_COUNT;
	}

	ZEND_FETCH_RESOURCE(handle, php_xslt *, processor_p, -1, le_xslt_name, le_xslt);
	convert_to_string_ex(xml_p);
	convert_to_string_ex(xslt_p);

	xml  = Z_STRVAL_PP(xml_p);
	xslt = Z_STRVAL_PP(xslt_p);

	/* Well, no result file was given or result buffer, that means (guess what?)
	   we're returning the result yipp di doo! */
	if (argc < 4 || Z_TYPE_PP(result_p) == IS_NULL) {
		result = "arg:/_result";
	}
	/* The result buffer to place the data into, either a file or an argument buffer, etc. */
	else {
		convert_to_string_ex(result_p);
		result = Z_STRVAL_PP(result_p);
	}

	/* Translate a PHP array into a Sablotron array */
	if (argc > 4) {
		xslt_make_array(args_p,   &args);
	}
	
	if (argc > 5) {
		xslt_make_array(params_p, &params);
	}
	
	/* Perform transformation */
	error = SablotRunProcessor(XSLT_PROCESSOR(handle), xslt, xml, result, params, args);
	if (error) {
		XSLT_ERRNO(handle) = error;

		if (params) xslt_free_array(params);
		if (args)   xslt_free_array(args);

		RETURN_FALSE;
	}

	/* If the result buffer is specified, then we return the results of the XSLT
	   transformation */
	if (!strcmp(result, "arg:/_result")) {
		char *trans_result;

		/* Fetch the result buffer into trans_result */
		error = SablotGetResultArg(handle->processor.ptr, "arg:/_result", &trans_result);
		if (error) {
			/* Save the error number */
			XSLT_ERRNO(handle) = error;
			
			/* Cleanup */
			if (params) xslt_free_array(params);
			if (args)   xslt_free_array(args);
			
			RETURN_FALSE;
		}

		RETVAL_STRING(trans_result, 1);
		SablotFree(trans_result);
	}
	else {
		RETVAL_TRUE;
	}
	
	/* Cleanup */
	if (params) xslt_free_array(params);
	if (args)   xslt_free_array(args);
}
/* }}} */

/* {{{ proto int xslt_errno(resource processor)
   Error number */
PHP_FUNCTION(xslt_errno)
{
	zval        **processor_p;   /* Resource pointer to a PHP-XSLT processor */
	php_xslt     *handle;        /* A PHP-XSLT processor */

	if (ZEND_NUM_ARGS() != 1 ||
	    zend_get_parameters_ex(1, &processor_p) == FAILURE) {
		WRONG_PARAM_COUNT;
	}
	ZEND_FETCH_RESOURCE(handle, php_xslt *, processor_p, -1, le_xslt_name, le_xslt);

	RETURN_LONG(XSLT_ERRNO(handle));
}
/* }}} */

/* {{{ proto string xslt_error(resource processor)
   Error string */
PHP_FUNCTION(xslt_error)
{
	zval      **processor_p;  /* Resource pointer to a PHP-XSLT processor */
	php_xslt   *handle;       /* A PHP-XSLT processor */

	if (ZEND_NUM_ARGS() != 1 ||
	    zend_get_parameters_ex(1, &processor_p) == FAILURE) {
		WRONG_PARAM_COUNT;
	}
	ZEND_FETCH_RESOURCE(handle, php_xslt *, processor_p, -1, le_xslt_name, le_xslt);

	RETURN_STRING(XSLT_ERRSTR(handle), 1);	
}
/* }}} */

/* {{{ proto void xslt_free(resource processor)
   Free the xslt processor up */
PHP_FUNCTION(xslt_free)
{
	zval     **processor_p;   /* Resource pointer to a php-xslt processor */
	php_xslt  *handle;        /* A PHP-XSLT processor */
	
	if (ZEND_NUM_ARGS() != 1 ||
	    zend_get_parameters_ex(1, &processor_p) == FAILURE) {
		WRONG_PARAM_COUNT;
	}
	ZEND_FETCH_RESOURCE(handle, php_xslt *, processor_p, -1, le_xslt_name, le_xslt);
	
	/* Remove the entry from the list */
	zend_list_delete(Z_LVAL_PP(processor_p));
}
/* }}} */

/* {{{ free_processor()
   Free an XSLT processor */
static void free_processor(zend_rsrc_list_entry *rsrc)
{
	php_xslt *handle = (php_xslt *) rsrc->ptr;     /* A PHP-XSLT processor */
	
	/* Free the processor */
	if (XSLT_PROCESSOR(handle)) {
		SablotUnregHandler(XSLT_PROCESSOR(handle), HLR_MESSAGE, NULL, NULL);
		SablotUnregHandler(XSLT_PROCESSOR(handle), HLR_SAX,     NULL, NULL);
		SablotUnregHandler(XSLT_PROCESSOR(handle), HLR_SCHEME,  NULL, NULL);
		SablotDestroyProcessor(XSLT_PROCESSOR(handle));
	}

	/* Free Scheme handlers */
	zval_ptr_dtor(&XSLT_SCHEME(handle).get_all);
	zval_ptr_dtor(&XSLT_SCHEME(handle).open);
	zval_ptr_dtor(&XSLT_SCHEME(handle).get);
	zval_ptr_dtor(&XSLT_SCHEME(handle).put);
	zval_ptr_dtor(&XSLT_SCHEME(handle).close);
	/* Free SAX handlers */
	zval_ptr_dtor(&XSLT_SAX(handle).doc_start);
	zval_ptr_dtor(&XSLT_SAX(handle).element_start);
	zval_ptr_dtor(&XSLT_SAX(handle).element_end);
	zval_ptr_dtor(&XSLT_SAX(handle).namespace_start);
	zval_ptr_dtor(&XSLT_SAX(handle).namespace_end);
	zval_ptr_dtor(&XSLT_SAX(handle).comment);
	zval_ptr_dtor(&XSLT_SAX(handle).pi);
	zval_ptr_dtor(&XSLT_SAX(handle).characters);
	zval_ptr_dtor(&XSLT_SAX(handle).doc_end);
	/* Free error handler */
	zval_ptr_dtor(&XSLT_ERROR(handle));

	/* Free error message, if any */
	if (XSLT_ERRSTR(handle)) {
		efree(XSLT_ERRSTR(handle));
	}

	/* Close log file */
	if (XSLT_LOG(handle).fd) {
		close(XSLT_LOG(handle).fd);
	}
	
	/* Free log file path */
	if (XSLT_LOG(handle).path) {
		efree(XSLT_LOG(handle).path);
	}

	/* Free up the handle */
	efree(handle->handlers);
	efree(handle->err);
	efree(handle);
}
/* }}} */

/* {{{ register_sax_handler_pair()
   Register a pair of sax handlers */
static void register_sax_handler_pair(zval *handler1, zval *handler2, zval **handler)
{
	zval **current;   /* The current handler we're grabbing */
	
	/* Grab and assign handler 1 */
	if (zend_hash_index_find(Z_ARRVAL_PP(handler), 0, (void **) &current) == SUCCESS) {
		handler1 = *current;
		zval_add_ref(handler1);
	}
	else {
		php_error(E_WARNING, "Wrong format of arguments to xslt_set_sax_handlers()");
		return;
	}
	
	/* Grab and assign handler 2 */
	if (zend_hash_index_find(Z_ARRVAL_PP(handler), 1, (void **) &current) == SUCCESS) {
		handler2 = *current;
		zval_add_ref(handler2);
	}
	else {
		php_error(E_WARNING, "Wrong format of arguments to xslt_set_sax_handlers()");
		return;
	}
}
/* }}} */

/* {{{ scheme_getall()
   The getall scheme handler */
static int scheme_getall(void *user_data, SablotHandle proc, const char *scheme, const char *rest, char **buffer, int *byte_count)
{
	zval       *argv[3];                           /* Arguments to the scheme getall function */
	zval       *retval;                            /* Return value from the scheme getall function */
	php_xslt   *handle = (php_xslt *) user_data;   /* A PHP-XSLT processor */

	/* If the scheme handler get all function doesn't
	   exist, exit out */
	if (!XSLT_SCHEME(handle).get_all) {
		return 0;
	}

	/* Allocate and initialize */
	MAKE_STD_ZVAL(argv[0]);
	MAKE_STD_ZVAL(argv[1]);
	MAKE_STD_ZVAL(argv[2]);

	/* Argument 1: XSLT processor (resource)
	   Argument 2: Scheme         (string)
	   Argument 3: Rest           (string)
	 */
	ZVAL_RESOURCE(argv[0], handle->processor.idx);
	zend_list_addref(handle->processor.idx);
	ZVAL_STRING(argv[1], (char *) scheme, 1);
	ZVAL_STRING(argv[2], (char *) rest, 1);

	xslt_call_function("scheme get all", XSLT_SCHEME(handle).get_all, 
	                   3, argv, &retval);

	/* Save the return value in the buffer (copying it) */
	*buffer     = estrndup(Z_STRVAL_P(retval), Z_STRLEN_P(retval));
	*byte_count = Z_STRLEN_P(retval);

	/* Free return value */
	zval_ptr_dtor(&retval);

	return 0;
}
/* }}} */

/* {{{ scheme_handler_is_registered()
   Check to see if the scheme handler is registered with the user */
static int scheme_handler_is_registered(php_xslt *handle)
{
	/* If one of the functions is exists, then scheme
	   handlers are registered */
	if (XSLT_SCHEME(handle).get_all  ||
	    XSLT_SCHEME(handle).open     ||
	    XSLT_SCHEME(handle).get      ||
	    XSLT_SCHEME(handle).put      ||
	    XSLT_SCHEME(handle).close)
	 	return 1;
	/* otherwise, no cigar */
	else
		return 0;
}
/* }}} */

/* {{{ scheme_freememory()
   Called when sablotron needs to free memory related to scheme handling */
static int scheme_freememory(void *user_data, SablotHandle proc, char *buffer)
{
	/* If we don't have any scheme handler's registered, then emalloc() wasn't
	   used, and if emalloc() wasn't then efree shouldn't be used */
	if (!scheme_handler_is_registered((php_xslt *) user_data)) {
		return 0;
	}

	/* Free the memory using efree() and remove it from the register */
	efree(buffer);
	
	return 0;
}
/* }}} */

/* {{{ scheme_open()
   Called when the URI needs to be opened */
static int  scheme_open(void *user_data, SablotHandle proc, const char *scheme, const char *rest, int *fd)
{
	zval      *argv[3];                            /* Arguments to the scheme open function */
	zval      *retval;                             /* The return value from the scheme open function */
	php_xslt  *handle = (php_xslt *) user_data;    /* A PHP-XSLT processor */

	/* If no open handler exists, let's exit */
	if (!XSLT_SCHEME(handle).open) {
		return 0;
	}

	/* Allocate and initialize arguments */
	MAKE_STD_ZVAL(argv[0]);
	MAKE_STD_ZVAL(argv[1]);
	MAKE_STD_ZVAL(argv[2]);

	/* Argument 1: XSLT Processor (resource)
	   Argument 2: Scheme         (string)
	   Argument 3: Rest           (string)
	 */
	ZVAL_RESOURCE(argv[0], handle->processor.idx);
	zend_list_addref(handle->processor.idx);
	ZVAL_STRING(argv[1], (char *) scheme, 1);
	ZVAL_STRING(argv[2], (char *) rest, 1);
	
	/* Call the function */
	xslt_call_function("scheme open", XSLT_SCHEME(handle).open,
	                   3, argv, &retval);

	/* Return value is a resource pointer to an open file */
	*fd = Z_LVAL_P(retval);

	/* Free it all up */
	zval_ptr_dtor(&retval);

	/* return success */
	return 0;
}
/* }}} */

/* {{{ scheme_get()
   Called when data needs to be fetched from the URI */
static int  scheme_get(void *user_data, SablotHandle proc, int fd, char *buffer, int *byte_count)
{
	zval       *argv[3];                           /* Arguments to the scheme get function  */
	zval       *retval;                            /* Return value from the scheme get function */
	php_xslt   *handle = (php_xslt *) user_data;   /* A PHP-XSLT processor */

	/* If no get handler exists, let's exit */
	if (!XSLT_SCHEME(handle).get) {
		return 0;
	}

	/* Allocate and initialize arguments */
	MAKE_STD_ZVAL(argv[0]);
	MAKE_STD_ZVAL(argv[1]);
	MAKE_STD_ZVAL(argv[2]);

	/* Argument 1: XSLT Processor (resource)
	   Argument 2: File Pointer   (resource)
	   Argument 3: Data           (string)
	 */
	ZVAL_RESOURCE(argv[0], handle->processor.idx);
	zend_list_addref(handle->processor.idx);
	ZVAL_RESOURCE(argv[1], fd);
	zend_list_addref(fd);
	ZVAL_STRINGL(argv[2], buffer, *byte_count, 0);
	
	/* Call the function */
	xslt_call_function("scheme get", XSLT_SCHEME(handle).get,
	                   3, argv, &retval);
	
	/* Returns the number of bytes read */
	*byte_count = Z_LVAL_P(retval);

	/* Free things up */
	zval_ptr_dtor(&retval);

	/* return success */
	return 0;
}
/* }}} */

/* {{{ scheme_put()
   Called when data needs to be written */
static int  scheme_put(void *user_data, SablotHandle proc, int fd, const char *buffer, int *byte_count)
{
	zval       *argv[3];                            /* Arguments to the scheme put function */
	zval       *retval;                             /* Return value from the scheme put function */
	php_xslt   *handle = (php_xslt *) user_data;    /* A PHP-XSLT processor */

	/* If no put handler exists, let's exit */
	if (!XSLT_SCHEME(handle).put) {
		return 0;
	}
	
	/* Allocate and initialize arguments */
	MAKE_STD_ZVAL(argv[0]);
	MAKE_STD_ZVAL(argv[1]);
	MAKE_STD_ZVAL(argv[2]);

	/* Argument 1: XSLT processor (resource)
	   Argument 2: File pointer   (resource)
	   Argument 3: Data           (string)
	 */
	ZVAL_RESOURCE(argv[0], handle->processor.idx);
	zend_list_addref(handle->processor.idx);
	ZVAL_RESOURCE(argv[1], fd);
	zend_list_addref(fd);
	ZVAL_STRINGL(argv[2], (char *) buffer, *byte_count, 1);
	
	/* Call the scheme put function already */
	xslt_call_function("scheme put", XSLT_SCHEME(handle).put,
	                   3, argv, &retval);

	/* The return value is the number of bytes written */
	*byte_count = Z_LVAL_P(retval);

	/* Free everything up */
	zval_ptr_dtor(&retval);

	/* Return success */
	return 0;
}
/* }}} */

/* {{{ scheme_close()
   Called when its time to close the fd */
static int  scheme_close(void *user_data, SablotHandle proc, int fd)
{
	zval       *argv[2];                           /* Arguments to the scheme close function*/
	zval       *retval;                            /* Return value from the scheme close function */
	php_xslt   *handle = (php_xslt *) user_data;   /* A PHP-XSLT processor */

	/* if no close handler exists, exit */
	if (!XSLT_SCHEME(handle).close) {
		return 0;
	}

	/* Allocate and initialize arguments */
	MAKE_STD_ZVAL(argv[0]);
	MAKE_STD_ZVAL(argv[1]);

	/* Argument 1: XSLT processor (resource)
	   Argument 2: File pointer   (resource)
	 */
	ZVAL_RESOURCE(argv[0], handle->processor.idx);
	zend_list_addref(handle->processor.idx);
	ZVAL_RESOURCE(argv[1], fd);
	zend_list_addref(fd);
	
	/* Call the scheme handler close function */
	xslt_call_function("scheme close", XSLT_SCHEME(handle).close,
	                   2, argv, &retval);

	/* Free everything up */
	zval_ptr_dtor(&retval);

	/* Return success */
	return 0;
}
/* }}} */

/* {{{ sax_startdoc()
   Called when the document starts to be processed */
static SAX_RETURN sax_startdoc(void *ctx)
{
	zval       *argv[1];                    /* Arguments to the sax start doc function */
	zval       *retval;                     /* Return value from sax start doc function */
	php_xslt   *handle = (php_xslt *) ctx;  /* A PHP-XSLT processor */

	/* if no document start function exists, exit */
	if (!XSLT_SAX(handle).doc_start) {
		return;
	}

	/* Allocate and initialize arguments */
	MAKE_STD_ZVAL(argv[0]);

	/* Argument 1:  XSLT processor (resource) */
	ZVAL_RESOURCE(argv[0], handle->processor.idx);
	zend_list_addref(handle->processor.idx);
	
	/* Call the Sax start doc function */
	xslt_call_function("sax start doc", XSLT_SAX(handle).doc_start,
	                   1, argv, &retval);

	/* Cleanup */
	zval_ptr_dtor(&retval);
}
/* }}} */

/* {{{ sax_startelement()
   Called when an element is begun to be processed */
static SAX_RETURN sax_startelement(void *ctx, 
                                   const char  *name, 
                                   const char **attr)
{
	zval       *argv[3];                     /* Arguments to the sax start element function */
	zval       *retval;                      /* Return value from the sax start element function */
	php_xslt   *handle = (php_xslt *) ctx;   /* A PHP-XSLT processor */
	char      **p;                           /* Pointer to attributes */

	/* If no element start function is found, exit */
	if (!XSLT_SAX(handle).element_start) {
		return;
	}

	/* Allocate and initialize arguments */
	MAKE_STD_ZVAL(argv[0]);
	MAKE_STD_ZVAL(argv[1]);
	MAKE_STD_ZVAL(argv[2]);
	array_init(argv[2]);

	/* Argument 1: XSLT processor (resource)
	   Argument 2: Element name   (string)
	   Argument 3: Element attributes (array)
	 */
	ZVAL_RESOURCE(argv[0], handle->processor.idx);
	zend_list_addref(handle->processor.idx);
	ZVAL_STRING(argv[1], (char *) name, 1);

	/* loop through the attributes array, copying it onto our
	   php array */
	p = (char **) attr;
	while (p && *p) {
		add_assoc_string(argv[2], *p, *(p + 1), 1);
		p += 2;
	}

	/* Call the sax element start function */
	xslt_call_function("sax start element", XSLT_SAX(handle).element_start, 
	                   3, argv, &retval);
	
	/* Cleanup */
	zval_ptr_dtor(&retval);
}
/* }}} */

/* {{{ xslt_sax_endelement()
   Called when an ending XML element is encountered */
static SAX_RETURN sax_endelement(void *ctx, const char *name)
{
	zval        *argv[2];                   /* Arguments to the sax end element function */
	zval        *retval;                    /* Return value from the sax end element function */
	php_xslt    *handle = (php_xslt *) ctx; /* A PHP-XSLT processor */

	/* If no element end function exists, exit */
	if (!XSLT_SAX(handle).element_end) {
		return;
	}
	
	/* Allocate and initialize arguments */
	MAKE_STD_ZVAL(argv[0]);
	MAKE_STD_ZVAL(argv[1]);

	/* Argument 1: XSLT processor (resource)
	   Argument 2: Element name   (string)
	 */
	ZVAL_RESOURCE(argv[0], handle->processor.idx);
	zend_list_addref(handle->processor.idx);
	ZVAL_STRING(argv[1], (char *) name, 1);

	/* Call the sax end element function */
	xslt_call_function("sax end element", XSLT_SAX(handle).element_end,
	                   2, argv, &retval);
	
	/* Cleanup */
	zval_ptr_dtor(&retval);
}
/* }}} */

/* {{{ sax_startnamespace()
   Called at the beginning of the parsing of a new namespace */
static SAX_RETURN sax_startnamespace(void *ctx, 
                                     const char *prefix, 
                                     const char *uri)
{
	zval       *argv[3];                    /* Arguments to the sax start namespace function */
	zval       *retval;                     /* Return value from the sax start namespace function */
	php_xslt   *handle = (php_xslt *) ctx;  /* A PHP-XSLT processor */

	/* if no namespace start function exists, exit */
	if (!XSLT_SAX(handle).namespace_start) {
		return;
	}
	
	/* Allocate and initialize arguments */
	MAKE_STD_ZVAL(argv[0]);
	MAKE_STD_ZVAL(argv[1]);
	MAKE_STD_ZVAL(argv[2]);

	/* Argument 1: XSLT processor (resource)
	   Argument 2: Prefix         (string)
	   Argument 3: URI            (string)
	 */
	ZVAL_RESOURCE(argv[0], handle->processor.idx);
	zend_list_addref(handle->processor.idx);
	ZVAL_STRING(argv[1], (char *) prefix, 1);
	ZVAL_STRING(argv[2], (char *) uri, 1);

	/* Call the sax start namespace function */
	xslt_call_function("sax start namespace", XSLT_SAX(handle).namespace_start,
	                   3, argv, &retval);

	/* Cleanup */
	zval_ptr_dtor(&retval);
}
/* }}} */

/* {{{ sax_endnamespace()
   Called when a new namespace is finished being parsed */
static SAX_RETURN sax_endnamespace(void *ctx, const char *prefix)
{
	zval        *argv[2];                    /* Arguments to the sax end namespace function */
	zval        *retval;                     /* Return value from the sax end namespace function */
	php_xslt    *handle = (php_xslt *) ctx;  /* A PHP-XSLT processor */

	/* If no namespace end function exists, exit */
	if (!XSLT_SAX(handle).namespace_end) {
		return;
	}
	
	/* Allocate and initialize arguments */
	MAKE_STD_ZVAL(argv[0]);
	MAKE_STD_ZVAL(argv[1]);
	
	/* Argument 1: XSLT processor (resource)
	   Argument 2: Prefix         (string)
	 */
	ZVAL_RESOURCE(argv[0], handle->processor.idx);
	zend_list_addref(handle->processor.idx);
	ZVAL_STRING(argv[1], (char *) prefix, 1);
	
	/* Call the sax end namespace function */
	xslt_call_function("sax end namespace", XSLT_SAX(handle).namespace_end,
	                   2, argv, &retval);
	
	/* Cleanup */
	zval_ptr_dtor(&retval);
}
/* }}} */

/* {{{ sax_comment()
   Called when a comment is found */
static SAX_RETURN sax_comment(void *ctx, const char *contents)
{
	zval        *argv[2];                    /* Arguments to the sax comment function */
	zval        *retval;                     /* Return value from the sax comment function */
	php_xslt    *handle = (php_xslt *) ctx;  /* A PHP-XSLT processor */

	/* if no comment function exists, exit */
	if (!XSLT_SAX(handle).comment) {
		return;
	}
	
	/* Allocate and initialize arguments */
	MAKE_STD_ZVAL(argv[0]);
	MAKE_STD_ZVAL(argv[1]);
	
	/* Argument 1: XSLT processor   (resource)
	   Argument 2: Comment contents (string)
	 */
	ZVAL_RESOURCE(argv[0], handle->processor.idx);
	zend_list_addref(handle->processor.idx);
	ZVAL_STRING(argv[1], (char *) contents, 1);
	
	/* Call the sax comment function */
	xslt_call_function("sax comment", XSLT_SAX(handle).comment,
	                   2, argv, &retval);
	
	/* Cleanup */
	zval_ptr_dtor(&retval);
}
/* }}} */

/* {{{ sax_pi()
   Called when processing instructions are found */
static SAX_RETURN sax_pi(void *ctx, 
                         const char *target, 
                         const char *contents)
{
	zval        *argv[3];                    /* Arguments to the sax processing instruction function */
	zval        *retval;                     /* Return value from the sax processing instruction function */
	php_xslt    *handle = (php_xslt *) ctx;  /* A PHP-XSLT processor */

	/* If no processing instructions function exists, exit */
	if (!XSLT_SAX(handle).pi) {
		return;
	}

	/* Allocate and initialize arguments */
	MAKE_STD_ZVAL(argv[0]);
	MAKE_STD_ZVAL(argv[1]);
	MAKE_STD_ZVAL(argv[2]);
	
	/* Argument 1: XSLT processor     (resource)
	   Argument 2: Target of the pi   (string)
	   Argument 3: Contents of the pi (string)
	 */
	ZVAL_RESOURCE(argv[0], handle->processor.idx);
	zend_list_addref(handle->processor.idx);
	ZVAL_STRING(argv[1], (char *) target, 1);
	ZVAL_STRING(argv[2], (char *) contents, 1);

	/* Call processing instructions function */
	xslt_call_function("sax processing instructions", XSLT_SAX(handle).pi,
	                   3, argv, &retval);

	/* Cleanup */
	zval_ptr_dtor(&retval);
}
/* }}} */

/* {{{ sax_characters()
   Called when characters are come upon */
static SAX_RETURN sax_characters(void *ctx,
                                 const char *contents, 
                                 int length)
{
	zval         *argv[2];                    /* Arguments to the sax characters function */
	zval         *retval;                     /* Return value to the sax characters function */
	php_xslt     *handle = (php_xslt *) ctx;  /* A PHP-XSLT processor */

	/* If no characters function exists, exit */
	if (!XSLT_SAX(handle).characters) {
		return;
	}
	
	/* Allocate and initialize arguments */
	MAKE_STD_ZVAL(argv[0]);
	MAKE_STD_ZVAL(argv[1]);

	/* Argument 1: XSLT processor (resource)
	   Argument 2: Contents       (string)
	 */
	ZVAL_RESOURCE(argv[0], handle->processor.idx);
	zend_list_addref(handle->processor.idx);
	ZVAL_STRINGL(argv[1], (char *) contents, length, 1);

	/* Call characters function */
	xslt_call_function("sax characters", XSLT_SAX(handle).characters,
	                   2, argv, &retval);
	
	/* Cleanup */
	zval_ptr_dtor(&retval);
}
/* }}} */

/* {{{ sax_enddoc()
   Called when the document is finished being parsed */
static SAX_RETURN sax_enddoc(void *ctx)
{
	zval        *argv[1];                    /* Arguments to the end document function */
	zval        *retval;                     /* Return value from the end document function */
	php_xslt    *handle = (php_xslt *) ctx;  /* A PHP-XSLT processor */

	/* If no end document function exists, exit */
	if (!XSLT_SAX(handle).doc_end) {
		return;
	}
	
	/* Allocate and initialize arguments */
	MAKE_STD_ZVAL(argv[0]);

	/* Argument 1: XSLT Processor (resource)
	 */
	ZVAL_RESOURCE(argv[0], handle->processor.idx);
	zend_list_addref(handle->processor.idx);
	
	/* Call the function */
	xslt_call_function("sax end doc", XSLT_SAX(handle).doc_end,
	                   1, argv, &retval);
	
	/* Cleanup */
	zval_ptr_dtor(&retval);
}
/* }}} */

/* {{{ error_makecode()
   Make the error code */
static MH_ERROR error_makecode(void *user_data, SablotHandle proc, int severity, unsigned short facility, unsigned short code)
{
	return(0);
}
/* }}} */

/* {{{ error_log()
   Called when its time to log data */
static MH_ERROR error_log(void *user_data, SablotHandle proc, MH_ERROR code, MH_LEVEL level, char **fields)
{
	php_xslt *handle = (php_xslt *) user_data;                              /* A PHP-XSLT processor */
	char     *errmsg  = NULL;                                               /* Error message*/
	char     *errtype = NULL;                                               /* Error type */
	char     *errline = NULL;                                               /* Error line */
	char     *msgbuf  = NULL;                                               /* Message buffer */
	char      msgformat[] = "Sablotron Message on line %s, level %s: %s\n"; /* Message format */
	int       error = 0;                                                    /* Error container */

	/* Parse the error array */
	/* Loop through the error array */
	if (fields) {
		while (fields && *fields) {
			char *key;  /* Key to for the message */
			char *val;  /* The message itself */
			char *ptr;  /* Pointer to the location of the ':' (separator) */
			int   pos;  /* Position of the ':' (separator) */
			int   len;  /* Length of the string */

			len = strlen(*fields);

			/* Grab the separator's position */
			ptr = strchr(*fields, ':');
			if (!ptr) {
				continue;
			}
			pos = ptr - *fields;

			/* Allocate the key and value and copy the data onto them */
			key = emalloc(pos + 1);
			val = emalloc((len - pos) + 1);

			memcpy(key, *fields, pos);
			memcpy(val, *fields + pos + 1, len - pos - 1);

			key[pos] = '\0';
			val[len - pos - 1] = '\0';

			/* Check to see whether or not we want to save the data */
			if (!strcmp(key, "msg")) {
				errmsg = estrndup(val, len - pos -1);
			}
			else if (!strcmp(key, "type")) {
				errtype = estrndup(val, len - pos - 1);
			}
			else if (!strcmp(key, "line")) {
				errline = estrndup(val, len - pos - 1);
			}
			
			/* Cleanup */
			if (key) efree(key);
			if (val) efree(val);

			/* Next key:value pair please :) */
			fields++;
		}
	}

	
	/* If no error line is given, then place none in the 
	   file */
	if (!errline) {
		errline = estrndup("none", sizeof("none") - 1);
	}
	
	/* Default type is a log handle */
	if (!errtype) {
		errtype = estrndup("log", sizeof("log") - 1);
	}
	
	/* No error message, no cry */
	if (!errmsg) {
		errmsg = estrndup("unknown error", sizeof("unknown error") - 1);
	}
	
	/* Allocate the message buf and copy the data into it */
	msgbuf = emalloc((sizeof(msgformat) - 6) +
	                 strlen(errmsg) +
	                 strlen(errline) +
	                 strlen(errtype) + 1);
	sprintf(msgbuf, msgformat, errline, errtype, errmsg);

	/* If the error is serious enough, copy it to our error buffer 
	   which will show up when someone calls the xslt_error() function */
	if (level == MH_LEVEL_WARN  ||
	    level == MH_LEVEL_ERROR ||
	    level == MH_LEVEL_CRITICAL) {
		if (XSLT_ERRSTR(handle))
			efree(XSLT_ERRSTR(handle));
		
		XSLT_ERRSTR(handle) = estrdup(errmsg);
	}

	/* If we haven't allocated and opened the file yet */
	if (!XSLT_LOG(handle).fd) {
		/* Lets open up a file */
		if (XSLT_LOG(handle).path && strcmp(XSLT_LOG(handle).path, "php://stderr")) {
			/* open for append */
			XSLT_LOG(handle).fd = open(XSLT_LOG(handle).path, 
			                           O_WRONLY|O_CREAT|O_APPEND,
			                           S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR);
			if (XSLT_LOG(handle).fd < 0) {
				php_error(E_WARNING, "Cannot open log file, %s [%d]: %s",
				          XSLT_LOG(handle).path, errno, strerror(errno));
				XSLT_LOG(handle).fd = 0;
			}
		}
		/* Default is stderr, or if the user provided "php://stderr" that's the
		   stream */
		else {
			XSLT_LOG(handle).fd = 2;
		}
	}
	
	/* Write the error to the file */
	error = write(XSLT_LOG(handle).fd, msgbuf, strlen(msgbuf));
	if (error < 1) {
		php_error(E_WARNING, "Cannot write data to log file, %s, with fd, %d [%d]: %s",
		          (XSLT_LOG(handle).path ? XSLT_LOG(handle).path : "stderr"),
		          XSLT_LOG(handle).fd,
		          error,
		          strerror(error));
		return 0;
	}

	/* Cleanup */
	if (msgbuf)  efree(msgbuf);
	if (errtype) efree(errtype);
	if (errline) efree(errline);
	if (errmsg)  efree(errmsg);
	
	return 0;
}
/* }}} */

/* {{{ error_print()
   Print out an error message or call the error handler */
static MH_ERROR error_print(void *user_data, SablotHandle proc, MH_ERROR code, MH_LEVEL level, char **fields)
{
	php_xslt *handle = (php_xslt *) user_data;   /* A PHP-XSLT processor */
	
	if (XSLT_ERROR(handle)) {
		zval   *argv[4];   /* Arguments to the error function */
		zval   *retval;    /* Return value from the error function */

		/* Allocate and initialize */
		MAKE_STD_ZVAL(argv[0]);
		MAKE_STD_ZVAL(argv[1]);
		MAKE_STD_ZVAL(argv[2]);
		MAKE_STD_ZVAL(argv[3]);
		array_init(argv[3]);

		/* Argument 1: XSLT Processor (resource)
		   Argument 2: Error level    (long)
		   Argument 3: Error code     (long)
		   Argument 4: Error messages (array)
		 */
		ZVAL_RESOURCE(argv[0], handle->processor.idx);
		zend_list_addref(handle->processor.idx);
		ZVAL_LONG(argv[1], level);
		ZVAL_LONG(argv[2], code);

		if (fields) {
			while (fields && *fields) {
				char *key;  /* Key to for the message */
				char *val;  /* The message itself */
				char *ptr;  /* Pointer to the location of the ':' (separator) */
				int   pos;  /* Position of the ':' (separator) */
				int   len;  /* Length of the string */
			
				len = strlen(*fields);
			
				/* Grab the separator's position */
				ptr = strchr(*fields, ':');
				if (!ptr) {
					continue;
				}
				pos = ptr - *fields;

				/* Allocate the key and value and copy the data onto them */
				key = emalloc(pos + 1);
				val = emalloc((len - pos) + 1);

				memcpy(key, *fields, pos);
				memcpy(val, *fields + pos + 1, len - pos - 1);
				key[pos] = '\0';
				val[len - pos - 1] = '\0';

				/* Add it */				
				add_assoc_stringl_ex(argv[3], key, pos, val, len - pos - 1, 1);

				/* Cleanup */
				efree(key);
				efree(val);

				/* Next field please */
				fields++;
			}
		}

		/* Call the function */
		xslt_call_function("error handler", XSLT_ERROR(handle),
		                   4, argv, &retval);

		/* Free up */
		zval_ptr_dtor(&retval);
	}
	else {
		char *errmsg  = NULL;                                  /* Error message */
		char *errline = NULL;                                  /* Error line */
		char *msgbuf  = NULL;                                  /* Message buffer */
		char  msgformat[] = "Sablotron error on line %s: %s";  /* Message format */

		/* If the error is not serious, exit out */
		if (code == MH_LEVEL_WARN  || 
		    code == MH_LEVEL_ERROR || 
		    code == MH_LEVEL_CRITICAL) {
			return 0;
		}

		/* Loop through and extract the error message and the 
		   error line */
		if (fields) {
			while (fields && *fields) {
				char *key;  /* Key to for the message */
				char *val;  /* The message itself */
				char *ptr;  /* Pointer to the location of the ':' (separator) */
				int   pos;  /* Position of the ':' (separator) */
				int   len;  /* Length of the string */
			
				len = strlen(*fields);
			
				/* Grab the separator's position */
				ptr = strchr(*fields, ':');
				if (!ptr) {
					continue;
				}
				pos = ptr - *fields;
			
				/* Allocate the key and value and copy the data onto them */
				key = emalloc(pos + 1);
				val = emalloc((len - pos) + 1);
			
				memcpy(key, *fields, pos);
				memcpy(val, *fields + pos + 1, len - pos - 1);
			
				key[pos] = '\0';
				val[len - pos - 1] = '\0';
			
				/* Check to see whether or not we want to save the data */
				if (!strcmp(key, "msg")) {
					errmsg = estrdup(val);
				}
				else if (!strcmp(key, "line")) {
					errline = estrdup(val);
				}
			
				/* Cleanup */
				if (key) efree(key);
				if (val) efree(val);
			
				/* Next key:value pair please :) */
				fields++;
			}
		}
		
		if (!errline) {
			errline = estrndup("none", sizeof("none") - 1);
		}

		/* Allocate the message buffer and copy the data onto it */
		msgbuf = emalloc((sizeof(msgformat) - 4) + strlen(errmsg) + strlen(errline) + 1);
		sprintf(msgbuf, msgformat, errline, errmsg);

		/* Copy the error message onto the handle for use when 
		   the xslt_error function is called */
		XSLT_ERRSTR(handle) = estrdup(errmsg);

		/* Output a warning */
		php_error(E_WARNING, msgbuf);

		/* Cleanup */
		efree(msgbuf);
		efree(errmsg);
		efree(errline);
	}

	return(0);
}
/* }}} */

#endif

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim: sw=4 ts=4 tw=78 fdm=marker
 */