~ubuntu-branches/ubuntu/lucid/tidy/lucid

« back to all changes in this revision

Viewing changes to src/config.c

  • Committer: Bazaar Package Importer
  • Author(s): Jason Thomas
  • Date: 2008-01-20 21:46:03 UTC
  • mfrom: (3.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080120214603-poklofici9og61tx
Tags: 20080116cvs-2
* debian/control: build depends on xsltproc
  (closes: #461608)
* debian/tidy.preinst,postinst: add code to move old config file
  (closes: #461623)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
  config.c -- read config file and manage config properties
3
3
  
4
 
  (c) 1998-2005 (W3C) MIT, ERCIM, Keio University
 
4
  (c) 1998-2007 (W3C) MIT, ERCIM, Keio University
5
5
  See tidy.h for the copyright notice.
6
6
 
7
7
  CVS Info :
8
8
 
9
9
    $Author: arnaud02 $ 
10
 
    $Date: 2005/08/16 17:02:18 $ 
11
 
    $Revision: 1.92 $ 
 
10
    $Date: 2007/08/13 16:27:26 $ 
 
11
    $Revision: 1.109 $ 
12
12
 
13
13
*/
14
14
 
52
52
#include "win32tc.h"
53
53
#endif
54
54
 
55
 
void InitConfig( TidyDocImpl* doc )
 
55
void TY_(InitConfig)( TidyDocImpl* doc )
56
56
{
57
 
    ClearMemory( &doc->config, sizeof(TidyConfigImpl) );
58
 
    ResetConfigToDefault( doc );
 
57
    TidyClearMemory( &doc->config, sizeof(TidyConfigImpl) );
 
58
    TY_(ResetConfigToDefault)( doc );
59
59
}
60
60
 
61
 
void FreeConfig( TidyDocImpl* doc )
 
61
void TY_(FreeConfig)( TidyDocImpl* doc )
62
62
{
63
 
    ResetConfigToDefault( doc );
64
 
    TakeConfigSnapshot( doc );
 
63
    TY_(ResetConfigToDefault)( doc );
 
64
    TY_(TakeConfigSnapshot)( doc );
65
65
}
66
66
 
67
67
 
144
144
  NULL 
145
145
};
146
146
 
 
147
static const ctmbstr sorterPicks[] = 
 
148
{
 
149
  "none",
 
150
  "alpha",
 
151
  NULL
 
152
};
 
153
 
147
154
#define MU TidyMarkup
148
155
#define DG TidyDiagnostics
149
156
#define PP TidyPrettyPrint
166
173
#define ParseAcc NULL 
167
174
#endif
168
175
 
 
176
static void AdjustConfig( TidyDocImpl* doc );
 
177
 
 
178
/* parser for integer values */
 
179
static ParseProperty ParseInt;
 
180
 
 
181
/* parser for 't'/'f', 'true'/'false', 'y'/'n', 'yes'/'no' or '1'/'0' */
 
182
static ParseProperty ParseBool;
 
183
 
 
184
/* parser for 't'/'f', 'true'/'false', 'y'/'n', 'yes'/'no', '1'/'0'
 
185
   or 'auto' */
 
186
static ParseProperty ParseAutoBool;
 
187
 
 
188
/* a string excluding whitespace */
 
189
static ParseProperty ParseName;
 
190
 
 
191
/* a CSS1 selector - CSS class naming for -clean option */
 
192
static ParseProperty ParseCSS1Selector;
 
193
 
 
194
/* a string including whitespace */
 
195
static ParseProperty ParseString;
 
196
 
 
197
/* a space or comma separated list of tag names */
 
198
static ParseProperty ParseTagNames;
 
199
 
 
200
/* alpha */
 
201
static ParseProperty ParseSorter;
 
202
 
 
203
/* RAW, ASCII, LATIN0, LATIN1, UTF8, ISO2022, MACROMAN, 
 
204
   WIN1252, IBM858, UTF16LE, UTF16BE, UTF16, BIG5, SHIFTJIS
 
205
*/
 
206
static ParseProperty ParseCharEnc;
 
207
static ParseProperty ParseNewline;
 
208
 
 
209
/* omit | auto | strict | loose | <fpi> */
 
210
static ParseProperty ParseDocType;
 
211
 
 
212
/* keep-first or keep-last? */
 
213
static ParseProperty ParseRepeatAttr;
 
214
 
 
215
 
169
216
static const TidyOptionImpl option_defs[] =
170
217
{
171
218
  { TidyUnknownOption,           MS, "unknown!",                    IN, 0,               NULL,              NULL            },
233
280
  { TidyEmacs,                   MS, "gnu-emacs",                   BL, no,              ParseBool,         boolPicks       },
234
281
  { TidyEmacsFile,               MS, "gnu-emacs-file",              ST, 0,               ParseString,       NULL            },
235
282
  { TidyLiteralAttribs,          MU, "literal-attributes",          BL, no,              ParseBool,         boolPicks       },
236
 
  { TidyBodyOnly,                MU, "show-body-only",              BL, no,              ParseBool,         boolPicks       },
 
283
  { TidyBodyOnly,                MU, "show-body-only",              IN, no,              ParseAutoBool,     autoBoolPicks   },
237
284
  { TidyFixUri,                  MU, "fix-uri",                     BL, yes,             ParseBool,         boolPicks       },
238
285
  { TidyLowerLiterals,           MU, "lower-literals",              BL, yes,             ParseBool,         boolPicks       },
239
286
  { TidyHideComments,            MU, "hide-comments",               BL, no,              ParseBool,         boolPicks       },
263
310
  { TidyPunctWrap,               PP, "punctuation-wrap",            BL, no,              ParseBool,         boolPicks       },
264
311
#endif
265
312
  { TidyMergeDivs,               MU, "merge-divs",                  IN, TidyAutoState,   ParseAutoBool,     autoBoolPicks   },
 
313
  { TidyDecorateInferredUL,      MU, "decorate-inferred-ul",        BL, no,              ParseBool,         boolPicks       },
 
314
  { TidyPreserveEntities,        MU, "preserve-entities",           BL, no,              ParseBool,         boolPicks       },
 
315
  { TidySortAttributes,          PP, "sort-attributes",             IN, TidySortAttrNone,ParseSorter,       sorterPicks     },
 
316
  { TidyMergeSpans,               MU, "merge-spans",                IN, TidyAutoState,   ParseAutoBool,     autoBoolPicks   },
266
317
  { N_TIDY_OPTIONS,              XX, NULL,                          XY, 0,               NULL,              NULL            }
267
318
};
268
319
 
270
321
** thus, it is cheaper to do a few scans than set
271
322
** up every option in a hash table.
272
323
*/
273
 
const TidyOptionImpl* lookupOption( ctmbstr s )
 
324
const TidyOptionImpl* TY_(lookupOption)( ctmbstr s )
274
325
{
275
326
    const TidyOptionImpl* np = option_defs;
276
327
    for ( /**/; np < option_defs + N_TIDY_OPTIONS; ++np )
277
328
    {
278
 
        if ( tmbstrcasecmp(s, np->name) == 0 )
 
329
        if ( TY_(tmbstrcasecmp)(s, np->name) == 0 )
279
330
            return np;
280
331
    }
281
332
    return NULL;
282
333
}
283
334
 
284
 
const TidyOptionImpl* getOption( TidyOptionId optId )
 
335
const TidyOptionImpl* TY_(getOption)( TidyOptionId optId )
285
336
{
286
337
  if ( optId < N_TIDY_OPTIONS )
287
338
      return option_defs + optId;
289
340
}
290
341
 
291
342
 
292
 
static void FreeOptionValue( const TidyOptionImpl* option, ulong value )
 
343
static void FreeOptionValue( TidyDocImpl* doc, const TidyOptionImpl* option, TidyOptionValue* value )
293
344
{
294
 
    if ( value && option->type == TidyString && value != option->dflt )
295
 
    {
296
 
        MemFree( (void*) value );
297
 
    }
 
345
    if ( option->type == TidyString && value->p && value->p != option->pdflt )
 
346
        TidyDocFree( doc, value->p );
298
347
}
299
348
 
300
 
static void CopyOptionValue( const TidyOptionImpl* option,
301
 
                             ulong* oldval, ulong newval )
 
349
static void CopyOptionValue( TidyDocImpl* doc, const TidyOptionImpl* option,
 
350
                             TidyOptionValue* oldval, const TidyOptionValue* newval )
302
351
{
303
352
    assert( oldval != NULL );
304
 
    FreeOptionValue( option, *oldval );
 
353
    FreeOptionValue( doc, option, oldval );
305
354
 
306
 
    if ( newval && option->type == TidyString && newval != option->dflt )
307
 
        *oldval = (ulong) tmbstrdup( (ctmbstr) newval );
 
355
    if ( option->type == TidyString )
 
356
    {
 
357
        if ( newval->p && newval->p != option->pdflt )
 
358
            oldval->p = TY_(tmbstrdup)( doc->allocator, newval->p );
 
359
        else
 
360
            oldval->p = newval->p;
 
361
    }
308
362
    else
309
 
        *oldval = newval;
 
363
        oldval->v = newval->v;
310
364
}
311
365
 
312
366
 
313
 
Bool SetOptionValue( TidyDocImpl* doc, TidyOptionId optId, ctmbstr val )
 
367
static Bool SetOptionValue( TidyDocImpl* doc, TidyOptionId optId, ctmbstr val )
314
368
{
315
369
   const TidyOptionImpl* option = &option_defs[ optId ];
316
370
   Bool status = ( optId < N_TIDY_OPTIONS );
317
371
   if ( status )
318
372
   {
319
373
      assert( option->id == optId && option->type == TidyString );
320
 
      FreeOptionValue( option, doc->config.value[ optId ] );
321
 
      doc->config.value[ optId ] = (ulong) tmbstrdup( val );
 
374
      FreeOptionValue( doc, option, &doc->config.value[ optId ] );
 
375
      doc->config.value[ optId ].p = TY_(tmbstrdup)( doc->allocator, val );
322
376
   }
323
377
   return status;
324
378
}
325
379
 
326
 
Bool SetOptionInt( TidyDocImpl* doc, TidyOptionId optId, ulong val )
 
380
Bool TY_(SetOptionInt)( TidyDocImpl* doc, TidyOptionId optId, ulong val )
327
381
{
328
382
   Bool status = ( optId < N_TIDY_OPTIONS );
329
383
   if ( status )
330
384
   {
331
385
       assert( option_defs[ optId ].type == TidyInteger );
332
 
       doc->config.value[ optId ] = val;
 
386
       doc->config.value[ optId ].v = val;
333
387
   }
334
388
   return status;
335
389
}
336
390
 
337
 
Bool SetOptionBool( TidyDocImpl* doc, TidyOptionId optId, Bool val )
 
391
Bool TY_(SetOptionBool)( TidyDocImpl* doc, TidyOptionId optId, Bool val )
338
392
{
339
393
   Bool status = ( optId < N_TIDY_OPTIONS );
340
394
   if ( status )
341
395
   {
342
396
       assert( option_defs[ optId ].type == TidyBoolean );
343
 
       doc->config.value[ optId ] = val;
 
397
       doc->config.value[ optId ].v = val;
344
398
   }
345
399
   return status;
346
400
}
347
401
 
348
 
Bool ResetOptionToDefault( TidyDocImpl* doc, TidyOptionId optId )
 
402
static void GetOptionDefault( const TidyOptionImpl* option,
 
403
                              TidyOptionValue* dflt )
 
404
{
 
405
    if ( option->type == TidyString )
 
406
        dflt->p = (char*)option->pdflt;
 
407
    else
 
408
        dflt->v = option->dflt;
 
409
}
 
410
 
 
411
static Bool OptionValueEqDefault( const TidyOptionImpl* option,
 
412
                                  const TidyOptionValue* val )
 
413
{
 
414
    return ( option->type == TidyString ) ?
 
415
        val->p == option->pdflt :
 
416
        val->v == option->dflt;
 
417
}
 
418
 
 
419
Bool TY_(ResetOptionToDefault)( TidyDocImpl* doc, TidyOptionId optId )
349
420
{
350
421
    Bool status = ( optId > 0 && optId < N_TIDY_OPTIONS );
351
422
    if ( status )
352
423
    {
 
424
        TidyOptionValue dflt;
353
425
        const TidyOptionImpl* option = option_defs + optId;
354
 
        ulong* value = &doc->config.value[ optId ];
 
426
        TidyOptionValue* value = &doc->config.value[ optId ];
355
427
        assert( optId == option->id );
356
 
        CopyOptionValue( option, value, option->dflt );
 
428
        GetOptionDefault( option, &dflt );
 
429
        CopyOptionValue( doc, option, value, &dflt );
357
430
    }
358
431
    return status;
359
432
}
361
434
static void ReparseTagType( TidyDocImpl* doc, TidyOptionId optId )
362
435
{
363
436
    ctmbstr tagdecl = cfgStr( doc, optId );
364
 
    tmbstr dupdecl = tmbstrdup( tagdecl );
365
 
    ParseConfigValue( doc, optId, dupdecl );
366
 
    MemFree( dupdecl );
367
 
}
368
 
 
369
 
/* Not efficient, but effective */
370
 
static void ReparseTagDecls( TidyDocImpl* doc )
371
 
{
372
 
    FreeDeclaredTags( doc, tagtype_null );
373
 
    if ( cfg(doc, TidyInlineTags) )
374
 
        ReparseTagType( doc, TidyInlineTags );
375
 
    if ( cfg(doc, TidyBlockTags) )
376
 
        ReparseTagType( doc, TidyBlockTags );
377
 
    if ( cfg(doc, TidyEmptyTags) )
378
 
        ReparseTagType( doc, TidyEmptyTags );
379
 
    if ( cfg(doc, TidyPreTags) )
380
 
        ReparseTagType( doc, TidyPreTags );
381
 
}
382
 
 
383
 
void ResetConfigToDefault( TidyDocImpl* doc )
384
 
{
385
 
    uint ixVal;
386
 
    const TidyOptionImpl* option = option_defs;
387
 
    ulong* value = &doc->config.value[ 0 ];
388
 
    for ( ixVal=0; ixVal < N_TIDY_OPTIONS; ++option, ++ixVal )
389
 
    {
390
 
        assert( ixVal == (uint) option->id );
391
 
        CopyOptionValue( option, &value[ixVal], option->dflt );
392
 
    }
393
 
    FreeDeclaredTags( doc, tagtype_null );
394
 
}
395
 
 
396
 
void TakeConfigSnapshot( TidyDocImpl* doc )
397
 
{
398
 
    uint ixVal;
399
 
    const TidyOptionImpl* option = option_defs;
400
 
    ulong* value = &doc->config.value[ 0 ];
401
 
    ulong* snap  = &doc->config.snapshot[ 0 ];
 
437
    tmbstr dupdecl = TY_(tmbstrdup)( doc->allocator, tagdecl );
 
438
    TY_(ParseConfigValue)( doc, optId, dupdecl );
 
439
    TidyDocFree( doc, dupdecl );
 
440
}
 
441
 
 
442
static Bool OptionValueIdentical( const TidyOptionImpl* option,
 
443
                                  const TidyOptionValue* val1,
 
444
                                  const TidyOptionValue* val2 )
 
445
{
 
446
    if ( option->type == TidyString )
 
447
    {
 
448
        if ( val1->p == val2->p )
 
449
            return yes;
 
450
        if ( !val1->p || !val2->p )
 
451
            return no;
 
452
        return TY_(tmbstrcmp)( val1->p, val2->p ) == 0;
 
453
    }
 
454
    else
 
455
        return val1->v == val2->v;
 
456
}
 
457
 
 
458
static Bool NeedReparseTagDecls( const TidyOptionValue* current,
 
459
                                 const TidyOptionValue* new,
 
460
                                 uint *changedUserTags )
 
461
{
 
462
    Bool ret = no;
 
463
    uint ixVal;
 
464
    const TidyOptionImpl* option = option_defs;
 
465
    *changedUserTags = tagtype_null;
 
466
 
 
467
    for ( ixVal=0; ixVal < N_TIDY_OPTIONS; ++option, ++ixVal )
 
468
    {
 
469
        assert( ixVal == (uint) option->id );
 
470
        switch (option->id)
 
471
        {
 
472
#define TEST_USERTAGS(USERTAGOPTION,USERTAGTYPE) \
 
473
        case USERTAGOPTION: \
 
474
            if (!OptionValueIdentical(option,&current[ixVal],&new[ixVal])) \
 
475
            { \
 
476
                *changedUserTags |= USERTAGTYPE; \
 
477
                ret = yes; \
 
478
            } \
 
479
            break
 
480
            TEST_USERTAGS(TidyInlineTags,tagtype_inline);
 
481
            TEST_USERTAGS(TidyBlockTags,tagtype_block);
 
482
            TEST_USERTAGS(TidyEmptyTags,tagtype_empty);
 
483
            TEST_USERTAGS(TidyPreTags,tagtype_pre);
 
484
        }
 
485
    }
 
486
    return ret;
 
487
}
 
488
 
 
489
static void ReparseTagDecls( TidyDocImpl* doc, uint changedUserTags  )
 
490
{
 
491
#define REPARSE_USERTAGS(USERTAGTYPE) \
 
492
    if ( changedUserTags & USERTAGTYPE ) \
 
493
    { \
 
494
        TY_(FreeDeclaredTags)( doc, USERTAGTYPE ); \
 
495
        ReparseTagType( doc, USERTAGTYPE ); \
 
496
    }
 
497
    REPARSE_USERTAGS(TidyInlineTags);
 
498
    REPARSE_USERTAGS(TidyBlockTags);
 
499
    REPARSE_USERTAGS(TidyEmptyTags);
 
500
    REPARSE_USERTAGS(TidyPreTags);
 
501
}
 
502
 
 
503
void TY_(ResetConfigToDefault)( TidyDocImpl* doc )
 
504
{
 
505
    uint ixVal;
 
506
    const TidyOptionImpl* option = option_defs;
 
507
    TidyOptionValue* value = &doc->config.value[ 0 ];
 
508
    for ( ixVal=0; ixVal < N_TIDY_OPTIONS; ++option, ++ixVal )
 
509
    {
 
510
        TidyOptionValue dflt;
 
511
        assert( ixVal == (uint) option->id );
 
512
        GetOptionDefault( option, &dflt );
 
513
        CopyOptionValue( doc, option, &value[ixVal], &dflt );
 
514
    }
 
515
    TY_(FreeDeclaredTags)( doc, tagtype_null );
 
516
}
 
517
 
 
518
void TY_(TakeConfigSnapshot)( TidyDocImpl* doc )
 
519
{
 
520
    uint ixVal;
 
521
    const TidyOptionImpl* option = option_defs;
 
522
    const TidyOptionValue* value = &doc->config.value[ 0 ];
 
523
    TidyOptionValue* snap  = &doc->config.snapshot[ 0 ];
402
524
 
403
525
    AdjustConfig( doc );  /* Make sure it's consistent */
404
526
    for ( ixVal=0; ixVal < N_TIDY_OPTIONS; ++option, ++ixVal )
405
527
    {
406
528
        assert( ixVal == (uint) option->id );
407
 
        CopyOptionValue( option, &snap[ixVal], value[ixVal] );
 
529
        CopyOptionValue( doc, option, &snap[ixVal], &value[ixVal] );
408
530
    }
409
531
}
410
532
 
411
 
void ResetConfigToSnapshot( TidyDocImpl* doc )
 
533
void TY_(ResetConfigToSnapshot)( TidyDocImpl* doc )
412
534
{
413
535
    uint ixVal;
414
536
    const TidyOptionImpl* option = option_defs;
415
 
    ulong* value = &doc->config.value[ 0 ];
416
 
    ulong* snap  = &doc->config.snapshot[ 0 ];
417
 
 
 
537
    TidyOptionValue* value = &doc->config.value[ 0 ];
 
538
    const TidyOptionValue* snap  = &doc->config.snapshot[ 0 ];
 
539
    uint changedUserTags;
 
540
    Bool needReparseTagsDecls = NeedReparseTagDecls( value, snap,
 
541
                                                     &changedUserTags );
 
542
    
418
543
    for ( ixVal=0; ixVal < N_TIDY_OPTIONS; ++option, ++ixVal )
419
544
    {
420
545
        assert( ixVal == (uint) option->id );
421
 
        CopyOptionValue( option, &value[ixVal], snap[ixVal] );
 
546
        CopyOptionValue( doc, option, &value[ixVal], &snap[ixVal] );
422
547
    }
423
 
    FreeDeclaredTags( doc, tagtype_null );
424
 
    ReparseTagDecls( doc );
 
548
    if ( needReparseTagsDecls )
 
549
        ReparseTagDecls( doc, changedUserTags );
425
550
}
426
551
 
427
 
void CopyConfig( TidyDocImpl* docTo, TidyDocImpl* docFrom )
 
552
void TY_(CopyConfig)( TidyDocImpl* docTo, TidyDocImpl* docFrom )
428
553
{
429
554
    if ( docTo != docFrom )
430
555
    {
431
556
        uint ixVal;
432
557
        const TidyOptionImpl* option = option_defs;
433
 
        ulong* from = &docFrom->config.value[ 0 ];
434
 
        ulong* to   = &docTo->config.value[ 0 ];
 
558
        const TidyOptionValue* from = &docFrom->config.value[ 0 ];
 
559
        TidyOptionValue* to   = &docTo->config.value[ 0 ];
 
560
        uint changedUserTags;
 
561
        Bool needReparseTagsDecls = NeedReparseTagDecls( to, from,
 
562
                                                         &changedUserTags );
435
563
 
436
 
        TakeConfigSnapshot( docTo );
 
564
        TY_(TakeConfigSnapshot)( docTo );
437
565
        for ( ixVal=0; ixVal < N_TIDY_OPTIONS; ++option, ++ixVal )
438
566
        {
439
567
            assert( ixVal == (uint) option->id );
440
 
            CopyOptionValue( option, &to[ixVal], from[ixVal] );
 
568
            CopyOptionValue( docTo, option, &to[ixVal], &from[ixVal] );
441
569
        }
442
 
        ReparseTagDecls( docTo );
 
570
        if ( needReparseTagsDecls )
 
571
            ReparseTagDecls( docTo, changedUserTags  );
443
572
        AdjustConfig( docTo );  /* Make sure it's consistent */
444
573
    }
445
574
}
448
577
#ifdef _DEBUG
449
578
 
450
579
/* Debug accessor functions will be type-safe and assert option type match */
451
 
ulong   _cfgGet( TidyDocImpl* doc, TidyOptionId optId )
 
580
ulong   TY_(_cfgGet)( TidyDocImpl* doc, TidyOptionId optId )
452
581
{
453
582
  assert( optId < N_TIDY_OPTIONS );
454
 
  return doc->config.value[ optId ];
 
583
  return doc->config.value[ optId ].v;
455
584
}
456
585
 
457
 
Bool    _cfgGetBool( TidyDocImpl* doc, TidyOptionId optId )
 
586
Bool    TY_(_cfgGetBool)( TidyDocImpl* doc, TidyOptionId optId )
458
587
{
459
 
  ulong val = _cfgGet( doc, optId );
 
588
  ulong val = TY_(_cfgGet)( doc, optId );
460
589
  const TidyOptionImpl* opt = &option_defs[ optId ];
461
590
  assert( opt && opt->type == TidyBoolean );
462
591
  return (Bool) val;
463
592
}
464
593
 
465
 
TidyTriState    _cfgGetAutoBool( TidyDocImpl* doc, TidyOptionId optId )
 
594
TidyTriState    TY_(_cfgGetAutoBool)( TidyDocImpl* doc, TidyOptionId optId )
466
595
{
467
 
  ulong val = _cfgGet( doc, optId );
 
596
  ulong val = TY_(_cfgGet)( doc, optId );
468
597
  const TidyOptionImpl* opt = &option_defs[ optId ];
469
 
  assert( opt && opt->type == TidyInteger );
 
598
  assert( opt && opt->type == TidyInteger
 
599
          && opt->parser == ParseAutoBool );
470
600
  return (TidyTriState) val;
471
601
}
472
602
 
473
 
ctmbstr _cfgGetString( TidyDocImpl* doc, TidyOptionId optId )
 
603
ctmbstr TY_(_cfgGetString)( TidyDocImpl* doc, TidyOptionId optId )
474
604
{
475
 
  ulong val = _cfgGet( doc, optId );
476
 
  const TidyOptionImpl* opt = &option_defs[ optId ];
 
605
  const TidyOptionImpl* opt;
 
606
 
 
607
  assert( optId < N_TIDY_OPTIONS );
 
608
  opt = &option_defs[ optId ];
477
609
  assert( opt && opt->type == TidyString );
478
 
  return (ctmbstr) val;
 
610
  return doc->config.value[ optId ].p;
479
611
}
480
612
#endif
481
613
 
482
614
 
 
615
#if 0
483
616
/* for use with Gnu Emacs */
484
617
void SetEmacsFilename( TidyDocImpl* doc, ctmbstr filename )
485
618
{
486
619
    SetOptionValue( doc, TidyEmacsFile, filename );
487
620
}
488
 
 
 
621
#endif
489
622
 
490
623
static tchar GetC( TidyConfigImpl* config )
491
624
{
492
625
    if ( config->cfgIn )
493
 
        return ReadChar( config->cfgIn );
 
626
        return TY_(ReadChar)( config->cfgIn );
494
627
    return EndOfStream;
495
628
}
496
629
 
509
642
 
510
643
static tchar SkipWhite( TidyConfigImpl* config )
511
644
{
512
 
    while ( IsWhite(config->c) && !IsNewline(config->c) )
 
645
    while ( TY_(IsWhite)(config->c) && !TY_(IsNewline)(config->c) )
513
646
        config->c = GetC( config );
514
647
    return config->c;
515
648
}
546
679
        if ( config->c == '\n' )
547
680
            config->c = GetC( config );
548
681
    }
549
 
    while ( IsWhite(config->c) );  /* line continuation? */
 
682
    while ( TY_(IsWhite)(config->c) );  /* line continuation? */
550
683
 
551
684
    return config->c;
552
685
}
559
692
 work on systems that support getpwnam(userid), 
