scm.cpp revision cea38eeef9a0a2060a343a9bf2b82a1f51963b46
/* $Id$ */
/** @file
* IPRT Testcase / Tool - Source Code Massager.
*/
/*
* Copyright (C) 2010 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/initterm.h>
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/** End of line marker type. */
typedef enum SCMEOL
{
SCMEOL_NONE = 0,
SCMEOL_LF = 1,
SCMEOL_CRLF = 2
} SCMEOL;
/** Pointer to an end of line marker type. */
/**
* Line record.
*/
typedef struct SCMSTREAMLINE
{
/** The offset of the line. */
/** The line length, excluding the LF character.
* @todo This could be derived from the offset of the next line if that wasn't
* so tedious. */
/** The end of line marker type. */
/** Pointer to a line record. */
typedef SCMSTREAMLINE *PSCMSTREAMLINE;
/**
* Source code massager stream.
*/
typedef struct SCMSTREAM
{
/** Pointer to the file memory. */
char *pch;
/** The current stream position. */
/** The current stream size. */
/** The size of the memory pb points to. */
/** Line records. */
/** The current line. */
/** The current stream size given in lines. */
/** The sizeof the the memory backing paLines. */
/** Set if write-only, clear if read-only. */
bool fWriteOrRead;
/** Set if the memory pb points to is from RTFileReadAll. */
bool fFileMemory;
/** Set if fully broken into lines. */
bool fFullyLineated;
/** Stream status code (IPRT). */
int rc;
} SCMSTREAM;
/** Pointer to a SCM stream. */
typedef SCMSTREAM *PSCMSTREAM;
/** Pointer to a const SCM stream. */
typedef SCMSTREAM const *PCSCMSTREAM;
/**
* A rewriter.
*
* This works like a stream editor, reading @a pIn, modifying it and writing it
* to @a pOut.
*
* @returns true if any changes were made, false if not.
* @param pIn The input stream.
* @param pOut The output stream.
*/
/**
* Configuration entry.
*/
typedef struct SCMCFGENTRY
{
/** Number of rewriters. */
/** Pointer to an array of rewriters. */
PFNSCMREWRITER const *papfnRewriter;
/** File pattern (simple). */
const char *pszFilePattern;
} SCMCFGENTRY;
typedef SCMCFGENTRY *PSCMCFGENTRY;
typedef SCMCFGENTRY const *PCSCMCFGENTRY;
/**
* Diff state.
*/
typedef struct SCMDIFFSTATE
{
const char *pszFilename;
/** Whether to ignore end of line markers when diffing. */
bool fIgnoreEol;
/** Whether to ignore trailing whitespace. */
bool fIgnoreTrailingWhite;
/** Whether to ignore leading whitespace. */
bool fIgnoreLeadingWhite;
/** Whether to print special characters in human readable form or not. */
bool fSpecialChars;
/** Where to push the diff. */
} SCMDIFFSTATE;
/** Pointer to a diff state. */
typedef SCMDIFFSTATE *PSCMDIFFSTATE;
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/*******************************************************************************
* Global Variables *
*******************************************************************************/
static const char g_szProgName[] = "scm";
static const char *g_pszChangedSuff = "";
static bool g_fDryRun = true;
static bool g_fStripTrailingBlanks = true;
static bool g_fStripTrailingLines = true;
static bool g_fForceFinalEol = true;
static bool g_fForceTrailingLine = false;
static bool g_fConvertTabs = true;
static unsigned g_cchTab = 8;
static bool g_fConvertEol = true;
static bool g_fDiffSpecialChars = true;
static bool g_fDiffIgnoreEol = false;
static bool g_fDiffIgnoreLeadingWS = false;
static bool g_fDiffIgnoreTrailingWS = false;
static const char *g_pszFileFilter = "*";
static const char *g_pszFileFilterOut =
"*.exe|"
"*.com|"
"20*-*-*.log|"
"*/src/VBox/Runtime/testcase/soundcard.h|"
"/dummy/."
;
static const char *g_pszDirFilter = NULL;
static const char *g_pszDirFilterOut =
// generic
".svn|"
"CVS|"
// root
"out|"
"tools|"
"webtools|"
"kBuild|"
"debian|"
"SlickEdit|"
// src
"*/src/VBox/Additions/x11/x11include/.|"
"*/src/VBox/HostServices/SharedOpenGL/.|"
"*/src/VBox/Devices/PC/Etherboot-src/*/.|"
"*/src/VBox/Devices/Storage/VBoxHDDFormats/StorageCraft/*/.|"
"/dummy"
;
static PFNSCMREWRITER const g_aRewritersFor_Makefile_kup[] =
{
};
static PFNSCMREWRITER const g_aRewritersFor_Makefile_kmk[] =
{
};
static PFNSCMREWRITER const g_aRewritersFor_C_and_CPP[] =
{
};
static PFNSCMREWRITER const g_aRewritersFor_ShellScripts[] =
{
};
static PFNSCMREWRITER const g_aRewritersFor_BatchFiles[] =
{
};
static SCMCFGENTRY const g_aConfigs[] =
{
{ RT_ELEMENTS(g_aRewritersFor_C_and_CPP), &g_aRewritersFor_C_and_CPP[0], "*.c|*.h|*.cpp|*.hpp|*.C|*.CPP|*.cxx|*.cc" },
{ RT_ELEMENTS(g_aRewritersFor_BatchFiles), &g_aRewritersFor_BatchFiles[0], "*.bat|*.cmd|*.btm|*.vbs|*.ps1" },
};
/* -=-=-=-=-=- memory streams -=-=-=-=-=- */
/**
* Initializes the stream structure.
*
* @param pStream The stream structure.
* @param fWriteOrRead The value of the fWriteOrRead stream member.
*/
{
pStream->cbAllocated = 0;
pStream->cLinesAllocated = 0;
pStream->fFileMemory = false;
pStream->fFullyLineated = false;
}
/**
* Initialize an input stream.
*
* @returns IPRT status code.
* @param pStream The stream to initialize.
* @param pszFilename The file to take the stream content from.
*/
{
void *pvFile;
if (RT_SUCCESS(rc))
{
pStream->fFileMemory = true;
}
return rc;
}
/**
* Initialize an output stream.
*
* @returns IPRT status code
* @param pStream The stream to initialize.
* @param pRelatedStream Pointer to a related stream. NULL is fine.
*/
{
/* allocate stuff */
: _64K;
{
: cbEstimate / 24;
{
return VINF_SUCCESS;
}
}
}
/**
* Frees the resources associated with the stream.
*
* Nothing is happens to whatever the stream was initialized from or dumped to.
*
* @param pStream The stream to delete.
*/
{
{
if (pStream->fFileMemory)
else
}
pStream->cbAllocated = 0;
{
}
pStream->cLinesAllocated = 0;
}
/**
* Get the stream status code.
*
* @returns IPRT status code.
* @param pStream The stream.
*/
{
}
/**
* Grows the buffer of a write stream.
*
* @returns IPRT status code.
* @param pStream The stream. Must be in write mode.
* @param cbAppending The minimum number of bytes to grow the buffer
* with.
*/
{
void *pvNew;
if (!pStream->fFileMemory)
{
if (!pvNew)
}
else
{
if (!pvNew)
pStream->fFileMemory = false;
}
return VINF_SUCCESS;
}
/**
* Grows the line array of a stream.
*
* @returns IPRT status code.
* @param pStream The stream.
* @param iMinLine Minimum line number.
*/
{
if (!pvNew)
return VINF_SUCCESS;
}
/**
* Rewinds the stream and sets the mode to read.
*
* @param pStream The stream.
*/
{
pStream->fWriteOrRead = false;
}
/**
* Rewinds the stream and sets the mode to write.
*
* @param pStream The stream.
*/
{
pStream->fWriteOrRead = true;
pStream->fFullyLineated = true;
}
/**
* Checks if it's a text stream.
*
* Not 100% proof.
*
* @returns true if it probably is a text file, false if not.
* @param pStream The stream. Write or read, doesn't matter.
*/
{
return false;
return false;
return true;
}
/**
* Performs an integrity check of the stream.
*
* @returns IPRT status code.
* @param pStream The stream.
*/
{
/*
* Perform sanity checks.
*/
{
{
case SCMEOL_LF:
break;
case SCMEOL_CRLF:
break;
case SCMEOL_NONE:
break;
default:
}
}
return VINF_SUCCESS;
}
/**
* Writes the stream to a file.
*
* @returns IPRT status code
* @param pStream The stream.
* @param pszFilenameFmt The filename format string.
* @param ... Format arguments.
*/
{
int rc;
#ifdef RT_STRICT
/*
* Check that what we're going to write makes sense first.
*/
if (RT_FAILURE(rc))
return rc;
#endif
/*
* Do the actual writing.
*/
rc = RTFileOpenV(&hFile, RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_WRITE, pszFilenameFmt, va);
if (RT_SUCCESS(rc))
{
}
return rc;
}
/**
* Worker for ScmStreamGetLine that builds the line number index while parsing
* the stream.
*
* @returns Same as SCMStreamGetLine.
* @param pStream The stream. Must be in read mode.
* @param pcchLine Where to return the line length.
* @param penmEol Where to return the kind of end of line marker.
*/
{
return NULL;
{
pStream->fFullyLineated = true;
return NULL;
}
{
if (RT_FAILURE(rc))
return NULL;
}
{
if ( cb < 1
else
{
cb--;
}
}
else
{
}
return pchRet;
}
/**
* Internal worker that lineates a stream.
*
* @returns IPRT status code.
* @param pStream The stream. Caller must check that it is in
* read mode.
*/
{
/* Save the stream position. */
/* Get each line. */
/* nothing */;
/* Restore the position */
}
/**
* Get the current stream position as a line number.
*
* @returns The current line (0-based).
* @param pStream The stream.
*/
{
}
/**
* Gets the number of lines in the stream.
*
* @returns The number of lines.
* @param pStream The stream.
*/
{
if (!pStream->fFullyLineated)
}
/**
* Seeks to a given line in the tream.
*
* @returns IPRT status code.
*
* @param pStream The stream. Must be in read mode.
* @param iLine The line to seek to. If this is beyond the end
* of the stream, the position is set to the end.
*/
{
/* Must be fully lineated of course. */
{
if (RT_FAILURE(rc))
return rc;
}
/* Ok, do the job. */
{
}
else
{
}
return VINF_SUCCESS;
}
/**
* Get a numbered line from the stream (changes the position).
*
* A line is always delimited by a LF character or the end of the stream. The
* delimiter is not included in returned line length, but instead returned via
* the @a penmEol indicator.
*
* @returns Pointer to the first character in the line, not NULL terminated.
* NULL if the end of the stream has been reached or some problem
* occured.
*
* @param pStream The stream. Must be in read mode.
* @param iLine The line to get (0-based).
* @param pcchLine The length.
* @param penmEol Where to return the end of line type indicator.
*/
static const char *ScmStreamGetLineByNo(PSCMSTREAM pStream, size_t iLine, size_t *pcchLine, PSCMEOL penmEol)
{
return NULL;
/* Make sure it's fully lineated so we can use the index. */
{
if (RT_FAILURE(rc))
return NULL;
}
/* End of stream? */
{
return NULL;
}
/* Get the data. */
/* update the stream position. */
return pchRet;
}
/**
* Get a line from the stream.
*
* A line is always delimited by a LF character or the end of the stream. The
* delimiter is not included in returned line length, but instead returned via
* the @a penmEol indicator.
*
* @returns Pointer to the first character in the line, not NULL terminated.
* NULL if the end of the stream has been reached or some problem
* occured.
*
* @param pStream The stream. Must be in read mode.
* @param pcchLine The length.
* @param penmEol Where to return the end of line type indicator.
*/
{
if (!pStream->fFullyLineated)
}
/**
* Checks if the given line is empty or full of white space.
*
* @returns true if white space only, false if not (or if non-existant).
* @param pStream The stream. Must be in read mode.
* @param iLine The line in question.
*/
{
if (!pchLine)
return false;
return cchLine == 0;
}
/**
* Try figure out the end of line style of the give stream.
*
* @returns Most likely end of line style.
* @param pStream The stream.
*/
{
else
{
else
}
if (enmEol == SCMEOL_NONE)
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
#else
#endif
return enmEol;
}
/**
* Get the end of line indicator type for a line.
*
* @returns The EOL indicator. If the line isn't found, the default EOL
* indicator is return.
* @param pStream The stream.
* @param iLine The line (0-base).
*/
{
else
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
#else
#endif
return enmEol;
}
/**
* Appends a line to the stream.
*
* @returns IPRT status code.
* @param pStream The stream. Must be in write mode.
* @param pchLine Pointer to the line.
* @param cchLine Line length.
* @param enmEol Which end of line indicator to use.
*/
{
/*
* Make sure the previous line has a new-line indicator.
*/
if (RT_UNLIKELY( iLine != 0
{
{
if (RT_FAILURE(rc))
return rc;
}
else
{
}
}
/*
* Ensure we've got sufficient buffer space.
*/
{
if (RT_FAILURE(rc))
return rc;
}
/*
* Add a line record.
*/
{
if (RT_FAILURE(rc))
return rc;
}
iLine++;
/*
* Copy the line
*/
else if (enmEol == SCMEOL_CRLF)
{
}
/*
* Start a new line.
*/
return VINF_SUCCESS;
}
/**
* Writes to the stream.
*
* @returns IPRT status code
* @param pStream The stream. Must be in write mode.
* @param pchBuf What to write.
* @param cchBuf How much to write.
*/
{
/*
* Ensure we've got sufficient buffer space.
*/
{
if (RT_FAILURE(rc))
return rc;
}
/*
* Deal with the odd case where we've already pushed a line with SCMEOL_NONE.
*/
if (RT_UNLIKELY( iLine > 0
{
iLine--;
}
/*
* Deal with lines.
*/
if (!pchLF)
else
{
for (;;)
{
{
if (RT_FAILURE(rc))
{
return rc;
}
}
if ( cchLine
else
{
cchLine--;
}
iLine++;
if (!pchLF)
{
break;
}
}
}
/*
* Copy the data and update position and size.
*/
return VINF_SUCCESS;
}
/**
* Write a character to the stream.
*
* @returns IPRT status code
* @param pStream The stream. Must be in write mode.
* @param pchBuf What to write.
* @param cchBuf How much to write.
*/
{
/*
* Only deal with the simple cases here, use ScmStreamWrite for the
* annyoing stuff.
*/
if ( ch == '\n'
/*
* Just append it.
*/
return VINF_SUCCESS;
}
/**
* Copies @a cLines from the @a pSrc stream onto the @a pDst stream.
*
* The stream positions will be used and changed in both streams.
*
* @returns IPRT status code.
* @param pDst The destionation stream. Must be in write mode.
* @param cLines The number of lines. (0 is accepted.)
* @param pSrc The source stream. Must be in read mode.
*/
{
while (cLines-- > 0)
{
if (!pchLine)
if (RT_FAILURE(rc))
return rc;
}
return VINF_SUCCESS;
}
/* -=-=-=-=-=- diff -=-=-=-=-=- */
/**
* Prints a range of lines with a prefix.
*
* @param pState The diff state.
* @param chPrefix The prefix.
* @param pStream The stream to get the lines from.
* @param iLine The first line.
* @param cLines The number of lines.
*/
static void scmDiffPrintLines(PSCMDIFFSTATE pState, char chPrefix, PSCMSTREAM pStream, size_t iLine, size_t cLines)
{
while (cLines-- > 0)
{
{
if (!pState->fSpecialChars)
else
{
while (pchTab)
{
switch (cchTab)
{
}
/* next */
}
if (cchLeft)
}
}
if (!pState->fSpecialChars)
else if (enmEol == SCMEOL_CRLF)
else
iLine++;
}
}
/**
* Reports a difference and propells the streams to the lines following the
* resync.
*
*
* @returns New pState->cDiff value (just to return something).
* @param pState The diff state. The cDiffs member will be
* incremented.
* @param cMatches The resync length.
* @param iLeft Where the difference starts on the left side.
* @param cLeft How long it is on this side. ~(size_t)0 is used
* to indicate that it goes all the way to the end.
* @param iRight Where the difference starts on the right side.
* @param cRight How long it is.
*/
{
/*
* Adjust the input.
*/
{
if (c >= iLeft)
else
{
iLeft = c;
cLeft = 0;
}
}
{
if (c >= iRight)
else
{
iRight = c;
cRight = 0;
}
}
/*
* Print header if it's the first difference
*/
/*
* Emit the change description.
*/
? 'a'
: cRight == 0
? 'd'
: 'c';
RTStrmPrintf(pState->pDiff, "%zu,%zu%c%zu,%zu\n", iLeft + 1, iLeft + cLeft, ch, iRight + 1, iRight + cRight);
else if (cLeft > 1)
else if (cRight > 1)
else
/*
* And the lines.
*/
if (cLeft)
if (cRight)
/*
* Reposition the streams (safely ignores return value).
*/
}
/**
* Helper for scmDiffCompare that takes care of trailing spaces and stuff
* like that.
*/
{
if (pState->fIgnoreTrailingWhite)
{
cchLeft--;
cchRight--;
}
if (pState->fIgnoreLeadingWhite)
{
}
return false;
return true;
}
/**
* Compare two lines.
*
* @returns true if the are equal, false if not.
*/
{
{
if ( pState->fIgnoreTrailingWhite
return scmDiffCompareSlow(pState,
return false;
}
return true;
}
/**
* Compares two sets of lines from the two files.
*
* @returns true if they matches, false if they don't.
* @param pState The diff state.
* @param iLeft Where to start in the left stream.
* @param iRight Where to start in the right stream.
* @param cLines How many lines to compare.
*/
{
{
const char *pchRight = ScmStreamGetLineByNo(pState->pRight, iRight + iLine, &cchRight, &enmEolRight);
return false;
}
return true;
}
/**
* Resynchronize the two streams and reports the difference.
*
* Upon return, the streams will be positioned after the block of @a cMatches
* lines where it resynchronized them.
*
* @returns pState->cDiffs (just so we can use it in a return statement).
* @param pState The state.
* @param cMatches The number of lines that needs to match for the
* stream to be considered synchronized again.
*/
{
/*
* Compare each new line from each of the streams will all the preceding
* ones, including iStartLeft/Right.
*/
{
/*
* Get the next line in the left stream and compare it against all the
* preceding lines on the right side.
*/
if (!pchLine)
{
&cchRight, &enmEolRight);
cMatches - 1)
)
}
/*
* Get the next line in the right stream and compare it against all the
* lines on the right side.
*/
if (!pchLine)
{
&cchLeft, &enmEolLeft);
cMatches - 1)
)
}
}
}
/**
* Creates a diff of the changes between the streams @a pLeft and @a pRight.
*
* This currently only implements the simplest diff format, so no contexts.
*
* Also, note that we won't detect differences in the final newline of the
* streams.
*
* @returns The number of differences.
* @param pszFilename The filename.
* @param pLeft The left side stream.
* @param pRight The right side stream.
* @param fIgnoreEol Whether to ignore end of line markers.
* @param fIgnoreLeadingWhite Set if leading white space should be ignored.
* @param fIgnoreTrailingWhite Set if trailing white space should be ignored.
* @param fSpecialChars Whether to print special chars in a human
* readable form or not.
* @param pDiff Where to write the diff.
*/
size_t ScmDiffStreams(const char *pszFilename, PSCMSTREAM pLeft, PSCMSTREAM pRight, bool fIgnoreEol,
{
#ifdef RT_STRICT
#endif
/*
* Set up the diff state.
*/
/*
* Compare them line by line.
*/
const char *pchLeft;
const char *pchRight;
for (;;)
{
break;
}
/*
* Deal with any remaining differences.
*/
if (pchLeft)
else if (pchRight)
/*
* Report any errors.
*/
}
/* -=-=-=-=-=- misc -=-=-=-=-=- */
/**
* Prints a verbose message if the level is high enough.
*
* @param iLevel The required verbosity level.
* @param pszFormat The message format string.
* @param ... Format arguments.
*/
{
if (iLevel <= g_iVerbosity)
{
}
}
/* -=-=-=-=-=- rewriters -=-=-=-=-=- */
/**
* Strip trailing blanks (space & tab).
*
* @returns True if modified, false if not.
* @param pIn The input stream.
* @param pOut The output stream.
*/
{
if (!g_fStripTrailingBlanks)
return false;
bool fModified = false;
const char *pchLine;
{
int rc;
if ( cchLine == 0
else
{
cchLine--;
cchLine--;
fModified = true;
}
if (RT_FAILURE(rc))
return false;
}
if (fModified)
return fModified;
}
/**
* Expand tabs.
*
* @returns True if modified, false if not.
* @param pIn The input stream.
* @param pOut The output stream.
*/
{
if (!g_fConvertTabs)
return false;
bool fModified = false;
const char *pchLine;
{
int rc;
if (!pchTab)
else
{
for (;;)
{
if (!pchTab)
{
break;
}
}
fModified = true;
}
if (RT_FAILURE(rc))
return false;
}
if (fModified)
return fModified;
}
/**
* Worker for rewrite_ForceNativeEol, rewrite_ForceLF and rewrite_ForceCRLF.
*
* @returns true if modifications were made, false if not.
* @param pIn The input stream.
* @param pOut The output stream.
* @param enmDesiredEol The desired end of line indicator type.
*/
{
if (!g_fConvertEol)
return false;
bool fModified = false;
const char *pchLine;
{
if ( enmEol != enmDesiredEol
&& enmEol != SCMEOL_NONE)
{
fModified = true;
}
if (RT_FAILURE(rc))
return false;
}
if (fModified)
return fModified;
}
/**
* Force native end of line indicator.
*
* @returns true if modifications were made, false if not.
* @param pIn The input stream.
* @param pOut The output stream.
*/
{
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
#else
#endif
}
/**
* Force the stream to use LF as the end of line indicator.
*
* @returns true if modifications were made, false if not.
* @param pIn The input stream.
* @param pOut The output stream.
*/
{
}
/**
* Force the stream to use CRLF as the end of line indicator.
*
* @returns true if modifications were made, false if not.
* @param pIn The input stream.
* @param pOut The output stream.
*/
{
}
/**
* at the end of the file.
*
* @returns true if modifications were made, false if not.
* @param pIn The input stream.
* @param pOut The output stream.
*
* @remarks ASSUMES trailing white space has been removed already.
*/
{
return false;
/* Empty files remains empty. */
if (cLines <= 1)
return false;
/* Figure out if we need to adjust the number of lines or not. */
{
while ( cLinesNew > 1
cLinesNew--;
}
if ( g_fForceTrailingLine
cLinesNew++;
bool fFixMissingEol = g_fForceFinalEol
if ( !fFixMissingEol
return false;
/* Copy the number of lines we've arrived at. */
{
}
else if (fFixMissingEol)
{
else
}
return true;
}
/**
* Makefile.kup are empty files, enforce this.
*
* @returns true if modifications were made, false if not.
* @param pIn The input stream.
* @param pOut The output stream.
*/
{
/* These files should be zero bytes. */
return false;
return true;
}
/**
* Rewrite a kBuild makefile.
*
* @returns true if modifications were made, false if not.
* @param pIn The input stream.
* @param pOut The output stream.
*
* @todo
*
* Ideas for Makefile.kmk and Config.kmk:
* - line continuation slashes should only be preceeded by one space.
*/
{
return false;
}
/**
* Rewrite a C/C++ source or header file.
*
* @returns true if modifications were made, false if not.
* @param pIn The input stream.
* @param pOut The output stream.
*
* @todo
*
* Ideas for C/C++:
* - space after if, while, for, switch
* - spaces in for (i=0;i<x;i++)
* - complex conditional, bird style.
* - remove unnecessary parentheses.
* - sort defined RT_OS_*|| and RT_ARCH
* - sizeof without parenthesis.
* - defined without parenthesis.
* - trailing spaces.
* - parameter indentation.
* - space after comma.
* - while (x--); -> multi line + comment.
* - else statement;
* - space between function and left parenthesis.
* - TODO, XXX, @todo cleanup.
* - ensure new line at end of file.
* - Indentation of precompiler statements (#ifdef, #defines).
* - space between functions.
*/
{
return false;
}
/* -=-=-=-=-=- file and directory processing -=-=-=-=-=- */
/**
* Processes a file.
*
* @returns IPRT status code.
* @param pszFilename The file name.
* @param pszBasename The base name (pointer within @a pszFilename).
* @param cchBasename The length of the base name. (For passing to
* RTStrSimplePatternMultiMatch.)
*/
{
/*
* Do the file level filtering.
*/
if ( g_pszFileFilter
{
return VINF_SUCCESS;
}
if ( g_pszFileFilterOut
{
return VINF_SUCCESS;
}
/*
* Try find a matching rewrite config for this filename.
*/
if (RTStrSimplePatternMultiMatch(g_aConfigs[iCfg].pszFilePattern, RTSTR_MAX, pszBasename, cchBasename, NULL))
{
break;
}
if (!pCfg)
{
return VINF_SUCCESS;
}
/*
* Create an input stream from the file and check that it's text.
*/
if (RT_FAILURE(rc))
{
return rc;
}
if (ScmStreamIsText(&Stream1))
{
/*
* Create two more streams for output and push the text thru all the
* rewriters, switching the two streams around when something is
* actually rewritten. Stream1 remains unchanged.
*/
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
bool fModified = false;
{
if (fRc)
{
fModified = true;
}
}
/*
* If rewritten, write it back to disk.
*/
if (fModified)
{
if (!g_fDryRun)
{
if (RT_FAILURE(rc))
}
else
{
}
}
else
}
else
}
else
}
else
return rc;
}
/**
* Tries to correct RTDIRENTRY_UNKNOWN.
*
* @returns Corrected type.
* @param pszPath The path to the object in question.
*/
{
if (RT_FAILURE(rc))
return RTDIRENTRYTYPE_UNKNOWN;
return RTDIRENTRYTYPE_DIRECTORY;
return RTDIRENTRYTYPE_FILE;
return RTDIRENTRYTYPE_UNKNOWN;
}
/**
* Recurse into a sub-directory and process all the files and directories.
*
* @returns IPRT status code.
* @param pszBuf Path buffer containing the directory path on
* entry. This ends with a dot. This is passed
* along when recusing in order to save stack space
* and avoid needless copying.
* @param cchDir Length of our path in pszbuf.
* @param pEntry Directory entry buffer. This is also passed
* along when recursing to save stack space.
* @param iRecursion The recursion depth. This is used to restrict
* the recursions.
*/
static int scmProcessDirTreeRecursion(char *pszBuf, size_t cchDir, PRTDIRENTRY pEntry, unsigned iRecursion)
{
/*
* Make sure we stop somewhere.
*/
if (iRecursion > 128)
{
return VINF_SUCCESS; /* ignore */
}
/*
* Try open and read the directory.
*/
if (RT_FAILURE(rc))
{
return rc;
}
for (;;)
{
/* Read the next entry. */
if (RT_FAILURE(rc))
break;
/* Skip '.' and '..'. */
continue;
/* Enter it into the buffer so we've got a full name to work
with when needed. */
{
continue;
}
/* Figure the type. */
if (enmType == RTDIRENTRYTYPE_UNKNOWN)
/* Process the file or directory, skip the rest. */
if (enmType == RTDIRENTRYTYPE_FILE)
else if (enmType == RTDIRENTRYTYPE_DIRECTORY)
{
/* Append the dot for the benefit of the pattern matching. */
{
continue;
}
if ( ( g_pszDirFilter == NULL
&& ( g_pszDirFilterOut == NULL
)
)
)
{
}
}
if (RT_FAILURE(rc))
break;
}
}
/**
* Process a directory tree.
*
* @returns IPRT status code.
* @param pszDir The directory to start with. This is pointer to
* a RTPATH_MAX sized buffer.
*/
static int scmProcessDirTree(char *pszDir)
{
/*
* Setup the recursion.
*/
if (RT_SUCCESS(rc))
{
}
else
return rc;
}
/**
* Processes a file or directory specified as an command line argument.
*
* @returns IPRT status code
* @param pszSomething What we found in the commad line arguments.
*/
static int scmProcessSomething(const char *pszSomething)
{
char szBuf[RTPATH_MAX];
if (RT_SUCCESS(rc))
{
if (RTFileExists(szBuf))
{
if (pszBasename)
{
}
else
{
RTMsgError("RTPathFilename: NULL\n");
}
}
else
}
else
return rc;
}
{
if (RT_FAILURE(rc))
return 1;
/*
* Parse arguments and process input in order (because this is the only
* thing that works at the moment).
*/
enum SCMOPT
{
SCMOPT_DIFF_IGNORE_EOL = 10000,
};
static const RTGETOPTDEF s_aOpts[] =
{
};
size_t cProcessed = 0;
{
switch (rc)
{
case 'b':
g_fStripTrailingBlanks = true;
break;
case 'B':
g_fStripTrailingBlanks = false;
break;
case 'c':
g_fConvertTabs = true;
break;
case 'C':
g_fConvertTabs = false;
break;
case 'd':
g_fDryRun = true;
break;
case 'D':
g_fDryRun = false;
break;
case 'e':
g_fConvertEol = true;
break;
case 'E':
g_fConvertEol = false;
break;
case 'f':
break;
case 'h':
RTPrintf("Source Code Massager\n"
"\n"
"Usage: %s [options] <files & dirs>\n"
"\n"
"Options:\n", g_szProgName);
else
return 1;
case 'q':
g_iVerbosity = 0;
break;
case 't':
{
RTMsgError("Invalid tab size: %u - must be in {1..%u}\n",
return 2;
}
break;
case 'v':
g_iVerbosity++;
break;
case SCMOPT_DIFF_IGNORE_EOL:
g_fDiffIgnoreEol = true;
break;
g_fDiffIgnoreEol = false;
break;
case SCMOPT_DIFF_IGNORE_SPACE:
break;
g_fDiffIgnoreTrailingWS = g_fDiffIgnoreLeadingWS = false;
break;
g_fDiffIgnoreLeadingWS = true;
break;
g_fDiffIgnoreLeadingWS = false;
break;
g_fDiffIgnoreTrailingWS = true;
break;
g_fDiffIgnoreTrailingWS = false;
break;
g_fDiffSpecialChars = true;
break;
g_fDiffSpecialChars = false;
break;
g_fStripTrailingLines = true;
break;
g_fStripTrailingLines = false;
break;
case SCMOPT_FORCE_FINAL_EOL:
g_fForceFinalEol = true;
break;
g_fForceFinalEol = false;
break;
g_fForceTrailingLine = true;
break;
g_fForceTrailingLine = false;
break;
case VINF_GETOPT_NOT_OPTION:
{
if (!g_fDryRun)
{
if (!cProcessed)
{
RTPrintf("%s: Warning! This program will make changes to your source files and\n"
"%s: there is a slight risk that bugs or a full disk may cause\n"
"%s: LOSS OF DATA. So, please make sure you have checked in\n"
"%s: all your changes already. If you didn't, then don't blame\n"
"%s: anyone for not warning you!\n"
"%s:\n"
"%s: Press any key to continue...\n",
}
cProcessed++;
}
if (RT_FAILURE(rc))
return rc;
break;
}
default:
}
}
return 0;
}