VirtualBox

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

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

scm: check svn:sync-process on files and parent dir.

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