560
693
 namely Unix/Linux.
561
694
*/
562
 
ctmbstr ExpandTilde( ctmbstr filename )
 
695
static ctmbstr ExpandTilde( TidyDocImpl* doc, ctmbstr filename )
563
696
{
564
697
    char *home_dir = NULL;
565
698
 
585
718
        while ( *s && *s != '/' )
586
719
            s++;
587
720
 
588
 
        if ( t = MemAlloc(s - filename) )
 
721
        if ( t = TidyDocAlloc(doc, s - filename) )
589
722
        {
590
723
            memcpy(t, filename+1, s-filename-1);
591
724
            t[s-filename-1] = 0;
592
725
 
593
726
            passwd = getpwnam(t);
594
727
 
595
 
            MemFree(t);
 
728
            TidyDocFree(doc, t);
596
729
        }
597
730
 
598
731
        if ( passwd )
605
738
 
606
739
    if ( home_dir )
607
740
    {
608
 
        uint len = tmbstrlen(filename) + tmbstrlen(home_dir) + 1;
609
 
        tmbstr p = (tmbstr)MemAlloc( len );
610
 
        tmbstrcpy( p, home_dir );
611
 
        tmbstrcat( p, filename );
 
741
        uint len = TY_(tmbstrlen)(filename) + TY_(tmbstrlen)(home_dir) + 1;
 
742
        tmbstr p = (tmbstr)TidyDocAlloc( doc, len );
 
743
        TY_(tmbstrcpy)( p, home_dir );
 
744
        TY_(tmbstrcat)( p, filename );
612
745
        return (ctmbstr) p;
613
746
    }
614
747
    return (ctmbstr) filename;
615
748
}
616
749
 
