-
Notifications
You must be signed in to change notification settings - Fork 1
/
tclxml.c
executable file
·3706 lines (3129 loc) · 95.6 KB
/
tclxml.c
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
/*
* tclxml.c --
*
* Entry point for XML parsers, DOM and XSLT.
*
* Copyright (c) 2005-2007 Steve Ball, explain
* http://www.explain.com.au/
* Copyright (c) 1998-2004 Steve Ball, Zveno Pty Ltd
*
* See the file "LICENSE" for information on usage and
* redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* $Id: tclxml.c,v 1.32.2.1 2005/12/28 06:49:50 balls Exp $
*
*/
#include <tclxml/tclxml.h>
#include <tclxml-libxml2/tclxml-libxml2.h>
#include <tcldom-libxml2/tcldom-libxml2.h>
#include <tclxslt/tclxslt.h>
#include <string.h>
int Tcldom_libxml2_Init ( Tcl_Interp *interp );
#define TCL_DOES_STUBS \
(TCL_MAJOR_VERSION > 8 || TCL_MAJOR_VERSION == 8 && (TCL_MINOR_VERSION > 1 || \
(TCL_MINOR_VERSION == 1 && TCL_RELEASE_LEVEL == TCL_FINAL_RELEASE)))
#ifdef USE_TCLXML_STUBS
#ifndef TCLXML_DOES_STUBS
# define TCLXML_DOES_STUBS TCL_DOES_STUBS
#endif /* USE_TCLXML_STUBS */
#endif /* TCL_DOES_STUBS */
/*
* The structure below is used to manage package options.
*/
typedef struct ThreadSpecificData {
int initialized;
TclXML_ParserClassInfo *defaultParser; /* Current default parser */
Tcl_HashTable *registeredParsers; /* All known parser classes */
/*
* Retain a pointer to the whitespace variable
*/
Tcl_Obj *whitespaceRE;
/*
* Counter to generate unique command names
*/
int uniqueCounter;
/*
* Callback for external entity resolution
*/
Tcl_Obj *externalentitycmd;
Tcl_Interp *interp;
} ThreadSpecificData;
static Tcl_ThreadDataKey dataKey;
/* This string is a backup. Value should be defined in xml package. */
static char whitespace[] = " \t\r\n";
/*
* Configuration option tables
*/
static CONST84 char *globalConfigureSwitches[] = {
"-externalentitycommand",
(char *) NULL
};
enum globalConfigureSwitches {
TCLXML_GLOBAL_EXTERNALENTITYCOMMAND
};
static CONST84 char *instanceConfigureSwitches[] = {
"-final",
"-validate",
"-baseurl",
"-baseuri",
"-encoding",
"-elementstartcommand",
"-elementendcommand",
"-characterdatacommand",
"-processinginstructioncommand",
"-defaultcommand",
"-unparsedentitydeclcommand",
"-notationdeclcommand",
"-externalentitycommand",
"-unknownencodingcommand",
"-commentcommand",
"-notstandalonecommand",
"-startcdatasectioncommand",
"-endcdatasectioncommand",
"-defaultexpandinternalentities",
"-elementdeclcommand",
"-attlistdeclcommand",
"-startdoctypedeclcommand",
"-enddoctypedeclcommand",
"-paramentityparsing",
"-ignorewhitespace",
"-reportempty",
"-entitydeclcommand", /* added to avoid exception */
"-parameterentitydeclcommand", /* added to avoid exception */
"-doctypecommand", /* added to avoid exception */
"-entityreferencecommand", /* added to avoid exception */
"-xmldeclcommand", /* added to avoid exception */
(char *) NULL
};
enum instanceConfigureSwitches {
TCLXML_FINAL, TCLXML_VALIDATE, TCLXML_BASEURL, TCLXML_BASEURI,
TCLXML_ENCODING,
TCLXML_ELEMENTSTARTCMD, TCLXML_ELEMENTENDCMD,
TCLXML_DATACMD, TCLXML_PICMD,
TCLXML_DEFAULTCMD,
TCLXML_UNPARSEDENTITYCMD, TCLXML_NOTATIONCMD,
TCLXML_EXTERNALENTITYCMD, TCLXML_UNKNOWNENCODINGCMD,
TCLXML_COMMENTCMD, TCLXML_NOTSTANDALONECMD,
TCLXML_STARTCDATASECTIONCMD, TCLXML_ENDCDATASECTIONCMD,
TCLXML_DEFAULTEXPANDINTERNALENTITIES,
TCLXML_ELEMENTDECLCMD, TCLXML_ATTLISTDECLCMD,
TCLXML_STARTDOCTYPEDECLCMD, TCLXML_ENDDOCTYPEDECLCMD,
TCLXML_PARAMENTITYPARSING,
TCLXML_NOWHITESPACE,
TCLXML_REPORTEMPTY,
TCLXML_ENTITYDECLCMD,
TCLXML_PARAMENTITYDECLCMD,
TCLXML_DOCTYPECMD,
TCLXML_ENTITYREFCMD,
TCLXML_XMLDECLCMD
};
/*
* Prototypes for procedures defined later in this file:
*/
static void TclXMLInstanceDeleteCmd _ANSI_ARGS_((ClientData clientData));
static int TclXMLDestroyParserInstance _ANSI_ARGS_((TclXML_Info *xmlinfo));
static int TclXMLInstanceCmd _ANSI_ARGS_((ClientData dummy,
Tcl_Interp *interp, int objc, struct Tcl_Obj *CONST objv[]));
static int TclXMLCreateParserCmd _ANSI_ARGS_((ClientData dummy,
Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]));
static int TclXMLParserClassCmd _ANSI_ARGS_((ClientData dummy,
Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]));
static int TclXMLResetParser _ANSI_ARGS_((Tcl_Interp *interp, TclXML_Info *xmlinfo));
static int TclXMLConfigureCmd _ANSI_ARGS_((ClientData dummy,
Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]));
static Tcl_Obj* FindUniqueCmdName _ANSI_ARGS_((Tcl_Interp *interp));
static int TclXMLInstanceConfigure _ANSI_ARGS_((Tcl_Interp *interp,
TclXML_Info *xmlinfo, int objc, Tcl_Obj *CONST objv[]));
static int TclXMLCget _ANSI_ARGS_((Tcl_Interp *interp,
TclXML_Info *xmlinfo, int objc, Tcl_Obj *CONST objv[]));
static int TclXMLConfigureParserInstance _ANSI_ARGS_((
TclXML_Info *xmlinfo, Tcl_Obj *option, Tcl_Obj *value));
static int TclXMLGet _ANSI_ARGS_((Tcl_Interp *interp,
TclXML_Info *xmlinfo, int objc, Tcl_Obj *CONST objv[]));
static int TclXMLParse _ANSI_ARGS_((Tcl_Interp *interp,
TclXML_Info *xmlinfo, char *data, int len));
static void TclXMLDispatchPCDATA _ANSI_ARGS_((TclXML_Info *xmlinfo));
#if (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION == 0)
/*
*----------------------------------------------------------------------------
*
* Tcl_GetString --
*
* Compatibility routine for Tcl 8.0
*
* Results:
* String representation of object..
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
static char *
Tcl_GetString (obj)
Tcl_Obj *obj; /* Object to retrieve string from. */
{
char *s;
int i;
s = Tcl_GetStringFromObj(obj, &i);
return s;
}
#endif /* TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION == 0 */
/*
*----------------------------------------------------------------------------
*
* Tclxml_Init --
*
* Initialisation routine for loadable module.
* Also calls the initialisation routines for TclDOM and TclXSLT,
* as these were originally separate modules.
*
* Results:
* None.
*
* Side effects:
* Creates commands in the interpreter,
* loads xml, dom and xslt packages.
*
*----------------------------------------------------------------------------
*/
int
Tclxml_Init (interp)
Tcl_Interp *interp; /* Interpreter to initialise. */
{
ThreadSpecificData *tsdPtr;
#ifdef USE_TCL_STUBS
if (Tcl_InitStubs(interp, "8.1", 0) == NULL) {
return TCL_ERROR;
}
#endif
tsdPtr = (ThreadSpecificData *) Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
tsdPtr->initialized = 1;
tsdPtr->defaultParser = NULL;
tsdPtr->uniqueCounter = 0;
tsdPtr->whitespaceRE = Tcl_GetVar2Ex(interp, "::xml::Wsp", NULL, TCL_GLOBAL_ONLY);
if (tsdPtr->whitespaceRE == NULL) {
tsdPtr->whitespaceRE = Tcl_SetVar2Ex(interp, "::xml::Wsp", NULL, Tcl_NewStringObj(whitespace, -1), TCL_GLOBAL_ONLY|TCL_LEAVE_ERR_MSG);
if (tsdPtr->whitespaceRE == NULL) {
return TCL_ERROR;
}
}
Tcl_IncrRefCount(tsdPtr->whitespaceRE);
tsdPtr->registeredParsers = (Tcl_HashTable *) Tcl_Alloc(sizeof(Tcl_HashTable));
Tcl_InitHashTable(tsdPtr->registeredParsers, TCL_STRING_KEYS);
tsdPtr->externalentitycmd = NULL;
tsdPtr->interp = interp;
Tcl_CreateObjCommand(interp, "xml::configure", TclXMLConfigureCmd, NULL, NULL);
Tcl_CreateObjCommand(interp, "xml::parser", TclXMLCreateParserCmd, NULL, NULL);
Tcl_CreateObjCommand(interp, "xml::parserclass", TclXMLParserClassCmd, NULL, NULL);
if (Tclxml_libxml2_Init(interp) != TCL_OK) {
return TCL_ERROR;
}
if (Tcldom_libxml2_Init(interp) != TCL_OK) {
return TCL_ERROR;
}
if (Tclxslt_libxslt_Init(interp) != TCL_OK) {
return TCL_ERROR;
}
#if TCLXML_DOES_STUBS
{
extern TclxmlStubs tclxmlStubs;
if (Tcl_PkgProvideEx(interp, "xml::c", TCLXML_VERSION,
(ClientData) &tclxmlStubs) != TCL_OK) {
return TCL_ERROR;
}
}
#else
if (Tcl_PkgProvide(interp, "xml::c", TCLXML_VERSION) != TCL_OK) {
return TCL_ERROR;
}
#endif
return TCL_OK;
}
/*
*----------------------------------------------------------------------------
*
* Tclxml_SafeInit --
*
* Initialisation routine for loadable module in a safe interpreter.
*
* Results:
* None.
*
* Side effects:
* Creates commands in the interpreter,
* loads xml package.
*
*----------------------------------------------------------------------------
*/
int
Tclxml_SafeInit (interp)
Tcl_Interp *interp; /* Interpreter to initialise. */
{
return Tclxml_Init(interp);
}
/*
*----------------------------------------------------------------------------
*
* TclXMLConfigureCmd --
*
* Command for xml::configure command.
*
* Results:
* Depends on method.
*
* Side effects:
* Depends on method.
*
*----------------------------------------------------------------------------
*/
static int
TclXMLConfigureCmd(clientData, interp, objc, objv)
ClientData clientData;
Tcl_Interp *interp;
int objc;
Tcl_Obj *CONST objv[];
{
ThreadSpecificData *tsdPtr = (ThreadSpecificData *) Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
int index;
if (objc < 3) {
Tcl_SetResult(interp, "must specify option", NULL);
return TCL_ERROR;
} else if (objc == 3) {
/* TODO: retrieve option's value */
return TCL_OK;
} else if (objc % 2 == 1) {
Tcl_SetResult(interp, "value for option missing", NULL);
return TCL_ERROR;
}
for (objc -= 2, objv += 2; objc; objc -= 2, objv += 2) {
if (Tcl_GetIndexFromObj(interp, objv[0], globalConfigureSwitches,
"switch", 0, &index) != TCL_OK) {
return TCL_ERROR;
}
switch ((enum globalConfigureSwitches) index) {
case TCLXML_GLOBAL_EXTERNALENTITYCOMMAND:
tsdPtr->externalentitycmd = objv[1];
Tcl_IncrRefCount(tsdPtr->externalentitycmd);
break;
default:
return TCL_ERROR;
}
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------------
*
* TclXMLParserClassCmd --
*
* Command for xml::parserclass command.
*
* Results:
* Depends on method.
*
* Side effects:
* Depends on method.
*
*----------------------------------------------------------------------------
*/
static int
TclXMLParserClassCmd(clientData, interp, objc, objv)
ClientData clientData;
Tcl_Interp *interp;
int objc;
Tcl_Obj *CONST objv[];
{
ThreadSpecificData *tsdPtr = (ThreadSpecificData *) Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
TclXML_ParserClassInfo *classinfo;
int method, index;
Tcl_Obj *listPtr;
Tcl_HashEntry *entryPtr;
Tcl_HashSearch search;
static CONST84 char *methods[] = {
"create", "destroy", "info",
NULL
};
enum methods {
TCLXML_CREATE, TCLXML_DESTROY, TCLXML_INFO
};
static CONST84 char *createOptions[] = {
"-createcommand", "-createentityparsercommand",
"-parsecommand", "-configurecommand",
"-deletecommand", "-resetcommand",
NULL
};
enum createOptions {
TCLXML_CREATEPROC, TCLXML_CREATE_ENTITY_PARSER,
TCLXML_PARSEPROC, TCLXML_CONFIGUREPROC,
TCLXML_DELETEPROC, TCLXML_RESETPROC
};
static CONST84 char *infoMethods[] = {
"names", "default",
NULL
};
enum infoMethods {
TCLXML_INFO_NAMES, TCLXML_INFO_DEFAULT
};
if (objc < 2) {
Tcl_WrongNumArgs(interp, 1, objv, "method ?args?");
return TCL_ERROR;
}
if (Tcl_GetIndexFromObj(interp, objv[1], methods,
"method", 0, &method) != TCL_OK) {
return TCL_ERROR;
}
switch ((enum methods) method) {
case TCLXML_CREATE:
if (objc < 3) {
Tcl_WrongNumArgs(interp, 2, objv, "create name ?args?");
return TCL_ERROR;
}
classinfo = (TclXML_ParserClassInfo *) Tcl_Alloc(sizeof(TclXML_ParserClassInfo));
classinfo->name = objv[2];
Tcl_IncrRefCount(classinfo->name);
classinfo->create = NULL;
classinfo->createCmd = NULL;
classinfo->createEntity = NULL;
classinfo->createEntityCmd = NULL;
classinfo->parse = NULL;
classinfo->parseCmd = NULL;
classinfo->configure = NULL;
classinfo->configureCmd = NULL;
classinfo->reset = NULL;
classinfo->resetCmd = NULL;
classinfo->destroy = NULL;
classinfo->destroyCmd = NULL;
objv += 3;
objc -= 3;
while (objc > 1) {
if (Tcl_GetIndexFromObj(interp, objv[0], createOptions,
"options", 0, &index) != TCL_OK) {
return TCL_ERROR;
}
Tcl_IncrRefCount(objv[1]);
switch ((enum createOptions) index) {
case TCLXML_CREATEPROC:
classinfo->createCmd = objv[1];
break;
case TCLXML_CREATE_ENTITY_PARSER:
classinfo->createEntityCmd = objv[1];
break;
case TCLXML_PARSEPROC:
classinfo->parseCmd = objv[1];
break;
case TCLXML_CONFIGUREPROC:
classinfo->configureCmd = objv[1];
break;
case TCLXML_RESETPROC:
classinfo->resetCmd = objv[1];
break;
case TCLXML_DELETEPROC:
classinfo->destroyCmd = objv[1];
break;
default:
Tcl_AppendResult(interp, "unknown option \"", Tcl_GetStringFromObj(objv[0], NULL), "\"", NULL);
Tcl_DecrRefCount(objv[1]);
Tcl_DecrRefCount(classinfo->name);
Tcl_Free((char *)classinfo);
return TCL_ERROR;
}
objc -= 2;
objv += 2;
}
if (TclXML_RegisterXMLParser(interp, classinfo) != TCL_OK) {
Tcl_Free((char *)classinfo);
return TCL_ERROR;
}
break;
case TCLXML_DESTROY:
/* Not yet implemented */
break;
case TCLXML_INFO:
if (objc < 3) {
Tcl_WrongNumArgs(interp, 2, objv, "method");
return TCL_ERROR;
}
if (Tcl_GetIndexFromObj(interp, objv[2], infoMethods,
"method", 0, &index) != TCL_OK) {
return TCL_ERROR;
}
switch ((enum infoMethods) index) {
case TCLXML_INFO_NAMES:
listPtr = Tcl_NewListObj(0, NULL);
entryPtr = Tcl_FirstHashEntry(tsdPtr->registeredParsers, &search);
while (entryPtr != NULL) {
Tcl_ListObjAppendElement(interp, listPtr, Tcl_NewStringObj(Tcl_GetHashKey(tsdPtr->registeredParsers, entryPtr), -1));
entryPtr = Tcl_NextHashEntry(&search);
}
Tcl_SetObjResult(interp, listPtr);
break;
case TCLXML_INFO_DEFAULT:
if (!tsdPtr->defaultParser) {
Tcl_SetResult(interp, "", NULL);
} else {
Tcl_SetObjResult(interp, tsdPtr->defaultParser->name);
}
break;
default:
Tcl_SetResult(interp, "unknown method", NULL);
return TCL_ERROR;
}
break;
default:
Tcl_SetResult(interp, "unknown method", NULL);
return TCL_ERROR;
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------------
*
* TclXML_RegisterXMLParser --
*
* Adds a new XML parser.
*
* Results:
* Standard Tcl return code.
*
* Side effects:
* New parser is available for use in parser instances.
*
*----------------------------------------------------------------------------
*/
int
TclXML_RegisterXMLParser(interp, classinfo)
Tcl_Interp *interp;
TclXML_ParserClassInfo *classinfo;
{
ThreadSpecificData *tsdPtr = (ThreadSpecificData *) Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
int new;
Tcl_HashEntry *entryPtr;
entryPtr = Tcl_CreateHashEntry(tsdPtr->registeredParsers, Tcl_GetStringFromObj(classinfo->name, NULL), &new);
if (!new) {
Tcl_Obj *ptr = Tcl_NewStringObj("parser class \"", -1);
Tcl_AppendObjToObj(ptr, classinfo->name);
Tcl_AppendObjToObj(ptr, Tcl_NewStringObj("\" already registered", -1));
Tcl_ResetResult(interp);
Tcl_SetObjResult(interp, ptr);
return TCL_ERROR;
}
Tcl_SetHashValue(entryPtr, (ClientData) classinfo);
/*
* Set default parser - last wins
*/
tsdPtr->defaultParser = classinfo;
return TCL_OK;
}
/*
*----------------------------------------------------------------------------
*
* TclXMLCreateParserCmd --
*
* Creation command for xml::parser command.
*
* Results:
* The name of the newly created parser instance.
*
* Side effects:
* This creates a parser instance.
*
*----------------------------------------------------------------------------
*/
static int
TclXMLCreateParserCmd(clientData, interp, objc, objv)
ClientData clientData;
Tcl_Interp *interp;
int objc;
Tcl_Obj *CONST objv[];
{
ThreadSpecificData *tsdPtr = (ThreadSpecificData *) Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
TclXML_Info *xmlinfo;
int found, i, index, poption;
static CONST84 char *switches[] = {
"-parser",
(char *) NULL
};
enum switches {
TCLXML_PARSER
};
if (tsdPtr == NULL) {
Tcl_SetObjResult(interp, Tcl_NewStringObj("TclXML package improperly initialised", -1));
return TCL_ERROR;
}
if (!tsdPtr->defaultParser) {
Tcl_SetResult(interp, "no parsers available", NULL);
return TCL_ERROR;
}
/*
* Create the data structures for this parser.
*/
if (!(xmlinfo = (TclXML_Info *) Tcl_Alloc(sizeof(TclXML_Info)))) {
Tcl_SetResult(interp, "unable to create parser", NULL);
return TCL_ERROR;
}
xmlinfo->interp = interp;
xmlinfo->clientData = NULL;
xmlinfo->base = NULL;
xmlinfo->encoding = Tcl_NewStringObj("utf-8", -1);
/*
* Find unique command name
*/
if (objc < 2) {
xmlinfo->name = FindUniqueCmdName(interp);
} else {
xmlinfo->name = objv[1];
if (*(Tcl_GetStringFromObj(xmlinfo->name, NULL)) != '-') {
Tcl_IncrRefCount(xmlinfo->name);
objv++;
objc--;
} else {
xmlinfo->name = FindUniqueCmdName(interp);
}
}
xmlinfo->validate = 0;
xmlinfo->elementstartcommand = NULL;
xmlinfo->elementstart = NULL;
xmlinfo->elementstartdata = 0;
xmlinfo->elementendcommand = NULL;
xmlinfo->elementend = NULL;
xmlinfo->elementenddata = 0;
xmlinfo->datacommand = NULL;
xmlinfo->cdatacb = NULL;
xmlinfo->cdatacbdata = 0;
xmlinfo->picommand = NULL;
xmlinfo->pi = NULL;
xmlinfo->pidata = 0;
xmlinfo->defaultcommand = NULL;
xmlinfo->defaultcb = NULL;
xmlinfo->defaultdata = 0;
xmlinfo->unparsedcommand = NULL;
xmlinfo->unparsed = NULL;
xmlinfo->unparseddata = 0;
xmlinfo->notationcommand = NULL;
xmlinfo->notation = NULL;
xmlinfo->notationdata = 0;
xmlinfo->entitycommand = NULL;
xmlinfo->entity = NULL;
xmlinfo->entitydata = 0;
xmlinfo->unknownencodingcommand = NULL;
xmlinfo->unknownencoding = NULL;
xmlinfo->unknownencodingdata = 0;
/* [email protected] */
xmlinfo->commentCommand = NULL;
xmlinfo->comment = NULL;
xmlinfo->commentdata = 0;
xmlinfo->notStandaloneCommand = NULL;
xmlinfo->notStandalone = NULL;
xmlinfo->notstandalonedata = 0;
xmlinfo->elementDeclCommand = NULL;
xmlinfo->elementDecl = NULL;
xmlinfo->elementdecldata = 0;
xmlinfo->attlistDeclCommand = NULL;
xmlinfo->attlistDecl = NULL;
xmlinfo->attlistdecldata = 0;
xmlinfo->startDoctypeDeclCommand = NULL;
xmlinfo->startDoctypeDecl = NULL;
xmlinfo->startdoctypedecldata = 0;
xmlinfo->endDoctypeDeclCommand = NULL;
xmlinfo->endDoctypeDecl = NULL;
xmlinfo->enddoctypedecldata = 0;
#ifdef TCLXML_CDATASECTIONS
xmlinfo->startCDATASectionCommand = NULL;
xmlinfo->startCDATASection = NULL;
xmlinfo->startcdatasectiondata = 0;
xmlinfo->endCdataSectionCommand = NULL;
xmlinfo->endCdataSection = NULL;
xmlinfo->endcdatasectiondata = 0;
#endif
/*
* Options may include an explicit desired parser class
*
* SF TclXML Bug 513909 ...
* Start search at first argument! If there was a parser name
* specified we already skipped over it.
*
* Changing the search. Do not stop at the first occurence of
* "-parser". There can be more than one instance of the option in
* the argument list and it is the last instance that counts.
*/
found = 0;
i = 1;
poption = -1;
while (i < objc) {
Tcl_ResetResult (interp);
if (Tcl_GetIndexFromObj(interp, objv[i], switches, "option", 0, &index) == TCL_OK) {
poption = i;
found = 1;
}
i += 2;
}
Tcl_ResetResult (interp);
if (found) {
Tcl_HashEntry *pentry;
if (poption == (objc - 1)) {
Tcl_SetResult(interp, "no value for option", NULL);
goto error;
}
/*
* Use given parser class
*/
pentry = Tcl_FindHashEntry(tsdPtr->registeredParsers,
Tcl_GetStringFromObj(objv[poption + 1],
NULL));
if (pentry != NULL) {
xmlinfo->parserClass = Tcl_GetHashValue(pentry);
} else {
Tcl_AppendResult(interp, "no such parser class \"",
Tcl_GetStringFromObj(objv[poption + 1], NULL),
"\"", NULL);
goto error;
}
} else {
/*
* Use default parser
*/
xmlinfo->parserClass = tsdPtr->defaultParser;
}
if (TclXMLResetParser(interp, xmlinfo) != TCL_OK) {
/* this may leak memory...
Tcl_Free((char *)xmlinfo);
*/
return TCL_ERROR;
}
/*
* Register a Tcl command for this parser instance.
*/
Tcl_CreateObjCommand(interp, Tcl_GetStringFromObj(xmlinfo->name, NULL),
TclXMLInstanceCmd, (ClientData) xmlinfo, TclXMLInstanceDeleteCmd);
/*
* Handle configuration options
*
* SF TclXML Bug 513909 ...
* Note: If the caller used "-parser" to specify a parser class we
* have to take care that it and its argument are *not* seen by
* "TclXMLInstanceConfigure" because this option is not allowed
* during general configuration.
*/
if (objc > 1) {
if (found) {
/*
* The options contained at least one instance of "-parser
* class". We now go through the whole list of arguments and
* build a new list which contains only the non-"-parser"
* switches. The 'ResetResult' takes care of clearing the
* interpreter result before "Tcl_GetIndexFromObj" tries to
* use it again.
*/
int res;
int cfgc = 0;
Tcl_Obj** cfgv = (Tcl_Obj**) Tcl_Alloc (objc * sizeof (Tcl_Obj*));
i = 1;
while (i < objc) {
Tcl_ResetResult (interp);
if (Tcl_GetIndexFromObj(interp, objv[i], switches, "option", 0, &index) == TCL_OK) {
/* Ignore "-parser" during copying */
i += 2;
continue;
}
cfgv [cfgc] = objv [i]; i++ ; cfgc++ ; /* copy option ... */
cfgv [cfgc] = objv [i]; i++ ; cfgc++ ; /* ... and value */
}
Tcl_ResetResult (interp);
res = TclXMLInstanceConfigure(interp, xmlinfo, cfgc, cfgv);
Tcl_Free ((char*) cfgv);
if (res == TCL_ERROR) {
return TCL_ERROR;
}
} else {
/*
* The options contained no "-parser class" specification. We
* can propagate it unchanged.
*/
if (TclXMLInstanceConfigure(interp, xmlinfo, objc - 1, objv + 1) == TCL_ERROR) {
return TCL_ERROR;
}
}
}
Tcl_SetObjResult(interp, xmlinfo->name);
return TCL_OK;
error:
/* this may leak memory
Tcl_Free((char*)xmlinfo);
*/
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------------
*
* FindUniqueCmdName --
*
* Generate new command name in caller's namespace.
*
* Results:
* Returns newly allocated Tcl object containing name.
*
* Side effects:
* Allocates Tcl object.
*
*----------------------------------------------------------------------------
*/
static Tcl_Obj *
FindUniqueCmdName(interp)
Tcl_Interp *interp;
{
ThreadSpecificData *tsdPtr = (ThreadSpecificData *) Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
Tcl_Obj *name;
Tcl_CmdInfo cmdinfo;
char s[20];
name = Tcl_NewObj();
Tcl_IncrRefCount(name);
do {
sprintf(s, "xmlparser%d", tsdPtr->uniqueCounter++);
Tcl_SetStringObj(name, s, -1);
} while (Tcl_GetCommandInfo(interp, Tcl_GetStringFromObj(name, NULL), &cmdinfo));
return name;
}
/*
*----------------------------------------------------------------------------
*
* TclXMLResetParser --
*
* (Re-)Initialise the parser instance structure.
*
* Results:
* Parser made ready for parsing.
*
* Side effects:
* Destroys and creates a parser instance.
* Modifies TclXML_Info fields.
*
*----------------------------------------------------------------------------
*/
static int
TclXMLResetParser(interp, xmlinfo)
Tcl_Interp *interp;
TclXML_Info *xmlinfo;
{
TclXML_ParserClassInfo *classInfo = (TclXML_ParserClassInfo *) xmlinfo->parserClass;
if (xmlinfo->base) {
Tcl_DecrRefCount(xmlinfo->base);
xmlinfo->base = NULL;
}
xmlinfo->final = 1;
xmlinfo->status = TCL_OK;
xmlinfo->result = NULL;
xmlinfo->continueCount = 0;
xmlinfo->context = NULL;
xmlinfo->cdata = NULL;
xmlinfo->nowhitespace = 0;
xmlinfo->reportempty = 0;
xmlinfo->expandinternalentities = 1;
xmlinfo->paramentities = 1;
if (classInfo->reset) {
if ((*classInfo->reset)((ClientData) xmlinfo) != TCL_OK) {
return TCL_ERROR;
}
} else if (classInfo->resetCmd) {
Tcl_Obj *cmdPtr = Tcl_DuplicateObj(classInfo->resetCmd);
int result;
Tcl_IncrRefCount(cmdPtr);
Tcl_Preserve((ClientData) interp);
Tcl_ListObjAppendElement(interp, cmdPtr, xmlinfo->name);
result = Tcl_GlobalEvalObj(xmlinfo->interp, cmdPtr);
Tcl_DecrRefCount(cmdPtr);
Tcl_Release((ClientData) interp);
if (result != TCL_OK) {
Tcl_Free((char*)xmlinfo);
return TCL_ERROR;
}
} else if (classInfo->create) {
/*
* Otherwise destroy and then create a fresh parser instance
*/
/*
* Destroy the old parser instance, if it exists
* Could probably just reset it, but this approach
* is pretty much guaranteed to work.
*/
if (TclXMLDestroyParserInstance(xmlinfo) != TCL_OK) {
return TCL_ERROR;
}
/*
* Directly invoke the create routine
*/
if ((xmlinfo->clientData = (*classInfo->create)(interp, xmlinfo)) == NULL) {
Tcl_Free((char*)xmlinfo);
return TCL_ERROR;
}
} else if (classInfo->createCmd) {
Tcl_Obj *cmdPtr = Tcl_DuplicateObj(classInfo->createCmd);
int result, i;
Tcl_IncrRefCount(cmdPtr);
Tcl_Preserve((ClientData) interp);
Tcl_ListObjAppendElement(interp, cmdPtr, xmlinfo->name);