-
Notifications
You must be signed in to change notification settings - Fork 429
/
Copy pathtidylib.c
2766 lines (2421 loc) · 78.5 KB
/
tidylib.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
/* tidylib.c -- internal library definitions
(c) 1998-2008 (W3C) MIT, ERCIM, Keio University
See tidy.h for the copyright notice.
Defines HTML Tidy API implemented by tidy library.
Very rough initial cut for discussion purposes.
Public interface is const-correct and doesn't explicitly depend
on any globals. Thus, thread-safety may be introduced w/out
changing the interface.
Looking ahead to a C++ wrapper, C functions always pass
this-equivalent as 1st arg.
Created 2001-05-20 by Charles Reitzel
*/
#include <errno.h>
#include "tidy-int.h"
#include "parser.h"
#include "clean.h"
#include "gdoc.h"
#include "config.h"
#include "message.h"
#include "messageobj.h"
#include "pprint.h"
#include "entities.h"
#include "tmbstr.h"
#include "utf8.h"
#include "mappedio.h"
#include "language.h"
#include "attrs.h"
#include "sprtf.h"
#if SUPPORT_LOCALIZATIONS
# include "stdlib.h"
# include "locale.h"
#endif
/* Create/Destroy a Tidy "document" object */
static TidyDocImpl* tidyDocCreate( TidyAllocator *allocator );
static void tidyDocRelease( TidyDocImpl* impl );
static int tidyDocStatus( TidyDocImpl* impl );
/* Parse Markup */
static int tidyDocParseFile( TidyDocImpl* impl, ctmbstr htmlfil );
static int tidyDocParseStdin( TidyDocImpl* impl );
static int tidyDocParseString( TidyDocImpl* impl, ctmbstr content );
static int tidyDocParseBuffer( TidyDocImpl* impl, TidyBuffer* inbuf );
static int tidyDocParseSource( TidyDocImpl* impl, TidyInputSource* docIn );
/* Execute post-parse diagnostics and cleanup.
** Note, the order is important. You will get different
** results from the diagnostics depending on if they are run
** pre-or-post repair.
*/
static int tidyDocRunDiagnostics( TidyDocImpl* doc );
static void tidyDocReportDoctype( TidyDocImpl* doc );
static int tidyDocCleanAndRepair( TidyDocImpl* doc );
/* Save cleaned up file to file/buffer/sink */
static int tidyDocSaveFile( TidyDocImpl* impl, ctmbstr htmlfil );
static int tidyDocSaveStdout( TidyDocImpl* impl );
static int tidyDocSaveString( TidyDocImpl* impl, tmbstr buffer, uint* buflen );
static int tidyDocSaveBuffer( TidyDocImpl* impl, TidyBuffer* outbuf );
static int tidyDocSaveSink( TidyDocImpl* impl, TidyOutputSink* docOut );
static int tidyDocSaveStream( TidyDocImpl* impl, StreamOut* out );
/* Tidy public interface
**
** Most functions return an integer:
**
** 0 -> SUCCESS
** >0 -> WARNING
** <0 -> ERROR
**
*/
TidyDoc TIDY_CALL tidyCreate(void)
{
TidyDocImpl* impl = tidyDocCreate( &TY_(g_default_allocator) );
return tidyImplToDoc( impl );
}
TidyDoc TIDY_CALL tidyCreateWithAllocator( TidyAllocator *allocator )
{
TidyDocImpl* impl = tidyDocCreate( allocator );
return tidyImplToDoc( impl );
}
void TIDY_CALL tidyRelease( TidyDoc tdoc )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
tidyDocRelease( impl );
}
TidyDocImpl* tidyDocCreate( TidyAllocator *allocator )
{
TidyDocImpl* doc = (TidyDocImpl*)TidyAlloc( allocator, sizeof(TidyDocImpl) );
TidyClearMemory( doc, sizeof(*doc) );
doc->allocator = allocator;
TY_(InitMap)();
TY_(InitTags)( doc );
TY_(InitAttrs)( doc );
TY_(InitConfig)( doc );
TY_(InitPrintBuf)( doc );
TY_(InitParserStack)( doc );
/* Set the locale for tidy's output. This both configures
** LibTidy to use the environment's locale as well as the
** standard library.
*/
#if SUPPORT_LOCALIZATIONS
if ( TY_(tidyGetLanguageSetByUser)() == no )
{
if( ! TY_(tidySetLanguage)( getenv( "LC_MESSAGES" ) ) )
{
if( ! TY_(tidySetLanguage)( getenv( "LANG" ) ) )
{
/*\
* Is. #770 #783 #780 #790 and maybe others -
* TY_(tidySetLanguage)( setlocale( LC_ALL, "" ) );
* this seems a 'bad' choice!
\*/
}
}
}
#endif
/* By default, wire tidy messages to standard error.
** Document input will be set by parsing routines.
** Document output will be set by pretty print routines.
** Config input will be set by config parsing routines.
** But we need to start off with a way to report errors.
*/
doc->errout = TY_(StdErrOutput)();
return doc;
}
void tidyDocRelease( TidyDocImpl* doc )
{
/* doc in/out opened and closed by parse/print routines */
if ( doc )
{
assert( doc->docIn == NULL );
assert( doc->docOut == NULL );
TY_(ReleaseStreamOut)( doc, doc->errout );
doc->errout = NULL;
TY_(FreePrintBuf)( doc );
TY_(FreeNode)(doc, &doc->root);
TidyClearMemory(&doc->root, sizeof(Node));
if (doc->givenDoctype)
TidyDocFree(doc, doc->givenDoctype);
TY_(FreeConfig)( doc );
TY_(FreeAttrTable)( doc );
TY_(FreeAttrPriorityList)( doc );
TY_(FreeMutedMessageList( doc ));
TY_(FreeTags)( doc );
/*\
* Issue #186 - Now FreeNode depend on the doctype, so the lexer is needed
* to determine which hash is to be used, so free it last.
\*/
TY_(FreeLexer)( doc );
TY_(FreeParserStack)( doc );
TidyDocFree( doc, doc );
}
}
/* Let application store a chunk of data w/ each Tidy tdocance.
** Useful for callbacks.
*/
void TIDY_CALL tidySetAppData( TidyDoc tdoc, void* appData )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
impl->appData = appData;
}
void* TIDY_CALL tidyGetAppData( TidyDoc tdoc )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
return impl->appData;
return NULL;
}
ctmbstr TIDY_CALL tidyReleaseDate(void)
{
return TY_(ReleaseDate)();
}
ctmbstr TIDY_CALL tidyLibraryVersion(void)
{
return TY_(tidyLibraryVersion)();
}
ctmbstr TIDY_CALL tidyPlatform(void)
{
#ifdef PLATFORM_NAME
return PLATFORM_NAME;
#else
return NULL;
#endif
}
/* Get/set configuration options
*/
Bool TIDY_CALL tidySetOptionCallback( TidyDoc tdoc, TidyOptCallback pOptCallback )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
{
impl->pOptCallback = pOptCallback;
return yes;
}
return no;
}
Bool TIDY_CALL tidySetConfigCallback(TidyDoc tdoc, TidyConfigCallback pConfigCallback)
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
{
impl->pConfigCallback = pConfigCallback;
return yes;
}
return no;
}
Bool TIDY_CALL tidySetConfigChangeCallback(TidyDoc tdoc, TidyConfigChangeCallback pCallback)
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
{
impl->pConfigChangeCallback = pCallback;
return yes;
}
return no;
}
int TIDY_CALL tidyLoadConfig( TidyDoc tdoc, ctmbstr cfgfil )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
return TY_(ParseConfigFile)( impl, cfgfil );
return -EINVAL;
}
int TIDY_CALL tidyLoadConfigEnc( TidyDoc tdoc, ctmbstr cfgfil, ctmbstr charenc )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
return TY_(ParseConfigFileEnc)( impl, cfgfil, charenc );
return -EINVAL;
}
int TIDY_CALL tidySetCharEncoding( TidyDoc tdoc, ctmbstr encnam )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
{
int enc = TY_(CharEncodingId)( impl, encnam );
if ( enc >= 0 && TY_(AdjustCharEncoding)(impl, enc) )
return 0;
TY_(ReportBadArgument)( impl, "char-encoding" );
}
return -EINVAL;
}
int TIDY_CALL tidySetInCharEncoding( TidyDoc tdoc, ctmbstr encnam )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
{
int enc = TY_(CharEncodingId)( impl, encnam );
if ( enc >= 0 && TY_(SetOptionInt)( impl, TidyInCharEncoding, enc ) )
return 0;
TY_(ReportBadArgument)( impl, "in-char-encoding" );
}
return -EINVAL;
}
int TIDY_CALL tidySetOutCharEncoding( TidyDoc tdoc, ctmbstr encnam )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
{
int enc = TY_(CharEncodingId)( impl, encnam );
if ( enc >= 0 && TY_(SetOptionInt)( impl, TidyOutCharEncoding, enc ) )
return 0;
TY_(ReportBadArgument)( impl, "out-char-encoding" );
}
return -EINVAL;
}
TidyOptionId TIDY_CALL tidyOptGetIdForName( ctmbstr optnam )
{
const TidyOptionImpl* option = TY_(lookupOption)( optnam );
if ( option )
return option->id;
return N_TIDY_OPTIONS; /* Error */
}
TidyIterator TIDY_CALL tidyGetOptionList( TidyDoc tdoc )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
return TY_(getOptionList)( impl );
return (TidyIterator) -1;
}
TidyOption TIDY_CALL tidyGetNextOption( TidyDoc tdoc, TidyIterator* pos )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
const TidyOptionImpl* option = NULL;
if ( impl )
option = TY_(getNextOption)( impl, pos );
else if ( pos )
*pos = 0;
return tidyImplToOption( option );
}
TidyOption TIDY_CALL tidyGetOption( TidyDoc ARG_UNUSED(tdoc), TidyOptionId optId )
{
const TidyOptionImpl* option = TY_(getOption)( optId );
return tidyImplToOption( option );
}
TidyOption TIDY_CALL tidyGetOptionByName( TidyDoc ARG_UNUSED(doc), ctmbstr optnam )
{
const TidyOptionImpl* option = TY_(lookupOption)( optnam );
return tidyImplToOption( option );
}
TidyOptionId TIDY_CALL tidyOptGetId( TidyOption topt )
{
const TidyOptionImpl* option = tidyOptionToImpl( topt );
if ( option )
return option->id;
return N_TIDY_OPTIONS;
}
ctmbstr TIDY_CALL tidyOptGetName( TidyOption topt )
{
const TidyOptionImpl* option = tidyOptionToImpl( topt );
if ( option )
return option->name;
return NULL;
}
TidyOptionType TIDY_CALL tidyOptGetType( TidyOption topt )
{
const TidyOptionImpl* option = tidyOptionToImpl( topt );
if ( option )
return option->type;
return (TidyOptionType) -1;
}
Bool TIDY_CALL tidyOptionIsList( TidyOption opt )
{
const TidyOptionImpl* option = tidyOptionToImpl( opt );
if ( option )
return TY_(getOptionIsList)( option->id );
return no;
}
TidyConfigCategory TIDY_CALL tidyOptGetCategory( TidyOption topt )
{
const TidyOptionImpl* option = tidyOptionToImpl( topt );
if ( option )
return option->category;
return (TidyConfigCategory) -1;
}
ctmbstr TIDY_CALL tidyOptGetDefault( TidyOption topt )
{
const TidyOptionImpl* option = tidyOptionToImpl( topt );
/* Special case for TidyDoctype, because it is declared as string */
if ( option && option->id == TidyDoctype )
{
const TidyOptionImpl* newopt = TY_(getOption)( TidyDoctypeMode );
return TY_(GetPickListLabelForPick)( TidyDoctypeMode, newopt->dflt );
}
if ( option && option->type == TidyString )
return option->pdflt; /* Issue #306 - fix an old typo hidden by a cast! */
return NULL;
}
ulong TIDY_CALL tidyOptGetDefaultInt( TidyOption topt )
{
const TidyOptionImpl* option = tidyOptionToImpl( topt );
if ( option && option->type != TidyString )
return option->dflt;
/* Special case for TidyDoctype, because it has a picklist */
if ( option->id == TidyDoctype )
{
const TidyOptionImpl* newopt = TY_(getOption)( TidyDoctypeMode );
return newopt->dflt;
}
return ~0U;
}
Bool TIDY_CALL tidyOptGetDefaultBool( TidyOption topt )
{
const TidyOptionImpl* option = tidyOptionToImpl( topt );
if ( option && option->type != TidyString )
return ( option->dflt ? yes : no );
return no;
}
Bool TIDY_CALL tidyOptIsReadOnly( TidyOption topt )
{
const TidyOptionImpl* option = tidyOptionToImpl( topt );
if ( option )
return ( option->parser == NULL );
return yes;
}
TidyIterator TIDY_CALL tidyOptGetPickList( TidyOption topt )
{
const TidyOptionImpl* option = tidyOptionToImpl( topt );
if ( option )
return TY_(getOptionPickList)( option );
return (TidyIterator) -1;
}
ctmbstr TIDY_CALL tidyOptGetNextPick( TidyOption topt, TidyIterator* pos )
{
const TidyOptionImpl* option = tidyOptionToImpl( topt );
if ( option )
return TY_(getNextOptionPick)( option, pos );
return NULL;
}
ctmbstr TIDY_CALL tidyOptGetValue( TidyDoc tdoc, TidyOptionId optId )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
ctmbstr optval = NULL;
if ( impl )
{
if ( optId == TidyDoctype )
{
/* Special case for TidyDoctype, because it has a picklist and is a string. */
uint pick = tidyOptGetInt( tdoc, TidyDoctypeMode );
if ( pick != TidyDoctypeUser )
{
optval = TY_(GetPickListLabelForPick)( TidyDoctypeMode, pick );
} else {
optval = cfgStr( impl, optId );
}
} else {
/* Standard case. */
optval = cfgStr( impl, optId );
}
}
return optval;
}
Bool TIDY_CALL tidyOptSetValue( TidyDoc tdoc, TidyOptionId optId, ctmbstr val )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
return TY_(ParseConfigValue)( impl, optId, val );
return no;
}
Bool TIDY_CALL tidyOptParseValue( TidyDoc tdoc, ctmbstr optnam, ctmbstr val )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
return TY_(ParseConfigOption)( impl, optnam, val );
return no;
}
ulong TIDY_CALL tidyOptGetInt( TidyDoc tdoc, TidyOptionId optId )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
ulong opti = 0;
if ( impl )
{
/* Special case for TidyDoctype, because it has a picklist */
if ( optId == TidyDoctype )
opti = cfg( impl, TidyDoctypeMode);
else
opti = cfg( impl, optId );
}
return opti;
}
Bool TIDY_CALL tidyOptSetInt( TidyDoc tdoc, TidyOptionId optId, ulong val )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
{
/* Special case for TidyDoctype, because it has a picklist */
if ( optId == TidyDoctype )
return TY_(SetOptionInt)( impl, TidyDoctypeMode, val );
else
return TY_(SetOptionInt)( impl, optId, val );
}
return no;
}
Bool TIDY_CALL tidyOptGetBool( TidyDoc tdoc, TidyOptionId optId )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
Bool optb = no;
if ( impl )
{
const TidyOptionImpl* option = TY_(getOption)( optId );
if ( option )
{
optb = cfgBool( impl, optId );
}
}
return optb;
}
Bool TIDY_CALL tidyOptSetBool( TidyDoc tdoc, TidyOptionId optId, Bool val )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
return TY_(SetOptionBool)( impl, optId, val );
return no;
}
ctmbstr TIDY_CALL tidyOptGetEncName( TidyDoc tdoc, TidyOptionId optId )
{
uint enc = tidyOptGetInt( tdoc, optId );
return TY_(CharEncodingOptName)( enc );
}
ctmbstr TIDY_CALL tidyOptGetCurrPick( TidyDoc tdoc, TidyOptionId optId )
{
uint pick = tidyOptGetInt( tdoc, optId );
return TY_(GetPickListLabelForPick)( optId, pick );
}
TidyIterator TIDY_CALL tidyOptGetDeclTagList( TidyDoc tdoc )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
TidyIterator declIter = 0;
if ( impl )
declIter = TY_(GetDeclaredTagList)( impl );
return declIter;
}
ctmbstr TIDY_CALL tidyOptGetNextDeclTag( TidyDoc tdoc, TidyOptionId optId,
TidyIterator* iter )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
ctmbstr tagnam = NULL;
if ( impl )
{
UserTagType tagtyp = tagtype_null;
if ( optId == TidyInlineTags )
tagtyp = tagtype_inline;
else if ( optId == TidyBlockTags )
tagtyp = tagtype_block;
else if ( optId == TidyEmptyTags )
tagtyp = tagtype_empty;
else if ( optId == TidyPreTags )
tagtyp = tagtype_pre;
if ( tagtyp != tagtype_null )
tagnam = TY_(GetNextDeclaredTag)( impl, tagtyp, iter );
}
return tagnam;
}
TidyIterator TIDY_CALL tidyOptGetPriorityAttrList( TidyDoc tdoc )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
return TY_(getPriorityAttrList)( impl );
return (TidyIterator) -1;
}
ctmbstr TIDY_CALL tidyOptGetNextPriorityAttr(TidyDoc tdoc, TidyIterator* iter )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
ctmbstr result = NULL;
if ( impl )
result = TY_(getNextPriorityAttr)( impl, iter );
else if ( iter )
*iter = 0;
return result;
}
TidyIterator TIDY_CALL tidyOptGetMutedMessageList( TidyDoc tdoc )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
return TY_(getMutedMessageList)( impl );
return (TidyIterator) -1;
}
ctmbstr TIDY_CALL tidyOptGetNextMutedMessage(TidyDoc tdoc, TidyIterator* iter )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
ctmbstr result = NULL;
if ( impl )
result = TY_(getNextMutedMessage)( impl, iter );
else if ( iter )
*iter = 0;
return result;
}
ctmbstr TIDY_CALL tidyOptGetDoc( TidyDoc ARG_UNUSED(tdoc), TidyOption opt )
{
const TidyOptionId optId = tidyOptGetId( opt );
return tidyLocalizedString(optId);
}
#if SUPPORT_CONSOLE_APP
/* TODO - GROUP ALL CONSOLE-ONLY FUNCTIONS */
TidyIterator TIDY_CALL tidyOptGetDocLinksList( TidyDoc ARG_UNUSED(tdoc), TidyOption opt )
{
const TidyOptionId optId = tidyOptGetId( opt );
const TidyOptionDoc* docDesc = TY_(OptGetDocDesc)( optId );
if (docDesc && docDesc->links)
return (TidyIterator)docDesc->links;
return (TidyIterator)NULL;
}
#endif /* SUPPORT_CONSOLE_APP */
TidyOption TIDY_CALL tidyOptGetNextDocLinks( TidyDoc tdoc, TidyIterator* pos )
{
const TidyOptionId* curr = (const TidyOptionId *)*pos;
TidyOption opt;
if (*curr == TidyUnknownOption)
{
*pos = (TidyIterator)NULL;
return (TidyOption)0;
}
opt = tidyGetOption(tdoc, *curr);
curr++;
*pos = (*curr == TidyUnknownOption ) ?
(TidyIterator)NULL:(TidyIterator)curr;
return opt;
}
int TIDY_CALL tidyOptSaveFile( TidyDoc tdoc, ctmbstr cfgfil )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
return TY_(SaveConfigFile)( impl, cfgfil );
return -EINVAL;
}
int TIDY_CALL tidyOptSaveSink( TidyDoc tdoc, TidyOutputSink* sink )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
return TY_(SaveConfigSink)( impl, sink );
return -EINVAL;
}
Bool TIDY_CALL tidyOptSnapshot( TidyDoc tdoc )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
{
TY_(TakeConfigSnapshot)( impl );
return yes;
}
return no;
}
Bool TIDY_CALL tidyOptResetToSnapshot( TidyDoc tdoc )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
{
TY_(ResetConfigToSnapshot)( impl );
return yes;
}
return no;
}
Bool TIDY_CALL tidyOptResetAllToDefault( TidyDoc tdoc )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
{
TY_(ResetConfigToDefault)( impl );
return yes;
}
return no;
}
Bool TIDY_CALL tidyOptResetToDefault( TidyDoc tdoc, TidyOptionId optId )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
return TY_(ResetOptionToDefault)( impl, optId );
return no;
}
Bool TIDY_CALL tidyOptDiffThanDefault( TidyDoc tdoc )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
return TY_(ConfigDiffThanDefault)( impl );
return no;
}
Bool TIDY_CALL tidyOptDiffThanSnapshot( TidyDoc tdoc )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
return TY_(ConfigDiffThanSnapshot)( impl );
return no;
}
Bool TIDY_CALL tidyOptCopyConfig( TidyDoc to, TidyDoc from )
{
TidyDocImpl* docTo = tidyDocToImpl( to );
TidyDocImpl* docFrom = tidyDocToImpl( from );
if ( docTo && docFrom )
{
TY_(CopyConfig)( docTo, docFrom );
return yes;
}
return no;
}
/* I/O and Message handling interface
**
** By default, Tidy will define, create and use instance of input and output
** handlers for standard C buffered I/O (i.e. FILE* stdin, FILE* stdout and
** FILE* stderr for content input, content output and diagnostic output,
** respectively. A FILE* cfgFile input handler will be used for config files.
** Command line options will just be set directly.
*/
void TIDY_CALL tidySetEmacsFile( TidyDoc tdoc, ctmbstr filePath )
{
tidyOptSetValue( tdoc, TidyEmacsFile, filePath );
}
ctmbstr TIDY_CALL tidyGetEmacsFile( TidyDoc tdoc )
{
return tidyOptGetValue( tdoc, TidyEmacsFile );
}
/* Use TidyReportFilter to filter messages by diagnostic level:
** info, warning, etc. Just set diagnostic output
** handler to redirect all diagnostics output. Return true
** to proceed with output, false to cancel.
*/
Bool TIDY_CALL tidySetReportFilter( TidyDoc tdoc, TidyReportFilter filt )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
{
impl->reportFilter = filt;
return yes;
}
return no;
}
/* tidySetReportCallback functions similar to TidyReportFilter, but provides the
* string version of the internal enum name so that LibTidy users can use
** the string as a lookup key for providing their own error localizations.
** See the string key definitions in tidyenum.h.
*/
Bool TIDY_CALL tidySetReportCallback( TidyDoc tdoc, TidyReportCallback filt )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
{
impl->reportCallback = filt;
return yes;
}
return no;
}
Bool TIDY_CALL tidySetMessageCallback( TidyDoc tdoc, TidyMessageCallback filt )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
{
impl->messageCallback = filt;
return yes;
}
return no;
}
TidyDoc TIDY_CALL tidyGetMessageDoc( TidyMessage tmessage )
{
TidyMessageImpl *message = tidyMessageToImpl(tmessage);
TidyDocImpl* doc = TY_(getMessageDoc)(*message);
return tidyImplToDoc(doc);
}
uint TIDY_CALL tidyGetMessageCode( TidyMessage tmessage )
{
TidyMessageImpl *message = tidyMessageToImpl(tmessage);
return TY_(getMessageCode)(*message);
}
ctmbstr TIDY_CALL tidyGetMessageKey( TidyMessage tmessage )
{
TidyMessageImpl *message = tidyMessageToImpl(tmessage);
return TY_(getMessageKey)(*message);
}
int TIDY_CALL tidyGetMessageLine( TidyMessage tmessage )
{
TidyMessageImpl *message = tidyMessageToImpl(tmessage);
return TY_(getMessageLine)(*message);
}
int TIDY_CALL tidyGetMessageColumn( TidyMessage tmessage )
{
TidyMessageImpl *message = tidyMessageToImpl(tmessage);
return TY_(getMessageColumn)(*message);
}
TidyReportLevel TIDY_CALL tidyGetMessageLevel( TidyMessage tmessage )
{
TidyMessageImpl *message = tidyMessageToImpl(tmessage);
return TY_(getMessageLevel)(*message);
}
Bool TIDY_CALL tidyGetMessageIsMuted( TidyMessage tmessage )
{
TidyMessageImpl *message = tidyMessageToImpl(tmessage);
return TY_(getMessageIsMuted)(*message);
}
ctmbstr TIDY_CALL tidyGetMessageFormatDefault( TidyMessage tmessage )
{
TidyMessageImpl *message = tidyMessageToImpl(tmessage);
return TY_(getMessageFormatDefault)(*message);
}
ctmbstr TIDY_CALL tidyGetMessageFormat( TidyMessage tmessage )
{
TidyMessageImpl *message = tidyMessageToImpl(tmessage);
return TY_(getMessageFormat)(*message);
}
ctmbstr TIDY_CALL tidyGetMessageDefault( TidyMessage tmessage )
{
TidyMessageImpl *message = tidyMessageToImpl(tmessage);
return TY_(getMessageDefault)(*message);
}
ctmbstr TIDY_CALL tidyGetMessage( TidyMessage tmessage )
{
TidyMessageImpl *message = tidyMessageToImpl(tmessage);
return TY_(getMessage)(*message);
}
ctmbstr TIDY_CALL tidyGetMessagePosDefault( TidyMessage tmessage )
{
TidyMessageImpl *message = tidyMessageToImpl(tmessage);
return TY_(getMessagePosDefault)(*message);
}
ctmbstr TIDY_CALL tidyGetMessagePos( TidyMessage tmessage )
{
TidyMessageImpl *message = tidyMessageToImpl(tmessage);
return TY_(getMessagePos)(*message);
}
ctmbstr TIDY_CALL tidyGetMessagePrefixDefault( TidyMessage tmessage )
{
TidyMessageImpl *message = tidyMessageToImpl(tmessage);
return TY_(getMessagePrefixDefault)(*message);
}
ctmbstr TIDY_CALL tidyGetMessagePrefix( TidyMessage tmessage )
{
TidyMessageImpl *message = tidyMessageToImpl(tmessage);
return TY_(getMessagePrefix)(*message);
}
ctmbstr TIDY_CALL tidyGetMessageOutputDefault( TidyMessage tmessage )
{
TidyMessageImpl *message = tidyMessageToImpl(tmessage);
return TY_(getMessageOutputDefault)(*message);
}
ctmbstr TIDY_CALL tidyGetMessageOutput( TidyMessage tmessage )
{
TidyMessageImpl *message = tidyMessageToImpl(tmessage);
return TY_(getMessageOutput)(*message);
}
TidyIterator TIDY_CALL tidyGetMessageArguments( TidyMessage tmessage )
{
TidyMessageImpl *message = tidyMessageToImpl(tmessage);
return TY_(getMessageArguments)(*message);
}
TidyMessageArgument TIDY_CALL tidyGetNextMessageArgument( TidyMessage tmessage, TidyIterator* iter )
{
TidyMessageImpl *message = tidyMessageToImpl(tmessage);
return TY_(getNextMessageArgument)(*message, iter);
}
TidyFormatParameterType TIDY_CALL tidyGetArgType( TidyMessage tmessage, TidyMessageArgument* arg )
{
TidyMessageImpl *message = tidyMessageToImpl(tmessage);
return TY_(getArgType)(*message, arg);
}
ctmbstr TIDY_CALL tidyGetArgFormat( TidyMessage tmessage, TidyMessageArgument* arg )
{
TidyMessageImpl *message = tidyMessageToImpl(tmessage);
return TY_(getArgFormat)(*message, arg);
}
ctmbstr TIDY_CALL tidyGetArgValueString( TidyMessage tmessage, TidyMessageArgument* arg )
{
TidyMessageImpl *message = tidyMessageToImpl(tmessage);
return TY_(getArgValueString)(*message, arg);
}
uint TIDY_CALL tidyGetArgValueUInt( TidyMessage tmessage, TidyMessageArgument* arg )
{
TidyMessageImpl *message = tidyMessageToImpl(tmessage);
return TY_(getArgValueUInt)(*message, arg);
}
int TIDY_CALL tidyGetArgValueInt( TidyMessage tmessage, TidyMessageArgument* arg )
{
TidyMessageImpl *message = tidyMessageToImpl(tmessage);
return TY_(getArgValueInt)(*message, arg);
}
double TIDY_CALL tidyGetArgValueDouble( TidyMessage tmessage, TidyMessageArgument* arg )
{
TidyMessageImpl *message = tidyMessageToImpl(tmessage);
return TY_(getArgValueDouble)(*message, arg);
}
FILE* TIDY_CALL tidySetErrorFile( TidyDoc tdoc, ctmbstr errfilnam )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
{
FILE* errout = fopen( errfilnam, "wb" );
if ( errout )
{
uint outenc = cfg( impl, TidyOutCharEncoding );
uint nl = cfg( impl, TidyNewline );
TY_(ReleaseStreamOut)( impl, impl->errout );
impl->errout = TY_(FileOutput)( impl, errout, outenc, nl );
return errout;
}
else /* Emit message to current error sink */
TY_(ReportFileError)( impl, errfilnam, FILE_CANT_OPEN );
}
return NULL;
}
int TIDY_CALL tidySetErrorBuffer( TidyDoc tdoc, TidyBuffer* errbuf )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
{
uint outenc = cfg( impl, TidyOutCharEncoding );
uint nl = cfg( impl, TidyNewline );
TY_(ReleaseStreamOut)( impl, impl->errout );
impl->errout = TY_(BufferOutput)( impl, errbuf, outenc, nl );
return ( impl->errout ? 0 : -ENOMEM );
}
return -EINVAL;
}
int TIDY_CALL tidySetErrorSink( TidyDoc tdoc, TidyOutputSink* sink )
{
TidyDocImpl* impl = tidyDocToImpl( tdoc );
if ( impl )
{
uint outenc = cfg( impl, TidyOutCharEncoding );
uint nl = cfg( impl, TidyNewline );
TY_(ReleaseStreamOut)( impl, impl->errout );
impl->errout = TY_(UserOutput)( impl, sink, outenc, nl );
return ( impl->errout ? 0 : -ENOMEM );
}
return -EINVAL;
}