617
 
Bool TIDY_CALL tidyFileExists( ctmbstr filename )
 
750
Bool TIDY_CALL tidyFileExists( TidyDoc tdoc, ctmbstr filename )
618
751
{
619
 
  ctmbstr fname = (tmbstr) ExpandTilde( filename );
 
752
  TidyDocImpl* doc = tidyDocToImpl( tdoc );
 
753
  ctmbstr fname = (tmbstr) ExpandTilde( doc, filename );
620
754
#ifndef NO_ACCESS_SUPPORT
621
755
  Bool exists = ( access(fname, 0) == 0 );
622
756
#else
628
762
  exists = ( fin != NULL );
629
763
#endif
630
764
  if ( fname != filename )
631
 
      MemFree( (tmbstr) fname );
 
765
      TidyDocFree( doc, (tmbstr) fname );
632
766
  return exists;
633
767
}
634
768
 
637
771
#define TIDY_MAX_NAME 64
638
772
#endif
639
773
 
640
 
int ParseConfigFile( TidyDocImpl* doc, ctmbstr file )
 
774
int TY_(ParseConfigFile)( TidyDocImpl* doc, ctmbstr file )
641
775
{
642
 
    return ParseConfigFileEnc( doc, file, "ascii" );
 
776
    return TY_(ParseConfigFileEnc)( doc, file, "ascii" );
643
777
}
644
778
 
