VirtualBox

source: vbox/trunk/src/bldprogs/scm.cpp@ 106724

Last change on this file since 106724 was 106499, checked in by vboxsync, 6 weeks ago

scm: Shut up stupid VC 14.3 warning. jiraref:VBP-1171

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 124.1 KB
Line 
1/* $Id: scm.cpp 106499 2024-10-19 03:13:15Z vboxsync $ */
2/** @file
3 * IPRT Testcase / Tool - Source Code Massager.
4 */
5
6/*
7 * Copyright (C) 2010-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#include <iprt/assert.h>
33#include <iprt/ctype.h>
34#include <iprt/dir.h>
35#include <iprt/env.h>
36#include <iprt/file.h>
37#include <iprt/err.h>
38#include <iprt/getopt.h>
39#include <iprt/initterm.h>
40#include <iprt/mem.h>
41#include <iprt/message.h>
42#include <iprt/param.h>
43#include <iprt/path.h>
44#include <iprt/process.h>
45#include <iprt/stream.h>
46#include <iprt/string.h>
47
48#include "scm.h"
49#include "scmdiff.h"
50
51
52/*********************************************************************************************************************************
53* Defined Constants And Macros *
54*********************************************************************************************************************************/
55/** The name of the settings files. */
56#define SCM_SETTINGS_FILENAME ".scm-settings"
57
58
59/*********************************************************************************************************************************
60* Structures and Typedefs *
61*********************************************************************************************************************************/
62
63/**
64 * Option identifiers.
65 *
66 * @note The first chunk, down to SCMOPT_TAB_SIZE, are alternately set &
67 * clear. So, the option setting a flag (boolean) will have an even
68 * number and the one clearing it will have an odd number.
69 * @note Down to SCMOPT_LAST_SETTINGS corresponds exactly to SCMSETTINGSBASE.
70 */
71typedef enum SCMOPT
72{
73 SCMOPT_CONVERT_EOL = 10000,
74 SCMOPT_NO_CONVERT_EOL,
75 SCMOPT_CONVERT_TABS,
76 SCMOPT_NO_CONVERT_TABS,
77 SCMOPT_FORCE_FINAL_EOL,
78 SCMOPT_NO_FORCE_FINAL_EOL,
79 SCMOPT_FORCE_TRAILING_LINE,
80 SCMOPT_NO_FORCE_TRAILING_LINE,
81 SCMOPT_STRIP_TRAILING_BLANKS,
82 SCMOPT_NO_STRIP_TRAILING_BLANKS,
83 SCMOPT_STRIP_TRAILING_LINES,
84 SCMOPT_NO_STRIP_TRAILING_LINES,
85 SCMOPT_FIX_FLOWER_BOX_MARKERS,
86 SCMOPT_NO_FIX_FLOWER_BOX_MARKERS,
87 SCMOPT_FIX_HEADER_GUARDS,
88 SCMOPT_NO_FIX_HEADER_GUARDS,
89 SCMOPT_PRAGMA_ONCE,
90 SCMOPT_NO_PRAGMA_ONCE,
91 SCMOPT_FIX_HEADER_GUARD_ENDIF,
92 SCMOPT_NO_FIX_HEADER_GUARD_ENDIF,
93 SCMOPT_ENDIF_GUARD_COMMENT,
94 SCMOPT_NO_ENDIF_GUARD_COMMENT,
95 SCMOPT_GUARD_PREFIX,
96 SCMOPT_GUARD_RELATIVE_TO_DIR,
97 SCMOPT_FIX_TODOS,
98 SCMOPT_NO_FIX_TODOS,
99 SCMOPT_FIX_ERR_H,
100 SCMOPT_NO_FIX_ERR_H,
101 SCMOPT_ONLY_GUEST_HOST_PAGE,
102 SCMOPT_NO_ASM_MEM_PAGE_USE,
103 SCMOPT_UNRESTRICTED_ASM_MEM_PAGE_USE,
104 SCMOPT_NO_PAGE_RESTRICTIONS,
105 SCMOPT_NO_RC_USE,
106 SCMOPT_UNRESTRICTED_RC_USE,
107 SCMOPT_STANDARIZE_KMK,
108 SCMOPT_NO_STANDARIZE_KMK,
109 SCMOPT_UPDATE_COPYRIGHT_YEAR,
110 SCMOPT_NO_UPDATE_COPYRIGHT_YEAR,
111 SCMOPT_EXTERNAL_COPYRIGHT,
112 SCMOPT_NO_EXTERNAL_COPYRIGHT,
113 SCMOPT_NO_UPDATE_LICENSE,
114 SCMOPT_LICENSE_OSE_GPL,
115 SCMOPT_LICENSE_OSE_DUAL_GPL_CDDL,
116 SCMOPT_LICENSE_OSE_CDDL,
117 SCMOPT_LICENSE_LGPL,
118 SCMOPT_LICENSE_MIT,
119 SCMOPT_LICENSE_BASED_ON_MIT,
120 SCMOPT_LGPL_DISCLAIMER,
121 SCMOPT_NO_LGPL_DISCLAIMER,
122 SCMOPT_MIN_BLANK_LINES_BEFORE_FLOWER_BOX_MARKERS,
123 SCMOPT_ONLY_SVN_DIRS,
124 SCMOPT_NOT_ONLY_SVN_DIRS,
125 SCMOPT_ONLY_SVN_FILES,
126 SCMOPT_NOT_ONLY_SVN_FILES,
127 SCMOPT_SET_SVN_EOL,
128 SCMOPT_DONT_SET_SVN_EOL,
129 SCMOPT_SET_SVN_EXECUTABLE,
130 SCMOPT_DONT_SET_SVN_EXECUTABLE,
131 SCMOPT_SET_SVN_KEYWORDS,
132 SCMOPT_DONT_SET_SVN_KEYWORDS,
133 SCMOPT_SKIP_SVN_SYNC_PROCESS,
134 SCMOPT_DONT_SKIP_SVN_SYNC_PROCESS,
135 SCMOPT_SKIP_UNICODE_CHECKS,
136 SCMOPT_DONT_SKIP_UNICODE_CHECKS,
137 SCMOPT_TAB_SIZE,
138 SCMOPT_WIDTH,
139 SCMOPT_FILTER_OUT_DIRS,
140 SCMOPT_FILTER_FILES,
141 SCMOPT_FILTER_OUT_FILES,
142 SCMOPT_TREAT_AS,
143 SCMOPT_ADD_ACTION,
144 SCMOPT_DEL_ACTION,
145 SCMOPT_LAST_SETTINGS = SCMOPT_DEL_ACTION,
146 //
147 SCMOPT_CHECK_RUN,
148 SCMOPT_DIFF_IGNORE_EOL,
149 SCMOPT_DIFF_NO_IGNORE_EOL,
150 SCMOPT_DIFF_IGNORE_SPACE,
151 SCMOPT_DIFF_NO_IGNORE_SPACE,
152 SCMOPT_DIFF_IGNORE_LEADING_SPACE,
153 SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE,
154 SCMOPT_DIFF_IGNORE_TRAILING_SPACE,
155 SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE,
156 SCMOPT_DIFF_SPECIAL_CHARS,
157 SCMOPT_DIFF_NO_SPECIAL_CHARS,
158 SCMOPT_HELP_CONFIG,
159 SCMOPT_HELP_ACTIONS,
160 SCMOPT_END
161} SCMOPT;
162
163
164/*********************************************************************************************************************************
165* Global Variables *
166*********************************************************************************************************************************/
167const char g_szTabSpaces[16+1] = " ";
168const char g_szAsterisks[255+1] =
169"****************************************************************************************************"
170"****************************************************************************************************"
171"*******************************************************";
172const char g_szSpaces[255+1] =
173" "
174" "
175" ";
176static const char g_szProgName[] = "scm";
177static const char *g_pszChangedSuff = "";
178static bool g_fDryRun = true;
179static bool g_fDiffSpecialChars = true;
180static bool g_fDiffIgnoreEol = false;
181static bool g_fDiffIgnoreLeadingWS = false;
182static bool g_fDiffIgnoreTrailingWS = false;
183static int g_iVerbosity = 2;//99; //0;
184uint32_t g_uYear = 0; /**< The current year. */
185/** @name Statistics
186 * @{ */
187static uint32_t g_cDirsProcessed = 0;
188static uint32_t g_cFilesProcessed = 0;
189static uint32_t g_cFilesModified = 0;
190static uint32_t g_cFilesSkipped = 0;
191static uint32_t g_cFilesNotInSvn = 0;
192static uint32_t g_cFilesNoRewriters = 0;
193static uint32_t g_cFilesBinaries = 0;
194static uint32_t g_cFilesRequiringManualFixing = 0;
195/** @} */
196
197/** The global settings. */
198static SCMSETTINGSBASE const g_Defaults =
199{
200 /* .fConvertEol = */ true,
201 /* .fConvertTabs = */ true,
202 /* .fForceFinalEol = */ true,
203 /* .fForceTrailingLine = */ false,
204 /* .fStripTrailingBlanks = */ true,
205 /* .fStripTrailingLines = */ true,
206 /* .fFixFlowerBoxMarkers = */ true,
207 /* .cMinBlankLinesBeforeFlowerBoxMakers = */ 2,
208 /* .fFixHeaderGuards = */ true,
209 /* .fPragmaOnce = */ true,
210 /* .fFixHeaderGuardEndif = */ true,
211 /* .fEndifGuardComment = */ true,
212 /* .pszGuardPrefix = */ (char *)"VBOX_INCLUDED_SRC_",
213 /* .pszGuardRelativeToDir = */ (char *)"{parent}",
214 /* .fFixTodos = */ true,
215 /* .fFixErrH = */ true,
216 /* .fOnlyGuestHostPage = */ false,
217 /* .fNoASMMemPageUse = */ false,
218 /* .fOnlyHrcVrcInsteadOfRc */ false,
219 /* .fStandarizeKmk */ true,
220 /* .fUpdateCopyrightYear = */ false,
221 /* .fExternalCopyright = */ false,
222 /* .fLgplDisclaimer = */ false,
223 /* .enmUpdateLicense = */ kScmLicense_OseGpl,
224 /* .fOnlySvnFiles = */ false,
225 /* .fOnlySvnDirs = */ false,
226 /* .fSetSvnEol = */ false,
227 /* .fSetSvnExecutable = */ false,
228 /* .fSetSvnKeywords = */ false,
229 /* .fSkipSvnSyncProcess = */ false,
230 /* .fSkipUnicodeChecks = */ false,
231 /* .cchTab = */ 8,
232 /* .cchWidth = */ 130,
233 /* .fFreeTreatAs = */ false,
234 /* .pTreatAs = */ NULL,
235 /* .pszFilterFiles = */ (char *)"",
236 /* .pszFilterOutFiles = */ (char *)"*.exe|*.com|20*-*-*.log",
237 /* .pszFilterOutDirs = */ (char *)".svn|.hg|.git|CVS",
238};
239
240/** Option definitions for the base settings. */
241static RTGETOPTDEF g_aScmOpts[] =
242{
243 /* rewriters */
244 { "--convert-eol", SCMOPT_CONVERT_EOL, RTGETOPT_REQ_NOTHING },
245 { "--no-convert-eol", SCMOPT_NO_CONVERT_EOL, RTGETOPT_REQ_NOTHING },
246 { "--convert-tabs", SCMOPT_CONVERT_TABS, RTGETOPT_REQ_NOTHING },
247 { "--no-convert-tabs", SCMOPT_NO_CONVERT_TABS, RTGETOPT_REQ_NOTHING },
248 { "--force-final-eol", SCMOPT_FORCE_FINAL_EOL, RTGETOPT_REQ_NOTHING },
249 { "--no-force-final-eol", SCMOPT_NO_FORCE_FINAL_EOL, RTGETOPT_REQ_NOTHING },
250 { "--force-trailing-line", SCMOPT_FORCE_TRAILING_LINE, RTGETOPT_REQ_NOTHING },
251 { "--no-force-trailing-line", SCMOPT_NO_FORCE_TRAILING_LINE, RTGETOPT_REQ_NOTHING },
252 { "--strip-trailing-blanks", SCMOPT_STRIP_TRAILING_BLANKS, RTGETOPT_REQ_NOTHING },
253 { "--no-strip-trailing-blanks", SCMOPT_NO_STRIP_TRAILING_BLANKS, RTGETOPT_REQ_NOTHING },
254 { "--strip-trailing-lines", SCMOPT_STRIP_TRAILING_LINES, RTGETOPT_REQ_NOTHING },
255 { "--strip-no-trailing-lines", SCMOPT_NO_STRIP_TRAILING_LINES, RTGETOPT_REQ_NOTHING },
256 { "--min-blank-lines-before-flower-box-makers", SCMOPT_MIN_BLANK_LINES_BEFORE_FLOWER_BOX_MARKERS, RTGETOPT_REQ_UINT8 },
257 { "--fix-flower-box-markers", SCMOPT_FIX_FLOWER_BOX_MARKERS, RTGETOPT_REQ_NOTHING },
258 { "--no-fix-flower-box-markers", SCMOPT_NO_FIX_FLOWER_BOX_MARKERS, RTGETOPT_REQ_NOTHING },
259 { "--fix-header-guards", SCMOPT_FIX_HEADER_GUARDS, RTGETOPT_REQ_NOTHING },
260 { "--no-fix-header-guards", SCMOPT_NO_FIX_HEADER_GUARDS, RTGETOPT_REQ_NOTHING },
261 { "--pragma-once", SCMOPT_PRAGMA_ONCE, RTGETOPT_REQ_NOTHING },
262 { "--no-pragma-once", SCMOPT_NO_PRAGMA_ONCE, RTGETOPT_REQ_NOTHING },
263 { "--fix-header-guard-endif", SCMOPT_FIX_HEADER_GUARD_ENDIF, RTGETOPT_REQ_NOTHING },
264 { "--no-fix-header-guard-endif", SCMOPT_NO_FIX_HEADER_GUARD_ENDIF, RTGETOPT_REQ_NOTHING },
265 { "--endif-guard-comment", SCMOPT_ENDIF_GUARD_COMMENT, RTGETOPT_REQ_NOTHING },
266 { "--no-endif-guard-comment", SCMOPT_NO_ENDIF_GUARD_COMMENT, RTGETOPT_REQ_NOTHING },
267 { "--guard-prefix", SCMOPT_GUARD_PREFIX, RTGETOPT_REQ_STRING },
268 { "--guard-relative-to-dir", SCMOPT_GUARD_RELATIVE_TO_DIR, RTGETOPT_REQ_STRING },
269 { "--fix-todos", SCMOPT_FIX_TODOS, RTGETOPT_REQ_NOTHING },
270 { "--no-fix-todos", SCMOPT_NO_FIX_TODOS, RTGETOPT_REQ_NOTHING },
271 { "--fix-err-h", SCMOPT_FIX_ERR_H, RTGETOPT_REQ_NOTHING },
272 { "--no-fix-err-h", SCMOPT_NO_FIX_ERR_H, RTGETOPT_REQ_NOTHING },
273 { "--only-guest-host-page", SCMOPT_ONLY_GUEST_HOST_PAGE, RTGETOPT_REQ_NOTHING },
274 { "--no-page-restrictions", SCMOPT_NO_PAGE_RESTRICTIONS, RTGETOPT_REQ_NOTHING },
275 { "--no-ASMMemPage-use", SCMOPT_NO_ASM_MEM_PAGE_USE, RTGETOPT_REQ_NOTHING },
276 { "--unrestricted-ASMMemPage-use", SCMOPT_UNRESTRICTED_ASM_MEM_PAGE_USE, RTGETOPT_REQ_NOTHING },
277 { "--no-rc-use", SCMOPT_NO_RC_USE, RTGETOPT_REQ_NOTHING },
278 { "--unrestricted-rc-use", SCMOPT_UNRESTRICTED_RC_USE, RTGETOPT_REQ_NOTHING },
279 { "--standarize-kmk", SCMOPT_STANDARIZE_KMK, RTGETOPT_REQ_NOTHING },
280 { "--no-standarize-kmk", SCMOPT_NO_STANDARIZE_KMK, RTGETOPT_REQ_NOTHING },
281 { "--update-copyright-year", SCMOPT_UPDATE_COPYRIGHT_YEAR, RTGETOPT_REQ_NOTHING },
282 { "--no-update-copyright-year", SCMOPT_NO_UPDATE_COPYRIGHT_YEAR, RTGETOPT_REQ_NOTHING },
283 { "--external-copyright", SCMOPT_EXTERNAL_COPYRIGHT, RTGETOPT_REQ_NOTHING },
284 { "--no-external-copyright", SCMOPT_NO_EXTERNAL_COPYRIGHT, RTGETOPT_REQ_NOTHING },
285 { "--no-update-license", SCMOPT_NO_UPDATE_LICENSE, RTGETOPT_REQ_NOTHING },
286 { "--license-ose-gpl", SCMOPT_LICENSE_OSE_GPL, RTGETOPT_REQ_NOTHING },
287 { "--license-ose-dual", SCMOPT_LICENSE_OSE_DUAL_GPL_CDDL, RTGETOPT_REQ_NOTHING },
288 { "--license-ose-cddl", SCMOPT_LICENSE_OSE_CDDL, RTGETOPT_REQ_NOTHING },
289 { "--license-lgpl", SCMOPT_LICENSE_LGPL, RTGETOPT_REQ_NOTHING },
290 { "--license-mit", SCMOPT_LICENSE_MIT, RTGETOPT_REQ_NOTHING },
291 { "--license-based-on-mit", SCMOPT_LICENSE_BASED_ON_MIT, RTGETOPT_REQ_NOTHING },
292 { "--lgpl-disclaimer", SCMOPT_LGPL_DISCLAIMER, RTGETOPT_REQ_NOTHING },
293 { "--no-lgpl-disclaimer", SCMOPT_NO_LGPL_DISCLAIMER, RTGETOPT_REQ_NOTHING },
294 { "--set-svn-eol", SCMOPT_SET_SVN_EOL, RTGETOPT_REQ_NOTHING },
295 { "--dont-set-svn-eol", SCMOPT_DONT_SET_SVN_EOL, RTGETOPT_REQ_NOTHING },
296 { "--set-svn-executable", SCMOPT_SET_SVN_EXECUTABLE, RTGETOPT_REQ_NOTHING },
297 { "--dont-set-svn-executable", SCMOPT_DONT_SET_SVN_EXECUTABLE, RTGETOPT_REQ_NOTHING },
298 { "--set-svn-keywords", SCMOPT_SET_SVN_KEYWORDS, RTGETOPT_REQ_NOTHING },
299 { "--dont-set-svn-keywords", SCMOPT_DONT_SET_SVN_KEYWORDS, RTGETOPT_REQ_NOTHING },
300 { "--skip-svn-sync-process", SCMOPT_SKIP_SVN_SYNC_PROCESS, RTGETOPT_REQ_NOTHING },
301 { "--dont-skip-svn-sync-process", SCMOPT_DONT_SKIP_SVN_SYNC_PROCESS, RTGETOPT_REQ_NOTHING },
302 { "--skip-unicode-checks", SCMOPT_SKIP_UNICODE_CHECKS, RTGETOPT_REQ_NOTHING },
303 { "--dont-skip-unicode-checks", SCMOPT_DONT_SKIP_UNICODE_CHECKS, RTGETOPT_REQ_NOTHING },
304 { "--tab-size", SCMOPT_TAB_SIZE, RTGETOPT_REQ_UINT8 },
305 { "--width", SCMOPT_WIDTH, RTGETOPT_REQ_UINT8 },
306
307 /* input selection */
308 { "--only-svn-dirs", SCMOPT_ONLY_SVN_DIRS, RTGETOPT_REQ_NOTHING },
309 { "--not-only-svn-dirs", SCMOPT_NOT_ONLY_SVN_DIRS, RTGETOPT_REQ_NOTHING },
310 { "--only-svn-files", SCMOPT_ONLY_SVN_FILES, RTGETOPT_REQ_NOTHING },
311 { "--not-only-svn-files", SCMOPT_NOT_ONLY_SVN_FILES, RTGETOPT_REQ_NOTHING },
312 { "--filter-out-dirs", SCMOPT_FILTER_OUT_DIRS, RTGETOPT_REQ_STRING },
313 { "--filter-files", SCMOPT_FILTER_FILES, RTGETOPT_REQ_STRING },
314 { "--filter-out-files", SCMOPT_FILTER_OUT_FILES, RTGETOPT_REQ_STRING },
315
316 /* rewriter selection */
317 { "--treat-as", SCMOPT_TREAT_AS, RTGETOPT_REQ_STRING },
318 { "--add-action", SCMOPT_ADD_ACTION, RTGETOPT_REQ_STRING },
319 { "--del-action", SCMOPT_DEL_ACTION, RTGETOPT_REQ_STRING },
320
321 /* Additional help */
322 { "--help-config", SCMOPT_HELP_CONFIG, RTGETOPT_REQ_NOTHING },
323 { "--help-actions", SCMOPT_HELP_ACTIONS, RTGETOPT_REQ_NOTHING },
324};
325
326/** Consider files matching the following patterns (base names only). */
327static const char *g_pszFileFilter = NULL;
328
329/* The rewriter configuration. */
330#define SCM_REWRITER_CFG(a_Global, a_szName, fnRewriter) static const SCMREWRITERCFG a_Global = { &fnRewriter, a_szName }
331SCM_REWRITER_CFG(g_StripTrailingBlanks, "strip-trailing-blanks", rewrite_StripTrailingBlanks);
332SCM_REWRITER_CFG(g_ExpandTabs, "expand-tabs", rewrite_ExpandTabs);
333SCM_REWRITER_CFG(g_ForceNativeEol, "force-native-eol", rewrite_ForceNativeEol);
334SCM_REWRITER_CFG(g_ForceLF, "force-lf", rewrite_ForceLF);
335SCM_REWRITER_CFG(g_ForceCRLF, "force-crlf", rewrite_ForceCRLF);
336SCM_REWRITER_CFG(g_AdjustTrailingLines, "adjust-trailing-lines", rewrite_AdjustTrailingLines);
337SCM_REWRITER_CFG(g_SvnNoExecutable, "svn-no-executable", rewrite_SvnNoExecutable);
338SCM_REWRITER_CFG(g_SvnNoKeywords, "svn-no-keywords", rewrite_SvnNoKeywords);
339SCM_REWRITER_CFG(g_SvnNoEolStyle, "svn-no-eol-style", rewrite_SvnNoEolStyle);
340SCM_REWRITER_CFG(g_SvnBinary, "svn-binary", rewrite_SvnBinary);
341SCM_REWRITER_CFG(g_SvnKeywords, "svn-keywords", rewrite_SvnKeywords);
342SCM_REWRITER_CFG(g_SvnSyncProcess, "svn-sync-process", rewrite_SvnSyncProcess);
343SCM_REWRITER_CFG(g_UnicodeChecks, "unicode-checks", rewrite_UnicodeChecks);
344SCM_REWRITER_CFG(g_PageChecks, "page-checks", rewrite_PageChecks);
345SCM_REWRITER_CFG(g_ForceHrcVrcInsteadOfRc, "force-hrc-vrc-no-rc", rewrite_ForceHrcVrcInsteadOfRc);
346SCM_REWRITER_CFG(g_Copyright_CstyleComment, "copyright-c-style", rewrite_Copyright_CstyleComment);
347SCM_REWRITER_CFG(g_Copyright_HashComment, "copyright-hash-style", rewrite_Copyright_HashComment);
348SCM_REWRITER_CFG(g_Copyright_PythonComment, "copyright-python-style", rewrite_Copyright_PythonComment);
349SCM_REWRITER_CFG(g_Copyright_RemComment, "copyright-rem-style", rewrite_Copyright_RemComment);
350SCM_REWRITER_CFG(g_Copyright_SemicolonComment, "copyright-semicolon-style", rewrite_Copyright_SemicolonComment);
351SCM_REWRITER_CFG(g_Copyright_SqlComment, "copyright-sql-style", rewrite_Copyright_SqlComment);
352SCM_REWRITER_CFG(g_Copyright_TickComment, "copyright-tick-style", rewrite_Copyright_TickComment);
353SCM_REWRITER_CFG(g_Copyright_XmlComment, "copyright-xml-style", rewrite_Copyright_XmlComment);
354SCM_REWRITER_CFG(g_Makefile_kup, "makefile-kup", rewrite_Makefile_kup);
355SCM_REWRITER_CFG(g_Makefile_kmk, "makefile-kmk", rewrite_Makefile_kmk);
356SCM_REWRITER_CFG(g_FixFlowerBoxMarkers, "fix-flower-boxes", rewrite_FixFlowerBoxMarkers);
357SCM_REWRITER_CFG(g_FixHeaderGuards, "fix-header-guard", rewrite_FixHeaderGuards);
358SCM_REWRITER_CFG(g_Fix_C_and_CPP_Todos, "fix-c-todos", rewrite_Fix_C_and_CPP_Todos);
359SCM_REWRITER_CFG(g_Fix_Err_H, "fix-err-h", rewrite_Fix_Err_H);
360SCM_REWRITER_CFG(g_C_and_CPP, "c-and-cpp", rewrite_C_and_CPP);
361
362/** The rewriter actions. */
363static PCSCMREWRITERCFG const g_papRewriterActions[] =
364{
365 &g_StripTrailingBlanks,
366 &g_ExpandTabs,
367 &g_ForceNativeEol,
368 &g_ForceLF,
369 &g_ForceCRLF,
370 &g_AdjustTrailingLines,
371 &g_SvnNoExecutable,
372 &g_SvnNoKeywords,
373 &g_SvnNoEolStyle,
374 &g_SvnBinary,
375 &g_SvnKeywords,
376 &g_SvnSyncProcess,
377 &g_Copyright_CstyleComment,
378 &g_Copyright_HashComment,
379 &g_Copyright_PythonComment,
380 &g_Copyright_RemComment,
381 &g_Copyright_SemicolonComment,
382 &g_Copyright_SqlComment,
383 &g_Copyright_TickComment,
384 &g_Makefile_kup,
385 &g_Makefile_kmk,
386 &g_FixFlowerBoxMarkers,
387 &g_FixHeaderGuards,
388 &g_Fix_C_and_CPP_Todos,
389 &g_Fix_Err_H,
390 &g_UnicodeChecks,
391 &g_PageChecks,
392 &g_ForceHrcVrcInsteadOfRc,
393 &g_C_and_CPP,
394};
395
396
397static PCSCMREWRITERCFG const g_apRewritersFor_Makefile_kup[] =
398{
399 &g_SvnNoExecutable,
400 &g_SvnSyncProcess,
401 &g_UnicodeChecks,
402 &g_Makefile_kup
403};
404
405static PCSCMREWRITERCFG const g_apRewritersFor_Makefile_kmk[] =
406{
407 &g_ForceNativeEol,
408 &g_StripTrailingBlanks,
409 &g_AdjustTrailingLines,
410 &g_SvnNoExecutable,
411 &g_SvnKeywords,
412 &g_SvnSyncProcess,
413 &g_UnicodeChecks,
414 &g_Copyright_HashComment,
415 &g_Makefile_kmk
416};
417
418static PCSCMREWRITERCFG const g_apRewritersFor_OtherMakefiles[] =
419{
420 &g_ForceNativeEol,
421 &g_StripTrailingBlanks,
422 &g_AdjustTrailingLines,
423 &g_SvnNoExecutable,
424 &g_SvnKeywords,
425 &g_SvnSyncProcess,
426 &g_UnicodeChecks,
427 &g_Copyright_HashComment,
428};
429
430static PCSCMREWRITERCFG const g_apRewritersFor_C_and_CPP[] =
431{
432 &g_ForceNativeEol,
433 &g_ExpandTabs,
434 &g_StripTrailingBlanks,
435 &g_AdjustTrailingLines,
436 &g_SvnNoExecutable,
437 &g_SvnKeywords,
438 &g_SvnSyncProcess,
439 &g_UnicodeChecks,
440 &g_PageChecks,
441 &g_ForceHrcVrcInsteadOfRc,
442 &g_Copyright_CstyleComment,
443 &g_FixFlowerBoxMarkers,
444 &g_Fix_C_and_CPP_Todos,
445 &g_Fix_Err_H,
446 &g_C_and_CPP,
447};
448
449static PCSCMREWRITERCFG const g_apRewritersFor_H_and_HPP[] =
450{
451 &g_ForceNativeEol,
452 &g_ExpandTabs,
453 &g_StripTrailingBlanks,
454 &g_AdjustTrailingLines,
455 &g_SvnNoExecutable,
456 &g_SvnKeywords,
457 &g_SvnSyncProcess,
458 &g_UnicodeChecks,
459 &g_PageChecks,
460 &g_ForceHrcVrcInsteadOfRc,
461 &g_Copyright_CstyleComment,
462 /// @todo &g_FixFlowerBoxMarkers,
463 &g_FixHeaderGuards,
464 &g_C_and_CPP
465};
466
467static PCSCMREWRITERCFG const g_apRewritersFor_RC[] =
468{
469 &g_ForceNativeEol,
470 &g_ExpandTabs,
471 &g_StripTrailingBlanks,
472 &g_AdjustTrailingLines,
473 &g_SvnNoExecutable,
474 &g_SvnKeywords,
475 &g_SvnSyncProcess,
476 &g_UnicodeChecks,
477 &g_Copyright_CstyleComment,
478};
479
480static PCSCMREWRITERCFG const g_apRewritersFor_DTrace[] =
481{
482 &g_ForceNativeEol,
483 &g_ExpandTabs,
484 &g_StripTrailingBlanks,
485 &g_AdjustTrailingLines,
486 &g_SvnKeywords,
487 &g_SvnSyncProcess,
488 &g_UnicodeChecks,
489 &g_Copyright_CstyleComment,
490};
491
492static PCSCMREWRITERCFG const g_apRewritersFor_DSL[] =
493{
494 &g_ForceNativeEol,
495 &g_ExpandTabs,
496 &g_StripTrailingBlanks,
497 &g_AdjustTrailingLines,
498 &g_SvnNoExecutable,
499 &g_SvnKeywords,
500 &g_SvnSyncProcess,
501 &g_UnicodeChecks,
502 &g_Copyright_CstyleComment,
503};
504
505static PCSCMREWRITERCFG const g_apRewritersFor_ASM[] =
506{
507 &g_ForceNativeEol,
508 &g_ExpandTabs,
509 &g_StripTrailingBlanks,
510 &g_AdjustTrailingLines,
511 &g_SvnNoExecutable,
512 &g_SvnKeywords,
513 &g_SvnSyncProcess,
514 &g_UnicodeChecks,
515 &g_Copyright_SemicolonComment,
516};
517
518static PCSCMREWRITERCFG const g_apRewritersFor_DEF[] =
519{
520 &g_ForceNativeEol,
521 &g_ExpandTabs,
522 &g_StripTrailingBlanks,
523 &g_AdjustTrailingLines,
524 &g_SvnNoExecutable,
525 &g_SvnKeywords,
526 &g_SvnSyncProcess,
527 &g_UnicodeChecks,
528 &g_Copyright_SemicolonComment,
529};
530
531static PCSCMREWRITERCFG const g_apRewritersFor_ShellScripts[] =
532{
533 &g_ForceLF,
534 &g_ExpandTabs,
535 &g_StripTrailingBlanks,
536 &g_SvnSyncProcess,
537 &g_UnicodeChecks,
538 &g_Copyright_HashComment,
539};
540
541static PCSCMREWRITERCFG const g_apRewritersFor_BatchFiles[] =
542{
543 &g_ForceCRLF,
544 &g_ExpandTabs,
545 &g_StripTrailingBlanks,
546 &g_SvnSyncProcess,
547 &g_UnicodeChecks,
548 &g_Copyright_RemComment,
549};
550
551static PCSCMREWRITERCFG const g_apRewritersFor_BasicScripts[] =
552{
553 &g_ForceCRLF,
554 &g_ExpandTabs,
555 &g_StripTrailingBlanks,
556 &g_SvnSyncProcess,
557 &g_UnicodeChecks,
558 &g_Copyright_TickComment,
559};
560
561static PCSCMREWRITERCFG const g_apRewritersFor_SedScripts[] =
562{
563 &g_ForceLF,
564 &g_ExpandTabs,
565 &g_StripTrailingBlanks,
566 &g_SvnSyncProcess,
567 &g_UnicodeChecks,
568 &g_Copyright_HashComment,
569};
570
571static PCSCMREWRITERCFG const g_apRewritersFor_Python[] =
572{
573 /** @todo &g_ForceLFIfExecutable */
574 &g_ExpandTabs,
575 &g_StripTrailingBlanks,
576 &g_AdjustTrailingLines,
577 &g_SvnKeywords,
578 &g_SvnSyncProcess,
579 &g_UnicodeChecks,
580 &g_Copyright_PythonComment,
581};
582
583static PCSCMREWRITERCFG const g_apRewritersFor_Perl[] =
584{
585 /** @todo &g_ForceLFIfExecutable */
586 &g_ExpandTabs,
587 &g_StripTrailingBlanks,
588 &g_AdjustTrailingLines,
589 &g_SvnKeywords,
590 &g_SvnSyncProcess,
591 &g_UnicodeChecks,
592 &g_Copyright_HashComment,
593};
594
595static PCSCMREWRITERCFG const g_apRewritersFor_DriverInfFiles[] =
596{
597 &g_ForceNativeEol,
598 &g_ExpandTabs,
599 &g_StripTrailingBlanks,
600 &g_AdjustTrailingLines,
601 &g_SvnKeywords,
602 &g_SvnNoExecutable,
603 &g_SvnSyncProcess,
604 &g_UnicodeChecks,
605 &g_Copyright_SemicolonComment,
606};
607
608static PCSCMREWRITERCFG const g_apRewritersFor_NsisFiles[] =
609{
610 &g_ForceNativeEol,
611 &g_ExpandTabs,
612 &g_StripTrailingBlanks,
613 &g_AdjustTrailingLines,
614 &g_SvnKeywords,
615 &g_SvnNoExecutable,
616 &g_SvnSyncProcess,
617 &g_UnicodeChecks,
618 &g_Copyright_SemicolonComment,
619};
620
621static PCSCMREWRITERCFG const g_apRewritersFor_Java[] =
622{
623 &g_ForceNativeEol,
624 &g_ExpandTabs,
625 &g_StripTrailingBlanks,
626 &g_AdjustTrailingLines,
627 &g_SvnNoExecutable,
628 &g_SvnKeywords,
629 &g_SvnSyncProcess,
630 &g_UnicodeChecks,
631 &g_Copyright_CstyleComment,
632 &g_FixFlowerBoxMarkers,
633 &g_Fix_C_and_CPP_Todos,
634};
635
636static PCSCMREWRITERCFG const g_apRewritersFor_ScmSettings[] =
637{
638 &g_ForceNativeEol,
639 &g_ExpandTabs,
640 &g_StripTrailingBlanks,
641 &g_AdjustTrailingLines,
642 &g_SvnNoExecutable,
643 &g_SvnKeywords,
644 &g_SvnSyncProcess,
645 &g_UnicodeChecks,
646 &g_Copyright_HashComment,
647};
648
649static PCSCMREWRITERCFG const g_apRewritersFor_Images[] =
650{
651 &g_SvnNoExecutable,
652 &g_SvnBinary,
653 &g_SvnSyncProcess,
654};
655
656static PCSCMREWRITERCFG const g_apRewritersFor_Xslt[] =
657{
658 &g_ForceNativeEol,
659 &g_ExpandTabs,
660 &g_StripTrailingBlanks,
661 &g_AdjustTrailingLines,
662 &g_SvnNoExecutable,
663 &g_SvnKeywords,
664 &g_SvnSyncProcess,
665 &g_UnicodeChecks,
666 &g_Copyright_XmlComment,
667};
668
669static PCSCMREWRITERCFG const g_apRewritersFor_Xml[] =
670{
671 &g_ForceNativeEol,
672 &g_ExpandTabs,
673 &g_StripTrailingBlanks,
674 &g_AdjustTrailingLines,
675 &g_SvnNoExecutable,
676 &g_SvnKeywords,
677 &g_SvnSyncProcess,
678 &g_UnicodeChecks,
679 &g_Copyright_XmlComment,
680};
681
682static PCSCMREWRITERCFG const g_apRewritersFor_Wix[] =
683{
684 &g_ForceNativeEol,
685 &g_ExpandTabs,
686 &g_StripTrailingBlanks,
687 &g_AdjustTrailingLines,
688 &g_SvnNoExecutable,
689 &g_SvnKeywords,
690 &g_SvnSyncProcess,
691 &g_UnicodeChecks,
692 &g_Copyright_XmlComment,
693};
694
695static PCSCMREWRITERCFG const g_apRewritersFor_QtProject[] =
696{
697 &g_ForceNativeEol,
698 &g_StripTrailingBlanks,
699 &g_AdjustTrailingLines,
700 &g_SvnNoExecutable,
701 &g_SvnKeywords,
702 &g_SvnSyncProcess,
703 &g_UnicodeChecks,
704 &g_Copyright_HashComment,
705};
706
707static PCSCMREWRITERCFG const g_apRewritersFor_QtResourceFiles[] =
708{
709 &g_ForceNativeEol,
710 &g_SvnNoExecutable,
711 &g_SvnKeywords,
712 &g_SvnSyncProcess,
713 &g_UnicodeChecks,
714 /** @todo figure out copyright for Qt resource XML files. */
715};
716
717static PCSCMREWRITERCFG const g_apRewritersFor_QtTranslations[] =
718{
719 &g_ForceNativeEol,
720 &g_SvnNoExecutable,
721};
722
723static PCSCMREWRITERCFG const g_apRewritersFor_QtUiFiles[] =
724{
725 &g_ForceNativeEol,
726 &g_SvnNoExecutable,
727 &g_SvnKeywords,
728 &g_SvnSyncProcess,
729 &g_UnicodeChecks,
730 /** @todo copyright is in an XML 'comment' element. */
731};
732
733static PCSCMREWRITERCFG const g_apRewritersFor_SifFiles[] =
734{
735 &g_ForceCRLF,
736 &g_ExpandTabs,
737 &g_StripTrailingBlanks,
738 &g_AdjustTrailingLines,
739 &g_SvnKeywords,
740 &g_SvnNoExecutable,
741 &g_SvnSyncProcess,
742 &g_UnicodeChecks,
743 &g_Copyright_SemicolonComment,
744};
745
746static PCSCMREWRITERCFG const g_apRewritersFor_SqlFiles[] =
747{
748 &g_ForceNativeEol,
749 &g_ExpandTabs,
750 &g_StripTrailingBlanks,
751 &g_AdjustTrailingLines,
752 &g_SvnKeywords,
753 &g_SvnNoExecutable,
754 &g_SvnSyncProcess,
755 &g_UnicodeChecks,
756 &g_Copyright_SqlComment,
757};
758
759static PCSCMREWRITERCFG const g_apRewritersFor_GnuAsm[] =
760{
761 &g_ForceNativeEol,
762 &g_ExpandTabs,
763 &g_StripTrailingBlanks,
764 &g_AdjustTrailingLines,
765 &g_SvnKeywords,
766 &g_SvnNoExecutable,
767 &g_SvnSyncProcess,
768 &g_UnicodeChecks,
769 &g_Copyright_CstyleComment,
770};
771
772static PCSCMREWRITERCFG const g_apRewritersFor_TextFiles[] =
773{
774 &g_ForceNativeEol,
775 &g_StripTrailingBlanks,
776 &g_SvnKeywords,
777 &g_SvnNoExecutable,
778 &g_SvnSyncProcess,
779 &g_UnicodeChecks,
780 /** @todo check for plain copyright + license in text files. */
781};
782
783static PCSCMREWRITERCFG const g_apRewritersFor_PlainTextFiles[] =
784{
785 &g_ForceNativeEol,
786 &g_StripTrailingBlanks,
787 &g_SvnKeywords,
788 &g_SvnNoExecutable,
789 &g_SvnSyncProcess,
790 &g_UnicodeChecks,
791};
792
793static PCSCMREWRITERCFG const g_apRewritersFor_BinaryFiles[] =
794{
795 &g_SvnBinary,
796 &g_SvnSyncProcess,
797};
798
799static PCSCMREWRITERCFG const g_apRewritersFor_FileLists[] = /* both makefile and shell script */
800{
801 &g_ForceLF,
802 &g_ExpandTabs,
803 &g_StripTrailingBlanks,
804 &g_AdjustTrailingLines,
805 &g_SvnSyncProcess,
806 &g_UnicodeChecks,
807 &g_Copyright_HashComment,
808};
809
810
811/**
812 * Array of standard rewriter configurations.
813 */
814static SCMCFGENTRY const g_aConfigs[] =
815{
816#define SCM_CFG_ENTRY(a_szName, a_aRewriters, a_fBinary, a_szFilePatterns) \
817 { RT_ELEMENTS(a_aRewriters), &a_aRewriters[0], a_fBinary, a_szFilePatterns, a_szName }
818 SCM_CFG_ENTRY("kup", g_apRewritersFor_Makefile_kup, false, "Makefile.kup" ),
819 SCM_CFG_ENTRY("kmk", g_apRewritersFor_Makefile_kmk, false, "*.kmk" ),
820 SCM_CFG_ENTRY("c", g_apRewritersFor_C_and_CPP, false, "*.c|*.cpp|*.C|*.CPP|*.cxx|*.cc|*.m|*.mm|*.lds" ),
821 SCM_CFG_ENTRY("h", g_apRewritersFor_H_and_HPP, false, "*.h|*.hpp" ),
822 SCM_CFG_ENTRY("rc", g_apRewritersFor_RC, false, "*.rc" ),
823 SCM_CFG_ENTRY("asm", g_apRewritersFor_ASM, false, "*.asm|*.mac|*.inc" ),
824 SCM_CFG_ENTRY("dtrace", g_apRewritersFor_DTrace, false, "*.d" ),
825 SCM_CFG_ENTRY("def", g_apRewritersFor_DEF, false, "*.def" ),
826 SCM_CFG_ENTRY("iasl", g_apRewritersFor_DSL, false, "*.dsl" ),
827 SCM_CFG_ENTRY("shell", g_apRewritersFor_ShellScripts, false, "*.sh|configure" ),
828 SCM_CFG_ENTRY("batch", g_apRewritersFor_BatchFiles, false, "*.bat|*.cmd|*.btm" ),
829 SCM_CFG_ENTRY("vbs", g_apRewritersFor_BasicScripts, false, "*.vbs|*.vb" ),
830 SCM_CFG_ENTRY("sed", g_apRewritersFor_SedScripts, false, "*.sed" ),
831 SCM_CFG_ENTRY("python", g_apRewritersFor_Python, false, "*.py" ),
832 SCM_CFG_ENTRY("perl", g_apRewritersFor_Perl, false, "*.pl|*.pm" ),
833 SCM_CFG_ENTRY("drvinf", g_apRewritersFor_DriverInfFiles, false, "*.inf" ),
834 SCM_CFG_ENTRY("nsis", g_apRewritersFor_NsisFiles, false, "*.nsh|*.nsi|*.nsis" ),
835 SCM_CFG_ENTRY("java", g_apRewritersFor_Java, false, "*.java" ),
836 SCM_CFG_ENTRY("scm", g_apRewritersFor_ScmSettings, false, "*.scm-settings" ),
837 SCM_CFG_ENTRY("image", g_apRewritersFor_Images, true, "*.png|*.bmp|*.jpg|*.pnm|*.ico|*.icns|*.tiff|*.tif|"
838 "*.xcf|*.gif|*.jar|*.dll|*.exe|*.ttf|*.woff|*.woff2" ),
839 SCM_CFG_ENTRY("xslt", g_apRewritersFor_Xslt, false, "*.xsl" ),
840 SCM_CFG_ENTRY("xml", g_apRewritersFor_Xml, false, "*.xml|*.dist|*.qhcp" ),
841 SCM_CFG_ENTRY("wix", g_apRewritersFor_Wix, false, "*.wxi|*.wxs|*.wxl" ),
842 SCM_CFG_ENTRY("qt-pro", g_apRewritersFor_QtProject, false, "*.pro" ),
843 SCM_CFG_ENTRY("qt-rc", g_apRewritersFor_QtResourceFiles, false, "*.qrc" ),
844 SCM_CFG_ENTRY("qt-ts", g_apRewritersFor_QtTranslations, false, "*.ts" ),
845 SCM_CFG_ENTRY("qt-ui", g_apRewritersFor_QtUiFiles, false, "*.ui" ),
846 SCM_CFG_ENTRY("sif", g_apRewritersFor_SifFiles, false, "*.sif" ),
847 SCM_CFG_ENTRY("sql", g_apRewritersFor_SqlFiles, false, "*.pgsql|*.sql" ),
848 SCM_CFG_ENTRY("gas", g_apRewritersFor_GnuAsm, false, "*.S" ),
849 SCM_CFG_ENTRY("binary", g_apRewritersFor_BinaryFiles, true, "*.bin|*.pdf|*.zip|*.bz2|*.gz" ),
850 /* These should be be last: */
851 SCM_CFG_ENTRY("make", g_apRewritersFor_OtherMakefiles, false, "Makefile|makefile|GNUmakefile|SMakefile|Makefile.am|Makefile.in|*.cmake|*.gmk" ),
852 SCM_CFG_ENTRY("text", g_apRewritersFor_TextFiles, false, "*.txt|README*|readme*|ReadMe*|NOTE*|TODO*" ),
853 SCM_CFG_ENTRY("plaintext", g_apRewritersFor_PlainTextFiles, false, "LICENSE|ChangeLog|FAQ|AUTHORS|INSTALL|NEWS" ),
854 SCM_CFG_ENTRY("file-list", g_apRewritersFor_FileLists, false, "files_*" ),
855};
856
857
858
859/* -=-=-=-=-=- settings -=-=-=-=-=- */
860
861/**
862 * Delete the given config entry.
863 *
864 * @param pEntry The configuration entry to delete.
865 */
866static void scmCfgEntryDelete(PSCMCFGENTRY pEntry)
867{
868 RTMemFree((void *)pEntry->paRewriters);
869 pEntry->paRewriters = NULL;
870 RTMemFree(pEntry);
871}
872
873/**
874 * Create a new configuration entry.
875 *
876 * @returns The new entry. NULL if out of memory.
877 * @param pEntry The configuration entry to duplicate.
878 */
879static PSCMCFGENTRY scmCfgEntryNew(void)
880{
881 PSCMCFGENTRY pNew = (PSCMCFGENTRY)RTMemAlloc(sizeof(*pNew));
882 if (pNew)
883 {
884 pNew->pszName = "custom";
885 pNew->pszFilePattern = "custom";
886 pNew->cRewriters = 0;
887 pNew->paRewriters = NULL;
888 pNew->fBinary = false;
889 }
890 return pNew;
891}
892
893/**
894 * Duplicate the given config entry.
895 *
896 * @returns The duplicate. NULL if out of memory.
897 * @param pEntry The configuration entry to duplicate.
898 */
899static PSCMCFGENTRY scmCfgEntryDup(PCSCMCFGENTRY pEntry)
900{
901 if (pEntry)
902 {
903 PSCMCFGENTRY pDup = (PSCMCFGENTRY)RTMemDup(pEntry, sizeof(*pEntry));
904 if (pDup)
905 {
906 size_t cbSrcRewriters = sizeof(pEntry->paRewriters[0]) * pEntry->cRewriters;
907 size_t cbDstRewriters = sizeof(pEntry->paRewriters[0]) * RT_ALIGN_Z(pEntry->cRewriters, 8);
908 pDup->paRewriters = (PCSCMREWRITERCFG const *)RTMemDupEx(pEntry->paRewriters, cbSrcRewriters,
909 cbDstRewriters - cbSrcRewriters);
910 if (pDup->paRewriters)
911 return pDup;
912
913 RTMemFree(pDup);
914 }
915 return NULL;
916 }
917 return scmCfgEntryNew();
918}
919
920/**
921 * Adds a rewriter action to the given config entry (--add-action).
922 *
923 * @returns VINF_SUCCESS.
924 * @param pEntry The configuration entry.
925 * @param pAction The rewriter action to add.
926 */
927static int scmCfgEntryAddAction(PSCMCFGENTRY pEntry, PCSCMREWRITERCFG pAction)
928{
929 PCSCMREWRITERCFG *paRewriters = (PCSCMREWRITERCFG *)pEntry->paRewriters;
930 if (pEntry->cRewriters % 8 == 0)
931 {
932 size_t cbRewriters = sizeof(pEntry->paRewriters[0]) * RT_ALIGN_Z((pEntry->cRewriters + 1), 8);
933 void *pvNew = RTMemRealloc(paRewriters, cbRewriters);
934 if (pvNew)
935 pEntry->paRewriters = paRewriters = (PCSCMREWRITERCFG *)pvNew;
936 else
937 return VERR_NO_MEMORY;
938 }
939
940 paRewriters[pEntry->cRewriters++] = pAction;
941 return VINF_SUCCESS;
942}
943
944/**
945 * Delets an rewriter action from the given config entry (--del-action).
946 *
947 * @param pEntry The configuration entry.
948 * @param pAction The rewriter action to remove.
949 */
950static void scmCfgEntryDelAction(PSCMCFGENTRY pEntry, PCSCMREWRITERCFG pAction)
951{
952 PCSCMREWRITERCFG *paRewriters = (PCSCMREWRITERCFG *)pEntry->paRewriters;
953 size_t const cEntries = pEntry->cRewriters;
954 size_t iDst = 0;
955 for (size_t iSrc = 0; iSrc < cEntries; iSrc++)
956 {
957 PCSCMREWRITERCFG pCurAction = paRewriters[iSrc];
958 if (pCurAction != pAction)
959 paRewriters[iDst++] = pCurAction;
960 }
961 pEntry->cRewriters = iDst;
962}
963
964/**
965 * Init a settings structure with settings from @a pSrc.
966 *
967 * @returns IPRT status code
968 * @param pSettings The settings.
969 * @param pSrc The source settings.
970 */
971static int scmSettingsBaseInitAndCopy(PSCMSETTINGSBASE pSettings, PCSCMSETTINGSBASE pSrc)
972{
973 *pSettings = *pSrc;
974
975 int rc = RTStrDupEx(&pSettings->pszFilterFiles, pSrc->pszFilterFiles);
976 if (RT_SUCCESS(rc))
977 {
978 rc = RTStrDupEx(&pSettings->pszFilterOutFiles, pSrc->pszFilterOutFiles);
979 if (RT_SUCCESS(rc))
980 {
981 rc = RTStrDupEx(&pSettings->pszFilterOutDirs, pSrc->pszFilterOutDirs);
982 if (RT_SUCCESS(rc))
983 {
984 rc = RTStrDupEx(&pSettings->pszGuardPrefix, pSrc->pszGuardPrefix);
985 if (RT_SUCCESS(rc))
986 {
987 if (pSrc->pszGuardRelativeToDir)
988 rc = RTStrDupEx(&pSettings->pszGuardRelativeToDir, pSrc->pszGuardRelativeToDir);
989 if (RT_SUCCESS(rc))
990 {
991
992 if (!pSrc->fFreeTreatAs)
993 return VINF_SUCCESS;
994
995 pSettings->pTreatAs = scmCfgEntryDup(pSrc->pTreatAs);
996 if (pSettings->pTreatAs)
997 return VINF_SUCCESS;
998
999 RTStrFree(pSettings->pszGuardRelativeToDir);
1000 }
1001 RTStrFree(pSettings->pszGuardPrefix);
1002 }
1003 }
1004 RTStrFree(pSettings->pszFilterOutFiles);
1005 }
1006 RTStrFree(pSettings->pszFilterFiles);
1007 }
1008
1009 pSettings->pszGuardRelativeToDir = NULL;
1010 pSettings->pszGuardPrefix = NULL;
1011 pSettings->pszFilterFiles = NULL;
1012 pSettings->pszFilterOutFiles = NULL;
1013 pSettings->pszFilterOutDirs = NULL;
1014 pSettings->pTreatAs = NULL;
1015 return rc;
1016}
1017
1018/**
1019 * Init a settings structure.
1020 *
1021 * @returns IPRT status code
1022 * @param pSettings The settings.
1023 */
1024static int scmSettingsBaseInit(PSCMSETTINGSBASE pSettings)
1025{
1026 return scmSettingsBaseInitAndCopy(pSettings, &g_Defaults);
1027}
1028
1029/**
1030 * Deletes the settings, i.e. free any dynamically allocated content.
1031 *
1032 * @param pSettings The settings.
1033 */
1034static void scmSettingsBaseDelete(PSCMSETTINGSBASE pSettings)
1035{
1036 if (pSettings)
1037 {
1038 Assert(pSettings->cchTab != UINT8_MAX);
1039 pSettings->cchTab = UINT8_MAX;
1040
1041 RTStrFree(pSettings->pszGuardPrefix);
1042 RTStrFree(pSettings->pszGuardRelativeToDir);
1043 RTStrFree(pSettings->pszFilterFiles);
1044 RTStrFree(pSettings->pszFilterOutFiles);
1045 RTStrFree(pSettings->pszFilterOutDirs);
1046 if (pSettings->fFreeTreatAs)
1047 scmCfgEntryDelete((PSCMCFGENTRY)pSettings->pTreatAs);
1048
1049 pSettings->pszGuardPrefix = NULL;
1050 pSettings->pszGuardRelativeToDir = NULL;
1051 pSettings->pszFilterOutDirs = NULL;
1052 pSettings->pszFilterOutFiles = NULL;
1053 pSettings->pszFilterFiles = NULL;
1054 pSettings->pTreatAs = NULL;
1055 pSettings->fFreeTreatAs = false;
1056 }
1057}
1058
1059/**
1060 * Processes a RTGetOpt result.
1061 *
1062 * @retval VINF_SUCCESS if handled.
1063 * @retval VERR_OUT_OF_RANGE if the option value was out of range.
1064 * @retval VERR_GETOPT_UNKNOWN_OPTION if the option was not recognized.
1065 *
1066 * @param pSettings The settings to change.
1067 * @param rc The RTGetOpt return value.
1068 * @param pValueUnion The RTGetOpt value union.
1069 * @param pchDir The absolute path to the directory relative
1070 * components in pchLine should be relative to.
1071 * @param cchDir The length of the @a pchDir string.
1072 */
1073static int scmSettingsBaseHandleOpt(PSCMSETTINGSBASE pSettings, int rc, PRTGETOPTUNION pValueUnion,
1074 const char *pchDir, size_t cchDir)
1075{
1076 Assert(pchDir[cchDir - 1] == '/');
1077
1078 switch (rc)
1079 {
1080 case SCMOPT_CONVERT_EOL:
1081 pSettings->fConvertEol = true;
1082 return VINF_SUCCESS;
1083 case SCMOPT_NO_CONVERT_EOL:
1084 pSettings->fConvertEol = false;
1085 return VINF_SUCCESS;
1086
1087 case SCMOPT_CONVERT_TABS:
1088 pSettings->fConvertTabs = true;
1089 return VINF_SUCCESS;
1090 case SCMOPT_NO_CONVERT_TABS:
1091 pSettings->fConvertTabs = false;
1092 return VINF_SUCCESS;
1093
1094 case SCMOPT_FORCE_FINAL_EOL:
1095 pSettings->fForceFinalEol = true;
1096 return VINF_SUCCESS;
1097 case SCMOPT_NO_FORCE_FINAL_EOL:
1098 pSettings->fForceFinalEol = false;
1099 return VINF_SUCCESS;
1100
1101 case SCMOPT_FORCE_TRAILING_LINE:
1102 pSettings->fForceTrailingLine = true;
1103 return VINF_SUCCESS;
1104 case SCMOPT_NO_FORCE_TRAILING_LINE:
1105 pSettings->fForceTrailingLine = false;
1106 return VINF_SUCCESS;
1107
1108
1109 case SCMOPT_STRIP_TRAILING_BLANKS:
1110 pSettings->fStripTrailingBlanks = true;
1111 return VINF_SUCCESS;
1112 case SCMOPT_NO_STRIP_TRAILING_BLANKS:
1113 pSettings->fStripTrailingBlanks = false;
1114 return VINF_SUCCESS;
1115
1116 case SCMOPT_MIN_BLANK_LINES_BEFORE_FLOWER_BOX_MARKERS:
1117 pSettings->cMinBlankLinesBeforeFlowerBoxMakers = pValueUnion->u8;
1118 return VINF_SUCCESS;
1119
1120
1121 case SCMOPT_STRIP_TRAILING_LINES:
1122 pSettings->fStripTrailingLines = true;
1123 return VINF_SUCCESS;
1124 case SCMOPT_NO_STRIP_TRAILING_LINES:
1125 pSettings->fStripTrailingLines = false;
1126 return VINF_SUCCESS;
1127
1128 case SCMOPT_FIX_FLOWER_BOX_MARKERS:
1129 pSettings->fFixFlowerBoxMarkers = true;
1130 return VINF_SUCCESS;
1131 case SCMOPT_NO_FIX_FLOWER_BOX_MARKERS:
1132 pSettings->fFixFlowerBoxMarkers = false;
1133 return VINF_SUCCESS;
1134
1135 case SCMOPT_FIX_HEADER_GUARDS:
1136 pSettings->fFixHeaderGuards = true;
1137 return VINF_SUCCESS;
1138 case SCMOPT_NO_FIX_HEADER_GUARDS:
1139 pSettings->fFixHeaderGuards = false;
1140 return VINF_SUCCESS;
1141
1142 case SCMOPT_PRAGMA_ONCE:
1143 pSettings->fPragmaOnce = true;
1144 return VINF_SUCCESS;
1145 case SCMOPT_NO_PRAGMA_ONCE:
1146 pSettings->fPragmaOnce = false;
1147 return VINF_SUCCESS;
1148
1149 case SCMOPT_FIX_HEADER_GUARD_ENDIF:
1150 pSettings->fFixHeaderGuardEndif = true;
1151 return VINF_SUCCESS;
1152 case SCMOPT_NO_FIX_HEADER_GUARD_ENDIF:
1153 pSettings->fFixHeaderGuardEndif = false;
1154 return VINF_SUCCESS;
1155
1156 case SCMOPT_ENDIF_GUARD_COMMENT:
1157 pSettings->fEndifGuardComment = true;
1158 return VINF_SUCCESS;
1159 case SCMOPT_NO_ENDIF_GUARD_COMMENT:
1160 pSettings->fEndifGuardComment = false;
1161 return VINF_SUCCESS;
1162
1163 case SCMOPT_GUARD_PREFIX:
1164 RTStrFree(pSettings->pszGuardPrefix);
1165 pSettings->pszGuardPrefix = NULL;
1166 return RTStrDupEx(&pSettings->pszGuardPrefix, pValueUnion->psz);
1167
1168 case SCMOPT_GUARD_RELATIVE_TO_DIR:
1169 RTStrFree(pSettings->pszGuardRelativeToDir);
1170 pSettings->pszGuardRelativeToDir = NULL;
1171 if (*pValueUnion->psz != '\0')
1172 {
1173 if ( strcmp(pValueUnion->psz, "{dir}") == 0
1174 || strcmp(pValueUnion->psz, "{parent}") == 0)
1175 return RTStrDupEx(&pSettings->pszGuardRelativeToDir, pValueUnion->psz);
1176 if (cchDir == 1 && *pchDir == '/')
1177 {
1178 pSettings->pszGuardRelativeToDir = RTPathAbsDup(pValueUnion->psz);
1179 if (pSettings->pszGuardRelativeToDir)
1180 return VINF_SUCCESS;
1181 }
1182 else
1183 {
1184 char *pszDir = RTStrDupN(pchDir, cchDir);
1185 if (pszDir)
1186 {
1187 pSettings->pszGuardRelativeToDir = RTPathAbsExDup(pszDir, pValueUnion->psz, RTPATH_STR_F_STYLE_HOST);
1188 RTStrFree(pszDir);
1189 if (pSettings->pszGuardRelativeToDir)
1190 return VINF_SUCCESS;
1191 }
1192 }
1193 RTMsgError("Failed to abspath --guard-relative-to-dir value '%s' - probably out of memory\n", pValueUnion->psz);
1194 return VERR_NO_STR_MEMORY;
1195 }
1196 return VINF_SUCCESS;
1197
1198 case SCMOPT_FIX_TODOS:
1199 pSettings->fFixTodos = true;
1200 return VINF_SUCCESS;
1201 case SCMOPT_NO_FIX_TODOS:
1202 pSettings->fFixTodos = false;
1203 return VINF_SUCCESS;
1204
1205 case SCMOPT_FIX_ERR_H:
1206 pSettings->fFixErrH = true;
1207 return VINF_SUCCESS;
1208 case SCMOPT_NO_FIX_ERR_H:
1209 pSettings->fFixErrH = false;
1210 return VINF_SUCCESS;
1211
1212 case SCMOPT_ONLY_GUEST_HOST_PAGE:
1213 pSettings->fOnlyGuestHostPage = true;
1214 return VINF_SUCCESS;
1215 case SCMOPT_NO_PAGE_RESTRICTIONS:
1216 pSettings->fOnlyGuestHostPage = false;
1217 return VINF_SUCCESS;
1218
1219 case SCMOPT_NO_ASM_MEM_PAGE_USE:
1220 pSettings->fNoASMMemPageUse = true;
1221 return VINF_SUCCESS;
1222 case SCMOPT_UNRESTRICTED_ASM_MEM_PAGE_USE:
1223 pSettings->fNoASMMemPageUse = false;
1224 return VINF_SUCCESS;
1225
1226 case SCMOPT_NO_RC_USE:
1227 pSettings->fOnlyHrcVrcInsteadOfRc = true;
1228 return VINF_SUCCESS;
1229 case SCMOPT_UNRESTRICTED_RC_USE:
1230 pSettings->fOnlyHrcVrcInsteadOfRc = false;
1231 return VINF_SUCCESS;
1232
1233 case SCMOPT_STANDARIZE_KMK:
1234 pSettings->fStandarizeKmk = true;
1235 return VINF_SUCCESS;
1236 case SCMOPT_NO_STANDARIZE_KMK:
1237 pSettings->fStandarizeKmk = false;
1238 return VINF_SUCCESS;
1239
1240 case SCMOPT_UPDATE_COPYRIGHT_YEAR:
1241 pSettings->fUpdateCopyrightYear = true;
1242 return VINF_SUCCESS;
1243 case SCMOPT_NO_UPDATE_COPYRIGHT_YEAR:
1244 pSettings->fUpdateCopyrightYear = false;
1245 return VINF_SUCCESS;
1246
1247 case SCMOPT_EXTERNAL_COPYRIGHT:
1248 pSettings->fExternalCopyright = true;
1249 return VINF_SUCCESS;
1250 case SCMOPT_NO_EXTERNAL_COPYRIGHT:
1251 pSettings->fExternalCopyright = false;
1252 return VINF_SUCCESS;
1253
1254 case SCMOPT_NO_UPDATE_LICENSE:
1255 pSettings->enmUpdateLicense = kScmLicense_LeaveAlone;
1256 return VINF_SUCCESS;
1257 case SCMOPT_LICENSE_OSE_GPL:
1258 pSettings->enmUpdateLicense = kScmLicense_OseGpl;
1259 return VINF_SUCCESS;
1260 case SCMOPT_LICENSE_OSE_DUAL_GPL_CDDL:
1261 pSettings->enmUpdateLicense = kScmLicense_OseDualGplCddl;
1262 return VINF_SUCCESS;
1263 case SCMOPT_LICENSE_OSE_CDDL:
1264 pSettings->enmUpdateLicense = kScmLicense_OseCddl;
1265 return VINF_SUCCESS;
1266 case SCMOPT_LICENSE_LGPL:
1267 pSettings->enmUpdateLicense = kScmLicense_Lgpl;
1268 return VINF_SUCCESS;
1269 case SCMOPT_LICENSE_MIT:
1270 pSettings->enmUpdateLicense = kScmLicense_Mit;
1271 return VINF_SUCCESS;
1272 case SCMOPT_LICENSE_BASED_ON_MIT:
1273 pSettings->enmUpdateLicense = kScmLicense_BasedOnMit;
1274 return VINF_SUCCESS;
1275
1276 case SCMOPT_LGPL_DISCLAIMER:
1277 pSettings->fLgplDisclaimer = true;
1278 return VINF_SUCCESS;
1279 case SCMOPT_NO_LGPL_DISCLAIMER:
1280 pSettings->fLgplDisclaimer = false;
1281 return VINF_SUCCESS;
1282
1283 case SCMOPT_ONLY_SVN_DIRS:
1284 pSettings->fOnlySvnDirs = true;
1285 return VINF_SUCCESS;
1286 case SCMOPT_NOT_ONLY_SVN_DIRS:
1287 pSettings->fOnlySvnDirs = false;
1288 return VINF_SUCCESS;
1289
1290 case SCMOPT_ONLY_SVN_FILES:
1291 pSettings->fOnlySvnFiles = true;
1292 return VINF_SUCCESS;
1293 case SCMOPT_NOT_ONLY_SVN_FILES:
1294 pSettings->fOnlySvnFiles = false;
1295 return VINF_SUCCESS;
1296
1297 case SCMOPT_SET_SVN_EOL:
1298 pSettings->fSetSvnEol = true;
1299 return VINF_SUCCESS;
1300 case SCMOPT_DONT_SET_SVN_EOL:
1301 pSettings->fSetSvnEol = false;
1302 return VINF_SUCCESS;
1303
1304 case SCMOPT_SET_SVN_EXECUTABLE:
1305 pSettings->fSetSvnExecutable = true;
1306 return VINF_SUCCESS;
1307 case SCMOPT_DONT_SET_SVN_EXECUTABLE:
1308 pSettings->fSetSvnExecutable = false;
1309 return VINF_SUCCESS;
1310
1311 case SCMOPT_SET_SVN_KEYWORDS:
1312 pSettings->fSetSvnKeywords = true;
1313 return VINF_SUCCESS;
1314 case SCMOPT_DONT_SET_SVN_KEYWORDS:
1315 pSettings->fSetSvnKeywords = false;
1316 return VINF_SUCCESS;
1317
1318 case SCMOPT_SKIP_SVN_SYNC_PROCESS:
1319 pSettings->fSkipSvnSyncProcess = true;
1320 return VINF_SUCCESS;
1321 case SCMOPT_DONT_SKIP_SVN_SYNC_PROCESS:
1322 pSettings->fSkipSvnSyncProcess = false;
1323 return VINF_SUCCESS;
1324
1325 case SCMOPT_SKIP_UNICODE_CHECKS:
1326 pSettings->fSkipUnicodeChecks = true;
1327 return VINF_SUCCESS;
1328 case SCMOPT_DONT_SKIP_UNICODE_CHECKS:
1329 pSettings->fSkipUnicodeChecks = false;
1330 return VINF_SUCCESS;
1331
1332 case SCMOPT_TAB_SIZE:
1333 if ( pValueUnion->u8 < 1
1334 || pValueUnion->u8 >= RT_ELEMENTS(g_szTabSpaces))
1335 {
1336 RTMsgError("Invalid tab size: %u - must be in {1..%u}\n",
1337 pValueUnion->u8, RT_ELEMENTS(g_szTabSpaces) - 1);
1338 return VERR_OUT_OF_RANGE;
1339 }
1340 pSettings->cchTab = pValueUnion->u8;
1341 return VINF_SUCCESS;
1342
1343 case SCMOPT_WIDTH:
1344 if (pValueUnion->u8 < 20 || pValueUnion->u8 > 200)
1345 {
1346 RTMsgError("Invalid width size: %u - must be in {20..200} range\n", pValueUnion->u8);
1347 return VERR_OUT_OF_RANGE;
1348 }
1349 pSettings->cchWidth = pValueUnion->u8;
1350 return VINF_SUCCESS;
1351
1352 case SCMOPT_FILTER_OUT_DIRS:
1353 case SCMOPT_FILTER_FILES:
1354 case SCMOPT_FILTER_OUT_FILES:
1355 {
1356 char **ppsz = NULL;
1357 switch (rc)
1358 {
1359 case SCMOPT_FILTER_OUT_DIRS: ppsz = &pSettings->pszFilterOutDirs; break;
1360 case SCMOPT_FILTER_FILES: ppsz = &pSettings->pszFilterFiles; break;
1361 case SCMOPT_FILTER_OUT_FILES: ppsz = &pSettings->pszFilterOutFiles; break;
1362 }
1363
1364 /*
1365 * An empty string zaps the current list.
1366 */
1367 if (!*pValueUnion->psz)
1368 return RTStrATruncate(ppsz, 0);
1369
1370 /*
1371 * Non-empty strings are appended to the pattern list.
1372 *
1373 * Strip leading and trailing pattern separators before attempting
1374 * to append it. If it's just separators, don't do anything.
1375 */
1376 const char *pszSrc = pValueUnion->psz;
1377 while (*pszSrc == '|')
1378 pszSrc++;
1379 size_t cchSrc = strlen(pszSrc);
1380 while (cchSrc > 0 && pszSrc[cchSrc - 1] == '|')
1381 cchSrc--;
1382 if (!cchSrc)
1383 return VINF_SUCCESS;
1384
1385 /* Append it pattern by pattern, turning settings-relative paths into absolute ones. */
1386 for (;;)
1387 {
1388 const char *pszEnd = (const char *)memchr(pszSrc, '|', cchSrc);
1389 size_t cchPattern = pszEnd ? pszEnd - pszSrc : cchSrc;
1390 int rc2;
1391 if (*pszSrc == '/')
1392 rc2 = RTStrAAppendExN(ppsz, 3,
1393 "|", *ppsz && **ppsz != '\0' ? (size_t)1 : (size_t)0,
1394 pchDir, cchDir - 1,
1395 pszSrc, cchPattern);
1396 else
1397 rc2 = RTStrAAppendExN(ppsz, 2,
1398 "|", *ppsz && **ppsz != '\0' ? (size_t)1 : (size_t)0,
1399 pszSrc, cchPattern);
1400 if (RT_FAILURE(rc2))
1401 return rc2;
1402
1403 /* next */
1404 cchSrc -= cchPattern;
1405 if (!cchSrc)
1406 return VINF_SUCCESS;
1407 cchSrc -= 1;
1408 pszSrc += cchPattern + 1;
1409 }
1410 /* not reached */
1411#if RT_MSC_PREREQ(RT_MSC_VER_VS2022) /* VS 2022 complains about fall thru, other compilers complains about unreachable code. */
1412 break;
1413#endif
1414 }
1415
1416 case SCMOPT_TREAT_AS:
1417 if (pSettings->fFreeTreatAs)
1418 {
1419 scmCfgEntryDelete((PSCMCFGENTRY)pSettings->pTreatAs);
1420 pSettings->pTreatAs = NULL;
1421 pSettings->fFreeTreatAs = false;
1422 }
1423
1424 if (*pValueUnion->psz)
1425 {
1426 /* first check the names, then patterns (legacy). */
1427 for (size_t iCfg = 0; iCfg < RT_ELEMENTS(g_aConfigs); iCfg++)
1428 if (strcmp(g_aConfigs[iCfg].pszName, pValueUnion->psz) == 0)
1429 {
1430 pSettings->pTreatAs = &g_aConfigs[iCfg];
1431 return VINF_SUCCESS;
1432 }
1433 for (size_t iCfg = 0; iCfg < RT_ELEMENTS(g_aConfigs); iCfg++)
1434 if (RTStrSimplePatternMultiMatch(g_aConfigs[iCfg].pszFilePattern, RTSTR_MAX,
1435 pValueUnion->psz, RTSTR_MAX, NULL))
1436 {
1437 pSettings->pTreatAs = &g_aConfigs[iCfg];
1438 return VINF_SUCCESS;
1439 }
1440 /* Special help for listing the possibilities? */
1441 if (strcmp(pValueUnion->psz, "help") == 0)
1442 {
1443 RTPrintf("Possible --treat-as values:\n");
1444 for (size_t iCfg = 0; iCfg < RT_ELEMENTS(g_aConfigs); iCfg++)
1445 RTPrintf(" %s (%s)\n", g_aConfigs[iCfg].pszName, g_aConfigs[iCfg].pszFilePattern);
1446 }
1447 return VERR_NOT_FOUND;
1448 }
1449
1450 pSettings->pTreatAs = NULL;
1451 return VINF_SUCCESS;
1452
1453 case SCMOPT_ADD_ACTION:
1454 for (uint32_t iAction = 0; iAction < RT_ELEMENTS(g_papRewriterActions); iAction++)
1455 if (strcmp(g_papRewriterActions[iAction]->pszName, pValueUnion->psz) == 0)
1456 {
1457 PSCMCFGENTRY pEntry = (PSCMCFGENTRY)pSettings->pTreatAs;
1458 if (!pSettings->fFreeTreatAs)
1459 {
1460 pEntry = scmCfgEntryDup(pEntry);
1461 if (!pEntry)
1462 return VERR_NO_MEMORY;
1463 pSettings->pTreatAs = pEntry;
1464 pSettings->fFreeTreatAs = true;
1465 }
1466 return scmCfgEntryAddAction(pEntry, g_papRewriterActions[iAction]);
1467 }
1468 RTMsgError("Unknown --add-action value '%s'. Try --help-actions for a list.", pValueUnion->psz);
1469 return VERR_NOT_FOUND;
1470
1471 case SCMOPT_DEL_ACTION:
1472 {
1473 uint32_t cActions = 0;
1474 for (uint32_t iAction = 0; iAction < RT_ELEMENTS(g_papRewriterActions); iAction++)
1475 if (RTStrSimplePatternMatch(pValueUnion->psz, g_papRewriterActions[iAction]->pszName))
1476 {
1477 cActions++;
1478 PSCMCFGENTRY pEntry = (PSCMCFGENTRY)pSettings->pTreatAs;
1479 if (!pSettings->fFreeTreatAs)
1480 {
1481 pEntry = scmCfgEntryDup(pEntry);
1482 if (!pEntry)
1483 return VERR_NO_MEMORY;
1484 pSettings->pTreatAs = pEntry;
1485 pSettings->fFreeTreatAs = true;
1486 }
1487 scmCfgEntryDelAction(pEntry, g_papRewriterActions[iAction]);
1488 if (!strchr(pValueUnion->psz, '*'))
1489 return VINF_SUCCESS;
1490 }
1491 if (cActions > 0)
1492 return VINF_SUCCESS;
1493 RTMsgError("Unknown --del-action value '%s'. Try --help-actions for a list.", pValueUnion->psz);
1494 return VERR_NOT_FOUND;
1495 }
1496
1497 default:
1498 return VERR_GETOPT_UNKNOWN_OPTION;
1499 }
1500}
1501
1502/**
1503 * Parses an option string.
1504 *
1505 * @returns IPRT status code.
1506 * @param pBase The base settings structure to apply the options
1507 * to.
1508 * @param pszOptions The options to parse.
1509 * @param pchDir The absolute path to the directory relative
1510 * components in pchLine should be relative to.
1511 * @param cchDir The length of the @a pchDir string.
1512 */
1513static int scmSettingsBaseParseString(PSCMSETTINGSBASE pBase, const char *pszLine, const char *pchDir, size_t cchDir)
1514{
1515 int cArgs;
1516 char **papszArgs;
1517 int rc = RTGetOptArgvFromString(&papszArgs, &cArgs, pszLine, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH, NULL);
1518 if (RT_SUCCESS(rc))
1519 {
1520 RTGETOPTUNION ValueUnion;
1521 RTGETOPTSTATE GetOptState;
1522 rc = RTGetOptInit(&GetOptState, cArgs, papszArgs, &g_aScmOpts[0], RT_ELEMENTS(g_aScmOpts), 0, 0 /*fFlags*/);
1523 if (RT_SUCCESS(rc))
1524 {
1525 while ((rc = RTGetOpt(&GetOptState, &ValueUnion)) != 0)
1526 {
1527 rc = scmSettingsBaseHandleOpt(pBase, rc, &ValueUnion, pchDir, cchDir);
1528 if (RT_FAILURE(rc))
1529 break;
1530 }
1531 }
1532 RTGetOptArgvFree(papszArgs);
1533 }
1534
1535 return rc;
1536}
1537
1538/**
1539 * Parses an unterminated option string.
1540 *
1541 * @returns IPRT status code.
1542 * @param pBase The base settings structure to apply the options
1543 * to.
1544 * @param pchLine The line.
1545 * @param cchLine The line length.
1546 * @param pchDir The absolute path to the directory relative
1547 * components in pchLine should be relative to.
1548 * @param cchDir The length of the @a pchDir string.
1549 */
1550static int scmSettingsBaseParseStringN(PSCMSETTINGSBASE pBase, const char *pchLine, size_t cchLine,
1551 const char *pchDir, size_t cchDir)
1552{
1553 char *pszLine = RTStrDupN(pchLine, cchLine);
1554 if (!pszLine)
1555 return VERR_NO_MEMORY;
1556 int rc = scmSettingsBaseParseString(pBase, pszLine, pchDir, cchDir);
1557 RTStrFree(pszLine);
1558 return rc;
1559}
1560
1561/**
1562 * Verifies the options string.
1563 *
1564 * @returns IPRT status code.
1565 * @param pszOptions The options to verify .
1566 */
1567static int scmSettingsBaseVerifyString(const char *pszOptions)
1568{
1569 SCMSETTINGSBASE Base;
1570 int rc = scmSettingsBaseInit(&Base);
1571 if (RT_SUCCESS(rc))
1572 {
1573 rc = scmSettingsBaseParseString(&Base, pszOptions, "/", 1);
1574 scmSettingsBaseDelete(&Base);
1575 }
1576 return rc;
1577}
1578
1579/**
1580 * Loads settings found in editor and SCM settings directives within the
1581 * document (@a pStream).
1582 *
1583 * @returns IPRT status code.
1584 * @param pBase The settings base to load settings into.
1585 * @param pStream The stream to scan for settings directives.
1586 */
1587static int scmSettingsBaseLoadFromDocument(PSCMSETTINGSBASE pBase, PSCMSTREAM pStream)
1588{
1589 /** @todo Editor and SCM settings directives in documents. */
1590 RT_NOREF2(pBase, pStream);
1591 return VINF_SUCCESS;
1592}
1593
1594/**
1595 * Creates a new settings file struct, cloning @a pSettings.
1596 *
1597 * @returns IPRT status code.
1598 * @param ppSettings Where to return the new struct.
1599 * @param pSettingsBase The settings to inherit from.
1600 */
1601static int scmSettingsCreate(PSCMSETTINGS *ppSettings, PCSCMSETTINGSBASE pSettingsBase)
1602{
1603 PSCMSETTINGS pSettings = (PSCMSETTINGS)RTMemAlloc(sizeof(*pSettings));
1604 if (!pSettings)
1605 return VERR_NO_MEMORY;
1606 int rc = scmSettingsBaseInitAndCopy(&pSettings->Base, pSettingsBase);
1607 if (RT_SUCCESS(rc))
1608 {
1609 pSettings->pDown = NULL;
1610 pSettings->pUp = NULL;
1611 pSettings->paPairs = NULL;
1612 pSettings->cPairs = 0;
1613 *ppSettings = pSettings;
1614 return VINF_SUCCESS;
1615 }
1616 RTMemFree(pSettings);
1617 return rc;
1618}
1619
1620/**
1621 * Destroys a settings structure.
1622 *
1623 * @param pSettings The settings structure to destroy. NULL is OK.
1624 */
1625static void scmSettingsDestroy(PSCMSETTINGS pSettings)
1626{
1627 if (pSettings)
1628 {
1629 scmSettingsBaseDelete(&pSettings->Base);
1630 for (size_t i = 0; i < pSettings->cPairs; i++)
1631 {
1632 RTStrFree(pSettings->paPairs[i].pszPattern);
1633 RTStrFree(pSettings->paPairs[i].pszOptions);
1634 RTStrFree(pSettings->paPairs[i].pszRelativeTo);
1635 pSettings->paPairs[i].pszPattern = NULL;
1636 pSettings->paPairs[i].pszOptions = NULL;
1637 pSettings->paPairs[i].pszRelativeTo = NULL;
1638 }
1639 RTMemFree(pSettings->paPairs);
1640 pSettings->paPairs = NULL;
1641 RTMemFree(pSettings);
1642 }
1643}
1644
1645/**
1646 * Adds a pattern/options pair to the settings structure.
1647 *
1648 * @returns IPRT status code.
1649 * @param pSettings The settings.
1650 * @param pchLine The line containing the unparsed pair.
1651 * @param cchLine The length of the line.
1652 * @param offColon The offset of the colon into the line.
1653 * @param pchDir The absolute path to the directory relative
1654 * components in pchLine should be relative to.
1655 * @param cchDir The length of the @a pchDir string.
1656 */
1657static int scmSettingsAddPair(PSCMSETTINGS pSettings, const char *pchLine, size_t cchLine, size_t offColon,
1658 const char *pchDir, size_t cchDir)
1659{
1660 Assert(pchLine[offColon] == ':' && offColon < cchLine);
1661 Assert(pchDir[cchDir - 1] == '/');
1662
1663 /*
1664 * Split the string.
1665 */
1666 size_t cchPattern = offColon;
1667 size_t cchOptions = cchLine - cchPattern - 1;
1668
1669 /* strip spaces everywhere */
1670 while (cchPattern > 0 && RT_C_IS_SPACE(pchLine[cchPattern - 1]))
1671 cchPattern--;
1672 while (cchPattern > 0 && RT_C_IS_SPACE(*pchLine))
1673 cchPattern--, pchLine++;
1674
1675 const char *pchOptions = &pchLine[offColon + 1];
1676 while (cchOptions > 0 && RT_C_IS_SPACE(pchOptions[cchOptions - 1]))
1677 cchOptions--;
1678 while (cchOptions > 0 && RT_C_IS_SPACE(*pchOptions))
1679 cchOptions--, pchOptions++;
1680
1681 /* Quietly ignore empty patterns and empty options. */
1682 if (!cchOptions || !cchPattern)
1683 return VINF_SUCCESS;
1684
1685 /*
1686 * Prepair the pair and verify the option string.
1687 */
1688 uint32_t iPair = pSettings->cPairs;
1689 if ((iPair % 32) == 0)
1690 {
1691 void *pvNew = RTMemRealloc(pSettings->paPairs, (iPair + 32) * sizeof(pSettings->paPairs[0]));
1692 if (!pvNew)
1693 return VERR_NO_MEMORY;
1694 pSettings->paPairs = (PSCMPATRNOPTPAIR)pvNew;
1695 }
1696
1697 pSettings->paPairs[iPair].pszPattern = RTStrDupN(pchLine, cchPattern);
1698 pSettings->paPairs[iPair].pszOptions = RTStrDupN(pchOptions, cchOptions);
1699 pSettings->paPairs[iPair].pszRelativeTo = RTStrDupN(pchDir, cchDir);
1700 int rc;
1701 if ( pSettings->paPairs[iPair].pszPattern
1702 && pSettings->paPairs[iPair].pszOptions
1703 && pSettings->paPairs[iPair].pszRelativeTo)
1704 rc = scmSettingsBaseVerifyString(pSettings->paPairs[iPair].pszOptions);
1705 else
1706 rc = VERR_NO_MEMORY;
1707
1708 /*
1709 * If it checked out fine, expand any relative paths in the pattern.
1710 */
1711 if (RT_SUCCESS(rc))
1712 {
1713 size_t cPattern = 1;
1714 size_t cRelativePaths = 0;
1715 const char *pszSrc = pSettings->paPairs[iPair].pszPattern;
1716 for (;;)
1717 {
1718 if (*pszSrc == '/')
1719 cRelativePaths++;
1720 pszSrc = strchr(pszSrc, '|');
1721 if (!pszSrc)
1722 break;
1723 pszSrc++;
1724 cPattern++;
1725 }
1726 pSettings->paPairs[iPair].fMultiPattern = cPattern > 1;
1727 if (cRelativePaths > 0)
1728 {
1729 char *pszNewPattern = RTStrAlloc(cchPattern + cRelativePaths * (cchDir - 1) + 1);
1730 if (pszNewPattern)
1731 {
1732 char *pszDst = pszNewPattern;
1733 pszSrc = pSettings->paPairs[iPair].pszPattern;
1734 for (;;)
1735 {
1736 if (*pszSrc == '/')
1737 {
1738 memcpy(pszDst, pchDir, cchDir);
1739 pszDst += cchDir;
1740 pszSrc += 1;
1741 }
1742
1743 /* Look for the next relative path. */
1744 const char *pszSrcNext = strchr(pszSrc, '|');
1745 while (pszSrcNext && pszSrcNext[1] != '/')
1746 pszSrcNext = strchr(pszSrcNext, '|');
1747 if (!pszSrcNext)
1748 break;
1749
1750 /* Copy stuff between current and the next path. */
1751 pszSrcNext++;
1752 memcpy(pszDst, pszSrc, pszSrcNext - pszSrc);
1753 pszDst += pszSrcNext - pszSrc;
1754 pszSrc = pszSrcNext;
1755 }
1756
1757 /* Copy the final portion and replace the pattern. */
1758 strcpy(pszDst, pszSrc);
1759
1760 RTStrFree(pSettings->paPairs[iPair].pszPattern);
1761 pSettings->paPairs[iPair].pszPattern = pszNewPattern;
1762 }
1763 else
1764 rc = VERR_NO_MEMORY;
1765 }
1766 }
1767 if (RT_SUCCESS(rc))
1768 /*
1769 * Commit the pair.
1770 */
1771 pSettings->cPairs = iPair + 1;
1772 else
1773 {
1774 RTStrFree(pSettings->paPairs[iPair].pszPattern);
1775 RTStrFree(pSettings->paPairs[iPair].pszOptions);
1776 RTStrFree(pSettings->paPairs[iPair].pszRelativeTo);
1777 }
1778 return rc;
1779}
1780
1781/**
1782 * Loads in the settings from @a pszFilename.
1783 *
1784 * @returns IPRT status code.
1785 * @param pSettings Where to load the settings file.
1786 * @param pszFilename The file to load.
1787 */
1788static int scmSettingsLoadFile(PSCMSETTINGS pSettings, const char *pszFilename)
1789{
1790 ScmVerbose(NULL, 3, "Loading settings file '%s'...\n", pszFilename);
1791
1792 /* Turn filename into an absolute path and drop the filename. */
1793 char szAbsPath[RTPATH_MAX];
1794 int rc = RTPathAbs(pszFilename, szAbsPath, sizeof(szAbsPath));
1795 if (RT_FAILURE(rc))
1796 {
1797 RTMsgError("%s: RTPathAbs -> %Rrc\n", pszFilename, rc);
1798 return rc;
1799 }
1800 RTPathChangeToUnixSlashes(szAbsPath, true);
1801 size_t cchDir = RTPathFilename(szAbsPath) - &szAbsPath[0];
1802
1803 /* Try open it.*/
1804 SCMSTREAM Stream;
1805 rc = ScmStreamInitForReading(&Stream, pszFilename);
1806 if (RT_SUCCESS(rc))
1807 {
1808 SCMEOL enmEol;
1809 const char *pchLine;
1810 size_t cchLine;
1811 while ((pchLine = ScmStreamGetLine(&Stream, &cchLine, &enmEol)) != NULL)
1812 {
1813 /* Ignore leading spaces. */
1814 while (cchLine > 0 && RT_C_IS_SPACE(*pchLine))
1815 pchLine++, cchLine--;
1816
1817 /* Ignore empty lines and comment lines. */
1818 if (cchLine < 1 || *pchLine == '#')
1819 continue;
1820
1821 /* Deal with escaped newlines. */
1822 size_t iFirstLine = ~(size_t)0;
1823 char *pszFreeLine = NULL;
1824 if ( pchLine[cchLine - 1] == '\\'
1825 && ( cchLine < 2
1826 || pchLine[cchLine - 2] != '\\') )
1827 {
1828 iFirstLine = ScmStreamTellLine(&Stream);
1829
1830 cchLine--;
1831 while (cchLine > 0 && RT_C_IS_SPACE(pchLine[cchLine - 1]))
1832 cchLine--;
1833
1834 size_t cchTotal = cchLine;
1835 pszFreeLine = RTStrDupN(pchLine, cchLine);
1836 if (pszFreeLine)
1837 {
1838 /* Append following lines. */
1839 while ((pchLine = ScmStreamGetLine(&Stream, &cchLine, &enmEol)) != NULL)
1840 {
1841 while (cchLine > 0 && RT_C_IS_SPACE(*pchLine))
1842 pchLine++, cchLine--;
1843
1844 bool const fDone = cchLine == 0
1845 || pchLine[cchLine - 1] != '\\'
1846 || (cchLine >= 2 && pchLine[cchLine - 2] == '\\');
1847 if (!fDone)
1848 {
1849 cchLine--;
1850 while (cchLine > 0 && RT_C_IS_SPACE(pchLine[cchLine - 1]))
1851 cchLine--;
1852 }
1853
1854 rc = RTStrRealloc(&pszFreeLine, cchTotal + 1 + cchLine + 1);
1855 if (RT_FAILURE(rc))
1856 break;
1857 pszFreeLine[cchTotal++] = ' ';
1858 memcpy(&pszFreeLine[cchTotal], pchLine, cchLine);
1859 cchTotal += cchLine;
1860 pszFreeLine[cchTotal] = '\0';
1861
1862 if (fDone)
1863 break;
1864 }
1865 }
1866 else
1867 rc = VERR_NO_STR_MEMORY;
1868
1869 if (RT_FAILURE(rc))
1870 {
1871 RTStrFree(pszFreeLine);
1872 rc = RTMsgErrorRc(VERR_NO_MEMORY, "%s: Ran out of memory deal with escaped newlines", pszFilename);
1873 break;
1874 }
1875
1876 pchLine = pszFreeLine;
1877 cchLine = cchTotal;
1878 }
1879
1880 /* What kind of line is it? */
1881 const char *pchColon = (const char *)memchr(pchLine, ':', cchLine);
1882 if (pchColon)
1883 rc = scmSettingsAddPair(pSettings, pchLine, cchLine, pchColon - pchLine, szAbsPath, cchDir);
1884 else
1885 rc = scmSettingsBaseParseStringN(&pSettings->Base, pchLine, cchLine, szAbsPath, cchDir);
1886 if (pszFreeLine)
1887 RTStrFree(pszFreeLine);
1888 if (RT_FAILURE(rc))
1889 {
1890 RTMsgError("%s:%d: %Rrc\n",
1891 pszFilename, iFirstLine == ~(size_t)0 ? ScmStreamTellLine(&Stream) : iFirstLine, rc);
1892 break;
1893 }
1894 }
1895
1896 if (RT_SUCCESS(rc))
1897 {
1898 rc = ScmStreamGetStatus(&Stream);
1899 if (RT_FAILURE(rc))
1900 RTMsgError("%s: ScmStreamGetStatus- > %Rrc\n", pszFilename, rc);
1901 }
1902 ScmStreamDelete(&Stream);
1903 }
1904 else
1905 RTMsgError("%s: ScmStreamInitForReading -> %Rrc\n", pszFilename, rc);
1906 return rc;
1907}
1908
1909#if 0 /* unused */
1910/**
1911 * Parse the specified settings file creating a new settings struct from it.
1912 *
1913 * @returns IPRT status code
1914 * @param ppSettings Where to return the new settings.
1915 * @param pszFilename The file to parse.
1916 * @param pSettingsBase The base settings we inherit from.
1917 */
1918static int scmSettingsCreateFromFile(PSCMSETTINGS *ppSettings, const char *pszFilename, PCSCMSETTINGSBASE pSettingsBase)
1919{
1920 PSCMSETTINGS pSettings;
1921 int rc = scmSettingsCreate(&pSettings, pSettingsBase);
1922 if (RT_SUCCESS(rc))
1923 {
1924 rc = scmSettingsLoadFile(pSettings, pszFilename, RTPathFilename(pszFilename) - pszFilename);
1925 if (RT_SUCCESS(rc))
1926 {
1927 *ppSettings = pSettings;
1928 return VINF_SUCCESS;
1929 }
1930
1931 scmSettingsDestroy(pSettings);
1932 }
1933 *ppSettings = NULL;
1934 return rc;
1935}
1936#endif
1937
1938
1939/**
1940 * Create an initial settings structure when starting processing a new file or
1941 * directory.
1942 *
1943 * This will look for .scm-settings files from the root and down to the
1944 * specified directory, combining them into the returned settings structure.
1945 *
1946 * @returns IPRT status code.
1947 * @param ppSettings Where to return the pointer to the top stack
1948 * object.
1949 * @param pBaseSettings The base settings we inherit from (globals
1950 * typically).
1951 * @param pszPath The absolute path to the new directory or file.
1952 */
1953static int scmSettingsCreateForPath(PSCMSETTINGS *ppSettings, PCSCMSETTINGSBASE pBaseSettings, const char *pszPath)
1954{
1955 *ppSettings = NULL; /* try shut up gcc. */
1956
1957 /*
1958 * We'll be working with a stack copy of the path.
1959 */
1960 char szFile[RTPATH_MAX];
1961 size_t cchDir = strlen(pszPath);
1962 if (cchDir >= sizeof(szFile) - sizeof(SCM_SETTINGS_FILENAME))
1963 return VERR_FILENAME_TOO_LONG;
1964
1965 /*
1966 * Create the bottom-most settings.
1967 */
1968 PSCMSETTINGS pSettings;
1969 int rc = scmSettingsCreate(&pSettings, pBaseSettings);
1970 if (RT_FAILURE(rc))
1971 return rc;
1972
1973 /*
1974 * Enumerate the path components from the root and down. Load any setting
1975 * files we find.
1976 */
1977 size_t cComponents = RTPathCountComponents(pszPath);
1978 for (size_t i = 1; i <= cComponents; i++)
1979 {
1980 rc = RTPathCopyComponents(szFile, sizeof(szFile), pszPath, i);
1981 if (RT_SUCCESS(rc))
1982 rc = RTPathAppend(szFile, sizeof(szFile), SCM_SETTINGS_FILENAME);
1983 if (RT_FAILURE(rc))
1984 break;
1985 RTPathChangeToUnixSlashes(szFile, true);
1986
1987 if (RTFileExists(szFile))
1988 {
1989 rc = scmSettingsLoadFile(pSettings, szFile);
1990 if (RT_FAILURE(rc))
1991 break;
1992 }
1993 }
1994
1995 if (RT_SUCCESS(rc))
1996 *ppSettings = pSettings;
1997 else
1998 scmSettingsDestroy(pSettings);
1999 return rc;
2000}
2001
2002/**
2003 * Pushes a new settings set onto the stack.
2004 *
2005 * @param ppSettingsStack The pointer to the pointer to the top stack
2006 * element. This will be used as input and output.
2007 * @param pSettings The settings to push onto the stack.
2008 */
2009static void scmSettingsStackPush(PSCMSETTINGS *ppSettingsStack, PSCMSETTINGS pSettings)
2010{
2011 PSCMSETTINGS pOld = *ppSettingsStack;
2012 pSettings->pDown = pOld;
2013 pSettings->pUp = NULL;
2014 if (pOld)
2015 pOld->pUp = pSettings;
2016 *ppSettingsStack = pSettings;
2017}
2018
2019/**
2020 * Pushes the settings of the specified directory onto the stack.
2021 *
2022 * We will load any .scm-settings in the directory. A stack entry is added even
2023 * if no settings file was found.
2024 *
2025 * @returns IPRT status code.
2026 * @param ppSettingsStack The pointer to the pointer to the top stack
2027 * element. This will be used as input and output.
2028 * @param pszDir The directory to do this for.
2029 */
2030static int scmSettingsStackPushDir(PSCMSETTINGS *ppSettingsStack, const char *pszDir)
2031{
2032 char szFile[RTPATH_MAX];
2033 int rc = RTPathJoin(szFile, sizeof(szFile), pszDir, SCM_SETTINGS_FILENAME);
2034 if (RT_SUCCESS(rc))
2035 {
2036 RTPathChangeToUnixSlashes(szFile, true);
2037
2038 PSCMSETTINGS pSettings;
2039 rc = scmSettingsCreate(&pSettings, &(*ppSettingsStack)->Base);
2040 if (RT_SUCCESS(rc))
2041 {
2042 if (RTFileExists(szFile))
2043 rc = scmSettingsLoadFile(pSettings, szFile);
2044 if (RT_SUCCESS(rc))
2045 {
2046 scmSettingsStackPush(ppSettingsStack, pSettings);
2047 return VINF_SUCCESS;
2048 }
2049
2050 scmSettingsDestroy(pSettings);
2051 }
2052 }
2053 return rc;
2054}
2055
2056
2057/**
2058 * Pops a settings set off the stack.
2059 *
2060 * @returns The popped settings.
2061 * @param ppSettingsStack The pointer to the pointer to the top stack
2062 * element. This will be used as input and output.
2063 */
2064static PSCMSETTINGS scmSettingsStackPop(PSCMSETTINGS *ppSettingsStack)
2065{
2066 PSCMSETTINGS pRet = *ppSettingsStack;
2067 PSCMSETTINGS pNew = pRet ? pRet->pDown : NULL;
2068 *ppSettingsStack = pNew;
2069 if (pNew)
2070 pNew->pUp = NULL;
2071 if (pRet)
2072 {
2073 pRet->pUp = NULL;
2074 pRet->pDown = NULL;
2075 }
2076 return pRet;
2077}
2078
2079/**
2080 * Pops and destroys the top entry of the stack.
2081 *
2082 * @param ppSettingsStack The pointer to the pointer to the top stack
2083 * element. This will be used as input and output.
2084 */
2085static void scmSettingsStackPopAndDestroy(PSCMSETTINGS *ppSettingsStack)
2086{
2087 scmSettingsDestroy(scmSettingsStackPop(ppSettingsStack));
2088}
2089
2090/**
2091 * Constructs the base settings for the specified file name.
2092 *
2093 * @returns IPRT status code.
2094 * @param pSettingsStack The top element on the settings stack.
2095 * @param pszFilename The file name.
2096 * @param pszBasename The base name (pointer within @a pszFilename).
2097 * @param cchBasename The length of the base name. (For passing to
2098 * RTStrSimplePatternMultiMatch.)
2099 * @param pBase Base settings to initialize.
2100 */
2101static int scmSettingsStackMakeFileBase(PCSCMSETTINGS pSettingsStack, const char *pszFilename,
2102 const char *pszBasename, size_t cchBasename, PSCMSETTINGSBASE pBase)
2103{
2104 ScmVerbose(NULL, 5, "scmSettingsStackMakeFileBase(%s, %.*s)\n", pszFilename, cchBasename, pszBasename);
2105
2106 int rc = scmSettingsBaseInitAndCopy(pBase, &pSettingsStack->Base);
2107 if (RT_SUCCESS(rc))
2108 {
2109 /* find the bottom entry in the stack. */
2110 PCSCMSETTINGS pCur = pSettingsStack;
2111 while (pCur->pDown)
2112 pCur = pCur->pDown;
2113
2114 /* Work our way up thru the stack and look for matching pairs. */
2115 while (pCur)
2116 {
2117 size_t const cPairs = pCur->cPairs;
2118 if (cPairs)
2119 {
2120 for (size_t i = 0; i < cPairs; i++)
2121 if ( !pCur->paPairs[i].fMultiPattern
2122 ? RTStrSimplePatternNMatch(pCur->paPairs[i].pszPattern, RTSTR_MAX,
2123 pszBasename, cchBasename)
2124 || RTStrSimplePatternMatch(pCur->paPairs[i].pszPattern, pszFilename)
2125 : RTStrSimplePatternMultiMatch(pCur->paPairs[i].pszPattern, RTSTR_MAX,
2126 pszBasename, cchBasename, NULL)
2127 || RTStrSimplePatternMultiMatch(pCur->paPairs[i].pszPattern, RTSTR_MAX,
2128 pszFilename, RTSTR_MAX, NULL))
2129 {
2130 ScmVerbose(NULL, 5, "scmSettingsStackMakeFileBase: Matched '%s' : '%s'\n",
2131 pCur->paPairs[i].pszPattern, pCur->paPairs[i].pszOptions);
2132 rc = scmSettingsBaseParseString(pBase, pCur->paPairs[i].pszOptions,
2133 pCur->paPairs[i].pszRelativeTo, strlen(pCur->paPairs[i].pszRelativeTo));
2134 if (RT_FAILURE(rc))
2135 break;
2136 }
2137 if (RT_FAILURE(rc))
2138 break;
2139 }
2140
2141 /* advance */
2142 pCur = pCur->pUp;
2143 }
2144 }
2145 if (RT_FAILURE(rc))
2146 scmSettingsBaseDelete(pBase);
2147 return rc;
2148}
2149
2150
2151/* -=-=-=-=-=- misc -=-=-=-=-=- */
2152
2153
2154/**
2155 * Prints the per file banner needed and the message level is high enough.
2156 *
2157 * @param pState The rewrite state.
2158 * @param iLevel The required verbosity level.
2159 */
2160void ScmVerboseBanner(PSCMRWSTATE pState, int iLevel)
2161{
2162 if (iLevel <= g_iVerbosity && !pState->fFirst)
2163 {
2164 RTPrintf("%s: info: --= Rewriting '%s' =--\n", g_szProgName, pState->pszFilename);
2165 pState->fFirst = true;
2166 }
2167}
2168
2169
2170/**
2171 * Prints a verbose message if the level is high enough.
2172 *
2173 * @param pState The rewrite state. Optional.
2174 * @param iLevel The required verbosity level.
2175 * @param pszFormat The message format string. Can be NULL if we
2176 * only want to trigger the per file message.
2177 * @param ... Format arguments.
2178 */
2179void ScmVerbose(PSCMRWSTATE pState, int iLevel, const char *pszFormat, ...)
2180{
2181 if (iLevel <= g_iVerbosity)
2182 {
2183 if (pState && !pState->fFirst)
2184 {
2185 RTPrintf("%s: info: --= Rewriting '%s' =--\n", g_szProgName, pState->pszFilename);
2186 pState->fFirst = true;
2187 }
2188 RTPrintf(pState
2189 ? "%s: info: "
2190 : "%s: info: ",
2191 g_szProgName);
2192 va_list va;
2193 va_start(va, pszFormat);
2194 RTPrintfV(pszFormat, va);
2195 va_end(va);
2196 }
2197}
2198
2199
2200/**
2201 * Prints an error message.
2202 *
2203 * @returns kScmUnmodified
2204 * @param pState The rewrite state. Optional.
2205 * @param rc The error code.
2206 * @param pszFormat The message format string.
2207 * @param ... Format arguments.
2208 */
2209SCMREWRITERRES ScmError(PSCMRWSTATE pState, int rc, const char *pszFormat, ...)
2210{
2211 if (RT_SUCCESS(pState->rc))
2212 pState->rc = rc;
2213
2214 if (!pState->fFirst)
2215 {
2216 RTPrintf("%s: info: --= Rewriting '%s' =--\n", g_szProgName, pState->pszFilename);
2217 pState->fFirst = true;
2218 }
2219 va_list va;
2220 va_start(va, pszFormat);
2221 RTPrintf("%s: error: %s: %N", g_szProgName, pState->pszFilename, pszFormat, &va);
2222 va_end(va);
2223
2224 return kScmUnmodified;
2225}
2226
2227
2228/**
2229 * Prints message indicating that something requires manual fixing.
2230 *
2231 * @returns false
2232 * @param pState The rewrite state. Optional.
2233 * @param rc The error code.
2234 * @param pszFormat The message format string.
2235 * @param ... Format arguments.
2236 */
2237bool ScmFixManually(PSCMRWSTATE pState, const char *pszFormat, ...)
2238{
2239 va_list va;
2240 va_start(va, pszFormat);
2241 ScmFixManuallyV(pState, pszFormat, va);
2242 va_end(va);
2243 return false;
2244}
2245
2246
2247/**
2248 * Prints message indicating that something requires manual fixing.
2249 *
2250 * @returns false
2251 * @param pState The rewrite state. Optional.
2252 * @param rc The error code.
2253 * @param pszFormat The message format string.
2254 * @param va Format arguments.
2255 */
2256bool ScmFixManuallyV(PSCMRWSTATE pState, const char *pszFormat, va_list va)
2257{
2258 pState->fNeedsManualRepair = true;
2259
2260 if (!pState->fFirst)
2261 {
2262 RTPrintf("%s: info: --= Rewriting '%s' =--\n", g_szProgName, pState->pszFilename);
2263 pState->fFirst = true;
2264 }
2265 va_list vaCopy;
2266 va_copy(vaCopy, va);
2267 RTPrintf("%s: error/fixme: %s: %N", g_szProgName, pState->pszFilename, pszFormat, &vaCopy);
2268 va_end(vaCopy);
2269
2270 return false;
2271}
2272
2273
2274/* -=-=-=-=-=- file and directory processing -=-=-=-=-=- */
2275
2276
2277/**
2278 * Processes a file.
2279 *
2280 * @returns IPRT status code.
2281 * @param pState The rewriter state.
2282 * @param pszFilename The file name.
2283 * @param pszBasename The base name (pointer within @a pszFilename).
2284 * @param cchBasename The length of the base name. (For passing to
2285 * RTStrSimplePatternMultiMatch.)
2286 * @param pBaseSettings The base settings to use. It's OK to modify
2287 * these.
2288 */
2289static int scmProcessFileInner(PSCMRWSTATE pState, const char *pszFilename, const char *pszBasename, size_t cchBasename,
2290 PSCMSETTINGSBASE pBaseSettings)
2291{
2292 /*
2293 * Do the file level filtering.
2294 */
2295 if ( pBaseSettings->pszFilterFiles
2296 && *pBaseSettings->pszFilterFiles
2297 && !RTStrSimplePatternMultiMatch(pBaseSettings->pszFilterFiles, RTSTR_MAX, pszBasename, cchBasename, NULL))
2298 {
2299 ScmVerbose(NULL, 5, "skipping '%s': file filter mismatch\n", pszFilename);
2300 g_cFilesSkipped++;
2301 return VINF_SUCCESS;
2302 }
2303 if ( pBaseSettings->pszFilterOutFiles
2304 && *pBaseSettings->pszFilterOutFiles
2305 && ( RTStrSimplePatternMultiMatch(pBaseSettings->pszFilterOutFiles, RTSTR_MAX, pszBasename, cchBasename, NULL)
2306 || RTStrSimplePatternMultiMatch(pBaseSettings->pszFilterOutFiles, RTSTR_MAX, pszFilename, RTSTR_MAX, NULL)) )
2307 {
2308 ScmVerbose(NULL, 5, "skipping '%s': filterd out\n", pszFilename);
2309 g_cFilesSkipped++;
2310 return VINF_SUCCESS;
2311 }
2312 if ( pBaseSettings->fOnlySvnFiles
2313 && !ScmSvnIsInWorkingCopy(pState))
2314 {
2315 ScmVerbose(NULL, 5, "skipping '%s': not in SVN WC\n", pszFilename);
2316 g_cFilesNotInSvn++;
2317 return VINF_SUCCESS;
2318 }
2319
2320 /*
2321 * Create an input stream from the file and check that it's text.
2322 */
2323 SCMSTREAM Stream1;
2324 int rc = ScmStreamInitForReading(&Stream1, pszFilename);
2325 if (RT_FAILURE(rc))
2326 {
2327 RTMsgError("Failed to read '%s': %Rrc\n", pszFilename, rc);
2328 return rc;
2329 }
2330 bool const fIsText = ScmStreamIsText(&Stream1);
2331
2332 /*
2333 * Try find a matching rewrite config for this filename.
2334 */
2335 PCSCMCFGENTRY pCfg = pBaseSettings->pTreatAs;
2336 if (!pCfg)
2337 {
2338 for (size_t iCfg = 0; iCfg < RT_ELEMENTS(g_aConfigs); iCfg++)
2339 if (RTStrSimplePatternMultiMatch(g_aConfigs[iCfg].pszFilePattern, RTSTR_MAX, pszBasename, cchBasename, NULL))
2340 {
2341 pCfg = &g_aConfigs[iCfg];
2342 break;
2343 }
2344 if (!pCfg)
2345 {
2346 /* On failure try check for hash-bang stuff before giving up. */
2347 if (fIsText)
2348 {
2349 SCMEOL enmIgn;
2350 size_t cchFirst;
2351 const char *pchFirst = ScmStreamGetLine(&Stream1, &cchFirst, &enmIgn);
2352 if (cchFirst >= 9 && pchFirst && *pchFirst == '#')
2353 {
2354 do
2355 {
2356 pchFirst++;
2357 cchFirst--;
2358 } while (cchFirst > 0 && RT_C_IS_BLANK(*pchFirst));
2359 if (*pchFirst == '!')
2360 {
2361 do
2362 {
2363 pchFirst++;
2364 cchFirst--;
2365 } while (cchFirst > 0 && RT_C_IS_BLANK(*pchFirst));
2366 const char *pszTreatAs = NULL;
2367 if ( (cchFirst >= 7 && strncmp(pchFirst, "/bin/sh", 7) == 0)
2368 || (cchFirst >= 9 && strncmp(pchFirst, "/bin/bash", 9) == 0)
2369 || (cchFirst >= 4+9 && strncmp(pchFirst, "/usr/bin/bash", 4+9) == 0) )
2370 pszTreatAs = "shell";
2371 else if ( (cchFirst >= 15 && strncmp(pchFirst, "/usr/bin/python", 15) == 0)
2372 || (cchFirst >= 19 && strncmp(pchFirst, "/usr/bin/env python", 19) == 0) )
2373 pszTreatAs = "python";
2374 else if ( (cchFirst >= 13 && strncmp(pchFirst, "/usr/bin/perl", 13) == 0)
2375 || (cchFirst >= 17 && strncmp(pchFirst, "/usr/bin/env perl", 17) == 0) )
2376 pszTreatAs = "perl";
2377 if (pszTreatAs)
2378 {
2379 for (size_t iCfg = 0; iCfg < RT_ELEMENTS(g_aConfigs); iCfg++)
2380 if (strcmp(pszTreatAs, g_aConfigs[iCfg].pszName) == 0)
2381 {
2382 pCfg = &g_aConfigs[iCfg];
2383 break;
2384 }
2385 Assert(pCfg);
2386 }
2387 }
2388 }
2389 ScmStreamRewindForReading(&Stream1);
2390 }
2391 if (!pCfg)
2392 {
2393 ScmVerbose(NULL, 2, "skipping '%s': no rewriters configured\n", pszFilename);
2394 g_cFilesNoRewriters++;
2395 ScmStreamDelete(&Stream1);
2396 return VINF_SUCCESS;
2397 }
2398 }
2399 ScmVerbose(pState, 4, "matched \"%s\" (%s)\n", pCfg->pszFilePattern, pCfg->pszName);
2400 }
2401 else
2402 ScmVerbose(pState, 4, "treat-as \"%s\"\n", pCfg->pszName);
2403
2404 if (fIsText || pCfg->fBinary)
2405 {
2406 ScmVerboseBanner(pState, 3);
2407
2408 /*
2409 * Gather SCM and editor settings from the stream.
2410 */
2411 rc = scmSettingsBaseLoadFromDocument(pBaseSettings, &Stream1);
2412 if (RT_SUCCESS(rc))
2413 {
2414 ScmStreamRewindForReading(&Stream1);
2415
2416 /*
2417 * Create two more streams for output and push the text thru all the
2418 * rewriters, switching the two streams around when something is
2419 * actually rewritten. Stream1 remains unchanged.
2420 */
2421 SCMSTREAM Stream2;
2422 rc = ScmStreamInitForWriting(&Stream2, &Stream1);
2423 if (RT_SUCCESS(rc))
2424 {
2425 SCMSTREAM Stream3;
2426 rc = ScmStreamInitForWriting(&Stream3, &Stream1);
2427 if (RT_SUCCESS(rc))
2428 {
2429 bool fModified = false;
2430 PSCMSTREAM pIn = &Stream1;
2431 PSCMSTREAM pOut = &Stream2;
2432 for (size_t iRw = 0; iRw < pCfg->cRewriters; iRw++)
2433 {
2434 pState->rc = VINF_SUCCESS;
2435 SCMREWRITERRES enmRes = pCfg->paRewriters[iRw]->pfnRewriter(pState, pIn, pOut, pBaseSettings);
2436 if (RT_FAILURE(pState->rc))
2437 break;
2438 if (enmRes == kScmMaybeModified)
2439 enmRes = ScmStreamAreIdentical(pIn, pOut) ? kScmUnmodified : kScmModified;
2440 if (enmRes == kScmModified)
2441 {
2442 PSCMSTREAM pTmp = pOut;
2443 pOut = pIn == &Stream1 ? &Stream3 : pIn;
2444 pIn = pTmp;
2445 fModified = true;
2446 }
2447
2448 ScmStreamRewindForReading(pIn);
2449 ScmStreamRewindForWriting(pOut);
2450 }
2451
2452 rc = pState->rc;
2453 if (RT_SUCCESS(rc))
2454 {
2455 rc = ScmStreamGetStatus(&Stream1);
2456 if (RT_SUCCESS(rc))
2457 rc = ScmStreamGetStatus(&Stream2);
2458 if (RT_SUCCESS(rc))
2459 rc = ScmStreamGetStatus(&Stream3);
2460 if (RT_SUCCESS(rc))
2461 {
2462 /*
2463 * If rewritten, write it back to disk.
2464 */
2465 if (fModified && !pCfg->fBinary)
2466 {
2467 if (!g_fDryRun)
2468 {
2469 ScmVerbose(pState, 1, "writing modified file to \"%s%s\"\n", pszFilename, g_pszChangedSuff);
2470 rc = ScmStreamWriteToFile(pIn, "%s%s", pszFilename, g_pszChangedSuff);
2471 if (RT_FAILURE(rc))
2472 RTMsgError("Error writing '%s%s': %Rrc\n", pszFilename, g_pszChangedSuff, rc);
2473 }
2474 else
2475 {
2476 ScmVerboseBanner(pState, 1);
2477 ScmDiffStreams(pszFilename, &Stream1, pIn, g_fDiffIgnoreEol,
2478 g_fDiffIgnoreLeadingWS, g_fDiffIgnoreTrailingWS, g_fDiffSpecialChars,
2479 pBaseSettings->cchTab, g_pStdOut);
2480 ScmVerbose(pState, 2, "would have modified the file \"%s%s\"\n",
2481 pszFilename, g_pszChangedSuff);
2482 }
2483 g_cFilesModified++;
2484 }
2485 else if (fModified)
2486 rc = RTMsgErrorRc(VERR_INTERNAL_ERROR, "Rewriters modified binary file! Impossible!");
2487
2488 /*
2489 * If pending SVN property changes, apply them.
2490 */
2491 if (pState->cSvnPropChanges && RT_SUCCESS(rc))
2492 {
2493 if (!g_fDryRun)
2494 {
2495 rc = ScmSvnApplyChanges(pState);
2496 if (RT_FAILURE(rc))
2497 RTMsgError("%s: failed to apply SVN property changes (%Rrc)\n", pszFilename, rc);
2498 }
2499 else
2500 ScmSvnDisplayChanges(pState);
2501 if (!fModified)
2502 g_cFilesModified++;
2503 }
2504
2505 if (!fModified && !pState->cSvnPropChanges)
2506 ScmVerbose(pState, 3, "%s: no change\n", pszFilename);
2507 }
2508 else
2509 RTMsgError("%s: stream error %Rrc\n", pszFilename, rc);
2510 }
2511 ScmStreamDelete(&Stream3);
2512 }
2513 else
2514 RTMsgError("Failed to init stream for writing: %Rrc\n", rc);
2515 ScmStreamDelete(&Stream2);
2516 }
2517 else
2518 RTMsgError("Failed to init stream for writing: %Rrc\n", rc);
2519 }
2520 else
2521 RTMsgError("scmSettingsBaseLoadFromDocument: %Rrc\n", rc);
2522 }
2523 else
2524 {
2525 ScmVerbose(pState, 2, "not text file: \"%s\"\n", pszFilename);
2526 g_cFilesBinaries++;
2527 }
2528 ScmStreamDelete(&Stream1);
2529
2530 return rc;
2531}
2532
2533/**
2534 * Processes a file.
2535 *
2536 * This is just a wrapper for scmProcessFileInner for avoid wasting stack in the
2537 * directory recursion method.
2538 *
2539 * @returns IPRT status code.
2540 * @param pszFilename The file name.
2541 * @param pszBasename The base name (pointer within @a pszFilename).
2542 * @param cchBasename The length of the base name. (For passing to
2543 * RTStrSimplePatternMultiMatch.)
2544 * @param pSettingsStack The settings stack (pointer to the top element).
2545 */
2546static int scmProcessFile(const char *pszFilename, const char *pszBasename, size_t cchBasename,
2547 PSCMSETTINGS pSettingsStack)
2548{
2549 SCMSETTINGSBASE Base;
2550 int rc = scmSettingsStackMakeFileBase(pSettingsStack, pszFilename, pszBasename, cchBasename, &Base);
2551 if (RT_SUCCESS(rc))
2552 {
2553 SCMRWSTATE State;
2554 State.pszFilename = pszFilename;
2555 State.fFirst = false;
2556 State.fNeedsManualRepair = false;
2557 State.fIsInSvnWorkingCopy = 0;
2558 State.cSvnPropChanges = 0;
2559 State.paSvnPropChanges = NULL;
2560 State.rc = VINF_SUCCESS;
2561
2562 rc = scmProcessFileInner(&State, pszFilename, pszBasename, cchBasename, &Base);
2563
2564 size_t i = State.cSvnPropChanges;
2565 while (i-- > 0)
2566 {
2567 RTStrFree(State.paSvnPropChanges[i].pszName);
2568 RTStrFree(State.paSvnPropChanges[i].pszValue);
2569 }
2570 RTMemFree(State.paSvnPropChanges);
2571
2572 scmSettingsBaseDelete(&Base);
2573
2574 if (State.fNeedsManualRepair)
2575 g_cFilesRequiringManualFixing++;
2576 g_cFilesProcessed++;
2577 }
2578 return rc;
2579}
2580
2581/**
2582 * Tries to correct RTDIRENTRY_UNKNOWN.
2583 *
2584 * @returns Corrected type.
2585 * @param pszPath The path to the object in question.
2586 */
2587static RTDIRENTRYTYPE scmFigureUnknownType(const char *pszPath)
2588{
2589 RTFSOBJINFO Info;
2590 int rc = RTPathQueryInfo(pszPath, &Info, RTFSOBJATTRADD_NOTHING);
2591 if (RT_FAILURE(rc))
2592 return RTDIRENTRYTYPE_UNKNOWN;
2593 if (RTFS_IS_DIRECTORY(Info.Attr.fMode))
2594 return RTDIRENTRYTYPE_DIRECTORY;
2595 if (RTFS_IS_FILE(Info.Attr.fMode))
2596 return RTDIRENTRYTYPE_FILE;
2597 return RTDIRENTRYTYPE_UNKNOWN;
2598}
2599
2600/**
2601 * Recurse into a sub-directory and process all the files and directories.
2602 *
2603 * @returns IPRT status code.
2604 * @param pszBuf Path buffer containing the directory path on
2605 * entry. This ends with a dot. This is passed
2606 * along when recursing in order to save stack space
2607 * and avoid needless copying.
2608 * @param cchDir Length of our path in pszbuf.
2609 * @param pEntry Directory entry buffer. This is also passed
2610 * along when recursing to save stack space.
2611 * @param pSettingsStack The settings stack (pointer to the top element).
2612 * @param iRecursion The recursion depth. This is used to restrict
2613 * the recursions.
2614 */
2615static int scmProcessDirTreeRecursion(char *pszBuf, size_t cchDir, PRTDIRENTRY pEntry,
2616 PSCMSETTINGS pSettingsStack, unsigned iRecursion)
2617{
2618 int rc;
2619 Assert(cchDir > 1 && pszBuf[cchDir - 1] == '.');
2620
2621 /*
2622 * Make sure we stop somewhere.
2623 */
2624 if (iRecursion > 128)
2625 {
2626 RTMsgError("recursion too deep: %d\n", iRecursion);
2627 return VINF_SUCCESS; /* ignore */
2628 }
2629
2630 /*
2631 * Check if it's excluded by --only-svn-dir.
2632 */
2633 if (pSettingsStack->Base.fOnlySvnDirs)
2634 {
2635 if (!ScmSvnIsDirInWorkingCopy(pszBuf))
2636 return VINF_SUCCESS;
2637 }
2638 g_cDirsProcessed++;
2639
2640 /*
2641 * Try open and read the directory.
2642 */
2643 RTDIR hDir;
2644 rc = RTDirOpenFiltered(&hDir, pszBuf, RTDIRFILTER_NONE, 0 /*fFlags*/);
2645 if (RT_FAILURE(rc))
2646 {
2647 RTMsgError("Failed to enumerate directory '%s': %Rrc", pszBuf, rc);
2648 return rc;
2649 }
2650 for (;;)
2651 {
2652 /* Read the next entry. */
2653 rc = RTDirRead(hDir, pEntry, NULL);
2654 if (RT_FAILURE(rc))
2655 {
2656 if (rc == VERR_NO_MORE_FILES)
2657 rc = VINF_SUCCESS;
2658 else
2659 RTMsgError("RTDirRead -> %Rrc\n", rc);
2660 break;
2661 }
2662
2663 /* Skip '.' and '..'. */
2664 if ( pEntry->szName[0] == '.'
2665 && ( pEntry->cbName == 1
2666 || ( pEntry->cbName == 2
2667 && pEntry->szName[1] == '.')))
2668 continue;
2669
2670 /* Enter it into the buffer so we've got a full name to work
2671 with when needed. */
2672 if (pEntry->cbName + cchDir >= RTPATH_MAX)
2673 {
2674 RTMsgError("Skipping too long entry: %s", pEntry->szName);
2675 continue;
2676 }
2677 memcpy(&pszBuf[cchDir - 1], pEntry->szName, pEntry->cbName + 1);
2678
2679 /* Figure the type. */
2680 RTDIRENTRYTYPE enmType = pEntry->enmType;
2681 if (enmType == RTDIRENTRYTYPE_UNKNOWN)
2682 enmType = scmFigureUnknownType(pszBuf);
2683
2684 /* Process the file or directory, skip the rest. */
2685 if (enmType == RTDIRENTRYTYPE_FILE)
2686 rc = scmProcessFile(pszBuf, pEntry->szName, pEntry->cbName, pSettingsStack);
2687 else if (enmType == RTDIRENTRYTYPE_DIRECTORY)
2688 {
2689 /* Append the dot for the benefit of the pattern matching. */
2690 if (pEntry->cbName + cchDir + 5 >= RTPATH_MAX)
2691 {
2692 RTMsgError("Skipping too deep dir entry: %s", pEntry->szName);
2693 continue;
2694 }
2695 memcpy(&pszBuf[cchDir - 1 + pEntry->cbName], "/.", sizeof("/."));
2696 size_t cchSubDir = cchDir - 1 + pEntry->cbName + sizeof("/.") - 1;
2697
2698 if ( !pSettingsStack->Base.pszFilterOutDirs
2699 || !*pSettingsStack->Base.pszFilterOutDirs
2700 || ( !RTStrSimplePatternMultiMatch(pSettingsStack->Base.pszFilterOutDirs, RTSTR_MAX,
2701 pEntry->szName, pEntry->cbName, NULL)
2702 && !RTStrSimplePatternMultiMatch(pSettingsStack->Base.pszFilterOutDirs, RTSTR_MAX,
2703 pszBuf, cchSubDir, NULL)
2704 )
2705 )
2706 {
2707 rc = scmSettingsStackPushDir(&pSettingsStack, pszBuf);
2708 if (RT_SUCCESS(rc))
2709 {
2710 rc = scmProcessDirTreeRecursion(pszBuf, cchSubDir, pEntry, pSettingsStack, iRecursion + 1);
2711 scmSettingsStackPopAndDestroy(&pSettingsStack);
2712 }
2713 }
2714 }
2715 if (RT_FAILURE(rc))
2716 break;
2717 }
2718 RTDirClose(hDir);
2719 return rc;
2720
2721}
2722
2723/**
2724 * Process a directory tree.
2725 *
2726 * @returns IPRT status code.
2727 * @param pszDir The directory to start with. This is pointer to
2728 * a RTPATH_MAX sized buffer.
2729 */
2730static int scmProcessDirTree(char *pszDir, PSCMSETTINGS pSettingsStack)
2731{
2732 /*
2733 * Setup the recursion.
2734 */
2735 int rc = RTPathAppend(pszDir, RTPATH_MAX, ".");
2736 if (RT_SUCCESS(rc))
2737 {
2738 RTPathChangeToUnixSlashes(pszDir, true);
2739
2740 RTDIRENTRY Entry;
2741 rc = scmProcessDirTreeRecursion(pszDir, strlen(pszDir), &Entry, pSettingsStack, 0);
2742 }
2743 else
2744 RTMsgError("RTPathAppend: %Rrc\n", rc);
2745 return rc;
2746}
2747
2748
2749/**
2750 * Processes a file or directory specified as an command line argument.
2751 *
2752 * @returns IPRT status code
2753 * @param pszSomething What we found in the command line arguments.
2754 * @param pSettingsStack The settings stack (pointer to the top element).
2755 */
2756static int scmProcessSomething(const char *pszSomething, PSCMSETTINGS pSettingsStack)
2757{
2758 char szBuf[RTPATH_MAX];
2759 int rc = RTPathAbs(pszSomething, szBuf, sizeof(szBuf));
2760 if (RT_SUCCESS(rc))
2761 {
2762 RTPathChangeToUnixSlashes(szBuf, false /*fForce*/);
2763
2764 PSCMSETTINGS pSettings;
2765 rc = scmSettingsCreateForPath(&pSettings, &pSettingsStack->Base, szBuf);
2766 if (RT_SUCCESS(rc))
2767 {
2768 scmSettingsStackPush(&pSettingsStack, pSettings);
2769
2770 if (RTFileExists(szBuf))
2771 {
2772 const char *pszBasename = RTPathFilename(szBuf);
2773 if (pszBasename)
2774 {
2775 size_t cchBasename = strlen(pszBasename);
2776 rc = scmProcessFile(szBuf, pszBasename, cchBasename, pSettingsStack);
2777 }
2778 else
2779 {
2780 RTMsgError("RTPathFilename: NULL\n");
2781 rc = VERR_IS_A_DIRECTORY;
2782 }
2783 }
2784 else
2785 rc = scmProcessDirTree(szBuf, pSettingsStack);
2786
2787 PSCMSETTINGS pPopped = scmSettingsStackPop(&pSettingsStack);
2788 Assert(pPopped == pSettings); RT_NOREF_PV(pPopped);
2789 scmSettingsDestroy(pSettings);
2790 }
2791 else
2792 RTMsgError("scmSettingsInitStack: %Rrc\n", rc);
2793 }
2794 else
2795 RTMsgError("RTPathAbs: %Rrc\n", rc);
2796 return rc;
2797}
2798
2799/**
2800 * Print some stats.
2801 */
2802static void scmPrintStats(void)
2803{
2804 ScmVerbose(NULL, 0,
2805 g_fDryRun
2806 ? "%u out of %u file%s in %u dir%s would be modified (%u without rewriter%s, %u binar%s, %u not in svn, %u skipped)\n"
2807 : "%u out of %u file%s in %u dir%s was modified (%u without rewriter%s, %u binar%s, %u not in svn, %u skipped)\n",
2808 g_cFilesModified,
2809 g_cFilesProcessed, g_cFilesProcessed == 1 ? "" : "s",
2810 g_cDirsProcessed, g_cDirsProcessed == 1 ? "" : "s",
2811 g_cFilesNoRewriters, g_cFilesNoRewriters == 1 ? "" : "s",
2812 g_cFilesBinaries, g_cFilesBinaries == 1 ? "y" : "ies",
2813 g_cFilesNotInSvn, g_cFilesSkipped);
2814}
2815
2816/**
2817 * Display the rewriter actions.
2818 *
2819 * @returns RTEXITCODE_SUCCESS.
2820 */
2821static int scmHelpActions(void)
2822{
2823 RTPrintf("Available rewriter actions:\n");
2824 for (uint32_t i = 0; i < RT_ELEMENTS(g_papRewriterActions); i++)
2825 RTPrintf(" %s\n", g_papRewriterActions[i]->pszName);
2826 return RTEXITCODE_SUCCESS;
2827}
2828
2829/**
2830 * Display the default configuration.
2831 *
2832 * @returns RTEXITCODE_SUCCESS.
2833 */
2834static int scmHelpConfig(void)
2835{
2836 RTPrintf("Rewriter configuration:\n");
2837 for (size_t iCfg = 0; iCfg < RT_ELEMENTS(g_aConfigs); iCfg++)
2838 {
2839 RTPrintf("\n %s%s - %s:\n",
2840 g_aConfigs[iCfg].pszName, g_aConfigs[iCfg].fBinary ? " (binary)" : "", g_aConfigs[iCfg].pszFilePattern);
2841 for (size_t i = 0; i < g_aConfigs[iCfg].cRewriters; i++)
2842 RTPrintf(" %s\n", g_aConfigs[iCfg].paRewriters[i]->pszName);
2843 }
2844 return RTEXITCODE_SUCCESS;
2845}
2846
2847/**
2848 * Display the primary help text.
2849 *
2850 * @returns RTEXITCODE_SUCCESS.
2851 * @param paOpts Options.
2852 * @param cOpts Number of options.
2853 */
2854static int scmHelp(PCRTGETOPTDEF paOpts, size_t cOpts)
2855{
2856 RTPrintf("VirtualBox Source Code Massager\n"
2857 "\n"
2858 "Usage: %s [options] <files & dirs>\n"
2859 "\n"
2860 "General options:\n", g_szProgName);
2861 for (size_t i = 0; i < cOpts; i++)
2862 {
2863 /* Grouping. */
2864 switch (paOpts[i].iShort)
2865 {
2866 case SCMOPT_DIFF_IGNORE_EOL:
2867 RTPrintf("\nDiff options (dry runs):\n");
2868 break;
2869 case SCMOPT_CONVERT_EOL:
2870 RTPrintf("\nRewriter action options:\n");
2871 break;
2872 case SCMOPT_ONLY_SVN_DIRS:
2873 RTPrintf("\nInput selection options:\n");
2874 break;
2875 case SCMOPT_TREAT_AS:
2876 RTPrintf("\nMisc options:\n");
2877 break;
2878 }
2879
2880 size_t cExtraAdvance = 0;
2881 if ((paOpts[i].fFlags & RTGETOPT_REQ_MASK) == RTGETOPT_REQ_NOTHING)
2882 {
2883 cExtraAdvance = i + 1 < cOpts
2884 && ( strstr(paOpts[i+1].pszLong, "-no-") != NULL
2885 || strstr(paOpts[i+1].pszLong, "-not-") != NULL
2886 || strstr(paOpts[i+1].pszLong, "-dont-") != NULL
2887 || strstr(paOpts[i+1].pszLong, "-unrestricted-") != NULL
2888 || (paOpts[i].iShort == 'q' && paOpts[i+1].iShort == 'v')
2889 || (paOpts[i].iShort == 'd' && paOpts[i+1].iShort == 'D')
2890 );
2891 if (cExtraAdvance)
2892 RTPrintf(" %s, %s\n", paOpts[i].pszLong, paOpts[i + 1].pszLong);
2893 else if (paOpts[i].iShort != SCMOPT_NO_UPDATE_LICENSE || i + 6 >= cOpts /* paranoia */)
2894 RTPrintf(" %s\n", paOpts[i].pszLong);
2895 else
2896 {
2897 RTPrintf(" %s,\n"
2898 " %s,\n"
2899 " %s,\n"
2900 " %s,\n"
2901 " %s,\n"
2902 " %s,\n"
2903 " %s\n",
2904 paOpts[i].pszLong,
2905 paOpts[i + 1].pszLong,
2906 paOpts[i + 2].pszLong,
2907 paOpts[i + 3].pszLong,
2908 paOpts[i + 4].pszLong,
2909 paOpts[i + 5].pszLong,
2910 paOpts[i + 6].pszLong);
2911 cExtraAdvance = 6;
2912 }
2913 }
2914 else if ((paOpts[i].fFlags & RTGETOPT_REQ_MASK) == RTGETOPT_REQ_STRING)
2915 switch (paOpts[i].iShort)
2916 {
2917 case SCMOPT_DEL_ACTION:
2918 RTPrintf(" %s pattern\n", paOpts[i].pszLong);
2919 break;
2920 case SCMOPT_FILTER_OUT_DIRS:
2921 case SCMOPT_FILTER_FILES:
2922 case SCMOPT_FILTER_OUT_FILES:
2923 RTPrintf(" %s multi-pattern\n", paOpts[i].pszLong);
2924 break;
2925 default:
2926 RTPrintf(" %s string\n", paOpts[i].pszLong);
2927 }
2928 else
2929 RTPrintf(" %s value\n", paOpts[i].pszLong);
2930 switch (paOpts[i].iShort)
2931 {
2932 case 'd':
2933 case 'D': RTPrintf(" Default: --dry-run\n"); break;
2934 case SCMOPT_CHECK_RUN: RTPrintf(" Default: --dry-run\n"); break;
2935 case 'f': RTPrintf(" Default: none\n"); break;
2936 case 'q':
2937 case 'v': RTPrintf(" Default: -vv\n"); break;
2938 case SCMOPT_HELP_CONFIG: RTPrintf(" Shows the standard file rewriter configurations.\n"); break;
2939 case SCMOPT_HELP_ACTIONS: RTPrintf(" Shows the available rewriter actions.\n"); break;
2940
2941 case SCMOPT_DIFF_IGNORE_EOL: RTPrintf(" Default: false\n"); break;
2942 case SCMOPT_DIFF_IGNORE_SPACE: RTPrintf(" Default: false\n"); break;
2943 case SCMOPT_DIFF_IGNORE_LEADING_SPACE: RTPrintf(" Default: false\n"); break;
2944 case SCMOPT_DIFF_IGNORE_TRAILING_SPACE: RTPrintf(" Default: false\n"); break;
2945 case SCMOPT_DIFF_SPECIAL_CHARS: RTPrintf(" Default: true\n"); break;
2946
2947 case SCMOPT_CONVERT_EOL: RTPrintf(" Default: %RTbool\n", g_Defaults.fConvertEol); break;
2948 case SCMOPT_CONVERT_TABS: RTPrintf(" Default: %RTbool\n", g_Defaults.fConvertTabs); break;
2949 case SCMOPT_FORCE_FINAL_EOL: RTPrintf(" Default: %RTbool\n", g_Defaults.fForceFinalEol); break;
2950 case SCMOPT_FORCE_TRAILING_LINE: RTPrintf(" Default: %RTbool\n", g_Defaults.fForceTrailingLine); break;
2951 case SCMOPT_STRIP_TRAILING_BLANKS: RTPrintf(" Default: %RTbool\n", g_Defaults.fStripTrailingBlanks); break;
2952 case SCMOPT_STRIP_TRAILING_LINES: RTPrintf(" Default: %RTbool\n", g_Defaults.fStripTrailingLines); break;
2953 case SCMOPT_FIX_FLOWER_BOX_MARKERS: RTPrintf(" Default: %RTbool\n", g_Defaults.fFixFlowerBoxMarkers); break;
2954 case SCMOPT_MIN_BLANK_LINES_BEFORE_FLOWER_BOX_MARKERS: RTPrintf(" Default: %u\n", g_Defaults.cMinBlankLinesBeforeFlowerBoxMakers); break;
2955
2956 case SCMOPT_FIX_HEADER_GUARDS:
2957 RTPrintf(" Fix header guards and #pragma once. Default: %RTbool\n", g_Defaults.fFixHeaderGuards);
2958 break;
2959 case SCMOPT_PRAGMA_ONCE:
2960 RTPrintf(" Whether to include #pragma once with the header guard. Default: %RTbool\n", g_Defaults.fPragmaOnce);
2961 break;
2962 case SCMOPT_FIX_HEADER_GUARD_ENDIF:
2963 RTPrintf(" Whether to fix the #endif of a header guard. Default: %RTbool\n", g_Defaults.fFixHeaderGuardEndif);
2964 break;
2965 case SCMOPT_ENDIF_GUARD_COMMENT:
2966 RTPrintf(" Put a comment on the header guard #endif or not. Default: %RTbool\n", g_Defaults.fEndifGuardComment);
2967 break;
2968 case SCMOPT_GUARD_RELATIVE_TO_DIR:
2969 RTPrintf(" Header guard should be normalized relative to given dir.\n"
2970 " When relative to settings files, no preceeding slash.\n"
2971 " Header relative directory specification: {dir} and {parent}\n"
2972 " If empty no normalization takes place. Default: '%s'\n", g_Defaults.pszGuardRelativeToDir);
2973 break;
2974 case SCMOPT_GUARD_PREFIX:
2975 RTPrintf(" Prefix to use with --guard-relative-to-dir. Default: %s\n", g_Defaults.pszGuardPrefix);
2976 break;
2977 case SCMOPT_FIX_TODOS:
2978 RTPrintf(" Fix @todo statements so doxygen sees them. Default: %RTbool\n", g_Defaults.fFixTodos);
2979 break;
2980 case SCMOPT_FIX_ERR_H:
2981 RTPrintf(" Fix err.h/errcore.h usage. Default: %RTbool\n", g_Defaults.fFixErrH);
2982 break;
2983 case SCMOPT_ONLY_GUEST_HOST_PAGE:
2984 RTPrintf(" No PAGE_SIZE, PAGE_SHIFT or PAGE_OFFSET_MASK allowed, must have\n"
2985 " GUEST_ or HOST_ prefix. Also forbids use of PAGE_BASE_MASK,\n"
2986 " PAGE_BASE_HC_MASK, PAGE_BASE_GC_MASK, PAGE_ADDRESS,\n"
2987 " PHYS_PAGE_ADDRESS. Default: %RTbool\n", g_Defaults.fOnlyGuestHostPage);
2988 break;
2989 case SCMOPT_NO_ASM_MEM_PAGE_USE:
2990 RTPrintf(" No ASMMemIsZeroPage or ASMMemZeroPage allowed, must instead use\n"
2991 " ASMMemIsZero and RT_BZERO with appropriate page size. Default: %RTbool\n",
2992 g_Defaults.fNoASMMemPageUse);
2993 break;
2994 case SCMOPT_NO_RC_USE:
2995 RTPrintf(" No rc declaration allowed, must instead use\n"
2996 " vrc for IPRT status codes and hrc for COM status codes. Default: %RTbool\n",
2997 g_Defaults.fOnlyHrcVrcInsteadOfRc);
2998 break;
2999 case SCMOPT_STANDARIZE_KMK:
3000 RTPrintf(" Clean up kmk files (the makefile-kmk action). Default: %RTbool\n", g_Defaults.fStandarizeKmk);
3001 break;
3002 case SCMOPT_UPDATE_COPYRIGHT_YEAR:
3003 RTPrintf(" Update the copyright year. Default: %RTbool\n", g_Defaults.fUpdateCopyrightYear);
3004 break;
3005 case SCMOPT_EXTERNAL_COPYRIGHT:
3006 RTPrintf(" Only external copyright holders. Default: %RTbool\n", g_Defaults.fExternalCopyright);
3007 break;
3008 case SCMOPT_NO_UPDATE_LICENSE:
3009 RTPrintf(" License selection. Default: --license-ose-gpl\n");
3010 break;
3011
3012 case SCMOPT_LGPL_DISCLAIMER:
3013 RTPrintf(" Include LGPL version disclaimer. Default: --no-lgpl-disclaimer\n");
3014 break;
3015
3016 case SCMOPT_SET_SVN_EOL: RTPrintf(" Default: %RTbool\n", g_Defaults.fSetSvnEol); break;
3017 case SCMOPT_SET_SVN_EXECUTABLE: RTPrintf(" Default: %RTbool\n", g_Defaults.fSetSvnExecutable); break;
3018 case SCMOPT_SET_SVN_KEYWORDS: RTPrintf(" Default: %RTbool\n", g_Defaults.fSetSvnKeywords); break;
3019 case SCMOPT_SKIP_SVN_SYNC_PROCESS: RTPrintf(" Default: %RTbool\n", g_Defaults.fSkipSvnSyncProcess); break;
3020 case SCMOPT_SKIP_UNICODE_CHECKS: RTPrintf(" Default: %RTbool\n", g_Defaults.fSkipUnicodeChecks); break;
3021 case SCMOPT_TAB_SIZE: RTPrintf(" Default: %u\n", g_Defaults.cchTab); break;
3022 case SCMOPT_WIDTH: RTPrintf(" Default: %u\n", g_Defaults.cchWidth); break;
3023
3024 case SCMOPT_ONLY_SVN_DIRS: RTPrintf(" Default: %RTbool\n", g_Defaults.fOnlySvnDirs); break;
3025 case SCMOPT_ONLY_SVN_FILES: RTPrintf(" Default: %RTbool\n", g_Defaults.fOnlySvnFiles); break;
3026 case SCMOPT_FILTER_OUT_DIRS: RTPrintf(" Default: %s\n", g_Defaults.pszFilterOutDirs); break;
3027 case SCMOPT_FILTER_FILES: RTPrintf(" Default: %s\n", g_Defaults.pszFilterFiles); break;
3028 case SCMOPT_FILTER_OUT_FILES: RTPrintf(" Default: %s\n", g_Defaults.pszFilterOutFiles); break;
3029
3030 case SCMOPT_TREAT_AS:
3031 RTPrintf(" For treat the input file(s) differently, restting any --add-action.\n"
3032 " If the value is empty defaults will be used again. Possible values:\n");
3033 for (size_t iCfg = 0; iCfg < RT_ELEMENTS(g_aConfigs); iCfg++)
3034 RTPrintf(" %s (%s)\n", g_aConfigs[iCfg].pszName, g_aConfigs[iCfg].pszFilePattern);
3035 break;
3036
3037 case SCMOPT_ADD_ACTION:
3038 RTPrintf(" Adds a rewriter action. The first use after a --treat-as will copy and\n"
3039 " the action list selected by the --treat-as. The action list will be\n"
3040 " flushed by --treat-as.\n");
3041 break;
3042
3043 case SCMOPT_DEL_ACTION:
3044 RTPrintf(" Deletes one or more rewriter action (pattern). Best used after\n"
3045 " a --treat-as.\n");
3046 break;
3047
3048 default: AssertMsgFailed(("i=%d %d %s\n", i, paOpts[i].iShort, paOpts[i].pszLong));
3049 }
3050 i += cExtraAdvance;
3051 }
3052
3053 return RTEXITCODE_SUCCESS;
3054}
3055
3056int main(int argc, char **argv)
3057{
3058 int rc = RTR3InitExe(argc, &argv, 0);
3059 if (RT_FAILURE(rc))
3060 return 1;
3061
3062 /*
3063 * Init the current year.
3064 */
3065 RTTIMESPEC Now;
3066 RTTIME Time;
3067 RTTimeExplode(&Time, RTTimeNow(&Now));
3068 g_uYear = Time.i32Year;
3069
3070 /*
3071 * Init the settings.
3072 */
3073 PSCMSETTINGS pSettings;
3074 rc = scmSettingsCreate(&pSettings, &g_Defaults);
3075 if (RT_FAILURE(rc))
3076 {
3077 RTMsgError("scmSettingsCreate: %Rrc\n", rc);
3078 return 1;
3079 }
3080
3081 /*
3082 * Parse arguments and process input in order (because this is the only
3083 * thing that works at the moment).
3084 */
3085 static RTGETOPTDEF s_aOpts[14 + RT_ELEMENTS(g_aScmOpts)] =
3086 {
3087 { "--dry-run", 'd', RTGETOPT_REQ_NOTHING },
3088 { "--real-run", 'D', RTGETOPT_REQ_NOTHING },
3089 { "--check-run", SCMOPT_CHECK_RUN, RTGETOPT_REQ_NOTHING },
3090 { "--file-filter", 'f', RTGETOPT_REQ_STRING },
3091 { "--quiet", 'q', RTGETOPT_REQ_NOTHING },
3092 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
3093 { "--diff-ignore-eol", SCMOPT_DIFF_IGNORE_EOL, RTGETOPT_REQ_NOTHING },
3094 { "--diff-no-ignore-eol", SCMOPT_DIFF_NO_IGNORE_EOL, RTGETOPT_REQ_NOTHING },
3095 { "--diff-ignore-space", SCMOPT_DIFF_IGNORE_SPACE, RTGETOPT_REQ_NOTHING },
3096 { "--diff-no-ignore-space", SCMOPT_DIFF_NO_IGNORE_SPACE, RTGETOPT_REQ_NOTHING },
3097 { "--diff-ignore-leading-space", SCMOPT_DIFF_IGNORE_LEADING_SPACE, RTGETOPT_REQ_NOTHING },
3098 { "--diff-no-ignore-leading-space", SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE, RTGETOPT_REQ_NOTHING },
3099 { "--diff-ignore-trailing-space", SCMOPT_DIFF_IGNORE_TRAILING_SPACE, RTGETOPT_REQ_NOTHING },
3100 { "--diff-no-ignore-trailing-space", SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE, RTGETOPT_REQ_NOTHING },
3101 { "--diff-special-chars", SCMOPT_DIFF_SPECIAL_CHARS, RTGETOPT_REQ_NOTHING },
3102 { "--diff-no-special-chars", SCMOPT_DIFF_NO_SPECIAL_CHARS, RTGETOPT_REQ_NOTHING },
3103 };
3104 memcpy(&s_aOpts[RT_ELEMENTS(s_aOpts) - RT_ELEMENTS(g_aScmOpts)], &g_aScmOpts[0], sizeof(g_aScmOpts));
3105
3106 bool fCheckRun = false;
3107 RTGETOPTUNION ValueUnion;
3108 RTGETOPTSTATE GetOptState;
3109 rc = RTGetOptInit(&GetOptState, argc, argv, &s_aOpts[0], RT_ELEMENTS(s_aOpts), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
3110 AssertReleaseRCReturn(rc, 1);
3111
3112 while ( (rc = RTGetOpt(&GetOptState, &ValueUnion)) != 0
3113 && rc != VINF_GETOPT_NOT_OPTION)
3114 {
3115 switch (rc)
3116 {
3117 case 'd':
3118 g_fDryRun = true;
3119 fCheckRun = false;
3120 break;
3121 case 'D':
3122 g_fDryRun = fCheckRun = false;
3123 break;
3124 case SCMOPT_CHECK_RUN:
3125 g_fDryRun = fCheckRun = true;
3126 break;
3127
3128 case 'f':
3129 g_pszFileFilter = ValueUnion.psz;
3130 break;
3131
3132 case 'h':
3133 return scmHelp(s_aOpts, RT_ELEMENTS(s_aOpts));
3134
3135 case SCMOPT_HELP_CONFIG:
3136 return scmHelpConfig();
3137
3138 case SCMOPT_HELP_ACTIONS:
3139 return scmHelpActions();
3140
3141 case 'q':
3142 g_iVerbosity = 0;
3143 break;
3144
3145 case 'v':
3146 g_iVerbosity++;
3147 break;
3148
3149 case 'V':
3150 {
3151 /* The following is assuming that svn does it's job here. */
3152 static const char s_szRev[] = "$Revision: 106499 $";
3153 const char *psz = RTStrStripL(strchr(s_szRev, ' '));
3154 RTPrintf("r%.*s\n", strchr(psz, ' ') - psz, psz);
3155 return 0;
3156 }
3157
3158 case SCMOPT_DIFF_IGNORE_EOL:
3159 g_fDiffIgnoreEol = true;
3160 break;
3161 case SCMOPT_DIFF_NO_IGNORE_EOL:
3162 g_fDiffIgnoreEol = false;
3163 break;
3164
3165 case SCMOPT_DIFF_IGNORE_SPACE:
3166 g_fDiffIgnoreTrailingWS = g_fDiffIgnoreLeadingWS = true;
3167 break;
3168 case SCMOPT_DIFF_NO_IGNORE_SPACE:
3169 g_fDiffIgnoreTrailingWS = g_fDiffIgnoreLeadingWS = false;
3170 break;
3171
3172 case SCMOPT_DIFF_IGNORE_LEADING_SPACE:
3173 g_fDiffIgnoreLeadingWS = true;
3174 break;
3175 case SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE:
3176 g_fDiffIgnoreLeadingWS = false;
3177 break;
3178
3179 case SCMOPT_DIFF_IGNORE_TRAILING_SPACE:
3180 g_fDiffIgnoreTrailingWS = true;
3181 break;
3182 case SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE:
3183 g_fDiffIgnoreTrailingWS = false;
3184 break;
3185
3186 case SCMOPT_DIFF_SPECIAL_CHARS:
3187 g_fDiffSpecialChars = true;
3188 break;
3189 case SCMOPT_DIFF_NO_SPECIAL_CHARS:
3190 g_fDiffSpecialChars = false;
3191 break;
3192
3193 default:
3194 {
3195 int rc2 = scmSettingsBaseHandleOpt(&pSettings->Base, rc, &ValueUnion, "/", 1);
3196 if (RT_SUCCESS(rc2))
3197 break;
3198 if (rc2 != VERR_GETOPT_UNKNOWN_OPTION)
3199 return 2;
3200 return RTGetOptPrintError(rc, &ValueUnion);
3201 }
3202 }
3203 }
3204
3205 /*
3206 * Process non-options.
3207 */
3208 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
3209 if (rc == VINF_GETOPT_NOT_OPTION)
3210 {
3211 ScmSvnInit();
3212
3213 bool fWarned = g_fDryRun;
3214 while (rc == VINF_GETOPT_NOT_OPTION)
3215 {
3216 if (!fWarned)
3217 {
3218 RTPrintf("%s: Warning! This program will make changes to your source files and\n"
3219 "%s: there is a slight risk that bugs or a full disk may cause\n"
3220 "%s: LOSS OF DATA. So, please make sure you have checked in\n"
3221 "%s: all your changes already. If you didn't, then don't blame\n"
3222 "%s: anyone for not warning you!\n"
3223 "%s:\n"
3224 "%s: Press any key to continue...\n",
3225 g_szProgName, g_szProgName, g_szProgName, g_szProgName, g_szProgName,
3226 g_szProgName, g_szProgName);
3227 RTStrmGetCh(g_pStdIn);
3228 fWarned = true;
3229 }
3230
3231 rc = scmProcessSomething(ValueUnion.psz, pSettings);
3232 if (RT_FAILURE(rc))
3233 {
3234 rcExit = RTEXITCODE_FAILURE;
3235 break;
3236 }
3237
3238 /* next */
3239 rc = RTGetOpt(&GetOptState, &ValueUnion);
3240 if (RT_FAILURE(rc))
3241 rcExit = RTGetOptPrintError(rc, &ValueUnion);
3242 }
3243
3244 scmPrintStats();
3245 ScmSvnTerm();
3246 }
3247 else
3248 RTMsgWarning("No files or directories specified. Doing nothing");
3249
3250 scmSettingsDestroy(pSettings);
3251
3252 /* If we're in checking mode, fail if any files needed modification. */
3253 if ( rcExit == RTEXITCODE_SUCCESS
3254 && fCheckRun
3255 && g_cFilesModified > 0)
3256 {
3257 RTMsgError("Checking mode failed! %u file%s needs modifications", g_cFilesBinaries, g_cFilesBinaries > 1 ? "s" : "");
3258 rcExit = RTEXITCODE_FAILURE;
3259 }
3260
3261 /* Fail if any files require manual repair. */
3262 if (g_cFilesRequiringManualFixing > 0)
3263 {
3264 RTMsgError("%u file%s needs manual modifications", g_cFilesRequiringManualFixing,
3265 g_cFilesRequiringManualFixing > 1 ? "s" : "");
3266 if (rcExit == RTEXITCODE_SUCCESS)
3267 rcExit = RTEXITCODE_FAILURE;
3268 }
3269
3270 return rcExit;
3271}
3272
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette