-
Notifications
You must be signed in to change notification settings - Fork 429
/
Copy pathconfig.c
2006 lines (1685 loc) · 66.8 KB
/
config.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
/* config.c
* Read configuration files and manage configuration properties.
*
* Copyright (c) 1998-2017 World Wide Web Consortium (Massachusetts
* Institute of Technology, European Research Consortium for Informatics
* and Mathematics, Keio University) and HTACG.
*
* See tidy.h for the copyright notice.
*/
#include "config.h"
#include "tidy-int.h"
#include "message.h"
#include "tmbstr.h"
#include "tags.h"
#ifdef WINDOWS_OS
# include <io.h>
#else
# ifdef DMALLOC
/* macro for valloc() in dmalloc.h may conflict with declaration for valloc()
in unistd.h - we don't need (debugging for) valloc() here. dmalloc.h should
come last but it doesn't.*/
# ifdef valloc
# undef valloc
# endif
# endif
# include <unistd.h>
#endif
/*****************************************************************************
** Picklist Configuration
**
** Arrange so index can be cast to enum. Note that the value field in the
** following structures is not currently used in code; they're present for
** documentation purposes currently. The arrays must be populated in enum
** order.
******************************************************************************/
static PickListItems boolPicks = {
{ "no", TidyNoState, { "0", "n", "f", "no", "false", NULL } },
{ "yes", TidyYesState, { "1", "y", "t", "yes", "true", NULL } },
{ NULL }
};
static PickListItems autoBoolPicks = {
{ "no", TidyNoState, { "0", "n", "f", "no", "false", NULL } },
{ "yes", TidyYesState, { "1", "y", "t", "yes", "true", NULL } },
{ "auto", TidyAutoState, { "auto", NULL } },
{ NULL }
};
static PickListItems repeatAttrPicks = {
{ "keep-first", TidyNoState, { "keep-first", NULL } },
{ "keep-last", TidyYesState, { "keep-last", NULL } },
{ NULL }
};
static PickListItems accessPicks = {
{ "0 (Tidy Classic)", 0, { "0", "0 (Tidy Classic)", NULL } },
{ "1 (Priority 1 Checks)", 1, { "1", "1 (Priority 1 Checks)", NULL } },
{ "2 (Priority 2 Checks)", 2, { "2", "2 (Priority 2 Checks)", NULL } },
{ "3 (Priority 3 Checks)", 3, { "3", "3 (Priority 3 Checks)", NULL } },
{ NULL }
};
static PickListItems charEncPicks = {
{ "raw", TidyEncRaw, { "raw", NULL } },
{ "ascii", TidyEncAscii, { "ascii", NULL } },
{ "latin0", TidyEncLatin0, { "latin0", NULL } },
{ "latin1", TidyEncLatin1, { "latin1", NULL } },
{ "utf8", TidyEncUtf8, { "utf8", NULL } },
#ifndef NO_NATIVE_ISO2022_SUPPORT
{ "iso2022", TidyEncIso2022, { "iso2022", NULL } },
#endif
{ "mac", TidyEncMac, { "mac", NULL } },
{ "win1252", TidyEncWin1252, { "win1252", NULL } },
{ "ibm858", TidyEncIbm858, { "ibm858", NULL } },
{ "utf16le", TidyEncUtf16le, { "utf16le", NULL } },
{ "utf16be", TidyEncUtf16be, { "utf16be", NULL } },
{ "utf16", TidyEncUtf16, { "utf16", NULL } },
{ "big5", TidyEncBig5, { "big5", NULL } },
{ "shiftjis", TidyEncShiftjis, { "shiftjis", NULL } },
{ NULL }
};
static PickListItems newlinePicks = {
{ "LF", TidyLF, { "lf", NULL } },
{ "CRLF", TidyCRLF, { "crlf", NULL } },
{ "CR", TidyCR, { "cr", NULL } },
{ NULL }
};
static PickListItems doctypePicks = {
{ "html5", TidyDoctypeHtml5, { "html5", NULL } },
{ "omit", TidyDoctypeOmit, { "omit", NULL } },
{ "auto", TidyDoctypeAuto, { "auto", NULL } },
{ "strict", TidyDoctypeStrict, { "strict", NULL } },
{ "transitional", TidyDoctypeLoose, { "loose", "transitional", NULL } },
{ "user", TidyDoctypeUser, { "user", NULL } },
{ NULL }
};
static PickListItems sorterPicks = {
{ "none", TidySortAttrNone, { "none", NULL } },
{ "alpha", TidySortAttrAlpha, { "alpha", NULL } },
{ NULL }
};
static PickListItems customTagsPicks = {
{"no", TidyCustomNo, { "no", "n", NULL } },
{"blocklevel", TidyCustomBlocklevel, { "blocklevel", NULL } },
{"empty", TidyCustomEmpty, { "empty", NULL } },
{"inline", TidyCustomInline, { "inline", "y", "yes", NULL } },
{"pre", TidyCustomPre, { "pre", NULL } },
{ NULL }
};
static PickListItems attributeCasePicks = {
{ "no", TidyUppercaseNo, { "0", "n", "f", "no", "false", NULL } },
{ "yes", TidyUppercaseYes, { "1", "y", "t", "yes", "true", NULL } },
{ "preserve", TidyUppercasePreserve, { "preserve", NULL } },
{ NULL }
};
/*****************************************************************************
** Option Configuration
******************************************************************************/
#define DG TidyDiagnostics
#define DD TidyDisplay
#define DT TidyDocumentIO
#define CE TidyEncoding
#define IO TidyFileIO
#define MC TidyMarkupCleanup
#define ME TidyMarkupEntities
#define MR TidyMarkupRepair
#define MT TidyMarkupTeach
#define MX TidyMarkupXForm
#define PP TidyPrettyPrint
#define IR TidyInternalCategory
#define IN TidyInteger
#define BL TidyBoolean
#define ST TidyString
#define XX (TidyConfigCategory)-1
#define XY (TidyOptionType)-1
#define DLF DEFAULT_NL_CONFIG
/* forward declarations */
static ParseProperty ParseInt;
static ParseProperty ParseList;
static ParseProperty ParseName;
static ParseProperty ParseCSS1Selector;
static ParseProperty ParseString;
static ParseProperty ParseCharEnc;
static ParseProperty ParseDocType;
static ParseProperty ParseTabs;
static ParseProperty ParsePickList;
/*****************************************************************/
/* Ensure struct order is same order as tidyenum.h:TidyOptionId! */
/*****************************************************************/
static const TidyOptionImpl option_defs[] =
{
{ TidyUnknownOption, IR, "unknown!", IN, 0, NULL, NULL },
{ TidyAccessibilityCheckLevel, DG, "accessibility-check", IN, 0, ParsePickList, &accessPicks },
{ TidyAltText, MR, "alt-text", ST, 0, ParseString, NULL },
{ TidyAnchorAsName, MR, "anchor-as-name", BL, yes, ParsePickList, &boolPicks },
{ TidyAsciiChars, ME, "ascii-chars", BL, no, ParsePickList, &boolPicks },
{ TidyBlockTags, MT, "new-blocklevel-tags", ST, 0, ParseList, NULL },
{ TidyBodyOnly, DD, "show-body-only", IN, no, ParsePickList, &autoBoolPicks },
{ TidyBreakBeforeBR, PP, "break-before-br", BL, no, ParsePickList, &boolPicks },
{ TidyCharEncoding, CE, "char-encoding", IN, UTF8, ParseCharEnc, &charEncPicks },
{ TidyCoerceEndTags, MR, "coerce-endtags", BL, yes, ParsePickList, &boolPicks },
{ TidyCSSPrefix, MR, "css-prefix", ST, 0, ParseCSS1Selector, NULL, "c" },
{ TidyCustomTags, IR, "new-custom-tags", ST, 0, ParseList, NULL }, /* 20170309 - Issue #119 */
{ TidyDecorateInferredUL, MX, "decorate-inferred-ul", BL, no, ParsePickList, &boolPicks },
{ TidyDoctype, DT, "doctype", ST, TidyDoctypeAuto, ParseDocType, &doctypePicks },
#ifndef DOXYGEN_SHOULD_SKIP_THIS
{ TidyDoctypeMode, IR, "doctype-mode", IN, TidyDoctypeAuto, NULL, &doctypePicks },
#endif
{ TidyDropEmptyElems, MC, "drop-empty-elements", BL, yes, ParsePickList, &boolPicks },
{ TidyDropEmptyParas, MC, "drop-empty-paras", BL, yes, ParsePickList, &boolPicks },
{ TidyDropPropAttrs, MC, "drop-proprietary-attributes", BL, no, ParsePickList, &boolPicks },
{ TidyDuplicateAttrs, MR, "repeated-attributes", IN, TidyKeepLast, ParsePickList, &repeatAttrPicks },
{ TidyEmacs, DD, "gnu-emacs", BL, no, ParsePickList, &boolPicks },
#ifndef DOXYGEN_SHOULD_SKIP_THIS
{ TidyEmacsFile, IR, "gnu-emacs-file", ST, 0, ParseString, NULL },
#endif
{ TidyEmptyTags, MT, "new-empty-tags", ST, 0, ParseList, NULL },
{ TidyEncloseBlockText, MR, "enclose-block-text", BL, no, ParsePickList, &boolPicks },
{ TidyEncloseBodyText, MR, "enclose-text", BL, no, ParsePickList, &boolPicks },
{ TidyErrFile, IO, "error-file", ST, 0, ParseString, NULL },
{ TidyEscapeCdata, MX, "escape-cdata", BL, no, ParsePickList, &boolPicks },
{ TidyEscapeScripts, MR, "escape-scripts", BL, yes, ParsePickList, &boolPicks }, /* 20160227 - Issue #348 */
{ TidyFixBackslash, MR, "fix-backslash", BL, yes, ParsePickList, &boolPicks },
{ TidyFixComments, MR, "fix-bad-comments", IN, TidyAutoState, ParsePickList, &autoBoolPicks },
{ TidyFixUri, MR, "fix-uri", BL, yes, ParsePickList, &boolPicks },
{ TidyForceOutput, DG, "force-output", BL, no, ParsePickList, &boolPicks },
{ TidyGDocClean, MC, "gdoc", BL, no, ParsePickList, &boolPicks },
{ TidyHideComments, MX, "hide-comments", BL, no, ParsePickList, &boolPicks },
{ TidyHtmlOut, DT, "output-html", BL, no, ParsePickList, &boolPicks },
{ TidyInCharEncoding, CE, "input-encoding", IN, UTF8, ParseCharEnc, &charEncPicks },
{ TidyIndentAttributes, PP, "indent-attributes", BL, no, ParsePickList, &boolPicks },
{ TidyIndentCdata, PP, "indent-cdata", BL, no, ParsePickList, &boolPicks },
{ TidyIndentContent, PP, "indent", IN, TidyNoState, ParsePickList, &autoBoolPicks },
{ TidyIndentSpaces, PP, "indent-spaces", IN, 2, ParseInt, NULL },
{ TidyInlineTags, MT, "new-inline-tags", ST, 0, ParseList, NULL },
{ TidyJoinClasses, MX, "join-classes", BL, no, ParsePickList, &boolPicks },
{ TidyJoinStyles, MX, "join-styles", BL, yes, ParsePickList, &boolPicks },
{ TidyKeepFileTimes, IO, "keep-time", BL, no, ParsePickList, &boolPicks },
{ TidyKeepTabs, PP, "keep-tabs", BL, no, ParsePickList, &boolPicks }, /* 20171103 - Issue #403 */
{ TidyLiteralAttribs, MR, "literal-attributes", BL, no, ParsePickList, &boolPicks },
{ TidyLogicalEmphasis, MC, "logical-emphasis", BL, no, ParsePickList, &boolPicks },
{ TidyLowerLiterals, MR, "lower-literals", BL, yes, ParsePickList, &boolPicks },
{ TidyMakeBare, MC, "bare", BL, no, ParsePickList, &boolPicks },
{ TidyMakeClean, MC, "clean", BL, no, ParsePickList, &boolPicks },
{ TidyMark, PP, "tidy-mark", BL, yes, ParsePickList, &boolPicks },
{ TidyMergeDivs, MC, "merge-divs", IN, TidyAutoState, ParsePickList, &autoBoolPicks },
{ TidyMergeEmphasis, MX, "merge-emphasis", BL, yes, ParsePickList, &boolPicks },
{ TidyMergeSpans, MC, "merge-spans", IN, TidyAutoState, ParsePickList, &autoBoolPicks },
{ TidyMetaCharset, DT, "add-meta-charset", BL, no, ParsePickList, &boolPicks }, /* 20161004 - Issue #456 */
{ TidyMuteReports, DD, "mute", ST, 0, ParseList, NULL },
{ TidyMuteShow, DD, "mute-id", BL, no, ParsePickList, &boolPicks },
{ TidyNCR, ME, "ncr", BL, yes, ParsePickList, &boolPicks },
{ TidyNewline, CE, "newline", IN, DLF, ParsePickList, &newlinePicks },
{ TidyNumEntities, ME, "numeric-entities", BL, no, ParsePickList, &boolPicks },
{ TidyOmitOptionalTags, PP, "omit-optional-tags", BL, no, ParsePickList, &boolPicks },
{ TidyOutCharEncoding, CE, "output-encoding", IN, UTF8, ParseCharEnc, &charEncPicks },
{ TidyOutFile, IO, "output-file", ST, 0, ParseString, NULL },
{ TidyOutputBOM, CE, "output-bom", IN, TidyAutoState, ParsePickList, &autoBoolPicks },
{ TidyPPrintTabs, PP, "indent-with-tabs", BL, no, ParseTabs, &boolPicks }, /* 20150515 - Issue #108 */
{ TidyPreserveEntities, ME, "preserve-entities", BL, no, ParsePickList, &boolPicks },
{ TidyPreTags, MT, "new-pre-tags", ST, 0, ParseList, NULL },
{ TidyPriorityAttributes, PP, "priority-attributes", ST, 0, ParseList, NULL },
{ TidyPunctWrap, PP, "punctuation-wrap", BL, no, ParsePickList, &boolPicks },
{ TidyQuiet, DD, "quiet", BL, no, ParsePickList, &boolPicks },
{ TidyQuoteAmpersand, ME, "quote-ampersand", BL, yes, ParsePickList, &boolPicks },
{ TidyQuoteMarks, ME, "quote-marks", BL, no, ParsePickList, &boolPicks },
{ TidyQuoteNbsp, ME, "quote-nbsp", BL, yes, ParsePickList, &boolPicks },
{ TidyReplaceColor, MX, "replace-color", BL, no, ParsePickList, &boolPicks },
{ TidyShowErrors, DD, "show-errors", IN, 6, ParseInt, NULL },
{ TidyShowFilename, DD, "show-filename", BL, no, ParsePickList, &boolPicks },
{ TidyShowInfo, DD, "show-info", BL, yes, ParsePickList, &boolPicks },
{ TidyShowMarkup, DD, "markup", BL, yes, ParsePickList, &boolPicks },
{ TidyShowMetaChange, DG, "show-meta-change", BL, no, ParsePickList, &boolPicks }, /* 20170609 - Issue #456 */
{ TidyShowWarnings, DD, "show-warnings", BL, yes, ParsePickList, &boolPicks },
{ TidySkipNested, MR, "skip-nested", BL, yes, ParsePickList, &boolPicks }, /* 1642186 - Issue #65 */
{ TidySortAttributes, PP, "sort-attributes", IN, TidySortAttrNone,ParsePickList, &sorterPicks },
{ TidyStrictTagsAttr, MR, "strict-tags-attributes", BL, no, ParsePickList, &boolPicks }, /* 20160209 - Issue #350 */
{ TidyStyleTags, MR, "fix-style-tags", BL, yes, ParsePickList, &boolPicks },
{ TidyTabSize, PP, "tab-size", IN, 8, ParseInt, NULL },
{ TidyUpperCaseAttrs, MR, "uppercase-attributes", IN, TidyUppercaseNo, ParsePickList, &attributeCasePicks },
{ TidyUpperCaseTags, MR, "uppercase-tags", BL, no, ParsePickList, &boolPicks },
{ TidyUseCustomTags, MR, "custom-tags", IN, TidyCustomNo, ParsePickList, &customTagsPicks }, /* 20170309 - Issue #119 */
{ TidyVertSpace, PP, "vertical-space", IN, no, ParsePickList, &autoBoolPicks }, /* #228 - tri option */
{ TidyScriptNoFirstBlankLine, PP, "script-no-first-blank-line", BL, no, ParsePickList, &boolPicks },
{ TidyWarnPropAttrs, DG, "warn-proprietary-attributes", BL, yes, ParsePickList, &boolPicks },
{ TidyWord2000, MC, "word-2000", BL, no, ParsePickList, &boolPicks },
{ TidyWrapAsp, PP, "wrap-asp", BL, yes, ParsePickList, &boolPicks },
{ TidyWrapAttVals, PP, "wrap-attributes", BL, no, ParsePickList, &boolPicks },
{ TidyWrapJste, PP, "wrap-jste", BL, yes, ParsePickList, &boolPicks },
{ TidyWrapLen, PP, "wrap", IN, 68, ParseInt, NULL },
{ TidyWrapPhp, PP, "wrap-php", BL, no, ParsePickList, &boolPicks },
{ TidyWrapScriptlets, PP, "wrap-script-literals", BL, no, ParsePickList, &boolPicks },
{ TidyWrapSection, PP, "wrap-sections", BL, yes, ParsePickList, &boolPicks },
{ TidyWriteBack, IO, "write-back", BL, no, ParsePickList, &boolPicks },
{ TidyXhtmlOut, DT, "output-xhtml", BL, no, ParsePickList, &boolPicks },
{ TidyXmlDecl, DT, "add-xml-decl", BL, no, ParsePickList, &boolPicks },
{ TidyXmlOut, DT, "output-xml", BL, no, ParsePickList, &boolPicks },
{ TidyXmlPIs, MR, "assume-xml-procins", BL, no, ParsePickList, &boolPicks },
{ TidyXmlSpace, DT, "add-xml-space", BL, no, ParsePickList, &boolPicks },
{ TidyXmlTags, DT, "input-xml", BL, no, ParsePickList, &boolPicks },
{ N_TIDY_OPTIONS, XX, NULL, XY, 0, NULL, NULL }
};
/*****************************************************************************
** Deleted Options Configuration
**
** Keep track of options that have been removed from Tidy, so that we can
** suggests a replacement. When a deleted option is used, client programs
** will have the opportunity to consume the option first via the callback,
** and if not handled by the callback, will be handled by Tidy, generally
** by setting an alternate or new option, in `subDeprecatedOption()`.
******************************************************************************/
static const struct {
ctmbstr name; /**< name of the deprecated option */
TidyOptionId replacementId; /**< Id of the replacement option, or 0 if none. */
} deprecatedOptions[] = {
/* { "show-body-only", TidyBodyOnly }, */
{ NULL }
};
/*****************************************************************************
** Supporting Functions
******************************************************************************/
/* forward declarations */
static Bool GetPickListValue( ctmbstr value, PickListItems* pickList, uint *result );
void TY_(InitConfig)( TidyDocImpl* doc )
{
TidyClearMemory( &doc->config, sizeof(TidyConfigImpl) );
TY_(ResetConfigToDefault)( doc );
}
void TY_(FreeConfig)( TidyDocImpl* doc )
{
doc->pConfigChangeCallback = NULL;
TY_(ResetConfigToDefault)( doc );
TY_(TakeConfigSnapshot)( doc );
}
/* Should only be called by options set by name
** thus, it is cheaper to do a few scans than set
** up every option in a hash table.
*/
const TidyOptionImpl* TY_(lookupOption)( ctmbstr s )
{
const TidyOptionImpl* np = option_defs;
for ( /**/; np < option_defs + N_TIDY_OPTIONS; ++np )
{
if ( TY_(tmbstrcasecmp)(s, np->name) == 0 )
return np;
}
return NULL;
}
const TidyOptionImpl* TY_(getOption)( TidyOptionId optId )
{
if ( optId < N_TIDY_OPTIONS )
return option_defs + optId;
return NULL;
}
const Bool TY_(getOptionIsList)( TidyOptionId optId )
{
const TidyOptionImpl* option = TY_(getOption)( optId );
return option->parser == ParseList;
}
static Bool OptionChangedValuesDiffer( ctmbstr a, ctmbstr b )
{
if ( a != b )
{
if ( a == NULL || b == NULL ) /* can't both be null at this point. */
return yes;
else
return TY_(tmbstrcmp)( a, b ) != 0;
}
return no;
}
static void PerformOptionChangedCallback( TidyDocImpl* doc, const TidyOptionImpl* option )
{
if ( doc->pConfigChangeCallback )
{
TidyDoc tdoc = tidyImplToDoc( doc );
TidyOption opt = tidyImplToOption( option );
doc->pConfigChangeCallback( tdoc, opt );
}
}
static void FreeOptionValue( TidyDocImpl* doc, const TidyOptionImpl* option, TidyOptionValue* value )
{
if ( option->type == TidyString && value->p && value->p != option->pdflt )
TidyDocFree( doc, value->p );
}
static void CopyOptionValue( TidyDocImpl* doc, const TidyOptionImpl* option,
TidyOptionValue* oldval, const TidyOptionValue* newval )
{
Bool fire_callback = no;
assert( oldval != NULL );
/* Compare the old and new values. */
if ( doc->pConfigChangeCallback )
{
if ( option->type == TidyString )
fire_callback = OptionChangedValuesDiffer( oldval->p, newval->p );
else
fire_callback = oldval->v != newval->v;
}
FreeOptionValue( doc, option, oldval );
if ( option->type == TidyString )
{
if ( newval->p && newval->p != option->pdflt )
oldval->p = TY_(tmbstrdup)( doc->allocator, newval->p );
else
oldval->p = newval->p;
}
else
oldval->v = newval->v;
if ( fire_callback )
PerformOptionChangedCallback( doc, option );
}
static Bool SetOptionValue( TidyDocImpl* doc, TidyOptionId optId, ctmbstr val )
{
const TidyOptionImpl* option = &option_defs[ optId ];
Bool fire_callback = no;
Bool status = ( optId < N_TIDY_OPTIONS );
if ( status )
{
assert( option->id == optId && option->type == TidyString );
/* Compare the old and new values. */
if ( doc->pConfigChangeCallback )
{
TidyOptionValue* oldval = &(doc->config.value[ optId ]);
fire_callback = OptionChangedValuesDiffer( oldval->p, val );
}
FreeOptionValue( doc, option, &doc->config.value[ optId ] );
if ( TY_(tmbstrlen)(val)) /* Issue #218 - ONLY if it has LENGTH! */
doc->config.value[ optId ].p = TY_(tmbstrdup)( doc->allocator, val );
else
doc->config.value[ optId ].p = 0; /* should already be zero, but to be sure... */
}
if ( fire_callback )
PerformOptionChangedCallback( doc, option );
return status;
}
ctmbstr TY_(GetPickListLabelForPick)( TidyOptionId optId, uint pick )
{
const TidyOptionImpl* option = TY_(getOption)( optId );
if ( option && option->pickList )
{
uint ix = 0;
const PickListItem *item = NULL;
/* Loop through the picklist until index matches the value. */
while ( (item = &(*option->pickList)[ ix ]) && item->label && ix<pick )
{
++ix;
}
if ( ix==pick && item->label )
return item->label;
}
return NULL;
}
static void SetOptionInteger( TidyDocImpl* doc, TidyOptionId optId, ulong val )
{
const TidyOptionImpl* option = &option_defs[ optId ];
ulong* optVal = &(doc->config.value[ optId ].v);
Bool fire_callback = doc->pConfigChangeCallback && *optVal != val;
*optVal = val;
if ( fire_callback )
PerformOptionChangedCallback( doc, option );
}
Bool TY_(SetOptionInt)( TidyDocImpl* doc, TidyOptionId optId, ulong val )
{
Bool status = ( optId < N_TIDY_OPTIONS );
if ( status )
{
assert( option_defs[ optId ].type == TidyInteger );
SetOptionInteger( doc, optId, val );
}
return status;
}
Bool TY_(SetOptionBool)( TidyDocImpl* doc, TidyOptionId optId, Bool val )
{
Bool status = ( optId < N_TIDY_OPTIONS );
if ( status )
{
assert( option_defs[ optId ].type == TidyBoolean );
SetOptionInteger( doc, optId, (ulong)val );
}
return status;
}
static void GetOptionDefault( const TidyOptionImpl* option,
TidyOptionValue* dflt )
{
if ( option->type == TidyString )
dflt->p = (char*)option->pdflt;
else
dflt->v = option->dflt;
}
static Bool OptionValueEqDefault( const TidyOptionImpl* option,
const TidyOptionValue* val )
{
return ( option->type == TidyString ) ?
val->p == option->pdflt :
val->v == option->dflt;
}
Bool TY_(ResetOptionToDefault)( TidyDocImpl* doc, TidyOptionId optId )
{
Bool status = ( optId > 0 && optId < N_TIDY_OPTIONS );
if ( status )
{
TidyOptionValue dflt;
const TidyOptionImpl* option = option_defs + optId;
TidyOptionValue* value = &doc->config.value[ optId ];
assert( optId == option->id );
GetOptionDefault( option, &dflt );
CopyOptionValue( doc, option, value, &dflt );
}
return status;
}
static void ReparseTagType( TidyDocImpl* doc, TidyOptionId optId )
{
ctmbstr tagdecl = cfgStr( doc, optId );
tmbstr dupdecl = TY_(tmbstrdup)( doc->allocator, tagdecl );
TY_(ParseConfigValue)( doc, optId, dupdecl );
TidyDocFree( doc, dupdecl );
}
static Bool OptionValueIdentical( const TidyOptionImpl* option,
const TidyOptionValue* val1,
const TidyOptionValue* val2 )
{
if ( option->type == TidyString )
{
if ( val1->p == val2->p )
return yes;
if ( !val1->p || !val2->p )
return no;
return TY_(tmbstrcmp)( val1->p, val2->p ) == 0;
}
else
return val1->v == val2->v;
}
static Bool NeedReparseTagDecls( TidyDocImpl* doc,
const TidyOptionValue* current,
const TidyOptionValue* new,
uint *changedUserTags )
{
Bool ret = no;
uint ixVal;
const TidyOptionImpl* option = option_defs;
*changedUserTags = tagtype_null;
for ( ixVal=0; ixVal < N_TIDY_OPTIONS; ++option, ++ixVal )
{
assert( ixVal == (uint) option->id );
switch (option->id)
{
#define TEST_USERTAGS(USERTAGOPTION,USERTAGTYPE) \
case USERTAGOPTION: \
if (!OptionValueIdentical(option,¤t[ixVal],&new[ixVal])) \
{ \
*changedUserTags |= USERTAGTYPE; \
ret = yes; \
} \
break
TEST_USERTAGS(TidyInlineTags,tagtype_inline);
TEST_USERTAGS(TidyBlockTags,tagtype_block);
TEST_USERTAGS(TidyEmptyTags,tagtype_empty);
TEST_USERTAGS(TidyPreTags,tagtype_pre);
default:
break;
}
}
return ret;
}
static void ReparseTagDecls( TidyDocImpl* doc, uint changedUserTags )
{
#define REPARSE_USERTAGS(USERTAGOPTION,USERTAGTYPE) \
if ( changedUserTags & USERTAGTYPE ) \
{ \
TY_(FreeDeclaredTags)( doc, USERTAGTYPE ); \
ReparseTagType( doc, USERTAGOPTION ); \
}
REPARSE_USERTAGS(TidyInlineTags,tagtype_inline);
REPARSE_USERTAGS(TidyBlockTags,tagtype_block);
REPARSE_USERTAGS(TidyEmptyTags,tagtype_empty);
REPARSE_USERTAGS(TidyPreTags,tagtype_pre);
}
/* Returns the option id of the replacement Tidy option for optName. Because
** an option might not have a replacement (0, TidyUnknownOption), a return
** value of N_TIDY_OPTIONS indicates an error, i.e., that the option isn't
** in the deprecated list.
*/
static TidyOptionId getOptionReplacement( ctmbstr optName )
{
uint i = 0;
ctmbstr testName;
while ( (testName = deprecatedOptions[i].name) )
{
if ( TY_(tmbstrcasecmp)( optName, testName ) == 0 )
return deprecatedOptions[i].replacementId;
i++;
}
return N_TIDY_OPTIONS;
}
/* Indicates whether or not optName is deprecated */
static Bool isOptionDeprecated( ctmbstr optName )
{
return getOptionReplacement( optName ) != N_TIDY_OPTIONS;
}
/* Substitute the new option for the deprecated one. */
static Bool subDeprecatedOption( TidyDocImpl* doc, ctmbstr oldName, ctmbstr oldValue)
{
TidyOptionId newOptId = getOptionReplacement( oldName );
ctmbstr newName = TY_(getOption)( newOptId )->name;
TidyDoc tdoc = tidyImplToDoc( doc );
assert( isOptionDeprecated(oldName));
if ( newOptId == TidyUnknownOption )
{
TY_(Report)( doc, NULL, NULL, OPTION_REMOVED, oldName );
return no;
}
/********************/
/* `show-body-only` */
/********************/
if ( TY_(tmbstrcasecmp)( oldName, "show-body-only" ) == 0 )
{
uint value;
/* `show-body-only` used to use the autoBoolPicks */
if ( GetPickListValue( oldValue, &autoBoolPicks, &value ) )
{
if ( value == TidyNoState )
{
TY_(SetOptionInt)( doc, newOptId, value );
TY_(Report)( doc, NULL, NULL, OPTION_REMOVED_UNAPPLIED, oldName, newName );
}
else
{
ctmbstr val;
TY_(SetOptionInt)( doc, newOptId, value );
val = tidyOptGetCurrPick( tdoc, newOptId );
TY_(Report)( doc, NULL, NULL, OPTION_REMOVED_APPLIED, oldName, newName, val );
}
}
else
{
TY_(ReportBadArgument)(doc, oldName);
}
return yes;
}
return no;
}
void TY_(ResetConfigToDefault)( TidyDocImpl* doc )
{
uint ixVal;
const TidyOptionImpl* option = option_defs;
TidyOptionValue* value = &doc->config.value[ 0 ];
for ( ixVal=0; ixVal < N_TIDY_OPTIONS; ++option, ++ixVal )
{
TidyOptionValue dflt;
assert( ixVal == (uint) option->id );
GetOptionDefault( option, &dflt );
CopyOptionValue( doc, option, &value[ixVal], &dflt );
}
TY_(FreeDeclaredTags)( doc, tagtype_null );
}
void TY_(TakeConfigSnapshot)( TidyDocImpl* doc )
{
uint ixVal;
const TidyOptionImpl* option = option_defs;
const TidyOptionValue* value = &doc->config.value[ 0 ];
TidyOptionValue* snap = &doc->config.snapshot[ 0 ];
/* @jsd: do NOT mess with user-specified settings until
* absolutely necessary, and ensure that we can
* can restore them immediately after the need.
*/
// TY_(AdjustConfig)( doc ); /* Make sure it's consistent */
for ( ixVal=0; ixVal < N_TIDY_OPTIONS; ++option, ++ixVal )
{
assert( ixVal == (uint) option->id );
CopyOptionValue( doc, option, &snap[ixVal], &value[ixVal] );
}
}
void TY_(ResetConfigToSnapshot)( TidyDocImpl* doc )
{
uint ixVal;
const TidyOptionImpl* option = option_defs;
TidyOptionValue* value = &doc->config.value[ 0 ];
const TidyOptionValue* snap = &doc->config.snapshot[ 0 ];
uint changedUserTags;
Bool needReparseTagsDecls = NeedReparseTagDecls( doc, value, snap,
&changedUserTags );
for ( ixVal=0; ixVal < N_TIDY_OPTIONS; ++option, ++ixVal )
{
assert( ixVal == (uint) option->id );
CopyOptionValue( doc, option, &value[ixVal], &snap[ixVal] );
}
if ( needReparseTagsDecls )
ReparseTagDecls( doc, changedUserTags );
}
void TY_(CopyConfig)( TidyDocImpl* docTo, TidyDocImpl* docFrom )
{
if ( docTo != docFrom )
{
uint ixVal;
const TidyOptionImpl* option = option_defs;
const TidyOptionValue* from = &docFrom->config.value[ 0 ];
TidyOptionValue* to = &docTo->config.value[ 0 ];
uint changedUserTags;
Bool needReparseTagsDecls = NeedReparseTagDecls( docTo, to, from,
&changedUserTags );
TY_(TakeConfigSnapshot)( docTo );
for ( ixVal=0; ixVal < N_TIDY_OPTIONS; ++option, ++ixVal )
{
assert( ixVal == (uint) option->id );
CopyOptionValue( docTo, option, &to[ixVal], &from[ixVal] );
}
if ( needReparseTagsDecls )
ReparseTagDecls( docTo, changedUserTags );
/* @jsd: do NOT mess with user-specified settings until
* absolutely necessary, and ensure that we can
* can restore them immediately after the need.
*/
// TY_(AdjustConfig)( docTo ); /* Make sure it's consistent */
}
}
#ifdef _DEBUG
/* Debug accessor functions will be type-safe and assert option type match */
ulong TY_(_cfgGet)( TidyDocImpl* doc, TidyOptionId optId )
{
assert( optId < N_TIDY_OPTIONS );
return doc->config.value[ optId ].v;
}
Bool TY_(_cfgGetBool)( TidyDocImpl* doc, TidyOptionId optId )
{
ulong val = TY_(_cfgGet)( doc, optId );
const TidyOptionImpl* opt = &option_defs[ optId ];
assert( opt && opt->type == TidyBoolean );
return (Bool) val;
}
TidyTriState TY_(_cfgGetAutoBool)( TidyDocImpl* doc, TidyOptionId optId )
{
ulong val = TY_(_cfgGet)( doc, optId );
const TidyOptionImpl* opt = &option_defs[ optId ];
assert( opt && opt->type == TidyInteger
&& opt->parser == ParsePickList );
return (TidyTriState) val;
}
ctmbstr TY_(_cfgGetString)( TidyDocImpl* doc, TidyOptionId optId )
{
const TidyOptionImpl* opt;
assert( optId < N_TIDY_OPTIONS );
opt = &option_defs[ optId ];
assert( opt && opt->type == TidyString );
return doc->config.value[ optId ].p;
}
#endif
static tchar GetC( TidyConfigImpl* config )
{
if ( config->cfgIn )
return TY_(ReadChar)( config->cfgIn );
return EndOfStream;
}
static tchar FirstChar( TidyConfigImpl* config )
{
config->c = GetC( config );
return config->c;
}
static tchar AdvanceChar( TidyConfigImpl* config )
{
if ( config->c != EndOfStream )
config->c = GetC( config );
return config->c;
}
static tchar SkipWhite( TidyConfigImpl* config )
{
while ( TY_(IsWhite)(config->c) && !TY_(IsNewline)(config->c) )
config->c = GetC( config );
return config->c;
}
/* skip over line continuations to start of next property */
static uint NextProperty( TidyConfigImpl* config )
{
do
{
/* skip to end of line */
while ( config->c != '\n' && config->c != '\r' && config->c != EndOfStream )
config->c = GetC( config );
/* treat \r\n \r or \n as line ends */
if ( config->c == '\r' )
config->c = GetC( config );
if ( config->c == '\n' )
config->c = GetC( config );
}
while ( TY_(IsWhite)(config->c) ); /* line continuation? */
return config->c;
}
/*
Todd Lewis contributed this code for expanding ~/foo or ~your/foo according
to $HOME and your user name. This will work partially on any system which
defines $HOME. Support for ~user/foo will work on systems that support
getpwnam(userid), namely Unix/Linux.
*/
static ctmbstr ExpandTilde( TidyDocImpl* doc, ctmbstr filename )
{
char *home_dir = NULL;
if ( !filename )
return NULL;
if ( filename[0] != '~' )
return filename;
if (filename[1] == '/')
{
home_dir = getenv("HOME");
if (home_dir) {
++filename;
}
#ifdef _WIN32
else if (strlen(filename) >= 3) { /* at least '~/+1' */
/* no HOME env in Windows - got for HOMEDRIVE=C: HOMEPATH=\Users\user */
char * hd = getenv("HOMEDRIVE");
char * hp = getenv("HOMEPATH");
if (hd && hp) {
ctmbstr s = TidyDocAlloc(doc, _MAX_PATH);
strcpy(s, hd);
strcat(s, hp);
strcat(s, "\\");
strcat(s, &filename[2]);
return s;
}
}
#endif /* _WIN32 */
}
#ifdef SUPPORT_GETPWNAM
else
{
struct passwd *passwd = NULL;
ctmbstr s = filename + 1;
tmbstr t;
while ( *s && *s != '/' )
s++;
if ( (t = TidyDocAlloc(doc, s - filename)) )
{
memcpy(t, filename+1, s-filename-1);
t[s-filename-1] = 0;
passwd = getpwnam(t);
TidyDocFree(doc, t);
}
if ( passwd )
{
filename = s;
home_dir = passwd->pw_dir;
}
}
#endif /* SUPPORT_GETPWNAM */
if ( home_dir )
{
uint len = TY_(tmbstrlen)(filename) + TY_(tmbstrlen)(home_dir) + 1;
tmbstr p = (tmbstr)TidyDocAlloc( doc, len );
TY_(tmbstrcpy)( p, home_dir );
TY_(tmbstrcat)( p, filename );
return (ctmbstr) p;
}
return (ctmbstr) filename;
}
Bool TIDY_CALL tidyFileExists( TidyDoc tdoc, ctmbstr filename )
{
TidyDocImpl* doc = tidyDocToImpl( tdoc );
ctmbstr fname = (tmbstr) ExpandTilde( doc, filename );
#ifndef NO_ACCESS_SUPPORT
Bool exists = ( access(fname, 0) == 0 );
#else
Bool exists;
/* at present */
FILE* fin = fopen(fname, "r");
if (fin != NULL)
fclose(fin);
exists = ( fin != NULL );
#endif
if ( fname != filename )
TidyDocFree( doc, (tmbstr) fname );
return exists;
}
int TY_(ParseConfigFile)( TidyDocImpl* doc, ctmbstr file )
{
return TY_(ParseConfigFileEnc)( doc, file, "ascii" );
}
/* open the file and parse its contents
*/
int TY_(ParseConfigFileEnc)( TidyDocImpl* doc, ctmbstr file, ctmbstr charenc )
{
enum { tidy_max_name = 64 };
uint opterrs = doc->optionErrors;
tmbstr fname = (tmbstr) ExpandTilde( doc, file );
TidyConfigImpl* cfg = &doc->config;
FILE* fin = fopen( fname, "r" );
int enc = TY_(CharEncodingId)( doc, charenc );
if ( fin == NULL || enc < 0 )
{
TY_(ReportFileError)( doc, fname, FILE_CANT_OPEN_CFG );
return -1;
}
else
{
tchar c;
cfg->cfgIn = TY_(FileInput)( doc, fin, enc );
c = FirstChar( cfg );
for ( c = SkipWhite(cfg); c != EndOfStream; c = NextProperty(cfg) )
{
uint ix = 0;
tmbchar name[ tidy_max_name ] = {0};