645
779
/* open the file and parse its contents
646
780
*/
647
 
int ParseConfigFileEnc( TidyDocImpl* doc, ctmbstr file, ctmbstr charenc )
 
781
int TY_(ParseConfigFileEnc)( TidyDocImpl* doc, ctmbstr file, ctmbstr charenc )
648
782
{
649
783
    uint opterrs = doc->optionErrors;
650
 
    tmbstr fname = (tmbstr) ExpandTilde( file );
 
784
    tmbstr fname = (tmbstr) ExpandTilde( doc, file );
651
785
    TidyConfigImpl* cfg = &doc->config;
652
786
    FILE* fin = fopen( fname, "r" );
653
 
    int enc = CharEncodingId( charenc );
 
787
    int enc = TY_(CharEncodingId)( doc, charenc );
654
788
 
655
789
    if ( fin == NULL || enc < 0 )
656
790
    {
657
 
        FileError( doc, fname, TidyConfig );
 
791
        TY_(FileError)( doc, fname, TidyConfig );
658
792
        return -1;
659
793
    }
660
794
    else
661
795
    {
662
796
        tchar c;
663
 
        cfg->cfgIn = FileInput( doc, fin, enc );
 
797
        cfg->cfgIn = TY_(FileInput)( doc, fin, enc );
664
798
        c = FirstChar( cfg );
665
799
       
666
800
        for ( c = SkipWhite(cfg); c != EndOfStream; c = NextProperty(cfg) )
680
814
 
681
815
            if ( c == ':' )
682
816
            {
683
 
                const TidyOptionImpl* option = lookupOption( name );
 
817
                const TidyOptionImpl* option = TY_(lookupOption)( name );
684
818
                c = AdvanceChar( cfg );
685
819
                if ( option )
686
820
                    option->parser( doc, option );
707
841
                            if ( delim && c == delim )
708
842
                                break;
709
843
 
710
 
                            if ( IsWhite(c) )
 
844
                            if ( TY_(IsWhite)(c) )
711
845
                            {
712
846
                                if ( waswhite )
713
847
                                {
724
858
                        }
725
859
                        buf[i] = '\0';
726
860
                        if (no == (*doc->pOptCallback)( name, buf ))
727
 
                            ReportUnknownOption( doc, name );
 
861
                            TY_(ReportUnknownOption)( doc, name );
728
862
                    }
729
863
                    else
730
 
                        ReportUnknownOption( doc, name );
 
864
                        TY_(ReportUnknownOption)( doc, name );
731
865
                }
732
866
            }
733
867
        }
734
868
 
735
 
        fclose( fin );
736
 
        MemFree( (void *)cfg->cfgIn->source.sourceData ); /* fix for bug #810259 */
737
 
        freeStreamIn( cfg->cfgIn );
 
869
        TY_(freeFileSource)(&cfg->cfgIn->source, yes);
 
870
        TY_(freeStreamIn)( cfg->cfgIn );
738
871
        cfg->cfgIn = NULL;
739
872
    }
740
873
 
741
874
    if ( fname != (tmbstr) file )
742
 
        MemFree( fname );
 
875
        TidyDocFree( doc, fname );
743
876
 
744
877
    AdjustConfig( doc );
745
878
 
750
883
/* returns false if unknown option, missing parameter,
751
884
** or option doesn't use parameter
752
885
*/
753
 
Bool ParseConfigOption( TidyDocImpl* doc, ctmbstr optnam, ctmbstr optval )
 
886
Bool TY_(ParseConfigOption)( TidyDocImpl* doc, ctmbstr optnam, ctmbstr optval )
754
887
{
755
 
    const TidyOptionImpl* option = lookupOption( optnam );
 
888
    const TidyOptionImpl* option = TY_(lookupOption)( optnam );
756
889
    Bool status = ( option != NULL );
757
890
    if ( !status )
758
891
    {
761
894
        if (NULL != doc->pOptCallback)
762
895
            status = (*doc->pOptCallback)( optnam, optval );
763
896
        if (!status)
764
 
            ReportUnknownOption( doc, optnam );
 
897
            TY_(ReportUnknownOption)( doc, optnam );
765
898
    }
766
899
    else 
767
 
        status = ParseConfigValue( doc, option->id, optval );
 
900
        status = TY_(ParseConfigValue)( doc, option->id, optval );
768
901
    return status;
769
902
}
770
903
 
771
904
/* returns false if unknown option, missing parameter,
772
905
** or option doesn't use parameter
773
906
*/
774
 
Bool ParseConfigValue( TidyDocImpl* doc, TidyOptionId optId, ctmbstr optval )
 
