VirtualBox

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

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

scm: Allow line-ending escapes in .scm-settings files. Recognize more makefile names.

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