scm.cpp revision 739c9e0e5fccb99475b8202ead8fc5665b2fa64c
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/* $Id$ */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/** @file
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * IPRT Testcase / Tool - Source Code Massager.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/*
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Copyright (C) 2010-2012 Oracle Corporation
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync *
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * available from http://www.virtualbox.org. This file is free software;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * you can redistribute it and/or modify it under the terms of the GNU
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * General Public License (GPL) as published by the Free Software
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/*******************************************************************************
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync* Header Files *
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync*******************************************************************************/
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync#include <iprt/assert.h>
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync#include <iprt/ctype.h>
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync#include <iprt/dir.h>
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync#include <iprt/env.h>
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync#include <iprt/file.h>
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync#include <iprt/err.h>
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync#include <iprt/getopt.h>
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync#include <iprt/initterm.h>
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync#include <iprt/mem.h>
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync#include <iprt/message.h>
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync#include <iprt/param.h>
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync#include <iprt/path.h>
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync#include <iprt/process.h>
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync#include <iprt/stream.h>
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync#include <iprt/string.h>
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync#include "scmstream.h"
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync#include "scmdiff.h"
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/*******************************************************************************
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync* Defined Constants And Macros *
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync*******************************************************************************/
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/** The name of the settings files. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync#define SCM_SETTINGS_FILENAME ".scm-settings"
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync/*******************************************************************************
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync* Structures and Typedefs *
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync*******************************************************************************/
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/** Pointer to const massager settings. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsynctypedef struct SCMSETTINGSBASE const *PCSCMSETTINGSBASE;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
2c95722bb302f4ce9454d04324b09314c0ccc038vboxsync/**
2c95722bb302f4ce9454d04324b09314c0ccc038vboxsync * SVN property.
2c95722bb302f4ce9454d04324b09314c0ccc038vboxsync */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsynctypedef struct SCMSVNPROP
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync{
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /** The property. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync char *pszName;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /** The value.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * When used to record updates, this can be set to NULL to trigger the
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * deletion of the property. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync char *pszValue;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync} SCMSVNPROP;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/** Pointer to a SVN property. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsynctypedef SCMSVNPROP *PSCMSVNPROP;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/** Pointer to a const SVN property. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsynctypedef SCMSVNPROP const *PCSCMSVNPROP;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/**
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Rewriter state.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsynctypedef struct SCMRWSTATE
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync{
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /** The filename. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync const char *pszFilename;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /** Set after the printing the first verbose message about a file under
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * rewrite. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync bool fFirst;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /** The number of SVN property changes. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync size_t cSvnPropChanges;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /** Pointer to an array of SVN property changes. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync PSCMSVNPROP paSvnPropChanges;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync} SCMRWSTATE;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/** Pointer to the rewriter state. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsynctypedef SCMRWSTATE *PSCMRWSTATE;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/**
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * A rewriter.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync *
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * This works like a stream editor, reading @a pIn, modifying it and writing it
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * to @a pOut.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync *
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @returns true if any changes were made, false if not.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pIn The input stream.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pOut The output stream.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pSettings The settings.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsynctypedef bool (*PFNSCMREWRITER)(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/**
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Configuration entry.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsynctypedef struct SCMCFGENTRY
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync{
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /** Number of rewriters. */
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync size_t cRewriters;
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync /** Pointer to an array of rewriters. */
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync PFNSCMREWRITER const *papfnRewriter;
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync /** File pattern (simple). */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync const char *pszFilePattern;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync} SCMCFGENTRY;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsynctypedef SCMCFGENTRY *PSCMCFGENTRY;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsynctypedef SCMCFGENTRY const *PCSCMCFGENTRY;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/**
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Source Code Massager Settings.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsynctypedef struct SCMSETTINGSBASE
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync{
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync bool fConvertEol;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync bool fConvertTabs;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync bool fForceFinalEol;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync bool fForceTrailingLine;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync bool fStripTrailingBlanks;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync bool fStripTrailingLines;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /** Only process files that are part of a SVN working copy. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync bool fOnlySvnFiles;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /** Only recurse into directories containing an .svn dir. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync bool fOnlySvnDirs;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /** Set svn:eol-style if missing or incorrect. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync bool fSetSvnEol;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /** Set svn:executable according to type (unusually this means deleting it). */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync bool fSetSvnExecutable;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /** Set svn:keyword if completely or partially missing. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync bool fSetSvnKeywords;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /** */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync unsigned cchTab;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /** Only consider files matching these patterns. This is only applied to the
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * base names. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync char *pszFilterFiles;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /** Filter out files matching the following patterns. This is applied to base
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * names as well as the absolute paths. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync char *pszFilterOutFiles;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /** Filter out directories matching the following patterns. This is applied
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * to base names as well as the absolute paths. All absolute paths ends with a
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * slash and dot ("/."). */
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync char *pszFilterOutDirs;
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync} SCMSETTINGSBASE;
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync/** Pointer to massager settings. */
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsynctypedef SCMSETTINGSBASE *PSCMSETTINGSBASE;
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/**
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Option identifiers.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync *
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @note The first chunk, down to SCMOPT_TAB_SIZE, are alternately set &
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * clear. So, the option setting a flag (boolean) will have an even
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * number and the one clearing it will have an odd number.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @note Down to SCMOPT_LAST_SETTINGS corresponds exactly to SCMSETTINGSBASE.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsynctypedef enum SCMOPT
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync{
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_CONVERT_EOL = 10000,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_NO_CONVERT_EOL,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_CONVERT_TABS,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_NO_CONVERT_TABS,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_FORCE_FINAL_EOL,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_NO_FORCE_FINAL_EOL,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_FORCE_TRAILING_LINE,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_NO_FORCE_TRAILING_LINE,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_STRIP_TRAILING_BLANKS,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_NO_STRIP_TRAILING_BLANKS,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_STRIP_TRAILING_LINES,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_NO_STRIP_TRAILING_LINES,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_ONLY_SVN_DIRS,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_NOT_ONLY_SVN_DIRS,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_ONLY_SVN_FILES,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_NOT_ONLY_SVN_FILES,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_SET_SVN_EOL,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_DONT_SET_SVN_EOL,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_SET_SVN_EXECUTABLE,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_DONT_SET_SVN_EXECUTABLE,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_SET_SVN_KEYWORDS,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_DONT_SET_SVN_KEYWORDS,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_TAB_SIZE,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_FILTER_OUT_DIRS,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_FILTER_FILES,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_FILTER_OUT_FILES,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_LAST_SETTINGS = SCMOPT_FILTER_OUT_FILES,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync //
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_DIFF_IGNORE_EOL,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_DIFF_NO_IGNORE_EOL,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_DIFF_IGNORE_SPACE,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_DIFF_NO_IGNORE_SPACE,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_DIFF_IGNORE_LEADING_SPACE,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_DIFF_IGNORE_TRAILING_SPACE,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_DIFF_SPECIAL_CHARS,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_DIFF_NO_SPECIAL_CHARS,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync SCMOPT_END
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync} SCMOPT;
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync/**
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync * File/dir pattern + options.
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync */
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsynctypedef struct SCMPATRNOPTPAIR
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync{
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync char *pszPattern;
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync char *pszOptions;
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync} SCMPATRNOPTPAIR;
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync/** Pointer to a pattern + option pair. */
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsynctypedef SCMPATRNOPTPAIR *PSCMPATRNOPTPAIR;
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync/** Pointer to a settings set. */
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsynctypedef struct SCMSETTINGS *PSCMSETTINGS;
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync/**
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync * Settings set.
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync *
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync * This structure is constructed from the command line arguments or any
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync * .scm-settings file found in a directory we recurse into. When recursing in
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync * and out of a directory, we push and pop a settings set for it.
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync *
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync * The .scm-settings file has two kinds of setttings, first there are the
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync * unqualified base settings and then there are the settings which applies to a
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync * set of files or directories. The former are lines with command line options.
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync * For the latter, the options are preceded by a string pattern and a colon.
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync * The pattern specifies which files (and/or directories) the options applies
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync * to.
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync *
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync * We parse the base options into the Base member and put the others into the
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync * paPairs array.
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync */
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsynctypedef struct SCMSETTINGS
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync{
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync /** Pointer to the setting file below us in the stack. */
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync PSCMSETTINGS pDown;
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync /** Pointer to the setting file above us in the stack. */
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync PSCMSETTINGS pUp;
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync /** File/dir patterns and their options. */
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync PSCMPATRNOPTPAIR paPairs;
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync /** The number of entires in paPairs. */
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync uint32_t cPairs;
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync /** The base settings that was read out of the file. */
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync SCMSETTINGSBASE Base;
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync} SCMSETTINGS;
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync/** Pointer to a const settings set. */
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsynctypedef SCMSETTINGS const *PCSCMSETTINGS;
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/*******************************************************************************
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync* Internal Functions *
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync*******************************************************************************/
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic bool rewrite_StripTrailingBlanks(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic bool rewrite_ExpandTabs(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic bool rewrite_ForceNativeEol(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic bool rewrite_ForceLF(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic bool rewrite_ForceCRLF(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic bool rewrite_AdjustTrailingLines(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic bool rewrite_SvnNoExecutable(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic bool rewrite_SvnKeywords(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic bool rewrite_Makefile_kup(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsyncstatic bool rewrite_Makefile_kmk(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic bool rewrite_C_and_CPP(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/*******************************************************************************
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync* Global Variables *
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync*******************************************************************************/
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic const char g_szProgName[] = "scm";
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsyncstatic const char *g_pszChangedSuff = "";
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsyncstatic const char g_szTabSpaces[16+1] = " ";
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsyncstatic bool g_fDryRun = true;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic bool g_fDiffSpecialChars = true;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic bool g_fDiffIgnoreEol = false;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic bool g_fDiffIgnoreLeadingWS = false;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic bool g_fDiffIgnoreTrailingWS = false;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic int g_iVerbosity = 2;//99; //0;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/** The global settings. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic SCMSETTINGSBASE const g_Defaults =
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync{
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* .fConvertEol = */ true,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* .fConvertTabs = */ true,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* .fForceFinalEol = */ true,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* .fForceTrailingLine = */ false,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* .fStripTrailingBlanks = */ true,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* .fStripTrailingLines = */ true,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* .fOnlySvnFiles = */ false,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* .fOnlySvnDirs = */ false,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* .fSetSvnEol = */ false,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* .fSetSvnExecutable = */ false,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* .fSetSvnKeywords = */ false,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* .cchTab = */ 8,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* .pszFilterFiles = */ (char *)"",
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* .pszFilterOutFiles = */ (char *)"*.exe|*.com|20*-*-*.log",
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /* .pszFilterOutDirs = */ (char *)".svn|.hg|.git|CVS",
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync};
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/** Option definitions for the base settings. */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic RTGETOPTDEF g_aScmOpts[] =
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync{
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { "--convert-eol", SCMOPT_CONVERT_EOL, RTGETOPT_REQ_NOTHING },
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { "--no-convert-eol", SCMOPT_NO_CONVERT_EOL, RTGETOPT_REQ_NOTHING },
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { "--convert-tabs", SCMOPT_CONVERT_TABS, RTGETOPT_REQ_NOTHING },
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { "--no-convert-tabs", SCMOPT_NO_CONVERT_TABS, RTGETOPT_REQ_NOTHING },
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { "--force-final-eol", SCMOPT_FORCE_FINAL_EOL, RTGETOPT_REQ_NOTHING },
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { "--no-force-final-eol", SCMOPT_NO_FORCE_FINAL_EOL, RTGETOPT_REQ_NOTHING },
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { "--force-trailing-line", SCMOPT_FORCE_TRAILING_LINE, RTGETOPT_REQ_NOTHING },
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { "--no-force-trailing-line", SCMOPT_NO_FORCE_TRAILING_LINE, RTGETOPT_REQ_NOTHING },
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { "--strip-trailing-blanks", SCMOPT_STRIP_TRAILING_BLANKS, RTGETOPT_REQ_NOTHING },
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { "--no-strip-trailing-blanks", SCMOPT_NO_STRIP_TRAILING_BLANKS, RTGETOPT_REQ_NOTHING },
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { "--strip-trailing-lines", SCMOPT_STRIP_TRAILING_LINES, RTGETOPT_REQ_NOTHING },
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { "--strip-no-trailing-lines", SCMOPT_NO_STRIP_TRAILING_LINES, RTGETOPT_REQ_NOTHING },
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { "--only-svn-dirs", SCMOPT_ONLY_SVN_DIRS, RTGETOPT_REQ_NOTHING },
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { "--not-only-svn-dirs", SCMOPT_NOT_ONLY_SVN_DIRS, RTGETOPT_REQ_NOTHING },
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { "--only-svn-files", SCMOPT_ONLY_SVN_FILES, RTGETOPT_REQ_NOTHING },
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { "--not-only-svn-files", SCMOPT_NOT_ONLY_SVN_FILES, RTGETOPT_REQ_NOTHING },
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { "--set-svn-eol", SCMOPT_SET_SVN_EOL, RTGETOPT_REQ_NOTHING },
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { "--dont-set-svn-eol", SCMOPT_DONT_SET_SVN_EOL, RTGETOPT_REQ_NOTHING },
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { "--set-svn-executable", SCMOPT_SET_SVN_EXECUTABLE, RTGETOPT_REQ_NOTHING },
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { "--dont-set-svn-executable", SCMOPT_DONT_SET_SVN_EXECUTABLE, RTGETOPT_REQ_NOTHING },
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { "--set-svn-keywords", SCMOPT_SET_SVN_KEYWORDS, RTGETOPT_REQ_NOTHING },
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { "--dont-set-svn-keywords", SCMOPT_DONT_SET_SVN_KEYWORDS, RTGETOPT_REQ_NOTHING },
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { "--tab-size", SCMOPT_TAB_SIZE, RTGETOPT_REQ_UINT8 },
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { "--filter-out-dirs", SCMOPT_FILTER_OUT_DIRS, RTGETOPT_REQ_STRING },
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { "--filter-files", SCMOPT_FILTER_FILES, RTGETOPT_REQ_STRING },
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { "--filter-out-files", SCMOPT_FILTER_OUT_FILES, RTGETOPT_REQ_STRING },
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync};
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/** Consider files matching the following patterns (base names only). */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic const char *g_pszFileFilter = NULL;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic PFNSCMREWRITER const g_aRewritersFor_Makefile_kup[] =
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync{
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rewrite_SvnNoExecutable,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rewrite_Makefile_kup
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync};
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic PFNSCMREWRITER const g_aRewritersFor_Makefile_kmk[] =
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync{
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rewrite_ForceNativeEol,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rewrite_StripTrailingBlanks,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rewrite_AdjustTrailingLines,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rewrite_SvnNoExecutable,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rewrite_SvnKeywords,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rewrite_Makefile_kmk
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync};
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic PFNSCMREWRITER const g_aRewritersFor_C_and_CPP[] =
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync{
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rewrite_ForceNativeEol,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rewrite_ExpandTabs,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rewrite_StripTrailingBlanks,
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync rewrite_AdjustTrailingLines,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rewrite_SvnNoExecutable,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rewrite_SvnKeywords,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rewrite_C_and_CPP
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync};
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic PFNSCMREWRITER const g_aRewritersFor_H_and_HPP[] =
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync{
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rewrite_ForceNativeEol,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rewrite_ExpandTabs,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rewrite_StripTrailingBlanks,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rewrite_AdjustTrailingLines,
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync rewrite_SvnNoExecutable,
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync rewrite_C_and_CPP
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync};
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsyncstatic PFNSCMREWRITER const g_aRewritersFor_RC[] =
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync{
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync rewrite_ForceNativeEol,
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync rewrite_ExpandTabs,
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync rewrite_StripTrailingBlanks,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rewrite_AdjustTrailingLines,
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync rewrite_SvnNoExecutable,
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync rewrite_SvnKeywords
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync};
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic PFNSCMREWRITER const g_aRewritersFor_ShellScripts[] =
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync{
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync rewrite_ForceLF,
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync rewrite_ExpandTabs,
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync rewrite_StripTrailingBlanks
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync};
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsyncstatic PFNSCMREWRITER const g_aRewritersFor_BatchFiles[] =
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync{
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync rewrite_ForceCRLF,
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync rewrite_ExpandTabs,
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync rewrite_StripTrailingBlanks
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync};
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsyncstatic SCMCFGENTRY const g_aConfigs[] =
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync{
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync { RT_ELEMENTS(g_aRewritersFor_Makefile_kup), &g_aRewritersFor_Makefile_kup[0], "Makefile.kup" },
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { RT_ELEMENTS(g_aRewritersFor_Makefile_kmk), &g_aRewritersFor_Makefile_kmk[0], "Makefile.kmk|Config.kmk" },
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync { RT_ELEMENTS(g_aRewritersFor_C_and_CPP), &g_aRewritersFor_C_and_CPP[0], "*.c|*.cpp|*.C|*.CPP|*.cxx|*.cc" },
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync { RT_ELEMENTS(g_aRewritersFor_H_and_HPP), &g_aRewritersFor_H_and_HPP[0], "*.h|*.hpp" },
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync { RT_ELEMENTS(g_aRewritersFor_RC), &g_aRewritersFor_RC[0], "*.rc" },
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync { RT_ELEMENTS(g_aRewritersFor_ShellScripts), &g_aRewritersFor_ShellScripts[0], "*.sh|configure" },
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync { RT_ELEMENTS(g_aRewritersFor_BatchFiles), &g_aRewritersFor_BatchFiles[0], "*.bat|*.cmd|*.btm|*.vbs|*.ps1" },
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync};
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/* -=-=-=-=-=- settings -=-=-=-=-=- */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/**
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Init a settings structure with settings from @a pSrc.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync *
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @returns IPRT status code
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pSettings The settings.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pSrc The source settings.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic int scmSettingsBaseInitAndCopy(PSCMSETTINGSBASE pSettings, PCSCMSETTINGSBASE pSrc)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync{
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync *pSettings = *pSrc;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync int rc = RTStrDupEx(&pSettings->pszFilterFiles, pSrc->pszFilterFiles);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if (RT_SUCCESS(rc))
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync {
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rc = RTStrDupEx(&pSettings->pszFilterOutFiles, pSrc->pszFilterOutFiles);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if (RT_SUCCESS(rc))
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync {
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rc = RTStrDupEx(&pSettings->pszFilterOutDirs, pSrc->pszFilterOutDirs);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if (RT_SUCCESS(rc))
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return VINF_SUCCESS;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync RTStrFree(pSettings->pszFilterOutFiles);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync }
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync RTStrFree(pSettings->pszFilterFiles);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync }
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->pszFilterFiles = NULL;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->pszFilterOutFiles = NULL;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->pszFilterOutDirs = NULL;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return rc;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync}
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/**
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Init a settings structure.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync *
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @returns IPRT status code
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pSettings The settings.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic int scmSettingsBaseInit(PSCMSETTINGSBASE pSettings)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync{
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return scmSettingsBaseInitAndCopy(pSettings, &g_Defaults);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync}
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/**
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Deletes the settings, i.e. free any dynamically allocated content.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync *
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pSettings The settings.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic void scmSettingsBaseDelete(PSCMSETTINGSBASE pSettings)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync{
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if (pSettings)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync {
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync Assert(pSettings->cchTab != ~(unsigned)0);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->cchTab = ~(unsigned)0;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync RTStrFree(pSettings->pszFilterFiles);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->pszFilterFiles = NULL;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync RTStrFree(pSettings->pszFilterOutFiles);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->pszFilterOutFiles = NULL;
67c73271505adae0686ddbc74eaeef778ef16792vboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync RTStrFree(pSettings->pszFilterOutDirs);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->pszFilterOutDirs = NULL;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync }
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync}
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/**
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Processes a RTGetOpt result.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync *
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @retval VINF_SUCCESS if handled.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @retval VERR_OUT_OF_RANGE if the option value was out of range.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @retval VERR_GETOPT_UNKNOWN_OPTION if the option was not recognized.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync *
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pSettings The settings to change.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param rc The RTGetOpt return value.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pValueUnion The RTGetOpt value union.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic int scmSettingsBaseHandleOpt(PSCMSETTINGSBASE pSettings, int rc, PRTGETOPTUNION pValueUnion)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync{
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync switch (rc)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync {
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_CONVERT_EOL:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->fConvertEol = true;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return VINF_SUCCESS;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_NO_CONVERT_EOL:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->fConvertEol = false;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return VINF_SUCCESS;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_CONVERT_TABS:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->fConvertTabs = true;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return VINF_SUCCESS;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_NO_CONVERT_TABS:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->fConvertTabs = false;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return VINF_SUCCESS;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_FORCE_FINAL_EOL:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->fForceFinalEol = true;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return VINF_SUCCESS;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_NO_FORCE_FINAL_EOL:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->fForceFinalEol = false;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return VINF_SUCCESS;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_FORCE_TRAILING_LINE:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->fForceTrailingLine = true;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return VINF_SUCCESS;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_NO_FORCE_TRAILING_LINE:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->fForceTrailingLine = false;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return VINF_SUCCESS;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_STRIP_TRAILING_BLANKS:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->fStripTrailingBlanks = true;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return VINF_SUCCESS;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_NO_STRIP_TRAILING_BLANKS:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->fStripTrailingBlanks = false;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return VINF_SUCCESS;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_STRIP_TRAILING_LINES:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->fStripTrailingLines = true;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return VINF_SUCCESS;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_NO_STRIP_TRAILING_LINES:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->fStripTrailingLines = false;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return VINF_SUCCESS;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_ONLY_SVN_DIRS:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->fOnlySvnDirs = true;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return VINF_SUCCESS;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_NOT_ONLY_SVN_DIRS:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->fOnlySvnDirs = false;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return VINF_SUCCESS;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_ONLY_SVN_FILES:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->fOnlySvnFiles = true;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return VINF_SUCCESS;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_NOT_ONLY_SVN_FILES:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->fOnlySvnFiles = false;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return VINF_SUCCESS;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_SET_SVN_EOL:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->fSetSvnEol = true;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return VINF_SUCCESS;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_DONT_SET_SVN_EOL:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->fSetSvnEol = false;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return VINF_SUCCESS;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_SET_SVN_EXECUTABLE:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->fSetSvnExecutable = true;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return VINF_SUCCESS;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_DONT_SET_SVN_EXECUTABLE:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->fSetSvnExecutable = false;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return VINF_SUCCESS;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_SET_SVN_KEYWORDS:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->fSetSvnKeywords = true;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return VINF_SUCCESS;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_DONT_SET_SVN_KEYWORDS:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->fSetSvnKeywords = false;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return VINF_SUCCESS;
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync case SCMOPT_TAB_SIZE:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if ( pValueUnion->u8 < 1
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync || pValueUnion->u8 >= RT_ELEMENTS(g_szTabSpaces))
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync {
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync RTMsgError("Invalid tab size: %u - must be in {1..%u}\n",
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pValueUnion->u8, RT_ELEMENTS(g_szTabSpaces) - 1);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return VERR_OUT_OF_RANGE;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync }
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pSettings->cchTab = pValueUnion->u8;
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync return VINF_SUCCESS;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_FILTER_OUT_DIRS:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_FILTER_FILES:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_FILTER_OUT_FILES:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync {
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync char **ppsz = NULL;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync switch (rc)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync {
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_FILTER_OUT_DIRS: ppsz = &pSettings->pszFilterOutDirs; break;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_FILTER_FILES: ppsz = &pSettings->pszFilterFiles; break;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync case SCMOPT_FILTER_OUT_FILES: ppsz = &pSettings->pszFilterOutFiles; break;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync }
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /*
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * An empty string zaps the current list.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if (!*pValueUnion->psz)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return RTStrATruncate(ppsz, 0);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync /*
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Non-empty strings are appended to the pattern list.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync *
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Strip leading and trailing pattern separators before attempting
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * to append it. If it's just separators, don't do anything.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync const char *pszSrc = pValueUnion->psz;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync while (*pszSrc == '|')
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pszSrc++;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync size_t cchSrc = strlen(pszSrc);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync while (cchSrc > 0 && pszSrc[cchSrc - 1] == '|')
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync cchSrc--;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if (!cchSrc)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return VINF_SUCCESS;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return RTStrAAppendExN(ppsz, 2,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync "|", *ppsz && **ppsz ? 1 : 0,
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync pszSrc, cchSrc);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync }
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync default:
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return VERR_GETOPT_UNKNOWN_OPTION;
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync }
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync}
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/**
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Parses an option string.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync *
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @returns IPRT status code.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pBase The base settings structure to apply the options
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * to.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pszOptions The options to parse.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic int scmSettingsBaseParseString(PSCMSETTINGSBASE pBase, const char *pszLine)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync{
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync int cArgs;
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync char **papszArgs;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync int rc = RTGetOptArgvFromString(&papszArgs, &cArgs, pszLine, NULL);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if (RT_SUCCESS(rc))
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync {
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync RTGETOPTUNION ValueUnion;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync RTGETOPTSTATE GetOptState;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rc = RTGetOptInit(&GetOptState, cArgs, papszArgs, &g_aScmOpts[0], RT_ELEMENTS(g_aScmOpts), 0, 0 /*fFlags*/);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if (RT_SUCCESS(rc))
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync {
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync while ((rc = RTGetOpt(&GetOptState, &ValueUnion)) != 0)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync {
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync rc = scmSettingsBaseHandleOpt(pBase, rc, &ValueUnion);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if (RT_FAILURE(rc))
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync break;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync }
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync }
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync RTGetOptArgvFree(papszArgs);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync }
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return rc;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync}
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync/**
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * Parses an unterminated option string.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync *
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @returns IPRT status code.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pBase The base settings structure to apply the options
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * to.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param pchLine The line.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync * @param cchLine The line length.
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync */
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsyncstatic int scmSettingsBaseParseStringN(PSCMSETTINGSBASE pBase, const char *pchLine, size_t cchLine)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync{
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync char *pszLine = RTStrDupN(pchLine, cchLine);
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync if (!pszLine)
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync return VERR_NO_MEMORY;
ae41886ba231ade1b868dd32ff24ee49813ebbabvboxsync int rc = scmSettingsBaseParseString(pBase, pszLine);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync RTStrFree(pszLine);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync return rc;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync}
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync/**
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * Verifies the options string.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync *
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * @returns IPRT status code.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * @param pszOptions The options to verify .
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync */
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsyncstatic int scmSettingsBaseVerifyString(const char *pszOptions)
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync{
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync SCMSETTINGSBASE Base;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync int rc = scmSettingsBaseInit(&Base);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync if (RT_SUCCESS(rc))
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync {
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync rc = scmSettingsBaseParseString(&Base, pszOptions);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync scmSettingsBaseDelete(&Base);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync }
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync return rc;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync}
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync/**
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * Loads settings found in editor and SCM settings directives within the
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * document (@a pStream).
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync *
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * @returns IPRT status code.
b377a71756cce9f39d25b561f72bfa33ce8a39devboxsync * @param pBase The settings base to load settings into.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * @param pStream The stream to scan for settings directives.
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync */
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsyncstatic int scmSettingsBaseLoadFromDocument(PSCMSETTINGSBASE pBase, PSCMSTREAM pStream)
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync{
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync /** @todo Editor and SCM settings directives in documents. */
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync return VINF_SUCCESS;
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync}
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync/**
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * Creates a new settings file struct, cloning @a pSettings.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync *
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * @returns IPRT status code.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * @param ppSettings Where to return the new struct.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * @param pSettingsBase The settings to inherit from.
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync */
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsyncstatic int scmSettingsCreate(PSCMSETTINGS *ppSettings, PCSCMSETTINGSBASE pSettingsBase)
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync{
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync PSCMSETTINGS pSettings = (PSCMSETTINGS)RTMemAlloc(sizeof(*pSettings));
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync if (!pSettings)
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync return VERR_NO_MEMORY;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync int rc = scmSettingsBaseInitAndCopy(&pSettings->Base, pSettingsBase);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync if (RT_SUCCESS(rc))
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync {
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync pSettings->pDown = NULL;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync pSettings->pUp = NULL;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync pSettings->paPairs = NULL;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync pSettings->cPairs = 0;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync *ppSettings = pSettings;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync return VINF_SUCCESS;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync }
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync RTMemFree(pSettings);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync return rc;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync}
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync/**
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * Destroys a settings structure.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync *
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * @param pSettings The settings structure to destroy. NULL is OK.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync */
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsyncstatic void scmSettingsDestroy(PSCMSETTINGS pSettings)
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync{
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync if (pSettings)
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync {
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync scmSettingsBaseDelete(&pSettings->Base);
b377a71756cce9f39d25b561f72bfa33ce8a39devboxsync for (size_t i = 0; i < pSettings->cPairs; i++)
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync {
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync RTStrFree(pSettings->paPairs[i].pszPattern);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync RTStrFree(pSettings->paPairs[i].pszOptions);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync pSettings->paPairs[i].pszPattern = NULL;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync pSettings->paPairs[i].pszOptions = NULL;
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync }
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync RTMemFree(pSettings->paPairs);
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync pSettings->paPairs = NULL;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync RTMemFree(pSettings);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync }
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync}
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync/**
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync * Adds a pattern/options pair to the settings structure.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync *
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync * @returns IPRT status code.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * @param pSettings The settings.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * @param pchLine The line containing the unparsed pair.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * @param cchLine The length of the line.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync */
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsyncstatic int scmSettingsAddPair(PSCMSETTINGS pSettings, const char *pchLine, size_t cchLine)
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync{
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync /*
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * Split the string.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync */
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync const char *pchOptions = (const char *)memchr(pchLine, ':', cchLine);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync if (!pchOptions)
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync return VERR_INVALID_PARAMETER;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync size_t cchPattern = pchOptions - pchLine;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync size_t cchOptions = cchLine - cchPattern - 1;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync pchOptions++;
b377a71756cce9f39d25b561f72bfa33ce8a39devboxsync
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync /* strip spaces everywhere */
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync while (cchPattern > 0 && RT_C_IS_SPACE(pchLine[cchPattern - 1]))
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync cchPattern--;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync while (cchPattern > 0 && RT_C_IS_SPACE(*pchLine))
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync cchPattern--, pchLine++;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync while (cchOptions > 0 && RT_C_IS_SPACE(pchOptions[cchOptions - 1]))
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync cchOptions--;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync while (cchOptions > 0 && RT_C_IS_SPACE(*pchOptions))
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync cchOptions--, pchOptions++;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync /* Quietly ignore empty patterns and empty options. */
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync if (!cchOptions || !cchPattern)
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync return VINF_SUCCESS;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync /*
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * Add the pair and verify the option string.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync */
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync uint32_t iPair = pSettings->cPairs;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync if ((iPair % 32) == 0)
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync {
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync void *pvNew = RTMemRealloc(pSettings->paPairs, (iPair + 32) * sizeof(pSettings->paPairs[0]));
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync if (!pvNew)
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync return VERR_NO_MEMORY;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync pSettings->paPairs = (PSCMPATRNOPTPAIR)pvNew;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync }
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync pSettings->paPairs[iPair].pszPattern = RTStrDupN(pchLine, cchPattern);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync pSettings->paPairs[iPair].pszOptions = RTStrDupN(pchOptions, cchOptions);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync int rc;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync if ( pSettings->paPairs[iPair].pszPattern
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync && pSettings->paPairs[iPair].pszOptions)
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync rc = scmSettingsBaseVerifyString(pSettings->paPairs[iPair].pszOptions);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync else
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync rc = VERR_NO_MEMORY;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync if (RT_SUCCESS(rc))
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync pSettings->cPairs = iPair + 1;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync else
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync {
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync RTStrFree(pSettings->paPairs[iPair].pszPattern);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync RTStrFree(pSettings->paPairs[iPair].pszOptions);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync }
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync return rc;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync}
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync/**
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * Loads in the settings from @a pszFilename.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync *
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * @returns IPRT status code.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * @param pSettings Where to load the settings file.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * @param pszFilename The file to load.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync */
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsyncstatic int scmSettingsLoadFile(PSCMSETTINGS pSettings, const char *pszFilename)
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync{
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync SCMSTREAM Stream;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync int rc = ScmStreamInitForReading(&Stream, pszFilename);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync if (RT_FAILURE(rc))
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync {
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync RTMsgError("%s: ScmStreamInitForReading -> %Rrc\n", pszFilename, rc);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync return rc;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync }
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync SCMEOL enmEol;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync const char *pchLine;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync size_t cchLine;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync while ((pchLine = ScmStreamGetLine(&Stream, &cchLine, &enmEol)) != NULL)
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync {
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync /* Ignore leading spaces. */
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync while (cchLine > 0 && RT_C_IS_SPACE(*pchLine))
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync pchLine++, cchLine--;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync /* Ignore empty lines and comment lines. */
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync if (cchLine < 1 || *pchLine == '#')
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync continue;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync /* What kind of line is it? */
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync const char *pchColon = (const char *)memchr(pchLine, ':', cchLine);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync if (pchColon)
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync rc = scmSettingsAddPair(pSettings, pchLine, cchLine);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync else
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync rc = scmSettingsBaseParseStringN(&pSettings->Base, pchLine, cchLine);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync if (RT_FAILURE(rc))
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync {
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync RTMsgError("%s:%d: %Rrc\n", pszFilename, ScmStreamTellLine(&Stream), rc);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync break;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync }
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync }
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync if (RT_SUCCESS(rc))
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync {
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync rc = ScmStreamGetStatus(&Stream);
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync if (RT_FAILURE(rc))
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync RTMsgError("%s: ScmStreamGetStatus- > %Rrc\n", pszFilename, rc);
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync }
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync ScmStreamDelete(&Stream);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync return rc;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync}
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync/**
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * Parse the specified settings file creating a new settings struct from it.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync *
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync * @returns IPRT status code
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * @param ppSettings Where to return the new settings.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * @param pszFilename The file to parse.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * @param pSettingsBase The base settings we inherit from.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync */
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsyncstatic int scmSettingsCreateFromFile(PSCMSETTINGS *ppSettings, const char *pszFilename, PCSCMSETTINGSBASE pSettingsBase)
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync{
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync PSCMSETTINGS pSettings;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync int rc = scmSettingsCreate(&pSettings, pSettingsBase);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync if (RT_SUCCESS(rc))
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync {
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync rc = scmSettingsLoadFile(pSettings, pszFilename);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync if (RT_SUCCESS(rc))
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync {
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync *ppSettings = pSettings;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync return VINF_SUCCESS;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync }
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync scmSettingsDestroy(pSettings);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync }
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync *ppSettings = NULL;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync return rc;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync}
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync/**
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * Create an initial settings structure when starting processing a new file or
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * directory.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync *
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * This will look for .scm-settings files from the root and down to the
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * specified directory, combining them into the returned settings structure.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync *
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * @returns IPRT status code.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * @param ppSettings Where to return the pointer to the top stack
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * object.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * @param pBaseSettings The base settings we inherit from (globals
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * typically).
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * @param pszPath The absolute path to the new directory or file.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync */
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsyncstatic int scmSettingsCreateForPath(PSCMSETTINGS *ppSettings, PCSCMSETTINGSBASE pBaseSettings, const char *pszPath)
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync{
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync *ppSettings = NULL; /* try shut up gcc. */
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync /*
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync * We'll be working with a stack copy of the path.
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync */
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync char szFile[RTPATH_MAX];
4aafb1203580a5c145a7bdae57ebf69a36fa4f01vboxsync size_t cchDir = strlen(pszPath);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync if (cchDir >= sizeof(szFile) - sizeof(SCM_SETTINGS_FILENAME))
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync return VERR_FILENAME_TOO_LONG;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync /*
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * Create the bottom-most settings.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync */
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync PSCMSETTINGS pSettings;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync int rc = scmSettingsCreate(&pSettings, pBaseSettings);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync if (RT_FAILURE(rc))
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync return rc;
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync /*
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * Enumerate the path components from the root and down. Load any setting
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync * files we find.
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync */
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync size_t cComponents = RTPathCountComponents(pszPath);
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync for (size_t i = 1; i <= cComponents; i++)
bc0c1e33e433d1276ea1606ace81f61594ee3838vboxsync {
rc = RTPathCopyComponents(szFile, sizeof(szFile), pszPath, i);
if (RT_SUCCESS(rc))
rc = RTPathAppend(szFile, sizeof(szFile), SCM_SETTINGS_FILENAME);
if (RT_FAILURE(rc))
break;
if (RTFileExists(szFile))
{
rc = scmSettingsLoadFile(pSettings, szFile);
if (RT_FAILURE(rc))
break;
}
}
if (RT_SUCCESS(rc))
*ppSettings = pSettings;
else
scmSettingsDestroy(pSettings);
return rc;
}
/**
* Pushes a new settings set onto the stack.
*
* @param ppSettingsStack The pointer to the pointer to the top stack
* element. This will be used as input and output.
* @param pSettings The settings to push onto the stack.
*/
static void scmSettingsStackPush(PSCMSETTINGS *ppSettingsStack, PSCMSETTINGS pSettings)
{
PSCMSETTINGS pOld = *ppSettingsStack;
pSettings->pDown = pOld;
pSettings->pUp = NULL;
if (pOld)
pOld->pUp = pSettings;
*ppSettingsStack = pSettings;
}
/**
* Pushes the settings of the specified directory onto the stack.
*
* We will load any .scm-settings in the directory. A stack entry is added even
* if no settings file was found.
*
* @returns IPRT status code.
* @param ppSettingsStack The pointer to the pointer to the top stack
* element. This will be used as input and output.
* @param pszDir The directory to do this for.
*/
static int scmSettingsStackPushDir(PSCMSETTINGS *ppSettingsStack, const char *pszDir)
{
char szFile[RTPATH_MAX];
int rc = RTPathJoin(szFile, sizeof(szFile), pszDir, SCM_SETTINGS_FILENAME);
if (RT_SUCCESS(rc))
{
PSCMSETTINGS pSettings;
rc = scmSettingsCreate(&pSettings, &(*ppSettingsStack)->Base);
if (RT_SUCCESS(rc))
{
if (RTFileExists(szFile))
rc = scmSettingsLoadFile(pSettings, szFile);
if (RT_SUCCESS(rc))
{
scmSettingsStackPush(ppSettingsStack, pSettings);
return VINF_SUCCESS;
}
scmSettingsDestroy(pSettings);
}
}
return rc;
}
/**
* Pops a settings set off the stack.
*
* @returns The popped setttings.
* @param ppSettingsStack The pointer to the pointer to the top stack
* element. This will be used as input and output.
*/
static PSCMSETTINGS scmSettingsStackPop(PSCMSETTINGS *ppSettingsStack)
{
PSCMSETTINGS pRet = *ppSettingsStack;
PSCMSETTINGS pNew = pRet ? pRet->pDown : NULL;
*ppSettingsStack = pNew;
if (pNew)
pNew->pUp = NULL;
if (pRet)
{
pRet->pUp = NULL;
pRet->pDown = NULL;
}
return pRet;
}
/**
* Pops and destroys the top entry of the stack.
*
* @param ppSettingsStack The pointer to the pointer to the top stack
* element. This will be used as input and output.
*/
static void scmSettingsStackPopAndDestroy(PSCMSETTINGS *ppSettingsStack)
{
scmSettingsDestroy(scmSettingsStackPop(ppSettingsStack));
}
/**
* Constructs the base settings for the specified file name.
*
* @returns IPRT status code.
* @param pSettingsStack The top element on the settings stack.
* @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.)
* @param pBase Base settings to initialize.
*/
static int scmSettingsStackMakeFileBase(PCSCMSETTINGS pSettingsStack, const char *pszFilename,
const char *pszBasename, size_t cchBasename, PSCMSETTINGSBASE pBase)
{
int rc = scmSettingsBaseInitAndCopy(pBase, &pSettingsStack->Base);
if (RT_SUCCESS(rc))
{
/* find the bottom entry in the stack. */
PCSCMSETTINGS pCur = pSettingsStack;
while (pCur->pDown)
pCur = pCur->pDown;
/* Work our way up thru the stack and look for matching pairs. */
while (pCur)
{
size_t const cPairs = pCur->cPairs;
if (cPairs)
{
for (size_t i = 0; i < cPairs; i++)
if ( RTStrSimplePatternMultiMatch(pCur->paPairs[i].pszPattern, RTSTR_MAX,
pszBasename, cchBasename, NULL)
|| RTStrSimplePatternMultiMatch(pCur->paPairs[i].pszPattern, RTSTR_MAX,
pszFilename, RTSTR_MAX, NULL))
{
rc = scmSettingsBaseParseString(pBase, pCur->paPairs[i].pszOptions);
if (RT_FAILURE(rc))
break;
}
if (RT_FAILURE(rc))
break;
}
/* advance */
pCur = pCur->pUp;
}
}
if (RT_FAILURE(rc))
scmSettingsBaseDelete(pBase);
return rc;
}
/* -=-=-=-=-=- misc -=-=-=-=-=- */
/**
* Prints a verbose message if the level is high enough.
*
* @param pState The rewrite state. Optional.
* @param iLevel The required verbosity level.
* @param pszFormat The message format string. Can be NULL if we
* only want to trigger the per file message.
* @param ... Format arguments.
*/
static void ScmVerbose(PSCMRWSTATE pState, int iLevel, const char *pszFormat, ...)
{
if (iLevel <= g_iVerbosity)
{
if (pState && !pState->fFirst)
{
RTPrintf("%s: info: --= Rewriting '%s' =--\n", g_szProgName, pState->pszFilename);
pState->fFirst = true;
}
if (pszFormat)
{
RTPrintf(pState
? "%s: info: "
: "%s: info: ",
g_szProgName);
va_list va;
va_start(va, pszFormat);
RTPrintfV(pszFormat, va);
va_end(va);
}
}
}
/* -=-=-=-=-=- subversion -=-=-=-=-=- */
#define SCM_WITHOUT_LIBSVN
#ifdef SCM_WITHOUT_LIBSVN
/**
* Callback that is call for each path to search.
*/
static DECLCALLBACK(int) scmSvnFindSvnBinaryCallback(char const *pchPath, size_t cchPath, void *pvUser1, void *pvUser2)
{
char *pszDst = (char *)pvUser1;
size_t cchDst = (size_t)pvUser2;
if (cchDst > cchPath)
{
memcpy(pszDst, pchPath, cchPath);
pszDst[cchPath] = '\0';
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
int rc = RTPathAppend(pszDst, cchDst, "svn.exe");
#else
int rc = RTPathAppend(pszDst, cchDst, "svn");
#endif
if ( RT_SUCCESS(rc)
&& RTFileExists(pszDst))
return VINF_SUCCESS;
}
return VERR_TRY_AGAIN;
}
/**
* Finds the svn binary.
*
* @param pszPath Where to store it. Worst case, we'll return
* "svn" here.
* @param cchPath The size of the buffer pointed to by @a pszPath.
*/
static void scmSvnFindSvnBinary(char *pszPath, size_t cchPath)
{
/** @todo code page fun... */
Assert(cchPath >= sizeof("svn"));
#ifdef RT_OS_WINDOWS
const char *pszEnvVar = RTEnvGet("Path");
#else
const char *pszEnvVar = RTEnvGet("PATH");
#endif
if (pszPath)
{
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
int rc = RTPathTraverseList(pszEnvVar, ';', scmSvnFindSvnBinaryCallback, pszPath, (void *)cchPath);
#else
int rc = RTPathTraverseList(pszEnvVar, ':', scmSvnFindSvnBinaryCallback, pszPath, (void *)cchPath);
#endif
if (RT_SUCCESS(rc))
return;
}
strcpy(pszPath, "svn");
}
/**
* Construct a dot svn filename for the file being rewritten.
*
* @returns IPRT status code.
* @param pState The rewrite state (for the name).
* @param pszDir The directory, including ".svn/".
* @param pszSuff The filename suffix.
* @param pszDst The output buffer. RTPATH_MAX in size.
*/
static int scmSvnConstructName(PSCMRWSTATE pState, const char *pszDir, const char *pszSuff, char *pszDst)
{
strcpy(pszDst, pState->pszFilename); /* ASSUMES sizeof(szBuf) <= sizeof(szPath) */
RTPathStripFilename(pszDst);
int rc = RTPathAppend(pszDst, RTPATH_MAX, pszDir);
if (RT_SUCCESS(rc))
{
rc = RTPathAppend(pszDst, RTPATH_MAX, RTPathFilename(pState->pszFilename));
if (RT_SUCCESS(rc))
{
size_t cchDst = strlen(pszDst);
size_t cchSuff = strlen(pszSuff);
if (cchDst + cchSuff < RTPATH_MAX)
{
memcpy(&pszDst[cchDst], pszSuff, cchSuff + 1);
return VINF_SUCCESS;
}
else
rc = VERR_BUFFER_OVERFLOW;
}
}
return rc;
}
/**
* Interprets the specified string as decimal numbers.
*
* @returns true if parsed successfully, false if not.
* @param pch The string (not terminated).
* @param cch The string length.
* @param pu Where to return the value.
*/
static bool scmSvnReadNumber(const char *pch, size_t cch, size_t *pu)
{
size_t u = 0;
while (cch-- > 0)
{
char ch = *pch++;
if (ch < '0' || ch > '9')
return false;
u *= 10;
u += ch - '0';
}
*pu = u;
return true;
}
#endif /* SCM_WITHOUT_LIBSVN */
/**
* Checks if the file we're operating on is part of a SVN working copy.
*
* @returns true if it is, false if it isn't or we cannot tell.
* @param pState The rewrite state to work on.
*/
static bool scmSvnIsInWorkingCopy(PSCMRWSTATE pState)
{
#ifdef SCM_WITHOUT_LIBSVN
/*
* Hack: check if the .svn/text-base/<file>.svn-base file exists.
*/
char szPath[RTPATH_MAX];
int rc = scmSvnConstructName(pState, ".svn/text-base/", ".svn-base", szPath);
if (RT_SUCCESS(rc))
return RTFileExists(szPath);
#else
NOREF(pState);
#endif
return false;
}
/**
* Queries the value of an SVN property.
*
* This will automatically adjust for scheduled changes.
*
* @returns IPRT status code.
* @retval VERR_INVALID_STATE if not a SVN WC file.
* @retval VERR_NOT_FOUND if the property wasn't found.
* @param pState The rewrite state to work on.
* @param pszName The property name.
* @param ppszValue Where to return the property value. Free this
* using RTStrFree. Optional.
*/
static int scmSvnQueryProperty(PSCMRWSTATE pState, const char *pszName, char **ppszValue)
{
/*
* Look it up in the scheduled changes.
*/
uint32_t i = pState->cSvnPropChanges;
while (i-- > 0)
if (!strcmp(pState->paSvnPropChanges[i].pszName, pszName))
{
const char *pszValue = pState->paSvnPropChanges[i].pszValue;
if (!pszValue)
return VERR_NOT_FOUND;
if (ppszValue)
return RTStrDupEx(ppszValue, pszValue);
return VINF_SUCCESS;
}
#ifdef SCM_WITHOUT_LIBSVN
/*
* Hack: Read the .svn/props/<file>.svn-work file exists.
*/
char szPath[RTPATH_MAX];
int rc = scmSvnConstructName(pState, ".svn/props/", ".svn-work", szPath);
if (RT_SUCCESS(rc) && !RTFileExists(szPath))
rc = scmSvnConstructName(pState, ".svn/prop-base/", ".svn-base", szPath);
if (RT_SUCCESS(rc))
{
SCMSTREAM Stream;
rc = ScmStreamInitForReading(&Stream, szPath);
if (RT_SUCCESS(rc))
{
/*
* The current format is K len\n<name>\nV len\n<value>\n" ... END.
*/
rc = VERR_NOT_FOUND;
size_t const cchName = strlen(pszName);
SCMEOL enmEol;
size_t cchLine;
const char *pchLine;
while ((pchLine = ScmStreamGetLine(&Stream, &cchLine, &enmEol)) != NULL)
{
/*
* Parse the 'K num' / 'END' line.
*/
if ( cchLine == 3
&& !memcmp(pchLine, "END", 3))
break;
size_t cchKey;
if ( cchLine < 3
|| pchLine[0] != 'K'
|| pchLine[1] != ' '
|| !scmSvnReadNumber(&pchLine[2], cchLine - 2, &cchKey)
|| cchKey == 0
|| cchKey > 4096)
{
RTMsgError("%s:%u: Unexpected data '%.*s'\n", szPath, ScmStreamTellLine(&Stream), cchLine, pchLine);
rc = VERR_PARSE_ERROR;
break;
}
/*
* Match the key and skip to the value line. Don't bother with
* names containing EOL markers.
*/
size_t const offKey = ScmStreamTell(&Stream);
bool fMatch = cchName == cchKey;
if (fMatch)
{
pchLine = ScmStreamGetLine(&Stream, &cchLine, &enmEol);
if (!pchLine)
break;
fMatch = cchLine == cchName
&& !memcmp(pchLine, pszName, cchName);
}
if (RT_FAILURE(ScmStreamSeekAbsolute(&Stream, offKey + cchKey)))
break;
if (RT_FAILURE(ScmStreamSeekByLine(&Stream, ScmStreamTellLine(&Stream) + 1)))
break;
/*
* Read and Parse the 'V num' line.
*/
pchLine = ScmStreamGetLine(&Stream, &cchLine, &enmEol);
if (!pchLine)
break;
size_t cchValue;
if ( cchLine < 3
|| pchLine[0] != 'V'
|| pchLine[1] != ' '
|| !scmSvnReadNumber(&pchLine[2], cchLine - 2, &cchValue)
|| cchValue > _1M)
{
RTMsgError("%s:%u: Unexpected data '%.*s'\n", szPath, ScmStreamTellLine(&Stream), cchLine, pchLine);
rc = VERR_PARSE_ERROR;
break;
}
/*
* If we have a match, allocate a return buffer and read the
* value into it. Otherwise skip this value and continue
* searching.
*/
if (fMatch)
{
if (!ppszValue)
rc = VINF_SUCCESS;
else
{
char *pszValue;
rc = RTStrAllocEx(&pszValue, cchValue + 1);
if (RT_SUCCESS(rc))
{
rc = ScmStreamRead(&Stream, pszValue, cchValue);
if (RT_SUCCESS(rc))
*ppszValue = pszValue;
else
RTStrFree(pszValue);
}
}
break;
}
if (RT_FAILURE(ScmStreamSeekRelative(&Stream, cchValue)))
break;
if (RT_FAILURE(ScmStreamSeekByLine(&Stream, ScmStreamTellLine(&Stream) + 1)))
break;
}
if (RT_FAILURE(ScmStreamGetStatus(&Stream)))
{
rc = ScmStreamGetStatus(&Stream);
RTMsgError("%s: stream error %Rrc\n", szPath, rc);
}
ScmStreamDelete(&Stream);
}
}
if (rc == VERR_FILE_NOT_FOUND)
rc = VERR_NOT_FOUND;
return rc;
#else
NOREF(pState);
#endif
return VERR_NOT_FOUND;
}
/**
* Schedules the setting of a property.
*
* @returns IPRT status code.
* @retval VERR_INVALID_STATE if not a SVN WC file.
* @param pState The rewrite state to work on.
* @param pszName The name of the property to set.
* @param pszValue The value. NULL means deleting it.
*/
static int scmSvnSetProperty(PSCMRWSTATE pState, const char *pszName, const char *pszValue)
{
/*
* Update any existing entry first.
*/
size_t i = pState->cSvnPropChanges;
while (i-- > 0)
if (!strcmp(pState->paSvnPropChanges[i].pszName, pszName))
{
if (!pszValue)
{
RTStrFree(pState->paSvnPropChanges[i].pszValue);
pState->paSvnPropChanges[i].pszValue = NULL;
}
else
{
char *pszCopy;
int rc = RTStrDupEx(&pszCopy, pszValue);
if (RT_FAILURE(rc))
return rc;
pState->paSvnPropChanges[i].pszValue = pszCopy;
}
return VINF_SUCCESS;
}
/*
* Insert a new entry.
*/
i = pState->cSvnPropChanges;
if ((i % 32) == 0)
{
void *pvNew = RTMemRealloc(pState->paSvnPropChanges, (i + 32) * sizeof(SCMSVNPROP));
if (!pvNew)
return VERR_NO_MEMORY;
pState->paSvnPropChanges = (PSCMSVNPROP)pvNew;
}
pState->paSvnPropChanges[i].pszName = RTStrDup(pszName);
pState->paSvnPropChanges[i].pszValue = pszValue ? RTStrDup(pszValue) : NULL;
if ( pState->paSvnPropChanges[i].pszName
&& (pState->paSvnPropChanges[i].pszValue || !pszValue) )
pState->cSvnPropChanges = i + 1;
else
{
RTStrFree(pState->paSvnPropChanges[i].pszName);
pState->paSvnPropChanges[i].pszName = NULL;
RTStrFree(pState->paSvnPropChanges[i].pszValue);
pState->paSvnPropChanges[i].pszValue = NULL;
return VERR_NO_MEMORY;
}
return VINF_SUCCESS;
}
/**
* Schedules a property deletion.
*
* @returns IPRT status code.
* @param pState The rewrite state to work on.
* @param pszName The name of the property to delete.
*/
static int scmSvnDelProperty(PSCMRWSTATE pState, const char *pszName)
{
return scmSvnSetProperty(pState, pszName, NULL);
}
/**
* Applies any SVN property changes to the work copy of the file.
*
* @returns IPRT status code.
* @param pState The rewrite state which SVN property changes
* should be applied.
*/
static int scmSvnDisplayChanges(PSCMRWSTATE pState)
{
size_t i = pState->cSvnPropChanges;
while (i-- > 0)
{
const char *pszName = pState->paSvnPropChanges[i].pszName;
const char *pszValue = pState->paSvnPropChanges[i].pszValue;
if (pszValue)
ScmVerbose(pState, 0, "svn ps '%s' '%s' %s\n", pszName, pszValue, pState->pszFilename);
else
ScmVerbose(pState, 0, "svn pd '%s' %s\n", pszName, pszValue, pState->pszFilename);
}
return VINF_SUCCESS;
}
/**
* Applies any SVN property changes to the work copy of the file.
*
* @returns IPRT status code.
* @param pState The rewrite state which SVN property changes
* should be applied.
*/
static int scmSvnApplyChanges(PSCMRWSTATE pState)
{
#ifdef SCM_WITHOUT_LIBSVN
/*
* This sucks. We gotta find svn(.exe).
*/
static char s_szSvnPath[RTPATH_MAX];
if (s_szSvnPath[0] == '\0')
scmSvnFindSvnBinary(s_szSvnPath, sizeof(s_szSvnPath));
/*
* Iterate thru the changes and apply them by starting the svn client.
*/
for (size_t i = 0; i <pState->cSvnPropChanges; i++)
{
const char *apszArgv[6];
apszArgv[0] = s_szSvnPath;
apszArgv[1] = pState->paSvnPropChanges[i].pszValue ? "ps" : "pd";
apszArgv[2] = pState->paSvnPropChanges[i].pszName;
int iArg = 3;
if (pState->paSvnPropChanges[i].pszValue)
apszArgv[iArg++] = pState->paSvnPropChanges[i].pszValue;
apszArgv[iArg++] = pState->pszFilename;
apszArgv[iArg++] = NULL;
ScmVerbose(pState, 2, "executing: %s %s %s %s %s\n",
apszArgv[0], apszArgv[1], apszArgv[2], apszArgv[3], apszArgv[4]);
RTPROCESS pid;
int rc = RTProcCreate(s_szSvnPath, apszArgv, RTENV_DEFAULT, 0 /*fFlags*/, &pid);
if (RT_SUCCESS(rc))
{
RTPROCSTATUS Status;
rc = RTProcWait(pid, RTPROCWAIT_FLAGS_BLOCK, &Status);
if ( RT_SUCCESS(rc)
&& ( Status.enmReason != RTPROCEXITREASON_NORMAL
|| Status.iStatus != 0) )
{
RTMsgError("%s: %s %s %s %s %s -> %s %u\n",
pState->pszFilename, apszArgv[0], apszArgv[1], apszArgv[2], apszArgv[3], apszArgv[4],
Status.enmReason == RTPROCEXITREASON_NORMAL ? "exit code"
: Status.enmReason == RTPROCEXITREASON_SIGNAL ? "signal"
: Status.enmReason == RTPROCEXITREASON_ABEND ? "abnormal end"
: "abducted by alien",
Status.iStatus);
return VERR_GENERAL_FAILURE;
}
}
if (RT_FAILURE(rc))
{
RTMsgError("%s: error executing %s %s %s %s %s: %Rrc\n",
pState->pszFilename, apszArgv[0], apszArgv[1], apszArgv[2], apszArgv[3], apszArgv[4], rc);
return rc;
}
}
return VINF_SUCCESS;
#else
return VERR_NOT_IMPLEMENTED;
#endif
}
/* -=-=-=-=-=- rewriters -=-=-=-=-=- */
/**
* Strip trailing blanks (space & tab).
*
* @returns True if modified, false if not.
* @param pIn The input stream.
* @param pOut The output stream.
* @param pSettings The settings.
*/
static bool rewrite_StripTrailingBlanks(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
{
if (!pSettings->fStripTrailingBlanks)
return false;
bool fModified = false;
SCMEOL enmEol;
size_t cchLine;
const char *pchLine;
while ((pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol)) != NULL)
{
int rc;
if ( cchLine == 0
|| !RT_C_IS_BLANK(pchLine[cchLine - 1]) )
rc = ScmStreamPutLine(pOut, pchLine, cchLine, enmEol);
else
{
cchLine--;
while (cchLine > 0 && RT_C_IS_BLANK(pchLine[cchLine - 1]))
cchLine--;
rc = ScmStreamPutLine(pOut, pchLine, cchLine, enmEol);
fModified = true;
}
if (RT_FAILURE(rc))
return false;
}
if (fModified)
ScmVerbose(pState, 2, " * Stripped trailing blanks\n");
return fModified;
}
/**
* Expand tabs.
*
* @returns True if modified, false if not.
* @param pIn The input stream.
* @param pOut The output stream.
* @param pSettings The settings.
*/
static bool rewrite_ExpandTabs(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
{
if (!pSettings->fConvertTabs)
return false;
size_t const cchTab = pSettings->cchTab;
bool fModified = false;
SCMEOL enmEol;
size_t cchLine;
const char *pchLine;
while ((pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol)) != NULL)
{
int rc;
const char *pchTab = (const char *)memchr(pchLine, '\t', cchLine);
if (!pchTab)
rc = ScmStreamPutLine(pOut, pchLine, cchLine, enmEol);
else
{
size_t offTab = 0;
const char *pchChunk = pchLine;
for (;;)
{
size_t cchChunk = pchTab - pchChunk;
offTab += cchChunk;
ScmStreamWrite(pOut, pchChunk, cchChunk);
size_t cchToTab = cchTab - offTab % cchTab;
ScmStreamWrite(pOut, g_szTabSpaces, cchToTab);
offTab += cchToTab;
pchChunk = pchTab + 1;
size_t cchLeft = cchLine - (pchChunk - pchLine);
pchTab = (const char *)memchr(pchChunk, '\t', cchLeft);
if (!pchTab)
{
rc = ScmStreamPutLine(pOut, pchChunk, cchLeft, enmEol);
break;
}
}
fModified = true;
}
if (RT_FAILURE(rc))
return false;
}
if (fModified)
ScmVerbose(pState, 2, " * Expanded tabs\n");
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 pSettings The settings.
* @param enmDesiredEol The desired end of line indicator type.
* @param pszDesiredSvnEol The desired svn:eol-style.
*/
static bool rewrite_ForceEol(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings,
SCMEOL enmDesiredEol, const char *pszDesiredSvnEol)
{
if (!pSettings->fConvertEol)
return false;
bool fModified = false;
SCMEOL enmEol;
size_t cchLine;
const char *pchLine;
while ((pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol)) != NULL)
{
if ( enmEol != enmDesiredEol
&& enmEol != SCMEOL_NONE)
{
fModified = true;
enmEol = enmDesiredEol;
}
int rc = ScmStreamPutLine(pOut, pchLine, cchLine, enmEol);
if (RT_FAILURE(rc))
return false;
}
if (fModified)
ScmVerbose(pState, 2, " * Converted EOL markers\n");
/* Check svn:eol-style if appropriate */
if ( pSettings->fSetSvnEol
&& scmSvnIsInWorkingCopy(pState))
{
char *pszEol;
int rc = scmSvnQueryProperty(pState, "svn:eol-style", &pszEol);
if ( (RT_SUCCESS(rc) && strcmp(pszEol, pszDesiredSvnEol))
|| rc == VERR_NOT_FOUND)
{
if (rc == VERR_NOT_FOUND)
ScmVerbose(pState, 2, " * Setting svn:eol-style to %s (missing)\n", pszDesiredSvnEol);
else
ScmVerbose(pState, 2, " * Setting svn:eol-style to %s (was: %s)\n", pszDesiredSvnEol, pszEol);
int rc2 = scmSvnSetProperty(pState, "svn:eol-style", pszDesiredSvnEol);
if (RT_FAILURE(rc2))
RTMsgError("scmSvnSetProperty: %Rrc\n", rc2); /** @todo propagate the error somehow... */
}
if (RT_SUCCESS(rc))
RTStrFree(pszEol);
}
/** @todo also check the subversion svn:eol-style state! */
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.
* @param pSettings The settings.
*/
static bool rewrite_ForceNativeEol(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
{
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
return rewrite_ForceEol(pState, pIn, pOut, pSettings, SCMEOL_CRLF, "native");
#else
return rewrite_ForceEol(pState, pIn, pOut, pSettings, SCMEOL_LF, "native");
#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.
* @param pSettings The settings.
*/
static bool rewrite_ForceLF(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
{
return rewrite_ForceEol(pState, pIn, pOut, pSettings, SCMEOL_LF, "LF");
}
/**
* 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.
* @param pSettings The settings.
*/
static bool rewrite_ForceCRLF(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
{
return rewrite_ForceEol(pState, pIn, pOut, pSettings, SCMEOL_CRLF, "CRLF");
}
/**
* Strip trailing blank lines and/or make sure there is exactly one blank line
* 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.
* @param pSettings The settings.
*
* @remarks ASSUMES trailing white space has been removed already.
*/
static bool rewrite_AdjustTrailingLines(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
{
if ( !pSettings->fStripTrailingLines
&& !pSettings->fForceTrailingLine
&& !pSettings->fForceFinalEol)
return false;
size_t const cLines = ScmStreamCountLines(pIn);
/* Empty files remains empty. */
if (cLines <= 1)
return false;
/* Figure out if we need to adjust the number of lines or not. */
size_t cLinesNew = cLines;
if ( pSettings->fStripTrailingLines
&& ScmStreamIsWhiteLine(pIn, cLinesNew - 1))
{
while ( cLinesNew > 1
&& ScmStreamIsWhiteLine(pIn, cLinesNew - 2))
cLinesNew--;
}
if ( pSettings->fForceTrailingLine
&& !ScmStreamIsWhiteLine(pIn, cLinesNew - 1))
cLinesNew++;
bool fFixMissingEol = pSettings->fForceFinalEol
&& ScmStreamGetEolByLine(pIn, cLinesNew - 1) == SCMEOL_NONE;
if ( !fFixMissingEol
&& cLines == cLinesNew)
return false;
/* Copy the number of lines we've arrived at. */
ScmStreamRewindForReading(pIn);
size_t cCopied = RT_MIN(cLinesNew, cLines);
ScmStreamCopyLines(pOut, pIn, cCopied);
if (cCopied != cLinesNew)
{
while (cCopied++ < cLinesNew)
ScmStreamPutLine(pOut, "", 0, ScmStreamGetEol(pIn));
}
/* Fix missing EOL if required. */
else if (fFixMissingEol)
{
if (ScmStreamGetEol(pIn) == SCMEOL_LF)
ScmStreamWrite(pOut, "\n", 1);
else
ScmStreamWrite(pOut, "\r\n", 2);
}
ScmVerbose(pState, 2, " * Adjusted trailing blank lines\n");
return true;
}
/**
* Make sure there is no svn:executable keyword on the current file.
*
* @returns false - the state carries these kinds of changes.
* @param pState The rewriter state.
* @param pIn The input stream.
* @param pOut The output stream.
* @param pSettings The settings.
*/
static bool rewrite_SvnNoExecutable(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
{
if ( !pSettings->fSetSvnExecutable
|| !scmSvnIsInWorkingCopy(pState))
return false;
int rc = scmSvnQueryProperty(pState, "svn:executable", NULL);
if (RT_SUCCESS(rc))
{
ScmVerbose(pState, 2, " * removing svn:executable\n");
rc = scmSvnDelProperty(pState, "svn:executable");
if (RT_FAILURE(rc))
RTMsgError("scmSvnSetProperty: %Rrc\n", rc); /** @todo error propagation here.. */
}
return false;
}
/**
* Make sure the Id and Revision keywords are expanded.
*
* @returns false - the state carries these kinds of changes.
* @param pState The rewriter state.
* @param pIn The input stream.
* @param pOut The output stream.
* @param pSettings The settings.
*/
static bool rewrite_SvnKeywords(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
{
if ( !pSettings->fSetSvnKeywords
|| !scmSvnIsInWorkingCopy(pState))
return false;
char *pszKeywords;
int rc = scmSvnQueryProperty(pState, "svn:keywords", &pszKeywords);
if ( RT_SUCCESS(rc)
&& ( !strstr(pszKeywords, "Id") /** @todo need some function for finding a word in a string. */
|| !strstr(pszKeywords, "Revision")) )
{
if (!strstr(pszKeywords, "Id") && !strstr(pszKeywords, "Revision"))
rc = RTStrAAppend(&pszKeywords, " Id Revision");
else if (!strstr(pszKeywords, "Id"))
rc = RTStrAAppend(&pszKeywords, " Id");
else
rc = RTStrAAppend(&pszKeywords, " Revision");
if (RT_SUCCESS(rc))
{
ScmVerbose(pState, 2, " * changing svn:keywords to '%s'\n", pszKeywords);
rc = scmSvnSetProperty(pState, "svn:keywords", pszKeywords);
if (RT_FAILURE(rc))
RTMsgError("scmSvnSetProperty: %Rrc\n", rc); /** @todo error propagation here.. */
}
else
RTMsgError("RTStrAppend: %Rrc\n", rc); /** @todo error propagation here.. */
RTStrFree(pszKeywords);
}
else if (rc == VERR_NOT_FOUND)
{
ScmVerbose(pState, 2, " * setting svn:keywords to 'Id Revision'\n");
rc = scmSvnSetProperty(pState, "svn:keywords", "Id Revision");
if (RT_FAILURE(rc))
RTMsgError("scmSvnSetProperty: %Rrc\n", rc); /** @todo error propagation here.. */
}
else if (RT_SUCCESS(rc))
RTStrFree(pszKeywords);
return false;
}
/**
* 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.
* @param pSettings The settings.
*/
static bool rewrite_Makefile_kup(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
{
/* These files should be zero bytes. */
if (pIn->cb == 0)
return false;
ScmVerbose(pState, 2, " * Truncated file to zero bytes\n");
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.
* @param pSettings The settings.
*
* @todo
*
* Ideas for Makefile.kmk and Config.kmk:
* - sort if1of/ifn1of sets.
* - line continuation slashes should only be preceded by one space.
*/
static bool rewrite_Makefile_kmk(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
{
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.
* @param pSettings The settings.
*
* @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.
* - Space before/after '*'.
* - ensure new line at end of file.
* - Indentation of precompiler statements (#ifdef, #defines).
* - space between functions.
* - string.h -> iprt/string.h, stdarg.h -> iprt/stdarg.h, etc.
*/
static bool rewrite_C_and_CPP(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
{
return false;
}
/* -=-=-=-=-=- file and directory processing -=-=-=-=-=- */
/**
* Processes a file.
*
* @returns IPRT status code.
* @param pState The rewriter state.
* @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.)
* @param pBaseSettings The base settings to use. It's OK to modify
* these.
*/
static int scmProcessFileInner(PSCMRWSTATE pState, const char *pszFilename, const char *pszBasename, size_t cchBasename,
PSCMSETTINGSBASE pBaseSettings)
{
/*
* Do the file level filtering.
*/
if ( pBaseSettings->pszFilterFiles
&& *pBaseSettings->pszFilterFiles
&& !RTStrSimplePatternMultiMatch(pBaseSettings->pszFilterFiles, RTSTR_MAX, pszBasename, cchBasename, NULL))
{
ScmVerbose(NULL, 5, "skipping '%s': file filter mismatch\n", pszFilename);
return VINF_SUCCESS;
}
if ( pBaseSettings->pszFilterOutFiles
&& *pBaseSettings->pszFilterOutFiles
&& ( RTStrSimplePatternMultiMatch(pBaseSettings->pszFilterOutFiles, RTSTR_MAX, pszBasename, cchBasename, NULL)
|| RTStrSimplePatternMultiMatch(pBaseSettings->pszFilterOutFiles, RTSTR_MAX, pszFilename, RTSTR_MAX, NULL)) )
{
ScmVerbose(NULL, 5, "skipping '%s': filterd out\n", pszFilename);
return VINF_SUCCESS;
}
if ( pBaseSettings->fOnlySvnFiles
&& !scmSvnIsInWorkingCopy(pState))
{
ScmVerbose(NULL, 5, "skipping '%s': not in SVN WC\n", pszFilename);
return VINF_SUCCESS;
}
/*
* Try find a matching rewrite config for this filename.
*/
PCSCMCFGENTRY pCfg = NULL;
for (size_t iCfg = 0; iCfg < RT_ELEMENTS(g_aConfigs); iCfg++)
if (RTStrSimplePatternMultiMatch(g_aConfigs[iCfg].pszFilePattern, RTSTR_MAX, pszBasename, cchBasename, NULL))
{
pCfg = &g_aConfigs[iCfg];
break;
}
if (!pCfg)
{
ScmVerbose(NULL, 4, "skipping '%s': no rewriters configured\n", pszFilename);
return VINF_SUCCESS;
}
ScmVerbose(pState, 4, "matched \"%s\"\n", pCfg->pszFilePattern);
/*
* Create an input stream from the file and check that it's text.
*/
SCMSTREAM Stream1;
int rc = ScmStreamInitForReading(&Stream1, pszFilename);
if (RT_FAILURE(rc))
{
RTMsgError("Failed to read '%s': %Rrc\n", pszFilename, rc);
return rc;
}
if (ScmStreamIsText(&Stream1))
{
ScmVerbose(pState, 3, NULL);
/*
* Gather SCM and editor settings from the stream.
*/
rc = scmSettingsBaseLoadFromDocument(pBaseSettings, &Stream1);
if (RT_SUCCESS(rc))
{
ScmStreamRewindForReading(&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.
*/
SCMSTREAM Stream2;
rc = ScmStreamInitForWriting(&Stream2, &Stream1);
if (RT_SUCCESS(rc))
{
SCMSTREAM Stream3;
rc = ScmStreamInitForWriting(&Stream3, &Stream1);
if (RT_SUCCESS(rc))
{
bool fModified = false;
PSCMSTREAM pIn = &Stream1;
PSCMSTREAM pOut = &Stream2;
for (size_t iRw = 0; iRw < pCfg->cRewriters; iRw++)
{
bool fRc = pCfg->papfnRewriter[iRw](pState, pIn, pOut, pBaseSettings);
if (fRc)
{
PSCMSTREAM pTmp = pOut;
pOut = pIn == &Stream1 ? &Stream3 : pIn;
pIn = pTmp;
fModified = true;
}
ScmStreamRewindForReading(pIn);
ScmStreamRewindForWriting(pOut);
}
rc = ScmStreamGetStatus(&Stream1);
if (RT_SUCCESS(rc))
rc = ScmStreamGetStatus(&Stream2);
if (RT_SUCCESS(rc))
rc = ScmStreamGetStatus(&Stream3);
if (RT_SUCCESS(rc))
{
/*
* If rewritten, write it back to disk.
*/
if (fModified)
{
if (!g_fDryRun)
{
ScmVerbose(pState, 1, "writing modified file to \"%s%s\"\n", pszFilename, g_pszChangedSuff);
rc = ScmStreamWriteToFile(pIn, "%s%s", pszFilename, g_pszChangedSuff);
if (RT_FAILURE(rc))
RTMsgError("Error writing '%s%s': %Rrc\n", pszFilename, g_pszChangedSuff, rc);
}
else
{
ScmVerbose(pState, 1, NULL);
ScmDiffStreams(pszFilename, &Stream1, pIn, g_fDiffIgnoreEol, g_fDiffIgnoreLeadingWS,
g_fDiffIgnoreTrailingWS, g_fDiffSpecialChars, pBaseSettings->cchTab, g_pStdOut);
ScmVerbose(pState, 2, "would have modified the file \"%s%s\"\n", pszFilename, g_pszChangedSuff);
}
}
/*
* If pending SVN property changes, apply them.
*/
if (pState->cSvnPropChanges && RT_SUCCESS(rc))
{
if (!g_fDryRun)
{
rc = scmSvnApplyChanges(pState);
if (RT_FAILURE(rc))
RTMsgError("%s: failed to apply SVN property changes (%Rrc)\n", pszFilename, rc);
}
else
scmSvnDisplayChanges(pState);
}
if (!fModified && !pState->cSvnPropChanges)
ScmVerbose(pState, 3, "no change\n", pszFilename);
}
else
RTMsgError("%s: stream error %Rrc\n", pszFilename);
ScmStreamDelete(&Stream3);
}
else
RTMsgError("Failed to init stream for writing: %Rrc\n", rc);
ScmStreamDelete(&Stream2);
}
else
RTMsgError("Failed to init stream for writing: %Rrc\n", rc);
}
else
RTMsgError("scmSettingsBaseLoadFromDocument: %Rrc\n", rc);
}
else
ScmVerbose(pState, 4, "not text file: \"%s\"\n", pszFilename);
ScmStreamDelete(&Stream1);
return rc;
}
/**
* Processes a file.
*
* This is just a wrapper for scmProcessFileInner for avoid wasting stack in the
* directory recursion method.
*
* @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.)
* @param pSettingsStack The settings stack (pointer to the top element).
*/
static int scmProcessFile(const char *pszFilename, const char *pszBasename, size_t cchBasename,
PSCMSETTINGS pSettingsStack)
{
SCMSETTINGSBASE Base;
int rc = scmSettingsStackMakeFileBase(pSettingsStack, pszFilename, pszBasename, cchBasename, &Base);
if (RT_SUCCESS(rc))
{
SCMRWSTATE State;
State.fFirst = false;
State.pszFilename = pszFilename;
State.cSvnPropChanges = 0;
State.paSvnPropChanges = NULL;
rc = scmProcessFileInner(&State, pszFilename, pszBasename, cchBasename, &Base);
size_t i = State.cSvnPropChanges;
while (i-- > 0)
{
RTStrFree(State.paSvnPropChanges[i].pszName);
RTStrFree(State.paSvnPropChanges[i].pszValue);
}
RTMemFree(State.paSvnPropChanges);
scmSettingsBaseDelete(&Base);
}
return rc;
}
/**
* Tries to correct RTDIRENTRY_UNKNOWN.
*
* @returns Corrected type.
* @param pszPath The path to the object in question.
*/
static RTDIRENTRYTYPE scmFigureUnknownType(const char *pszPath)
{
RTFSOBJINFO Info;
int rc = RTPathQueryInfo(pszPath, &Info, RTFSOBJATTRADD_NOTHING);
if (RT_FAILURE(rc))
return RTDIRENTRYTYPE_UNKNOWN;
if (RTFS_IS_DIRECTORY(Info.Attr.fMode))
return RTDIRENTRYTYPE_DIRECTORY;
if (RTFS_IS_FILE(Info.Attr.fMode))
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 recursing 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 pSettingsStack The settings stack (pointer to the top element).
* @param iRecursion The recursion depth. This is used to restrict
* the recursions.
*/
static int scmProcessDirTreeRecursion(char *pszBuf, size_t cchDir, PRTDIRENTRY pEntry,
PSCMSETTINGS pSettingsStack, unsigned iRecursion)
{
int rc;
Assert(cchDir > 1 && pszBuf[cchDir - 1] == '.');
/*
* Make sure we stop somewhere.
*/
if (iRecursion > 128)
{
RTMsgError("recursion too deep: %d\n", iRecursion);
return VINF_SUCCESS; /* ignore */
}
/*
* Check if it's excluded by --only-svn-dir.
*/
if (pSettingsStack->Base.fOnlySvnDirs)
{
rc = RTPathAppend(pszBuf, RTPATH_MAX, ".svn");
if (RT_FAILURE(rc))
{
RTMsgError("RTPathAppend: %Rrc\n", rc);
return rc;
}
if (!RTDirExists(pszBuf))
return VINF_SUCCESS;
Assert(RTPATH_IS_SLASH(pszBuf[cchDir]));
pszBuf[cchDir] = '\0';
pszBuf[cchDir - 1] = '.';
}
/*
* Try open and read the directory.
*/
PRTDIR pDir;
rc = RTDirOpenFiltered(&pDir, pszBuf, RTDIRFILTER_NONE, 0);
if (RT_FAILURE(rc))
{
RTMsgError("Failed to enumerate directory '%s': %Rrc", pszBuf, rc);
return rc;
}
for (;;)
{
/* Read the next entry. */
rc = RTDirRead(pDir, pEntry, NULL);
if (RT_FAILURE(rc))
{
if (rc == VERR_NO_MORE_FILES)
rc = VINF_SUCCESS;
else
RTMsgError("RTDirRead -> %Rrc\n", rc);
break;
}
/* Skip '.' and '..'. */
if ( pEntry->szName[0] == '.'
&& ( pEntry->cbName == 1
|| ( pEntry->cbName == 2
&& pEntry->szName[1] == '.')))
continue;
/* Enter it into the buffer so we've got a full name to work
with when needed. */
if (pEntry->cbName + cchDir >= RTPATH_MAX)
{
RTMsgError("Skipping too long entry: %s", pEntry->szName);
continue;
}
memcpy(&pszBuf[cchDir - 1], pEntry->szName, pEntry->cbName + 1);
/* Figure the type. */
RTDIRENTRYTYPE enmType = pEntry->enmType;
if (enmType == RTDIRENTRYTYPE_UNKNOWN)
enmType = scmFigureUnknownType(pszBuf);
/* Process the file or directory, skip the rest. */
if (enmType == RTDIRENTRYTYPE_FILE)
rc = scmProcessFile(pszBuf, pEntry->szName, pEntry->cbName, pSettingsStack);
else if (enmType == RTDIRENTRYTYPE_DIRECTORY)
{
/* Append the dot for the benefit of the pattern matching. */
if (pEntry->cbName + cchDir + 5 >= RTPATH_MAX)
{
RTMsgError("Skipping too deep dir entry: %s", pEntry->szName);
continue;
}
memcpy(&pszBuf[cchDir - 1 + pEntry->cbName], "/.", sizeof("/."));
size_t cchSubDir = cchDir - 1 + pEntry->cbName + sizeof("/.") - 1;
if ( !pSettingsStack->Base.pszFilterOutDirs
|| !*pSettingsStack->Base.pszFilterOutDirs
|| ( !RTStrSimplePatternMultiMatch(pSettingsStack->Base.pszFilterOutDirs, RTSTR_MAX,
pEntry->szName, pEntry->cbName, NULL)
&& !RTStrSimplePatternMultiMatch(pSettingsStack->Base.pszFilterOutDirs, RTSTR_MAX,
pszBuf, cchSubDir, NULL)
)
)
{
rc = scmSettingsStackPushDir(&pSettingsStack, pszBuf);
if (RT_SUCCESS(rc))
{
rc = scmProcessDirTreeRecursion(pszBuf, cchSubDir, pEntry, pSettingsStack, iRecursion + 1);
scmSettingsStackPopAndDestroy(&pSettingsStack);
}
}
}
if (RT_FAILURE(rc))
break;
}
RTDirClose(pDir);
return rc;
}
/**
* 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, PSCMSETTINGS pSettingsStack)
{
/*
* Setup the recursion.
*/
int rc = RTPathAppend(pszDir, RTPATH_MAX, ".");
if (RT_SUCCESS(rc))
{
RTDIRENTRY Entry;
rc = scmProcessDirTreeRecursion(pszDir, strlen(pszDir), &Entry, pSettingsStack, 0);
}
else
RTMsgError("RTPathAppend: %Rrc\n", rc);
return rc;
}
/**
* Processes a file or directory specified as an command line argument.
*
* @returns IPRT status code
* @param pszSomething What we found in the command line arguments.
* @param pSettingsStack The settings stack (pointer to the top element).
*/
static int scmProcessSomething(const char *pszSomething, PSCMSETTINGS pSettingsStack)
{
char szBuf[RTPATH_MAX];
int rc = RTPathAbs(pszSomething, szBuf, sizeof(szBuf));
if (RT_SUCCESS(rc))
{
RTPathChangeToUnixSlashes(szBuf, false /*fForce*/);
PSCMSETTINGS pSettings;
rc = scmSettingsCreateForPath(&pSettings, &pSettingsStack->Base, szBuf);
if (RT_SUCCESS(rc))
{
scmSettingsStackPush(&pSettingsStack, pSettings);
if (RTFileExists(szBuf))
{
const char *pszBasename = RTPathFilename(szBuf);
if (pszBasename)
{
size_t cchBasename = strlen(pszBasename);
rc = scmProcessFile(szBuf, pszBasename, cchBasename, pSettingsStack);
}
else
{
RTMsgError("RTPathFilename: NULL\n");
rc = VERR_IS_A_DIRECTORY;
}
}
else
rc = scmProcessDirTree(szBuf, pSettingsStack);
PSCMSETTINGS pPopped = scmSettingsStackPop(&pSettingsStack);
Assert(pPopped == pSettings);
scmSettingsDestroy(pSettings);
}
else
RTMsgError("scmSettingsInitStack: %Rrc\n", rc);
}
else
RTMsgError("RTPathAbs: %Rrc\n", rc);
return rc;
}
int main(int argc, char **argv)
{
int rc = RTR3InitExe(argc, &argv, 0);
if (RT_FAILURE(rc))
return 1;
/*
* Init the settings.
*/
PSCMSETTINGS pSettings;
rc = scmSettingsCreate(&pSettings, &g_Defaults);
if (RT_FAILURE(rc))
{
RTMsgError("scmSettingsCreate: %Rrc\n", rc);
return 1;
}
/*
* Parse arguments and process input in order (because this is the only
* thing that works at the moment).
*/
static RTGETOPTDEF s_aOpts[14 + RT_ELEMENTS(g_aScmOpts)] =
{
{ "--dry-run", 'd', RTGETOPT_REQ_NOTHING },
{ "--real-run", 'D', RTGETOPT_REQ_NOTHING },
{ "--file-filter", 'f', RTGETOPT_REQ_STRING },
{ "--quiet", 'q', RTGETOPT_REQ_NOTHING },
{ "--verbose", 'v', RTGETOPT_REQ_NOTHING },
{ "--diff-ignore-eol", SCMOPT_DIFF_IGNORE_EOL, RTGETOPT_REQ_NOTHING },
{ "--diff-no-ignore-eol", SCMOPT_DIFF_NO_IGNORE_EOL, RTGETOPT_REQ_NOTHING },
{ "--diff-ignore-space", SCMOPT_DIFF_IGNORE_SPACE, RTGETOPT_REQ_NOTHING },
{ "--diff-no-ignore-space", SCMOPT_DIFF_NO_IGNORE_SPACE, RTGETOPT_REQ_NOTHING },
{ "--diff-ignore-leading-space", SCMOPT_DIFF_IGNORE_LEADING_SPACE, RTGETOPT_REQ_NOTHING },
{ "--diff-no-ignore-leading-space", SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE, RTGETOPT_REQ_NOTHING },
{ "--diff-ignore-trailing-space", SCMOPT_DIFF_IGNORE_TRAILING_SPACE, RTGETOPT_REQ_NOTHING },
{ "--diff-no-ignore-trailing-space", SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE, RTGETOPT_REQ_NOTHING },
{ "--diff-special-chars", SCMOPT_DIFF_SPECIAL_CHARS, RTGETOPT_REQ_NOTHING },
{ "--diff-no-special-chars", SCMOPT_DIFF_NO_SPECIAL_CHARS, RTGETOPT_REQ_NOTHING },
};
memcpy(&s_aOpts[RT_ELEMENTS(s_aOpts) - RT_ELEMENTS(g_aScmOpts)], &g_aScmOpts[0], sizeof(g_aScmOpts));
RTGETOPTUNION ValueUnion;
RTGETOPTSTATE GetOptState;
rc = RTGetOptInit(&GetOptState, argc, argv, &s_aOpts[0], RT_ELEMENTS(s_aOpts), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
AssertReleaseRCReturn(rc, 1);
size_t cProcessed = 0;
while ((rc = RTGetOpt(&GetOptState, &ValueUnion)) != 0)
{
switch (rc)
{
case 'd':
g_fDryRun = true;
break;
case 'D':
g_fDryRun = false;
break;
case 'f':
g_pszFileFilter = ValueUnion.psz;
break;
case 'h':
RTPrintf("VirtualBox Source Code Massager\n"
"\n"
"Usage: %s [options] <files & dirs>\n"
"\n"
"Options:\n", g_szProgName);
for (size_t i = 0; i < RT_ELEMENTS(s_aOpts); i++)
{
bool fAdvanceTwo = false;
if ((s_aOpts[i].fFlags & RTGETOPT_REQ_MASK) == RTGETOPT_REQ_NOTHING)
{
fAdvanceTwo = i + 1 < RT_ELEMENTS(s_aOpts)
&& ( strstr(s_aOpts[i+1].pszLong, "-no-") != NULL
|| strstr(s_aOpts[i+1].pszLong, "-not-") != NULL
|| strstr(s_aOpts[i+1].pszLong, "-dont-") != NULL
);
if (fAdvanceTwo)
RTPrintf(" %s, %s\n", s_aOpts[i].pszLong, s_aOpts[i + 1].pszLong);
else
RTPrintf(" %s\n", s_aOpts[i].pszLong);
}
else if ((s_aOpts[i].fFlags & RTGETOPT_REQ_MASK) == RTGETOPT_REQ_STRING)
RTPrintf(" %s string\n", s_aOpts[i].pszLong);
else
RTPrintf(" %s value\n", s_aOpts[i].pszLong);
switch (s_aOpts[i].iShort)
{
case SCMOPT_CONVERT_EOL: RTPrintf(" Default: %RTbool\n", g_Defaults.fConvertEol); break;
case SCMOPT_CONVERT_TABS: RTPrintf(" Default: %RTbool\n", g_Defaults.fConvertTabs); break;
case SCMOPT_FORCE_FINAL_EOL: RTPrintf(" Default: %RTbool\n", g_Defaults.fForceFinalEol); break;
case SCMOPT_FORCE_TRAILING_LINE: RTPrintf(" Default: %RTbool\n", g_Defaults.fForceTrailingLine); break;
case SCMOPT_STRIP_TRAILING_BLANKS: RTPrintf(" Default: %RTbool\n", g_Defaults.fStripTrailingBlanks); break;
case SCMOPT_STRIP_TRAILING_LINES: RTPrintf(" Default: %RTbool\n", g_Defaults.fStripTrailingLines); break;
case SCMOPT_ONLY_SVN_DIRS: RTPrintf(" Default: %RTbool\n", g_Defaults.fOnlySvnDirs); break;
case SCMOPT_ONLY_SVN_FILES: RTPrintf(" Default: %RTbool\n", g_Defaults.fOnlySvnFiles); break;
case SCMOPT_SET_SVN_EOL: RTPrintf(" Default: %RTbool\n", g_Defaults.fSetSvnEol); break;
case SCMOPT_SET_SVN_EXECUTABLE: RTPrintf(" Default: %RTbool\n", g_Defaults.fSetSvnExecutable); break;
case SCMOPT_SET_SVN_KEYWORDS: RTPrintf(" Default: %RTbool\n", g_Defaults.fSetSvnKeywords); break;
case SCMOPT_TAB_SIZE: RTPrintf(" Default: %u\n", g_Defaults.cchTab); break;
case SCMOPT_FILTER_OUT_DIRS: RTPrintf(" Default: %s\n", g_Defaults.pszFilterOutDirs); break;
case SCMOPT_FILTER_FILES: RTPrintf(" Default: %s\n", g_Defaults.pszFilterFiles); break;
case SCMOPT_FILTER_OUT_FILES: RTPrintf(" Default: %s\n", g_Defaults.pszFilterOutFiles); break;
}
i += fAdvanceTwo;
}
return 1;
case 'q':
g_iVerbosity = 0;
break;
case 'v':
g_iVerbosity++;
break;
case 'V':
{
/* The following is assuming that svn does it's job here. */
static const char s_szRev[] = "$Revision$";
const char *psz = RTStrStripL(strchr(s_szRev, ' '));
RTPrintf("r%.*s\n", strchr(psz, ' ') - psz, psz);
return 0;
}
case SCMOPT_DIFF_IGNORE_EOL:
g_fDiffIgnoreEol = true;
break;
case SCMOPT_DIFF_NO_IGNORE_EOL:
g_fDiffIgnoreEol = false;
break;
case SCMOPT_DIFF_IGNORE_SPACE:
g_fDiffIgnoreTrailingWS = g_fDiffIgnoreLeadingWS = true;
break;
case SCMOPT_DIFF_NO_IGNORE_SPACE:
g_fDiffIgnoreTrailingWS = g_fDiffIgnoreLeadingWS = false;
break;
case SCMOPT_DIFF_IGNORE_LEADING_SPACE:
g_fDiffIgnoreLeadingWS = true;
break;
case SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE:
g_fDiffIgnoreLeadingWS = false;
break;
case SCMOPT_DIFF_IGNORE_TRAILING_SPACE:
g_fDiffIgnoreTrailingWS = true;
break;
case SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE:
g_fDiffIgnoreTrailingWS = false;
break;
case SCMOPT_DIFF_SPECIAL_CHARS:
g_fDiffSpecialChars = true;
break;
case SCMOPT_DIFF_NO_SPECIAL_CHARS:
g_fDiffSpecialChars = 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",
g_szProgName, g_szProgName, g_szProgName, g_szProgName, g_szProgName,
g_szProgName, g_szProgName);
RTStrmGetCh(g_pStdIn);
}
cProcessed++;
}
rc = scmProcessSomething(ValueUnion.psz, pSettings);
if (RT_FAILURE(rc))
return rc;
break;
}
default:
{
int rc2 = scmSettingsBaseHandleOpt(&pSettings->Base, rc, &ValueUnion);
if (RT_SUCCESS(rc2))
break;
if (rc2 != VERR_GETOPT_UNKNOWN_OPTION)
return 2;
return RTGetOptPrintError(rc, &ValueUnion);
}
}
}
scmSettingsDestroy(pSettings);
return 0;
}