907
Bool TY_(ParseConfigValue)( TidyDocImpl* doc, TidyOptionId optId, ctmbstr optval )
775
908
{
776
909
    const TidyOptionImpl* option = option_defs + optId;
777
910
    Bool status = ( optId < N_TIDY_OPTIONS && optval != NULL );
778
911
 
779
912
    if ( !status )
780
 
        ReportBadArgument( doc, option->name );
 
913
        TY_(ReportBadArgument)( doc, option->name );
781
914
    else
782
915
    {
783
 
        TidyBuffer inbuf = {0};            /* Set up input source */
784
 
        tidyBufAttach( &inbuf, (byte*)optval, tmbstrlen(optval)+1 );
785
 
        doc->config.cfgIn = BufferInput( doc, &inbuf, ASCII );
 
916
        TidyBuffer inbuf;            /* Set up input source */
 
917
        tidyBufInitWithAllocator( &inbuf, doc->allocator );
 
918
        tidyBufAttach( &inbuf, (byte*)optval, TY_(tmbstrlen)(optval)+1 );
 
919
        doc->config.cfgIn = TY_(BufferInput)( doc, &inbuf, ASCII );
786
920
        doc->config.c = GetC( &doc->config );
787
921
 
788
922
        status = option->parser( doc, option );
789
923
 
790
 
        freeStreamIn(doc->config.cfgIn);  /* Release input source */
 
924
        TY_(freeStreamIn)(doc->config.cfgIn);  /* Release input source */
791
925
        doc->config.cfgIn  = NULL;
792
926
        tidyBufDetach( &inbuf );
793
927
    }
796
930
 
797
931
 
798
932
/* ensure that char encodings are self consistent */
799
 
Bool  AdjustCharEncoding( TidyDocImpl* doc, int encoding )
 
933
Bool  TY_(AdjustCharEncoding)( TidyDocImpl* doc, int encoding )
800
934
{
801
935
    int outenc = -1;
802
936
    int inenc = -1;
850
984
 
851
985
    if ( inenc >= 0 )
852
986
    {
853
 
        SetOptionInt( doc, TidyCharEncoding, encoding );
854
 
        SetOptionInt( doc, TidyInCharEncoding, inenc );
855
 
        SetOptionInt( doc, TidyOutCharEncoding, outenc );
 
987
        TY_(SetOptionInt)( doc, TidyCharEncoding, encoding );
 
988
        TY_(SetOptionInt)( doc, TidyInCharEncoding, inenc );
 
989
        TY_(SetOptionInt)( doc, TidyOutCharEncoding, outenc );
856
990
        return yes;
857
991
    }
858
992
    return no;
862
996
void AdjustConfig( TidyDocImpl* doc )
863
997
{
864
998
    if ( cfgBool(doc, TidyEncloseBlockText) )
865
 
        SetOptionBool( doc, TidyEncloseBodyText, yes );
 
999
        TY_(SetOptionBool)( doc, TidyEncloseBodyText, yes );
866
1000
 
867
1001
    if ( cfgAutoBool(doc, TidyIndentContent) == TidyNoState )
868
 
        SetOptionInt( doc, TidyIndentSpaces, 0 );
 
1002
        TY_(SetOptionInt)( doc, TidyIndentSpaces, 0 );
869
1003
 
870
1004
    /* disable wrapping */
871
1005
    if ( cfg(doc, TidyWrapLen) == 0 )
872
 
        SetOptionInt( doc, TidyWrapLen, 0x7FFFFFFF );
 
1006
        TY_(SetOptionInt)( doc, TidyWrapLen, 0x7FFFFFFF );
873
1007
 
874
1008
    /* Word 2000 needs o:p to be declared as inline */
875
1009
    if ( cfgBool(doc, TidyWord2000) )
876
1010
    {
877
1011
        doc->config.defined_tags |= tagtype_inline;
878
 
        DefineTag( doc, tagtype_inline, "o:p" );
 
1012
        TY_(DefineTag)( doc, tagtype_inline, "o:p" );
879
1013
    }
880
1014
 
881
1015
    /* #480701 disable XHTML output flag if both output-xhtml and xml input are set */
882
1016
    if ( cfgBool(doc, TidyXmlTags) )
883
 
        SetOptionBool( doc, TidyXhtmlOut, no );
 
1017
        TY_(SetOptionBool)( doc, TidyXhtmlOut, no );
884
1018
 
885
1019
    /* XHTML is written in lower case */
886
1020
    if ( cfgBool(doc, TidyXhtmlOut) )
887
1021
    {
888
 
        SetOptionBool( doc, TidyXmlOut, yes );
889
 
        SetOptionBool( doc, TidyUpperCaseTags, no );
890
 
        SetOptionBool( doc, TidyUpperCaseAttrs, no );
891
 
        /* SetOptionBool( doc, TidyXmlPIs, yes ); */
 
1022
        TY_(SetOptionBool)( doc, TidyXmlOut, yes );
 
1023
        TY_(SetOptionBool)( doc, TidyUpperCaseTags, no );
 
1024
        TY_(SetOptionBool)( doc, TidyUpperCaseAttrs, no );
 
1025
        /* TY_(SetOptionBool)( doc, TidyXmlPIs, yes ); */
892
1026
    }
893
1027
 
894
1028
    /* if XML in, then XML out */
895
1029
    if ( cfgBool(doc, TidyXmlTags) )
896
1030
    {
897
 
        SetOptionBool( doc, TidyXmlOut, yes );
898
 
        SetOptionBool( doc, TidyXmlPIs, yes );
 
1031
        TY_(SetOptionBool)( doc, TidyXmlOut, yes );
 
1032
        TY_(SetOptionBool)( doc, TidyXmlPIs, yes );
899
1033
    }
900
1034
 
901
1035
    /* #427837 - fix by Dave Raggett 02 Jun 01
912
1046
         cfg(doc, TidyOutCharEncoding) != RAW &&
913
1047
         cfgBool(doc, TidyXmlOut) )
914
1048
    {
915
 
        SetOptionBool( doc, TidyXmlDecl, yes );
 
1049
        TY_(SetOptionBool)( doc, TidyXmlDecl, yes );
916
1050
    }
917
1051
 
918
1052
    /* XML requires end tags */
922
1056
        /* XML requires a BOM on output if using UTF-16 encoding */
923
1057
        ulong enc = cfg( doc, TidyOutCharEncoding );
924
1058
        if ( enc == UTF16LE || enc == UTF16BE || enc == UTF16 )
925
 
            SetOptionInt( doc, TidyOutputBOM, yes );
 
1059
            TY_(SetOptionInt)( doc, TidyOutputBOM, yes );
926
1060
#endif
927
 
        SetOptionBool( doc, TidyQuoteAmpersand, yes );
928
 
        SetOptionBool( doc, TidyHideEndTags, no );
 
1061
        TY_(SetOptionBool)( doc, TidyQuoteAmpersand, yes );
 
1062
        TY_(SetOptionBool)( doc, TidyHideEndTags, no );
929
1063
    }
930
1064
}
931
1065
 
937
1071
    TidyConfigImpl* cfg = &doc->config;
938
1072
    tchar c = SkipWhite( cfg );
939
1073
 
940
 
    while ( IsDigit(c) )
 
1074
    while ( TY_(IsDigit)(c) )
941
1075
    {
942
1076
        number = c - '0' + (10 * number);
943
1077
        digits = yes;
945
1079
    }
946
1080
 
947
1081
    if ( !digits )
948
 
        ReportBadArgument( doc, entry->name );
 
1082
        TY_(ReportBadArgument)( doc, entry->name );
949
1083
    else
950
 
        SetOptionInt( doc, entry->id, number );
 
1084
        TY_(SetOptionInt)( doc, entry->id, number );
951
1085
    return digits;
952
1086
}
953
1087
 
954
1088
/* true/false or yes/no or 0/1 or "auto" only looks at 1st char */
955
1089
static Bool ParseTriState( TidyTriState theState, TidyDocImpl* doc,
956
 
                    const TidyOptionImpl* entry, ulong* flag )
 
1090
                           const TidyOptionImpl* entry, ulong* flag )
957
1091
{
958
1092
    TidyConfigImpl* cfg = &doc->config;
959
1093
    tchar c = SkipWhite( cfg );
966
1100
        *flag = TidyAutoState;
967
1101
    else
968
1102
    {
969
 
        ReportBadArgument( doc, entry->name );
 
1103
        TY_(ReportBadArgument)( doc, entry->name );
970
1104
        return no;
971
1105
    }
972
1106
 
982
1116
    TidyConfigImpl* cfg = &doc->config;
983
1117
    tchar c = SkipWhite( cfg );
984
1118
 
985
 
    while ( c!=EndOfStream && cp < end && !IsWhite(c) && c != '\r' && c != '\n' )
 
1119
    while ( c!=EndOfStream && cp < end && !TY_(IsWhite)(c) && c != '\r' && c != '\n' )
986
1120
    {
987
1121
        *cp++ = (tmbchar) c;
988
1122
        c = AdvanceChar( cfg );
989
1123
    }
990
1124
    *cp = 0;
991
1125
 
992
 
    if ( tmbstrcasecmp(work, "lf") == 0 )
 
1126
    if ( TY_(tmbstrcasecmp)(work, "lf") == 0 )
993
1127
        nl = TidyLF;
994
 
    else if ( tmbstrcasecmp(work, "crlf") == 0 )
 
1128
    else if ( TY_(tmbstrcasecmp)(work, "crlf") == 0 )
995
1129
        nl = TidyCRLF;
996
 
    else if ( tmbstrcasecmp(work, "cr") == 0 )
 
1130
    else if ( TY_(tmbstrcasecmp)(work, "cr") == 0 )
997
1131
        nl = TidyCR;
998
1132
 
999
1133
    if ( nl < TidyLF || nl > TidyCR )
1000
 
        ReportBadArgument( doc, entry->name );
 
1134
        TY_(ReportBadArgument)( doc, entry->name );
1001
1135
    else
1002
 
        SetOptionInt( doc, entry->id, nl );
 
1136
        TY_(SetOptionInt)( doc, entry->id, nl );
1003
1137
    return ( nl >= TidyLF && nl <= TidyCR );
1004
1138
}
1005
1139
 
1008
1142
    ulong flag = 0;
1009
1143
    Bool status = ParseTriState( TidyNoState, doc, entry, &flag );
1010
1144
    if ( status )
1011
 
        SetOptionBool( doc, entry->id, flag != 0 );
 
1145
        TY_(SetOptionBool)( doc, entry->id, flag != 0 );
1012
1146
    return status;
1013
1147
}
1014
1148
 
1017
1151
    ulong flag = 0;
1018
1152
    Bool status = ParseTriState( TidyAutoState, doc, entry, &flag );
1019
1153
    if ( status )
1020
 
        SetOptionInt( doc, entry->id, flag );
 
1154
        TY_(SetOptionInt)( doc, entry->id, flag );
1021
1155
    return status;
1022
1156
}
1023
1157
 
1028
1162
    uint i = 0;
1029
1163
    uint c = SkipWhite( &doc->config );
1030
1164
 
1031
 
    while ( i < sizeof(buf)-2 && c != EndOfStream && !IsWhite(c) )
 
1165
    while ( i < sizeof(buf)-2 && c != EndOfStream && !TY_(IsWhite)(c) )
1032
1166
    {
1033
1167
        buf[i++] = (tmbchar) c;
1034
1168
        c = AdvanceChar( &doc->config );
1036
1170
    buf[i] = 0;
1037
1171
 
1038
1172
    if ( i == 0 )
1039
 
        ReportBadArgument( doc, option->name );
 
1173
        TY_(ReportBadArgument)( doc, option->name );
1040
1174
    else
1041
1175
        SetOptionValue( doc, option->id, buf );
1042
1176
    return ( i > 0 );
1049
1183
    uint i = 0;
1050
1184
    uint c = SkipWhite( &doc->config );
1051
1185
 
1052
 
    while ( i < sizeof(buf)-2 && c != EndOfStream && !IsWhite(c) )
 
1186
    while ( i < sizeof(buf)-2 && c != EndOfStream && !TY_(IsWhite)(c) )
1053
1187
    {
1054
1188
        buf[i++] = (tmbchar) c;
1055
1189
        c = AdvanceChar( &doc->config );
1056
1190
    }
1057
1191
    buf[i] = '\0';
1058
1192
 
1059
 
    if ( i == 0 || !IsCSS1Selector(buf) ) {
1060
 
        ReportBadArgument( doc, option->name );
 
1193
    if ( i == 0 || !TY_(IsCSS1Selector)(buf) ) {
 
1194
        TY_(ReportBadArgument)( doc, option->name );
1061
1195
        return no;
1062
1196
    }
1063
1197
 
1078
1212
  ctmbstr theval = name;
1079
1213
  if ( prvval )
1080
1214
  {
1081
 
    uint len = tmbstrlen(name) + tmbstrlen(prvval) + 3;
1082
 
    catval = tmbstrndup( prvval, len );
1083
 
    tmbstrcat( catval, ", " );
1084
 
    tmbstrcat( catval, name );
 
1215
    uint len = TY_(tmbstrlen)(name) + TY_(tmbstrlen)(prvval) + 3;
 
1216
    catval = TY_(tmbstrndup)( doc->allocator, prvval, len );
 
1217
    TY_(tmbstrcat)( catval, ", " );
 
1218
    TY_(tmbstrcat)( catval, name );
1085
1219
    theval = catval;
1086
1220
  }
1087
 
  DefineTag( doc, tagType, name );
 
1221
  TY_(DefineTag)( doc, tagType, name );
1088
1222
  SetOptionValue( doc, optId, theval );
1089
1223
  if ( catval )
1090
 
    MemFree( catval );
 
1224
    TidyDocFree( doc, catval );
1091
1225
}
1092
1226
 
1093
1227
/* a space or comma separated list of tag names */
1106
1240
    case TidyEmptyTags:   ttyp = tagtype_empty;     break;
1107
1241
    case TidyPreTags:     ttyp = tagtype_pre;       break;
1108
1242
    default:
1109
 
       ReportUnknownOption( doc, option->name );
 
1243
       TY_(ReportUnknownOption)( doc, option->name );
1110
1244
       return no;
1111
1245
    }
1112
1246
 
1113
1247
    SetOptionValue( doc, option->id, NULL );
1114
 
    FreeDeclaredTags( doc, ttyp );
 
1248
    TY_(FreeDeclaredTags)( doc, ttyp );
1115
1249
    cfg->defined_tags |= ttyp;
1116
1250
 
1117
1251
    do
1130
1264
            else
1131
1265
                c = c2;
1132
1266
 
1133
 
            if ( !IsWhite(c) )
 
1267
            if ( !TY_(IsWhite)(c) )
1134
1268
            {
1135
1269
                buf[i] = 0;
1136
 
                UngetChar( c, cfg->cfgIn );
1137
 
                UngetChar( '\n', cfg->cfgIn );
 
1270
                TY_(UngetChar)( c, cfg->cfgIn );
 
1271
                TY_(UngetChar)( '\n', cfg->cfgIn );
1138
1272
                break;
1139
1273
            }
1140
1274
        }
1143
1277
        if ( c == '\n' )
1144
1278
        {
1145
1279
            c = AdvanceChar( cfg );
1146
 
            if ( !IsWhite(c) )
 
1280
            if ( !TY_(IsWhite)(c) )
1147
1281
            {
1148
1282
                buf[i] = 0;
1149
 
                UngetChar( c, cfg->cfgIn );
1150
 
                UngetChar( '\n', cfg->cfgIn );
 
1283
                TY_(UngetChar)( c, cfg->cfgIn );
 
1284
                TY_(UngetChar)( '\n', cfg->cfgIn );
1151
1285
                break;
1152
1286
            }
1153
1287
        }
1154
1288
        */
1155
1289
 
1156
 
        while ( i < sizeof(buf)-2 && c != EndOfStream && !IsWhite(c) && c != ',' )
 
1290
        while ( i < sizeof(buf)-2 && c != EndOfStream && !TY_(IsWhite)(c) && c != ',' )
