VirtualBox

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

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

scm: License and copyright updating.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 67.9 KB
Line 
1/* $Id: scm.cpp 69166 2017-10-23 15:43:33Z vboxsync $ */
2/** @file
3 * IPRT Testcase / Tool - Source Code Massager.
4 */
5
6/*
7 * Copyright (C) 2010-2016 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include <iprt/assert.h>
23#include <iprt/ctype.h>
24#include <iprt/dir.h>
25#include <iprt/env.h>
26#include <iprt/file.h>
27#include <iprt/err.h>
28#include <iprt/getopt.h>
29#include <iprt/initterm.h>
30#include <iprt/mem.h>
31#include <iprt/message.h>
32#include <iprt/param.h>
33#include <iprt/path.h>
34#include <iprt/process.h>
35#include <iprt/stream.h>
36#include <iprt/string.h>
37
38#include "scm.h"
39#include "scmdiff.h"
40
41
42/*********************************************************************************************************************************
43* Defined Constants And Macros *
44*********************************************************************************************************************************/
45/** The name of the settings files. */
46#define SCM_SETTINGS_FILENAME ".scm-settings"
47
48
49/*********************************************************************************************************************************
50* Structures and Typedefs *
51*********************************************************************************************************************************/
52
53/**
54 * Option identifiers.
55 *
56 * @note The first chunk, down to SCMOPT_TAB_SIZE, are alternately set &
57 * clear. So, the option setting a flag (boolean) will have an even
58 * number and the one clearing it will have an odd number.
59 * @note Down to SCMOPT_LAST_SETTINGS corresponds exactly to SCMSETTINGSBASE.
60 */
61typedef enum SCMOPT
62{
63 SCMOPT_CONVERT_EOL = 10000,
64 SCMOPT_NO_CONVERT_EOL,
65 SCMOPT_CONVERT_TABS,
66 SCMOPT_NO_CONVERT_TABS,
67 SCMOPT_FORCE_FINAL_EOL,
68 SCMOPT_NO_FORCE_FINAL_EOL,
69 SCMOPT_FORCE_TRAILING_LINE,
70 SCMOPT_NO_FORCE_TRAILING_LINE,
71 SCMOPT_STRIP_TRAILING_BLANKS,
72 SCMOPT_NO_STRIP_TRAILING_BLANKS,
73 SCMOPT_STRIP_TRAILING_LINES,
74 SCMOPT_NO_STRIP_TRAILING_LINES,
75 SCMOPT_FIX_FLOWER_BOX_MARKERS,
76 SCMOPT_NO_FIX_FLOWER_BOX_MARKERS,
77 SCMOPT_FIX_TODOS,
78 SCMOPT_NO_FIX_TODOS,
79 SCMOPT_UPDATE_COPYRIGHT_YEAR,
80 SCMOPT_NO_UPDATE_COPYRIGHT_YEAR,
81 SCMOPT_NO_UPDATE_LICENSE,
82 SCMOPT_LICENSE_OSE_GPL,
83 SCMOPT_LICENSE_OSE_DUAL_GPL_CDDL,
84 SCMOPT_LICENSE_LGPL,
85 SCMOPT_LICENSE_MIT,
86 SCMOPT_MIN_BLANK_LINES_BEFORE_FLOWER_BOX_MARKERS,
87 SCMOPT_ONLY_SVN_DIRS,
88 SCMOPT_NOT_ONLY_SVN_DIRS,
89 SCMOPT_ONLY_SVN_FILES,
90 SCMOPT_NOT_ONLY_SVN_FILES,
91 SCMOPT_SET_SVN_EOL,
92 SCMOPT_DONT_SET_SVN_EOL,
93 SCMOPT_SET_SVN_EXECUTABLE,
94 SCMOPT_DONT_SET_SVN_EXECUTABLE,
95 SCMOPT_SET_SVN_KEYWORDS,
96 SCMOPT_DONT_SET_SVN_KEYWORDS,
97 SCMOPT_TAB_SIZE,
98 SCMOPT_WIDTH,
99 SCMOPT_FILTER_OUT_DIRS,
100 SCMOPT_FILTER_FILES,
101 SCMOPT_FILTER_OUT_FILES,
102 SCMOPT_LAST_SETTINGS = SCMOPT_FILTER_OUT_FILES,
103 //
104 SCMOPT_DIFF_IGNORE_EOL,
105 SCMOPT_DIFF_NO_IGNORE_EOL,
106 SCMOPT_DIFF_IGNORE_SPACE,
107 SCMOPT_DIFF_NO_IGNORE_SPACE,
108 SCMOPT_DIFF_IGNORE_LEADING_SPACE,
109 SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE,
110 SCMOPT_DIFF_IGNORE_TRAILING_SPACE,
111 SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE,
112 SCMOPT_DIFF_SPECIAL_CHARS,
113 SCMOPT_DIFF_NO_SPECIAL_CHARS,
114 SCMOPT_END
115} SCMOPT;
116
117
118/*********************************************************************************************************************************
119* Global Variables *
120*********************************************************************************************************************************/
121const char g_szTabSpaces[16+1] = " ";
122const char g_szAsterisks[255+1] =
123"****************************************************************************************************"
124"****************************************************************************************************"
125"*******************************************************";
126const char g_szSpaces[255+1] =
127" "
128" "
129" ";
130static const char g_szProgName[] = "scm";
131static const char *g_pszChangedSuff = "";
132static bool g_fDryRun = true;
133static bool g_fDiffSpecialChars = true;
134static bool g_fDiffIgnoreEol = false;
135static bool g_fDiffIgnoreLeadingWS = false;
136static bool g_fDiffIgnoreTrailingWS = false;
137static int g_iVerbosity = 2;//99; //0;
138uint32_t g_uYear = 0; /**< The current year. */
139
140/** The global settings. */
141static SCMSETTINGSBASE const g_Defaults =
142{
143 /* .fConvertEol = */ true,
144 /* .fConvertTabs = */ true,
145 /* .fForceFinalEol = */ true,
146 /* .fForceTrailingLine = */ false,
147 /* .fStripTrailingBlanks = */ true,
148 /* .fStripTrailingLines = */ true,
149 /* .fFixFlowerBoxMarkers = */ true,
150 /* .cMinBlankLinesBeforeFlowerBoxMakers = */ 2,
151 /* .fFixTodos = */ true,
152 /* .fUpdateCopyrightYear = */ false,
153 /* .enmUpdateLicense = */ kScmLicense_OseGpl,
154 /* .fOnlySvnFiles = */ false,
155 /* .fOnlySvnDirs = */ false,
156 /* .fSetSvnEol = */ false,
157 /* .fSetSvnExecutable = */ false,
158 /* .fSetSvnKeywords = */ false,
159 /* .cchTab = */ 8,
160 /* .cchWidth = */ 130,
161 /* .pszFilterFiles = */ (char *)"",
162 /* .pszFilterOutFiles = */ (char *)"*.exe|*.com|20*-*-*.log",
163 /* .pszFilterOutDirs = */ (char *)".svn|.hg|.git|CVS",
164};
165
166/** Option definitions for the base settings. */
167static RTGETOPTDEF g_aScmOpts[] =
168{
169 { "--convert-eol", SCMOPT_CONVERT_EOL, RTGETOPT_REQ_NOTHING },
170 { "--no-convert-eol", SCMOPT_NO_CONVERT_EOL, RTGETOPT_REQ_NOTHING },
171 { "--convert-tabs", SCMOPT_CONVERT_TABS, RTGETOPT_REQ_NOTHING },
172 { "--no-convert-tabs", SCMOPT_NO_CONVERT_TABS, RTGETOPT_REQ_NOTHING },
173 { "--force-final-eol", SCMOPT_FORCE_FINAL_EOL, RTGETOPT_REQ_NOTHING },
174 { "--no-force-final-eol", SCMOPT_NO_FORCE_FINAL_EOL, RTGETOPT_REQ_NOTHING },
175 { "--force-trailing-line", SCMOPT_FORCE_TRAILING_LINE, RTGETOPT_REQ_NOTHING },
176 { "--no-force-trailing-line", SCMOPT_NO_FORCE_TRAILING_LINE, RTGETOPT_REQ_NOTHING },
177 { "--strip-trailing-blanks", SCMOPT_STRIP_TRAILING_BLANKS, RTGETOPT_REQ_NOTHING },
178 { "--no-strip-trailing-blanks", SCMOPT_NO_STRIP_TRAILING_BLANKS, RTGETOPT_REQ_NOTHING },
179 { "--strip-trailing-lines", SCMOPT_STRIP_TRAILING_LINES, RTGETOPT_REQ_NOTHING },
180 { "--strip-no-trailing-lines", SCMOPT_NO_STRIP_TRAILING_LINES, RTGETOPT_REQ_NOTHING },
181 { "--min-blank-lines-before-flower-box-makers", SCMOPT_MIN_BLANK_LINES_BEFORE_FLOWER_BOX_MARKERS, RTGETOPT_REQ_UINT8 },
182 { "--fix-flower-box-markers", SCMOPT_FIX_FLOWER_BOX_MARKERS, RTGETOPT_REQ_NOTHING },
183 { "--no-fix-flower-box-markers", SCMOPT_NO_FIX_FLOWER_BOX_MARKERS, RTGETOPT_REQ_NOTHING },
184 { "--fix-todos", SCMOPT_FIX_TODOS, RTGETOPT_REQ_NOTHING },
185 { "--no-fix-todos", SCMOPT_NO_FIX_TODOS, RTGETOPT_REQ_NOTHING },
186 { "--update-copyright-year", SCMOPT_UPDATE_COPYRIGHT_YEAR, RTGETOPT_REQ_NOTHING },
187 { "--no-update-copyright-year", SCMOPT_NO_UPDATE_COPYRIGHT_YEAR, RTGETOPT_REQ_NOTHING },
188 { "--no-update-license", SCMOPT_NO_UPDATE_LICENSE, RTGETOPT_REQ_NOTHING },
189 { "--license-ose-gpl", SCMOPT_LICENSE_OSE_GPL, RTGETOPT_REQ_NOTHING },
190 { "--license-ose-dual", SCMOPT_LICENSE_OSE_DUAL_GPL_CDDL, RTGETOPT_REQ_NOTHING },
191 { "--license-lgpl", SCMOPT_LICENSE_LGPL, RTGETOPT_REQ_NOTHING },
192 { "--license-mit", SCMOPT_LICENSE_MIT, RTGETOPT_REQ_NOTHING },
193 { "--only-svn-dirs", SCMOPT_ONLY_SVN_DIRS, RTGETOPT_REQ_NOTHING },
194 { "--not-only-svn-dirs", SCMOPT_NOT_ONLY_SVN_DIRS, RTGETOPT_REQ_NOTHING },
195 { "--only-svn-files", SCMOPT_ONLY_SVN_FILES, RTGETOPT_REQ_NOTHING },
196 { "--not-only-svn-files", SCMOPT_NOT_ONLY_SVN_FILES, RTGETOPT_REQ_NOTHING },
197 { "--set-svn-eol", SCMOPT_SET_SVN_EOL, RTGETOPT_REQ_NOTHING },
198 { "--dont-set-svn-eol", SCMOPT_DONT_SET_SVN_EOL, RTGETOPT_REQ_NOTHING },
199 { "--set-svn-executable", SCMOPT_SET_SVN_EXECUTABLE, RTGETOPT_REQ_NOTHING },
200 { "--dont-set-svn-executable", SCMOPT_DONT_SET_SVN_EXECUTABLE, RTGETOPT_REQ_NOTHING },
201 { "--set-svn-keywords", SCMOPT_SET_SVN_KEYWORDS, RTGETOPT_REQ_NOTHING },
202 { "--dont-set-svn-keywords", SCMOPT_DONT_SET_SVN_KEYWORDS, RTGETOPT_REQ_NOTHING },
203 { "--tab-size", SCMOPT_TAB_SIZE, RTGETOPT_REQ_UINT8 },
204 { "--width", SCMOPT_WIDTH, RTGETOPT_REQ_UINT8 },
205 { "--filter-out-dirs", SCMOPT_FILTER_OUT_DIRS, RTGETOPT_REQ_STRING },
206 { "--filter-files", SCMOPT_FILTER_FILES, RTGETOPT_REQ_STRING },
207 { "--filter-out-files", SCMOPT_FILTER_OUT_FILES, RTGETOPT_REQ_STRING },
208};
209
210/** Consider files matching the following patterns (base names only). */
211static const char *g_pszFileFilter = NULL;
212
213static PFNSCMREWRITER const g_aRewritersFor_Makefile_kup[] =
214{
215 rewrite_SvnNoExecutable,
216 rewrite_Makefile_kup
217};
218
219static PFNSCMREWRITER const g_aRewritersFor_Makefile_kmk[] =
220{
221 rewrite_ForceNativeEol,
222 rewrite_StripTrailingBlanks,
223 rewrite_AdjustTrailingLines,
224 rewrite_SvnNoExecutable,
225 rewrite_SvnKeywords,
226 rewrite_Copyright_HashComment,
227 rewrite_Makefile_kmk
228};
229
230static PFNSCMREWRITER const g_aRewritersFor_C_and_CPP[] =
231{
232 rewrite_ForceNativeEol,
233 rewrite_ExpandTabs,
234 rewrite_StripTrailingBlanks,
235 rewrite_AdjustTrailingLines,
236 rewrite_SvnNoExecutable,
237 rewrite_SvnKeywords,
238 rewrite_Copyright_CstyleComment,
239 rewrite_FixFlowerBoxMarkers,
240 rewrite_Fix_C_and_CPP_Todos,
241 rewrite_C_and_CPP
242};
243
244static PFNSCMREWRITER const g_aRewritersFor_H_and_HPP[] =
245{
246 rewrite_ForceNativeEol,
247 rewrite_ExpandTabs,
248 rewrite_StripTrailingBlanks,
249 rewrite_AdjustTrailingLines,
250 rewrite_SvnNoExecutable,
251 rewrite_Copyright_CstyleComment,
252 rewrite_C_and_CPP
253};
254
255static PFNSCMREWRITER const g_aRewritersFor_RC[] =
256{
257 rewrite_ForceNativeEol,
258 rewrite_ExpandTabs,
259 rewrite_StripTrailingBlanks,
260 rewrite_AdjustTrailingLines,
261 rewrite_SvnNoExecutable,
262 rewrite_SvnKeywords,
263 rewrite_Copyright_CstyleComment,
264};
265
266static PFNSCMREWRITER const g_aRewritersFor_DEF[] =
267{
268 rewrite_ForceNativeEol,
269 rewrite_ExpandTabs,
270 rewrite_StripTrailingBlanks,
271 rewrite_AdjustTrailingLines,
272 rewrite_SvnNoExecutable,
273 rewrite_SvnKeywords,
274 rewrite_Copyright_SemicolonComment,
275};
276
277static PFNSCMREWRITER const g_aRewritersFor_ShellScripts[] =
278{
279 rewrite_ForceLF,
280 rewrite_ExpandTabs,
281 rewrite_StripTrailingBlanks,
282 rewrite_Copyright_HashComment,
283};
284
285static PFNSCMREWRITER const g_aRewritersFor_BatchFiles[] =
286{
287 rewrite_ForceCRLF,
288 rewrite_ExpandTabs,
289 rewrite_StripTrailingBlanks,
290 rewrite_Copyright_RemComment,
291};
292
293static PFNSCMREWRITER const g_aRewritersFor_SedScripts[] =
294{
295 rewrite_ForceLF,
296 rewrite_ExpandTabs,
297 rewrite_StripTrailingBlanks,
298 rewrite_Copyright_HashComment,
299};
300
301static PFNSCMREWRITER const g_aRewritersFor_Python[] =
302{
303 /** @todo rewrite_ForceLFIfExecutable */
304 rewrite_ExpandTabs,
305 rewrite_StripTrailingBlanks,
306 rewrite_AdjustTrailingLines,
307 rewrite_SvnKeywords,
308 rewrite_Copyright_PythonComment,
309};
310
311
312static SCMCFGENTRY const g_aConfigs[] =
313{
314 { RT_ELEMENTS(g_aRewritersFor_Makefile_kup), &g_aRewritersFor_Makefile_kup[0], "Makefile.kup" },
315 { RT_ELEMENTS(g_aRewritersFor_Makefile_kmk), &g_aRewritersFor_Makefile_kmk[0], "Makefile.kmk|Config.kmk" },
316 { RT_ELEMENTS(g_aRewritersFor_C_and_CPP), &g_aRewritersFor_C_and_CPP[0], "*.c|*.cpp|*.C|*.CPP|*.cxx|*.cc|*.m|*.mm" },
317 { RT_ELEMENTS(g_aRewritersFor_H_and_HPP), &g_aRewritersFor_H_and_HPP[0], "*.h|*.hpp" },
318 { RT_ELEMENTS(g_aRewritersFor_RC), &g_aRewritersFor_RC[0], "*.rc" },
319 { RT_ELEMENTS(g_aRewritersFor_DEF), &g_aRewritersFor_DEF[0], "*.def" },
320 { RT_ELEMENTS(g_aRewritersFor_ShellScripts), &g_aRewritersFor_ShellScripts[0], "*.sh|configure" },
321 { RT_ELEMENTS(g_aRewritersFor_BatchFiles), &g_aRewritersFor_BatchFiles[0], "*.bat|*.cmd|*.btm|*.vbs|*.ps1" },
322 { RT_ELEMENTS(g_aRewritersFor_SedScripts), &g_aRewritersFor_SedScripts[0], "*.sed" },
323 { RT_ELEMENTS(g_aRewritersFor_Python), &g_aRewritersFor_Python[0], "*.py" },
324};
325
326
327
328/* -=-=-=-=-=- settings -=-=-=-=-=- */
329
330
331/**
332 * Init a settings structure with settings from @a pSrc.
333 *
334 * @returns IPRT status code
335 * @param pSettings The settings.
336 * @param pSrc The source settings.
337 */
338static int scmSettingsBaseInitAndCopy(PSCMSETTINGSBASE pSettings, PCSCMSETTINGSBASE pSrc)
339{
340 *pSettings = *pSrc;
341
342 int rc = RTStrDupEx(&pSettings->pszFilterFiles, pSrc->pszFilterFiles);
343 if (RT_SUCCESS(rc))
344 {
345 rc = RTStrDupEx(&pSettings->pszFilterOutFiles, pSrc->pszFilterOutFiles);
346 if (RT_SUCCESS(rc))
347 {
348 rc = RTStrDupEx(&pSettings->pszFilterOutDirs, pSrc->pszFilterOutDirs);
349 if (RT_SUCCESS(rc))
350 return VINF_SUCCESS;
351
352 RTStrFree(pSettings->pszFilterOutFiles);
353 }
354 RTStrFree(pSettings->pszFilterFiles);
355 }
356
357 pSettings->pszFilterFiles = NULL;
358 pSettings->pszFilterOutFiles = NULL;
359 pSettings->pszFilterOutDirs = NULL;
360 return rc;
361}
362
363/**
364 * Init a settings structure.
365 *
366 * @returns IPRT status code
367 * @param pSettings The settings.
368 */
369static int scmSettingsBaseInit(PSCMSETTINGSBASE pSettings)
370{
371 return scmSettingsBaseInitAndCopy(pSettings, &g_Defaults);
372}
373
374/**
375 * Deletes the settings, i.e. free any dynamically allocated content.
376 *
377 * @param pSettings The settings.
378 */
379static void scmSettingsBaseDelete(PSCMSETTINGSBASE pSettings)
380{
381 if (pSettings)
382 {
383 Assert(pSettings->cchTab != UINT8_MAX);
384 pSettings->cchTab = UINT8_MAX;
385
386 RTStrFree(pSettings->pszFilterFiles);
387 pSettings->pszFilterFiles = NULL;
388
389 RTStrFree(pSettings->pszFilterOutFiles);
390 pSettings->pszFilterOutFiles = NULL;
391
392 RTStrFree(pSettings->pszFilterOutDirs);
393 pSettings->pszFilterOutDirs = NULL;
394 }
395}
396
397
398/**
399 * Processes a RTGetOpt result.
400 *
401 * @retval VINF_SUCCESS if handled.
402 * @retval VERR_OUT_OF_RANGE if the option value was out of range.
403 * @retval VERR_GETOPT_UNKNOWN_OPTION if the option was not recognized.
404 *
405 * @param pSettings The settings to change.
406 * @param rc The RTGetOpt return value.
407 * @param pValueUnion The RTGetOpt value union.
408 */
409static int scmSettingsBaseHandleOpt(PSCMSETTINGSBASE pSettings, int rc, PRTGETOPTUNION pValueUnion)
410{
411 switch (rc)
412 {
413 case SCMOPT_CONVERT_EOL:
414 pSettings->fConvertEol = true;
415 return VINF_SUCCESS;
416 case SCMOPT_NO_CONVERT_EOL:
417 pSettings->fConvertEol = false;
418 return VINF_SUCCESS;
419
420 case SCMOPT_CONVERT_TABS:
421 pSettings->fConvertTabs = true;
422 return VINF_SUCCESS;
423 case SCMOPT_NO_CONVERT_TABS:
424 pSettings->fConvertTabs = false;
425 return VINF_SUCCESS;
426
427 case SCMOPT_FORCE_FINAL_EOL:
428 pSettings->fForceFinalEol = true;
429 return VINF_SUCCESS;
430 case SCMOPT_NO_FORCE_FINAL_EOL:
431 pSettings->fForceFinalEol = false;
432 return VINF_SUCCESS;
433
434 case SCMOPT_FORCE_TRAILING_LINE:
435 pSettings->fForceTrailingLine = true;
436 return VINF_SUCCESS;
437 case SCMOPT_NO_FORCE_TRAILING_LINE:
438 pSettings->fForceTrailingLine = false;
439 return VINF_SUCCESS;
440
441
442 case SCMOPT_STRIP_TRAILING_BLANKS:
443 pSettings->fStripTrailingBlanks = true;
444 return VINF_SUCCESS;
445 case SCMOPT_NO_STRIP_TRAILING_BLANKS:
446 pSettings->fStripTrailingBlanks = false;
447 return VINF_SUCCESS;
448
449 case SCMOPT_MIN_BLANK_LINES_BEFORE_FLOWER_BOX_MARKERS:
450 pSettings->cMinBlankLinesBeforeFlowerBoxMakers = pValueUnion->u8;
451 return VINF_SUCCESS;
452
453
454 case SCMOPT_STRIP_TRAILING_LINES:
455 pSettings->fStripTrailingLines = true;
456 return VINF_SUCCESS;
457 case SCMOPT_NO_STRIP_TRAILING_LINES:
458 pSettings->fStripTrailingLines = false;
459 return VINF_SUCCESS;
460
461 case SCMOPT_FIX_FLOWER_BOX_MARKERS:
462 pSettings->fFixFlowerBoxMarkers = true;
463 return VINF_SUCCESS;
464 case SCMOPT_NO_FIX_FLOWER_BOX_MARKERS:
465 pSettings->fFixFlowerBoxMarkers = false;
466 return VINF_SUCCESS;
467
468 case SCMOPT_UPDATE_COPYRIGHT_YEAR:
469 pSettings->fUpdateCopyrightYear = true;
470 return VINF_SUCCESS;
471 case SCMOPT_NO_UPDATE_COPYRIGHT_YEAR:
472 pSettings->fUpdateCopyrightYear = false;
473 return VINF_SUCCESS;
474
475 case SCMOPT_NO_UPDATE_LICENSE:
476 pSettings->enmUpdateLicense = kScmLicense_LeaveAlone;
477 return VINF_SUCCESS;
478 case SCMOPT_LICENSE_OSE_GPL:
479 pSettings->enmUpdateLicense = kScmLicense_OseGpl;
480 return VINF_SUCCESS;
481 case SCMOPT_LICENSE_OSE_DUAL_GPL_CDDL:
482 pSettings->enmUpdateLicense = kScmLicense_OseDualGplCddl;
483 return VINF_SUCCESS;
484 case SCMOPT_LICENSE_LGPL:
485 pSettings->enmUpdateLicense = kScmLicense_Lgpl;
486 return VINF_SUCCESS;
487 case SCMOPT_LICENSE_MIT:
488 pSettings->enmUpdateLicense = kScmLicense_Mit;
489 return VINF_SUCCESS;
490
491 case SCMOPT_ONLY_SVN_DIRS:
492 pSettings->fOnlySvnDirs = true;
493 return VINF_SUCCESS;
494 case SCMOPT_NOT_ONLY_SVN_DIRS:
495 pSettings->fOnlySvnDirs = false;
496 return VINF_SUCCESS;
497
498 case SCMOPT_ONLY_SVN_FILES:
499 pSettings->fOnlySvnFiles = true;
500 return VINF_SUCCESS;
501 case SCMOPT_NOT_ONLY_SVN_FILES:
502 pSettings->fOnlySvnFiles = false;
503 return VINF_SUCCESS;
504
505 case SCMOPT_SET_SVN_EOL:
506 pSettings->fSetSvnEol = true;
507 return VINF_SUCCESS;
508 case SCMOPT_DONT_SET_SVN_EOL:
509 pSettings->fSetSvnEol = false;
510 return VINF_SUCCESS;
511
512 case SCMOPT_SET_SVN_EXECUTABLE:
513 pSettings->fSetSvnExecutable = true;
514 return VINF_SUCCESS;
515 case SCMOPT_DONT_SET_SVN_EXECUTABLE:
516 pSettings->fSetSvnExecutable = false;
517 return VINF_SUCCESS;
518
519 case SCMOPT_SET_SVN_KEYWORDS:
520 pSettings->fSetSvnKeywords = true;
521 return VINF_SUCCESS;
522 case SCMOPT_DONT_SET_SVN_KEYWORDS:
523 pSettings->fSetSvnKeywords = false;
524 return VINF_SUCCESS;
525
526 case SCMOPT_TAB_SIZE:
527 if ( pValueUnion->u8 < 1
528 || pValueUnion->u8 >= RT_ELEMENTS(g_szTabSpaces))
529 {
530 RTMsgError("Invalid tab size: %u - must be in {1..%u}\n",
531 pValueUnion->u8, RT_ELEMENTS(g_szTabSpaces) - 1);
532 return VERR_OUT_OF_RANGE;
533 }
534 pSettings->cchTab = pValueUnion->u8;
535 return VINF_SUCCESS;
536
537 case SCMOPT_WIDTH:
538 if (pValueUnion->u8 < 20 || pValueUnion->u8 > 200)
539 {
540 RTMsgError("Invalid width size: %u - must be in {20..200} range\n", pValueUnion->u8);
541 return VERR_OUT_OF_RANGE;
542 }
543 pSettings->cchWidth = pValueUnion->u8;
544 return VINF_SUCCESS;
545
546 case SCMOPT_FILTER_OUT_DIRS:
547 case SCMOPT_FILTER_FILES:
548 case SCMOPT_FILTER_OUT_FILES:
549 {
550 char **ppsz = NULL;
551 switch (rc)
552 {
553 case SCMOPT_FILTER_OUT_DIRS: ppsz = &pSettings->pszFilterOutDirs; break;
554 case SCMOPT_FILTER_FILES: ppsz = &pSettings->pszFilterFiles; break;
555 case SCMOPT_FILTER_OUT_FILES: ppsz = &pSettings->pszFilterOutFiles; break;
556 }
557
558 /*
559 * An empty string zaps the current list.
560 */
561 if (!*pValueUnion->psz)
562 return RTStrATruncate(ppsz, 0);
563
564 /*
565 * Non-empty strings are appended to the pattern list.
566 *
567 * Strip leading and trailing pattern separators before attempting
568 * to append it. If it's just separators, don't do anything.
569 */
570 const char *pszSrc = pValueUnion->psz;
571 while (*pszSrc == '|')
572 pszSrc++;
573 size_t cchSrc = strlen(pszSrc);
574 while (cchSrc > 0 && pszSrc[cchSrc - 1] == '|')
575 cchSrc--;
576 if (!cchSrc)
577 return VINF_SUCCESS;
578
579 return RTStrAAppendExN(ppsz, 2,
580 "|", *ppsz && **ppsz ? (size_t)1 : (size_t)0,
581 pszSrc, cchSrc);
582 }
583
584 default:
585 return VERR_GETOPT_UNKNOWN_OPTION;
586 }
587}
588
589/**
590 * Parses an option string.
591 *
592 * @returns IPRT status code.
593 * @param pBase The base settings structure to apply the options
594 * to.
595 * @param pszOptions The options to parse.
596 */
597static int scmSettingsBaseParseString(PSCMSETTINGSBASE pBase, const char *pszLine)
598{
599 int cArgs;
600 char **papszArgs;
601 int rc = RTGetOptArgvFromString(&papszArgs, &cArgs, pszLine, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH, NULL);
602 if (RT_SUCCESS(rc))
603 {
604 RTGETOPTUNION ValueUnion;
605 RTGETOPTSTATE GetOptState;
606 rc = RTGetOptInit(&GetOptState, cArgs, papszArgs, &g_aScmOpts[0], RT_ELEMENTS(g_aScmOpts), 0, 0 /*fFlags*/);
607 if (RT_SUCCESS(rc))
608 {
609 while ((rc = RTGetOpt(&GetOptState, &ValueUnion)) != 0)
610 {
611 rc = scmSettingsBaseHandleOpt(pBase, rc, &ValueUnion);
612 if (RT_FAILURE(rc))
613 break;
614 }
615 }
616 RTGetOptArgvFree(papszArgs);
617 }
618
619 return rc;
620}
621
622/**
623 * Parses an unterminated option string.
624 *
625 * @returns IPRT status code.
626 * @param pBase The base settings structure to apply the options
627 * to.
628 * @param pchLine The line.
629 * @param cchLine The line length.
630 */
631static int scmSettingsBaseParseStringN(PSCMSETTINGSBASE pBase, const char *pchLine, size_t cchLine)
632{
633 char *pszLine = RTStrDupN(pchLine, cchLine);
634 if (!pszLine)
635 return VERR_NO_MEMORY;
636 int rc = scmSettingsBaseParseString(pBase, pszLine);
637 RTStrFree(pszLine);
638 return rc;
639}
640
641/**
642 * Verifies the options string.
643 *
644 * @returns IPRT status code.
645 * @param pszOptions The options to verify .
646 */
647static int scmSettingsBaseVerifyString(const char *pszOptions)
648{
649 SCMSETTINGSBASE Base;
650 int rc = scmSettingsBaseInit(&Base);
651 if (RT_SUCCESS(rc))
652 {
653 rc = scmSettingsBaseParseString(&Base, pszOptions);
654 scmSettingsBaseDelete(&Base);
655 }
656 return rc;
657}
658
659/**
660 * Loads settings found in editor and SCM settings directives within the
661 * document (@a pStream).
662 *
663 * @returns IPRT status code.
664 * @param pBase The settings base to load settings into.
665 * @param pStream The stream to scan for settings directives.
666 */
667static int scmSettingsBaseLoadFromDocument(PSCMSETTINGSBASE pBase, PSCMSTREAM pStream)
668{
669 /** @todo Editor and SCM settings directives in documents. */
670 RT_NOREF2(pBase, pStream);
671 return VINF_SUCCESS;
672}
673
674/**
675 * Creates a new settings file struct, cloning @a pSettings.
676 *
677 * @returns IPRT status code.
678 * @param ppSettings Where to return the new struct.
679 * @param pSettingsBase The settings to inherit from.
680 */
681static int scmSettingsCreate(PSCMSETTINGS *ppSettings, PCSCMSETTINGSBASE pSettingsBase)
682{
683 PSCMSETTINGS pSettings = (PSCMSETTINGS)RTMemAlloc(sizeof(*pSettings));
684 if (!pSettings)
685 return VERR_NO_MEMORY;
686 int rc = scmSettingsBaseInitAndCopy(&pSettings->Base, pSettingsBase);
687 if (RT_SUCCESS(rc))
688 {
689 pSettings->pDown = NULL;
690 pSettings->pUp = NULL;
691 pSettings->paPairs = NULL;
692 pSettings->cPairs = 0;
693 *ppSettings = pSettings;
694 return VINF_SUCCESS;
695 }
696 RTMemFree(pSettings);
697 return rc;
698}
699
700/**
701 * Destroys a settings structure.
702 *
703 * @param pSettings The settings structure to destroy. NULL is OK.
704 */
705static void scmSettingsDestroy(PSCMSETTINGS pSettings)
706{
707 if (pSettings)
708 {
709 scmSettingsBaseDelete(&pSettings->Base);
710 for (size_t i = 0; i < pSettings->cPairs; i++)
711 {
712 RTStrFree(pSettings->paPairs[i].pszPattern);
713 RTStrFree(pSettings->paPairs[i].pszOptions);
714 pSettings->paPairs[i].pszPattern = NULL;
715 pSettings->paPairs[i].pszOptions = NULL;
716 }
717 RTMemFree(pSettings->paPairs);
718 pSettings->paPairs = NULL;
719 RTMemFree(pSettings);
720 }
721}
722
723/**
724 * Adds a pattern/options pair to the settings structure.
725 *
726 * @returns IPRT status code.
727 * @param pSettings The settings.
728 * @param pchLine The line containing the unparsed pair.
729 * @param cchLine The length of the line.
730 */
731static int scmSettingsAddPair(PSCMSETTINGS pSettings, const char *pchLine, size_t cchLine)
732{
733 /*
734 * Split the string.
735 */
736 const char *pchOptions = (const char *)memchr(pchLine, ':', cchLine);
737 if (!pchOptions)
738 return VERR_INVALID_PARAMETER;
739 size_t cchPattern = pchOptions - pchLine;
740 size_t cchOptions = cchLine - cchPattern - 1;
741 pchOptions++;
742
743 /* strip spaces everywhere */
744 while (cchPattern > 0 && RT_C_IS_SPACE(pchLine[cchPattern - 1]))
745 cchPattern--;
746 while (cchPattern > 0 && RT_C_IS_SPACE(*pchLine))
747 cchPattern--, pchLine++;
748
749 while (cchOptions > 0 && RT_C_IS_SPACE(pchOptions[cchOptions - 1]))
750 cchOptions--;
751 while (cchOptions > 0 && RT_C_IS_SPACE(*pchOptions))
752 cchOptions--, pchOptions++;
753
754 /* Quietly ignore empty patterns and empty options. */
755 if (!cchOptions || !cchPattern)
756 return VINF_SUCCESS;
757
758 /*
759 * Add the pair and verify the option string.
760 */
761 uint32_t iPair = pSettings->cPairs;
762 if ((iPair % 32) == 0)
763 {
764 void *pvNew = RTMemRealloc(pSettings->paPairs, (iPair + 32) * sizeof(pSettings->paPairs[0]));
765 if (!pvNew)
766 return VERR_NO_MEMORY;
767 pSettings->paPairs = (PSCMPATRNOPTPAIR)pvNew;
768 }
769
770 pSettings->paPairs[iPair].pszPattern = RTStrDupN(pchLine, cchPattern);
771 pSettings->paPairs[iPair].pszOptions = RTStrDupN(pchOptions, cchOptions);
772 int rc;
773 if ( pSettings->paPairs[iPair].pszPattern
774 && pSettings->paPairs[iPair].pszOptions)
775 rc = scmSettingsBaseVerifyString(pSettings->paPairs[iPair].pszOptions);
776 else
777 rc = VERR_NO_MEMORY;
778 if (RT_SUCCESS(rc))
779 pSettings->cPairs = iPair + 1;
780 else
781 {
782 RTStrFree(pSettings->paPairs[iPair].pszPattern);
783 RTStrFree(pSettings->paPairs[iPair].pszOptions);
784 }
785 return rc;
786}
787
788/**
789 * Loads in the settings from @a pszFilename.
790 *
791 * @returns IPRT status code.
792 * @param pSettings Where to load the settings file.
793 * @param pszFilename The file to load.
794 */
795static int scmSettingsLoadFile(PSCMSETTINGS pSettings, const char *pszFilename)
796{
797 ScmVerbose(NULL, 3, "Loading settings file '%s'...\n", pszFilename);
798
799 SCMSTREAM Stream;
800 int rc = ScmStreamInitForReading(&Stream, pszFilename);
801 if (RT_FAILURE(rc))
802 {
803 RTMsgError("%s: ScmStreamInitForReading -> %Rrc\n", pszFilename, rc);
804 return rc;
805 }
806
807 SCMEOL enmEol;
808 const char *pchLine;
809 size_t cchLine;
810 while ((pchLine = ScmStreamGetLine(&Stream, &cchLine, &enmEol)) != NULL)
811 {
812 /* Ignore leading spaces. */
813 while (cchLine > 0 && RT_C_IS_SPACE(*pchLine))
814 pchLine++, cchLine--;
815
816 /* Ignore empty lines and comment lines. */
817 if (cchLine < 1 || *pchLine == '#')
818 continue;
819
820 /* What kind of line is it? */
821 const char *pchColon = (const char *)memchr(pchLine, ':', cchLine);
822 if (pchColon)
823 rc = scmSettingsAddPair(pSettings, pchLine, cchLine);
824 else
825 rc = scmSettingsBaseParseStringN(&pSettings->Base, pchLine, cchLine);
826 if (RT_FAILURE(rc))
827 {
828 RTMsgError("%s:%d: %Rrc\n", pszFilename, ScmStreamTellLine(&Stream), rc);
829 break;
830 }
831 }
832
833 if (RT_SUCCESS(rc))
834 {
835 rc = ScmStreamGetStatus(&Stream);
836 if (RT_FAILURE(rc))
837 RTMsgError("%s: ScmStreamGetStatus- > %Rrc\n", pszFilename, rc);
838 }
839
840 ScmStreamDelete(&Stream);
841 return rc;
842}
843
844#if 0 /* unused */
845/**
846 * Parse the specified settings file creating a new settings struct from it.
847 *
848 * @returns IPRT status code
849 * @param ppSettings Where to return the new settings.
850 * @param pszFilename The file to parse.
851 * @param pSettingsBase The base settings we inherit from.
852 */
853static int scmSettingsCreateFromFile(PSCMSETTINGS *ppSettings, const char *pszFilename, PCSCMSETTINGSBASE pSettingsBase)
854{
855 PSCMSETTINGS pSettings;
856 int rc = scmSettingsCreate(&pSettings, pSettingsBase);
857 if (RT_SUCCESS(rc))
858 {
859 rc = scmSettingsLoadFile(pSettings, pszFilename);
860 if (RT_SUCCESS(rc))
861 {
862 *ppSettings = pSettings;
863 return VINF_SUCCESS;
864 }
865
866 scmSettingsDestroy(pSettings);
867 }
868 *ppSettings = NULL;
869 return rc;
870}
871#endif
872
873
874/**
875 * Create an initial settings structure when starting processing a new file or
876 * directory.
877 *
878 * This will look for .scm-settings files from the root and down to the
879 * specified directory, combining them into the returned settings structure.
880 *
881 * @returns IPRT status code.
882 * @param ppSettings Where to return the pointer to the top stack
883 * object.
884 * @param pBaseSettings The base settings we inherit from (globals
885 * typically).
886 * @param pszPath The absolute path to the new directory or file.
887 */
888static int scmSettingsCreateForPath(PSCMSETTINGS *ppSettings, PCSCMSETTINGSBASE pBaseSettings, const char *pszPath)
889{
890 *ppSettings = NULL; /* try shut up gcc. */
891
892 /*
893 * We'll be working with a stack copy of the path.
894 */
895 char szFile[RTPATH_MAX];
896 size_t cchDir = strlen(pszPath);
897 if (cchDir >= sizeof(szFile) - sizeof(SCM_SETTINGS_FILENAME))
898 return VERR_FILENAME_TOO_LONG;
899
900 /*
901 * Create the bottom-most settings.
902 */
903 PSCMSETTINGS pSettings;
904 int rc = scmSettingsCreate(&pSettings, pBaseSettings);
905 if (RT_FAILURE(rc))
906 return rc;
907
908 /*
909 * Enumerate the path components from the root and down. Load any setting
910 * files we find.
911 */
912 size_t cComponents = RTPathCountComponents(pszPath);
913 for (size_t i = 1; i <= cComponents; i++)
914 {
915 rc = RTPathCopyComponents(szFile, sizeof(szFile), pszPath, i);
916 if (RT_SUCCESS(rc))
917 rc = RTPathAppend(szFile, sizeof(szFile), SCM_SETTINGS_FILENAME);
918 if (RT_FAILURE(rc))
919 break;
920 if (RTFileExists(szFile))
921 {
922 rc = scmSettingsLoadFile(pSettings, szFile);
923 if (RT_FAILURE(rc))
924 break;
925 }
926 }
927
928 if (RT_SUCCESS(rc))
929 *ppSettings = pSettings;
930 else
931 scmSettingsDestroy(pSettings);
932 return rc;
933}
934
935/**
936 * Pushes a new settings set onto the stack.
937 *
938 * @param ppSettingsStack The pointer to the pointer to the top stack
939 * element. This will be used as input and output.
940 * @param pSettings The settings to push onto the stack.
941 */
942static void scmSettingsStackPush(PSCMSETTINGS *ppSettingsStack, PSCMSETTINGS pSettings)
943{
944 PSCMSETTINGS pOld = *ppSettingsStack;
945 pSettings->pDown = pOld;
946 pSettings->pUp = NULL;
947 if (pOld)
948 pOld->pUp = pSettings;
949 *ppSettingsStack = pSettings;
950}
951
952/**
953 * Pushes the settings of the specified directory onto the stack.
954 *
955 * We will load any .scm-settings in the directory. A stack entry is added even
956 * if no settings file was found.
957 *
958 * @returns IPRT status code.
959 * @param ppSettingsStack The pointer to the pointer to the top stack
960 * element. This will be used as input and output.
961 * @param pszDir The directory to do this for.
962 */
963static int scmSettingsStackPushDir(PSCMSETTINGS *ppSettingsStack, const char *pszDir)
964{
965 char szFile[RTPATH_MAX];
966 int rc = RTPathJoin(szFile, sizeof(szFile), pszDir, SCM_SETTINGS_FILENAME);
967 if (RT_SUCCESS(rc))
968 {
969 PSCMSETTINGS pSettings;
970 rc = scmSettingsCreate(&pSettings, &(*ppSettingsStack)->Base);
971 if (RT_SUCCESS(rc))
972 {
973 if (RTFileExists(szFile))
974 rc = scmSettingsLoadFile(pSettings, szFile);
975 if (RT_SUCCESS(rc))
976 {
977 scmSettingsStackPush(ppSettingsStack, pSettings);
978 return VINF_SUCCESS;
979 }
980
981 scmSettingsDestroy(pSettings);
982 }
983 }
984 return rc;
985}
986
987
988/**
989 * Pops a settings set off the stack.
990 *
991 * @returns The popped setttings.
992 * @param ppSettingsStack The pointer to the pointer to the top stack
993 * element. This will be used as input and output.
994 */
995static PSCMSETTINGS scmSettingsStackPop(PSCMSETTINGS *ppSettingsStack)
996{
997 PSCMSETTINGS pRet = *ppSettingsStack;
998 PSCMSETTINGS pNew = pRet ? pRet->pDown : NULL;
999 *ppSettingsStack = pNew;
1000 if (pNew)
1001 pNew->pUp = NULL;
1002 if (pRet)
1003 {
1004 pRet->pUp = NULL;
1005 pRet->pDown = NULL;
1006 }
1007 return pRet;
1008}
1009
1010/**
1011 * Pops and destroys the top entry of the stack.
1012 *
1013 * @param ppSettingsStack The pointer to the pointer to the top stack
1014 * element. This will be used as input and output.
1015 */
1016static void scmSettingsStackPopAndDestroy(PSCMSETTINGS *ppSettingsStack)
1017{
1018 scmSettingsDestroy(scmSettingsStackPop(ppSettingsStack));
1019}
1020
1021/**
1022 * Constructs the base settings for the specified file name.
1023 *
1024 * @returns IPRT status code.
1025 * @param pSettingsStack The top element on the settings stack.
1026 * @param pszFilename The file name.
1027 * @param pszBasename The base name (pointer within @a pszFilename).
1028 * @param cchBasename The length of the base name. (For passing to
1029 * RTStrSimplePatternMultiMatch.)
1030 * @param pBase Base settings to initialize.
1031 */
1032static int scmSettingsStackMakeFileBase(PCSCMSETTINGS pSettingsStack, const char *pszFilename,
1033 const char *pszBasename, size_t cchBasename, PSCMSETTINGSBASE pBase)
1034{
1035 int rc = scmSettingsBaseInitAndCopy(pBase, &pSettingsStack->Base);
1036 if (RT_SUCCESS(rc))
1037 {
1038 /* find the bottom entry in the stack. */
1039 PCSCMSETTINGS pCur = pSettingsStack;
1040 while (pCur->pDown)
1041 pCur = pCur->pDown;
1042
1043 /* Work our way up thru the stack and look for matching pairs. */
1044 while (pCur)
1045 {
1046 size_t const cPairs = pCur->cPairs;
1047 if (cPairs)
1048 {
1049 for (size_t i = 0; i < cPairs; i++)
1050 if ( RTStrSimplePatternMultiMatch(pCur->paPairs[i].pszPattern, RTSTR_MAX,
1051 pszBasename, cchBasename, NULL)
1052 || RTStrSimplePatternMultiMatch(pCur->paPairs[i].pszPattern, RTSTR_MAX,
1053 pszFilename, RTSTR_MAX, NULL))
1054 {
1055 rc = scmSettingsBaseParseString(pBase, pCur->paPairs[i].pszOptions);
1056 if (RT_FAILURE(rc))
1057 break;
1058 }
1059 if (RT_FAILURE(rc))
1060 break;
1061 }
1062
1063 /* advance */
1064 pCur = pCur->pUp;
1065 }
1066 }
1067 if (RT_FAILURE(rc))
1068 scmSettingsBaseDelete(pBase);
1069 return rc;
1070}
1071
1072
1073/* -=-=-=-=-=- misc -=-=-=-=-=- */
1074
1075
1076/**
1077 * Prints a verbose message if the level is high enough.
1078 *
1079 * @param pState The rewrite state. Optional.
1080 * @param iLevel The required verbosity level.
1081 * @param pszFormat The message format string. Can be NULL if we
1082 * only want to trigger the per file message.
1083 * @param ... Format arguments.
1084 */
1085void ScmVerbose(PSCMRWSTATE pState, int iLevel, const char *pszFormat, ...)
1086{
1087 if (iLevel <= g_iVerbosity)
1088 {
1089 if (pState && !pState->fFirst)
1090 {
1091 RTPrintf("%s: info: --= Rewriting '%s' =--\n", g_szProgName, pState->pszFilename);
1092 pState->fFirst = true;
1093 }
1094 if (pszFormat)
1095 {
1096 RTPrintf(pState
1097 ? "%s: info: "
1098 : "%s: info: ",
1099 g_szProgName);
1100 va_list va;
1101 va_start(va, pszFormat);
1102 RTPrintfV(pszFormat, va);
1103 va_end(va);
1104 }
1105 }
1106}
1107
1108
1109/**
1110 * Prints an error message.
1111 *
1112 * @returns false
1113 * @param pState The rewrite state. Optional.
1114 * @param rc The error code.
1115 * @param pszFormat The message format string.
1116 * @param ... Format arguments.
1117 */
1118bool ScmError(PSCMRWSTATE pState, int rc, const char *pszFormat, ...)
1119{
1120 if (RT_SUCCESS(pState->rc))
1121 pState->rc = rc;
1122
1123 if (!pState->fFirst)
1124 {
1125 RTPrintf("%s: info: --= Rewriting '%s' =--\n", g_szProgName, pState->pszFilename);
1126 pState->fFirst = true;
1127 }
1128 va_list va;
1129 va_start(va, pszFormat);
1130 RTPrintf("%s: error: %s: %N", g_szProgName, pState->pszFilename, pszFormat, &va);
1131 va_end(va);
1132
1133 return false;
1134}
1135
1136
1137/* -=-=-=-=-=- file and directory processing -=-=-=-=-=- */
1138
1139
1140/**
1141 * Processes a file.
1142 *
1143 * @returns IPRT status code.
1144 * @param pState The rewriter state.
1145 * @param pszFilename The file name.
1146 * @param pszBasename The base name (pointer within @a pszFilename).
1147 * @param cchBasename The length of the base name. (For passing to
1148 * RTStrSimplePatternMultiMatch.)
1149 * @param pBaseSettings The base settings to use. It's OK to modify
1150 * these.
1151 */
1152static int scmProcessFileInner(PSCMRWSTATE pState, const char *pszFilename, const char *pszBasename, size_t cchBasename,
1153 PSCMSETTINGSBASE pBaseSettings)
1154{
1155 /*
1156 * Do the file level filtering.
1157 */
1158 if ( pBaseSettings->pszFilterFiles
1159 && *pBaseSettings->pszFilterFiles
1160 && !RTStrSimplePatternMultiMatch(pBaseSettings->pszFilterFiles, RTSTR_MAX, pszBasename, cchBasename, NULL))
1161 {
1162 ScmVerbose(NULL, 5, "skipping '%s': file filter mismatch\n", pszFilename);
1163 return VINF_SUCCESS;
1164 }
1165 if ( pBaseSettings->pszFilterOutFiles
1166 && *pBaseSettings->pszFilterOutFiles
1167 && ( RTStrSimplePatternMultiMatch(pBaseSettings->pszFilterOutFiles, RTSTR_MAX, pszBasename, cchBasename, NULL)
1168 || RTStrSimplePatternMultiMatch(pBaseSettings->pszFilterOutFiles, RTSTR_MAX, pszFilename, RTSTR_MAX, NULL)) )
1169 {
1170 ScmVerbose(NULL, 5, "skipping '%s': filterd out\n", pszFilename);
1171 return VINF_SUCCESS;
1172 }
1173 if ( pBaseSettings->fOnlySvnFiles
1174 && !ScmSvnIsInWorkingCopy(pState))
1175 {
1176 ScmVerbose(NULL, 5, "skipping '%s': not in SVN WC\n", pszFilename);
1177 return VINF_SUCCESS;
1178 }
1179
1180 /*
1181 * Try find a matching rewrite config for this filename.
1182 */
1183 PCSCMCFGENTRY pCfg = NULL;
1184 for (size_t iCfg = 0; iCfg < RT_ELEMENTS(g_aConfigs); iCfg++)
1185 if (RTStrSimplePatternMultiMatch(g_aConfigs[iCfg].pszFilePattern, RTSTR_MAX, pszBasename, cchBasename, NULL))
1186 {
1187 pCfg = &g_aConfigs[iCfg];
1188 break;
1189 }
1190 if (!pCfg)
1191 {
1192 ScmVerbose(NULL, 4, "skipping '%s': no rewriters configured\n", pszFilename);
1193 return VINF_SUCCESS;
1194 }
1195 ScmVerbose(pState, 4, "matched \"%s\"\n", pCfg->pszFilePattern);
1196
1197 /*
1198 * Create an input stream from the file and check that it's text.
1199 */
1200 SCMSTREAM Stream1;
1201 int rc = ScmStreamInitForReading(&Stream1, pszFilename);
1202 if (RT_FAILURE(rc))
1203 {
1204 RTMsgError("Failed to read '%s': %Rrc\n", pszFilename, rc);
1205 return rc;
1206 }
1207 if (ScmStreamIsText(&Stream1))
1208 {
1209 ScmVerbose(pState, 3, NULL);
1210
1211 /*
1212 * Gather SCM and editor settings from the stream.
1213 */
1214 rc = scmSettingsBaseLoadFromDocument(pBaseSettings, &Stream1);
1215 if (RT_SUCCESS(rc))
1216 {
1217 ScmStreamRewindForReading(&Stream1);
1218
1219 /*
1220 * Create two more streams for output and push the text thru all the
1221 * rewriters, switching the two streams around when something is
1222 * actually rewritten. Stream1 remains unchanged.
1223 */
1224 SCMSTREAM Stream2;
1225 rc = ScmStreamInitForWriting(&Stream2, &Stream1);
1226 if (RT_SUCCESS(rc))
1227 {
1228 SCMSTREAM Stream3;
1229 rc = ScmStreamInitForWriting(&Stream3, &Stream1);
1230 if (RT_SUCCESS(rc))
1231 {
1232 bool fModified = false;
1233 PSCMSTREAM pIn = &Stream1;
1234 PSCMSTREAM pOut = &Stream2;
1235 for (size_t iRw = 0; iRw < pCfg->cRewriters; iRw++)
1236 {
1237 pState->rc = VINF_SUCCESS;
1238 bool fRc = pCfg->papfnRewriter[iRw](pState, pIn, pOut, pBaseSettings);
1239 if (RT_FAILURE(pState->rc))
1240 break;
1241 if (fRc)
1242 {
1243 PSCMSTREAM pTmp = pOut;
1244 pOut = pIn == &Stream1 ? &Stream3 : pIn;
1245 pIn = pTmp;
1246 fModified = true;
1247 }
1248
1249 ScmStreamRewindForReading(pIn);
1250 ScmStreamRewindForWriting(pOut);
1251 }
1252
1253 rc = pState->rc;
1254 if (RT_SUCCESS(rc))
1255 {
1256 rc = ScmStreamGetStatus(&Stream1);
1257 if (RT_SUCCESS(rc))
1258 rc = ScmStreamGetStatus(&Stream2);
1259 if (RT_SUCCESS(rc))
1260 rc = ScmStreamGetStatus(&Stream3);
1261 if (RT_SUCCESS(rc))
1262 {
1263 /*
1264 * If rewritten, write it back to disk.
1265 */
1266 if (fModified)
1267 {
1268 if (!g_fDryRun)
1269 {
1270 ScmVerbose(pState, 1, "writing modified file to \"%s%s\"\n", pszFilename, g_pszChangedSuff);
1271 rc = ScmStreamWriteToFile(pIn, "%s%s", pszFilename, g_pszChangedSuff);
1272 if (RT_FAILURE(rc))
1273 RTMsgError("Error writing '%s%s': %Rrc\n", pszFilename, g_pszChangedSuff, rc);
1274 }
1275 else
1276 {
1277 ScmVerbose(pState, 1, NULL);
1278 ScmDiffStreams(pszFilename, &Stream1, pIn, g_fDiffIgnoreEol,
1279 g_fDiffIgnoreLeadingWS, g_fDiffIgnoreTrailingWS, g_fDiffSpecialChars,
1280 pBaseSettings->cchTab, g_pStdOut);
1281 ScmVerbose(pState, 2, "would have modified the file \"%s%s\"\n",
1282 pszFilename, g_pszChangedSuff);
1283 }
1284 }
1285
1286 /*
1287 * If pending SVN property changes, apply them.
1288 */
1289 if (pState->cSvnPropChanges && RT_SUCCESS(rc))
1290 {
1291 if (!g_fDryRun)
1292 {
1293 rc = ScmSvnApplyChanges(pState);
1294 if (RT_FAILURE(rc))
1295 RTMsgError("%s: failed to apply SVN property changes (%Rrc)\n", pszFilename, rc);
1296 }
1297 else
1298 ScmSvnDisplayChanges(pState);
1299 }
1300
1301 if (!fModified && !pState->cSvnPropChanges)
1302 ScmVerbose(pState, 3, "no change\n", pszFilename);
1303 }
1304 else
1305 RTMsgError("%s: stream error %Rrc\n", pszFilename, rc);
1306 }
1307 ScmStreamDelete(&Stream3);
1308 }
1309 else
1310 RTMsgError("Failed to init stream for writing: %Rrc\n", rc);
1311 ScmStreamDelete(&Stream2);
1312 }
1313 else
1314 RTMsgError("Failed to init stream for writing: %Rrc\n", rc);
1315 }
1316 else
1317 RTMsgError("scmSettingsBaseLoadFromDocument: %Rrc\n", rc);
1318 }
1319 else
1320 ScmVerbose(pState, 4, "not text file: \"%s\"\n", pszFilename);
1321 ScmStreamDelete(&Stream1);
1322
1323 return rc;
1324}
1325
1326/**
1327 * Processes a file.
1328 *
1329 * This is just a wrapper for scmProcessFileInner for avoid wasting stack in the
1330 * directory recursion method.
1331 *
1332 * @returns IPRT status code.
1333 * @param pszFilename The file name.
1334 * @param pszBasename The base name (pointer within @a pszFilename).
1335 * @param cchBasename The length of the base name. (For passing to
1336 * RTStrSimplePatternMultiMatch.)
1337 * @param pSettingsStack The settings stack (pointer to the top element).
1338 */
1339static int scmProcessFile(const char *pszFilename, const char *pszBasename, size_t cchBasename,
1340 PSCMSETTINGS pSettingsStack)
1341{
1342 SCMSETTINGSBASE Base;
1343 int rc = scmSettingsStackMakeFileBase(pSettingsStack, pszFilename, pszBasename, cchBasename, &Base);
1344 if (RT_SUCCESS(rc))
1345 {
1346 SCMRWSTATE State;
1347 State.fFirst = false;
1348 State.pszFilename = pszFilename;
1349 State.cSvnPropChanges = 0;
1350 State.paSvnPropChanges = NULL;
1351 State.rc = VINF_SUCCESS;
1352
1353 rc = scmProcessFileInner(&State, pszFilename, pszBasename, cchBasename, &Base);
1354
1355 size_t i = State.cSvnPropChanges;
1356 while (i-- > 0)
1357 {
1358 RTStrFree(State.paSvnPropChanges[i].pszName);
1359 RTStrFree(State.paSvnPropChanges[i].pszValue);
1360 }
1361 RTMemFree(State.paSvnPropChanges);
1362
1363 scmSettingsBaseDelete(&Base);
1364 }
1365 return rc;
1366}
1367
1368
1369/**
1370 * Tries to correct RTDIRENTRY_UNKNOWN.
1371 *
1372 * @returns Corrected type.
1373 * @param pszPath The path to the object in question.
1374 */
1375static RTDIRENTRYTYPE scmFigureUnknownType(const char *pszPath)
1376{
1377 RTFSOBJINFO Info;
1378 int rc = RTPathQueryInfo(pszPath, &Info, RTFSOBJATTRADD_NOTHING);
1379 if (RT_FAILURE(rc))
1380 return RTDIRENTRYTYPE_UNKNOWN;
1381 if (RTFS_IS_DIRECTORY(Info.Attr.fMode))
1382 return RTDIRENTRYTYPE_DIRECTORY;
1383 if (RTFS_IS_FILE(Info.Attr.fMode))
1384 return RTDIRENTRYTYPE_FILE;
1385 return RTDIRENTRYTYPE_UNKNOWN;
1386}
1387
1388/**
1389 * Recurse into a sub-directory and process all the files and directories.
1390 *
1391 * @returns IPRT status code.
1392 * @param pszBuf Path buffer containing the directory path on
1393 * entry. This ends with a dot. This is passed
1394 * along when recursing in order to save stack space
1395 * and avoid needless copying.
1396 * @param cchDir Length of our path in pszbuf.
1397 * @param pEntry Directory entry buffer. This is also passed
1398 * along when recursing to save stack space.
1399 * @param pSettingsStack The settings stack (pointer to the top element).
1400 * @param iRecursion The recursion depth. This is used to restrict
1401 * the recursions.
1402 */
1403static int scmProcessDirTreeRecursion(char *pszBuf, size_t cchDir, PRTDIRENTRY pEntry,
1404 PSCMSETTINGS pSettingsStack, unsigned iRecursion)
1405{
1406 int rc;
1407 Assert(cchDir > 1 && pszBuf[cchDir - 1] == '.');
1408
1409 /*
1410 * Make sure we stop somewhere.
1411 */
1412 if (iRecursion > 128)
1413 {
1414 RTMsgError("recursion too deep: %d\n", iRecursion);
1415 return VINF_SUCCESS; /* ignore */
1416 }
1417
1418 /*
1419 * Check if it's excluded by --only-svn-dir.
1420 */
1421 if (pSettingsStack->Base.fOnlySvnDirs)
1422 {
1423 if (!ScmSvnIsDirInWorkingCopy(pszBuf))
1424 return VINF_SUCCESS;
1425 }
1426
1427 /*
1428 * Try open and read the directory.
1429 */
1430 PRTDIR pDir;
1431 rc = RTDirOpenFiltered(&pDir, pszBuf, RTDIRFILTER_NONE, 0);
1432 if (RT_FAILURE(rc))
1433 {
1434 RTMsgError("Failed to enumerate directory '%s': %Rrc", pszBuf, rc);
1435 return rc;
1436 }
1437 for (;;)
1438 {
1439 /* Read the next entry. */
1440 rc = RTDirRead(pDir, pEntry, NULL);
1441 if (RT_FAILURE(rc))
1442 {
1443 if (rc == VERR_NO_MORE_FILES)
1444 rc = VINF_SUCCESS;
1445 else
1446 RTMsgError("RTDirRead -> %Rrc\n", rc);
1447 break;
1448 }
1449
1450 /* Skip '.' and '..'. */
1451 if ( pEntry->szName[0] == '.'
1452 && ( pEntry->cbName == 1
1453 || ( pEntry->cbName == 2
1454 && pEntry->szName[1] == '.')))
1455 continue;
1456
1457 /* Enter it into the buffer so we've got a full name to work
1458 with when needed. */
1459 if (pEntry->cbName + cchDir >= RTPATH_MAX)
1460 {
1461 RTMsgError("Skipping too long entry: %s", pEntry->szName);
1462 continue;
1463 }
1464 memcpy(&pszBuf[cchDir - 1], pEntry->szName, pEntry->cbName + 1);
1465
1466 /* Figure the type. */
1467 RTDIRENTRYTYPE enmType = pEntry->enmType;
1468 if (enmType == RTDIRENTRYTYPE_UNKNOWN)
1469 enmType = scmFigureUnknownType(pszBuf);
1470
1471 /* Process the file or directory, skip the rest. */
1472 if (enmType == RTDIRENTRYTYPE_FILE)
1473 rc = scmProcessFile(pszBuf, pEntry->szName, pEntry->cbName, pSettingsStack);
1474 else if (enmType == RTDIRENTRYTYPE_DIRECTORY)
1475 {
1476 /* Append the dot for the benefit of the pattern matching. */
1477 if (pEntry->cbName + cchDir + 5 >= RTPATH_MAX)
1478 {
1479 RTMsgError("Skipping too deep dir entry: %s", pEntry->szName);
1480 continue;
1481 }
1482 memcpy(&pszBuf[cchDir - 1 + pEntry->cbName], "/.", sizeof("/."));
1483 size_t cchSubDir = cchDir - 1 + pEntry->cbName + sizeof("/.") - 1;
1484
1485 if ( !pSettingsStack->Base.pszFilterOutDirs
1486 || !*pSettingsStack->Base.pszFilterOutDirs
1487 || ( !RTStrSimplePatternMultiMatch(pSettingsStack->Base.pszFilterOutDirs, RTSTR_MAX,
1488 pEntry->szName, pEntry->cbName, NULL)
1489 && !RTStrSimplePatternMultiMatch(pSettingsStack->Base.pszFilterOutDirs, RTSTR_MAX,
1490 pszBuf, cchSubDir, NULL)
1491 )
1492 )
1493 {
1494 rc = scmSettingsStackPushDir(&pSettingsStack, pszBuf);
1495 if (RT_SUCCESS(rc))
1496 {
1497 rc = scmProcessDirTreeRecursion(pszBuf, cchSubDir, pEntry, pSettingsStack, iRecursion + 1);
1498 scmSettingsStackPopAndDestroy(&pSettingsStack);
1499 }
1500 }
1501 }
1502 if (RT_FAILURE(rc))
1503 break;
1504 }
1505 RTDirClose(pDir);
1506 return rc;
1507
1508}
1509
1510/**
1511 * Process a directory tree.
1512 *
1513 * @returns IPRT status code.
1514 * @param pszDir The directory to start with. This is pointer to
1515 * a RTPATH_MAX sized buffer.
1516 */
1517static int scmProcessDirTree(char *pszDir, PSCMSETTINGS pSettingsStack)
1518{
1519 /*
1520 * Setup the recursion.
1521 */
1522 int rc = RTPathAppend(pszDir, RTPATH_MAX, ".");
1523 if (RT_SUCCESS(rc))
1524 {
1525 RTDIRENTRY Entry;
1526 rc = scmProcessDirTreeRecursion(pszDir, strlen(pszDir), &Entry, pSettingsStack, 0);
1527 }
1528 else
1529 RTMsgError("RTPathAppend: %Rrc\n", rc);
1530 return rc;
1531}
1532
1533
1534/**
1535 * Processes a file or directory specified as an command line argument.
1536 *
1537 * @returns IPRT status code
1538 * @param pszSomething What we found in the command line arguments.
1539 * @param pSettingsStack The settings stack (pointer to the top element).
1540 */
1541static int scmProcessSomething(const char *pszSomething, PSCMSETTINGS pSettingsStack)
1542{
1543 char szBuf[RTPATH_MAX];
1544 int rc = RTPathAbs(pszSomething, szBuf, sizeof(szBuf));
1545 if (RT_SUCCESS(rc))
1546 {
1547 RTPathChangeToUnixSlashes(szBuf, false /*fForce*/);
1548
1549 PSCMSETTINGS pSettings;
1550 rc = scmSettingsCreateForPath(&pSettings, &pSettingsStack->Base, szBuf);
1551 if (RT_SUCCESS(rc))
1552 {
1553 scmSettingsStackPush(&pSettingsStack, pSettings);
1554
1555 if (RTFileExists(szBuf))
1556 {
1557 const char *pszBasename = RTPathFilename(szBuf);
1558 if (pszBasename)
1559 {
1560 size_t cchBasename = strlen(pszBasename);
1561 rc = scmProcessFile(szBuf, pszBasename, cchBasename, pSettingsStack);
1562 }
1563 else
1564 {
1565 RTMsgError("RTPathFilename: NULL\n");
1566 rc = VERR_IS_A_DIRECTORY;
1567 }
1568 }
1569 else
1570 rc = scmProcessDirTree(szBuf, pSettingsStack);
1571
1572 PSCMSETTINGS pPopped = scmSettingsStackPop(&pSettingsStack);
1573 Assert(pPopped == pSettings); RT_NOREF_PV(pPopped);
1574 scmSettingsDestroy(pSettings);
1575 }
1576 else
1577 RTMsgError("scmSettingsInitStack: %Rrc\n", rc);
1578 }
1579 else
1580 RTMsgError("RTPathAbs: %Rrc\n", rc);
1581 return rc;
1582}
1583
1584static void usage(PCRTGETOPTDEF paOpts, size_t cOpts)
1585{
1586 RTPrintf("VirtualBox Source Code Massager\n"
1587 "\n"
1588 "Usage: %s [options] <files & dirs>\n"
1589 "\n"
1590 "Options:\n", g_szProgName);
1591 for (size_t i = 0; i < cOpts; i++)
1592 {
1593 size_t cExtraAdvance = 0;
1594 if ((paOpts[i].fFlags & RTGETOPT_REQ_MASK) == RTGETOPT_REQ_NOTHING)
1595 {
1596 cExtraAdvance = i + 1 < cOpts
1597 && ( strstr(paOpts[i+1].pszLong, "-no-") != NULL
1598 || strstr(paOpts[i+1].pszLong, "-not-") != NULL
1599 || strstr(paOpts[i+1].pszLong, "-dont-") != NULL
1600 || (paOpts[i].iShort == 'q' && paOpts[i+1].iShort == 'v')
1601 || (paOpts[i].iShort == 'd' && paOpts[i+1].iShort == 'D')
1602 );
1603 if (cExtraAdvance)
1604 RTPrintf(" %s, %s\n", paOpts[i].pszLong, paOpts[i + 1].pszLong);
1605 else if (paOpts[i].iShort != SCMOPT_NO_UPDATE_LICENSE)
1606 RTPrintf(" %s\n", paOpts[i].pszLong);
1607 else
1608 {
1609 RTPrintf(" %s,\n"
1610 " %s,\n"
1611 " %s,\n"
1612 " %s,\n"
1613 " %s\n",
1614 paOpts[i].pszLong,
1615 paOpts[i + 1].pszLong,
1616 paOpts[i + 2].pszLong,
1617 paOpts[i + 3].pszLong,
1618 paOpts[i + 4].pszLong);
1619 cExtraAdvance = 4;
1620 }
1621 }
1622 else if ((paOpts[i].fFlags & RTGETOPT_REQ_MASK) == RTGETOPT_REQ_STRING)
1623 RTPrintf(" %s string\n", paOpts[i].pszLong);
1624 else
1625 RTPrintf(" %s value\n", paOpts[i].pszLong);
1626 switch (paOpts[i].iShort)
1627 {
1628 case 'd':
1629 case 'D': RTPrintf(" Default: --dry-run\n"); break;
1630 case 'f': RTPrintf(" Default: none\n"); break;
1631 case 'q':
1632 case 'v': RTPrintf(" Default: -vv\n"); break;
1633
1634 case SCMOPT_DIFF_IGNORE_EOL: RTPrintf(" Default: false\n"); break;
1635 case SCMOPT_DIFF_IGNORE_SPACE: RTPrintf(" Default: false\n"); break;
1636 case SCMOPT_DIFF_IGNORE_LEADING_SPACE: RTPrintf(" Default: false\n"); break;
1637 case SCMOPT_DIFF_IGNORE_TRAILING_SPACE: RTPrintf(" Default: false\n"); break;
1638 case SCMOPT_DIFF_SPECIAL_CHARS: RTPrintf(" Default: true\n"); break;
1639
1640 case SCMOPT_CONVERT_EOL: RTPrintf(" Default: %RTbool\n", g_Defaults.fConvertEol); break;
1641 case SCMOPT_CONVERT_TABS: RTPrintf(" Default: %RTbool\n", g_Defaults.fConvertTabs); break;
1642 case SCMOPT_FORCE_FINAL_EOL: RTPrintf(" Default: %RTbool\n", g_Defaults.fForceFinalEol); break;
1643 case SCMOPT_FORCE_TRAILING_LINE: RTPrintf(" Default: %RTbool\n", g_Defaults.fForceTrailingLine); break;
1644 case SCMOPT_STRIP_TRAILING_BLANKS: RTPrintf(" Default: %RTbool\n", g_Defaults.fStripTrailingBlanks); break;
1645 case SCMOPT_STRIP_TRAILING_LINES: RTPrintf(" Default: %RTbool\n", g_Defaults.fStripTrailingLines); break;
1646 case SCMOPT_FIX_FLOWER_BOX_MARKERS: RTPrintf(" Default: %RTbool\n", g_Defaults.fFixFlowerBoxMarkers); break;
1647 case SCMOPT_MIN_BLANK_LINES_BEFORE_FLOWER_BOX_MARKERS: RTPrintf(" Default: %u\n", g_Defaults.cMinBlankLinesBeforeFlowerBoxMakers); break;
1648
1649 case SCMOPT_FIX_TODOS:
1650 RTPrintf(" Fix @todo statements so doxygen sees them. Default: %RTbool\n", g_Defaults.fFixTodos);
1651 break;
1652 case SCMOPT_UPDATE_COPYRIGHT_YEAR:
1653 RTPrintf(" Update the copyright year. Default: %RTbool\n", g_Defaults.fUpdateCopyrightYear);
1654 break;
1655 case SCMOPT_NO_UPDATE_LICENSE:
1656 RTPrintf(" License selection. Default: --license-ose-gpl\n");
1657 break;
1658
1659 case SCMOPT_ONLY_SVN_DIRS: RTPrintf(" Default: %RTbool\n", g_Defaults.fOnlySvnDirs); break;
1660 case SCMOPT_ONLY_SVN_FILES: RTPrintf(" Default: %RTbool\n", g_Defaults.fOnlySvnFiles); break;
1661 case SCMOPT_SET_SVN_EOL: RTPrintf(" Default: %RTbool\n", g_Defaults.fSetSvnEol); break;
1662 case SCMOPT_SET_SVN_EXECUTABLE: RTPrintf(" Default: %RTbool\n", g_Defaults.fSetSvnExecutable); break;
1663 case SCMOPT_SET_SVN_KEYWORDS: RTPrintf(" Default: %RTbool\n", g_Defaults.fSetSvnKeywords); break;
1664 case SCMOPT_TAB_SIZE: RTPrintf(" Default: %u\n", g_Defaults.cchTab); break;
1665 case SCMOPT_WIDTH: RTPrintf(" Default: %u\n", g_Defaults.cchWidth); break;
1666 case SCMOPT_FILTER_OUT_DIRS: RTPrintf(" Default: %s\n", g_Defaults.pszFilterOutDirs); break;
1667 case SCMOPT_FILTER_FILES: RTPrintf(" Default: %s\n", g_Defaults.pszFilterFiles); break;
1668 case SCMOPT_FILTER_OUT_FILES: RTPrintf(" Default: %s\n", g_Defaults.pszFilterOutFiles); break;
1669 default: AssertMsgFailed(("i=%d %d %s\n", i, paOpts[i].iShort, paOpts[i].pszLong));
1670 }
1671 i += cExtraAdvance;
1672 }
1673
1674}
1675
1676int main(int argc, char **argv)
1677{
1678 int rc = RTR3InitExe(argc, &argv, 0);
1679 if (RT_FAILURE(rc))
1680 return 1;
1681
1682 /*
1683 * Init the current year.
1684 */
1685 RTTIMESPEC Now;
1686 RTTIME Time;
1687 RTTimeExplode(&Time, RTTimeNow(&Now));
1688 g_uYear = Time.i32Year;
1689
1690 /*
1691 * Init the settings.
1692 */
1693 PSCMSETTINGS pSettings;
1694 rc = scmSettingsCreate(&pSettings, &g_Defaults);
1695 if (RT_FAILURE(rc))
1696 {
1697 RTMsgError("scmSettingsCreate: %Rrc\n", rc);
1698 return 1;
1699 }
1700
1701 /*
1702 * Parse arguments and process input in order (because this is the only
1703 * thing that works at the moment).
1704 */
1705 static RTGETOPTDEF s_aOpts[14 + RT_ELEMENTS(g_aScmOpts)] =
1706 {
1707 { "--dry-run", 'd', RTGETOPT_REQ_NOTHING },
1708 { "--real-run", 'D', RTGETOPT_REQ_NOTHING },
1709 { "--file-filter", 'f', RTGETOPT_REQ_STRING },
1710 { "--quiet", 'q', RTGETOPT_REQ_NOTHING },
1711 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
1712 { "--diff-ignore-eol", SCMOPT_DIFF_IGNORE_EOL, RTGETOPT_REQ_NOTHING },
1713 { "--diff-no-ignore-eol", SCMOPT_DIFF_NO_IGNORE_EOL, RTGETOPT_REQ_NOTHING },
1714 { "--diff-ignore-space", SCMOPT_DIFF_IGNORE_SPACE, RTGETOPT_REQ_NOTHING },
1715 { "--diff-no-ignore-space", SCMOPT_DIFF_NO_IGNORE_SPACE, RTGETOPT_REQ_NOTHING },
1716 { "--diff-ignore-leading-space", SCMOPT_DIFF_IGNORE_LEADING_SPACE, RTGETOPT_REQ_NOTHING },
1717 { "--diff-no-ignore-leading-space", SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE, RTGETOPT_REQ_NOTHING },
1718 { "--diff-ignore-trailing-space", SCMOPT_DIFF_IGNORE_TRAILING_SPACE, RTGETOPT_REQ_NOTHING },
1719 { "--diff-no-ignore-trailing-space", SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE, RTGETOPT_REQ_NOTHING },
1720 { "--diff-special-chars", SCMOPT_DIFF_SPECIAL_CHARS, RTGETOPT_REQ_NOTHING },
1721 { "--diff-no-special-chars", SCMOPT_DIFF_NO_SPECIAL_CHARS, RTGETOPT_REQ_NOTHING },
1722 };
1723 memcpy(&s_aOpts[RT_ELEMENTS(s_aOpts) - RT_ELEMENTS(g_aScmOpts)], &g_aScmOpts[0], sizeof(g_aScmOpts));
1724
1725 RTGETOPTUNION ValueUnion;
1726 RTGETOPTSTATE GetOptState;
1727 rc = RTGetOptInit(&GetOptState, argc, argv, &s_aOpts[0], RT_ELEMENTS(s_aOpts), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
1728 AssertReleaseRCReturn(rc, 1);
1729 size_t cProcessed = 0;
1730
1731 while ((rc = RTGetOpt(&GetOptState, &ValueUnion)) != 0)
1732 {
1733 switch (rc)
1734 {
1735 case 'd':
1736 g_fDryRun = true;
1737 break;
1738 case 'D':
1739 g_fDryRun = false;
1740 break;
1741
1742 case 'f':
1743 g_pszFileFilter = ValueUnion.psz;
1744 break;
1745
1746 case 'h':
1747 usage(s_aOpts, RT_ELEMENTS(s_aOpts));
1748 return 1;
1749
1750 case 'q':
1751 g_iVerbosity = 0;
1752 break;
1753
1754 case 'v':
1755 g_iVerbosity++;
1756 break;
1757
1758 case 'V':
1759 {
1760 /* The following is assuming that svn does it's job here. */
1761 static const char s_szRev[] = "$Revision: 69166 $";
1762 const char *psz = RTStrStripL(strchr(s_szRev, ' '));
1763 RTPrintf("r%.*s\n", strchr(psz, ' ') - psz, psz);
1764 return 0;
1765 }
1766
1767 case SCMOPT_DIFF_IGNORE_EOL:
1768 g_fDiffIgnoreEol = true;
1769 break;
1770 case SCMOPT_DIFF_NO_IGNORE_EOL:
1771 g_fDiffIgnoreEol = false;
1772 break;
1773
1774 case SCMOPT_DIFF_IGNORE_SPACE:
1775 g_fDiffIgnoreTrailingWS = g_fDiffIgnoreLeadingWS = true;
1776 break;
1777 case SCMOPT_DIFF_NO_IGNORE_SPACE:
1778 g_fDiffIgnoreTrailingWS = g_fDiffIgnoreLeadingWS = false;
1779 break;
1780
1781 case SCMOPT_DIFF_IGNORE_LEADING_SPACE:
1782 g_fDiffIgnoreLeadingWS = true;
1783 break;
1784 case SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE:
1785 g_fDiffIgnoreLeadingWS = false;
1786 break;
1787
1788 case SCMOPT_DIFF_IGNORE_TRAILING_SPACE:
1789 g_fDiffIgnoreTrailingWS = true;
1790 break;
1791 case SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE:
1792 g_fDiffIgnoreTrailingWS = false;
1793 break;
1794
1795 case SCMOPT_DIFF_SPECIAL_CHARS:
1796 g_fDiffSpecialChars = true;
1797 break;
1798 case SCMOPT_DIFF_NO_SPECIAL_CHARS:
1799 g_fDiffSpecialChars = false;
1800 break;
1801
1802 case VINF_GETOPT_NOT_OPTION:
1803 {
1804 if (!g_fDryRun)
1805 {
1806 if (!cProcessed)
1807 {
1808 RTPrintf("%s: Warning! This program will make changes to your source files and\n"
1809 "%s: there is a slight risk that bugs or a full disk may cause\n"
1810 "%s: LOSS OF DATA. So, please make sure you have checked in\n"
1811 "%s: all your changes already. If you didn't, then don't blame\n"
1812 "%s: anyone for not warning you!\n"
1813 "%s:\n"
1814 "%s: Press any key to continue...\n",
1815 g_szProgName, g_szProgName, g_szProgName, g_szProgName, g_szProgName,
1816 g_szProgName, g_szProgName);
1817 RTStrmGetCh(g_pStdIn);
1818 }
1819 cProcessed++;
1820 }
1821 rc = scmProcessSomething(ValueUnion.psz, pSettings);
1822 if (RT_FAILURE(rc))
1823 return rc;
1824 break;
1825 }
1826
1827 default:
1828 {
1829 int rc2 = scmSettingsBaseHandleOpt(&pSettings->Base, rc, &ValueUnion);
1830 if (RT_SUCCESS(rc2))
1831 break;
1832 if (rc2 != VERR_GETOPT_UNKNOWN_OPTION)
1833 return 2;
1834 return RTGetOptPrintError(rc, &ValueUnion);
1835 }
1836 }
1837 }
1838
1839 scmSettingsDestroy(pSettings);
1840 return 0;
1841}
1842
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