VirtualBox

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

Last change on this file since 76565 was 76555, checked in by vboxsync, 6 years ago

scm: made header guard #endif massaging configurable and for the time being optional.

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