From dedcb7bb4d7f6417a8d7f68f5fa6f32c6cef5a83 Mon Sep 17 00:00:00 2001 From: Jim Derry Date: Tue, 3 Oct 2017 22:31:55 -0400 Subject: [PATCH] SPRTF is now compatible with macOS and Linux, and most likely all supported platforms. --- CMakeLists.txt | 36 ++++++++++++++++-------------------- console/tidy.c | 10 +++++----- include/tidy.h | 14 ++++++-------- include/tidyplatform.h | 7 +++++++ src/alloc.c | 8 ++++---- src/language.h | 8 ++++---- src/message.h | 2 +- src/pprint.c | 20 +++++++++----------- src/sprtf.h | 14 +++++++------- src/tags.c | 7 ++----- src/tidylib.c | 12 +++++------- 11 files changed, 66 insertions(+), 72 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2508b1a..778253c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -142,21 +142,22 @@ endif () # NOTE: Currently only available in the Debug configuration build in # Windows, but could be maybe extended to Unix, others... #------------------------------------------------------------------------ +option( ENABLE_ALLOC_DEBUG "Set ON to output node allocation diagnostics." OFF ) +option( ENABLE_MEMORY_DEBUG "Set ON to output some memory diagnostics." OFF ) + +if (ENABLE_ALLOC_DEBUG) + add_definitions ( -DDEBUG_ALLOCATION ) # see lexer.c for details + message(STATUS "*** Note, lexer.c node allocation diagnostics are ON") +endif () + + if (ENABLE_MEMORY_DEBUG) + add_definitions ( -DDEBUG_MEMORY ) # see alloc.c for details + message(STATUS "*** Note, alloc.c memory diagnostics are ON") + endif () + if (WIN32) - option( ENABLE_MEMORY_DEBUG "Set ON to output some memory diagnostics." OFF ) - option( ENABLE_ALLOC_DEBUG "Set ON to output node allocation diagnostics." OFF ) option( ENABLE_CRTDBG_MEMORY "Set ON to enable the Windows CRT debug library." OFF ) - if (ENABLE_MEMORY_DEBUG) - add_definitions ( -DDEBUG_MEMORY ) # see alloc.c for details - message(STATUS "*** Note, alloc.c memory diagnostics are ON") - endif () - - if (ENABLE_ALLOC_DEBUG) - add_definitions ( -DDEBUG_ALLOCATION ) # see lexer.c for details - message(STATUS "*** Note, lexer.c node allocation diagnostics are ON") - endif () - if (ENABLE_CRTDBG_MEMORY) add_definitions ( -D_CRTDBG_MAP_ALLOC ) # see tidy.c for details message(STATUS "*** Note, tidy.c Windows CRT memory debug is ON") @@ -334,7 +335,7 @@ set ( CFILES ${SRCDIR}/buffio.c ${SRCDIR}/fileio.c ${SRCDIR}/streamio.c ${SRCDIR}/tagask.c ${SRCDIR}/tmbstr.c ${SRCDIR}/utf8.c ${SRCDIR}/tidylib.c ${SRCDIR}/mappedio.c ${SRCDIR}/gdoc.c - ${SRCDIR}/language.c ${SRCDIR}/messageobj.c ) + ${SRCDIR}/language.c ${SRCDIR}/messageobj.c ${SRCDIR}/sprtf.c ) set ( HFILES ${INCDIR}/tidyplatform.h ${INCDIR}/tidy.h ${INCDIR}/tidyenum.h @@ -352,13 +353,8 @@ set ( LIBHFILES ${SRCDIR}/pprint.h ${SRCDIR}/streamio.h ${SRCDIR}/tags.h ${SRCDIR}/tmbstr.h ${SRCDIR}/utf8.h ${SRCDIR}/tidy-int.h ${SRCDIR}/version.h ${SRCDIR}/gdoc.h ${SRCDIR}/language.h - ${SRCDIR}/language_en.h ) - -if (MSVC) - list(APPEND CFILES ${SRCDIR}/sprtf.c) - list(APPEND LIBHFILES ${SRCDIR}/sprtf.h) -endif () - + ${SRCDIR}/language_en.h ${SRCDIR}/sprtf.h ) + #------------------------------------------------------------------------ # Target Locations diff --git a/console/tidy.c b/console/tidy.c index 507baac..000dee1 100644 --- a/console/tidy.c +++ b/console/tidy.c @@ -25,13 +25,13 @@ #if defined(_WIN32) # include /* Force console to UTF8. */ #endif -#if !defined(NDEBUG) && defined(_MSC_VER) -# ifdef _CRTDBG_MAP_ALLOC -# include -# include -# endif +#if !defined(NDEBUG) && defined(_MSC_VER) && defined(_CRTDBG_MAP_ALLOC) +# include +# include #endif +#include "sprtf.h" + /** Tidy will send errors to this file, which will be stderr later. */ static FILE* errout = NULL; diff --git a/include/tidy.h b/include/tidy.h index 3b5762a..365c70d 100644 --- a/include/tidy.h +++ b/include/tidy.h @@ -1418,10 +1418,8 @@ TIDY_EXPORT double TIDY_CALL tidyGetArgValueDouble(TidyMessage tmessage, /**< ** Your callback function will be provided with the following parameters. ** @param tdoc Indicates the source tidy document. ** @param line Indicates the line in the source document at this point in the process. - ** @param column Indicates the column in the source document at this point in the process. + ** @param col Indicates the column in the source document at this point in the process. ** @param destLine Indicates the line number in the output document at this point in the process. - ** @return Your callback function will return `yes` if Tidy should include the - ** report in its own output sink, or `no` if Tidy should suppress it. */ typedef void (TIDY_CALL *TidyPPProgress)( TidyDoc tdoc, uint line, uint col, uint destLine ); @@ -1898,7 +1896,7 @@ TIDY_EXPORT uint TIDY_CALL tidyErrorCodeFromKey(ctmbstr code); ** @result Returns a TidyIterator, which is a token used to represent the ** current position in a list within LibTidy. */ -TIDY_EXPORT TidyIterator TIDY_CALL getErrorCodeList(); +TIDY_EXPORT TidyIterator TIDY_CALL getErrorCodeList(void); /** Given a valid TidyIterator initiated with getErrorCodeList(), returns ** an instance of the opaque type TidyMessageArgument, which serves as a token @@ -1946,7 +1944,7 @@ TIDY_EXPORT Bool TIDY_CALL tidySetLanguage( ctmbstr languageCode ); /** Gets the current language used by Tidy. ** @result Returns a string indicating the currently set language. */ -TIDY_EXPORT ctmbstr TIDY_CALL tidyGetLanguage(); +TIDY_EXPORT ctmbstr TIDY_CALL tidyGetLanguage(void); /** @} ** @name Locale Mappings @@ -1978,7 +1976,7 @@ opaque_type(tidyLocaleMapItem); ** @result Returns a TidyIterator, which is a token used to represent the ** current position in a list within LibTidy. */ -TIDY_EXPORT TidyIterator TIDY_CALL getWindowsLanguageList(); +TIDY_EXPORT TidyIterator TIDY_CALL getWindowsLanguageList(void); /** Given a valid TidyIterator initiated with getWindowsLanguageList(), returns ** a pointer to a tidyLocaleMapItem, which can be further interrogated with @@ -2046,7 +2044,7 @@ TIDY_EXPORT ctmbstr TIDY_CALL tidyDefaultString( uint messageType ); ** @result Returns a TidyIterator, which is a token used to represent the ** current position in a list within LibTidy. */ -TIDY_EXPORT TidyIterator TIDY_CALL getStringKeyList(); +TIDY_EXPORT TidyIterator TIDY_CALL getStringKeyList(void); /** Given a valid TidyIterator initiated with getStringKeyList(), returns ** an unsigned integer representing the next key value. @@ -2076,7 +2074,7 @@ TIDY_EXPORT uint TIDY_CALL getNextStringKey( TidyIterator* iter ); ** @result Returns a TidyIterator, which is a token used to represent the ** current position in a list within LibTidy. */ -TIDY_EXPORT TidyIterator TIDY_CALL getInstalledLanguageList(); +TIDY_EXPORT TidyIterator TIDY_CALL getInstalledLanguageList(void); /** Given a valid TidyIterator initiated with getInstalledLanguageList(), ** returns a string representing a language name that is installed in Tidy. diff --git a/include/tidyplatform.h b/include/tidyplatform.h index 5fa7cb7..f4842ef 100644 --- a/include/tidyplatform.h +++ b/include/tidyplatform.h @@ -654,7 +654,14 @@ opaque_type( TidyIterator ); /*============================================================================= * Debugging + * When building and not defining the NDEBUG macro, Tidy will output + * extensive debug information. In addition to this macro, you can supply + * build flags for additional diagnostic information: + * - _CRTDBG_MAP_ALLOC (_MSC_VER only) + * - DEBUG_ALLOCATION + * - DEBUG_MEMORY *===========================================================================*/ + #if !defined(NDEBUG) # include "sprtf.h" #endif diff --git a/src/alloc.c b/src/alloc.c index 17a1227..59aef73 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -15,7 +15,7 @@ static TidyRealloc g_realloc = NULL; static TidyFree g_free = NULL; static TidyPanic g_panic = NULL; -#if !defined(NDEBUG) && defined(_MSC_VER) && defined(DEBUG_MEMORY) +#if !defined(NDEBUG) && defined(DEBUG_MEMORY) static int alloccnt = 0; static int realloccnt = 0; static int freecnt = 0; @@ -63,7 +63,7 @@ static void* TIDY_CALL defaultAlloc( TidyAllocator* allocator, size_t size ) void *p = ( g_malloc ? g_malloc(size) : malloc(size) ); if ( !p ) defaultPanic( allocator,"Out of memory!"); -#if !defined(NDEBUG) && defined(_MSC_VER) && defined(DEBUG_MEMORY) +#if !defined(NDEBUG) && defined(DEBUG_MEMORY) alloccnt++; SPRTF("%d: alloc MEM %p, size %d\n", alloccnt, p, (int)size ); if (size == 0) { @@ -82,7 +82,7 @@ static void* TIDY_CALL defaultRealloc( TidyAllocator* allocator, void* mem, size p = ( g_realloc ? g_realloc(mem, newsize) : realloc(mem, newsize) ); if (!p) defaultPanic( allocator, "Out of memory!"); -#if !defined(NDEBUG) && defined(_MSC_VER) && defined(DEBUG_MEMORY) +#if !defined(NDEBUG) && defined(DEBUG_MEMORY) realloccnt++; SPRTF("%d: realloc MEM %p, size %d\n", realloccnt, p, (int)newsize ); #endif @@ -93,7 +93,7 @@ static void TIDY_CALL defaultFree( TidyAllocator* ARG_UNUSED(allocator), void* m { if ( mem ) { -#if !defined(NDEBUG) && defined(_MSC_VER) && defined(DEBUG_MEMORY) +#if !defined(NDEBUG) && defined(DEBUG_MEMORY) freecnt++; SPRTF("%d: free MEM %p\n", freecnt, mem ); #endif diff --git a/src/language.h b/src/language.h index 5419df6..12991eb 100644 --- a/src/language.h +++ b/src/language.h @@ -113,7 +113,7 @@ Bool TY_(tidySetLanguage)( ctmbstr languageCode ); /** * Gets the current language used by Tidy. */ -ctmbstr TY_(tidyGetLanguage)(); +ctmbstr TY_(tidyGetLanguage)(void); /** * Provides a string given `messageType` in the current @@ -145,7 +145,7 @@ ctmbstr TY_(tidyDefaultString)( uint messageType ); * these are provided for documentation generation purposes * and probably aren't useful for LibTidy implementors. */ -TidyIterator TY_(getStringKeyList)(); +TidyIterator TY_(getStringKeyList)(void); /* * Provides the next key value in Tidy's list of localized @@ -160,7 +160,7 @@ uint TY_(getNextStringKey)( TidyIterator* iter ); * in Tidy's structure of Windows<->POSIX local mapping. * Items can be retrieved with getNextWindowsLanguage(); */ -TidyIterator TY_(getWindowsLanguageList)(); +TidyIterator TY_(getWindowsLanguageList)(void); /** * Returns the next record of type `localeMapItem` in @@ -183,7 +183,7 @@ const ctmbstr TY_(TidyLangPosixName)( const tidyLocaleMapItemImpl *item ); * in Tidy's list of installed language codes. * Items can be retrieved with getNextInstalledLanguage(); */ -TidyIterator TY_(getInstalledLanguageList)(); +TidyIterator TY_(getInstalledLanguageList)(void); /** * Returns the next installed language. diff --git a/src/message.h b/src/message.h index f12ee95..4871e10 100644 --- a/src/message.h +++ b/src/message.h @@ -213,7 +213,7 @@ uint TY_(tidyErrorCodeFromKey)(ctmbstr code); * `TidyReportFilter3`. * Items can be retrieved with getNextErrorCode(); */ -TidyIterator TY_(getErrorCodeList)(); +TidyIterator TY_(getErrorCodeList)(void); /** * Returns the next error code having initialized the iterator diff --git a/src/pprint.c b/src/pprint.c index a085463..d371507 100644 --- a/src/pprint.c +++ b/src/pprint.c @@ -18,12 +18,10 @@ #include "utf8.h" /* *** FOR DEBUG ONLY *** */ -#if !defined(NDEBUG) && defined(_MSC_VER) /* #define DEBUG_PPRINT */ /* #define DEBUG_INDENT */ -# ifdef DEBUG_PPRINT +#if !defined(NDEBUG) && defined(DEBUG_PPRINT) extern void dbg_show_node( TidyDocImpl* doc, Node *node, int caller, int indent ); -# endif #endif /* @@ -616,7 +614,7 @@ static Bool CheckWrapIndent( TidyDocImpl* doc, uint indent ) WrapLine( doc ); if ( pprint->indent[ 0 ].spaces < 0 ) { -#if !defined(NDEBUG) && defined(_MSC_VER) && defined(DEBUG_INDENT) +#if !defined(NDEBUG) && defined(DEBUG_INDENT) SPRTF("%s Indent from %d to %d\n", __FUNCTION__, pprint->indent[ 0 ].spaces, indent ); #endif pprint->indent[ 0 ].spaces = indent; @@ -680,7 +678,7 @@ void TY_(PFlushLine)( TidyDocImpl* doc, uint indent ) if (pprint->indent[ 0 ].spaces != (int)indent ) { -#if !defined(NDEBUG) && defined(_MSC_VER) && defined(DEBUG_INDENT) +#if !defined(NDEBUG) && defined(DEBUG_INDENT) SPRTF("%s Indent from %d to %d\n", __FUNCTION__, pprint->indent[ 0 ].spaces, indent ); #endif pprint->indent[ 0 ].spaces = indent; @@ -702,7 +700,7 @@ static void PCondFlushLine( TidyDocImpl* doc, uint indent ) /* Issue #390 - Whether chars to flush or not, set new indent */ if ( pprint->indent[ 0 ].spaces != (int)indent ) { -#if !defined(NDEBUG) && defined(_MSC_VER) && defined(DEBUG_INDENT) +#if !defined(NDEBUG) && defined(DEBUG_INDENT) SPRTF("%s Indent from %d to %d\n", __FUNCTION__, pprint->indent[ 0 ].spaces, indent ); #endif pprint->indent[ 0 ].spaces = indent; @@ -730,7 +728,7 @@ void TY_(PFlushLineSmart)( TidyDocImpl* doc, uint indent ) if ( pprint->indent[ 0 ].spaces != (int)indent ) { -#if !defined(NDEBUG) && defined(_MSC_VER) && defined(DEBUG_INDENT) +#if !defined(NDEBUG) && defined(DEBUG_INDENT) SPRTF("%s Indent from %d to %d\n", __FUNCTION__, pprint->indent[ 0 ].spaces, indent ); #endif pprint->indent[ 0 ].spaces = indent; @@ -759,7 +757,7 @@ static void PCondFlushLineSmart( TidyDocImpl* doc, uint indent ) \*/ if (pprint->indent[ 0 ].spaces != (int)indent) { -#if !defined(NDEBUG) && defined(_MSC_VER) && defined(DEBUG_INDENT) +#if !defined(NDEBUG) && defined(DEBUG_INDENT) SPRTF("%s Indent from %d to %d\n", __FUNCTION__, pprint->indent[ 0 ].spaces, indent ); #endif pprint->indent[ 0 ].spaces = indent; @@ -1947,7 +1945,7 @@ void PPrintScriptStyle( TidyDocImpl* doc, uint mode, uint indent, Node *node ) if ( node->content && pprint->indent[ 0 ].spaces != (int)indent ) { -#if !defined(NDEBUG) && defined(_MSC_VER) && defined(DEBUG_INDENT) +#if !defined(NDEBUG) && defined(DEBUG_INDENT) SPRTF("%s Indent from %d to %d\n", __FUNCTION__, pprint->indent[ 0 ].spaces, indent ); #endif pprint->indent[ 0 ].spaces = indent; @@ -2055,7 +2053,7 @@ void TY_(PPrintTree)( TidyDocImpl* doc, uint mode, uint indent, Node *node ) doc->progressCallback( tidyImplToDoc(doc), node->line, node->column, doc->pprint.line + 1 ); } -#if !defined(NDEBUG) && defined(_MSC_VER) && defined(DEBUG_PPRINT) +#if !defined(NDEBUG) && defined(DEBUG_PPRINT) dbg_show_node( doc, node, 4, GetSpaces( &doc->pprint ) ); #endif @@ -2315,7 +2313,7 @@ void TY_(PPrintTree)( TidyDocImpl* doc, uint mode, uint indent, Node *node ) TidyPrintImpl* pprint = &doc->pprint; if (pprint->indent[ 0 ].spaces != (int)indent) { -#if !defined(NDEBUG) && defined(_MSC_VER) && defined(DEBUG_INDENT) +#if !defined(NDEBUG) && defined(DEBUG_INDENT) SPRTF("%s Indent from %d to %d\n", __FUNCTION__, pprint->indent[ 0 ].spaces, indent ); #endif pprint->indent[ 0 ].spaces = indent; diff --git a/src/sprtf.h b/src/sprtf.h index c589707..322009c 100644 --- a/src/sprtf.h +++ b/src/sprtf.h @@ -43,20 +43,20 @@ TIDY_EXPORT int add_append_log( int val ); TIDY_EXPORT int open_log_file( void ); TIDY_EXPORT void close_log_file( void ); -TIDY_EXPORT void set_log_file( char * nf, int open ); /* used */ -TIDY_EXPORT char * get_log_file( void ); /* used */ +TIDY_EXPORT void set_log_file( char * nf, int open ); +TIDY_EXPORT char * get_log_file( void ); -TIDY_EXPORT int MCDECL sprtf( const char *pf, ... ); /* used */ +TIDY_EXPORT int MCDECL sprtf( const char *pf, ... ); #define M_MAX_SPRTF 2048 TIDY_EXPORT int direct_out_it( char *cp ); -TIDY_EXPORT char *GetNxtBuf(); +TIDY_EXPORT char *GetNxtBuf(void); #define EndBuf(a) ( a + strlen(a) ) -TIDY_EXPORT char *get_date_stg(); -TIDY_EXPORT char *get_time_stg(); -TIDY_EXPORT char *get_date_time_stg(); +TIDY_EXPORT char *get_date_stg(void); +TIDY_EXPORT char *get_time_stg(void); +TIDY_EXPORT char *get_date_time_stg(void); #ifdef _MSC_VER TIDY_EXPORT int gettimeofday(struct timeval *tp, void *tzp); diff --git a/src/tags.c b/src/tags.c index 81d67a8..f965177 100644 --- a/src/tags.c +++ b/src/tags.c @@ -473,10 +473,7 @@ static void declare( TidyDocImpl* doc, TidyTagImpl* tags, } } -#if !defined(NDEBUG) && defined(_MSC_VER) -/* ==================================================================== - MSVC DEBUG ONLY - */ +#if !defined(NDEBUG) void ListElementsPerVersion( uint vers, Bool has ) { uint val, cnt, total, wrap = 10; @@ -522,7 +519,7 @@ void show_have_html5(void) ListElementsPerVersion( VERS_HTML5, yes ); } -#endif +#endif /* !defined(NDEBUG) */ /* public interface for finding tag by name */ Bool TY_(FindTag)( TidyDocImpl* doc, Node *node ) diff --git a/src/tidylib.c b/src/tidylib.c index 9befcba..d433edc 100644 --- a/src/tidylib.c +++ b/src/tidylib.c @@ -1405,7 +1405,7 @@ void tidyDocReportDoctype( TidyDocImpl* doc ) /***************************************************************************** * HTML5 STUFF *****************************************************************************/ -#if !defined(NDEBUG) && defined(_MSC_VER) +#if 0 && !defined(NDEBUG) extern void show_not_html5(void); /* ----------------------------- List tags that do not have version HTML5 (HT50|XH50) @@ -1504,9 +1504,7 @@ void TY_(CheckHTML5)( TidyDocImpl* doc, Node* node ) Bool warn = yes; /* should this be a warning, error, or report??? */ AttVal* attr = NULL; int i = 0; -#if !defined(NDEBUG) && defined(_MSC_VER) - // list_not_html5(); -#endif + while (node) { if ( nodeHasAlignAttr( node ) ) { @@ -1782,7 +1780,7 @@ void TY_(CheckHTMLTagsAttribsVersions)( TidyDocImpl* doc, Node* node ) } -#if !defined(NDEBUG) && defined(_MSC_VER) +#if !defined(NDEBUG) /* *** FOR DEBUG ONLY *** */ const char *dbg_get_lexer_type( void *vp ) { @@ -1919,7 +1917,7 @@ int tidyDocCleanAndRepair( TidyDocImpl* doc ) Bool mergeEmphasis = cfgBool( doc, TidyMergeEmphasis ); Node* node; -#if !defined(NDEBUG) && defined(_MSC_VER) +#if !defined(NDEBUG) SPRTF("All nodes BEFORE clean and repair\n"); dbg_show_all_nodes( doc, &doc->root, 0 ); #endif @@ -2037,7 +2035,7 @@ int tidyDocCleanAndRepair( TidyDocImpl* doc ) } } -#if !defined(NDEBUG) && defined(_MSC_VER) +#if !defined(NDEBUG) SPRTF("All nodes AFTER clean and repair\n"); dbg_show_all_nodes( doc, &doc->root, 0 ); #endif