1157
1291
        {
1158
1292
            buf[i++] = (tmbchar) c;
1159
1293
            c = AdvanceChar( cfg );
1199
1333
        if ( delim && c == delim )
1200
1334
            break;
1201
1335
 
1202
 
        if ( IsWhite(c) )
 
1336
        if ( TY_(IsWhite)(c) )
1203
1337
        {
1204
1338
            if ( waswhite )
1205
1339
            {
1228
1362
    Bool validEncoding = yes;
1229
1363
    tchar c = SkipWhite( &doc->config );
1230
1364
 
1231
 
    while ( i < sizeof(buf)-2 && c != EndOfStream && !IsWhite(c) )
 
1365
    while ( i < sizeof(buf)-2 && c != EndOfStream && !TY_(IsWhite)(c) )
1232
1366
    {
1233
 
        buf[i++] = (tmbchar) ToLower( c );
 
1367
        buf[i++] = (tmbchar) TY_(ToLower)( c );
1234
1368
        c = AdvanceChar( &doc->config );
1235
1369
    }
1236
1370
    buf[i] = 0;
1237
1371
 
1238
 
    enc = CharEncodingId( buf );
 
1372
    enc = TY_(CharEncodingId)( doc, buf );
1239
1373
 
1240
1374
#ifdef TIDY_WIN32_MLANG_SUPPORT
1241
1375
    /* limit support to --input-encoding */
1246
1380
    if ( enc < 0 )
1247
1381
    {
1248
1382
        validEncoding = no;
1249
 
        ReportBadArgument( doc, option->name );
 
1383
        TY_(ReportBadArgument)( doc, option->name );
1250
1384
    }
1251
1385
    else
1252
 
        SetOptionInt( doc, option->id, enc );
 
1386
        TY_(SetOptionInt)( doc, option->id, enc );
1253
1387
 
1254
1388
    if ( validEncoding && option->id == TidyCharEncoding )
1255
 
        AdjustCharEncoding( doc, enc );
 
1389
        TY_(AdjustCharEncoding)( doc, enc );
1256
1390
    return validEncoding;
1257
1391
}
1258
1392
 
1259
1393
 
1260
 
int CharEncodingId( ctmbstr charenc )
 
1394
int TY_(CharEncodingId)( TidyDocImpl* ARG_UNUSED(doc), ctmbstr charenc )
1261
1395
{
1262
 
    int enc = GetCharEncodingFromOptName( charenc );
 
1396
    int enc = TY_(GetCharEncodingFromOptName)( charenc );
1263
1397
 
1264
1398
#ifdef TIDY_WIN32_MLANG_SUPPORT
1265
1399
    if (enc == -1)
1266
1400
    {
1267
 
        uint wincp = Win32MLangGetCPFromName(charenc);
 
1401
        uint wincp = TY_(Win32MLangGetCPFromName)(doc->allocator, charenc);
1268
1402
        if (wincp)
1269
1403
            enc = wincp;
1270
1404
    }
1273
1407
    return enc;
1274
1408
}
1275
1409
 
1276
 
ctmbstr CharEncodingName( int encoding )
 
1410
ctmbstr TY_(CharEncodingName)( int encoding )
1277
1411
{
1278
 
    ctmbstr encodingName = GetEncodingNameFromTidyId(encoding);
 
1412
    ctmbstr encodingName = TY_(GetEncodingNameFromTidyId)(encoding);
1279
1413
 
1280
1414
    if (!encodingName)
1281
1415
        encodingName = "unknown";
1283
1417
    return encodingName;
1284
1418
}
1285
1419
 
1286
 
ctmbstr CharEncodingOptName( int encoding )
 
1420
ctmbstr TY_(CharEncodingOptName)( int encoding )
1287
1421
{
1288
 
    ctmbstr encodingName = GetEncodingOptNameFromTidyId(encoding);
 
1422
    ctmbstr encodingName = TY_(GetEncodingOptNameFromTidyId)(encoding);
1289
1423
 
1290
1424
    if (!encodingName)
1291
1425
        encodingName = "unknown";
1316
1450
    {
1317
1451
        status = ParseString(doc, option);
1318
1452
        if (status)
1319
 
            SetOptionInt( doc, TidyDoctypeMode, TidyDoctypeUser );
 
1453
            TY_(SetOptionInt)( doc, TidyDoctypeMode, TidyDoctypeUser );
1320
1454
 
1321
1455
        return status;
1322
1456
    }
1323
1457
 
1324
1458
    /* read first word */
1325
 
    while ( i < sizeof(buf)-1 && c != EndOfStream && !IsWhite(c) )
 
1459
    while ( i < sizeof(buf)-1 && c != EndOfStream && !TY_(IsWhite)(c) )
1326
1460
    {
1327
1461
        buf[i++] = (tmbchar) c;
1328
1462
        c = AdvanceChar( cfg );
1329
1463
    }
1330
1464
    buf[i] = '\0';
1331
1465
 
1332
 
    if ( tmbstrcasecmp(buf, "auto") == 0 )
 
1466
    if ( TY_(tmbstrcasecmp)(buf, "auto") == 0 )
1333
1467
        dtmode = TidyDoctypeAuto;
1334
 
    else if ( tmbstrcasecmp(buf, "omit") == 0 )
 
1468
    else if ( TY_(tmbstrcasecmp)(buf, "omit") == 0 )
1335
1469
        dtmode = TidyDoctypeOmit;
1336
 
    else if ( tmbstrcasecmp(buf, "strict") == 0 )
 
1470
    else if ( TY_(tmbstrcasecmp)(buf, "strict") == 0 )
1337
1471
        dtmode = TidyDoctypeStrict;
1338
 
    else if ( tmbstrcasecmp(buf, "loose") == 0 ||
1339
 
              tmbstrcasecmp(buf, "transitional") == 0 )
 
1472
    else if ( TY_(tmbstrcasecmp)(buf, "loose") == 0 ||
 
1473
              TY_(tmbstrcasecmp)(buf, "transitional") == 0 )
1340
1474
        dtmode = TidyDoctypeLoose;
1341
1475
    else
1342
1476
    {
1343
 
        ReportBadArgument( doc, option->name );
 
1477
        TY_(ReportBadArgument)( doc, option->name );
1344
1478
        status = no;
1345
1479
    }
1346
1480
     
1347
1481
    if ( status )
1348
 
        SetOptionInt( doc, TidyDoctypeMode, dtmode );
 
1482
        TY_(SetOptionInt)( doc, TidyDoctypeMode, dtmode );
1349
1483
    return status;
1350
1484
}
1351
1485
 
1358
1492
    TidyConfigImpl* cfg = &doc->config;
1359
1493
    tchar c = SkipWhite( cfg );
1360
1494
 
1361
 
    while (i < sizeof(buf)-1 && c != EndOfStream && !IsWhite(c))
1362
 
    {
1363
 
        buf[i++] = (tmbchar) c;
1364
 
        c = AdvanceChar( cfg );
1365
 
    }
1366
 
    buf[i] = '\0';
1367
 
 
1368
 
    if ( tmbstrcasecmp(buf, "keep-first") == 0 )
1369
 
        cfg->value[ TidyDuplicateAttrs ] = TidyKeepFirst;
1370
 
    else if ( tmbstrcasecmp(buf, "keep-last") == 0 )
1371
 
        cfg->value[ TidyDuplicateAttrs ] = TidyKeepLast;
1372
 
    else
1373
 
    {
1374
 
        ReportBadArgument( doc, option->name );
 
1495
    while (i < sizeof(buf)-1 && c != EndOfStream && !TY_(IsWhite)(c))
 
1496
    {
 
1497
        buf[i++] = (tmbchar) c;
 
1498
        c = AdvanceChar( cfg );
 
1499
    }
 
1500
    buf[i] = '\0';
 
1501
 
 
1502
    if ( TY_(tmbstrcasecmp)(buf, "keep-first") == 0 )
 
1503
        cfg->value[ TidyDuplicateAttrs ].v = TidyKeepFirst;
 
1504
    else if ( TY_(tmbstrcasecmp)(buf, "keep-last") == 0 )
 
1505
        cfg->value[ TidyDuplicateAttrs ].v = TidyKeepLast;
 
1506
    else
 
1507
    {
 
1508
        TY_(ReportBadArgument)( doc, option->name );
 
1509
        status = no;
 
1510
    }
 
1511
    return status;
 
1512
}
 
1513
 
 
1514
Bool ParseSorter( TidyDocImpl* doc, const TidyOptionImpl* option )
 
1515
{
 
1516
    Bool status = yes;
 
1517
    tmbchar buf[64] = {0};
 
1518
    uint i = 0;
 
1519
 
 
1520
    TidyConfigImpl* cfg = &doc->config;
 
1521
    tchar c = SkipWhite( cfg );
 
1522
 
 
1523
    while (i < sizeof(buf)-1 && c != EndOfStream && !TY_(IsWhite)(c))
 
1524
    {
 
1525
        buf[i++] = (tmbchar) c;
 
1526
        c = AdvanceChar( cfg );
 
1527
    }
 
1528
    buf[i] = '\0';
 
1529
 
 
1530
    if ( TY_(tmbstrcasecmp)(buf, "alpha") == 0 )
 
1531
        cfg->value[ TidySortAttributes ].v = TidySortAttrAlpha;
 
1532
    else if ( TY_(tmbstrcasecmp)(buf, "none") == 0)
 
1533
        cfg->value[ TidySortAttributes ].v = TidySortAttrNone;
 
1534
    else
 
1535
    {
 
1536
        TY_(ReportBadArgument)( doc, option->name );
1375
1537
        status = no;
1376
1538
    }
1377
1539
    return status;
1380
1542
/* Use TidyOptionId as iterator.
1381
1543
** Send index of 1st option after TidyOptionUnknown as start of list.
1382
1544
*/
1383
 
TidyIterator getOptionList( TidyDocImpl* ARG_UNUSED(doc) )
 
1545
TidyIterator TY_(getOptionList)( TidyDocImpl* ARG_UNUSED(doc) )
1384
1546
{
1385
 
  return (TidyIterator) 1;
 
1547
    return (TidyIterator) (size_t)1;
1386
1548
}
1387
1549
 
1388
1550
/* Check if this item is last valid option.
1389
1551
** If so, zero out iterator.
1390
1552
*/
1391
 
const TidyOptionImpl*  getNextOption( TidyDocImpl* ARG_UNUSED(doc),
1392
 
                                      TidyIterator* iter )
 
1553
const TidyOptionImpl*  TY_(getNextOption)( TidyDocImpl* ARG_UNUSED(doc),
 
1554
                                           TidyIterator* iter )
1393
1555
{
1394
1556
  const TidyOptionImpl* option = NULL;
1395
 
  ulong optId;
 
1557
  size_t optId;
1396
1558
  assert( iter != NULL );
1397
 
  optId = (ulong) *iter;
 
1559
  optId = (size_t) *iter;
1398
1560
  if ( optId > TidyUnknownOption && optId < N_TIDY_OPTIONS )
1399
1561
  {
1400
1562
    option = &option_defs[ optId ];
1401
1563
    optId++;
1402
1564
  }
1403
 
  *iter = (TidyIterator) ( optId < N_TIDY_OPTIONS ? optId : 0 );
 
1565
  *iter = (TidyIterator) ( optId < N_TIDY_OPTIONS ? optId : (size_t)0 );
1404
1566
  return option;
1405
1567
}
1406
1568
 
1407
1569
/* Use a 1-based array index as iterator: 0 == end-of-list
1408
1570
*/
1409
 
TidyIterator getOptionPickList( const TidyOptionImpl* option )
 
1571
TidyIterator TY_(getOptionPickList)( const TidyOptionImpl* option )
1410
1572
{
1411
 
    ulong ix = 0;
 
1573
    size_t ix = 0;
1412
1574
    if ( option && option->pickList )
1413
1575
        ix = 1;
1414
1576
    return (TidyIterator) ix;
1415
1577
}
1416
1578
 
1417
 
ctmbstr      getNextOptionPick( const TidyOptionImpl* option,
1418
 
                                TidyIterator* iter )
 
1579
ctmbstr      TY_(getNextOptionPick)( const TidyOptionImpl* option,
 
1580
                                     TidyIterator* iter )
1419
1581
{
1420
 
    ulong ix;
 
1582
    size_t ix;
1421
1583
    ctmbstr val = NULL;
1422
1584
    assert( option!=NULL && iter != NULL );
1423
1585
 
1424
 
    ix = (ulong) *iter;
 
1586
    ix = (size_t) *iter;
1425
1587
    if ( ix > 0 && ix < 16 && option->pickList )
1426
1588
        val = option->pickList[ ix-1 ];
1427
 
    *iter = (TidyIterator) ( val && option->pickList[ix] ? ix + 1 : 0 );
 
1589
    *iter = (TidyIterator) ( val && option->pickList[ix] ? ix + 1 : (size_t)0 );
1428
1590
    return val;
1429
1591
}
1430
1592
 
1433
1595
{
1434
1596
  ctmbstr cp = option->name;
1435
1597
  while ( *cp )
1436
 
      WriteChar( *cp++, out );
1437
 
  WriteChar( ':', out );
1438
 
  WriteChar( ' ', out );
 
1598
      TY_(WriteChar)( *cp++, out );
 
1599
  TY_(WriteChar)( ':', out );
 
1600
  TY_(WriteChar)( ' ', out );
1439
1601
  cp = sval;
1440
1602
  while ( *cp )
1441
 
      WriteChar( *cp++, out );
1442
 
  WriteChar( '\n', out );
 
1603
      TY_(WriteChar)( *cp++, out );
 
1604
  TY_(WriteChar)( '\n', out );
1443
1605
  return 0;
1444
1606
}
1445
1607
 
1446
1608
static int  WriteOptionInt( const TidyOptionImpl* option, uint ival, StreamOut* out )
1447
1609
{
1448
1610
  tmbchar sval[ 32 ] = {0};
1449
 
  tmbsnprintf(sval, sizeof(sval), "%u", ival );
 
1611
  TY_(tmbsnprintf)(sval, sizeof(sval), "%u", ival );
1450
1612
  return WriteOptionString( option, sval, out );
1451
1613
}
1452
1614
 
1467
1629
    return -1;
1468
1630
}
1469
1631
 
1470
 
Bool  ConfigDiffThanSnapshot( TidyDocImpl* doc )
 
1632
Bool  TY_(ConfigDiffThanSnapshot)( TidyDocImpl* doc )
1471
1633
{
1472
1634
  int diff = memcmp( &doc->config.value, &doc->config.snapshot,
1473
1635
                     N_TIDY_OPTIONS * sizeof(uint) );
1474
1636
  return ( diff != 0 );
1475
1637
}
1476
1638
 
1477
 
Bool  ConfigDiffThanDefault( TidyDocImpl* doc )
 
1639
Bool  TY_(ConfigDiffThanDefault)( TidyDocImpl* doc )
1478
1640
{
1479
1641
  Bool diff = no;
1480
1642
  const TidyOptionImpl* option = option_defs + 1;
1481
 
  ulong* ival = doc->config.value;
1482
 
  for ( /**/; !diff && option && option->name; ++option, ++ival )
 
1643
  const TidyOptionValue* val = doc->config.value;
 
1644
  for ( /**/; !diff && option && option->name; ++option, ++val )
1483
1645
  {
1484
 
    diff = ( *ival != option->dflt );
 
1646
      diff = !OptionValueEqDefault( option, val );
1485
1647
  }
1486
1648
  return diff;
1487
1649
}
1493
1655
    const TidyOptionImpl* option;
1494
1656
    for ( option=option_defs+1; 0==rc && option && option->name; ++option )
1495
1657
    {
1496
 
        ulong ival = doc->config.value[ option->id ];
 
1658
        const TidyOptionValue* val = &doc->config.value[ option->id ];
1497
1659
        if ( option->parser == NULL )
1498
1660
            continue;
1499
 
        if ( ival == option->dflt && option->id != TidyDoctype)
 
1661
        if ( OptionValueEqDefault( option, val ) && option->id != TidyDoctype)
1500
1662
            continue;
1501
1663
 
1502
1664
        if ( option->id == TidyDoctype )  /* Special case */
1507
1669
            tmbstr t;
1508
1670
            
1509
1671
            /* add 2 double quotes */
1510
 
            if (( t = (tmbstr)MemAlloc( tmbstrlen( (ctmbstr)ival) + 2 ) ))
 
1672
            if (( t = (tmbstr)TidyDocAlloc( doc, TY_(tmbstrlen)( val->p ) + 2 ) ))
1511
1673
            {
1512
1674
              t[0] = '\"'; t[1] = 0;
1513
1675
            
1514
 
              tmbstrcat( t, (ctmbstr)ival );
1515
 
              tmbstrcat( t, "\"" );
1516
 
              rc = WriteOptionString( option, (ctmbstr)t, out );
 
1676
              TY_(tmbstrcat)( t, val->p );
 
1677
              TY_(tmbstrcat)( t, "\"" );
 
1678
              rc = WriteOptionString( option, t, out );
1517
1679
            
1518
 
              MemFree( t );
 
1680
              TidyDocFree( doc, t );
1519
1681
            }
1520
1682
          }
1521
1683
          else if ( dtmode == option_defs[TidyDoctypeMode].dflt )
1524
1686
            rc = WriteOptionPick( option, dtmode, out );
1525
1687
        }
1526
1688
        else if ( option->pickList )
1527
 
          rc = WriteOptionPick( option, ival, out );
 
1689
          rc = WriteOptionPick( option, val->v, out );
1528
1690
        else
1529
1691
        {
1530
1692
          switch ( option->type )
1531
1693
          {
1532
1694
          case TidyString:
1533
 
            rc = WriteOptionString( option, (ctmbstr) ival, out );
 
1695
            rc = WriteOptionString( option, val->p, out );
1534
1696
            break;
1535
1697
          case TidyInteger:
1536
 
            rc = WriteOptionInt( option, ival, out );
 
1698
            rc = WriteOptionInt( option, val->v, out );
1537
1699
            break;
1538
1700
          case TidyBoolean:
1539
 
            rc = WriteOptionBool( option, ival ? yes : no, out );
 
1701
            rc = WriteOptionBool( option, val->v ? yes : no, out );
1540
1702
            break;
1541
1703
          }
1542
1704
        }
1544
1706
    return rc;
1545
1707
}
1546
1708
 
