VirtualBox

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

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

scm: License and copyright updating. [build fix]

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 68.3 KB
Line 
1/* $Id: scm.cpp 69171 2017-10-23 16:03:51Z 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 RTPrintf(pState
1095 ? "%s: info: "
1096 : "%s: info: ",
1097 g_szProgName);
1098 va_list va;
1099 va_start(va, pszFormat);
1100 RTPrintfV(pszFormat, va);
1101 va_end(va);
1102 }
1103}
1104
1105
1106/**
1107 * Prints the per file banner needed and the message level is high enough.
1108 *
1109 * @param pState The rewrite state.
1110 * @param iLevel The required verbosity level.
1111 */
1112void ScmVerboseBanner(PSCMRWSTATE pState, int iLevel)
1113{
1114 if (iLevel <= g_iVerbosity && !pState->fFirst)
1115 {
1116 RTPrintf("%s: info: --= Rewriting '%s' =--\n", g_szProgName, pState->pszFilename);
1117 pState->fFirst = true;
1118 }
1119}
1120
1121
1122/**
1123 * Prints an error message.
1124 *
1125 * @returns false
1126 * @param pState The rewrite state. Optional.
1127 * @param rc The error code.
1128 * @param pszFormat The message format string.
1129 * @param ... Format arguments.
1130 */
1131bool ScmError(PSCMRWSTATE pState, int rc, const char *pszFormat, ...)
1132{
1133 if (RT_SUCCESS(pState->rc))
1134 pState->rc = rc;
1135
1136 if (!pState->fFirst)
1137 {
1138 RTPrintf("%s: info: --= Rewriting '%s' =--\n", g_szProgName, pState->pszFilename);
1139 pState->fFirst = true;
1140 }
1141 va_list va;
1142 va_start(va, pszFormat);
1143 RTPrintf("%s: error: %s: %N", g_szProgName, pState->pszFilename, pszFormat, &va);
1144 va_end(va);
1145
1146 return false;
1147}
1148
1149
1150/* -=-=-=-=-=- file and directory processing -=-=-=-=-=- */
1151
1152
1153/**
1154 * Processes a file.
1155 *
1156 * @returns IPRT status code.
1157 * @param pState The rewriter state.
1158 * @param pszFilename The file name.
1159 * @param pszBasename The base name (pointer within @a pszFilename).
1160 * @param cchBasename The length of the base name. (For passing to
1161 * RTStrSimplePatternMultiMatch.)
1162 * @param pBaseSettings The base settings to use. It's OK to modify
1163 * these.
1164 */
1165static int scmProcessFileInner(PSCMRWSTATE pState, const char *pszFilename, const char *pszBasename, size_t cchBasename,
1166 PSCMSETTINGSBASE pBaseSettings)
1167{
1168 /*
1169 * Do the file level filtering.
1170 */
1171 if ( pBaseSettings->pszFilterFiles
1172 && *pBaseSettings->pszFilterFiles
1173 && !RTStrSimplePatternMultiMatch(pBaseSettings->pszFilterFiles, RTSTR_MAX, pszBasename, cchBasename, NULL))
1174 {
1175 ScmVerbose(NULL, 5, "skipping '%s': file filter mismatch\n", pszFilename);
1176 return VINF_SUCCESS;
1177 }
1178 if ( pBaseSettings->pszFilterOutFiles
1179 && *pBaseSettings->pszFilterOutFiles
1180 && ( RTStrSimplePatternMultiMatch(pBaseSettings->pszFilterOutFiles, RTSTR_MAX, pszBasename, cchBasename, NULL)
1181 || RTStrSimplePatternMultiMatch(pBaseSettings->pszFilterOutFiles, RTSTR_MAX, pszFilename, RTSTR_MAX, NULL)) )
1182 {
1183 ScmVerbose(NULL, 5, "skipping '%s': filterd out\n", pszFilename);
1184 return VINF_SUCCESS;
1185 }
1186 if ( pBaseSettings->fOnlySvnFiles
1187 && !ScmSvnIsInWorkingCopy(pState))
1188 {
1189 ScmVerbose(NULL, 5, "skipping '%s': not in SVN WC\n", pszFilename);
1190 return VINF_SUCCESS;
1191 }
1192
1193 /*
1194 * Try find a matching rewrite config for this filename.
1195 */
1196 PCSCMCFGENTRY pCfg = NULL;
1197 for (size_t iCfg = 0; iCfg < RT_ELEMENTS(g_aConfigs); iCfg++)
1198 if (RTStrSimplePatternMultiMatch(g_aConfigs[iCfg].pszFilePattern, RTSTR_MAX, pszBasename, cchBasename, NULL))
1199 {
1200 pCfg = &g_aConfigs[iCfg];
1201 break;
1202 }
1203 if (!pCfg)
1204 {
1205 ScmVerbose(NULL, 4, "skipping '%s': no rewriters configured\n", pszFilename);
1206 return VINF_SUCCESS;
1207 }
1208 ScmVerbose(pState, 4, "matched \"%s\"\n", pCfg->pszFilePattern);
1209
1210 /*
1211 * Create an input stream from the file and check that it's text.
1212 */
1213 SCMSTREAM Stream1;
1214 int rc = ScmStreamInitForReading(&Stream1, pszFilename);
1215 if (RT_FAILURE(rc))
1216 {
1217 RTMsgError("Failed to read '%s': %Rrc\n", pszFilename, rc);
1218 return rc;
1219 }
1220 if (ScmStreamIsText(&Stream1))
1221 {
1222 ScmVerboseBanner(pState, 3);
1223
1224 /*
1225 * Gather SCM and editor settings from the stream.
1226 */
1227 rc = scmSettingsBaseLoadFromDocument(pBaseSettings, &Stream1);
1228 if (RT_SUCCESS(rc))
1229 {
1230 ScmStreamRewindForReading(&Stream1);
1231
1232 /*
1233 * Create two more streams for output and push the text thru all the
1234 * rewriters, switching the two streams around when something is
1235 * actually rewritten. Stream1 remains unchanged.
1236 */
1237 SCMSTREAM Stream2;
1238 rc = ScmStreamInitForWriting(&Stream2, &Stream1);
1239 if (RT_SUCCESS(rc))
1240 {
1241 SCMSTREAM Stream3;
1242 rc = ScmStreamInitForWriting(&Stream3, &Stream1);
1243 if (RT_SUCCESS(rc))
1244 {
1245 bool fModified = false;
1246 PSCMSTREAM pIn = &Stream1;
1247 PSCMSTREAM pOut = &Stream2;
1248 for (size_t iRw = 0; iRw < pCfg->cRewriters; iRw++)
1249 {
1250 pState->rc = VINF_SUCCESS;
1251 bool fRc = pCfg->papfnRewriter[iRw](pState, pIn, pOut, pBaseSettings);
1252 if (RT_FAILURE(pState->rc))
1253 break;
1254 if (fRc)
1255 {
1256 PSCMSTREAM pTmp = pOut;
1257 pOut = pIn == &Stream1 ? &Stream3 : pIn;
1258 pIn = pTmp;
1259 fModified = true;
1260 }
1261
1262 ScmStreamRewindForReading(pIn);
1263 ScmStreamRewindForWriting(pOut);
1264 }
1265
1266 rc = pState->rc;
1267 if (RT_SUCCESS(rc))
1268 {
1269 rc = ScmStreamGetStatus(&Stream1);
1270 if (RT_SUCCESS(rc))
1271 rc = ScmStreamGetStatus(&Stream2);
1272 if (RT_SUCCESS(rc))
1273 rc = ScmStreamGetStatus(&Stream3);
1274 if (RT_SUCCESS(rc))
1275 {
1276 /*
1277 * If rewritten, write it back to disk.
1278 */
1279 if (fModified)
1280 {
1281 if (!g_fDryRun)
1282 {
1283 ScmVerbose(pState, 1, "writing modified file to \"%s%s\"\n", pszFilename, g_pszChangedSuff);
1284 rc = ScmStreamWriteToFile(pIn, "%s%s", pszFilename, g_pszChangedSuff);
1285 if (RT_FAILURE(rc))
1286 RTMsgError("Error writing '%s%s': %Rrc\n", pszFilename, g_pszChangedSuff, rc);
1287 }
1288 else
1289 {
1290 ScmVerboseBanner(pState, 1);
1291 ScmDiffStreams(pszFilename, &Stream1, pIn, g_fDiffIgnoreEol,
1292 g_fDiffIgnoreLeadingWS, g_fDiffIgnoreTrailingWS, g_fDiffSpecialChars,
1293 pBaseSettings->cchTab, g_pStdOut);
1294 ScmVerbose(pState, 2, "would have modified the file \"%s%s\"\n",
1295 pszFilename, g_pszChangedSuff);
1296 }
1297 }
1298
1299 /*
1300 * If pending SVN property changes, apply them.
1301 */
1302 if (pState->cSvnPropChanges && RT_SUCCESS(rc))
1303 {
1304 if (!g_fDryRun)
1305 {
1306 rc = ScmSvnApplyChanges(pState);
1307 if (RT_FAILURE(rc))
1308 RTMsgError("%s: failed to apply SVN property changes (%Rrc)\n", pszFilename, rc);
1309 }
1310 else
1311 ScmSvnDisplayChanges(pState);
1312 }
1313
1314 if (!fModified && !pState->cSvnPropChanges)
1315 ScmVerbose(pState, 3, "%s: no change\n", pszFilename);
1316 }
1317 else
1318 RTMsgError("%s: stream error %Rrc\n", pszFilename, rc);
1319 }
1320 ScmStreamDelete(&Stream3);
1321 }
1322 else
1323 RTMsgError("Failed to init stream for writing: %Rrc\n", rc);
1324 ScmStreamDelete(&Stream2);
1325 }
1326 else
1327 RTMsgError("Failed to init stream for writing: %Rrc\n", rc);
1328 }
1329 else
1330 RTMsgError("scmSettingsBaseLoadFromDocument: %Rrc\n", rc);
1331 }
1332 else
1333 ScmVerbose(pState, 4, "not text file: \"%s\"\n", pszFilename);
1334 ScmStreamDelete(&Stream1);
1335
1336 return rc;
1337}
1338
1339/**
1340 * Processes a file.
1341 *
1342 * This is just a wrapper for scmProcessFileInner for avoid wasting stack in the
1343 * directory recursion method.
1344 *
1345 * @returns IPRT status code.
1346 * @param pszFilename The file name.
1347 * @param pszBasename The base name (pointer within @a pszFilename).
1348 * @param cchBasename The length of the base name. (For passing to
1349 * RTStrSimplePatternMultiMatch.)
1350 * @param pSettingsStack The settings stack (pointer to the top element).
1351 */
1352static int scmProcessFile(const char *pszFilename, const char *pszBasename, size_t cchBasename,
1353 PSCMSETTINGS pSettingsStack)
1354{
1355 SCMSETTINGSBASE Base;
1356 int rc = scmSettingsStackMakeFileBase(pSettingsStack, pszFilename, pszBasename, cchBasename, &Base);
1357 if (RT_SUCCESS(rc))
1358 {
1359 SCMRWSTATE State;
1360 State.fFirst = false;
1361 State.pszFilename = pszFilename;
1362 State.cSvnPropChanges = 0;
1363 State.paSvnPropChanges = NULL;
1364 State.rc = VINF_SUCCESS;
1365
1366 rc = scmProcessFileInner(&State, pszFilename, pszBasename, cchBasename, &Base);
1367
1368 size_t i = State.cSvnPropChanges;
1369 while (i-- > 0)
1370 {
1371 RTStrFree(State.paSvnPropChanges[i].pszName);
1372 RTStrFree(State.paSvnPropChanges[i].pszValue);
1373 }
1374 RTMemFree(State.paSvnPropChanges);
1375
1376 scmSettingsBaseDelete(&Base);
1377 }
1378 return rc;
1379}
1380
1381
1382/**
1383 * Tries to correct RTDIRENTRY_UNKNOWN.
1384 *
1385 * @returns Corrected type.
1386 * @param pszPath The path to the object in question.
1387 */
1388static RTDIRENTRYTYPE scmFigureUnknownType(const char *pszPath)
1389{
1390 RTFSOBJINFO Info;
1391 int rc = RTPathQueryInfo(pszPath, &Info, RTFSOBJATTRADD_NOTHING);
1392 if (RT_FAILURE(rc))
1393 return RTDIRENTRYTYPE_UNKNOWN;
1394 if (RTFS_IS_DIRECTORY(Info.Attr.fMode))
1395 return RTDIRENTRYTYPE_DIRECTORY;
1396 if (RTFS_IS_FILE(Info.Attr.fMode))
1397 return RTDIRENTRYTYPE_FILE;
1398 return RTDIRENTRYTYPE_UNKNOWN;
1399}
1400
1401/**
1402 * Recurse into a sub-directory and process all the files and directories.
1403 *
1404 * @returns IPRT status code.
1405 * @param pszBuf Path buffer containing the directory path on
1406 * entry. This ends with a dot. This is passed
1407 * along when recursing in order to save stack space
1408 * and avoid needless copying.
1409 * @param cchDir Length of our path in pszbuf.
1410 * @param pEntry Directory entry buffer. This is also passed
1411 * along when recursing to save stack space.
1412 * @param pSettingsStack The settings stack (pointer to the top element).
1413 * @param iRecursion The recursion depth. This is used to restrict
1414 * the recursions.
1415 */
1416static int scmProcessDirTreeRecursion(char *pszBuf, size_t cchDir, PRTDIRENTRY pEntry,
1417 PSCMSETTINGS pSettingsStack, unsigned iRecursion)
1418{
1419 int rc;
1420 Assert(cchDir > 1 && pszBuf[cchDir - 1] == '.');
1421
1422 /*
1423 * Make sure we stop somewhere.
1424 */
1425 if (iRecursion > 128)
1426 {
1427 RTMsgError("recursion too deep: %d\n", iRecursion);
1428 return VINF_SUCCESS; /* ignore */
1429 }
1430
1431 /*
1432 * Check if it's excluded by --only-svn-dir.
1433 */
1434 if (pSettingsStack->Base.fOnlySvnDirs)
1435 {
1436 if (!ScmSvnIsDirInWorkingCopy(pszBuf))
1437 return VINF_SUCCESS;
1438 }
1439
1440 /*
1441 * Try open and read the directory.
1442 */
1443 PRTDIR pDir;
1444 rc = RTDirOpenFiltered(&pDir, pszBuf, RTDIRFILTER_NONE, 0);
1445 if (RT_FAILURE(rc))
1446 {
1447 RTMsgError("Failed to enumerate directory '%s': %Rrc", pszBuf, rc);
1448 return rc;
1449 }
1450 for (;;)
1451 {
1452 /* Read the next entry. */
1453 rc = RTDirRead(pDir, pEntry, NULL);
1454 if (RT_FAILURE(rc))
1455 {
1456 if (rc == VERR_NO_MORE_FILES)
1457 rc = VINF_SUCCESS;
1458 else
1459 RTMsgError("RTDirRead -> %Rrc\n", rc);
1460 break;
1461 }
1462
1463 /* Skip '.' and '..'. */
1464 if ( pEntry->szName[0] == '.'
1465 && ( pEntry->cbName == 1
1466 || ( pEntry->cbName == 2
1467 && pEntry->szName[1] == '.')))
1468 continue;
1469
1470 /* Enter it into the buffer so we've got a full name to work
1471 with when needed. */
1472 if (pEntry->cbName + cchDir >= RTPATH_MAX)
1473 {
1474 RTMsgError("Skipping too long entry: %s", pEntry->szName);
1475 continue;
1476 }
1477 memcpy(&pszBuf[cchDir - 1], pEntry->szName, pEntry->cbName + 1);
1478
1479 /* Figure the type. */
1480 RTDIRENTRYTYPE enmType = pEntry->enmType;
1481 if (enmType == RTDIRENTRYTYPE_UNKNOWN)
1482 enmType = scmFigureUnknownType(pszBuf);
1483
1484 /* Process the file or directory, skip the rest. */
1485 if (enmType == RTDIRENTRYTYPE_FILE)
1486 rc = scmProcessFile(pszBuf, pEntry->szName, pEntry->cbName, pSettingsStack);
1487 else if (enmType == RTDIRENTRYTYPE_DIRECTORY)
1488 {
1489 /* Append the dot for the benefit of the pattern matching. */
1490 if (pEntry->cbName + cchDir + 5 >= RTPATH_MAX)
1491 {
1492 RTMsgError("Skipping too deep dir entry: %s", pEntry->szName);
1493 continue;
1494 }
1495 memcpy(&pszBuf[cchDir - 1 + pEntry->cbName], "/.", sizeof("/."));
1496 size_t cchSubDir = cchDir - 1 + pEntry->cbName + sizeof("/.") - 1;
1497
1498 if ( !pSettingsStack->Base.pszFilterOutDirs
1499 || !*pSettingsStack->Base.pszFilterOutDirs
1500 || ( !RTStrSimplePatternMultiMatch(pSettingsStack->Base.pszFilterOutDirs, RTSTR_MAX,
1501 pEntry->szName, pEntry->cbName, NULL)
1502 && !RTStrSimplePatternMultiMatch(pSettingsStack->Base.pszFilterOutDirs, RTSTR_MAX,
1503 pszBuf, cchSubDir, NULL)
1504 )
1505 )
1506 {
1507 rc = scmSettingsStackPushDir(&pSettingsStack, pszBuf);
1508 if (RT_SUCCESS(rc))
1509 {
1510 rc = scmProcessDirTreeRecursion(pszBuf, cchSubDir, pEntry, pSettingsStack, iRecursion + 1);
1511 scmSettingsStackPopAndDestroy(&pSettingsStack);
1512 }
1513 }
1514 }
1515 if (RT_FAILURE(rc))
1516 break;
1517 }
1518 RTDirClose(pDir);
1519 return rc;
1520
1521}
1522
1523/**
1524 * Process a directory tree.
1525 *
1526 * @returns IPRT status code.
1527 * @param pszDir The directory to start with. This is pointer to
1528 * a RTPATH_MAX sized buffer.
1529 */
1530static int scmProcessDirTree(char *pszDir, PSCMSETTINGS pSettingsStack)
1531{
1532 /*
1533 * Setup the recursion.
1534 */
1535 int rc = RTPathAppend(pszDir, RTPATH_MAX, ".");
1536 if (RT_SUCCESS(rc))
1537 {
1538 RTDIRENTRY Entry;
1539 rc = scmProcessDirTreeRecursion(pszDir, strlen(pszDir), &Entry, pSettingsStack, 0);
1540 }
1541 else
1542 RTMsgError("RTPathAppend: %Rrc\n", rc);
1543 return rc;
1544}
1545
1546
1547/**
1548 * Processes a file or directory specified as an command line argument.
1549 *
1550 * @returns IPRT status code
1551 * @param pszSomething What we found in the command line arguments.
1552 * @param pSettingsStack The settings stack (pointer to the top element).
1553 */
1554static int scmProcessSomething(const char *pszSomething, PSCMSETTINGS pSettingsStack)
1555{
1556 char szBuf[RTPATH_MAX];
1557 int rc = RTPathAbs(pszSomething, szBuf, sizeof(szBuf));
1558 if (RT_SUCCESS(rc))
1559 {
1560 RTPathChangeToUnixSlashes(szBuf, false /*fForce*/);
1561
1562 PSCMSETTINGS pSettings;
1563 rc = scmSettingsCreateForPath(&pSettings, &pSettingsStack->Base, szBuf);
1564 if (RT_SUCCESS(rc))
1565 {
1566 scmSettingsStackPush(&pSettingsStack, pSettings);
1567
1568 if (RTFileExists(szBuf))
1569 {
1570 const char *pszBasename = RTPathFilename(szBuf);
1571 if (pszBasename)
1572 {
1573 size_t cchBasename = strlen(pszBasename);
1574 rc = scmProcessFile(szBuf, pszBasename, cchBasename, pSettingsStack);
1575 }
1576 else
1577 {
1578 RTMsgError("RTPathFilename: NULL\n");
1579 rc = VERR_IS_A_DIRECTORY;
1580 }
1581 }
1582 else
1583 rc = scmProcessDirTree(szBuf, pSettingsStack);
1584
1585 PSCMSETTINGS pPopped = scmSettingsStackPop(&pSettingsStack);
1586 Assert(pPopped == pSettings); RT_NOREF_PV(pPopped);
1587 scmSettingsDestroy(pSettings);
1588 }
1589 else
1590 RTMsgError("scmSettingsInitStack: %Rrc\n", rc);
1591 }
1592 else
1593 RTMsgError("RTPathAbs: %Rrc\n", rc);
1594 return rc;
1595}
1596
1597static void usage(PCRTGETOPTDEF paOpts, size_t cOpts)
1598{
1599 RTPrintf("VirtualBox Source Code Massager\n"
1600 "\n"
1601 "Usage: %s [options] <files & dirs>\n"
1602 "\n"
1603 "Options:\n", g_szProgName);
1604 for (size_t i = 0; i < cOpts; i++)
1605 {
1606 size_t cExtraAdvance = 0;
1607 if ((paOpts[i].fFlags & RTGETOPT_REQ_MASK) == RTGETOPT_REQ_NOTHING)
1608 {
1609 cExtraAdvance = i + 1 < cOpts
1610 && ( strstr(paOpts[i+1].pszLong, "-no-") != NULL
1611 || strstr(paOpts[i+1].pszLong, "-not-") != NULL
1612 || strstr(paOpts[i+1].pszLong, "-dont-") != NULL
1613 || (paOpts[i].iShort == 'q' && paOpts[i+1].iShort == 'v')
1614 || (paOpts[i].iShort == 'd' && paOpts[i+1].iShort == 'D')
1615 );
1616 if (cExtraAdvance)
1617 RTPrintf(" %s, %s\n", paOpts[i].pszLong, paOpts[i + 1].pszLong);
1618 else if (paOpts[i].iShort != SCMOPT_NO_UPDATE_LICENSE)
1619 RTPrintf(" %s\n", paOpts[i].pszLong);
1620 else
1621 {
1622 RTPrintf(" %s,\n"
1623 " %s,\n"
1624 " %s,\n"
1625 " %s,\n"
1626 " %s\n",
1627 paOpts[i].pszLong,
1628 paOpts[i + 1].pszLong,
1629 paOpts[i + 2].pszLong,
1630 paOpts[i + 3].pszLong,
1631 paOpts[i + 4].pszLong);
1632 cExtraAdvance = 4;
1633 }
1634 }
1635 else if ((paOpts[i].fFlags & RTGETOPT_REQ_MASK) == RTGETOPT_REQ_STRING)
1636 RTPrintf(" %s string\n", paOpts[i].pszLong);
1637 else
1638 RTPrintf(" %s value\n", paOpts[i].pszLong);
1639 switch (paOpts[i].iShort)
1640 {
1641 case 'd':
1642 case 'D': RTPrintf(" Default: --dry-run\n"); break;
1643 case 'f': RTPrintf(" Default: none\n"); break;
1644 case 'q':
1645 case 'v': RTPrintf(" Default: -vv\n"); break;
1646
1647 case SCMOPT_DIFF_IGNORE_EOL: RTPrintf(" Default: false\n"); break;
1648 case SCMOPT_DIFF_IGNORE_SPACE: RTPrintf(" Default: false\n"); break;
1649 case SCMOPT_DIFF_IGNORE_LEADING_SPACE: RTPrintf(" Default: false\n"); break;
1650 case SCMOPT_DIFF_IGNORE_TRAILING_SPACE: RTPrintf(" Default: false\n"); break;
1651 case SCMOPT_DIFF_SPECIAL_CHARS: RTPrintf(" Default: true\n"); break;
1652
1653 case SCMOPT_CONVERT_EOL: RTPrintf(" Default: %RTbool\n", g_Defaults.fConvertEol); break;
1654 case SCMOPT_CONVERT_TABS: RTPrintf(" Default: %RTbool\n", g_Defaults.fConvertTabs); break;
1655 case SCMOPT_FORCE_FINAL_EOL: RTPrintf(" Default: %RTbool\n", g_Defaults.fForceFinalEol); break;
1656 case SCMOPT_FORCE_TRAILING_LINE: RTPrintf(" Default: %RTbool\n", g_Defaults.fForceTrailingLine); break;
1657 case SCMOPT_STRIP_TRAILING_BLANKS: RTPrintf(" Default: %RTbool\n", g_Defaults.fStripTrailingBlanks); break;
1658 case SCMOPT_STRIP_TRAILING_LINES: RTPrintf(" Default: %RTbool\n", g_Defaults.fStripTrailingLines); break;
1659 case SCMOPT_FIX_FLOWER_BOX_MARKERS: RTPrintf(" Default: %RTbool\n", g_Defaults.fFixFlowerBoxMarkers); break;
1660 case SCMOPT_MIN_BLANK_LINES_BEFORE_FLOWER_BOX_MARKERS: RTPrintf(" Default: %u\n", g_Defaults.cMinBlankLinesBeforeFlowerBoxMakers); break;
1661
1662 case SCMOPT_FIX_TODOS:
1663 RTPrintf(" Fix @todo statements so doxygen sees them. Default: %RTbool\n", g_Defaults.fFixTodos);
1664 break;
1665 case SCMOPT_UPDATE_COPYRIGHT_YEAR:
1666 RTPrintf(" Update the copyright year. Default: %RTbool\n", g_Defaults.fUpdateCopyrightYear);
1667 break;
1668 case SCMOPT_NO_UPDATE_LICENSE:
1669 RTPrintf(" License selection. Default: --license-ose-gpl\n");
1670 break;
1671
1672 case SCMOPT_ONLY_SVN_DIRS: RTPrintf(" Default: %RTbool\n", g_Defaults.fOnlySvnDirs); break;
1673 case SCMOPT_ONLY_SVN_FILES: RTPrintf(" Default: %RTbool\n", g_Defaults.fOnlySvnFiles); break;
1674 case SCMOPT_SET_SVN_EOL: RTPrintf(" Default: %RTbool\n", g_Defaults.fSetSvnEol); break;
1675 case SCMOPT_SET_SVN_EXECUTABLE: RTPrintf(" Default: %RTbool\n", g_Defaults.fSetSvnExecutable); break;
1676 case SCMOPT_SET_SVN_KEYWORDS: RTPrintf(" Default: %RTbool\n", g_Defaults.fSetSvnKeywords); break;
1677 case SCMOPT_TAB_SIZE: RTPrintf(" Default: %u\n", g_Defaults.cchTab); break;
1678 case SCMOPT_WIDTH: RTPrintf(" Default: %u\n", g_Defaults.cchWidth); break;
1679 case SCMOPT_FILTER_OUT_DIRS: RTPrintf(" Default: %s\n", g_Defaults.pszFilterOutDirs); break;
1680 case SCMOPT_FILTER_FILES: RTPrintf(" Default: %s\n", g_Defaults.pszFilterFiles); break;
1681 case SCMOPT_FILTER_OUT_FILES: RTPrintf(" Default: %s\n", g_Defaults.pszFilterOutFiles); break;
1682 default: AssertMsgFailed(("i=%d %d %s\n", i, paOpts[i].iShort, paOpts[i].pszLong));
1683 }
1684 i += cExtraAdvance;
1685 }
1686
1687}
1688
1689int main(int argc, char **argv)
1690{
1691 int rc = RTR3InitExe(argc, &argv, 0);
1692 if (RT_FAILURE(rc))
1693 return 1;
1694
1695 /*
1696 * Init the current year.
1697 */
1698 RTTIMESPEC Now;
1699 RTTIME Time;
1700 RTTimeExplode(&Time, RTTimeNow(&Now));
1701 g_uYear = Time.i32Year;
1702
1703 /*
1704 * Init the settings.
1705 */
1706 PSCMSETTINGS pSettings;
1707 rc = scmSettingsCreate(&pSettings, &g_Defaults);
1708 if (RT_FAILURE(rc))
1709 {
1710 RTMsgError("scmSettingsCreate: %Rrc\n", rc);
1711 return 1;
1712 }
1713
1714 /*
1715 * Parse arguments and process input in order (because this is the only
1716 * thing that works at the moment).
1717 */
1718 static RTGETOPTDEF s_aOpts[14 + RT_ELEMENTS(g_aScmOpts)] =
1719 {
1720 { "--dry-run", 'd', RTGETOPT_REQ_NOTHING },
1721 { "--real-run", 'D', RTGETOPT_REQ_NOTHING },
1722 { "--file-filter", 'f', RTGETOPT_REQ_STRING },
1723 { "--quiet", 'q', RTGETOPT_REQ_NOTHING },
1724 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
1725 { "--diff-ignore-eol", SCMOPT_DIFF_IGNORE_EOL, RTGETOPT_REQ_NOTHING },
1726 { "--diff-no-ignore-eol", SCMOPT_DIFF_NO_IGNORE_EOL, RTGETOPT_REQ_NOTHING },
1727 { "--diff-ignore-space", SCMOPT_DIFF_IGNORE_SPACE, RTGETOPT_REQ_NOTHING },
1728 { "--diff-no-ignore-space", SCMOPT_DIFF_NO_IGNORE_SPACE, RTGETOPT_REQ_NOTHING },
1729 { "--diff-ignore-leading-space", SCMOPT_DIFF_IGNORE_LEADING_SPACE, RTGETOPT_REQ_NOTHING },
1730 { "--diff-no-ignore-leading-space", SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE, RTGETOPT_REQ_NOTHING },
1731 { "--diff-ignore-trailing-space", SCMOPT_DIFF_IGNORE_TRAILING_SPACE, RTGETOPT_REQ_NOTHING },
1732 { "--diff-no-ignore-trailing-space", SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE, RTGETOPT_REQ_NOTHING },
1733 { "--diff-special-chars", SCMOPT_DIFF_SPECIAL_CHARS, RTGETOPT_REQ_NOTHING },
1734 { "--diff-no-special-chars", SCMOPT_DIFF_NO_SPECIAL_CHARS, RTGETOPT_REQ_NOTHING },
1735 };
1736 memcpy(&s_aOpts[RT_ELEMENTS(s_aOpts) - RT_ELEMENTS(g_aScmOpts)], &g_aScmOpts[0], sizeof(g_aScmOpts));
1737
1738 RTGETOPTUNION ValueUnion;
1739 RTGETOPTSTATE GetOptState;
1740 rc = RTGetOptInit(&GetOptState, argc, argv, &s_aOpts[0], RT_ELEMENTS(s_aOpts), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
1741 AssertReleaseRCReturn(rc, 1);
1742 size_t cProcessed = 0;
1743
1744 while ((rc = RTGetOpt(&GetOptState, &ValueUnion)) != 0)
1745 {
1746 switch (rc)
1747 {
1748 case 'd':
1749 g_fDryRun = true;
1750 break;
1751 case 'D':
1752 g_fDryRun = false;
1753 break;
1754
1755 case 'f':
1756 g_pszFileFilter = ValueUnion.psz;
1757 break;
1758
1759 case 'h':
1760 usage(s_aOpts, RT_ELEMENTS(s_aOpts));
1761 return 1;
1762
1763 case 'q':
1764 g_iVerbosity = 0;
1765 break;
1766
1767 case 'v':
1768 g_iVerbosity++;
1769 break;
1770
1771 case 'V':
1772 {
1773 /* The following is assuming that svn does it's job here. */
1774 static const char s_szRev[] = "$Revision: 69171 $";
1775 const char *psz = RTStrStripL(strchr(s_szRev, ' '));
1776 RTPrintf("r%.*s\n", strchr(psz, ' ') - psz, psz);
1777 return 0;
1778 }
1779
1780 case SCMOPT_DIFF_IGNORE_EOL:
1781 g_fDiffIgnoreEol = true;
1782 break;
1783 case SCMOPT_DIFF_NO_IGNORE_EOL:
1784 g_fDiffIgnoreEol = false;
1785 break;
1786
1787 case SCMOPT_DIFF_IGNORE_SPACE:
1788 g_fDiffIgnoreTrailingWS = g_fDiffIgnoreLeadingWS = true;
1789 break;
1790 case SCMOPT_DIFF_NO_IGNORE_SPACE:
1791 g_fDiffIgnoreTrailingWS = g_fDiffIgnoreLeadingWS = false;
1792 break;
1793
1794 case SCMOPT_DIFF_IGNORE_LEADING_SPACE:
1795 g_fDiffIgnoreLeadingWS = true;
1796 break;
1797 case SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE:
1798 g_fDiffIgnoreLeadingWS = false;
1799 break;
1800
1801 case SCMOPT_DIFF_IGNORE_TRAILING_SPACE:
1802 g_fDiffIgnoreTrailingWS = true;
1803 break;
1804 case SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE:
1805 g_fDiffIgnoreTrailingWS = false;
1806 break;
1807
1808 case SCMOPT_DIFF_SPECIAL_CHARS:
1809 g_fDiffSpecialChars = true;
1810 break;
1811 case SCMOPT_DIFF_NO_SPECIAL_CHARS:
1812 g_fDiffSpecialChars = false;
1813 break;
1814
1815 case VINF_GETOPT_NOT_OPTION:
1816 {
1817 if (!g_fDryRun)
1818 {
1819 if (!cProcessed)
1820 {
1821 RTPrintf("%s: Warning! This program will make changes to your source files and\n"
1822 "%s: there is a slight risk that bugs or a full disk may cause\n"
1823 "%s: LOSS OF DATA. So, please make sure you have checked in\n"
1824 "%s: all your changes already. If you didn't, then don't blame\n"
1825 "%s: anyone for not warning you!\n"
1826 "%s:\n"
1827 "%s: Press any key to continue...\n",
1828 g_szProgName, g_szProgName, g_szProgName, g_szProgName, g_szProgName,
1829 g_szProgName, g_szProgName);
1830 RTStrmGetCh(g_pStdIn);
1831 }
1832 cProcessed++;
1833 }
1834 rc = scmProcessSomething(ValueUnion.psz, pSettings);
1835 if (RT_FAILURE(rc))
1836 return rc;
1837 break;
1838 }
1839
1840 default:
1841 {
1842 int rc2 = scmSettingsBaseHandleOpt(&pSettings->Base, rc, &ValueUnion);
1843 if (RT_SUCCESS(rc2))
1844 break;
1845 if (rc2 != VERR_GETOPT_UNKNOWN_OPTION)
1846 return 2;
1847 return RTGetOptPrintError(rc, &ValueUnion);
1848 }
1849 }
1850 }
1851
1852 scmSettingsDestroy(pSettings);
1853 return 0;
1854}
1855
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