VirtualBox

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

Last change on this file since 69355 was 69355, checked in by vboxsync, 7 years ago

scm: nsis files

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 84.6 KB
Line 
1/* $Id: scm.cpp 69355 2017-10-26 14:38:46Z vboxsync $ */
2/** @file
3 * IPRT Testcase / Tool - Source Code Massager.
4 */
5
6/*
7 * Copyright (C) 2010-2016 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_TODOS,
78 SCMOPT_NO_FIX_TODOS,
79 SCMOPT_UPDATE_COPYRIGHT_YEAR,
80 SCMOPT_NO_UPDATE_COPYRIGHT_YEAR,
81 SCMOPT_EXTERNAL_COPYRIGHT,
82 SCMOPT_NO_EXTERNAL_COPYRIGHT,
83 SCMOPT_NO_UPDATE_LICENSE,
84 SCMOPT_LICENSE_OSE_GPL,
85 SCMOPT_LICENSE_OSE_DUAL_GPL_CDDL,
86 SCMOPT_LICENSE_OSE_CDDL,
87 SCMOPT_LICENSE_LGPL,
88 SCMOPT_LICENSE_MIT,
89 SCMOPT_LICENSE_BASED_ON_MIT,
90 SCMOPT_LGPL_DISCLAIMER,
91 SCMOPT_NO_LGPL_DISCLAIMER,
92 SCMOPT_MIN_BLANK_LINES_BEFORE_FLOWER_BOX_MARKERS,
93 SCMOPT_ONLY_SVN_DIRS,
94 SCMOPT_NOT_ONLY_SVN_DIRS,
95 SCMOPT_ONLY_SVN_FILES,
96 SCMOPT_NOT_ONLY_SVN_FILES,
97 SCMOPT_SET_SVN_EOL,
98 SCMOPT_DONT_SET_SVN_EOL,
99 SCMOPT_SET_SVN_EXECUTABLE,
100 SCMOPT_DONT_SET_SVN_EXECUTABLE,
101 SCMOPT_SET_SVN_KEYWORDS,
102 SCMOPT_DONT_SET_SVN_KEYWORDS,
103 SCMOPT_TAB_SIZE,
104 SCMOPT_WIDTH,
105 SCMOPT_TREAT_AS,
106 SCMOPT_FILTER_OUT_DIRS,
107 SCMOPT_FILTER_FILES,
108 SCMOPT_FILTER_OUT_FILES,
109 SCMOPT_LAST_SETTINGS = SCMOPT_FILTER_OUT_FILES,
110 //
111 SCMOPT_DIFF_IGNORE_EOL,
112 SCMOPT_DIFF_NO_IGNORE_EOL,
113 SCMOPT_DIFF_IGNORE_SPACE,
114 SCMOPT_DIFF_NO_IGNORE_SPACE,
115 SCMOPT_DIFF_IGNORE_LEADING_SPACE,
116 SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE,
117 SCMOPT_DIFF_IGNORE_TRAILING_SPACE,
118 SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE,
119 SCMOPT_DIFF_SPECIAL_CHARS,
120 SCMOPT_DIFF_NO_SPECIAL_CHARS,
121 SCMOPT_END
122} SCMOPT;
123
124
125/*********************************************************************************************************************************
126* Global Variables *
127*********************************************************************************************************************************/
128const char g_szTabSpaces[16+1] = " ";
129const char g_szAsterisks[255+1] =
130"****************************************************************************************************"
131"****************************************************************************************************"
132"*******************************************************";
133const char g_szSpaces[255+1] =
134" "
135" "
136" ";
137static const char g_szProgName[] = "scm";
138static const char *g_pszChangedSuff = "";
139static bool g_fDryRun = true;
140static bool g_fDiffSpecialChars = true;
141static bool g_fDiffIgnoreEol = false;
142static bool g_fDiffIgnoreLeadingWS = false;
143static bool g_fDiffIgnoreTrailingWS = false;
144static int g_iVerbosity = 2;//99; //0;
145uint32_t g_uYear = 0; /**< The current year. */
146/** @name Statistics
147 * @{ */
148static uint32_t g_cDirsProcessed = 0;
149static uint32_t g_cFilesProcessed = 0;
150static uint32_t g_cFilesModified = 0;
151static uint32_t g_cFilesSkipped = 0;
152static uint32_t g_cFilesNotInSvn = 0;
153static uint32_t g_cFilesNoRewriters = 0;
154static uint32_t g_cFilesBinaries = 0;
155/** @} */
156
157/** The global settings. */
158static SCMSETTINGSBASE const g_Defaults =
159{
160 /* .fConvertEol = */ true,
161 /* .fConvertTabs = */ true,
162 /* .fForceFinalEol = */ true,
163 /* .fForceTrailingLine = */ false,
164 /* .fStripTrailingBlanks = */ true,
165 /* .fStripTrailingLines = */ true,
166 /* .fFixFlowerBoxMarkers = */ true,
167 /* .cMinBlankLinesBeforeFlowerBoxMakers = */ 2,
168 /* .fFixTodos = */ true,
169 /* .fUpdateCopyrightYear = */ false,
170 /* .fExternalCopyright = */ false,
171 /* .fLgplDisclaimer = */ false,
172 /* .enmUpdateLicense = */ kScmLicense_OseGpl,
173 /* .fOnlySvnFiles = */ false,
174 /* .fOnlySvnDirs = */ false,
175 /* .fSetSvnEol = */ false,
176 /* .fSetSvnExecutable = */ false,
177 /* .fSetSvnKeywords = */ false,
178 /* .cchTab = */ 8,
179 /* .cchWidth = */ 130,
180 /* .pszTreatAsName = */ NULL,
181 /* .pszFilterFiles = */ (char *)"",
182 /* .pszFilterOutFiles = */ (char *)"*.exe|*.com|20*-*-*.log",
183 /* .pszFilterOutDirs = */ (char *)".svn|.hg|.git|CVS",
184};
185
186/** Option definitions for the base settings. */
187static RTGETOPTDEF g_aScmOpts[] =
188{
189 { "--convert-eol", SCMOPT_CONVERT_EOL, RTGETOPT_REQ_NOTHING },
190 { "--no-convert-eol", SCMOPT_NO_CONVERT_EOL, RTGETOPT_REQ_NOTHING },
191 { "--convert-tabs", SCMOPT_CONVERT_TABS, RTGETOPT_REQ_NOTHING },
192 { "--no-convert-tabs", SCMOPT_NO_CONVERT_TABS, RTGETOPT_REQ_NOTHING },
193 { "--force-final-eol", SCMOPT_FORCE_FINAL_EOL, RTGETOPT_REQ_NOTHING },
194 { "--no-force-final-eol", SCMOPT_NO_FORCE_FINAL_EOL, RTGETOPT_REQ_NOTHING },
195 { "--force-trailing-line", SCMOPT_FORCE_TRAILING_LINE, RTGETOPT_REQ_NOTHING },
196 { "--no-force-trailing-line", SCMOPT_NO_FORCE_TRAILING_LINE, RTGETOPT_REQ_NOTHING },
197 { "--strip-trailing-blanks", SCMOPT_STRIP_TRAILING_BLANKS, RTGETOPT_REQ_NOTHING },
198 { "--no-strip-trailing-blanks", SCMOPT_NO_STRIP_TRAILING_BLANKS, RTGETOPT_REQ_NOTHING },
199 { "--strip-trailing-lines", SCMOPT_STRIP_TRAILING_LINES, RTGETOPT_REQ_NOTHING },
200 { "--strip-no-trailing-lines", SCMOPT_NO_STRIP_TRAILING_LINES, RTGETOPT_REQ_NOTHING },
201 { "--min-blank-lines-before-flower-box-makers", SCMOPT_MIN_BLANK_LINES_BEFORE_FLOWER_BOX_MARKERS, RTGETOPT_REQ_UINT8 },
202 { "--fix-flower-box-markers", SCMOPT_FIX_FLOWER_BOX_MARKERS, RTGETOPT_REQ_NOTHING },
203 { "--no-fix-flower-box-markers", SCMOPT_NO_FIX_FLOWER_BOX_MARKERS, RTGETOPT_REQ_NOTHING },
204 { "--fix-todos", SCMOPT_FIX_TODOS, RTGETOPT_REQ_NOTHING },
205 { "--no-fix-todos", SCMOPT_NO_FIX_TODOS, RTGETOPT_REQ_NOTHING },
206 { "--update-copyright-year", SCMOPT_UPDATE_COPYRIGHT_YEAR, RTGETOPT_REQ_NOTHING },
207 { "--no-update-copyright-year", SCMOPT_NO_UPDATE_COPYRIGHT_YEAR, RTGETOPT_REQ_NOTHING },
208 { "--external-copyright", SCMOPT_EXTERNAL_COPYRIGHT, RTGETOPT_REQ_NOTHING },
209 { "--no-external-copyright", SCMOPT_NO_EXTERNAL_COPYRIGHT, RTGETOPT_REQ_NOTHING },
210 { "--no-update-license", SCMOPT_NO_UPDATE_LICENSE, RTGETOPT_REQ_NOTHING },
211 { "--license-ose-gpl", SCMOPT_LICENSE_OSE_GPL, RTGETOPT_REQ_NOTHING },
212 { "--license-ose-dual", SCMOPT_LICENSE_OSE_DUAL_GPL_CDDL, RTGETOPT_REQ_NOTHING },
213 { "--license-ose-cddl", SCMOPT_LICENSE_OSE_CDDL, RTGETOPT_REQ_NOTHING },
214 { "--license-lgpl", SCMOPT_LICENSE_LGPL, RTGETOPT_REQ_NOTHING },
215 { "--license-mit", SCMOPT_LICENSE_MIT, RTGETOPT_REQ_NOTHING },
216 { "--license-based-on-mit", SCMOPT_LICENSE_BASED_ON_MIT, RTGETOPT_REQ_NOTHING },
217 { "--lgpl-disclaimer", SCMOPT_LGPL_DISCLAIMER, RTGETOPT_REQ_NOTHING },
218 { "--no-lgpl-disclaimer", SCMOPT_NO_LGPL_DISCLAIMER, RTGETOPT_REQ_NOTHING },
219 { "--only-svn-dirs", SCMOPT_ONLY_SVN_DIRS, RTGETOPT_REQ_NOTHING },
220 { "--not-only-svn-dirs", SCMOPT_NOT_ONLY_SVN_DIRS, RTGETOPT_REQ_NOTHING },
221 { "--only-svn-files", SCMOPT_ONLY_SVN_FILES, RTGETOPT_REQ_NOTHING },
222 { "--not-only-svn-files", SCMOPT_NOT_ONLY_SVN_FILES, RTGETOPT_REQ_NOTHING },
223 { "--set-svn-eol", SCMOPT_SET_SVN_EOL, RTGETOPT_REQ_NOTHING },
224 { "--dont-set-svn-eol", SCMOPT_DONT_SET_SVN_EOL, RTGETOPT_REQ_NOTHING },
225 { "--set-svn-executable", SCMOPT_SET_SVN_EXECUTABLE, RTGETOPT_REQ_NOTHING },
226 { "--dont-set-svn-executable", SCMOPT_DONT_SET_SVN_EXECUTABLE, RTGETOPT_REQ_NOTHING },
227 { "--set-svn-keywords", SCMOPT_SET_SVN_KEYWORDS, RTGETOPT_REQ_NOTHING },
228 { "--dont-set-svn-keywords", SCMOPT_DONT_SET_SVN_KEYWORDS, RTGETOPT_REQ_NOTHING },
229 { "--tab-size", SCMOPT_TAB_SIZE, RTGETOPT_REQ_UINT8 },
230 { "--width", SCMOPT_WIDTH, RTGETOPT_REQ_UINT8 },
231 { "--treat-as", SCMOPT_TREAT_AS, RTGETOPT_REQ_STRING },
232 { "--filter-out-dirs", SCMOPT_FILTER_OUT_DIRS, RTGETOPT_REQ_STRING },
233 { "--filter-files", SCMOPT_FILTER_FILES, RTGETOPT_REQ_STRING },
234 { "--filter-out-files", SCMOPT_FILTER_OUT_FILES, RTGETOPT_REQ_STRING },
235};
236
237/** Consider files matching the following patterns (base names only). */
238static const char *g_pszFileFilter = NULL;
239
240static PFNSCMREWRITER const g_aRewritersFor_Makefile_kup[] =
241{
242 rewrite_SvnNoExecutable,
243 rewrite_Makefile_kup
244};
245
246static PFNSCMREWRITER const g_aRewritersFor_Makefile_kmk[] =
247{
248 rewrite_ForceNativeEol,
249 rewrite_StripTrailingBlanks,
250 rewrite_AdjustTrailingLines,
251 rewrite_SvnNoExecutable,
252 rewrite_SvnKeywords,
253 rewrite_Copyright_HashComment,
254 rewrite_Makefile_kmk
255};
256
257static PFNSCMREWRITER const g_aRewritersFor_OtherMakefiles[] =
258{
259 rewrite_ForceNativeEol,
260 rewrite_StripTrailingBlanks,
261 rewrite_AdjustTrailingLines,
262 rewrite_SvnNoExecutable,
263 rewrite_SvnKeywords,
264 rewrite_Copyright_HashComment,
265};
266
267static PFNSCMREWRITER const g_aRewritersFor_C_and_CPP[] =
268{
269 rewrite_ForceNativeEol,
270 rewrite_ExpandTabs,
271 rewrite_StripTrailingBlanks,
272 rewrite_AdjustTrailingLines,
273 rewrite_SvnNoExecutable,
274 rewrite_SvnKeywords,
275 rewrite_Copyright_CstyleComment,
276 rewrite_FixFlowerBoxMarkers,
277 rewrite_Fix_C_and_CPP_Todos,
278 rewrite_C_and_CPP
279};
280
281static PFNSCMREWRITER const g_aRewritersFor_H_and_HPP[] =
282{
283 rewrite_ForceNativeEol,
284 rewrite_ExpandTabs,
285 rewrite_StripTrailingBlanks,
286 rewrite_AdjustTrailingLines,
287 rewrite_SvnNoExecutable,
288 rewrite_Copyright_CstyleComment,
289 rewrite_C_and_CPP
290};
291
292static PFNSCMREWRITER const g_aRewritersFor_RC[] =
293{
294 rewrite_ForceNativeEol,
295 rewrite_ExpandTabs,
296 rewrite_StripTrailingBlanks,
297 rewrite_AdjustTrailingLines,
298 rewrite_SvnNoExecutable,
299 rewrite_SvnKeywords,
300 rewrite_Copyright_CstyleComment,
301};
302
303static PFNSCMREWRITER const g_aRewritersFor_DTrace[] =
304{
305 rewrite_ForceNativeEol,
306 rewrite_ExpandTabs,
307 rewrite_StripTrailingBlanks,
308 rewrite_AdjustTrailingLines,
309 rewrite_SvnKeywords,
310 rewrite_Copyright_CstyleComment,
311};
312
313static PFNSCMREWRITER const g_aRewritersFor_DSL[] =
314{
315 rewrite_ForceNativeEol,
316 rewrite_ExpandTabs,
317 rewrite_StripTrailingBlanks,
318 rewrite_AdjustTrailingLines,
319 rewrite_SvnNoExecutable,
320 rewrite_SvnKeywords,
321 rewrite_Copyright_CstyleComment,
322};
323
324static PFNSCMREWRITER const g_aRewritersFor_ASM[] =
325{
326 rewrite_ForceNativeEol,
327 rewrite_ExpandTabs,
328 rewrite_StripTrailingBlanks,
329 rewrite_AdjustTrailingLines,
330 rewrite_SvnNoExecutable,
331 rewrite_SvnKeywords,
332 rewrite_Copyright_SemicolonComment,
333};
334
335static PFNSCMREWRITER const g_aRewritersFor_DEF[] =
336{
337 rewrite_ForceNativeEol,
338 rewrite_ExpandTabs,
339 rewrite_StripTrailingBlanks,
340 rewrite_AdjustTrailingLines,
341 rewrite_SvnNoExecutable,
342 rewrite_SvnKeywords,
343 rewrite_Copyright_SemicolonComment,
344};
345
346static PFNSCMREWRITER const g_aRewritersFor_ShellScripts[] =
347{
348 rewrite_ForceLF,
349 rewrite_ExpandTabs,
350 rewrite_StripTrailingBlanks,
351 rewrite_Copyright_HashComment,
352};
353
354static PFNSCMREWRITER const g_aRewritersFor_BatchFiles[] =
355{
356 rewrite_ForceCRLF,
357 rewrite_ExpandTabs,
358 rewrite_StripTrailingBlanks,
359 rewrite_Copyright_RemComment,
360};
361
362static PFNSCMREWRITER const g_aRewritersFor_BasicScripts[] =
363{
364 rewrite_ForceCRLF,
365 rewrite_ExpandTabs,
366 rewrite_StripTrailingBlanks,
367 rewrite_Copyright_TickComment,
368};
369
370static PFNSCMREWRITER const g_aRewritersFor_SedScripts[] =
371{
372 rewrite_ForceLF,
373 rewrite_ExpandTabs,
374 rewrite_StripTrailingBlanks,
375 rewrite_Copyright_HashComment,
376};
377
378static PFNSCMREWRITER const g_aRewritersFor_Python[] =
379{
380 /** @todo rewrite_ForceLFIfExecutable */
381 rewrite_ExpandTabs,
382 rewrite_StripTrailingBlanks,
383 rewrite_AdjustTrailingLines,
384 rewrite_SvnKeywords,
385 rewrite_Copyright_PythonComment,
386};
387
388static PFNSCMREWRITER const g_aRewritersFor_Perl[] =
389{
390 /** @todo rewrite_ForceLFIfExecutable */
391 rewrite_ExpandTabs,
392 rewrite_StripTrailingBlanks,
393 rewrite_AdjustTrailingLines,
394 rewrite_SvnKeywords,
395 rewrite_Copyright_HashComment,
396};
397
398static PFNSCMREWRITER const g_aRewritersFor_DriverInfFiles[] =
399{
400 rewrite_ForceNativeEol,
401 rewrite_ExpandTabs,
402 rewrite_StripTrailingBlanks,
403 rewrite_AdjustTrailingLines,
404 rewrite_SvnKeywords,
405 rewrite_SvnNoExecutable,
406 rewrite_Copyright_SemicolonComment,
407};
408
409static PFNSCMREWRITER const g_aRewritersFor_NsisFiles[] =
410{
411 rewrite_ForceNativeEol,
412 rewrite_ExpandTabs,
413 rewrite_StripTrailingBlanks,
414 rewrite_AdjustTrailingLines,
415 rewrite_SvnKeywords,
416 rewrite_SvnNoExecutable,
417 rewrite_Copyright_SemicolonComment,
418};
419
420static PFNSCMREWRITER const g_aRewritersFor_ScmSettings[] =
421{
422 rewrite_ForceNativeEol,
423 rewrite_ExpandTabs,
424 rewrite_StripTrailingBlanks,
425 rewrite_AdjustTrailingLines,
426 rewrite_SvnNoExecutable,
427 rewrite_SvnKeywords,
428 rewrite_Copyright_HashComment,
429};
430
431static PFNSCMREWRITER const g_aRewritersFor_Images[] =
432{
433 rewrite_SvnNoExecutable,
434 rewrite_SvnBinary,
435};
436
437static PFNSCMREWRITER const g_aRewritersFor_Xslt[] =
438{
439 rewrite_ForceNativeEol,
440 rewrite_ExpandTabs,
441 rewrite_StripTrailingBlanks,
442 rewrite_AdjustTrailingLines,
443 rewrite_SvnNoExecutable,
444 rewrite_SvnKeywords,
445 /** @todo copyright is in an XML comment. */
446};
447
448static PFNSCMREWRITER const g_aRewritersFor_Xml[] =
449{
450 rewrite_ForceNativeEol,
451 rewrite_ExpandTabs,
452 rewrite_StripTrailingBlanks,
453 rewrite_AdjustTrailingLines,
454 rewrite_SvnNoExecutable,
455 rewrite_SvnKeywords,
456 /** @todo copyright is in an XML comment. */
457};
458
459static PFNSCMREWRITER const g_aRewritersFor_QtProject[] =
460{
461 rewrite_ForceNativeEol,
462 rewrite_StripTrailingBlanks,
463 rewrite_AdjustTrailingLines,
464 rewrite_SvnNoExecutable,
465 rewrite_SvnKeywords,
466 rewrite_Copyright_HashComment,
467};
468
469static PFNSCMREWRITER const g_aRewritersFor_QtResourceFiles[] =
470{
471 rewrite_ForceNativeEol,
472 rewrite_SvnNoExecutable,
473 rewrite_SvnKeywords,
474 /** @todo figure out copyright for Qt resource XML files. */
475};
476
477static PFNSCMREWRITER const g_aRewritersFor_QtTranslations[] =
478{
479 rewrite_ForceNativeEol,
480 rewrite_SvnNoExecutable,
481};
482
483static PFNSCMREWRITER const g_aRewritersFor_QtUiFiles[] =
484{
485 rewrite_ForceNativeEol,
486 rewrite_SvnNoExecutable,
487 rewrite_SvnKeywords,
488 /** @todo copyright is in an XML 'comment' element. */
489};
490
491static PFNSCMREWRITER const g_aRewritersFor_FileLists[] = /* both makefile and shell script */
492{
493 rewrite_ForceLF,
494 rewrite_ExpandTabs,
495 rewrite_StripTrailingBlanks,
496 rewrite_AdjustTrailingLines,
497 rewrite_Copyright_HashComment,
498};
499
500
501
502#define SCM_CFG_ENTRY(a_aRewriters, a_fBinary, a_szFilePatterns) \
503 { RT_ELEMENTS(a_aRewriters), &a_aRewriters[0], a_fBinary, a_szFilePatterns }
504static SCMCFGENTRY const g_aConfigs[] =
505{
506 SCM_CFG_ENTRY(g_aRewritersFor_Makefile_kup, false, "Makefile.kup" ),
507 SCM_CFG_ENTRY(g_aRewritersFor_Makefile_kmk, false, "*.kmk" ),
508 SCM_CFG_ENTRY(g_aRewritersFor_OtherMakefiles, false, "Makefile" ),
509 SCM_CFG_ENTRY(g_aRewritersFor_C_and_CPP, false, "*.c|*.cpp|*.C|*.CPP|*.cxx|*.cc|*.m|*.mm" ),
510 SCM_CFG_ENTRY(g_aRewritersFor_H_and_HPP, false, "*.h|*.hpp" ),
511 SCM_CFG_ENTRY(g_aRewritersFor_RC, false, "*.rc" ),
512 SCM_CFG_ENTRY(g_aRewritersFor_ASM, false, "*.asm|*.mac|*.inc" ),
513 SCM_CFG_ENTRY(g_aRewritersFor_DTrace, false, "*.d" ),
514 SCM_CFG_ENTRY(g_aRewritersFor_DEF, false, "*.def" ),
515 SCM_CFG_ENTRY(g_aRewritersFor_DSL, false, "*.dsl" ),
516 SCM_CFG_ENTRY(g_aRewritersFor_ShellScripts, false, "*.sh|configure" ),
517 SCM_CFG_ENTRY(g_aRewritersFor_BatchFiles, false, "*.bat|*.cmd|*.btm" ),
518 SCM_CFG_ENTRY(g_aRewritersFor_BasicScripts, false, "*.vbs|*.vb" ),
519 SCM_CFG_ENTRY(g_aRewritersFor_SedScripts, false, "*.sed" ),
520 SCM_CFG_ENTRY(g_aRewritersFor_Python, false, "*.py" ),
521 SCM_CFG_ENTRY(g_aRewritersFor_Perl, false, "*.pl" ),
522 SCM_CFG_ENTRY(g_aRewritersFor_DriverInfFiles, false, "*.inf" ),
523 SCM_CFG_ENTRY(g_aRewritersFor_NsisFiles, false, "*.nsh|*.nsi" ),
524 SCM_CFG_ENTRY(g_aRewritersFor_ScmSettings, false, "*.scm-settings" ),
525 SCM_CFG_ENTRY(g_aRewritersFor_Images, true, "*.png|*.bmp|*.jpg|*.pnm|*.ico|*.icns" ),
526 SCM_CFG_ENTRY(g_aRewritersFor_Xslt, false, "*.xsl" ),
527 SCM_CFG_ENTRY(g_aRewritersFor_Xml, false, "*.xml" ),
528 SCM_CFG_ENTRY(g_aRewritersFor_QtProject, false, "*.pro" ),
529 SCM_CFG_ENTRY(g_aRewritersFor_QtResourceFiles, false, "*.qrc" ),
530 SCM_CFG_ENTRY(g_aRewritersFor_QtTranslations, false, "*.ts" ),
531 SCM_CFG_ENTRY(g_aRewritersFor_QtUiFiles, false, "*.ui" ),
532 /* Must be be last: */
533 SCM_CFG_ENTRY(g_aRewritersFor_FileLists, false, "files_*" ),
534};
535
536
537
538/* -=-=-=-=-=- settings -=-=-=-=-=- */
539
540
541/**
542 * Init a settings structure with settings from @a pSrc.
543 *
544 * @returns IPRT status code
545 * @param pSettings The settings.
546 * @param pSrc The source settings.
547 */
548static int scmSettingsBaseInitAndCopy(PSCMSETTINGSBASE pSettings, PCSCMSETTINGSBASE pSrc)
549{
550 *pSettings = *pSrc;
551
552 int rc = RTStrDupEx(&pSettings->pszFilterFiles, pSrc->pszFilterFiles);
553 if (RT_SUCCESS(rc))
554 {
555 rc = RTStrDupEx(&pSettings->pszFilterOutFiles, pSrc->pszFilterOutFiles);
556 if (RT_SUCCESS(rc))
557 {
558 rc = RTStrDupEx(&pSettings->pszFilterOutDirs, pSrc->pszFilterOutDirs);
559 if (RT_SUCCESS(rc))
560 {
561 if (pSrc->pszTreatAsName)
562 rc = RTStrDupEx(&pSettings->pszTreatAsName, pSrc->pszTreatAsName);
563 if (RT_SUCCESS(rc))
564 return VINF_SUCCESS;
565
566 RTStrFree(pSettings->pszFilterOutDirs);
567 }
568 RTStrFree(pSettings->pszFilterOutFiles);
569 }
570 RTStrFree(pSettings->pszFilterFiles);
571 }
572
573 pSettings->pszFilterFiles = NULL;
574 pSettings->pszFilterOutFiles = NULL;
575 pSettings->pszFilterOutDirs = NULL;
576 pSettings->pszTreatAsName = NULL;
577 return rc;
578}
579
580/**
581 * Init a settings structure.
582 *
583 * @returns IPRT status code
584 * @param pSettings The settings.
585 */
586static int scmSettingsBaseInit(PSCMSETTINGSBASE pSettings)
587{
588 return scmSettingsBaseInitAndCopy(pSettings, &g_Defaults);
589}
590
591/**
592 * Deletes the settings, i.e. free any dynamically allocated content.
593 *
594 * @param pSettings The settings.
595 */
596static void scmSettingsBaseDelete(PSCMSETTINGSBASE pSettings)
597{
598 if (pSettings)
599 {
600 Assert(pSettings->cchTab != UINT8_MAX);
601 pSettings->cchTab = UINT8_MAX;
602
603 RTStrFree(pSettings->pszFilterFiles);
604 pSettings->pszFilterFiles = NULL;
605
606 RTStrFree(pSettings->pszFilterOutFiles);
607 pSettings->pszFilterOutFiles = NULL;
608
609 RTStrFree(pSettings->pszFilterOutDirs);
610 pSettings->pszFilterOutDirs = NULL;
611
612 RTStrFree(pSettings->pszTreatAsName);
613 pSettings->pszTreatAsName = NULL;
614 }
615}
616
617
618/**
619 * Processes a RTGetOpt result.
620 *
621 * @retval VINF_SUCCESS if handled.
622 * @retval VERR_OUT_OF_RANGE if the option value was out of range.
623 * @retval VERR_GETOPT_UNKNOWN_OPTION if the option was not recognized.
624 *
625 * @param pSettings The settings to change.
626 * @param rc The RTGetOpt return value.
627 * @param pValueUnion The RTGetOpt value union.
628 * @param pchDir The absolute path to the directory relative
629 * components in pchLine should be relative to.
630 * @param cchDir The length of the @a pchDir string.
631 */
632static int scmSettingsBaseHandleOpt(PSCMSETTINGSBASE pSettings, int rc, PRTGETOPTUNION pValueUnion,
633 const char *pchDir, size_t cchDir)
634{
635 Assert(pchDir[cchDir - 1] == '/');
636
637 switch (rc)
638 {
639 case SCMOPT_CONVERT_EOL:
640 pSettings->fConvertEol = true;
641 return VINF_SUCCESS;
642 case SCMOPT_NO_CONVERT_EOL:
643 pSettings->fConvertEol = false;
644 return VINF_SUCCESS;
645
646 case SCMOPT_CONVERT_TABS:
647 pSettings->fConvertTabs = true;
648 return VINF_SUCCESS;
649 case SCMOPT_NO_CONVERT_TABS:
650 pSettings->fConvertTabs = false;
651 return VINF_SUCCESS;
652
653 case SCMOPT_FORCE_FINAL_EOL:
654 pSettings->fForceFinalEol = true;
655 return VINF_SUCCESS;
656 case SCMOPT_NO_FORCE_FINAL_EOL:
657 pSettings->fForceFinalEol = false;
658 return VINF_SUCCESS;
659
660 case SCMOPT_FORCE_TRAILING_LINE:
661 pSettings->fForceTrailingLine = true;
662 return VINF_SUCCESS;
663 case SCMOPT_NO_FORCE_TRAILING_LINE:
664 pSettings->fForceTrailingLine = false;
665 return VINF_SUCCESS;
666
667
668 case SCMOPT_STRIP_TRAILING_BLANKS:
669 pSettings->fStripTrailingBlanks = true;
670 return VINF_SUCCESS;
671 case SCMOPT_NO_STRIP_TRAILING_BLANKS:
672 pSettings->fStripTrailingBlanks = false;
673 return VINF_SUCCESS;
674
675 case SCMOPT_MIN_BLANK_LINES_BEFORE_FLOWER_BOX_MARKERS:
676 pSettings->cMinBlankLinesBeforeFlowerBoxMakers = pValueUnion->u8;
677 return VINF_SUCCESS;
678
679
680 case SCMOPT_STRIP_TRAILING_LINES:
681 pSettings->fStripTrailingLines = true;
682 return VINF_SUCCESS;
683 case SCMOPT_NO_STRIP_TRAILING_LINES:
684 pSettings->fStripTrailingLines = false;
685 return VINF_SUCCESS;
686
687 case SCMOPT_FIX_FLOWER_BOX_MARKERS:
688 pSettings->fFixFlowerBoxMarkers = true;
689 return VINF_SUCCESS;
690 case SCMOPT_NO_FIX_FLOWER_BOX_MARKERS:
691 pSettings->fFixFlowerBoxMarkers = false;
692 return VINF_SUCCESS;
693
694 case SCMOPT_UPDATE_COPYRIGHT_YEAR:
695 pSettings->fUpdateCopyrightYear = true;
696 return VINF_SUCCESS;
697 case SCMOPT_NO_UPDATE_COPYRIGHT_YEAR:
698 pSettings->fUpdateCopyrightYear = false;
699 return VINF_SUCCESS;
700
701 case SCMOPT_EXTERNAL_COPYRIGHT:
702 pSettings->fExternalCopyright = true;
703 return VINF_SUCCESS;
704 case SCMOPT_NO_EXTERNAL_COPYRIGHT:
705 pSettings->fExternalCopyright = false;
706 return VINF_SUCCESS;
707
708 case SCMOPT_NO_UPDATE_LICENSE:
709 pSettings->enmUpdateLicense = kScmLicense_LeaveAlone;
710 return VINF_SUCCESS;
711 case SCMOPT_LICENSE_OSE_GPL:
712 pSettings->enmUpdateLicense = kScmLicense_OseGpl;
713 return VINF_SUCCESS;
714 case SCMOPT_LICENSE_OSE_DUAL_GPL_CDDL:
715 pSettings->enmUpdateLicense = kScmLicense_OseDualGplCddl;
716 return VINF_SUCCESS;
717 case SCMOPT_LICENSE_OSE_CDDL:
718 pSettings->enmUpdateLicense = kScmLicense_OseCddl;
719 return VINF_SUCCESS;
720 case SCMOPT_LICENSE_LGPL:
721 pSettings->enmUpdateLicense = kScmLicense_Lgpl;
722 return VINF_SUCCESS;
723 case SCMOPT_LICENSE_MIT:
724 pSettings->enmUpdateLicense = kScmLicense_Mit;
725 return VINF_SUCCESS;
726 case SCMOPT_LICENSE_BASED_ON_MIT:
727 pSettings->enmUpdateLicense = kScmLicense_BasedOnMit;
728 return VINF_SUCCESS;
729
730 case SCMOPT_LGPL_DISCLAIMER:
731 pSettings->fLgplDisclaimer = true;
732 return VINF_SUCCESS;
733 case SCMOPT_NO_LGPL_DISCLAIMER:
734 pSettings->fLgplDisclaimer = false;
735 return VINF_SUCCESS;
736
737 case SCMOPT_ONLY_SVN_DIRS:
738 pSettings->fOnlySvnDirs = true;
739 return VINF_SUCCESS;
740 case SCMOPT_NOT_ONLY_SVN_DIRS:
741 pSettings->fOnlySvnDirs = false;
742 return VINF_SUCCESS;
743
744 case SCMOPT_ONLY_SVN_FILES:
745 pSettings->fOnlySvnFiles = true;
746 return VINF_SUCCESS;
747 case SCMOPT_NOT_ONLY_SVN_FILES:
748 pSettings->fOnlySvnFiles = false;
749 return VINF_SUCCESS;
750
751 case SCMOPT_SET_SVN_EOL:
752 pSettings->fSetSvnEol = true;
753 return VINF_SUCCESS;
754 case SCMOPT_DONT_SET_SVN_EOL:
755 pSettings->fSetSvnEol = false;
756 return VINF_SUCCESS;
757
758 case SCMOPT_SET_SVN_EXECUTABLE:
759 pSettings->fSetSvnExecutable = true;
760 return VINF_SUCCESS;
761 case SCMOPT_DONT_SET_SVN_EXECUTABLE:
762 pSettings->fSetSvnExecutable = false;
763 return VINF_SUCCESS;
764
765 case SCMOPT_SET_SVN_KEYWORDS:
766 pSettings->fSetSvnKeywords = true;
767 return VINF_SUCCESS;
768 case SCMOPT_DONT_SET_SVN_KEYWORDS:
769 pSettings->fSetSvnKeywords = false;
770 return VINF_SUCCESS;
771
772 case SCMOPT_TAB_SIZE:
773 if ( pValueUnion->u8 < 1
774 || pValueUnion->u8 >= RT_ELEMENTS(g_szTabSpaces))
775 {
776 RTMsgError("Invalid tab size: %u - must be in {1..%u}\n",
777 pValueUnion->u8, RT_ELEMENTS(g_szTabSpaces) - 1);
778 return VERR_OUT_OF_RANGE;
779 }
780 pSettings->cchTab = pValueUnion->u8;
781 return VINF_SUCCESS;
782
783 case SCMOPT_WIDTH:
784 if (pValueUnion->u8 < 20 || pValueUnion->u8 > 200)
785 {
786 RTMsgError("Invalid width size: %u - must be in {20..200} range\n", pValueUnion->u8);
787 return VERR_OUT_OF_RANGE;
788 }
789 pSettings->cchWidth = pValueUnion->u8;
790 return VINF_SUCCESS;
791
792 case SCMOPT_TREAT_AS:
793 if (pSettings->pszTreatAsName)
794 {
795 RTStrFree(pSettings->pszTreatAsName);
796 pSettings->pszTreatAsName = NULL;
797 }
798 if (*pValueUnion->psz)
799 {
800 pSettings->pszTreatAsName = RTStrDup(pValueUnion->psz);
801 if (!pSettings->pszTreatAsName)
802 return VERR_NO_MEMORY;
803 }
804 return VINF_SUCCESS;
805
806 case SCMOPT_FILTER_OUT_DIRS:
807 case SCMOPT_FILTER_FILES:
808 case SCMOPT_FILTER_OUT_FILES:
809 {
810 char **ppsz = NULL;
811 switch (rc)
812 {
813 case SCMOPT_FILTER_OUT_DIRS: ppsz = &pSettings->pszFilterOutDirs; break;
814 case SCMOPT_FILTER_FILES: ppsz = &pSettings->pszFilterFiles; break;
815 case SCMOPT_FILTER_OUT_FILES: ppsz = &pSettings->pszFilterOutFiles; break;
816 }
817
818 /*
819 * An empty string zaps the current list.
820 */
821 if (!*pValueUnion->psz)
822 return RTStrATruncate(ppsz, 0);
823
824 /*
825 * Non-empty strings are appended to the pattern list.
826 *
827 * Strip leading and trailing pattern separators before attempting
828 * to append it. If it's just separators, don't do anything.
829 */
830 const char *pszSrc = pValueUnion->psz;
831 while (*pszSrc == '|')
832 pszSrc++;
833 size_t cchSrc = strlen(pszSrc);
834 while (cchSrc > 0 && pszSrc[cchSrc - 1] == '|')
835 cchSrc--;
836 if (!cchSrc)
837 return VINF_SUCCESS;
838
839 /* Append it pattern by pattern, turning settings-relative paths into absolute ones. */
840 for (;;)
841 {
842 const char *pszEnd = (const char *)memchr(pszSrc, '|', cchSrc);
843 size_t cchPattern = pszEnd ? pszEnd - pszSrc : cchSrc;
844 int rc2;
845 if (*pszSrc == '/')
846 rc2 = RTStrAAppendExN(ppsz, 3,
847 "|", *ppsz && **ppsz != '\0' ? (size_t)1 : (size_t)0,
848 pchDir, cchDir - 1,
849 pszSrc, cchPattern);
850 else
851 rc2 = RTStrAAppendExN(ppsz, 2,
852 "|", *ppsz && **ppsz != '\0' ? (size_t)1 : (size_t)0,
853 pszSrc, cchPattern);
854 if (RT_FAILURE(rc2))
855 return rc2;
856
857 /* next */
858 cchSrc -= cchPattern;
859 if (!cchSrc)
860 return VINF_SUCCESS;
861 cchSrc -= 1;
862 pszSrc += cchPattern + 1;
863 }
864 /* not reached */
865 }
866
867 default:
868 return VERR_GETOPT_UNKNOWN_OPTION;
869 }
870}
871
872/**
873 * Parses an option string.
874 *
875 * @returns IPRT status code.
876 * @param pBase The base settings structure to apply the options
877 * to.
878 * @param pszOptions The options to parse.
879 * @param pchDir The absolute path to the directory relative
880 * components in pchLine should be relative to.
881 * @param cchDir The length of the @a pchDir string.
882 */
883static int scmSettingsBaseParseString(PSCMSETTINGSBASE pBase, const char *pszLine, const char *pchDir, size_t cchDir)
884{
885 int cArgs;
886 char **papszArgs;
887 int rc = RTGetOptArgvFromString(&papszArgs, &cArgs, pszLine, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH, NULL);
888 if (RT_SUCCESS(rc))
889 {
890 RTGETOPTUNION ValueUnion;
891 RTGETOPTSTATE GetOptState;
892 rc = RTGetOptInit(&GetOptState, cArgs, papszArgs, &g_aScmOpts[0], RT_ELEMENTS(g_aScmOpts), 0, 0 /*fFlags*/);
893 if (RT_SUCCESS(rc))
894 {
895 while ((rc = RTGetOpt(&GetOptState, &ValueUnion)) != 0)
896 {
897 rc = scmSettingsBaseHandleOpt(pBase, rc, &ValueUnion, pchDir, cchDir);
898 if (RT_FAILURE(rc))
899 break;
900 }
901 }
902 RTGetOptArgvFree(papszArgs);
903 }
904
905 return rc;
906}
907
908/**
909 * Parses an unterminated option string.
910 *
911 * @returns IPRT status code.
912 * @param pBase The base settings structure to apply the options
913 * to.
914 * @param pchLine The line.
915 * @param cchLine The line length.
916 * @param pchDir The absolute path to the directory relative
917 * components in pchLine should be relative to.
918 * @param cchDir The length of the @a pchDir string.
919 */
920static int scmSettingsBaseParseStringN(PSCMSETTINGSBASE pBase, const char *pchLine, size_t cchLine,
921 const char *pchDir, size_t cchDir)
922{
923 char *pszLine = RTStrDupN(pchLine, cchLine);
924 if (!pszLine)
925 return VERR_NO_MEMORY;
926 int rc = scmSettingsBaseParseString(pBase, pszLine, pchDir, cchDir);
927 RTStrFree(pszLine);
928 return rc;
929}
930
931/**
932 * Verifies the options string.
933 *
934 * @returns IPRT status code.
935 * @param pszOptions The options to verify .
936 */
937static int scmSettingsBaseVerifyString(const char *pszOptions)
938{
939 SCMSETTINGSBASE Base;
940 int rc = scmSettingsBaseInit(&Base);
941 if (RT_SUCCESS(rc))
942 {
943 rc = scmSettingsBaseParseString(&Base, pszOptions, "/", 1);
944 scmSettingsBaseDelete(&Base);
945 }
946 return rc;
947}
948
949/**
950 * Loads settings found in editor and SCM settings directives within the
951 * document (@a pStream).
952 *
953 * @returns IPRT status code.
954 * @param pBase The settings base to load settings into.
955 * @param pStream The stream to scan for settings directives.
956 */
957static int scmSettingsBaseLoadFromDocument(PSCMSETTINGSBASE pBase, PSCMSTREAM pStream)
958{
959 /** @todo Editor and SCM settings directives in documents. */
960 RT_NOREF2(pBase, pStream);
961 return VINF_SUCCESS;
962}
963
964/**
965 * Creates a new settings file struct, cloning @a pSettings.
966 *
967 * @returns IPRT status code.
968 * @param ppSettings Where to return the new struct.
969 * @param pSettingsBase The settings to inherit from.
970 */
971static int scmSettingsCreate(PSCMSETTINGS *ppSettings, PCSCMSETTINGSBASE pSettingsBase)
972{
973 PSCMSETTINGS pSettings = (PSCMSETTINGS)RTMemAlloc(sizeof(*pSettings));
974 if (!pSettings)
975 return VERR_NO_MEMORY;
976 int rc = scmSettingsBaseInitAndCopy(&pSettings->Base, pSettingsBase);
977 if (RT_SUCCESS(rc))
978 {
979 pSettings->pDown = NULL;
980 pSettings->pUp = NULL;
981 pSettings->paPairs = NULL;
982 pSettings->cPairs = 0;
983 *ppSettings = pSettings;
984 return VINF_SUCCESS;
985 }
986 RTMemFree(pSettings);
987 return rc;
988}
989
990/**
991 * Destroys a settings structure.
992 *
993 * @param pSettings The settings structure to destroy. NULL is OK.
994 */
995static void scmSettingsDestroy(PSCMSETTINGS pSettings)
996{
997 if (pSettings)
998 {
999 scmSettingsBaseDelete(&pSettings->Base);
1000 for (size_t i = 0; i < pSettings->cPairs; i++)
1001 {
1002 RTStrFree(pSettings->paPairs[i].pszPattern);
1003 RTStrFree(pSettings->paPairs[i].pszOptions);
1004 RTStrFree(pSettings->paPairs[i].pszRelativeTo);
1005 pSettings->paPairs[i].pszPattern = NULL;
1006 pSettings->paPairs[i].pszOptions = NULL;
1007 pSettings->paPairs[i].pszRelativeTo = NULL;
1008 }
1009 RTMemFree(pSettings->paPairs);
1010 pSettings->paPairs = NULL;
1011 RTMemFree(pSettings);
1012 }
1013}
1014
1015/**
1016 * Adds a pattern/options pair to the settings structure.
1017 *
1018 * @returns IPRT status code.
1019 * @param pSettings The settings.
1020 * @param pchLine The line containing the unparsed pair.
1021 * @param cchLine The length of the line.
1022 * @param offColon The offset of the colon into the line.
1023 * @param pchDir The absolute path to the directory relative
1024 * components in pchLine should be relative to.
1025 * @param cchDir The length of the @a pchDir string.
1026 */
1027static int scmSettingsAddPair(PSCMSETTINGS pSettings, const char *pchLine, size_t cchLine, size_t offColon,
1028 const char *pchDir, size_t cchDir)
1029{
1030 Assert(pchLine[offColon] == ':' && offColon < cchLine);
1031 Assert(pchDir[cchDir - 1] == '/');
1032
1033 /*
1034 * Split the string.
1035 */
1036 size_t cchPattern = offColon;
1037 size_t cchOptions = cchLine - cchPattern - 1;
1038
1039 /* strip spaces everywhere */
1040 while (cchPattern > 0 && RT_C_IS_SPACE(pchLine[cchPattern - 1]))
1041 cchPattern--;
1042 while (cchPattern > 0 && RT_C_IS_SPACE(*pchLine))
1043 cchPattern--, pchLine++;
1044
1045 const char *pchOptions = &pchLine[offColon + 1];
1046 while (cchOptions > 0 && RT_C_IS_SPACE(pchOptions[cchOptions - 1]))
1047 cchOptions--;
1048 while (cchOptions > 0 && RT_C_IS_SPACE(*pchOptions))
1049 cchOptions--, pchOptions++;
1050
1051 /* Quietly ignore empty patterns and empty options. */
1052 if (!cchOptions || !cchPattern)
1053 return VINF_SUCCESS;
1054
1055 /*
1056 * Prepair the pair and verify the option string.
1057 */
1058 uint32_t iPair = pSettings->cPairs;
1059 if ((iPair % 32) == 0)
1060 {
1061 void *pvNew = RTMemRealloc(pSettings->paPairs, (iPair + 32) * sizeof(pSettings->paPairs[0]));
1062 if (!pvNew)
1063 return VERR_NO_MEMORY;
1064 pSettings->paPairs = (PSCMPATRNOPTPAIR)pvNew;
1065 }
1066
1067 pSettings->paPairs[iPair].pszPattern = RTStrDupN(pchLine, cchPattern);
1068 pSettings->paPairs[iPair].pszOptions = RTStrDupN(pchOptions, cchOptions);
1069 pSettings->paPairs[iPair].pszRelativeTo = RTStrDupN(pchDir, cchDir);
1070 int rc;
1071 if ( pSettings->paPairs[iPair].pszPattern
1072 && pSettings->paPairs[iPair].pszOptions
1073 && pSettings->paPairs[iPair].pszRelativeTo)
1074 rc = scmSettingsBaseVerifyString(pSettings->paPairs[iPair].pszOptions);
1075 else
1076 rc = VERR_NO_MEMORY;
1077
1078 /*
1079 * If it checked out fine, expand any relative paths in the pattern.
1080 */
1081 if (RT_SUCCESS(rc))
1082 {
1083 size_t cRelativePaths = 0;
1084 const char *pszSrc = pSettings->paPairs[iPair].pszPattern;
1085 for (;;)
1086 {
1087 if (*pszSrc == '/')
1088 cRelativePaths++;
1089 pszSrc = strchr(pszSrc, '|');
1090 if (!pszSrc)
1091 break;
1092 pszSrc++;
1093 }
1094 if (cRelativePaths > 0)
1095 {
1096 char *pszNewPattern = RTStrAlloc(cchPattern + cRelativePaths * (cchDir - 1) + 1);
1097 if (pszNewPattern)
1098 {
1099 char *pszDst = pszNewPattern;
1100 pszSrc = pSettings->paPairs[iPair].pszPattern;
1101 for (;;)
1102 {
1103 if (*pszSrc == '/')
1104 {
1105 memcpy(pszDst, pchDir, cchDir);
1106 pszDst += cchDir;
1107 pszSrc += 1;
1108 }
1109
1110 /* Look for the next relative path. */
1111 const char *pszSrcNext = strchr(pszSrc, '|');
1112 while (pszSrcNext && pszSrcNext[1] != '/')
1113 pszSrcNext = strchr(pszSrcNext, '|');
1114 if (!pszSrcNext)
1115 break;
1116
1117 /* Copy stuff between current and the next path. */
1118 pszSrcNext++;
1119 memcpy(pszDst, pszSrc, pszSrcNext - pszSrc);
1120 pszDst += pszSrcNext - pszSrc;
1121 pszSrc = pszSrcNext;
1122 }
1123
1124 /* Copy the final portion and replace the pattern. */
1125 strcpy(pszDst, pszSrc);
1126
1127 RTStrFree(pSettings->paPairs[iPair].pszPattern);
1128 pSettings->paPairs[iPair].pszPattern = pszNewPattern;
1129 }
1130 else
1131 rc = VERR_NO_MEMORY;
1132 }
1133 }
1134 if (RT_SUCCESS(rc))
1135 /*
1136 * Commit the pair.
1137 */
1138 pSettings->cPairs = iPair + 1;
1139 else
1140 {
1141 RTStrFree(pSettings->paPairs[iPair].pszPattern);
1142 RTStrFree(pSettings->paPairs[iPair].pszOptions);
1143 RTStrFree(pSettings->paPairs[iPair].pszRelativeTo);
1144 }
1145 return rc;
1146}
1147
1148/**
1149 * Loads in the settings from @a pszFilename.
1150 *
1151 * @returns IPRT status code.
1152 * @param pSettings Where to load the settings file.
1153 * @param pszFilename The file to load.
1154 */
1155static int scmSettingsLoadFile(PSCMSETTINGS pSettings, const char *pszFilename)
1156{
1157 ScmVerbose(NULL, 3, "Loading settings file '%s'...\n", pszFilename);
1158
1159 /* Turn filename into an absolute path and drop the filename. */
1160 char szAbsPath[RTPATH_MAX];
1161 int rc = RTPathAbs(pszFilename, szAbsPath, sizeof(szAbsPath));
1162 if (RT_FAILURE(rc))
1163 {
1164 RTMsgError("%s: RTPathAbs -> %Rrc\n", pszFilename, rc);
1165 return rc;
1166 }
1167 RTPathChangeToUnixSlashes(szAbsPath, true);
1168 size_t cchDir = RTPathFilename(szAbsPath) - &szAbsPath[0];
1169
1170 /* Try open it.*/
1171 SCMSTREAM Stream;
1172 rc = ScmStreamInitForReading(&Stream, pszFilename);
1173 if (RT_SUCCESS(rc))
1174 {
1175 SCMEOL enmEol;
1176 const char *pchLine;
1177 size_t cchLine;
1178 while ((pchLine = ScmStreamGetLine(&Stream, &cchLine, &enmEol)) != NULL)
1179 {
1180 /* Ignore leading spaces. */
1181 while (cchLine > 0 && RT_C_IS_SPACE(*pchLine))
1182 pchLine++, cchLine--;
1183
1184 /* Ignore empty lines and comment lines. */
1185 if (cchLine < 1 || *pchLine == '#')
1186 continue;
1187
1188 /* What kind of line is it? */
1189 const char *pchColon = (const char *)memchr(pchLine, ':', cchLine);
1190 if (pchColon)
1191 rc = scmSettingsAddPair(pSettings, pchLine, cchLine, pchColon - pchLine, szAbsPath, cchDir);
1192 else
1193 rc = scmSettingsBaseParseStringN(&pSettings->Base, pchLine, cchLine, szAbsPath, cchDir);
1194 if (RT_FAILURE(rc))
1195 {
1196 RTMsgError("%s:%d: %Rrc\n", pszFilename, ScmStreamTellLine(&Stream), rc);
1197 break;
1198 }
1199 }
1200
1201 if (RT_SUCCESS(rc))
1202 {
1203 rc = ScmStreamGetStatus(&Stream);
1204 if (RT_FAILURE(rc))
1205 RTMsgError("%s: ScmStreamGetStatus- > %Rrc\n", pszFilename, rc);
1206 }
1207 ScmStreamDelete(&Stream);
1208 }
1209 else
1210 RTMsgError("%s: ScmStreamInitForReading -> %Rrc\n", pszFilename, rc);
1211 return rc;
1212}
1213
1214#if 0 /* unused */
1215/**
1216 * Parse the specified settings file creating a new settings struct from it.
1217 *
1218 * @returns IPRT status code
1219 * @param ppSettings Where to return the new settings.
1220 * @param pszFilename The file to parse.
1221 * @param pSettingsBase The base settings we inherit from.
1222 */
1223static int scmSettingsCreateFromFile(PSCMSETTINGS *ppSettings, const char *pszFilename, PCSCMSETTINGSBASE pSettingsBase)
1224{
1225 PSCMSETTINGS pSettings;
1226 int rc = scmSettingsCreate(&pSettings, pSettingsBase);
1227 if (RT_SUCCESS(rc))
1228 {
1229 rc = scmSettingsLoadFile(pSettings, pszFilename, RTPathFilename(pszFilename) - pszFilename);
1230 if (RT_SUCCESS(rc))
1231 {
1232 *ppSettings = pSettings;
1233 return VINF_SUCCESS;
1234 }
1235
1236 scmSettingsDestroy(pSettings);
1237 }
1238 *ppSettings = NULL;
1239 return rc;
1240}
1241#endif
1242
1243
1244/**
1245 * Create an initial settings structure when starting processing a new file or
1246 * directory.
1247 *
1248 * This will look for .scm-settings files from the root and down to the
1249 * specified directory, combining them into the returned settings structure.
1250 *
1251 * @returns IPRT status code.
1252 * @param ppSettings Where to return the pointer to the top stack
1253 * object.
1254 * @param pBaseSettings The base settings we inherit from (globals
1255 * typically).
1256 * @param pszPath The absolute path to the new directory or file.
1257 */
1258static int scmSettingsCreateForPath(PSCMSETTINGS *ppSettings, PCSCMSETTINGSBASE pBaseSettings, const char *pszPath)
1259{
1260 *ppSettings = NULL; /* try shut up gcc. */
1261
1262 /*
1263 * We'll be working with a stack copy of the path.
1264 */
1265 char szFile[RTPATH_MAX];
1266 size_t cchDir = strlen(pszPath);
1267 if (cchDir >= sizeof(szFile) - sizeof(SCM_SETTINGS_FILENAME))
1268 return VERR_FILENAME_TOO_LONG;
1269
1270 /*
1271 * Create the bottom-most settings.
1272 */
1273 PSCMSETTINGS pSettings;
1274 int rc = scmSettingsCreate(&pSettings, pBaseSettings);
1275 if (RT_FAILURE(rc))
1276 return rc;
1277
1278 /*
1279 * Enumerate the path components from the root and down. Load any setting
1280 * files we find.
1281 */
1282 size_t cComponents = RTPathCountComponents(pszPath);
1283 for (size_t i = 1; i <= cComponents; i++)
1284 {
1285 rc = RTPathCopyComponents(szFile, sizeof(szFile), pszPath, i);
1286 if (RT_SUCCESS(rc))
1287 rc = RTPathAppend(szFile, sizeof(szFile), SCM_SETTINGS_FILENAME);
1288 if (RT_FAILURE(rc))
1289 break;
1290 RTPathChangeToUnixSlashes(szFile, true);
1291
1292 if (RTFileExists(szFile))
1293 {
1294 rc = scmSettingsLoadFile(pSettings, szFile);
1295 if (RT_FAILURE(rc))
1296 break;
1297 }
1298 }
1299
1300 if (RT_SUCCESS(rc))
1301 *ppSettings = pSettings;
1302 else
1303 scmSettingsDestroy(pSettings);
1304 return rc;
1305}
1306
1307/**
1308 * Pushes a new settings set onto the stack.
1309 *
1310 * @param ppSettingsStack The pointer to the pointer to the top stack
1311 * element. This will be used as input and output.
1312 * @param pSettings The settings to push onto the stack.
1313 */
1314static void scmSettingsStackPush(PSCMSETTINGS *ppSettingsStack, PSCMSETTINGS pSettings)
1315{
1316 PSCMSETTINGS pOld = *ppSettingsStack;
1317 pSettings->pDown = pOld;
1318 pSettings->pUp = NULL;
1319 if (pOld)
1320 pOld->pUp = pSettings;
1321 *ppSettingsStack = pSettings;
1322}
1323
1324/**
1325 * Pushes the settings of the specified directory onto the stack.
1326 *
1327 * We will load any .scm-settings in the directory. A stack entry is added even
1328 * if no settings file was found.
1329 *
1330 * @returns IPRT status code.
1331 * @param ppSettingsStack The pointer to the pointer to the top stack
1332 * element. This will be used as input and output.
1333 * @param pszDir The directory to do this for.
1334 */
1335static int scmSettingsStackPushDir(PSCMSETTINGS *ppSettingsStack, const char *pszDir)
1336{
1337 char szFile[RTPATH_MAX];
1338 int rc = RTPathJoin(szFile, sizeof(szFile), pszDir, SCM_SETTINGS_FILENAME);
1339 if (RT_SUCCESS(rc))
1340 {
1341 RTPathChangeToUnixSlashes(szFile, true);
1342
1343 PSCMSETTINGS pSettings;
1344 rc = scmSettingsCreate(&pSettings, &(*ppSettingsStack)->Base);
1345 if (RT_SUCCESS(rc))
1346 {
1347 if (RTFileExists(szFile))
1348 rc = scmSettingsLoadFile(pSettings, szFile);
1349 if (RT_SUCCESS(rc))
1350 {
1351 scmSettingsStackPush(ppSettingsStack, pSettings);
1352 return VINF_SUCCESS;
1353 }
1354
1355 scmSettingsDestroy(pSettings);
1356 }
1357 }
1358 return rc;
1359}
1360
1361
1362/**
1363 * Pops a settings set off the stack.
1364 *
1365 * @returns The popped setttings.
1366 * @param ppSettingsStack The pointer to the pointer to the top stack
1367 * element. This will be used as input and output.
1368 */
1369static PSCMSETTINGS scmSettingsStackPop(PSCMSETTINGS *ppSettingsStack)
1370{
1371 PSCMSETTINGS pRet = *ppSettingsStack;
1372 PSCMSETTINGS pNew = pRet ? pRet->pDown : NULL;
1373 *ppSettingsStack = pNew;
1374 if (pNew)
1375 pNew->pUp = NULL;
1376 if (pRet)
1377 {
1378 pRet->pUp = NULL;
1379 pRet->pDown = NULL;
1380 }
1381 return pRet;
1382}
1383
1384/**
1385 * Pops and destroys the top entry of the stack.
1386 *
1387 * @param ppSettingsStack The pointer to the pointer to the top stack
1388 * element. This will be used as input and output.
1389 */
1390static void scmSettingsStackPopAndDestroy(PSCMSETTINGS *ppSettingsStack)
1391{
1392 scmSettingsDestroy(scmSettingsStackPop(ppSettingsStack));
1393}
1394
1395/**
1396 * Constructs the base settings for the specified file name.
1397 *
1398 * @returns IPRT status code.
1399 * @param pSettingsStack The top element on the settings stack.
1400 * @param pszFilename The file name.
1401 * @param pszBasename The base name (pointer within @a pszFilename).
1402 * @param cchBasename The length of the base name. (For passing to
1403 * RTStrSimplePatternMultiMatch.)
1404 * @param pBase Base settings to initialize.
1405 */
1406static int scmSettingsStackMakeFileBase(PCSCMSETTINGS pSettingsStack, const char *pszFilename,
1407 const char *pszBasename, size_t cchBasename, PSCMSETTINGSBASE pBase)
1408{
1409 ScmVerbose(NULL, 5, "scmSettingsStackMakeFileBase(%s, %.*s)\n", pszFilename, cchBasename, pszBasename);
1410
1411 int rc = scmSettingsBaseInitAndCopy(pBase, &pSettingsStack->Base);
1412 if (RT_SUCCESS(rc))
1413 {
1414 /* find the bottom entry in the stack. */
1415 PCSCMSETTINGS pCur = pSettingsStack;
1416 while (pCur->pDown)
1417 pCur = pCur->pDown;
1418
1419 /* Work our way up thru the stack and look for matching pairs. */
1420 while (pCur)
1421 {
1422 size_t const cPairs = pCur->cPairs;
1423 if (cPairs)
1424 {
1425 for (size_t i = 0; i < cPairs; i++)
1426 if ( RTStrSimplePatternMultiMatch(pCur->paPairs[i].pszPattern, RTSTR_MAX,
1427 pszBasename, cchBasename, NULL)
1428 || RTStrSimplePatternMultiMatch(pCur->paPairs[i].pszPattern, RTSTR_MAX,
1429 pszFilename, RTSTR_MAX, NULL))
1430 {
1431 ScmVerbose(NULL, 5, "scmSettingsStackMakeFileBase: Matched '%s' : '%s'\n",
1432 pCur->paPairs[i].pszPattern, pCur->paPairs[i].pszOptions);
1433 rc = scmSettingsBaseParseString(pBase, pCur->paPairs[i].pszOptions,
1434 pCur->paPairs[i].pszRelativeTo, strlen(pCur->paPairs[i].pszRelativeTo));
1435 if (RT_FAILURE(rc))
1436 break;
1437 }
1438 if (RT_FAILURE(rc))
1439 break;
1440 }
1441
1442 /* advance */
1443 pCur = pCur->pUp;
1444 }
1445 }
1446 if (RT_FAILURE(rc))
1447 scmSettingsBaseDelete(pBase);
1448 return rc;
1449}
1450
1451
1452/* -=-=-=-=-=- misc -=-=-=-=-=- */
1453
1454
1455/**
1456 * Prints the per file banner needed and the message level is high enough.
1457 *
1458 * @param pState The rewrite state.
1459 * @param iLevel The required verbosity level.
1460 */
1461void ScmVerboseBanner(PSCMRWSTATE pState, int iLevel)
1462{
1463 if (iLevel <= g_iVerbosity && !pState->fFirst)
1464 {
1465 RTPrintf("%s: info: --= Rewriting '%s' =--\n", g_szProgName, pState->pszFilename);
1466 pState->fFirst = true;
1467 }
1468}
1469
1470
1471/**
1472 * Prints a verbose message if the level is high enough.
1473 *
1474 * @param pState The rewrite state. Optional.
1475 * @param iLevel The required verbosity level.
1476 * @param pszFormat The message format string. Can be NULL if we
1477 * only want to trigger the per file message.
1478 * @param ... Format arguments.
1479 */
1480void ScmVerbose(PSCMRWSTATE pState, int iLevel, const char *pszFormat, ...)
1481{
1482 if (iLevel <= g_iVerbosity)
1483 {
1484 if (pState && !pState->fFirst)
1485 {
1486 RTPrintf("%s: info: --= Rewriting '%s' =--\n", g_szProgName, pState->pszFilename);
1487 pState->fFirst = true;
1488 }
1489 RTPrintf(pState
1490 ? "%s: info: "
1491 : "%s: info: ",
1492 g_szProgName);
1493 va_list va;
1494 va_start(va, pszFormat);
1495 RTPrintfV(pszFormat, va);
1496 va_end(va);
1497 }
1498}
1499
1500
1501/**
1502 * Prints an error message.
1503 *
1504 * @returns false
1505 * @param pState The rewrite state. Optional.
1506 * @param rc The error code.
1507 * @param pszFormat The message format string.
1508 * @param ... Format arguments.
1509 */
1510bool ScmError(PSCMRWSTATE pState, int rc, const char *pszFormat, ...)
1511{
1512 if (RT_SUCCESS(pState->rc))
1513 pState->rc = rc;
1514
1515 if (!pState->fFirst)
1516 {
1517 RTPrintf("%s: info: --= Rewriting '%s' =--\n", g_szProgName, pState->pszFilename);
1518 pState->fFirst = true;
1519 }
1520 va_list va;
1521 va_start(va, pszFormat);
1522 RTPrintf("%s: error: %s: %N", g_szProgName, pState->pszFilename, pszFormat, &va);
1523 va_end(va);
1524
1525 return false;
1526}
1527
1528
1529/* -=-=-=-=-=- file and directory processing -=-=-=-=-=- */
1530
1531
1532/**
1533 * Processes a file.
1534 *
1535 * @returns IPRT status code.
1536 * @param pState The rewriter state.
1537 * @param pszFilename The file name.
1538 * @param pszBasename The base name (pointer within @a pszFilename).
1539 * @param cchBasename The length of the base name. (For passing to
1540 * RTStrSimplePatternMultiMatch.)
1541 * @param pBaseSettings The base settings to use. It's OK to modify
1542 * these.
1543 */
1544static int scmProcessFileInner(PSCMRWSTATE pState, const char *pszFilename, const char *pszBasename, size_t cchBasename,
1545 PSCMSETTINGSBASE pBaseSettings)
1546{
1547 /*
1548 * Do the file level filtering.
1549 */
1550 if ( pBaseSettings->pszFilterFiles
1551 && *pBaseSettings->pszFilterFiles
1552 && !RTStrSimplePatternMultiMatch(pBaseSettings->pszFilterFiles, RTSTR_MAX, pszBasename, cchBasename, NULL))
1553 {
1554 ScmVerbose(NULL, 5, "skipping '%s': file filter mismatch\n", pszFilename);
1555 g_cFilesSkipped++;
1556 return VINF_SUCCESS;
1557 }
1558 if ( pBaseSettings->pszFilterOutFiles
1559 && *pBaseSettings->pszFilterOutFiles
1560 && ( RTStrSimplePatternMultiMatch(pBaseSettings->pszFilterOutFiles, RTSTR_MAX, pszBasename, cchBasename, NULL)
1561 || RTStrSimplePatternMultiMatch(pBaseSettings->pszFilterOutFiles, RTSTR_MAX, pszFilename, RTSTR_MAX, NULL)) )
1562 {
1563 ScmVerbose(NULL, 5, "skipping '%s': filterd out\n", pszFilename);
1564 g_cFilesSkipped++;
1565 return VINF_SUCCESS;
1566 }
1567 if ( pBaseSettings->fOnlySvnFiles
1568 && !ScmSvnIsInWorkingCopy(pState))
1569 {
1570 ScmVerbose(NULL, 5, "skipping '%s': not in SVN WC\n", pszFilename);
1571 g_cFilesNotInSvn++;
1572 return VINF_SUCCESS;
1573 }
1574
1575 /*
1576 * Try find a matching rewrite config for this filename.
1577 */
1578 PCSCMCFGENTRY pCfg = NULL;
1579 if (!pBaseSettings->pszTreatAsName)
1580 {
1581 for (size_t iCfg = 0; iCfg < RT_ELEMENTS(g_aConfigs); iCfg++)
1582 if (RTStrSimplePatternMultiMatch(g_aConfigs[iCfg].pszFilePattern, RTSTR_MAX, pszBasename, cchBasename, NULL))
1583 {
1584 pCfg = &g_aConfigs[iCfg];
1585 break;
1586 }
1587 }
1588 else
1589 {
1590 size_t cchTreatAsName = strlen(pBaseSettings->pszTreatAsName);
1591 for (size_t iCfg = 0; iCfg < RT_ELEMENTS(g_aConfigs); iCfg++)
1592 if (RTStrSimplePatternMultiMatch(g_aConfigs[iCfg].pszFilePattern, RTSTR_MAX,
1593 pBaseSettings->pszTreatAsName, cchTreatAsName, NULL))
1594 {
1595 pCfg = &g_aConfigs[iCfg];
1596 break;
1597 }
1598 }
1599 if (!pCfg)
1600 {
1601 ScmVerbose(NULL, 2, "skipping '%s': no rewriters configured\n", pszFilename);
1602 g_cFilesNoRewriters++;
1603 return VINF_SUCCESS;
1604 }
1605 ScmVerbose(pState, 4, "matched \"%s\"\n", pCfg->pszFilePattern);
1606
1607 /*
1608 * Create an input stream from the file and check that it's text.
1609 */
1610 SCMSTREAM Stream1;
1611 int rc = ScmStreamInitForReading(&Stream1, pszFilename);
1612 if (RT_FAILURE(rc))
1613 {
1614 RTMsgError("Failed to read '%s': %Rrc\n", pszFilename, rc);
1615 return rc;
1616 }
1617 if (ScmStreamIsText(&Stream1) || pCfg->fBinary)
1618 {
1619 ScmVerboseBanner(pState, 3);
1620
1621 /*
1622 * Gather SCM and editor settings from the stream.
1623 */
1624 rc = scmSettingsBaseLoadFromDocument(pBaseSettings, &Stream1);
1625 if (RT_SUCCESS(rc))
1626 {
1627 ScmStreamRewindForReading(&Stream1);
1628
1629 /*
1630 * Create two more streams for output and push the text thru all the
1631 * rewriters, switching the two streams around when something is
1632 * actually rewritten. Stream1 remains unchanged.
1633 */
1634 SCMSTREAM Stream2;
1635 rc = ScmStreamInitForWriting(&Stream2, &Stream1);
1636 if (RT_SUCCESS(rc))
1637 {
1638 SCMSTREAM Stream3;
1639 rc = ScmStreamInitForWriting(&Stream3, &Stream1);
1640 if (RT_SUCCESS(rc))
1641 {
1642 bool fModified = false;
1643 PSCMSTREAM pIn = &Stream1;
1644 PSCMSTREAM pOut = &Stream2;
1645 for (size_t iRw = 0; iRw < pCfg->cRewriters; iRw++)
1646 {
1647 pState->rc = VINF_SUCCESS;
1648 bool fRc = pCfg->papfnRewriter[iRw](pState, pIn, pOut, pBaseSettings);
1649 if (RT_FAILURE(pState->rc))
1650 break;
1651 if (fRc)
1652 {
1653 PSCMSTREAM pTmp = pOut;
1654 pOut = pIn == &Stream1 ? &Stream3 : pIn;
1655 pIn = pTmp;
1656 fModified = true;
1657 }
1658
1659 ScmStreamRewindForReading(pIn);
1660 ScmStreamRewindForWriting(pOut);
1661 }
1662
1663 rc = pState->rc;
1664 if (RT_SUCCESS(rc))
1665 {
1666 rc = ScmStreamGetStatus(&Stream1);
1667 if (RT_SUCCESS(rc))
1668 rc = ScmStreamGetStatus(&Stream2);
1669 if (RT_SUCCESS(rc))
1670 rc = ScmStreamGetStatus(&Stream3);
1671 if (RT_SUCCESS(rc))
1672 {
1673 /*
1674 * If rewritten, write it back to disk.
1675 */
1676 if (fModified && !pCfg->fBinary)
1677 {
1678 if (!g_fDryRun)
1679 {
1680 ScmVerbose(pState, 1, "writing modified file to \"%s%s\"\n", pszFilename, g_pszChangedSuff);
1681 rc = ScmStreamWriteToFile(pIn, "%s%s", pszFilename, g_pszChangedSuff);
1682 if (RT_FAILURE(rc))
1683 RTMsgError("Error writing '%s%s': %Rrc\n", pszFilename, g_pszChangedSuff, rc);
1684 }
1685 else
1686 {
1687 ScmVerboseBanner(pState, 1);
1688 ScmDiffStreams(pszFilename, &Stream1, pIn, g_fDiffIgnoreEol,
1689 g_fDiffIgnoreLeadingWS, g_fDiffIgnoreTrailingWS, g_fDiffSpecialChars,
1690 pBaseSettings->cchTab, g_pStdOut);
1691 ScmVerbose(pState, 2, "would have modified the file \"%s%s\"\n",
1692 pszFilename, g_pszChangedSuff);
1693 }
1694 g_cFilesModified++;
1695 }
1696 else if (fModified)
1697 rc = RTMsgErrorRc(VERR_INTERNAL_ERROR, "Rewriters modified binary file! Impossible!");
1698
1699 /*
1700 * If pending SVN property changes, apply them.
1701 */
1702 if (pState->cSvnPropChanges && RT_SUCCESS(rc))
1703 {
1704 if (!g_fDryRun)
1705 {
1706 rc = ScmSvnApplyChanges(pState);
1707 if (RT_FAILURE(rc))
1708 RTMsgError("%s: failed to apply SVN property changes (%Rrc)\n", pszFilename, rc);
1709 }
1710 else
1711 ScmSvnDisplayChanges(pState);
1712 if (!fModified)
1713 g_cFilesModified++;
1714 }
1715
1716 if (!fModified && !pState->cSvnPropChanges)
1717 ScmVerbose(pState, 3, "%s: no change\n", pszFilename);
1718 }
1719 else
1720 RTMsgError("%s: stream error %Rrc\n", pszFilename, rc);
1721 }
1722 ScmStreamDelete(&Stream3);
1723 }
1724 else
1725 RTMsgError("Failed to init stream for writing: %Rrc\n", rc);
1726 ScmStreamDelete(&Stream2);
1727 }
1728 else
1729 RTMsgError("Failed to init stream for writing: %Rrc\n", rc);
1730 }
1731 else
1732 RTMsgError("scmSettingsBaseLoadFromDocument: %Rrc\n", rc);
1733 }
1734 else
1735 {
1736 ScmVerbose(pState, 2, "not text file: \"%s\"\n", pszFilename);
1737 g_cFilesBinaries++;
1738 }
1739 ScmStreamDelete(&Stream1);
1740
1741 return rc;
1742}
1743
1744/**
1745 * Processes a file.
1746 *
1747 * This is just a wrapper for scmProcessFileInner for avoid wasting stack in the
1748 * directory recursion method.
1749 *
1750 * @returns IPRT status code.
1751 * @param pszFilename The file name.
1752 * @param pszBasename The base name (pointer within @a pszFilename).
1753 * @param cchBasename The length of the base name. (For passing to
1754 * RTStrSimplePatternMultiMatch.)
1755 * @param pSettingsStack The settings stack (pointer to the top element).
1756 */
1757static int scmProcessFile(const char *pszFilename, const char *pszBasename, size_t cchBasename,
1758 PSCMSETTINGS pSettingsStack)
1759{
1760 SCMSETTINGSBASE Base;
1761 int rc = scmSettingsStackMakeFileBase(pSettingsStack, pszFilename, pszBasename, cchBasename, &Base);
1762 if (RT_SUCCESS(rc))
1763 {
1764 SCMRWSTATE State;
1765 State.fFirst = false;
1766 State.pszFilename = pszFilename;
1767 State.cSvnPropChanges = 0;
1768 State.paSvnPropChanges = NULL;
1769 State.rc = VINF_SUCCESS;
1770
1771 rc = scmProcessFileInner(&State, pszFilename, pszBasename, cchBasename, &Base);
1772
1773 size_t i = State.cSvnPropChanges;
1774 while (i-- > 0)
1775 {
1776 RTStrFree(State.paSvnPropChanges[i].pszName);
1777 RTStrFree(State.paSvnPropChanges[i].pszValue);
1778 }
1779 RTMemFree(State.paSvnPropChanges);
1780
1781 scmSettingsBaseDelete(&Base);
1782
1783 g_cFilesProcessed++;
1784 }
1785 return rc;
1786}
1787
1788
1789/**
1790 * Tries to correct RTDIRENTRY_UNKNOWN.
1791 *
1792 * @returns Corrected type.
1793 * @param pszPath The path to the object in question.
1794 */
1795static RTDIRENTRYTYPE scmFigureUnknownType(const char *pszPath)
1796{
1797 RTFSOBJINFO Info;
1798 int rc = RTPathQueryInfo(pszPath, &Info, RTFSOBJATTRADD_NOTHING);
1799 if (RT_FAILURE(rc))
1800 return RTDIRENTRYTYPE_UNKNOWN;
1801 if (RTFS_IS_DIRECTORY(Info.Attr.fMode))
1802 return RTDIRENTRYTYPE_DIRECTORY;
1803 if (RTFS_IS_FILE(Info.Attr.fMode))
1804 return RTDIRENTRYTYPE_FILE;
1805 return RTDIRENTRYTYPE_UNKNOWN;
1806}
1807
1808/**
1809 * Recurse into a sub-directory and process all the files and directories.
1810 *
1811 * @returns IPRT status code.
1812 * @param pszBuf Path buffer containing the directory path on
1813 * entry. This ends with a dot. This is passed
1814 * along when recursing in order to save stack space
1815 * and avoid needless copying.
1816 * @param cchDir Length of our path in pszbuf.
1817 * @param pEntry Directory entry buffer. This is also passed
1818 * along when recursing to save stack space.
1819 * @param pSettingsStack The settings stack (pointer to the top element).
1820 * @param iRecursion The recursion depth. This is used to restrict
1821 * the recursions.
1822 */
1823static int scmProcessDirTreeRecursion(char *pszBuf, size_t cchDir, PRTDIRENTRY pEntry,
1824 PSCMSETTINGS pSettingsStack, unsigned iRecursion)
1825{
1826 int rc;
1827 Assert(cchDir > 1 && pszBuf[cchDir - 1] == '.');
1828
1829 /*
1830 * Make sure we stop somewhere.
1831 */
1832 if (iRecursion > 128)
1833 {
1834 RTMsgError("recursion too deep: %d\n", iRecursion);
1835 return VINF_SUCCESS; /* ignore */
1836 }
1837
1838 /*
1839 * Check if it's excluded by --only-svn-dir.
1840 */
1841 if (pSettingsStack->Base.fOnlySvnDirs)
1842 {
1843 if (!ScmSvnIsDirInWorkingCopy(pszBuf))
1844 return VINF_SUCCESS;
1845 }
1846 g_cDirsProcessed++;
1847
1848 /*
1849 * Try open and read the directory.
1850 */
1851 PRTDIR pDir;
1852 rc = RTDirOpenFiltered(&pDir, pszBuf, RTDIRFILTER_NONE, 0);
1853 if (RT_FAILURE(rc))
1854 {
1855 RTMsgError("Failed to enumerate directory '%s': %Rrc", pszBuf, rc);
1856 return rc;
1857 }
1858 for (;;)
1859 {
1860 /* Read the next entry. */
1861 rc = RTDirRead(pDir, pEntry, NULL);
1862 if (RT_FAILURE(rc))
1863 {
1864 if (rc == VERR_NO_MORE_FILES)
1865 rc = VINF_SUCCESS;
1866 else
1867 RTMsgError("RTDirRead -> %Rrc\n", rc);
1868 break;
1869 }
1870
1871 /* Skip '.' and '..'. */
1872 if ( pEntry->szName[0] == '.'
1873 && ( pEntry->cbName == 1
1874 || ( pEntry->cbName == 2
1875 && pEntry->szName[1] == '.')))
1876 continue;
1877
1878 /* Enter it into the buffer so we've got a full name to work
1879 with when needed. */
1880 if (pEntry->cbName + cchDir >= RTPATH_MAX)
1881 {
1882 RTMsgError("Skipping too long entry: %s", pEntry->szName);
1883 continue;
1884 }
1885 memcpy(&pszBuf[cchDir - 1], pEntry->szName, pEntry->cbName + 1);
1886
1887 /* Figure the type. */
1888 RTDIRENTRYTYPE enmType = pEntry->enmType;
1889 if (enmType == RTDIRENTRYTYPE_UNKNOWN)
1890 enmType = scmFigureUnknownType(pszBuf);
1891
1892 /* Process the file or directory, skip the rest. */
1893 if (enmType == RTDIRENTRYTYPE_FILE)
1894 rc = scmProcessFile(pszBuf, pEntry->szName, pEntry->cbName, pSettingsStack);
1895 else if (enmType == RTDIRENTRYTYPE_DIRECTORY)
1896 {
1897 /* Append the dot for the benefit of the pattern matching. */
1898 if (pEntry->cbName + cchDir + 5 >= RTPATH_MAX)
1899 {
1900 RTMsgError("Skipping too deep dir entry: %s", pEntry->szName);
1901 continue;
1902 }
1903 memcpy(&pszBuf[cchDir - 1 + pEntry->cbName], "/.", sizeof("/."));
1904 size_t cchSubDir = cchDir - 1 + pEntry->cbName + sizeof("/.") - 1;
1905
1906 if ( !pSettingsStack->Base.pszFilterOutDirs
1907 || !*pSettingsStack->Base.pszFilterOutDirs
1908 || ( !RTStrSimplePatternMultiMatch(pSettingsStack->Base.pszFilterOutDirs, RTSTR_MAX,
1909 pEntry->szName, pEntry->cbName, NULL)
1910 && !RTStrSimplePatternMultiMatch(pSettingsStack->Base.pszFilterOutDirs, RTSTR_MAX,
1911 pszBuf, cchSubDir, NULL)
1912 )
1913 )
1914 {
1915 rc = scmSettingsStackPushDir(&pSettingsStack, pszBuf);
1916 if (RT_SUCCESS(rc))
1917 {
1918 rc = scmProcessDirTreeRecursion(pszBuf, cchSubDir, pEntry, pSettingsStack, iRecursion + 1);
1919 scmSettingsStackPopAndDestroy(&pSettingsStack);
1920 }
1921 }
1922 }
1923 if (RT_FAILURE(rc))
1924 break;
1925 }
1926 RTDirClose(pDir);
1927 return rc;
1928
1929}
1930
1931/**
1932 * Process a directory tree.
1933 *
1934 * @returns IPRT status code.
1935 * @param pszDir The directory to start with. This is pointer to
1936 * a RTPATH_MAX sized buffer.
1937 */
1938static int scmProcessDirTree(char *pszDir, PSCMSETTINGS pSettingsStack)
1939{
1940 /*
1941 * Setup the recursion.
1942 */
1943 int rc = RTPathAppend(pszDir, RTPATH_MAX, ".");
1944 if (RT_SUCCESS(rc))
1945 {
1946 RTPathChangeToUnixSlashes(pszDir, true);
1947
1948 RTDIRENTRY Entry;
1949 rc = scmProcessDirTreeRecursion(pszDir, strlen(pszDir), &Entry, pSettingsStack, 0);
1950 }
1951 else
1952 RTMsgError("RTPathAppend: %Rrc\n", rc);
1953 return rc;
1954}
1955
1956
1957/**
1958 * Processes a file or directory specified as an command line argument.
1959 *
1960 * @returns IPRT status code
1961 * @param pszSomething What we found in the command line arguments.
1962 * @param pSettingsStack The settings stack (pointer to the top element).
1963 */
1964static int scmProcessSomething(const char *pszSomething, PSCMSETTINGS pSettingsStack)
1965{
1966 char szBuf[RTPATH_MAX];
1967 int rc = RTPathAbs(pszSomething, szBuf, sizeof(szBuf));
1968 if (RT_SUCCESS(rc))
1969 {
1970 RTPathChangeToUnixSlashes(szBuf, false /*fForce*/);
1971
1972 PSCMSETTINGS pSettings;
1973 rc = scmSettingsCreateForPath(&pSettings, &pSettingsStack->Base, szBuf);
1974 if (RT_SUCCESS(rc))
1975 {
1976 scmSettingsStackPush(&pSettingsStack, pSettings);
1977
1978 if (RTFileExists(szBuf))
1979 {
1980 const char *pszBasename = RTPathFilename(szBuf);
1981 if (pszBasename)
1982 {
1983 size_t cchBasename = strlen(pszBasename);
1984 rc = scmProcessFile(szBuf, pszBasename, cchBasename, pSettingsStack);
1985 }
1986 else
1987 {
1988 RTMsgError("RTPathFilename: NULL\n");
1989 rc = VERR_IS_A_DIRECTORY;
1990 }
1991 }
1992 else
1993 rc = scmProcessDirTree(szBuf, pSettingsStack);
1994
1995 PSCMSETTINGS pPopped = scmSettingsStackPop(&pSettingsStack);
1996 Assert(pPopped == pSettings); RT_NOREF_PV(pPopped);
1997 scmSettingsDestroy(pSettings);
1998 }
1999 else
2000 RTMsgError("scmSettingsInitStack: %Rrc\n", rc);
2001 }
2002 else
2003 RTMsgError("RTPathAbs: %Rrc\n", rc);
2004 return rc;
2005}
2006
2007/**
2008 * Print some stats.
2009 */
2010static void scmPrintStats(void)
2011{
2012 ScmVerbose(NULL, 0,
2013 g_fDryRun
2014 ? "%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"
2015 : "%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",
2016 g_cFilesModified,
2017 g_cFilesProcessed, g_cFilesProcessed == 1 ? "" : "s",
2018 g_cDirsProcessed, g_cDirsProcessed == 1 ? "" : "s",
2019 g_cFilesNoRewriters, g_cFilesNoRewriters == 1 ? "" : "s",
2020 g_cFilesBinaries, g_cFilesBinaries == 1 ? "y" : "ies",
2021 g_cFilesNotInSvn, g_cFilesSkipped);
2022}
2023
2024static void usage(PCRTGETOPTDEF paOpts, size_t cOpts)
2025{
2026 RTPrintf("VirtualBox Source Code Massager\n"
2027 "\n"
2028 "Usage: %s [options] <files & dirs>\n"
2029 "\n"
2030 "Options:\n", g_szProgName);
2031 for (size_t i = 0; i < cOpts; i++)
2032 {
2033 size_t cExtraAdvance = 0;
2034 if ((paOpts[i].fFlags & RTGETOPT_REQ_MASK) == RTGETOPT_REQ_NOTHING)
2035 {
2036 cExtraAdvance = i + 1 < cOpts
2037 && ( strstr(paOpts[i+1].pszLong, "-no-") != NULL
2038 || strstr(paOpts[i+1].pszLong, "-not-") != NULL
2039 || strstr(paOpts[i+1].pszLong, "-dont-") != NULL
2040 || (paOpts[i].iShort == 'q' && paOpts[i+1].iShort == 'v')
2041 || (paOpts[i].iShort == 'd' && paOpts[i+1].iShort == 'D')
2042 );
2043 if (cExtraAdvance)
2044 RTPrintf(" %s, %s\n", paOpts[i].pszLong, paOpts[i + 1].pszLong);
2045 else if (paOpts[i].iShort != SCMOPT_NO_UPDATE_LICENSE)
2046 RTPrintf(" %s\n", paOpts[i].pszLong);
2047 else
2048 {
2049 RTPrintf(" %s,\n"
2050 " %s,\n"
2051 " %s,\n"
2052 " %s,\n"
2053 " %s,\n"
2054 " %s,\n"
2055 " %s\n",
2056 paOpts[i].pszLong,
2057 paOpts[i + 1].pszLong,
2058 paOpts[i + 2].pszLong,
2059 paOpts[i + 3].pszLong,
2060 paOpts[i + 4].pszLong,
2061 paOpts[i + 5].pszLong,
2062 paOpts[i + 6].pszLong);
2063 cExtraAdvance = 6;
2064 }
2065 }
2066 else if ((paOpts[i].fFlags & RTGETOPT_REQ_MASK) == RTGETOPT_REQ_STRING)
2067 RTPrintf(" %s string\n", paOpts[i].pszLong);
2068 else
2069 RTPrintf(" %s value\n", paOpts[i].pszLong);
2070 switch (paOpts[i].iShort)
2071 {
2072 case 'd':
2073 case 'D': RTPrintf(" Default: --dry-run\n"); break;
2074 case 'f': RTPrintf(" Default: none\n"); break;
2075 case 'q':
2076 case 'v': RTPrintf(" Default: -vv\n"); break;
2077
2078 case SCMOPT_DIFF_IGNORE_EOL: RTPrintf(" Default: false\n"); break;
2079 case SCMOPT_DIFF_IGNORE_SPACE: RTPrintf(" Default: false\n"); break;
2080 case SCMOPT_DIFF_IGNORE_LEADING_SPACE: RTPrintf(" Default: false\n"); break;
2081 case SCMOPT_DIFF_IGNORE_TRAILING_SPACE: RTPrintf(" Default: false\n"); break;
2082 case SCMOPT_DIFF_SPECIAL_CHARS: RTPrintf(" Default: true\n"); break;
2083
2084 case SCMOPT_CONVERT_EOL: RTPrintf(" Default: %RTbool\n", g_Defaults.fConvertEol); break;
2085 case SCMOPT_CONVERT_TABS: RTPrintf(" Default: %RTbool\n", g_Defaults.fConvertTabs); break;
2086 case SCMOPT_FORCE_FINAL_EOL: RTPrintf(" Default: %RTbool\n", g_Defaults.fForceFinalEol); break;
2087 case SCMOPT_FORCE_TRAILING_LINE: RTPrintf(" Default: %RTbool\n", g_Defaults.fForceTrailingLine); break;
2088 case SCMOPT_STRIP_TRAILING_BLANKS: RTPrintf(" Default: %RTbool\n", g_Defaults.fStripTrailingBlanks); break;
2089 case SCMOPT_STRIP_TRAILING_LINES: RTPrintf(" Default: %RTbool\n", g_Defaults.fStripTrailingLines); break;
2090 case SCMOPT_FIX_FLOWER_BOX_MARKERS: RTPrintf(" Default: %RTbool\n", g_Defaults.fFixFlowerBoxMarkers); break;
2091 case SCMOPT_MIN_BLANK_LINES_BEFORE_FLOWER_BOX_MARKERS: RTPrintf(" Default: %u\n", g_Defaults.cMinBlankLinesBeforeFlowerBoxMakers); break;
2092
2093 case SCMOPT_FIX_TODOS:
2094 RTPrintf(" Fix @todo statements so doxygen sees them. Default: %RTbool\n", g_Defaults.fFixTodos);
2095 break;
2096 case SCMOPT_UPDATE_COPYRIGHT_YEAR:
2097 RTPrintf(" Update the copyright year. Default: %RTbool\n", g_Defaults.fUpdateCopyrightYear);
2098 break;
2099 case SCMOPT_EXTERNAL_COPYRIGHT:
2100 RTPrintf(" Only external copyright holders. Default: %RTbool\n", g_Defaults.fExternalCopyright);
2101 break;
2102 case SCMOPT_NO_UPDATE_LICENSE:
2103 RTPrintf(" License selection. Default: --license-ose-gpl\n");
2104 break;
2105
2106 case SCMOPT_LGPL_DISCLAIMER:
2107 RTPrintf(" Include LGPL version disclaimer. Default: --no-lgpl-disclaimer\n");
2108 break;
2109
2110 case SCMOPT_ONLY_SVN_DIRS: RTPrintf(" Default: %RTbool\n", g_Defaults.fOnlySvnDirs); break;
2111 case SCMOPT_ONLY_SVN_FILES: RTPrintf(" Default: %RTbool\n", g_Defaults.fOnlySvnFiles); break;
2112 case SCMOPT_SET_SVN_EOL: RTPrintf(" Default: %RTbool\n", g_Defaults.fSetSvnEol); break;
2113 case SCMOPT_SET_SVN_EXECUTABLE: RTPrintf(" Default: %RTbool\n", g_Defaults.fSetSvnExecutable); break;
2114 case SCMOPT_SET_SVN_KEYWORDS: RTPrintf(" Default: %RTbool\n", g_Defaults.fSetSvnKeywords); break;
2115 case SCMOPT_TAB_SIZE: RTPrintf(" Default: %u\n", g_Defaults.cchTab); break;
2116 case SCMOPT_WIDTH: RTPrintf(" Default: %u\n", g_Defaults.cchWidth); break;
2117
2118 case SCMOPT_TREAT_AS:
2119 RTPrintf(" For files not using the default extension.\n");
2120 break;
2121
2122 case SCMOPT_FILTER_OUT_DIRS: RTPrintf(" Default: %s\n", g_Defaults.pszFilterOutDirs); break;
2123 case SCMOPT_FILTER_FILES: RTPrintf(" Default: %s\n", g_Defaults.pszFilterFiles); break;
2124 case SCMOPT_FILTER_OUT_FILES: RTPrintf(" Default: %s\n", g_Defaults.pszFilterOutFiles); break;
2125 default: AssertMsgFailed(("i=%d %d %s\n", i, paOpts[i].iShort, paOpts[i].pszLong));
2126 }
2127 i += cExtraAdvance;
2128 }
2129
2130}
2131
2132int main(int argc, char **argv)
2133{
2134 int rc = RTR3InitExe(argc, &argv, 0);
2135 if (RT_FAILURE(rc))
2136 return 1;
2137
2138 /*
2139 * Init the current year.
2140 */
2141 RTTIMESPEC Now;
2142 RTTIME Time;
2143 RTTimeExplode(&Time, RTTimeNow(&Now));
2144 g_uYear = Time.i32Year;
2145
2146 /*
2147 * Init the settings.
2148 */
2149 PSCMSETTINGS pSettings;
2150 rc = scmSettingsCreate(&pSettings, &g_Defaults);
2151 if (RT_FAILURE(rc))
2152 {
2153 RTMsgError("scmSettingsCreate: %Rrc\n", rc);
2154 return 1;
2155 }
2156
2157 /*
2158 * Parse arguments and process input in order (because this is the only
2159 * thing that works at the moment).
2160 */
2161 static RTGETOPTDEF s_aOpts[14 + RT_ELEMENTS(g_aScmOpts)] =
2162 {
2163 { "--dry-run", 'd', RTGETOPT_REQ_NOTHING },
2164 { "--real-run", 'D', RTGETOPT_REQ_NOTHING },
2165 { "--file-filter", 'f', RTGETOPT_REQ_STRING },
2166 { "--quiet", 'q', RTGETOPT_REQ_NOTHING },
2167 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
2168 { "--diff-ignore-eol", SCMOPT_DIFF_IGNORE_EOL, RTGETOPT_REQ_NOTHING },
2169 { "--diff-no-ignore-eol", SCMOPT_DIFF_NO_IGNORE_EOL, RTGETOPT_REQ_NOTHING },
2170 { "--diff-ignore-space", SCMOPT_DIFF_IGNORE_SPACE, RTGETOPT_REQ_NOTHING },
2171 { "--diff-no-ignore-space", SCMOPT_DIFF_NO_IGNORE_SPACE, RTGETOPT_REQ_NOTHING },
2172 { "--diff-ignore-leading-space", SCMOPT_DIFF_IGNORE_LEADING_SPACE, RTGETOPT_REQ_NOTHING },
2173 { "--diff-no-ignore-leading-space", SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE, RTGETOPT_REQ_NOTHING },
2174 { "--diff-ignore-trailing-space", SCMOPT_DIFF_IGNORE_TRAILING_SPACE, RTGETOPT_REQ_NOTHING },
2175 { "--diff-no-ignore-trailing-space", SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE, RTGETOPT_REQ_NOTHING },
2176 { "--diff-special-chars", SCMOPT_DIFF_SPECIAL_CHARS, RTGETOPT_REQ_NOTHING },
2177 { "--diff-no-special-chars", SCMOPT_DIFF_NO_SPECIAL_CHARS, RTGETOPT_REQ_NOTHING },
2178 };
2179 memcpy(&s_aOpts[RT_ELEMENTS(s_aOpts) - RT_ELEMENTS(g_aScmOpts)], &g_aScmOpts[0], sizeof(g_aScmOpts));
2180
2181 RTGETOPTUNION ValueUnion;
2182 RTGETOPTSTATE GetOptState;
2183 rc = RTGetOptInit(&GetOptState, argc, argv, &s_aOpts[0], RT_ELEMENTS(s_aOpts), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
2184 AssertReleaseRCReturn(rc, 1);
2185 size_t cProcessed = 0;
2186
2187 while ((rc = RTGetOpt(&GetOptState, &ValueUnion)) != 0)
2188 {
2189 switch (rc)
2190 {
2191 case 'd':
2192 g_fDryRun = true;
2193 break;
2194 case 'D':
2195 g_fDryRun = false;
2196 break;
2197
2198 case 'f':
2199 g_pszFileFilter = ValueUnion.psz;
2200 break;
2201
2202 case 'h':
2203 usage(s_aOpts, RT_ELEMENTS(s_aOpts));
2204 return 1;
2205
2206 case 'q':
2207 g_iVerbosity = 0;
2208 break;
2209
2210 case 'v':
2211 g_iVerbosity++;
2212 break;
2213
2214 case 'V':
2215 {
2216 /* The following is assuming that svn does it's job here. */
2217 static const char s_szRev[] = "$Revision: 69355 $";
2218 const char *psz = RTStrStripL(strchr(s_szRev, ' '));
2219 RTPrintf("r%.*s\n", strchr(psz, ' ') - psz, psz);
2220 return 0;
2221 }
2222
2223 case SCMOPT_DIFF_IGNORE_EOL:
2224 g_fDiffIgnoreEol = true;
2225 break;
2226 case SCMOPT_DIFF_NO_IGNORE_EOL:
2227 g_fDiffIgnoreEol = false;
2228 break;
2229
2230 case SCMOPT_DIFF_IGNORE_SPACE:
2231 g_fDiffIgnoreTrailingWS = g_fDiffIgnoreLeadingWS = true;
2232 break;
2233 case SCMOPT_DIFF_NO_IGNORE_SPACE:
2234 g_fDiffIgnoreTrailingWS = g_fDiffIgnoreLeadingWS = false;
2235 break;
2236
2237 case SCMOPT_DIFF_IGNORE_LEADING_SPACE:
2238 g_fDiffIgnoreLeadingWS = true;
2239 break;
2240 case SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE:
2241 g_fDiffIgnoreLeadingWS = false;
2242 break;
2243
2244 case SCMOPT_DIFF_IGNORE_TRAILING_SPACE:
2245 g_fDiffIgnoreTrailingWS = true;
2246 break;
2247 case SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE:
2248 g_fDiffIgnoreTrailingWS = false;
2249 break;
2250
2251 case SCMOPT_DIFF_SPECIAL_CHARS:
2252 g_fDiffSpecialChars = true;
2253 break;
2254 case SCMOPT_DIFF_NO_SPECIAL_CHARS:
2255 g_fDiffSpecialChars = false;
2256 break;
2257
2258 case VINF_GETOPT_NOT_OPTION:
2259 {
2260 if (!g_fDryRun)
2261 {
2262 if (!cProcessed)
2263 {
2264 RTPrintf("%s: Warning! This program will make changes to your source files and\n"
2265 "%s: there is a slight risk that bugs or a full disk may cause\n"
2266 "%s: LOSS OF DATA. So, please make sure you have checked in\n"
2267 "%s: all your changes already. If you didn't, then don't blame\n"
2268 "%s: anyone for not warning you!\n"
2269 "%s:\n"
2270 "%s: Press any key to continue...\n",
2271 g_szProgName, g_szProgName, g_szProgName, g_szProgName, g_szProgName,
2272 g_szProgName, g_szProgName);
2273 RTStrmGetCh(g_pStdIn);
2274 }
2275 cProcessed++;
2276 }
2277 rc = scmProcessSomething(ValueUnion.psz, pSettings);
2278 if (RT_FAILURE(rc))
2279 {
2280 scmPrintStats();
2281 return RTEXITCODE_FAILURE;
2282 }
2283 break;
2284 }
2285
2286 default:
2287 {
2288 int rc2 = scmSettingsBaseHandleOpt(&pSettings->Base, rc, &ValueUnion, "/", 1);
2289 if (RT_SUCCESS(rc2))
2290 break;
2291 if (rc2 != VERR_GETOPT_UNKNOWN_OPTION)
2292 return 2;
2293 return RTGetOptPrintError(rc, &ValueUnion);
2294 }
2295 }
2296 }
2297
2298 scmPrintStats();
2299 scmSettingsDestroy(pSettings);
2300 return 0;
2301}
2302
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