1547
 
int  SaveConfigFile( TidyDocImpl* doc, ctmbstr cfgfil )
 
1709
int  TY_(SaveConfigFile)( TidyDocImpl* doc, ctmbstr cfgfil )
1548
1710
{
1549
1711
    int status = -1;
1550
1712
    StreamOut* out = NULL;
1553
1715
    FILE* fout = fopen( cfgfil, "wb" );
1554
1716
    if ( fout )
1555
1717
    {
1556
 
        out = FileOutput( fout, outenc, nl );
 
1718
        out = TY_(FileOutput)( doc, fout, outenc, nl );
1557
1719
        status = SaveConfigToStream( doc, out );
1558
1720
        fclose( fout );
1559
 
        MemFree( out );
 
1721
        TidyDocFree( doc, out );
1560
1722
    }
1561
1723
    return status;
1562
1724
}
1563
1725
 
1564
 
int  SaveConfigSink( TidyDocImpl* doc, TidyOutputSink* sink )
 
1726
int  TY_(SaveConfigSink)( TidyDocImpl* doc, TidyOutputSink* sink )
1565
1727
{
1566
1728
    uint outenc = cfg( doc, TidyOutCharEncoding );
1567
1729
    uint nl = cfg( doc, TidyNewline );
1568
 
    StreamOut* out = UserOutput( sink, outenc, nl );
 
1730
    StreamOut* out = TY_(UserOutput)( doc, sink, outenc, nl );
1569
1731
    int status = SaveConfigToStream( doc, out );
1570
 
    MemFree( out );
 
1732
    TidyDocFree( doc, out );
1571
1733
    return status;
1572
1734
}
 
1735
 
 
1736
/*
 
1737
 * local variables:
 
1738
 * mode: c
 
1739
 * indent-tabs-mode: nil
 
1740
 * c-basic-offset: 4
 
1741
 * eval: (c-set-offset 'substatement-open 0)
 
1742
 * end:
 
1743
 */