-
Notifications
You must be signed in to change notification settings - Fork 427
/
Copy pathtidy.c
2595 lines (2269 loc) · 83.7 KB
/
tidy.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
/***************************************************************************//**
* @file
* HTML TidyLib command line driver.
*
* This console application utilizing LibTidy in order to offer a complete
* console application offering all of the features of LibTidy.
*
* @author HTACG, et al (consult git log)
*
* @copyright
* Copyright (c) 1998-2017 World Wide Web Consortium (Massachusetts
* Institute of Technology, European Research Consortium for Informatics
* and Mathematics, Keio University) and HTACG.
* @par
* All Rights Reserved.
* @par
* See `tidy.h` for the complete license.
*
* @date Additional updates: consult git log
******************************************************************************/
#include "tidy.h"
#include "tidybuffio.h"
#include "locale.h"
#include "sprtf.h"
#if defined(_WIN32)
# include <windows.h> /* Force console to UTF8. */
#endif
#if defined(ENABLE_DEBUG_LOG) && defined(_MSC_VER) && defined(_CRTDBG_MAP_ALLOC)
# include <stdlib.h>
# include <crtdbg.h>
#endif
/** Tidy will send errors to this file, which will be stderr later. */
static FILE* errout = NULL;
#if defined(_WIN32)
static uint win_cp; /* original Windows code page */
# if (defined(_MSC_VER) && (_MSC_VER < 1900))
# define snprintf _snprintf
# endif
#endif
/** @defgroup console_application Tidy Console Application
** @copydoc tidy.c
** @{
*/
/* MARK: - Miscellaneous Utilities */
/***************************************************************************//**
** @defgroup utilities_misc Miscellaneous Utilities
** This group contains general utilities used in the console application.
*******************************************************************************
** @{
*/
/** Indicates whether or not two filenames are the same.
** @result Returns a Bool indicating whether the filenames are the same.
*/
static Bool samefile(ctmbstr filename1, /**< First filename */
ctmbstr filename2 /**< Second filename */
)
{
#if FILENAMES_CASE_SENSITIVE
return ( strcmp( filename1, filename2 ) == 0 );
#else
return ( strcasecmp( filename1, filename2 ) == 0 );
#endif
}
/** Handles exit cleanup.
*/
static void tidy_cleanup( void )
{
#if defined(_WIN32)
/* Restore original Windows code page. */
SetConsoleOutputCP(win_cp);
#endif
}
/** Exits with an error in the event of an out of memory condition.
*/
static void outOfMemory(void)
{
fprintf(stderr, "%s", tidyLocalizedString(TC_STRING_OUT_OF_MEMORY));
exit(1);
}
/** Create a new, allocated string with a format and arguments.
** @result Returns a new, allocated string that you must free.
*/
static tmbstr stringWithFormat(const ctmbstr fmt, /**< The format string. */
... /**< Variable arguments. */
)
{
va_list argList;
tmbstr result = NULL;
int len = 0;
va_start(argList, fmt);
len = vsnprintf( result, 0, fmt, argList );
va_end(argList);
if (!(result = malloc( len + 1) ))
outOfMemory();
va_start(argList, fmt);
vsnprintf( result, len + 1, fmt, argList);
va_end(argList);
return result;
}
/** @} end utilities_misc group */
/* MARK: - Output Helping Functions */
/***************************************************************************//**
** @defgroup utilities_output Output Helping Functions
** This group functions that aid the formatting of output.
*******************************************************************************
** @{
*/
/** Used by `print1Column`, `print2Columns` and `print3Columns` to manage
** wrapping text within columns.
** @result The pointer to the next part of the string to output.
*/
static const char *cutToWhiteSpace(const char *s, /**< starting point of desired string to output */
uint offset, /**< column width desired */
char *sbuf /**< the buffer to output */
)
{
if (!s)
{
sbuf[0] = '\0';
return NULL;
}
else if (strlen(s) <= offset)
{
strcpy(sbuf,s);
sbuf[offset] = '\0';
return NULL;
}
else
{
uint j, l, n;
/* scan forward looking for newline */
j = 0;
while(j < offset && s[j] != '\n')
++j;
if ( j == offset ) {
/* scan backward looking for first space */
j = offset;
while(j && s[j] != ' ')
--j;
l = j;
n = j+1;
/* no white space */
if (j==0)
{
l = offset;
n = offset;
}
} else
{
l = j;
n = j+1;
}
strncpy(sbuf,s,l);
sbuf[l] = '\0';
return s+n;
}
}
/** Outputs one column of text.
*/
static void print1Column(const char* fmt, /**< The format string for formatting the output. */
uint l1, /**< The width of the column. */
const char *c1 /**< The content of the column. */
)
{
const char *pc1=c1;
char *c1buf = (char *)malloc(l1+1);
if (!c1buf) outOfMemory();
do
{
pc1 = cutToWhiteSpace(pc1, l1, c1buf);
printf(fmt, c1buf[0] !='\0' ? c1buf : "");
} while (pc1);
free(c1buf);
}
/** Outputs two columns of text.
*/
static void print2Columns(const char* fmt, /**< The format string for formatting the output. */
uint l1, /**< The width of column 1. */
uint l2, /**< The width of column 2. */
const char *c1, /**< The contents of column 1. */
const char *c2 /**< The contents of column 2. */
)
{
const char *pc1=c1, *pc2=c2;
char *c1buf = (char *)malloc(l1+1);
char *c2buf = (char *)malloc(l2+1);
if (!c1buf) outOfMemory();
if (!c2buf) outOfMemory();
do
{
pc1 = cutToWhiteSpace(pc1, l1, c1buf);
pc2 = cutToWhiteSpace(pc2, l2, c2buf);
printf(fmt, l1, l1, c1buf[0]!='\0'?c1buf:"",
l2, l2, c2buf[0]!='\0'?c2buf:"");
} while (pc1 || pc2);
free(c1buf);
free(c2buf);
}
/** Outputs three columns of text.
*/
static void print3Columns(const char* fmt, /**< The three column format string. */
uint l1, /**< Width of column 1. */
uint l2, /**< Width of column 2. */
uint l3, /**< Width of column 3. */
const char *c1, /**< Content of column 1. */
const char *c2, /**< Content of column 2. */
const char *c3 /**< Content of column 3. */
)
{
const char *pc1=c1, *pc2=c2, *pc3=c3;
char *c1buf = (char *)malloc(l1+1);
char *c2buf = (char *)malloc(l2+1);
char *c3buf = (char *)malloc(l3+1);
if (!c1buf) outOfMemory();
if (!c2buf) outOfMemory();
if (!c3buf) outOfMemory();
do
{
pc1 = cutToWhiteSpace(pc1, l1, c1buf);
pc2 = cutToWhiteSpace(pc2, l2, c2buf);
pc3 = cutToWhiteSpace(pc3, l3, c3buf);
printf(fmt,
c1buf[0]!='\0'?c1buf:"",
c2buf[0]!='\0'?c2buf:"",
c3buf[0]!='\0'?c3buf:"");
} while (pc1 || pc2 || pc3);
free(c1buf);
free(c2buf);
free(c3buf);
}
/** Provides the `unknown option` output to the current errout.
*/
static void unknownOption(TidyDoc tdoc, /**< The Tidy document. */
uint c /**< The unknown option. */
)
{
fprintf( errout, tidyLocalizedString( TC_STRING_UNKNOWN_OPTION ), (char)c );
fprintf( errout, "\n");
}
/** @} end utilities_output group */
/* MARK: - CLI Options Utilities */
/***************************************************************************//**
** @defgroup options_cli CLI Options Utilities
** These structures, arrays, declarations, and definitions are used throughout
** this console application.
*******************************************************************************
** @{
*/
/** @name Format strings and decorations used in output.
** @{
*/
static const char helpfmt[] = " %-*.*s %-*.*s\n";
static const char helpul[] = "-----------------------------------------------------------------";
static const char fmt[] = "%-27.27s %-9.9s %-40.40s\n";
static const char ul[] = "=================================================================";
/** @} */
/** This enum is used to categorize the options for help output.
*/
typedef enum
{
CmdOptFileManip,
CmdOptCatFIRST = CmdOptFileManip,
CmdOptProcDir,
CmdOptCharEnc,
CmdOptMisc,
CmdOptXML,
CmdOptCatLAST
} CmdOptCategory;
/** This array contains headings that will be used in help output.
*/
static const struct {
ctmbstr mnemonic; /**< Used in XML as a class. */
uint key; /**< Key to fetch the localized string. */
} cmdopt_catname[] = {
{ "file-manip", TC_STRING_FILE_MANIP },
{ "process-directives", TC_STRING_PROCESS_DIRECTIVES },
{ "char-encoding", TC_STRING_CHAR_ENCODING },
{ "misc", TC_STRING_MISC },
{ "xml", TC_STRING_XML }
};
/** The struct and subsequent array keep the help output structured
** because we _also_ output all of this stuff as as XML.
*/
typedef struct {
CmdOptCategory cat; /**< Category */
ctmbstr name1; /**< Name */
uint key; /**< Key to fetch the localized description. */
uint subKey; /**< Secondary substitution key. */
ctmbstr eqconfig; /**< Equivalent configuration option */
ctmbstr name2; /**< Name */
ctmbstr name3; /**< Name */
} CmdOptDesc;
/** All instances of %s will be substituted with localized string
** specified by the subKey field.
*/
static const CmdOptDesc cmdopt_defs[] = {
{ CmdOptFileManip, "-output <%s>", TC_OPT_OUTPUT, TC_LABEL_FILE, "output-file: <%s>", "-o <%s>" },
{ CmdOptFileManip, "-config <%s>", TC_OPT_CONFIG, TC_LABEL_FILE, NULL },
{ CmdOptFileManip, "-file <%s>", TC_OPT_FILE, TC_LABEL_FILE, "error-file: <%s>", "-f <%s>" },
{ CmdOptFileManip, "-modify", TC_OPT_MODIFY, 0, "write-back: yes", "-m" },
{ CmdOptProcDir, "-indent", TC_OPT_INDENT, 0, "indent: auto", "-i" },
{ CmdOptProcDir, "-wrap <%s>", TC_OPT_WRAP, TC_LABEL_COL, "wrap: <%s>", "-w <%s>" },
{ CmdOptProcDir, "-upper", TC_OPT_UPPER, 0, "uppercase-tags: yes", "-u" },
{ CmdOptProcDir, "-clean", TC_OPT_CLEAN, 0, "clean: yes", "-c" },
{ CmdOptProcDir, "-bare", TC_OPT_BARE, 0, "bare: yes", "-b" },
{ CmdOptProcDir, "-gdoc", TC_OPT_GDOC, 0, "gdoc: yes", "-g" },
{ CmdOptProcDir, "-numeric", TC_OPT_NUMERIC, 0, "numeric-entities: yes", "-n" },
{ CmdOptProcDir, "-errors", TC_OPT_ERRORS, 0, "markup: no", "-e" },
{ CmdOptProcDir, "-quiet", TC_OPT_QUIET, 0, "quiet: yes", "-q" },
{ CmdOptProcDir, "-omit", TC_OPT_OMIT, 0, "omit-optional-tags: yes" },
{ CmdOptProcDir, "-xml", TC_OPT_XML, 0, "input-xml: yes" },
{ CmdOptProcDir, "-asxml", TC_OPT_ASXML, 0, "output-xhtml: yes", "-asxhtml" },
{ CmdOptProcDir, "-ashtml", TC_OPT_ASHTML, 0, "output-html: yes" },
{ CmdOptProcDir, "-access <%s>", TC_OPT_ACCESS, TC_LABEL_LEVL, "accessibility-check: <%s>" },
{ CmdOptCharEnc, "-raw", TC_OPT_RAW, 0, NULL },
{ CmdOptCharEnc, "-ascii", TC_OPT_ASCII, 0, NULL },
{ CmdOptCharEnc, "-latin0", TC_OPT_LATIN0, 0, NULL },
{ CmdOptCharEnc, "-latin1", TC_OPT_LATIN1, 0, NULL },
#ifndef NO_NATIVE_ISO2022_SUPPORT
{ CmdOptCharEnc, "-iso2022", TC_OPT_ISO2022, 0, NULL },
#endif
{ CmdOptCharEnc, "-utf8", TC_OPT_UTF8, 0, NULL },
{ CmdOptCharEnc, "-mac", TC_OPT_MAC, 0, NULL },
{ CmdOptCharEnc, "-win1252", TC_OPT_WIN1252, 0, NULL },
{ CmdOptCharEnc, "-ibm858", TC_OPT_IBM858, 0, NULL },
{ CmdOptCharEnc, "-utf16le", TC_OPT_UTF16LE, 0, NULL },
{ CmdOptCharEnc, "-utf16be", TC_OPT_UTF16BE, 0, NULL },
{ CmdOptCharEnc, "-utf16", TC_OPT_UTF16, 0, NULL },
{ CmdOptCharEnc, "-big5", TC_OPT_BIG5, 0, NULL },
{ CmdOptCharEnc, "-shiftjis", TC_OPT_SHIFTJIS, 0, NULL },
{ CmdOptMisc, "-version", TC_OPT_VERSION, 0, NULL, "-v" },
{ CmdOptMisc, "-help", TC_OPT_HELP, 0, NULL, "-h", "-?" },
{ CmdOptMisc, "-help-config", TC_OPT_HELPCFG, 0, NULL },
{ CmdOptMisc, "-help-env", TC_OPT_HELPENV, 0, NULL },
{ CmdOptMisc, "-show-config", TC_OPT_SHOWCFG, 0, NULL },
{ CmdOptMisc, "-export-config", TC_OPT_EXP_CFG, 0, NULL },
{ CmdOptMisc, "-export-default-config", TC_OPT_EXP_DEF, 0, NULL },
{ CmdOptMisc, "-help-option <%s>", TC_OPT_HELPOPT, TC_LABEL_OPT, NULL },
{ CmdOptMisc, "-language <%s>", TC_OPT_LANGUAGE, TC_LABEL_LANG, "language: <%s>" },
{ CmdOptXML, "-xml-help", TC_OPT_XMLHELP, 0, NULL },
{ CmdOptXML, "-xml-config", TC_OPT_XMLCFG, 0, NULL },
{ CmdOptXML, "-xml-strings", TC_OPT_XMLSTRG, 0, NULL },
{ CmdOptXML, "-xml-error-strings", TC_OPT_XMLERRS, 0, NULL },
{ CmdOptXML, "-xml-options-strings", TC_OPT_XMLOPTS, 0, NULL },
{ CmdOptMisc, NULL, 0, 0, NULL }
};
/** Option names aren't localized, but the sample fields should be localized.
** For example, `<file>` should be `<archivo>` in Spanish.
** @param pos A CmdOptDesc array with fields that must be localized.
*/
static void localize_option_names( CmdOptDesc *pos)
{
ctmbstr fileString = tidyLocalizedString(pos->subKey);
pos->name1 = stringWithFormat(pos->name1, fileString);
if ( pos->name2 )
pos->name2 = stringWithFormat(pos->name2, fileString);
if ( pos->name3 )
pos->name3 = stringWithFormat(pos->name3, fileString);
if ( pos->eqconfig )
pos->eqconfig = stringWithFormat(pos->eqconfig, fileString);
}
/** Escape a name for XML output. For example, `-output <file>` becomes
** `-output <file>` for use in XML.
** @param name The option name to escape.
** @result Returns an allocated string.
*/
static tmbstr get_escaped_name( ctmbstr name )
{
tmbstr escpName;
char aux[2];
uint len = 0;
ctmbstr c;
for(c=name; *c!='\0'; ++c)
switch(*c)
{
case '<':
case '>':
len += 4;
break;
case '"':
len += 6;
break;
default:
len += 1;
break;
}
escpName = (tmbstr)malloc(len+1);
if (!escpName) outOfMemory();
escpName[0] = '\0';
aux[1] = '\0';
for(c=name; *c!='\0'; ++c)
switch(*c)
{
case '<':
strcat(escpName, "<");
break;
case '>':
strcat(escpName, ">");
break;
case '"':
strcat(escpName, """);
break;
default:
aux[0] = *c;
strcat(escpName, aux);
break;
}
return escpName;
}
/** @} end CLI Options Definitions Utilities group */
/* MARK: - Configuration Options Utilities */
/***************************************************************************//**
** @defgroup utilities_cli_options Configuration Options Utilities
** Provide utilities to manipulate configuration options for output.
*******************************************************************************
** @{
*/
/** Utility to determine if an option has a picklist.
** @param topt The option to check.
** @result Returns a Bool indicating whether the option has a picklist or not.
*/
static Bool hasPickList( TidyOption topt )
{
TidyIterator pos;
if ( tidyOptGetType( topt ) != TidyInteger)
return no;
pos = tidyOptGetPickList( topt );
return tidyOptGetNextPick( topt, &pos ) != NULL;
}
/** Returns the configuration category id for the specified configuration
** category id. This will be used as an XML class attribute value.
** @param id The TidyConfigCategory for which to lookup the category name.
** @result Returns the configuration category, such as "diagnostics".
*/
static ctmbstr ConfigCategoryId( TidyConfigCategory id )
{
if (id >= TidyDiagnostics && id <= TidyInternalCategory)
return tidyErrorCodeAsKey( id );
fprintf(stderr, tidyLocalizedString(TC_STRING_FATAL_ERROR), (int)id);
fprintf(stderr, "\n");
assert(0);
abort();
return "never_here"; /* only for the compiler warning */
}
/** Structure maintains a description of a configuration ption.
*/
typedef struct {
ctmbstr name; /**< Name */
ctmbstr cat; /**< Category */
uint catid; /**< Category ID */
ctmbstr type; /**< "String, ... */
ctmbstr vals; /**< Potential values. If NULL, use an external function */
ctmbstr def; /**< default */
tmbchar tempdefs[80]; /**< storage for default such as integer */
Bool haveVals; /**< if yes, vals is valid */
} OptionDesc;
/** A type for a function pointer for a function used to print out options
** descriptions.
** @param TidyDoc The document.
** @param TidyOption The Tidy option.
** @param OptionDesc A pointer to the option description structure.
*/
typedef void (*OptionFunc)( TidyDoc, TidyOption, OptionDesc * );
/** Create OptionDesc "d" related to "opt"
*/
static void GetOption(TidyDoc tdoc, /**< The tidy document. */
TidyOption topt, /**< The option to create a description for. */
OptionDesc *d /**< [out] The new option description. */
)
{
TidyOptionId optId = tidyOptGetId( topt );
TidyOptionType optTyp = tidyOptGetType( topt );
d->name = tidyOptGetName( topt );
d->cat = ConfigCategoryId( tidyOptGetCategory( topt ) );
d->catid = tidyOptGetCategory( topt );
d->vals = NULL;
d->def = NULL;
d->haveVals = yes;
/* Handle special cases first. */
switch ( optId )
{
case TidyInlineTags:
case TidyBlockTags:
case TidyEmptyTags:
case TidyPreTags:
d->type = "Tag Names";
d->vals = "tagX, tagY, ...";
d->def = NULL;
break;
case TidyPriorityAttributes:
d->type = "Attributes Names";
d->vals = "attributeX, attributeY, ...";
d->def = NULL;
break;
case TidyCharEncoding:
case TidyInCharEncoding:
case TidyOutCharEncoding:
d->type = "Encoding";
d->def = tidyOptGetEncName( tdoc, optId );
if (!d->def)
d->def = "?";
d->vals = NULL;
break;
/* General case will handle remaining */
default:
switch ( optTyp )
{
case TidyBoolean:
d->type = "Boolean";
d->def = tidyOptGetCurrPick( tdoc, optId );
break;
case TidyInteger:
if (hasPickList(topt))
{
d->type = "Enum";
d->def = tidyOptGetCurrPick( tdoc, optId );
}
else
{
uint idef;
d->type = "Integer";
if ( optId == TidyWrapLen )
d->vals = "0 (no wrapping), 1, 2, ...";
else
d->vals = "0, 1, 2, ...";
idef = tidyOptGetInt( tdoc, optId );
sprintf(d->tempdefs, "%u", idef);
d->def = d->tempdefs;
}
break;
case TidyString:
d->type = "String";
d->vals = NULL;
d->haveVals = no;
d->def = tidyOptGetValue( tdoc, optId );
break;
}
}
}
/** Array holding all options. Contains a trailing sentinel.
*/
typedef struct {
TidyOption topt[N_TIDY_OPTIONS];
} AllOption_t;
/** A simple option comparator, used for sorting the options.
** @result Returns an integer indicating the result of the comparison.
*/
static int cmpOpt(const void* e1_, /**< Item A to compare. */
const void *e2_ /**< Item B to compare. */
)
{
const TidyOption* e1 = (const TidyOption*)e1_;
const TidyOption* e2 = (const TidyOption*)e2_;
return strcmp(tidyOptGetName(*e1), tidyOptGetName(*e2));
}
/** Returns options sorted.
*/
static void getSortedOption(TidyDoc tdoc, /**< The Tidy document. */
AllOption_t *tOption /**< [out] The list of options. */
)
{
TidyIterator pos = tidyGetOptionList( tdoc );
uint i = 0;
while ( pos )
{
TidyOption topt = tidyGetNextOption( tdoc, &pos );
tOption->topt[i] = topt;
++i;
}
tOption->topt[i] = NULL; /* sentinel */
qsort(tOption->topt,
i, /* there are i items, not including the sentinel */
sizeof(tOption->topt[0]),
cmpOpt);
}
/** An iterator for the sorted options.
*/
static void ForEachSortedOption(TidyDoc tdoc, /**< The Tidy document. */
OptionFunc OptionPrint /**< The printing function to be used. */
)
{
AllOption_t tOption;
const TidyOption *topt;
getSortedOption( tdoc, &tOption );
for( topt = tOption.topt; *topt; ++topt)
{
OptionDesc d;
GetOption( tdoc, *topt, &d );
(*OptionPrint)( tdoc, *topt, &d );
}
}
/** An iterator for the unsorted options.
*/
static void ForEachOption(TidyDoc tdoc, /**< The Tidy document. */
OptionFunc OptionPrint /**< The printing function to be used. */
)
{
TidyIterator pos = tidyGetOptionList( tdoc );
while ( pos )
{
TidyOption topt = tidyGetNextOption( tdoc, &pos );
OptionDesc d;
GetOption( tdoc, topt, &d );
(*OptionPrint)( tdoc, topt, &d );
}
}
/** Prints an option's allowed value as specified in its pick list.
** @param topt The Tidy option.
*/
static void PrintAllowedValuesFromPick( TidyOption topt )
{
TidyIterator pos = tidyOptGetPickList( topt );
Bool first = yes;
ctmbstr def;
while ( pos )
{
if (first)
first = no;
else
printf(", ");
def = tidyOptGetNextPick( topt, &pos );
printf("%s", def);
}
}
/** Prints an option's allowed values.
*/
static void PrintAllowedValues(TidyOption topt, /**< The Tidy option. */
const OptionDesc *d /**< The OptionDesc for the option. */
)
{
if (d->vals)
printf( "%s", d->vals );
else
PrintAllowedValuesFromPick( topt );
}
/** @} end utilities_cli_options group */
/* MARK: - Provide the -help Service */
/***************************************************************************//**
** @defgroup service_help Provide the -help Service
*******************************************************************************
** @{
*/
/** Retrieve the option's name(s) from the structure as a single string,
** localizing the field values if application. For example, this might
** return `-output <file>, -o <file>`.
** @param pos A CmdOptDesc array item for which to get the names.
** @result Returns the name(s) for the option as a single string.
*/
static tmbstr get_option_names( const CmdOptDesc* pos )
{
tmbstr name;
uint len;
CmdOptDesc localPos = *pos;
localize_option_names( &localPos );
len = strlen(localPos.name1);
if (localPos.name2)
len += 2+strlen(localPos.name2);
if (localPos.name3)
len += 2+strlen(localPos.name3);
name = (tmbstr)malloc(len+1);
if (!name) outOfMemory();
strcpy(name, localPos.name1);
free((tmbstr)localPos.name1);
if (localPos.name2)
{
strcat(name, ", ");
strcat(name, localPos.name2);
free((tmbstr)localPos.name2);
}
if (localPos.name3)
{
strcat(name, ", ");
strcat(name, localPos.name3);
free((tmbstr)localPos.name3);
}
return name;
}
/** Returns the final name of the tidy executable by eliminating the path
** name components from the executable name.
** @param prog The path of the current executable.
*/
static ctmbstr get_final_name( ctmbstr prog )
{
ctmbstr name = prog;
int c;
size_t i;
size_t len = strlen(prog);
for (i = 0; i < len; i++)
{
c = prog[i];
if ((( c == '/' ) || ( c == '\\' )) && prog[i+1])
{
name = &prog[i+1];
}
}
return name;
}
/** Outputs all of the complete help options (text).
** @param tdoc The Tidydoc whose options are being printed.
*/
static void print_help_options( TidyDoc tdoc )
{
CmdOptCategory cat = CmdOptCatFIRST;
const CmdOptDesc* pos = cmdopt_defs;
uint col1, col2;
uint width = 78;
for( cat=CmdOptCatFIRST; cat!=CmdOptCatLAST; ++cat)
{
ctmbstr name = tidyLocalizedString(cmdopt_catname[cat].key);
size_t len = width < strlen(name) ? width : strlen(name);
printf( "%s\n", name );
printf( "%*.*s\n", (int)len, (int)len, helpul );
/* Tidy's "standard" 78-column output was always 25:52 ratio, so let's
try to preserve this approximately 1:2 ratio regardless of whatever
silly thing the user might have set for a console width, with a
maximum of 50 characters for the first column.
*/
col1 = width / 3; /* one third of the available */
col1 = col1 < 1 ? 1 : col1; /* at least 1 */
col1 = col1 > 35 ? 35 : col1; /* no greater than 35 */
col2 = width - col1 - 2; /* allow two spaces */
col2 = col2 < 1 ? 1 : col2; /* at least 1 */
for( pos=cmdopt_defs; pos->name1; ++pos)
{
tmbstr name;
if (pos->cat != cat)
continue;
name = get_option_names( pos );
print2Columns( helpfmt, col1, col2, name, tidyLocalizedString( pos->key ) );
free(name);
}
printf("\n");
}
}
/** Handles the -help service.
*/
static void help(TidyDoc tdoc, /**< The tidy document for which help is showing. */
ctmbstr prog /**< The path of the current executable. */
)
{
tmbstr temp_string = NULL;
uint width = 78;
printf( tidyLocalizedString(TC_TXT_HELP_1), get_final_name(prog), tidyLibraryVersion() );
printf("\n");
if ( tidyPlatform() )
temp_string = stringWithFormat( tidyLocalizedString(TC_TXT_HELP_2A), tidyPlatform() );
else
temp_string = stringWithFormat( tidyLocalizedString(TC_TXT_HELP_2B) );
width = width < strlen(temp_string) ? width : strlen(temp_string);
printf( "%s\n", temp_string );
printf( "%*.*s\n\n", width, width, ul);
free( temp_string );
print_help_options( tdoc );
printf("\n");
#if defined(TIDY_CONFIG_FILE) && defined(TIDY_USER_CONFIG_FILE)
temp_string = stringWithFormat( tidyLocalizedString(TC_TXT_HELP_3A), TIDY_CONFIG_FILE, TIDY_USER_CONFIG_FILE );
printf( tidyLocalizedString(TC_TXT_HELP_3), temp_string );
free( temp_string );
#else
printf( tidyLocalizedString(TC_TXT_HELP_3), "\n" );
#endif
printf("\n");
}
/** @} end service_help group */
/* MARK: - Provide the -help-config Service */
/***************************************************************************//**
** @defgroup service_help_config Provide the -help-config Service
*******************************************************************************
** @{
*/
/** Retrieves allowed values from an option's pick list.
** @param topt A TidyOption for which to get the allowed values.
** @result A string containing the allowed values.
*/
static tmbstr GetAllowedValuesFromPick( TidyOption topt )
{
TidyIterator pos;
Bool first;
ctmbstr def;
uint len = 0;
tmbstr val;
pos = tidyOptGetPickList( topt );
first = yes;
while ( pos )
{
if (first)
first = no;
else
len += 2;
def = tidyOptGetNextPick( topt, &pos );
len += strlen(def);
}
val = (tmbstr)malloc(len+1);
if (!val) outOfMemory();
val[0] = '\0';
pos = tidyOptGetPickList( topt );
first = yes;
while ( pos )
{
if (first)
first = no;
else
strcat(val, ", ");
def = tidyOptGetNextPick( topt, &pos );
strcat(val, def);
}
return val;
}
/** Retrieves allowed values for an option.
** @result A string containing the allowed values.
*/
static tmbstr GetAllowedValues(TidyOption topt, /**< A TidyOption for which to get the allowed values. */
const OptionDesc *d /**< A pointer to the OptionDesc array. */
)
{
if (d->vals)
{
tmbstr val = (tmbstr)malloc(1+strlen(d->vals));
if (!val) outOfMemory();
strcpy(val, d->vals);
return val;
}
else
return GetAllowedValuesFromPick( topt );
}
/** Prints a single option.
*/
static void printOption(TidyDoc ARG_UNUSED(tdoc), /**< The Tidy document. */
TidyOption topt, /**< The option to print. */
OptionDesc *d /**< A pointer to the OptionDesc array. */
)
{
if (tidyOptGetCategory( topt ) == TidyInternalCategory )
return;
if ( *d->name || *d->type )
{
ctmbstr pval = d->vals;
tmbstr val = NULL;
if (!d->haveVals)
{
pval = "-";
}
else if (pval == NULL)
{
val = GetAllowedValues( topt, d);
pval = val;
}
print3Columns( fmt, 27, 9, 40, d->name, d->type, pval );
if (val)
free(val);
}
}
/** Handles the -help-config service.
** @remark We will not support console word wrapping for the configuration
** options table. If users really have a small console, then they
* should make it wider or output to a file.
** @param tdoc The Tidy document.
*/
static void optionhelp( TidyDoc tdoc )
{
printf( "\n" );
printf( "%s", tidyLocalizedString( TC_TXT_HELP_CONFIG ) );
printf( fmt,
tidyLocalizedString( TC_TXT_HELP_CONFIG_NAME ),
tidyLocalizedString( TC_TXT_HELP_CONFIG_TYPE ),
tidyLocalizedString( TC_TXT_HELP_CONFIG_ALLW ) );
printf( fmt, ul, ul, ul );
ForEachSortedOption( tdoc, printOption );
}
/** @} end service_help_config group */
/* MARK: - Provide the -help-env Service */
/***************************************************************************//**
** @defgroup service_help_env Provide the -help-env Service
*******************************************************************************
** @{
*/