VirtualBox

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

Last change on this file since 26477 was 26477, checked in by vboxsync, 15 years ago

scm: More flexible settings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 102.8 KB
Line 
1/* $Id: scm.cpp 26477 2010-02-13 03:40:00Z vboxsync $ */
2/** @file
3 * IPRT Testcase / Tool - Source Code Massager.
4 */
5
6/*
7 * Copyright (C) 2010 Sun Microsystems, Inc.
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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include <iprt/assert.h>
35#include <iprt/ctype.h>
36#include <iprt/dir.h>
37#include <iprt/file.h>
38#include <iprt/err.h>
39#include <iprt/getopt.h>
40#include <iprt/initterm.h>
41#include <iprt/mem.h>
42#include <iprt/message.h>
43#include <iprt/param.h>
44#include <iprt/path.h>
45#include <iprt/stream.h>
46#include <iprt/string.h>
47
48
49/*******************************************************************************
50* Defined Constants And Macros *
51*******************************************************************************/
52/** The name of the settings files. */
53#define SCM_SETTINGS_FILENAME ".scm-settings"
54
55
56/*******************************************************************************
57* Structures and Typedefs *
58*******************************************************************************/
59/** Pointer to const massager settings. */
60typedef struct SCMSETTINGSBASE const *PCSCMSETTINGSBASE;
61
62/** End of line marker type. */
63typedef enum SCMEOL
64{
65 SCMEOL_NONE = 0,
66 SCMEOL_LF = 1,
67 SCMEOL_CRLF = 2
68} SCMEOL;
69/** Pointer to an end of line marker type. */
70typedef SCMEOL *PSCMEOL;
71
72/**
73 * Line record.
74 */
75typedef struct SCMSTREAMLINE
76{
77 /** The offset of the line. */
78 size_t off;
79 /** The line length, excluding the LF character.
80 * @todo This could be derived from the offset of the next line if that wasn't
81 * so tedious. */
82 size_t cch;
83 /** The end of line marker type. */
84 SCMEOL enmEol;
85} SCMSTREAMLINE;
86/** Pointer to a line record. */
87typedef SCMSTREAMLINE *PSCMSTREAMLINE;
88
89/**
90 * Source code massager stream.
91 */
92typedef struct SCMSTREAM
93{
94 /** Pointer to the file memory. */
95 char *pch;
96 /** The current stream position. */
97 size_t off;
98 /** The current stream size. */
99 size_t cb;
100 /** The size of the memory pb points to. */
101 size_t cbAllocated;
102
103 /** Line records. */
104 PSCMSTREAMLINE paLines;
105 /** The current line. */
106 size_t iLine;
107 /** The current stream size given in lines. */
108 size_t cLines;
109 /** The sizeof the the memory backing paLines. */
110 size_t cLinesAllocated;
111
112 /** Set if write-only, clear if read-only. */
113 bool fWriteOrRead;
114 /** Set if the memory pb points to is from RTFileReadAll. */
115 bool fFileMemory;
116 /** Set if fully broken into lines. */
117 bool fFullyLineated;
118
119 /** Stream status code (IPRT). */
120 int rc;
121} SCMSTREAM;
122/** Pointer to a SCM stream. */
123typedef SCMSTREAM *PSCMSTREAM;
124/** Pointer to a const SCM stream. */
125typedef SCMSTREAM const *PCSCMSTREAM;
126
127/**
128 * A rewriter.
129 *
130 * This works like a stream editor, reading @a pIn, modifying it and writing it
131 * to @a pOut.
132 *
133 * @returns true if any changes were made, false if not.
134 * @param pIn The input stream.
135 * @param pOut The output stream.
136 * @param pSettings The settings.
137 */
138typedef bool (*PFNSCMREWRITER)(PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
139
140
141/**
142 * Configuration entry.
143 */
144typedef struct SCMCFGENTRY
145{
146 /** Number of rewriters. */
147 size_t cRewriters;
148 /** Pointer to an array of rewriters. */
149 PFNSCMREWRITER const *papfnRewriter;
150 /** File pattern (simple). */
151 const char *pszFilePattern;
152} SCMCFGENTRY;
153typedef SCMCFGENTRY *PSCMCFGENTRY;
154typedef SCMCFGENTRY const *PCSCMCFGENTRY;
155
156
157/**
158 * Diff state.
159 */
160typedef struct SCMDIFFSTATE
161{
162 size_t cDiffs;
163 const char *pszFilename;
164
165 PSCMSTREAM pLeft;
166 PSCMSTREAM pRight;
167
168 /** Whether to ignore end of line markers when diffing. */
169 bool fIgnoreEol;
170 /** Whether to ignore trailing whitespace. */
171 bool fIgnoreTrailingWhite;
172 /** Whether to ignore leading whitespace. */
173 bool fIgnoreLeadingWhite;
174 /** Whether to print special characters in human readable form or not. */
175 bool fSpecialChars;
176 /** The tab size. */
177 size_t cchTab;
178 /** Where to push the diff. */
179 PRTSTREAM pDiff;
180} SCMDIFFSTATE;
181/** Pointer to a diff state. */
182typedef SCMDIFFSTATE *PSCMDIFFSTATE;
183
184/**
185 * Source Code Massager Settings.
186 */
187typedef struct SCMSETTINGSBASE
188{
189 bool fConvertEol;
190 bool fConvertTabs;
191 bool fForceFinalEol;
192 bool fForceTrailingLine;
193 bool fStripTrailingBlanks;
194 bool fStripTrailingLines;
195 unsigned cchTab;
196} SCMSETTINGSBASE;
197/** Pointer to massager settings. */
198typedef SCMSETTINGSBASE *PSCMSETTINGSBASE;
199
200/**
201 * Option identifiers.
202 *
203 * @note The first chunk, down to SCMOPT_TAB_SIZE, are alternately set &
204 * clear. So, the option setting a flag (boolean) will have an even
205 * number and the one clearing it will have an odd number.
206 * @note Down to SCMOPT_LAST_SETTINGS corresponds exactly to SCMSETTINGSBASE.
207 */
208typedef enum SCMOPT
209{
210 SCMOPT_CONVERT_EOL = 10000,
211 SCMOPT_NO_CONVERT_EOL,
212 SCMOPT_CONVERT_TABS,
213 SCMOPT_NO_CONVERT_TABS,
214 SCMOPT_FORCE_FINAL_EOL,
215 SCMOPT_NO_FORCE_FINAL_EOL,
216 SCMOPT_FORCE_TRAILING_LINE,
217 SCMOPT_NO_FORCE_TRAILING_LINE,
218 SCMOPT_STRIP_TRAILING_BLANKS,
219 SCMOPT_NO_STRIP_TRAILING_BLANKS,
220 SCMOPT_STRIP_TRAILING_LINES,
221 SCMOPT_NO_STRIP_TRAILING_LINES,
222 SCMOPT_TAB_SIZE,
223 SCMOPT_LAST_SETTINGS = SCMOPT_TAB_SIZE,
224 //
225 SCMOPT_DIFF_IGNORE_EOL,
226 SCMOPT_DIFF_NO_IGNORE_EOL,
227 SCMOPT_DIFF_IGNORE_SPACE,
228 SCMOPT_DIFF_NO_IGNORE_SPACE,
229 SCMOPT_DIFF_IGNORE_LEADING_SPACE,
230 SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE,
231 SCMOPT_DIFF_IGNORE_TRAILING_SPACE,
232 SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE,
233 SCMOPT_DIFF_SPECIAL_CHARS,
234 SCMOPT_DIFF_NO_SPECIAL_CHARS,
235 SCMOPT_END
236} SCMOPT;
237
238
239/**
240 * File/dir pattern + options.
241 */
242typedef struct SCMPATRNOPTPAIR
243{
244 char *pszPattern;
245 char *pszOptions;
246} SCMPATRNOPTPAIR;
247/** Pointer to a pattern + option pair. */
248typedef SCMPATRNOPTPAIR *PSCMPATRNOPTPAIR;
249
250
251/** Pointer to a settings set. */
252typedef struct SCMSETTINGS *PSCMSETTINGS;
253/**
254 * Settings set.
255 *
256 * This structure is constructed from the command line arguments or any
257 * .scm-settings file found in a directory we recurse into. When recusing in
258 * and out of a directory, we push and pop a settings set for it.
259 *
260 * The .scm-settings file has two kinds of setttings, first there are the
261 * unqualified base settings and then there are the settings which applies to a
262 * set of files or directories. The former are lines with command line options.
263 * For the latter, the options are preceeded by a string pattern and a colon.
264 * The pattern specifies which files (and/or directories) the options applies
265 * to.
266 *
267 * We parse the base options into the Base member and put the others into the
268 * paPairs array.
269 */
270typedef struct SCMSETTINGS
271{
272 /** Pointer to the setting file below us in the stack. */
273 PSCMSETTINGS pDown;
274 /** Pointer to the setting file above us in the stack. */
275 PSCMSETTINGS pUp;
276 /** File/dir patterns and their options. */
277 PSCMPATRNOPTPAIR paPairs;
278 /** The number of entires in paPairs. */
279 uint32_t cPairs;
280 /** The base settings that was read out of the file. */
281 SCMSETTINGSBASE Base;
282} SCMSETTINGS;
283/** Pointer to a const settings set. */
284typedef SCMSETTINGS const *PCSCMSETTINGS;
285
286
287
288/*******************************************************************************
289* Internal Functions *
290*******************************************************************************/
291static bool rewrite_StripTrailingBlanks(PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
292static bool rewrite_ExpandTabs(PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
293static bool rewrite_ForceNativeEol(PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
294static bool rewrite_ForceLF(PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
295static bool rewrite_ForceCRLF(PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
296static bool rewrite_AdjustTrailingLines(PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
297static bool rewrite_Makefile_kup(PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
298static bool rewrite_Makefile_kmk(PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
299static bool rewrite_C_and_CPP(PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
300
301
302/*******************************************************************************
303* Global Variables *
304*******************************************************************************/
305static const char g_szProgName[] = "scm";
306static const char *g_pszChangedSuff = "";
307static const char g_szTabSpaces[16+1] = " ";
308static bool g_fDryRun = true;
309static bool g_fDiffSpecialChars = true;
310static bool g_fDiffIgnoreEol = false;
311static bool g_fDiffIgnoreLeadingWS = false;
312static bool g_fDiffIgnoreTrailingWS = false;
313static int g_iVerbosity = 2;//99; //0;
314
315/** The global settings. */
316static SCMSETTINGSBASE const g_Defaults =
317{
318 /* .fStripTrailingBlanks = */ true,
319 /* .fStripTrailingLines = */ true,
320 /* .fForceFinalEol = */ true,
321 /* .fForceTrailingLine = */ false,
322 /* .fConvertTabs = */ true,
323 /* .cchTab = */ 8,
324 /* .fConvertEol = */ true
325};
326
327/** Option definitions for the base settings. */
328static RTGETOPTDEF g_aScmOpts[] =
329{
330 { "--convert-eol", SCMOPT_CONVERT_EOL, RTGETOPT_REQ_NOTHING },
331 { "--no-convert-eol", SCMOPT_NO_CONVERT_EOL, RTGETOPT_REQ_NOTHING },
332 { "--convert-tabs", SCMOPT_CONVERT_TABS, RTGETOPT_REQ_NOTHING },
333 { "--no-convert-tabs", SCMOPT_NO_CONVERT_TABS, RTGETOPT_REQ_NOTHING },
334 { "--force-final-eol", SCMOPT_FORCE_FINAL_EOL, RTGETOPT_REQ_NOTHING },
335 { "--no-force-final-eol", SCMOPT_NO_FORCE_FINAL_EOL, RTGETOPT_REQ_NOTHING },
336 { "--force-trailing-line", SCMOPT_FORCE_TRAILING_LINE, RTGETOPT_REQ_NOTHING },
337 { "--no-force-trailing-line", SCMOPT_NO_FORCE_TRAILING_LINE, RTGETOPT_REQ_NOTHING },
338 { "--strip-trailing-blanks", SCMOPT_STRIP_TRAILING_BLANKS, RTGETOPT_REQ_NOTHING },
339 { "--no-strip-trailing-blanks", SCMOPT_NO_STRIP_TRAILING_BLANKS, RTGETOPT_REQ_NOTHING },
340 { "--strip-trailing-lines", SCMOPT_STRIP_TRAILING_LINES, RTGETOPT_REQ_NOTHING },
341 { "--strip-no-trailing-lines", SCMOPT_NO_STRIP_TRAILING_LINES, RTGETOPT_REQ_NOTHING },
342 { "--tab-size", SCMOPT_TAB_SIZE, RTGETOPT_REQ_UINT8 },
343};
344
345/** Consider files matching the following patterns (base names only). */
346static const char *g_pszFileFilter = NULL;
347/** Filter out files matching the following patterns. This is applied to the
348 * base names as well as the absolute paths. */
349static const char *g_pszFileFilterOut =
350 "*.exe|"
351 "*.com|"
352 "20*-*-*.log|"
353 "*/src/VBox/Runtime/testcase/soundcard.h|"
354 "*/src/VBox/Runtime/include/internal/ldrELF*.h|"
355 "*/src/VBox/Runtime/common/math/x86/fenv-x86.c|"
356 "*/src/VBox/Runtime/common/checksum/md5.cpp|"
357 "/dummy/."
358;
359/** Consider directories matching the following patterns (base names only) */
360static const char *g_pszDirFilter = NULL;
361/** Filter out directories matching the following patterns. This is applied
362 * to base names as well as the aboslute paths. All absolute paths ends with a
363 * slash and dot ("/."). */
364static const char *g_pszDirFilterOut =
365 // generic
366 ".svn|"
367 ".hg|"
368 ".git|"
369 "CVS|"
370 // root
371 "out|"
372 "tools|"
373 "webtools|"
374 "kBuild|"
375 "debian|"
376 "SlickEdit|"
377 // src
378 "*/src/libs/.|"
379 "*/src/apps/.|"
380 "*/src/VBox/Frontends/.|"
381 "*/src/VBox/Additions/x11/x11include/.|"
382 "*/src/VBox/Additions/WINNT/Graphics/Wine/.|"
383 "*/src/VBox/Additions/common/crOpenGL/.|"
384 "*/src/VBox/HostServices/SharedOpenGL/.|"
385 "*/src/VBox/GuestHost/OpenGL/*/.|"
386 "*/src/VBox/Devices/PC/Etherboot-src/*/.|"
387 "*/src/VBox/Devices/Network/lwip/.|"
388 "*/src/VBox/Devices/Storage/VBoxHDDFormats/StorageCraft/*/.|"
389 "*/src/VBox/Runtime/r0drv/solaris/vbi/*/.|"
390 "*/src/VBox/Runtime/common/math/gcc/.|"
391 "/dummy"
392;
393
394static PFNSCMREWRITER const g_aRewritersFor_Makefile_kup[] =
395{
396 rewrite_Makefile_kup
397};
398
399static PFNSCMREWRITER const g_aRewritersFor_Makefile_kmk[] =
400{
401 rewrite_ForceNativeEol,
402 rewrite_StripTrailingBlanks,
403 rewrite_AdjustTrailingLines,
404 rewrite_Makefile_kmk
405};
406
407static PFNSCMREWRITER const g_aRewritersFor_C_and_CPP[] =
408{
409 rewrite_ForceNativeEol,
410 rewrite_ExpandTabs,
411 rewrite_StripTrailingBlanks,
412 rewrite_AdjustTrailingLines,
413 rewrite_C_and_CPP
414};
415
416static PFNSCMREWRITER const g_aRewritersFor_ShellScripts[] =
417{
418 rewrite_ForceLF,
419 rewrite_ExpandTabs,
420 rewrite_StripTrailingBlanks
421};
422
423static PFNSCMREWRITER const g_aRewritersFor_BatchFiles[] =
424{
425 rewrite_ForceCRLF,
426 rewrite_ExpandTabs,
427 rewrite_StripTrailingBlanks
428};
429
430static SCMCFGENTRY const g_aConfigs[] =
431{
432 { RT_ELEMENTS(g_aRewritersFor_Makefile_kup), &g_aRewritersFor_Makefile_kup[0], "Makefile.kup" },
433 { RT_ELEMENTS(g_aRewritersFor_Makefile_kmk), &g_aRewritersFor_Makefile_kmk[0], "Makefile.kmk" },
434 { RT_ELEMENTS(g_aRewritersFor_C_and_CPP), &g_aRewritersFor_C_and_CPP[0], "*.c|*.h|*.cpp|*.hpp|*.C|*.CPP|*.cxx|*.cc" },
435 { RT_ELEMENTS(g_aRewritersFor_ShellScripts), &g_aRewritersFor_ShellScripts[0], "*.sh|configure" },
436 { RT_ELEMENTS(g_aRewritersFor_BatchFiles), &g_aRewritersFor_BatchFiles[0], "*.bat|*.cmd|*.btm|*.vbs|*.ps1" },
437};
438
439
440/* -=-=-=-=-=- memory streams -=-=-=-=-=- */
441
442
443/**
444 * Initializes the stream structure.
445 *
446 * @param pStream The stream structure.
447 * @param fWriteOrRead The value of the fWriteOrRead stream member.
448 */
449static void scmStreamInitInternal(PSCMSTREAM pStream, bool fWriteOrRead)
450{
451 pStream->pch = NULL;
452 pStream->off = 0;
453 pStream->cb = 0;
454 pStream->cbAllocated = 0;
455
456 pStream->paLines = NULL;
457 pStream->iLine = 0;
458 pStream->cLines = 0;
459 pStream->cLinesAllocated = 0;
460
461 pStream->fWriteOrRead = fWriteOrRead;
462 pStream->fFileMemory = false;
463 pStream->fFullyLineated = false;
464
465 pStream->rc = VINF_SUCCESS;
466}
467
468/**
469 * Initialize an input stream.
470 *
471 * @returns IPRT status code.
472 * @param pStream The stream to initialize.
473 * @param pszFilename The file to take the stream content from.
474 */
475int ScmStreamInitForReading(PSCMSTREAM pStream, const char *pszFilename)
476{
477 scmStreamInitInternal(pStream, false /*fWriteOrRead*/);
478
479 void *pvFile;
480 size_t cbFile;
481 int rc = pStream->rc = RTFileReadAll(pszFilename, &pvFile, &cbFile);
482 if (RT_SUCCESS(rc))
483 {
484 pStream->pch = (char *)pvFile;
485 pStream->cb = cbFile;
486 pStream->cbAllocated = cbFile;
487 pStream->fFileMemory = true;
488 }
489 return rc;
490}
491
492/**
493 * Initialize an output stream.
494 *
495 * @returns IPRT status code
496 * @param pStream The stream to initialize.
497 * @param pRelatedStream Pointer to a related stream. NULL is fine.
498 */
499int ScmStreamInitForWriting(PSCMSTREAM pStream, PCSCMSTREAM pRelatedStream)
500{
501 scmStreamInitInternal(pStream, true /*fWriteOrRead*/);
502
503 /* allocate stuff */
504 size_t cbEstimate = pRelatedStream
505 ? pRelatedStream->cb + pRelatedStream->cb / 10
506 : _64K;
507 cbEstimate = RT_ALIGN(cbEstimate, _4K);
508 pStream->pch = (char *)RTMemAlloc(cbEstimate);
509 if (pStream->pch)
510 {
511 size_t cLinesEstimate = pRelatedStream && pRelatedStream->fFullyLineated
512 ? pRelatedStream->cLines + pRelatedStream->cLines / 10
513 : cbEstimate / 24;
514 cLinesEstimate = RT_ALIGN(cLinesEstimate, 512);
515 pStream->paLines = (PSCMSTREAMLINE)RTMemAlloc(cLinesEstimate * sizeof(SCMSTREAMLINE));
516 if (pStream->paLines)
517 {
518 pStream->paLines[0].off = 0;
519 pStream->paLines[0].cch = 0;
520 pStream->paLines[0].enmEol = SCMEOL_NONE;
521 pStream->cbAllocated = cbEstimate;
522 pStream->cLinesAllocated = cLinesEstimate;
523 return VINF_SUCCESS;
524 }
525
526 RTMemFree(pStream->pch);
527 pStream->pch = NULL;
528 }
529 return pStream->rc = VERR_NO_MEMORY;
530}
531
532/**
533 * Frees the resources associated with the stream.
534 *
535 * Nothing is happens to whatever the stream was initialized from or dumped to.
536 *
537 * @param pStream The stream to delete.
538 */
539void ScmStreamDelete(PSCMSTREAM pStream)
540{
541 if (pStream->pch)
542 {
543 if (pStream->fFileMemory)
544 RTFileReadAllFree(pStream->pch, pStream->cbAllocated);
545 else
546 RTMemFree(pStream->pch);
547 pStream->pch = NULL;
548 }
549 pStream->cbAllocated = 0;
550
551 if (pStream->paLines)
552 {
553 RTMemFree(pStream->paLines);
554 pStream->paLines = NULL;
555 }
556 pStream->cLinesAllocated = 0;
557}
558
559/**
560 * Get the stream status code.
561 *
562 * @returns IPRT status code.
563 * @param pStream The stream.
564 */
565int ScmStreamGetStatus(PCSCMSTREAM pStream)
566{
567 return pStream->rc;
568}
569
570/**
571 * Grows the buffer of a write stream.
572 *
573 * @returns IPRT status code.
574 * @param pStream The stream. Must be in write mode.
575 * @param cbAppending The minimum number of bytes to grow the buffer
576 * with.
577 */
578static int scmStreamGrowBuffer(PSCMSTREAM pStream, size_t cbAppending)
579{
580 size_t cbAllocated = pStream->cbAllocated;
581 cbAllocated += RT_MAX(0x1000 + cbAppending, cbAllocated);
582 cbAllocated = RT_ALIGN(cbAllocated, 0x1000);
583 void *pvNew;
584 if (!pStream->fFileMemory)
585 {
586 pvNew = RTMemRealloc(pStream->pch, cbAllocated);
587 if (!pvNew)
588 return pStream->rc = VERR_NO_MEMORY;
589 }
590 else
591 {
592 pvNew = RTMemDupEx(pStream->pch, pStream->off, cbAllocated - pStream->off);
593 if (!pvNew)
594 return pStream->rc = VERR_NO_MEMORY;
595 RTFileReadAllFree(pStream->pch, pStream->cbAllocated);
596 pStream->fFileMemory = false;
597 }
598 pStream->pch = (char *)pvNew;
599 pStream->cbAllocated = cbAllocated;
600
601 return VINF_SUCCESS;
602}
603
604/**
605 * Grows the line array of a stream.
606 *
607 * @returns IPRT status code.
608 * @param pStream The stream.
609 * @param iMinLine Minimum line number.
610 */
611static int scmStreamGrowLines(PSCMSTREAM pStream, size_t iMinLine)
612{
613 size_t cLinesAllocated = pStream->cLinesAllocated;
614 cLinesAllocated += RT_MAX(512 + iMinLine, cLinesAllocated);
615 cLinesAllocated = RT_ALIGN(cLinesAllocated, 512);
616 void *pvNew = RTMemRealloc(pStream->paLines, cLinesAllocated * sizeof(SCMSTREAMLINE));
617 if (!pvNew)
618 return pStream->rc = VERR_NO_MEMORY;
619
620 pStream->paLines = (PSCMSTREAMLINE)pvNew;
621 pStream->cLinesAllocated = cLinesAllocated;
622 return VINF_SUCCESS;
623}
624
625/**
626 * Rewinds the stream and sets the mode to read.
627 *
628 * @param pStream The stream.
629 */
630void ScmStreamRewindForReading(PSCMSTREAM pStream)
631{
632 pStream->off = 0;
633 pStream->iLine = 0;
634 pStream->fWriteOrRead = false;
635 pStream->rc = VINF_SUCCESS;
636}
637
638/**
639 * Rewinds the stream and sets the mode to write.
640 *
641 * @param pStream The stream.
642 */
643void ScmStreamRewindForWriting(PSCMSTREAM pStream)
644{
645 pStream->off = 0;
646 pStream->iLine = 0;
647 pStream->cLines = 0;
648 pStream->fWriteOrRead = true;
649 pStream->fFullyLineated = true;
650 pStream->rc = VINF_SUCCESS;
651}
652
653/**
654 * Checks if it's a text stream.
655 *
656 * Not 100% proof.
657 *
658 * @returns true if it probably is a text file, false if not.
659 * @param pStream The stream. Write or read, doesn't matter.
660 */
661bool ScmStreamIsText(PSCMSTREAM pStream)
662{
663 if (memchr(pStream->pch, '\0', pStream->cb))
664 return false;
665 if (!pStream->cb)
666 return false;
667 return true;
668}
669
670/**
671 * Performs an integrity check of the stream.
672 *
673 * @returns IPRT status code.
674 * @param pStream The stream.
675 */
676int ScmStreamCheckItegrity(PSCMSTREAM pStream)
677{
678 /*
679 * Perform sanity checks.
680 */
681 size_t const cbFile = pStream->cb;
682 for (size_t iLine = 0; iLine < pStream->cLines; iLine++)
683 {
684 size_t offEol = pStream->paLines[iLine].off + pStream->paLines[iLine].cch;
685 AssertReturn(offEol + pStream->paLines[iLine].enmEol <= cbFile, VERR_INTERNAL_ERROR_2);
686 switch (pStream->paLines[iLine].enmEol)
687 {
688 case SCMEOL_LF:
689 AssertReturn(pStream->pch[offEol] == '\n', VERR_INTERNAL_ERROR_3);
690 break;
691 case SCMEOL_CRLF:
692 AssertReturn(pStream->pch[offEol] == '\r', VERR_INTERNAL_ERROR_3);
693 AssertReturn(pStream->pch[offEol + 1] == '\n', VERR_INTERNAL_ERROR_3);
694 break;
695 case SCMEOL_NONE:
696 AssertReturn(iLine + 1 >= pStream->cLines, VERR_INTERNAL_ERROR_4);
697 break;
698 default:
699 AssertReturn(iLine + 1 >= pStream->cLines, VERR_INTERNAL_ERROR_5);
700 }
701 }
702 return VINF_SUCCESS;
703}
704
705/**
706 * Writes the stream to a file.
707 *
708 * @returns IPRT status code
709 * @param pStream The stream.
710 * @param pszFilenameFmt The filename format string.
711 * @param ... Format arguments.
712 */
713int ScmStreamWriteToFile(PSCMSTREAM pStream, const char *pszFilenameFmt, ...)
714{
715 int rc;
716
717#ifdef RT_STRICT
718 /*
719 * Check that what we're going to write makes sense first.
720 */
721 rc = ScmStreamCheckItegrity(pStream);
722 if (RT_FAILURE(rc))
723 return rc;
724#endif
725
726 /*
727 * Do the actual writing.
728 */
729 RTFILE hFile;
730 va_list va;
731 va_start(va, pszFilenameFmt);
732 rc = RTFileOpenV(&hFile, RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_WRITE, pszFilenameFmt, va);
733 if (RT_SUCCESS(rc))
734 {
735 rc = RTFileWrite(hFile, pStream->pch, pStream->cb, NULL);
736 RTFileClose(hFile);
737 }
738 return rc;
739}
740
741/**
742 * Worker for ScmStreamGetLine that builds the line number index while parsing
743 * the stream.
744 *
745 * @returns Same as SCMStreamGetLine.
746 * @param pStream The stream. Must be in read mode.
747 * @param pcchLine Where to return the line length.
748 * @param penmEol Where to return the kind of end of line marker.
749 */
750static const char *scmStreamGetLineInternal(PSCMSTREAM pStream, size_t *pcchLine, PSCMEOL penmEol)
751{
752 AssertReturn(!pStream->fWriteOrRead, NULL);
753 if (RT_FAILURE(pStream->rc))
754 return NULL;
755
756 size_t off = pStream->off;
757 size_t cb = pStream->cb;
758 if (RT_UNLIKELY(off >= cb))
759 {
760 pStream->fFullyLineated = true;
761 return NULL;
762 }
763
764 size_t iLine = pStream->iLine;
765 if (RT_UNLIKELY(iLine >= pStream->cLinesAllocated))
766 {
767 int rc = scmStreamGrowLines(pStream, iLine);
768 if (RT_FAILURE(rc))
769 return NULL;
770 }
771 pStream->paLines[iLine].off = off;
772
773 cb -= off;
774 const char *pchRet = &pStream->pch[off];
775 const char *pch = (const char *)memchr(pchRet, '\n', cb);
776 if (RT_LIKELY(pch))
777 {
778 cb = pch - pchRet;
779 pStream->off = off + cb + 1;
780 if ( cb < 1
781 || pch[-1] != '\r')
782 pStream->paLines[iLine].enmEol = *penmEol = SCMEOL_LF;
783 else
784 {
785 pStream->paLines[iLine].enmEol = *penmEol = SCMEOL_CRLF;
786 cb--;
787 }
788 }
789 else
790 {
791 pStream->off = off + cb;
792 pStream->paLines[iLine].enmEol = *penmEol = SCMEOL_NONE;
793 }
794 *pcchLine = cb;
795 pStream->paLines[iLine].cch = cb;
796 pStream->cLines = pStream->iLine = ++iLine;
797
798 return pchRet;
799}
800
801/**
802 * Internal worker that lineates a stream.
803 *
804 * @returns IPRT status code.
805 * @param pStream The stream. Caller must check that it is in
806 * read mode.
807 */
808static int scmStreamLineate(PSCMSTREAM pStream)
809{
810 /* Save the stream position. */
811 size_t const offSaved = pStream->off;
812 size_t const iLineSaved = pStream->iLine;
813
814 /* Get each line. */
815 size_t cchLine;
816 SCMEOL enmEol;
817 while (scmStreamGetLineInternal(pStream, &cchLine, &enmEol))
818 /* nothing */;
819 Assert(RT_FAILURE(pStream->rc) || pStream->fFullyLineated);
820
821 /* Restore the position */
822 pStream->off = offSaved;
823 pStream->iLine = iLineSaved;
824
825 return pStream->rc;
826}
827
828/**
829 * Get the current stream position as a line number.
830 *
831 * @returns The current line (0-based).
832 * @param pStream The stream.
833 */
834size_t ScmStreamTellLine(PSCMSTREAM pStream)
835{
836 return pStream->iLine;
837}
838
839/**
840 * Gets the number of lines in the stream.
841 *
842 * @returns The number of lines.
843 * @param pStream The stream.
844 */
845size_t ScmStreamCountLines(PSCMSTREAM pStream)
846{
847 if (!pStream->fFullyLineated)
848 scmStreamLineate(pStream);
849 return pStream->cLines;
850}
851
852/**
853 * Seeks to a given line in the tream.
854 *
855 * @returns IPRT status code.
856 *
857 * @param pStream The stream. Must be in read mode.
858 * @param iLine The line to seek to. If this is beyond the end
859 * of the stream, the position is set to the end.
860 */
861int ScmStreamSeekByLine(PSCMSTREAM pStream, size_t iLine)
862{
863 AssertReturn(!pStream->fWriteOrRead, VERR_ACCESS_DENIED);
864 if (RT_FAILURE(pStream->rc))
865 return pStream->rc;
866
867 /* Must be fully lineated of course. */
868 if (RT_UNLIKELY(!pStream->fFullyLineated))
869 {
870 int rc = scmStreamLineate(pStream);
871 if (RT_FAILURE(rc))
872 return rc;
873 }
874
875 /* Ok, do the job. */
876 if (iLine < pStream->cLines)
877 {
878 pStream->off = pStream->paLines[iLine].off;
879 pStream->iLine = iLine;
880 }
881 else
882 {
883 pStream->off = pStream->cb;
884 pStream->iLine = pStream->cLines;
885 }
886 return VINF_SUCCESS;
887}
888
889/**
890 * Get a numbered line from the stream (changes the position).
891 *
892 * A line is always delimited by a LF character or the end of the stream. The
893 * delimiter is not included in returned line length, but instead returned via
894 * the @a penmEol indicator.
895 *
896 * @returns Pointer to the first character in the line, not NULL terminated.
897 * NULL if the end of the stream has been reached or some problem
898 * occured.
899 *
900 * @param pStream The stream. Must be in read mode.
901 * @param iLine The line to get (0-based).
902 * @param pcchLine The length.
903 * @param penmEol Where to return the end of line type indicator.
904 */
905static const char *ScmStreamGetLineByNo(PSCMSTREAM pStream, size_t iLine, size_t *pcchLine, PSCMEOL penmEol)
906{
907 AssertReturn(!pStream->fWriteOrRead, NULL);
908 if (RT_FAILURE(pStream->rc))
909 return NULL;
910
911 /* Make sure it's fully lineated so we can use the index. */
912 if (RT_UNLIKELY(!pStream->fFullyLineated))
913 {
914 int rc = scmStreamLineate(pStream);
915 if (RT_FAILURE(rc))
916 return NULL;
917 }
918
919 /* End of stream? */
920 if (RT_UNLIKELY(iLine >= pStream->cLines))
921 {
922 pStream->off = pStream->cb;
923 pStream->iLine = pStream->cLines;
924 return NULL;
925 }
926
927 /* Get the data. */
928 const char *pchRet = &pStream->pch[pStream->paLines[iLine].off];
929 *pcchLine = pStream->paLines[iLine].cch;
930 *penmEol = pStream->paLines[iLine].enmEol;
931
932 /* update the stream position. */
933 pStream->off = pStream->paLines[iLine].cch + pStream->paLines[iLine].enmEol;
934 pStream->iLine = iLine + 1;
935
936 return pchRet;
937}
938
939/**
940 * Get a line from the stream.
941 *
942 * A line is always delimited by a LF character or the end of the stream. The
943 * delimiter is not included in returned line length, but instead returned via
944 * the @a penmEol indicator.
945 *
946 * @returns Pointer to the first character in the line, not NULL terminated.
947 * NULL if the end of the stream has been reached or some problem
948 * occured.
949 *
950 * @param pStream The stream. Must be in read mode.
951 * @param pcchLine The length.
952 * @param penmEol Where to return the end of line type indicator.
953 */
954static const char *ScmStreamGetLine(PSCMSTREAM pStream, size_t *pcchLine, PSCMEOL penmEol)
955{
956 if (!pStream->fFullyLineated)
957 return scmStreamGetLineInternal(pStream, pcchLine, penmEol);
958 return ScmStreamGetLineByNo(pStream, pStream->iLine, pcchLine, penmEol);
959}
960
961/**
962 * Checks if the given line is empty or full of white space.
963 *
964 * @returns true if white space only, false if not (or if non-existant).
965 * @param pStream The stream. Must be in read mode.
966 * @param iLine The line in question.
967 */
968static bool ScmStreamIsWhiteLine(PSCMSTREAM pStream, size_t iLine)
969{
970 SCMEOL enmEol;
971 size_t cchLine;
972 const char *pchLine = ScmStreamGetLineByNo(pStream, iLine, &cchLine, &enmEol);
973 if (!pchLine)
974 return false;
975 while (cchLine && RT_C_IS_SPACE(*pchLine))
976 pchLine++, cchLine--;
977 return cchLine == 0;
978}
979
980/**
981 * Try figure out the end of line style of the give stream.
982 *
983 * @returns Most likely end of line style.
984 * @param pStream The stream.
985 */
986SCMEOL ScmStreamGetEol(PSCMSTREAM pStream)
987{
988 SCMEOL enmEol;
989 if (pStream->cLines > 0)
990 enmEol = pStream->paLines[0].enmEol;
991 else if (pStream->cb == 0)
992 enmEol = SCMEOL_NONE;
993 else
994 {
995 const char *pchLF = (const char *)memchr(pStream->pch, '\n', pStream->cb);
996 if (pchLF && pchLF != pStream->pch && pchLF[-1] == '\r')
997 enmEol = SCMEOL_CRLF;
998 else
999 enmEol = SCMEOL_LF;
1000 }
1001
1002 if (enmEol == SCMEOL_NONE)
1003#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
1004 enmEol = SCMEOL_CRLF;
1005#else
1006 enmEol = SCMEOL_LF;
1007#endif
1008 return enmEol;
1009}
1010
1011/**
1012 * Get the end of line indicator type for a line.
1013 *
1014 * @returns The EOL indicator. If the line isn't found, the default EOL
1015 * indicator is return.
1016 * @param pStream The stream.
1017 * @param iLine The line (0-base).
1018 */
1019SCMEOL ScmStreamGetEolByLine(PSCMSTREAM pStream, size_t iLine)
1020{
1021 SCMEOL enmEol;
1022 if (iLine < pStream->cLines)
1023 enmEol = pStream->paLines[iLine].enmEol;
1024 else
1025#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
1026 enmEol = SCMEOL_CRLF;
1027#else
1028 enmEol = SCMEOL_LF;
1029#endif
1030 return enmEol;
1031}
1032
1033/**
1034 * Appends a line to the stream.
1035 *
1036 * @returns IPRT status code.
1037 * @param pStream The stream. Must be in write mode.
1038 * @param pchLine Pointer to the line.
1039 * @param cchLine Line length.
1040 * @param enmEol Which end of line indicator to use.
1041 */
1042int ScmStreamPutLine(PSCMSTREAM pStream, const char *pchLine, size_t cchLine, SCMEOL enmEol)
1043{
1044 AssertReturn(pStream->fWriteOrRead, VERR_ACCESS_DENIED);
1045 if (RT_FAILURE(pStream->rc))
1046 return pStream->rc;
1047
1048 /*
1049 * Make sure the previous line has a new-line indicator.
1050 */
1051 size_t off = pStream->off;
1052 size_t iLine = pStream->iLine;
1053 if (RT_UNLIKELY( iLine != 0
1054 && pStream->paLines[iLine - 1].enmEol == SCMEOL_NONE))
1055 {
1056 AssertReturn(pStream->paLines[iLine].cch == 0, VERR_INTERNAL_ERROR_3);
1057 SCMEOL enmEol2 = enmEol != SCMEOL_NONE ? enmEol : ScmStreamGetEol(pStream);
1058 if (RT_UNLIKELY(off + cchLine + enmEol + enmEol2 > pStream->cbAllocated))
1059 {
1060 int rc = scmStreamGrowBuffer(pStream, cchLine + enmEol + enmEol2);
1061 if (RT_FAILURE(rc))
1062 return rc;
1063 }
1064 if (enmEol2 == SCMEOL_LF)
1065 pStream->pch[off++] = '\n';
1066 else
1067 {
1068 pStream->pch[off++] = '\r';
1069 pStream->pch[off++] = '\n';
1070 }
1071 pStream->paLines[iLine - 1].enmEol = enmEol2;
1072 pStream->paLines[iLine].off = off;
1073 pStream->off = off;
1074 pStream->cb = off;
1075 }
1076
1077 /*
1078 * Ensure we've got sufficient buffer space.
1079 */
1080 if (RT_UNLIKELY(off + cchLine + enmEol > pStream->cbAllocated))
1081 {
1082 int rc = scmStreamGrowBuffer(pStream, cchLine + enmEol);
1083 if (RT_FAILURE(rc))
1084 return rc;
1085 }
1086
1087 /*
1088 * Add a line record.
1089 */
1090 if (RT_UNLIKELY(iLine + 1 >= pStream->cLinesAllocated))
1091 {
1092 int rc = scmStreamGrowLines(pStream, iLine);
1093 if (RT_FAILURE(rc))
1094 return rc;
1095 }
1096
1097 pStream->paLines[iLine].cch = off - pStream->paLines[iLine].off + cchLine;
1098 pStream->paLines[iLine].enmEol = enmEol;
1099
1100 iLine++;
1101 pStream->cLines = iLine;
1102 pStream->iLine = iLine;
1103
1104 /*
1105 * Copy the line
1106 */
1107 memcpy(&pStream->pch[off], pchLine, cchLine);
1108 off += cchLine;
1109 if (enmEol == SCMEOL_LF)
1110 pStream->pch[off++] = '\n';
1111 else if (enmEol == SCMEOL_CRLF)
1112 {
1113 pStream->pch[off++] = '\r';
1114 pStream->pch[off++] = '\n';
1115 }
1116 pStream->off = off;
1117 pStream->cb = off;
1118
1119 /*
1120 * Start a new line.
1121 */
1122 pStream->paLines[iLine].off = off;
1123 pStream->paLines[iLine].cch = 0;
1124 pStream->paLines[iLine].enmEol = SCMEOL_NONE;
1125
1126 return VINF_SUCCESS;
1127}
1128
1129/**
1130 * Writes to the stream.
1131 *
1132 * @returns IPRT status code
1133 * @param pStream The stream. Must be in write mode.
1134 * @param pchBuf What to write.
1135 * @param cchBuf How much to write.
1136 */
1137int ScmStreamWrite(PSCMSTREAM pStream, const char *pchBuf, size_t cchBuf)
1138{
1139 AssertReturn(pStream->fWriteOrRead, VERR_ACCESS_DENIED);
1140 if (RT_FAILURE(pStream->rc))
1141 return pStream->rc;
1142
1143 /*
1144 * Ensure we've got sufficient buffer space.
1145 */
1146 size_t off = pStream->off;
1147 if (RT_UNLIKELY(off + cchBuf > pStream->cbAllocated))
1148 {
1149 int rc = scmStreamGrowBuffer(pStream, cchBuf);
1150 if (RT_FAILURE(rc))
1151 return rc;
1152 }
1153
1154 /*
1155 * Deal with the odd case where we've already pushed a line with SCMEOL_NONE.
1156 */
1157 size_t iLine = pStream->iLine;
1158 if (RT_UNLIKELY( iLine > 0
1159 && pStream->paLines[iLine - 1].enmEol == SCMEOL_NONE))
1160 {
1161 iLine--;
1162 pStream->cLines = iLine;
1163 pStream->iLine = iLine;
1164 }
1165
1166 /*
1167 * Deal with lines.
1168 */
1169 const char *pchLF = (const char *)memchr(pchBuf, '\n', cchBuf);
1170 if (!pchLF)
1171 pStream->paLines[iLine].cch += cchBuf;
1172 else
1173 {
1174 const char *pchLine = pchBuf;
1175 for (;;)
1176 {
1177 if (RT_UNLIKELY(iLine + 1 >= pStream->cLinesAllocated))
1178 {
1179 int rc = scmStreamGrowLines(pStream, iLine);
1180 if (RT_FAILURE(rc))
1181 {
1182 iLine = pStream->iLine;
1183 pStream->paLines[iLine].cch = off - pStream->paLines[iLine].off;
1184 pStream->paLines[iLine].enmEol = SCMEOL_NONE;
1185 return rc;
1186 }
1187 }
1188
1189 size_t cchLine = pchLF - pchLine;
1190 if ( cchLine
1191 ? pchLF[-1] != '\r'
1192 : !pStream->paLines[iLine].cch
1193 || pStream->pch[pStream->paLines[iLine].off + pStream->paLines[iLine].cch - 1] != '\r')
1194 pStream->paLines[iLine].enmEol = SCMEOL_LF;
1195 else
1196 {
1197 pStream->paLines[iLine].enmEol = SCMEOL_CRLF;
1198 cchLine--;
1199 }
1200 pStream->paLines[iLine].cch += cchLine;
1201
1202 iLine++;
1203 size_t offBuf = pchLF + 1 - pchBuf;
1204 pStream->paLines[iLine].off = off + offBuf;
1205 pStream->paLines[iLine].cch = 0;
1206 pStream->paLines[iLine].enmEol = SCMEOL_NONE;
1207
1208 size_t cchLeft = cchBuf - offBuf;
1209 pchLF = (const char *)memchr(pchLF + 1, '\n', cchLeft);
1210 if (!pchLF)
1211 {
1212 pStream->paLines[iLine].cch = cchLeft;
1213 break;
1214 }
1215 }
1216
1217 pStream->iLine = iLine;
1218 pStream->cLines = iLine;
1219 }
1220
1221 /*
1222 * Copy the data and update position and size.
1223 */
1224 memcpy(&pStream->pch[off], pchBuf, cchBuf);
1225 off += cchBuf;
1226 pStream->off = off;
1227 pStream->cb = off;
1228
1229 return VINF_SUCCESS;
1230}
1231
1232/**
1233 * Write a character to the stream.
1234 *
1235 * @returns IPRT status code
1236 * @param pStream The stream. Must be in write mode.
1237 * @param pchBuf What to write.
1238 * @param cchBuf How much to write.
1239 */
1240int ScmStreamPutCh(PSCMSTREAM pStream, char ch)
1241{
1242 AssertReturn(pStream->fWriteOrRead, VERR_ACCESS_DENIED);
1243 if (RT_FAILURE(pStream->rc))
1244 return pStream->rc;
1245
1246 /*
1247 * Only deal with the simple cases here, use ScmStreamWrite for the
1248 * annyoing stuff.
1249 */
1250 size_t off = pStream->off;
1251 if ( ch == '\n'
1252 || RT_UNLIKELY(off + 1 > pStream->cbAllocated))
1253 return ScmStreamWrite(pStream, &ch, 1);
1254
1255 /*
1256 * Just append it.
1257 */
1258 pStream->pch[off] = ch;
1259 pStream->off = off + 1;
1260 pStream->paLines[pStream->iLine].cch++;
1261
1262 return VINF_SUCCESS;
1263}
1264
1265/**
1266 * Copies @a cLines from the @a pSrc stream onto the @a pDst stream.
1267 *
1268 * The stream positions will be used and changed in both streams.
1269 *
1270 * @returns IPRT status code.
1271 * @param pDst The destionation stream. Must be in write mode.
1272 * @param cLines The number of lines. (0 is accepted.)
1273 * @param pSrc The source stream. Must be in read mode.
1274 */
1275int ScmStreamCopyLines(PSCMSTREAM pDst, PSCMSTREAM pSrc, size_t cLines)
1276{
1277 AssertReturn(pDst->fWriteOrRead, VERR_ACCESS_DENIED);
1278 if (RT_FAILURE(pDst->rc))
1279 return pDst->rc;
1280
1281 AssertReturn(!pSrc->fWriteOrRead, VERR_ACCESS_DENIED);
1282 if (RT_FAILURE(pSrc->rc))
1283 return pSrc->rc;
1284
1285 while (cLines-- > 0)
1286 {
1287 SCMEOL enmEol;
1288 size_t cchLine;
1289 const char *pchLine = ScmStreamGetLine(pSrc, &cchLine, &enmEol);
1290 if (!pchLine)
1291 return pDst->rc = (RT_FAILURE(pSrc->rc) ? pSrc->rc : VERR_EOF);
1292
1293 int rc = ScmStreamPutLine(pDst, pchLine, cchLine, enmEol);
1294 if (RT_FAILURE(rc))
1295 return rc;
1296 }
1297
1298 return VINF_SUCCESS;
1299}
1300
1301/* -=-=-=-=-=- diff -=-=-=-=-=- */
1302
1303
1304/**
1305 * Prints a range of lines with a prefix.
1306 *
1307 * @param pState The diff state.
1308 * @param chPrefix The prefix.
1309 * @param pStream The stream to get the lines from.
1310 * @param iLine The first line.
1311 * @param cLines The number of lines.
1312 */
1313static void scmDiffPrintLines(PSCMDIFFSTATE pState, char chPrefix, PSCMSTREAM pStream, size_t iLine, size_t cLines)
1314{
1315 while (cLines-- > 0)
1316 {
1317 SCMEOL enmEol;
1318 size_t cchLine;
1319 const char *pchLine = ScmStreamGetLineByNo(pStream, iLine, &cchLine, &enmEol);
1320
1321 RTStrmPutCh(pState->pDiff, chPrefix);
1322 if (pchLine && cchLine)
1323 {
1324 if (!pState->fSpecialChars)
1325 RTStrmWrite(pState->pDiff, pchLine, cchLine);
1326 else
1327 {
1328 size_t offVir = 0;
1329 const char *pchStart = pchLine;
1330 const char *pchTab = (const char *)memchr(pchLine, '\t', cchLine);
1331 while (pchTab)
1332 {
1333 RTStrmWrite(pState->pDiff, pchStart, pchTab - pchStart);
1334 offVir += pchTab - pchStart;
1335
1336 size_t cchTab = pState->cchTab - offVir % pState->cchTab;
1337 switch (cchTab)
1338 {
1339 case 1: RTStrmPutStr(pState->pDiff, "."); break;
1340 case 2: RTStrmPutStr(pState->pDiff, ".."); break;
1341 case 3: RTStrmPutStr(pState->pDiff, "[T]"); break;
1342 case 4: RTStrmPutStr(pState->pDiff, "[TA]"); break;
1343 case 5: RTStrmPutStr(pState->pDiff, "[TAB]"); break;
1344 default: RTStrmPrintf(pState->pDiff, "[TAB%.*s]", cchTab - 5, g_szTabSpaces); break;
1345 }
1346 offVir += cchTab;
1347
1348 /* next */
1349 pchStart = pchTab + 1;
1350 pchTab = (const char *)memchr(pchStart, '\t', cchLine - (pchStart - pchLine));
1351 }
1352 size_t cchLeft = cchLine - (pchStart - pchLine);
1353 if (cchLeft)
1354 RTStrmWrite(pState->pDiff, pchStart, cchLeft);
1355 }
1356 }
1357
1358 if (!pState->fSpecialChars)
1359 RTStrmPutCh(pState->pDiff, '\n');
1360 else if (enmEol == SCMEOL_LF)
1361 RTStrmPutStr(pState->pDiff, "[LF]\n");
1362 else if (enmEol == SCMEOL_CRLF)
1363 RTStrmPutStr(pState->pDiff, "[CRLF]\n");
1364 else
1365 RTStrmPutStr(pState->pDiff, "[NONE]\n");
1366
1367 iLine++;
1368 }
1369}
1370
1371
1372/**
1373 * Reports a difference and propells the streams to the lines following the
1374 * resync.
1375 *
1376 *
1377 * @returns New pState->cDiff value (just to return something).
1378 * @param pState The diff state. The cDiffs member will be
1379 * incremented.
1380 * @param cMatches The resync length.
1381 * @param iLeft Where the difference starts on the left side.
1382 * @param cLeft How long it is on this side. ~(size_t)0 is used
1383 * to indicate that it goes all the way to the end.
1384 * @param iRight Where the difference starts on the right side.
1385 * @param cRight How long it is.
1386 */
1387static size_t scmDiffReport(PSCMDIFFSTATE pState, size_t cMatches,
1388 size_t iLeft, size_t cLeft,
1389 size_t iRight, size_t cRight)
1390{
1391 /*
1392 * Adjust the input.
1393 */
1394 if (cLeft == ~(size_t)0)
1395 {
1396 size_t c = ScmStreamCountLines(pState->pLeft);
1397 if (c >= iLeft)
1398 cLeft = c - iLeft;
1399 else
1400 {
1401 iLeft = c;
1402 cLeft = 0;
1403 }
1404 }
1405
1406 if (cRight == ~(size_t)0)
1407 {
1408 size_t c = ScmStreamCountLines(pState->pRight);
1409 if (c >= iRight)
1410 cRight = c - iRight;
1411 else
1412 {
1413 iRight = c;
1414 cRight = 0;
1415 }
1416 }
1417
1418 /*
1419 * Print header if it's the first difference
1420 */
1421 if (!pState->cDiffs)
1422 RTStrmPrintf(pState->pDiff, "diff %s %s\n", pState->pszFilename, pState->pszFilename);
1423
1424 /*
1425 * Emit the change description.
1426 */
1427 char ch = cLeft == 0
1428 ? 'a'
1429 : cRight == 0
1430 ? 'd'
1431 : 'c';
1432 if (cLeft > 1 && cRight > 1)
1433 RTStrmPrintf(pState->pDiff, "%zu,%zu%c%zu,%zu\n", iLeft + 1, iLeft + cLeft, ch, iRight + 1, iRight + cRight);
1434 else if (cLeft > 1)
1435 RTStrmPrintf(pState->pDiff, "%zu,%zu%c%zu\n", iLeft + 1, iLeft + cLeft, ch, iRight + 1);
1436 else if (cRight > 1)
1437 RTStrmPrintf(pState->pDiff, "%zu%c%zu,%zu\n", iLeft + 1, ch, iRight + 1, iRight + cRight);
1438 else
1439 RTStrmPrintf(pState->pDiff, "%zu%c%zu\n", iLeft + 1, ch, iRight + 1);
1440
1441 /*
1442 * And the lines.
1443 */
1444 if (cLeft)
1445 scmDiffPrintLines(pState, '<', pState->pLeft, iLeft, cLeft);
1446 if (cLeft && cRight)
1447 RTStrmPrintf(pState->pDiff, "---\n");
1448 if (cRight)
1449 scmDiffPrintLines(pState, '>', pState->pRight, iRight, cRight);
1450
1451 /*
1452 * Reposition the streams (safely ignores return value).
1453 */
1454 ScmStreamSeekByLine(pState->pLeft, iLeft + cLeft + cMatches);
1455 ScmStreamSeekByLine(pState->pRight, iRight + cRight + cMatches);
1456
1457 pState->cDiffs++;
1458 return pState->cDiffs;
1459}
1460
1461/**
1462 * Helper for scmDiffCompare that takes care of trailing spaces and stuff
1463 * like that.
1464 */
1465static bool scmDiffCompareSlow(PSCMDIFFSTATE pState,
1466 const char *pchLeft, size_t cchLeft, SCMEOL enmEolLeft,
1467 const char *pchRight, size_t cchRight, SCMEOL enmEolRight)
1468{
1469 if (pState->fIgnoreTrailingWhite)
1470 {
1471 while (cchLeft > 0 && RT_C_IS_SPACE(pchLeft[cchLeft - 1]))
1472 cchLeft--;
1473 while (cchRight > 0 && RT_C_IS_SPACE(pchRight[cchRight - 1]))
1474 cchRight--;
1475 }
1476
1477 if (pState->fIgnoreLeadingWhite)
1478 {
1479 while (cchLeft > 0 && RT_C_IS_SPACE(*pchLeft))
1480 pchLeft++, cchLeft--;
1481 while (cchRight > 0 && RT_C_IS_SPACE(*pchRight))
1482 pchRight++, cchRight--;
1483 }
1484
1485 if ( cchLeft != cchRight
1486 || (enmEolLeft != enmEolRight && !pState->fIgnoreEol)
1487 || memcmp(pchLeft, pchRight, cchLeft))
1488 return false;
1489 return true;
1490}
1491
1492/**
1493 * Compare two lines.
1494 *
1495 * @returns true if the are equal, false if not.
1496 */
1497DECLINLINE(bool) scmDiffCompare(PSCMDIFFSTATE pState,
1498 const char *pchLeft, size_t cchLeft, SCMEOL enmEolLeft,
1499 const char *pchRight, size_t cchRight, SCMEOL enmEolRight)
1500{
1501 if ( cchLeft != cchRight
1502 || (enmEolLeft != enmEolRight && !pState->fIgnoreEol)
1503 || memcmp(pchLeft, pchRight, cchLeft))
1504 {
1505 if ( pState->fIgnoreTrailingWhite
1506 || pState->fIgnoreTrailingWhite)
1507 return scmDiffCompareSlow(pState,
1508 pchLeft, cchLeft, enmEolLeft,
1509 pchRight, cchRight, enmEolRight);
1510 return false;
1511 }
1512 return true;
1513}
1514
1515/**
1516 * Compares two sets of lines from the two files.
1517 *
1518 * @returns true if they matches, false if they don't.
1519 * @param pState The diff state.
1520 * @param iLeft Where to start in the left stream.
1521 * @param iRight Where to start in the right stream.
1522 * @param cLines How many lines to compare.
1523 */
1524static bool scmDiffCompareLines(PSCMDIFFSTATE pState, size_t iLeft, size_t iRight, size_t cLines)
1525{
1526 for (size_t iLine = 0; iLine < cLines; iLine++)
1527 {
1528 SCMEOL enmEolLeft;
1529 size_t cchLeft;
1530 const char *pchLeft = ScmStreamGetLineByNo(pState->pLeft, iLeft + iLine, &cchLeft, &enmEolLeft);
1531
1532 SCMEOL enmEolRight;
1533 size_t cchRight;
1534 const char *pchRight = ScmStreamGetLineByNo(pState->pRight, iRight + iLine, &cchRight, &enmEolRight);
1535
1536 if (!scmDiffCompare(pState, pchLeft, cchLeft, enmEolLeft, pchRight, cchRight, enmEolRight))
1537 return false;
1538 }
1539 return true;
1540}
1541
1542
1543/**
1544 * Resynchronize the two streams and reports the difference.
1545 *
1546 * Upon return, the streams will be positioned after the block of @a cMatches
1547 * lines where it resynchronized them.
1548 *
1549 * @returns pState->cDiffs (just so we can use it in a return statement).
1550 * @param pState The state.
1551 * @param cMatches The number of lines that needs to match for the
1552 * stream to be considered synchronized again.
1553 */
1554static size_t scmDiffSynchronize(PSCMDIFFSTATE pState, size_t cMatches)
1555{
1556 size_t const iStartLeft = ScmStreamTellLine(pState->pLeft) - 1;
1557 size_t const iStartRight = ScmStreamTellLine(pState->pRight) - 1;
1558 Assert(cMatches > 0);
1559
1560 /*
1561 * Compare each new line from each of the streams will all the preceding
1562 * ones, including iStartLeft/Right.
1563 */
1564 for (size_t iRange = 1; ; iRange++)
1565 {
1566 /*
1567 * Get the next line in the left stream and compare it against all the
1568 * preceding lines on the right side.
1569 */
1570 SCMEOL enmEol;
1571 size_t cchLine;
1572 const char *pchLine = ScmStreamGetLineByNo(pState->pLeft, iStartLeft + iRange, &cchLine, &enmEol);
1573 if (!pchLine)
1574 return scmDiffReport(pState, 0, iStartLeft, ~(size_t)0, iStartRight, ~(size_t)0);
1575
1576 for (size_t iRight = cMatches - 1; iRight < iRange; iRight++)
1577 {
1578 SCMEOL enmEolRight;
1579 size_t cchRight;
1580 const char *pchRight = ScmStreamGetLineByNo(pState->pRight, iStartRight + iRight,
1581 &cchRight, &enmEolRight);
1582 if ( scmDiffCompare(pState, pchLine, cchLine, enmEol, pchRight, cchRight, enmEolRight)
1583 && scmDiffCompareLines(pState,
1584 iStartLeft + iRange + 1 - cMatches,
1585 iStartRight + iRight + 1 - cMatches,
1586 cMatches - 1)
1587 )
1588 return scmDiffReport(pState, cMatches,
1589 iStartLeft, iRange + 1 - cMatches,
1590 iStartRight, iRight + 1 - cMatches);
1591 }
1592
1593 /*
1594 * Get the next line in the right stream and compare it against all the
1595 * lines on the right side.
1596 */
1597 pchLine = ScmStreamGetLineByNo(pState->pRight, iStartRight + iRange, &cchLine, &enmEol);
1598 if (!pchLine)
1599 return scmDiffReport(pState, 0, iStartLeft, ~(size_t)0, iStartRight, ~(size_t)0);
1600
1601 for (size_t iLeft = cMatches - 1; iLeft <= iRange; iLeft++)
1602 {
1603 SCMEOL enmEolLeft;
1604 size_t cchLeft;
1605 const char *pchLeft = ScmStreamGetLineByNo(pState->pLeft, iStartLeft + iLeft,
1606 &cchLeft, &enmEolLeft);
1607 if ( scmDiffCompare(pState, pchLeft, cchLeft, enmEolLeft, pchLine, cchLine, enmEol)
1608 && scmDiffCompareLines(pState,
1609 iStartLeft + iLeft + 1 - cMatches,
1610 iStartRight + iRange + 1 - cMatches,
1611 cMatches - 1)
1612 )
1613 return scmDiffReport(pState, cMatches,
1614 iStartLeft, iLeft + 1 - cMatches,
1615 iStartRight, iRange + 1 - cMatches);
1616 }
1617 }
1618}
1619
1620/**
1621 * Creates a diff of the changes between the streams @a pLeft and @a pRight.
1622 *
1623 * This currently only implements the simplest diff format, so no contexts.
1624 *
1625 * Also, note that we won't detect differences in the final newline of the
1626 * streams.
1627 *
1628 * @returns The number of differences.
1629 * @param pszFilename The filename.
1630 * @param pLeft The left side stream.
1631 * @param pRight The right side stream.
1632 * @param fIgnoreEol Whether to ignore end of line markers.
1633 * @param fIgnoreLeadingWhite Set if leading white space should be ignored.
1634 * @param fIgnoreTrailingWhite Set if trailing white space should be ignored.
1635 * @param fSpecialChars Whether to print special chars in a human
1636 * readable form or not.
1637 * @param cchTab The tab size.
1638 * @param pDiff Where to write the diff.
1639 */
1640size_t ScmDiffStreams(const char *pszFilename, PSCMSTREAM pLeft, PSCMSTREAM pRight, bool fIgnoreEol,
1641 bool fIgnoreLeadingWhite, bool fIgnoreTrailingWhite, bool fSpecialChars,
1642 size_t cchTab, PRTSTREAM pDiff)
1643{
1644#ifdef RT_STRICT
1645 ScmStreamCheckItegrity(pLeft);
1646 ScmStreamCheckItegrity(pRight);
1647#endif
1648
1649 /*
1650 * Set up the diff state.
1651 */
1652 SCMDIFFSTATE State;
1653 State.cDiffs = 0;
1654 State.pszFilename = pszFilename;
1655 State.pLeft = pLeft;
1656 State.pRight = pRight;
1657 State.fIgnoreEol = fIgnoreEol;
1658 State.fIgnoreLeadingWhite = fIgnoreLeadingWhite;
1659 State.fIgnoreTrailingWhite = fIgnoreTrailingWhite;
1660 State.fSpecialChars = fSpecialChars;
1661 State.cchTab = cchTab;
1662 State.pDiff = pDiff;
1663
1664 /*
1665 * Compare them line by line.
1666 */
1667 ScmStreamRewindForReading(pLeft);
1668 ScmStreamRewindForReading(pRight);
1669 const char *pchLeft;
1670 const char *pchRight;
1671
1672 for (;;)
1673 {
1674 SCMEOL enmEolLeft;
1675 size_t cchLeft;
1676 pchLeft = ScmStreamGetLine(pLeft, &cchLeft, &enmEolLeft);
1677
1678 SCMEOL enmEolRight;
1679 size_t cchRight;
1680 pchRight = ScmStreamGetLine(pRight, &cchRight, &enmEolRight);
1681 if (!pchLeft || !pchRight)
1682 break;
1683
1684 if (!scmDiffCompare(&State, pchLeft, cchLeft, enmEolLeft, pchRight, cchRight, enmEolRight))
1685 scmDiffSynchronize(&State, 3);
1686 }
1687
1688 /*
1689 * Deal with any remaining differences.
1690 */
1691 if (pchLeft)
1692 scmDiffReport(&State, 0, ScmStreamTellLine(pLeft) - 1, ~(size_t)0, ScmStreamTellLine(pRight), 0);
1693 else if (pchRight)
1694 scmDiffReport(&State, 0, ScmStreamTellLine(pLeft), 0, ScmStreamTellLine(pRight) - 1, ~(size_t)0);
1695
1696 /*
1697 * Report any errors.
1698 */
1699 if (RT_FAILURE(ScmStreamGetStatus(pLeft)))
1700 RTMsgError("Left diff stream error: %Rrc\n", ScmStreamGetStatus(pLeft));
1701 if (RT_FAILURE(ScmStreamGetStatus(pRight)))
1702 RTMsgError("Right diff stream error: %Rrc\n", ScmStreamGetStatus(pRight));
1703
1704 return State.cDiffs;
1705}
1706
1707
1708
1709/* -=-=-=-=-=- settings -=-=-=-=-=- */
1710
1711/**
1712 * Init a settings structure with settings from @a pSrc.
1713 *
1714 * @returns IPRT status code
1715 * @param pSettings The settings.
1716 * @param pSrc The source settings.
1717 */
1718static int scmSettingsBaseInitAndCopy(PSCMSETTINGSBASE pSettings, PCSCMSETTINGSBASE pSrc)
1719{
1720 *pSettings = *pSrc;
1721 /* No dynamically allocated stuff yet. */
1722 return VINF_SUCCESS;
1723}
1724
1725/**
1726 * Init a settings structure.
1727 *
1728 * @returns IPRT status code
1729 * @param pSettings The settings.
1730 */
1731static int scmSettingsBaseInit(PSCMSETTINGSBASE pSettings)
1732{
1733 return scmSettingsBaseInitAndCopy(pSettings, &g_Defaults);
1734}
1735
1736/**
1737 * Deletes the settings, i.e. free any dynamically allocated content.
1738 *
1739 * @param pSettings The settings.
1740 */
1741static void scmSettingsBaseDelete(PSCMSETTINGSBASE pSettings)
1742{
1743 if (pSettings)
1744 {
1745 Assert(pSettings->cchTab != ~(unsigned)0);
1746 pSettings->cchTab = ~(unsigned)0;
1747 /* No dynamically allocated stuff yet. */
1748 }
1749}
1750
1751/**
1752 * Processes a RTGetOpt result.
1753 *
1754 * @retval VINF_SUCCESS if handled.
1755 * @retval VERR_OUT_OF_RANGE if the option value was out of range.
1756 * @retval VERR_GETOPT_UNKNOWN_OPTION if the option was not recognized.
1757 *
1758 * @param pSettings The settings to change.
1759 * @param rc The RTGetOpt return value.
1760 * @param pValueUnion The RTGetOpt value union.
1761 */
1762static int scmSettingsBaseHandleOpt(PSCMSETTINGSBASE pSettings, int rc, PRTGETOPTUNION pValueUnion)
1763{
1764 switch (rc)
1765 {
1766 case SCMOPT_CONVERT_EOL:
1767 pSettings->fConvertEol = true;
1768 return VINF_SUCCESS;
1769 case SCMOPT_NO_CONVERT_EOL:
1770 pSettings->fConvertEol = false;
1771 return VINF_SUCCESS;
1772
1773 case SCMOPT_CONVERT_TABS:
1774 pSettings->fConvertTabs = true;
1775 return VINF_SUCCESS;
1776 case SCMOPT_NO_CONVERT_TABS:
1777 pSettings->fConvertTabs = false;
1778 return VINF_SUCCESS;
1779
1780 case SCMOPT_FORCE_FINAL_EOL:
1781 pSettings->fForceFinalEol = true;
1782 return VINF_SUCCESS;
1783 case SCMOPT_NO_FORCE_FINAL_EOL:
1784 pSettings->fForceFinalEol = false;
1785 return VINF_SUCCESS;
1786
1787 case SCMOPT_FORCE_TRAILING_LINE:
1788 pSettings->fForceTrailingLine = true;
1789 return VINF_SUCCESS;
1790 case SCMOPT_NO_FORCE_TRAILING_LINE:
1791 pSettings->fForceTrailingLine = false;
1792 return VINF_SUCCESS;
1793
1794 case SCMOPT_STRIP_TRAILING_BLANKS:
1795 pSettings->fStripTrailingBlanks = true;
1796 return VINF_SUCCESS;
1797 case SCMOPT_NO_STRIP_TRAILING_BLANKS:
1798 pSettings->fStripTrailingBlanks = false;
1799 return VINF_SUCCESS;
1800
1801 case SCMOPT_STRIP_TRAILING_LINES:
1802 pSettings->fStripTrailingLines = true;
1803 return VINF_SUCCESS;
1804 case SCMOPT_NO_STRIP_TRAILING_LINES:
1805 pSettings->fStripTrailingLines = false;
1806 return VINF_SUCCESS;
1807
1808 case SCMOPT_TAB_SIZE:
1809 if ( pValueUnion->u8 < 1
1810 || pValueUnion->u8 >= RT_ELEMENTS(g_szTabSpaces))
1811 {
1812 RTMsgError("Invalid tab size: %u - must be in {1..%u}\n",
1813 pValueUnion->u8, RT_ELEMENTS(g_szTabSpaces) - 1);
1814 return VERR_OUT_OF_RANGE;
1815 }
1816 pSettings->cchTab = pValueUnion->u8;
1817 return VINF_SUCCESS;
1818
1819 default:
1820 return VERR_GETOPT_UNKNOWN_OPTION;
1821 }
1822}
1823
1824/**
1825 * Parses an option string.
1826 *
1827 * @returns IPRT status code.
1828 * @param pBase The base settings structure to apply the options
1829 * to.
1830 * @param pszOptions The options to parse.
1831 */
1832static int scmSettingsBaseParseString(PSCMSETTINGSBASE pBase, const char *pszLine)
1833{
1834 int cArgs;
1835 char **papszArgs;
1836 int rc = RTGetOptArgvFromString(&papszArgs, &cArgs, pszLine, NULL);
1837 if (RT_SUCCESS(rc))
1838 {
1839 RTGETOPTUNION ValueUnion;
1840 RTGETOPTSTATE GetOptState;
1841 rc = RTGetOptInit(&GetOptState, cArgs, papszArgs, &g_aScmOpts[0], RT_ELEMENTS(g_aScmOpts), 0, 0 /*fFlags*/);
1842 if (RT_SUCCESS(rc))
1843 {
1844 while ((rc = RTGetOpt(&GetOptState, &ValueUnion)) != 0)
1845 {
1846 rc = scmSettingsBaseHandleOpt(pBase, rc, &ValueUnion);
1847 if (RT_FAILURE(rc))
1848 break;
1849 }
1850 }
1851 RTGetOptArgvFree(papszArgs);
1852 }
1853
1854 return rc;
1855}
1856
1857/**
1858 * Parses an unterminated option string.
1859 *
1860 * @returns IPRT status code.
1861 * @param pBase The base settings structure to apply the options
1862 * to.
1863 * @param pchLine The line.
1864 * @param cchLine The line length.
1865 */
1866static int scmSettingsBaseParseStringN(PSCMSETTINGSBASE pBase, const char *pchLine, size_t cchLine)
1867{
1868 char *pszLine = RTStrDupN(pchLine, cchLine);
1869 if (!pszLine)
1870 return VERR_NO_MEMORY;
1871 int rc = scmSettingsBaseParseString(pBase, pszLine);
1872 RTStrFree(pszLine);
1873 return rc;
1874}
1875
1876/**
1877 * Verifies the options string.
1878 *
1879 * @returns IPRT status code.
1880 * @param pszOptions The options to verify .
1881 */
1882static int scmSettingsBaseVerifyString(const char *pszOptions)
1883{
1884 SCMSETTINGSBASE Base;
1885 int rc = scmSettingsBaseInit(&Base);
1886 if (RT_SUCCESS(rc))
1887 {
1888 rc = scmSettingsBaseParseString(&Base, pszOptions);
1889 scmSettingsBaseDelete(&Base);
1890 }
1891 return rc;
1892}
1893
1894/**
1895 * Loads settings found in editor and SCM settings directives within the
1896 * document (@a pStream).
1897 *
1898 * @returns IPRT status code.
1899 * @param pBase The settings base to load settings into.
1900 * @param pStream The stream to scan for settings directives.
1901 */
1902static int scmSettingsBaseLoadFromDocument(PSCMSETTINGSBASE pBase, PSCMSTREAM pStream)
1903{
1904 /** @todo Editor and SCM settings directives in documents. */
1905 return VINF_SUCCESS;
1906}
1907
1908/**
1909 * Creates a new settings file struct, cloning @a pSettings.
1910 *
1911 * @returns IPRT status code.
1912 * @param ppSettings Where to return the new struct.
1913 * @param pSettingsBase The settings to inherit from.
1914 */
1915static int scmSettingsCreate(PSCMSETTINGS *ppSettings, PCSCMSETTINGSBASE pSettingsBase)
1916{
1917 PSCMSETTINGS pSettings = (PSCMSETTINGS)RTMemAlloc(sizeof(*pSettings));
1918 if (!pSettings)
1919 return VERR_NO_MEMORY;
1920 int rc = scmSettingsBaseInitAndCopy(&pSettings->Base, pSettingsBase);
1921 if (RT_SUCCESS(rc))
1922 {
1923 pSettings->pDown = NULL;
1924 pSettings->pUp = NULL;
1925 pSettings->paPairs = NULL;
1926 pSettings->cPairs = 0;
1927 *ppSettings = pSettings;
1928 return VINF_SUCCESS;
1929 }
1930 RTMemFree(pSettings);
1931 return rc;
1932}
1933
1934/**
1935 * Destroys a settings structure.
1936 *
1937 * @param pSettings The settgins structure to destroy. NULL is OK.
1938 */
1939static void scmSettingsDestroy(PSCMSETTINGS pSettings)
1940{
1941 if (pSettings)
1942 {
1943 scmSettingsBaseDelete(&pSettings->Base);
1944 for (size_t i = 0; i < pSettings->cPairs; i++)
1945 {
1946 RTStrFree(pSettings->paPairs[i].pszPattern);
1947 RTStrFree(pSettings->paPairs[i].pszOptions);
1948 pSettings->paPairs[i].pszPattern = NULL;
1949 pSettings->paPairs[i].pszOptions = NULL;
1950 }
1951 RTMemFree(pSettings->paPairs);
1952 pSettings->paPairs = NULL;
1953 RTMemFree(pSettings);
1954 }
1955}
1956
1957/**
1958 * Adds a pattern/options pair to the settings structure.
1959 *
1960 * @returns IPRT status code.
1961 * @param pSettings The settings.
1962 * @param pchLine The line containing the unparsed pair.
1963 * @param cchLine The length of the line.
1964 */
1965static int scmSettingsAddPair(PSCMSETTINGS pSettings, const char *pchLine, size_t cchLine)
1966{
1967 /*
1968 * Split the string.
1969 */
1970 const char *pchOptions = (const char *)memchr(pchLine, ':', cchLine);
1971 if (!pchOptions)
1972 return VERR_INVALID_PARAMETER;
1973 size_t cchPattern = pchOptions - pchLine - 1;
1974 size_t cchOptions = cchLine - cchPattern - 1 - 1;
1975 pchOptions++;
1976
1977 /* strip spaces everywhere */
1978 while (cchPattern > 0 && RT_C_IS_SPACE(pchLine[cchPattern - 1]))
1979 cchPattern--;
1980 while (cchPattern > 0 && RT_C_IS_SPACE(*pchLine))
1981 cchPattern--, pchLine++;
1982
1983 while (cchOptions > 0 && RT_C_IS_SPACE(pchOptions[cchOptions - 1]))
1984 cchOptions--;
1985 while (cchOptions > 0 && RT_C_IS_SPACE(*pchOptions))
1986 cchOptions--, cchOptions++;
1987
1988 /* Quietly ignore empty patterns and empty options. */
1989 if (!cchOptions || !cchPattern)
1990 return VINF_SUCCESS;
1991
1992 /*
1993 * Add the pair and verify the option string.
1994 */
1995 uint32_t iPair = pSettings->cPairs;
1996 if ((iPair % 32) == 0)
1997 {
1998 void *pvNew = RTMemRealloc(pSettings->paPairs, (iPair + 32) * sizeof(pSettings->paPairs[0]));
1999 if (!pvNew)
2000 return VERR_NO_MEMORY;
2001 pSettings->paPairs = (PSCMPATRNOPTPAIR)pvNew;
2002 }
2003
2004 pSettings->paPairs[iPair].pszPattern = RTStrDupN(pchLine, cchPattern);
2005 pSettings->paPairs[iPair].pszOptions = RTStrDupN(pchOptions, cchOptions);
2006 int rc;
2007 if ( pSettings->paPairs[iPair].pszPattern
2008 && pSettings->paPairs[iPair].pszOptions)
2009 rc = scmSettingsBaseVerifyString(pSettings->paPairs[iPair].pszOptions);
2010 else
2011 rc = VERR_NO_MEMORY;
2012 if (RT_SUCCESS(rc))
2013 pSettings->cPairs = iPair + 1;
2014 else
2015 {
2016 RTStrFree(pSettings->paPairs[iPair].pszPattern);
2017 RTStrFree(pSettings->paPairs[iPair].pszOptions);
2018 }
2019 return rc;
2020}
2021
2022/**
2023 * Loads in the settings from @a pszFilename.
2024 *
2025 * @returns IPRT status code.
2026 * @param pSettings Where to load the settings file.
2027 * @param pszFilename The file to load.
2028 */
2029static int scmSettingsLoadFile(PSCMSETTINGS pSettings, const char *pszFilename)
2030{
2031 SCMSTREAM Stream;
2032 int rc = ScmStreamInitForReading(&Stream, pszFilename);
2033 if (RT_FAILURE(rc))
2034 {
2035 RTMsgError("%s: ScmStreamInitForReading -> %Rrc\n", pszFilename, rc);
2036 return rc;
2037 }
2038
2039 SCMEOL enmEol;
2040 const char *pchLine;
2041 size_t cchLine;
2042 while ((pchLine = ScmStreamGetLine(&Stream, &cchLine, &enmEol)) != NULL)
2043 {
2044 /* Ignore leading spaces. */
2045 while (cchLine > 0 && RT_C_IS_SPACE(*pchLine))
2046 pchLine++, cchLine--;
2047
2048 /* Ignore empty lines and comment lines. */
2049 if (cchLine < 1 || *pchLine == '#')
2050 continue;
2051
2052 /* What kind of line is it? */
2053 const char *pchColon = (const char *)memchr(pchLine, ':', cchLine);
2054 if (pchColon)
2055 rc = scmSettingsAddPair(pSettings, pchLine, cchLine);
2056 else
2057 rc = scmSettingsBaseParseStringN(&pSettings->Base, pchLine, cchLine);
2058 if (RT_FAILURE(rc))
2059 {
2060 RTMsgError("%s:%d: %Rrc\n", pszFilename, ScmStreamTellLine(&Stream), rc);
2061 break;
2062 }
2063 }
2064
2065 if (RT_SUCCESS(rc))
2066 {
2067 rc = ScmStreamGetStatus(&Stream);
2068 if (RT_FAILURE(rc))
2069 RTMsgError("%s: ScmStreamGetStatus- > %Rrc\n", pszFilename, rc);
2070 }
2071
2072 ScmStreamDelete(&Stream);
2073 return rc;
2074}
2075
2076/**
2077 * Parse the specified settings file creating a new settings struct from it.
2078 *
2079 * @returns IPRT status code
2080 * @param ppSettings Where to return the new settings.
2081 * @param pszFilename The file to parse.
2082 * @param pSettingsBase The base settings we inherit from.
2083 */
2084static int scmSettingsCreateFromFile(PSCMSETTINGS *ppSettings, const char *pszFilename, PCSCMSETTINGSBASE pSettingsBase)
2085{
2086 PSCMSETTINGS pSettings;
2087 int rc = scmSettingsCreate(&pSettings, pSettingsBase);
2088 if (RT_SUCCESS(rc))
2089 {
2090 rc = scmSettingsLoadFile(pSettings, pszFilename);
2091 if (RT_SUCCESS(rc))
2092 {
2093 *ppSettings = pSettings;
2094 return VINF_SUCCESS;
2095 }
2096
2097 scmSettingsDestroy(pSettings);
2098 }
2099 *ppSettings = NULL;
2100 return rc;
2101}
2102
2103
2104/**
2105 * Create an initial settings structure when starting processing a new file or
2106 * directory.
2107 *
2108 * This will look for .scm-settings files from the root and down to the
2109 * specified directory, combining them into the returned settings structure.
2110 *
2111 * @returns IPRT status code.
2112 * @param ppSettings Where to return the pointer to the top stack
2113 * object.
2114 * @param pBaseSettings The base settings we inherit from (globals
2115 * typically).
2116 * @param pszPath The absolute path to the new directory or file.
2117 */
2118static int scmSettingsCreateForPath(PSCMSETTINGS *ppSettings, PCSCMSETTINGSBASE pBaseSettings, const char *pszPath)
2119{
2120 /*
2121 * We'll be working with a stack copy of the path.
2122 */
2123 char szFile[RTPATH_MAX];
2124 size_t cchDir = strlen(pszPath);
2125 if (cchDir >= sizeof(szFile) - sizeof(SCM_SETTINGS_FILENAME))
2126 return VERR_FILENAME_TOO_LONG;
2127
2128 /*
2129 * Create the bottom-most settings.
2130 */
2131 PSCMSETTINGS pSettings;
2132 int rc = scmSettingsCreate(&pSettings, pBaseSettings);
2133 if (RT_FAILURE(rc))
2134 return rc;
2135
2136 /*
2137 * Enumerate the path components from the root and down. Load any setting
2138 * files we find.
2139 */
2140 size_t cComponents = RTPathCountComponents(pszPath);
2141 for (size_t i = 1; i < cComponents; i++)
2142 {
2143 rc = RTPathCopyComponents(szFile, sizeof(szFile), pszPath, i);
2144 if (RT_SUCCESS(rc))
2145 rc = RTPathAppend(szFile, sizeof(szFile), SCM_SETTINGS_FILENAME);
2146 if (RT_FAILURE(rc))
2147 break;
2148 if (RTFileExists(szFile))
2149 {
2150 rc = scmSettingsLoadFile(pSettings, szFile);
2151 if (RT_FAILURE(rc))
2152 break;
2153 }
2154 }
2155
2156 if (RT_SUCCESS(rc))
2157 *ppSettings = pSettings;
2158 else
2159 scmSettingsDestroy(pSettings);
2160 return rc;
2161}
2162
2163/**
2164 * Pushes a new settings set onto the stack.
2165 *
2166 * @param ppSettingsStack The pointer to the pointer to the top stack
2167 * element. This will be used as input and output.
2168 * @param pSettings The settings to push onto the stack.
2169 */
2170static void scmSettingsStackPush(PSCMSETTINGS *ppSettingsStack, PSCMSETTINGS pSettings)
2171{
2172 PSCMSETTINGS pOld = *ppSettingsStack;
2173 pSettings->pDown = pOld;
2174 pSettings->pUp = NULL;
2175 if (pOld)
2176 pOld->pUp = pSettings;
2177 *ppSettingsStack = pSettings;
2178}
2179
2180/**
2181 * Pushes the settings of the specified directory onto the stack.
2182 *
2183 * We will load any .scm-settings in the directory. A stack entry is added even
2184 * if no settings file was found.
2185 *
2186 * @returns IPRT status code.
2187 * @param ppSettingsStack The pointer to the pointer to the top stack
2188 * element. This will be used as input and output.
2189 * @param pszDir The directory to do this for.
2190 */
2191static int scmSettingsStackPushDir(PSCMSETTINGS *ppSettingsStack, const char *pszDir)
2192{
2193 char szFile[RTPATH_MAX];
2194 int rc = RTPathJoin(szFile, sizeof(szFile), pszDir, SCM_SETTINGS_FILENAME);
2195 if (RT_SUCCESS(rc))
2196 {
2197 PSCMSETTINGS pSettings;
2198 rc = scmSettingsCreate(&pSettings, &(*ppSettingsStack)->Base);
2199 if (RT_SUCCESS(rc))
2200 {
2201 if (RTFileExists(szFile))
2202 rc = scmSettingsLoadFile(pSettings, szFile);
2203 if (RT_SUCCESS(rc))
2204 {
2205 scmSettingsStackPush(ppSettingsStack, pSettings);
2206 return VINF_SUCCESS;
2207 }
2208
2209 scmSettingsDestroy(pSettings);
2210 }
2211 }
2212 return rc;
2213}
2214
2215
2216/**
2217 * Pops a settings set off the stack.
2218 *
2219 * @returns The popped setttings.
2220 * @param ppSettingsStack The pointer to the pointer to the top stack
2221 * element. This will be used as input and output.
2222 */
2223static PSCMSETTINGS scmSettingsStackPop(PSCMSETTINGS *ppSettingsStack)
2224{
2225 PSCMSETTINGS pRet = *ppSettingsStack;
2226 PSCMSETTINGS pNew = pRet ? pRet->pDown : NULL;
2227 *ppSettingsStack = pNew;
2228 if (pNew)
2229 pNew->pUp = NULL;
2230 if (pRet)
2231 {
2232 pRet->pUp = NULL;
2233 pRet->pDown = NULL;
2234 }
2235 return pRet;
2236}
2237
2238/**
2239 * Pops and destroys the top entry of the stack.
2240 *
2241 * @param ppSettingsStack The pointer to the pointer to the top stack
2242 * element. This will be used as input and output.
2243 */
2244static void scmSettingsStackPopAndDestroy(PSCMSETTINGS *ppSettingsStack)
2245{
2246 scmSettingsDestroy(scmSettingsStackPop(ppSettingsStack));
2247}
2248
2249/**
2250 * Constructs the base settings for the specified file name.
2251 *
2252 * @returns IPRT status code.
2253 * @param pSettingsStack The top element on the settings stack.
2254 * @param pszFilename The file name.
2255 * @param pszBasename The base name (pointer within @a pszFilename).
2256 * @param cchBasename The length of the base name. (For passing to
2257 * RTStrSimplePatternMultiMatch.)
2258 * @param pBase Base settings to initialize.
2259 */
2260static int scmSettingsStackMakeFileBase(PCSCMSETTINGS pSettingsStack, const char *pszFilename,
2261 const char *pszBasename, size_t cchBasename, PSCMSETTINGSBASE pBase)
2262{
2263 int rc = scmSettingsBaseInitAndCopy(pBase, &pSettingsStack->Base);
2264 if (RT_SUCCESS(rc))
2265 {
2266 /* find the bottom entry in the stack. */
2267 PCSCMSETTINGS pCur = pSettingsStack;
2268 while (pCur->pDown)
2269 pCur = pCur->pDown;
2270
2271 /* Work our way up thru the stack and look for matching pairs. */
2272 while (pCur)
2273 {
2274 size_t const cPairs = pCur->cPairs;
2275 if (cPairs)
2276 {
2277 for (size_t i = 0; i < cPairs; i++)
2278 if ( RTStrSimplePatternMultiMatch(pCur->paPairs[i].pszPattern, RTSTR_MAX,
2279 pszBasename, cchBasename, NULL)
2280 || RTStrSimplePatternMultiMatch(pCur->paPairs[i].pszPattern, RTSTR_MAX,
2281 pszFilename, RTSTR_MAX, NULL))
2282 {
2283 rc = scmSettingsBaseParseString(pBase, pCur->paPairs[i].pszOptions);
2284 if (RT_FAILURE(rc))
2285 break;
2286 }
2287 if (RT_FAILURE(rc))
2288 break;
2289 }
2290
2291 /* advance */
2292 pCur = pCur->pUp;
2293 }
2294 }
2295 if (RT_FAILURE(rc))
2296 scmSettingsBaseDelete(pBase);
2297 return rc;
2298}
2299
2300
2301/* -=-=-=-=-=- misc -=-=-=-=-=- */
2302
2303
2304/**
2305 * Prints a verbose message if the level is high enough.
2306 *
2307 * @param iLevel The required verbosity level.
2308 * @param pszFormat The message format string.
2309 * @param ... Format arguments.
2310 */
2311static void ScmVerbose(int iLevel, const char *pszFormat, ...)
2312{
2313 if (iLevel <= g_iVerbosity)
2314 {
2315 RTPrintf("%s: info: ", g_szProgName);
2316 va_list va;
2317 va_start(va, pszFormat);
2318 RTPrintfV(pszFormat, va);
2319 va_end(va);
2320 }
2321}
2322
2323
2324/* -=-=-=-=-=- rewriters -=-=-=-=-=- */
2325
2326
2327/**
2328 * Strip trailing blanks (space & tab).
2329 *
2330 * @returns True if modified, false if not.
2331 * @param pIn The input stream.
2332 * @param pOut The output stream.
2333 * @param pSettings The settings.
2334 */
2335static bool rewrite_StripTrailingBlanks(PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
2336{
2337 if (!pSettings->fStripTrailingBlanks)
2338 return false;
2339
2340 bool fModified = false;
2341 SCMEOL enmEol;
2342 size_t cchLine;
2343 const char *pchLine;
2344 while ((pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol)) != NULL)
2345 {
2346 int rc;
2347 if ( cchLine == 0
2348 || !RT_C_IS_BLANK(pchLine[cchLine - 1]) )
2349 rc = ScmStreamPutLine(pOut, pchLine, cchLine, enmEol);
2350 else
2351 {
2352 cchLine--;
2353 while (cchLine > 0 && RT_C_IS_BLANK(pchLine[cchLine - 1]))
2354 cchLine--;
2355 rc = ScmStreamPutLine(pOut, pchLine, cchLine, enmEol);
2356 fModified = true;
2357 }
2358 if (RT_FAILURE(rc))
2359 return false;
2360 }
2361 if (fModified)
2362 ScmVerbose(2, " * Stripped trailing blanks\n");
2363 return fModified;
2364}
2365
2366/**
2367 * Expand tabs.
2368 *
2369 * @returns True if modified, false if not.
2370 * @param pIn The input stream.
2371 * @param pOut The output stream.
2372 * @param pSettings The settings.
2373 */
2374static bool rewrite_ExpandTabs(PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
2375{
2376 if (!pSettings->fConvertTabs)
2377 return false;
2378
2379 size_t const cchTab = pSettings->cchTab;
2380 bool fModified = false;
2381 SCMEOL enmEol;
2382 size_t cchLine;
2383 const char *pchLine;
2384 while ((pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol)) != NULL)
2385 {
2386 int rc;
2387 const char *pchTab = (const char *)memchr(pchLine, '\t', cchLine);
2388 if (!pchTab)
2389 rc = ScmStreamPutLine(pOut, pchLine, cchLine, enmEol);
2390 else
2391 {
2392 size_t offTab = 0;
2393 const char *pchChunk = pchLine;
2394 for (;;)
2395 {
2396 size_t cchChunk = pchTab - pchChunk;
2397 offTab += cchChunk;
2398 ScmStreamWrite(pOut, pchChunk, cchChunk);
2399
2400 size_t cchToTab = cchTab - offTab % cchTab;
2401 ScmStreamWrite(pOut, g_szTabSpaces, cchToTab);
2402 offTab += cchToTab;
2403
2404 pchChunk = pchTab + 1;
2405 size_t cchLeft = cchLine - (pchChunk - pchLine);
2406 pchTab = (const char *)memchr(pchChunk, '\t', cchLeft);
2407 if (!pchTab)
2408 {
2409 rc = ScmStreamPutLine(pOut, pchChunk, cchLeft, enmEol);
2410 break;
2411 }
2412 }
2413
2414 fModified = true;
2415 }
2416 if (RT_FAILURE(rc))
2417 return false;
2418 }
2419 if (fModified)
2420 ScmVerbose(2, " * Expanded tabs\n");
2421 return fModified;
2422}
2423
2424/**
2425 * Worker for rewrite_ForceNativeEol, rewrite_ForceLF and rewrite_ForceCRLF.
2426 *
2427 * @returns true if modifications were made, false if not.
2428 * @param pIn The input stream.
2429 * @param pOut The output stream.
2430 * @param pSettings The settings.
2431 * @param enmDesiredEol The desired end of line indicator type.
2432 */
2433static bool rewrite_ForceEol(PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings, SCMEOL enmDesiredEol)
2434{
2435 if (!pSettings->fConvertEol)
2436 return false;
2437
2438 bool fModified = false;
2439 SCMEOL enmEol;
2440 size_t cchLine;
2441 const char *pchLine;
2442 while ((pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol)) != NULL)
2443 {
2444 if ( enmEol != enmDesiredEol
2445 && enmEol != SCMEOL_NONE)
2446 {
2447 fModified = true;
2448 enmEol = enmDesiredEol;
2449 }
2450 int rc = ScmStreamPutLine(pOut, pchLine, cchLine, enmEol);
2451 if (RT_FAILURE(rc))
2452 return false;
2453 }
2454 if (fModified)
2455 ScmVerbose(2, " * Converted EOL markers\n");
2456 return fModified;
2457}
2458
2459/**
2460 * Force native end of line indicator.
2461 *
2462 * @returns true if modifications were made, false if not.
2463 * @param pIn The input stream.
2464 * @param pOut The output stream.
2465 * @param pSettings The settings.
2466 */
2467static bool rewrite_ForceNativeEol(PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
2468{
2469#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
2470 return rewrite_ForceEol(pIn, pOut, pSettings, SCMEOL_CRLF);
2471#else
2472 return rewrite_ForceEol(pIn, pOut, pSettings, SCMEOL_LF);
2473#endif
2474}
2475
2476/**
2477 * Force the stream to use LF as the end of line indicator.
2478 *
2479 * @returns true if modifications were made, false if not.
2480 * @param pIn The input stream.
2481 * @param pOut The output stream.
2482 * @param pSettings The settings.
2483 */
2484static bool rewrite_ForceLF(PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
2485{
2486 return rewrite_ForceEol(pIn, pOut, pSettings, SCMEOL_LF);
2487}
2488
2489/**
2490 * Force the stream to use CRLF as the end of line indicator.
2491 *
2492 * @returns true if modifications were made, false if not.
2493 * @param pIn The input stream.
2494 * @param pOut The output stream.
2495 * @param pSettings The settings.
2496 */
2497static bool rewrite_ForceCRLF(PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
2498{
2499 return rewrite_ForceEol(pIn, pOut, pSettings, SCMEOL_CRLF);
2500}
2501
2502/**
2503 * Strip trailing blank lines and/or make sure there is exactly one blank line
2504 * at the end of the file.
2505 *
2506 * @returns true if modifications were made, false if not.
2507 * @param pIn The input stream.
2508 * @param pOut The output stream.
2509 * @param pSettings The settings.
2510 *
2511 * @remarks ASSUMES trailing white space has been removed already.
2512 */
2513static bool rewrite_AdjustTrailingLines(PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
2514{
2515 if ( !pSettings->fStripTrailingLines
2516 && !pSettings->fForceTrailingLine
2517 && !pSettings->fForceFinalEol)
2518 return false;
2519
2520 size_t const cLines = ScmStreamCountLines(pIn);
2521
2522 /* Empty files remains empty. */
2523 if (cLines <= 1)
2524 return false;
2525
2526 /* Figure out if we need to adjust the number of lines or not. */
2527 size_t cLinesNew = cLines;
2528
2529 if ( pSettings->fStripTrailingLines
2530 && ScmStreamIsWhiteLine(pIn, cLinesNew - 1))
2531 {
2532 while ( cLinesNew > 1
2533 && ScmStreamIsWhiteLine(pIn, cLinesNew - 2))
2534 cLinesNew--;
2535 }
2536
2537 if ( pSettings->fForceTrailingLine
2538 && !ScmStreamIsWhiteLine(pIn, cLinesNew - 1))
2539 cLinesNew++;
2540
2541 bool fFixMissingEol = pSettings->fForceFinalEol
2542 && ScmStreamGetEolByLine(pIn, cLinesNew - 1) == SCMEOL_NONE;
2543
2544 if ( !fFixMissingEol
2545 && cLines == cLinesNew)
2546 return false;
2547
2548 /* Copy the number of lines we've arrived at. */
2549 ScmStreamRewindForReading(pIn);
2550
2551 size_t cCopied = RT_MIN(cLinesNew, cLines);
2552 ScmStreamCopyLines(pOut, pIn, cCopied);
2553
2554 if (cCopied != cLinesNew)
2555 {
2556 while (cCopied++ < cLinesNew)
2557 ScmStreamPutLine(pOut, "", 0, ScmStreamGetEol(pIn));
2558 }
2559 /* Fix missing EOL if required. */
2560 else if (fFixMissingEol)
2561 {
2562 if (ScmStreamGetEol(pIn) == SCMEOL_LF)
2563 ScmStreamWrite(pOut, "\n", 1);
2564 else
2565 ScmStreamWrite(pOut, "\r\n", 2);
2566 }
2567
2568 ScmVerbose(2, " * Adjusted trailing blank lines\n");
2569 return true;
2570}
2571
2572/**
2573 * Makefile.kup are empty files, enforce this.
2574 *
2575 * @returns true if modifications were made, false if not.
2576 * @param pIn The input stream.
2577 * @param pOut The output stream.
2578 * @param pSettings The settings.
2579 */
2580static bool rewrite_Makefile_kup(PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
2581{
2582 /* These files should be zero bytes. */
2583 if (pIn->cb == 0)
2584 return false;
2585 ScmVerbose(2, " * Truncated file to zero bytes\n");
2586 return true;
2587}
2588
2589/**
2590 * Rewrite a kBuild makefile.
2591 *
2592 * @returns true if modifications were made, false if not.
2593 * @param pIn The input stream.
2594 * @param pOut The output stream.
2595 * @param pSettings The settings.
2596 *
2597 * @todo
2598 *
2599 * Ideas for Makefile.kmk and Config.kmk:
2600 * - sort if1of/ifn1of sets.
2601 * - line continuation slashes should only be preceeded by one space.
2602 */
2603static bool rewrite_Makefile_kmk(PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
2604{
2605 return false;
2606}
2607
2608/**
2609 * Rewrite a C/C++ source or header file.
2610 *
2611 * @returns true if modifications were made, false if not.
2612 * @param pIn The input stream.
2613 * @param pOut The output stream.
2614 * @param pSettings The settings.
2615 *
2616 * @todo
2617 *
2618 * Ideas for C/C++:
2619 * - space after if, while, for, switch
2620 * - spaces in for (i=0;i<x;i++)
2621 * - complex conditional, bird style.
2622 * - remove unnecessary parentheses.
2623 * - sort defined RT_OS_*|| and RT_ARCH
2624 * - sizeof without parenthesis.
2625 * - defined without parenthesis.
2626 * - trailing spaces.
2627 * - parameter indentation.
2628 * - space after comma.
2629 * - while (x--); -> multi line + comment.
2630 * - else statement;
2631 * - space between function and left parenthesis.
2632 * - TODO, XXX, @todo cleanup.
2633 * - Space before/after '*'.
2634 * - ensure new line at end of file.
2635 * - Indentation of precompiler statements (#ifdef, #defines).
2636 * - space between functions.
2637 */
2638static bool rewrite_C_and_CPP(PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
2639{
2640
2641 return false;
2642}
2643
2644/* -=-=-=-=-=- file and directory processing -=-=-=-=-=- */
2645
2646/**
2647 * Processes a file.
2648 *
2649 * @returns IPRT status code.
2650 * @param pszFilename The file name.
2651 * @param pszBasename The base name (pointer within @a pszFilename).
2652 * @param cchBasename The length of the base name. (For passing to
2653 * RTStrSimplePatternMultiMatch.)
2654 * @param pBaseSettings The base settings to use. It's OK to modify
2655 * these.
2656 */
2657static int scmProcessFileInner(const char *pszFilename, const char *pszBasename, size_t cchBasename,
2658 PSCMSETTINGSBASE pBaseSettings)
2659{
2660 /*
2661 * Do the file level filtering.
2662 */
2663 if ( g_pszFileFilter
2664 && !RTStrSimplePatternMultiMatch(g_pszFileFilter, RTSTR_MAX, pszBasename, cchBasename, NULL))
2665 {
2666 ScmVerbose(4, "file filter mismatch: \"%s\"\n", pszFilename);
2667 return VINF_SUCCESS;
2668 }
2669 if ( g_pszFileFilterOut
2670 && ( RTStrSimplePatternMultiMatch(g_pszFileFilterOut, RTSTR_MAX, pszBasename, cchBasename, NULL)
2671 || RTStrSimplePatternMultiMatch(g_pszFileFilterOut, RTSTR_MAX, pszFilename, RTSTR_MAX, NULL)) )
2672 {
2673 ScmVerbose(4, "file filter out: \"%s\"\n", pszFilename);
2674 return VINF_SUCCESS;
2675 }
2676
2677 /*
2678 * Try find a matching rewrite config for this filename.
2679 */
2680 PCSCMCFGENTRY pCfg = NULL;
2681 for (size_t iCfg = 0; iCfg < RT_ELEMENTS(g_aConfigs); iCfg++)
2682 if (RTStrSimplePatternMultiMatch(g_aConfigs[iCfg].pszFilePattern, RTSTR_MAX, pszBasename, cchBasename, NULL))
2683 {
2684 pCfg = &g_aConfigs[iCfg];
2685 break;
2686 }
2687 if (!pCfg)
2688 {
2689 ScmVerbose(3, "No rewriters configured for \"%s\"\n", pszFilename);
2690 return VINF_SUCCESS;
2691 }
2692 ScmVerbose(3, "\"%s\" matched \"%s\"\n", pszFilename, pCfg->pszFilePattern);
2693
2694 /*
2695 * Create an input stream from the file and check that it's text.
2696 */
2697 SCMSTREAM Stream1;
2698 int rc = ScmStreamInitForReading(&Stream1, pszFilename);
2699 if (RT_FAILURE(rc))
2700 {
2701 RTMsgError("Failed to read '%s': %Rrc\n", pszFilename, rc);
2702 return rc;
2703 }
2704 if (ScmStreamIsText(&Stream1))
2705 {
2706 ScmVerbose(1, "Processing '%s'...\n", pszFilename);
2707
2708 /*
2709 * Gather SCM and editor settings from the stream.
2710 */
2711 rc = scmSettingsBaseLoadFromDocument(pBaseSettings, &Stream1);
2712 if (RT_SUCCESS(rc))
2713 {
2714 ScmStreamRewindForReading(&Stream1);
2715
2716 /*
2717 * Create two more streams for output and push the text thru all the
2718 * rewriters, switching the two streams around when something is
2719 * actually rewritten. Stream1 remains unchanged.
2720 */
2721 SCMSTREAM Stream2;
2722 rc = ScmStreamInitForWriting(&Stream2, &Stream1);
2723 if (RT_SUCCESS(rc))
2724 {
2725 SCMSTREAM Stream3;
2726 rc = ScmStreamInitForWriting(&Stream3, &Stream1);
2727 if (RT_SUCCESS(rc))
2728 {
2729 bool fModified = false;
2730 PSCMSTREAM pIn = &Stream1;
2731 PSCMSTREAM pOut = &Stream2;
2732 for (size_t iRw = 0; iRw < pCfg->cRewriters; iRw++)
2733 {
2734 bool fRc = pCfg->papfnRewriter[iRw](pIn, pOut, pBaseSettings);
2735 if (fRc)
2736 {
2737 PSCMSTREAM pTmp = pOut;
2738 pOut = pIn == &Stream1 ? &Stream3 : pIn;
2739 pIn = pTmp;
2740 fModified = true;
2741 }
2742 ScmStreamRewindForReading(pIn);
2743 ScmStreamRewindForWriting(pOut);
2744 }
2745
2746 /*
2747 * If rewritten, write it back to disk.
2748 */
2749 if (fModified)
2750 {
2751 if (!g_fDryRun)
2752 {
2753 ScmVerbose(1, "Writing modified file to \"%s%s\"\n", pszFilename, g_pszChangedSuff);
2754 rc = ScmStreamWriteToFile(pIn, "%s%s", pszFilename, g_pszChangedSuff);
2755 if (RT_FAILURE(rc))
2756 RTMsgError("Error writing '%s%s': %Rrc\n", pszFilename, g_pszChangedSuff, rc);
2757 }
2758 else
2759 {
2760 ScmVerbose(1, "Would have modified file \"%s\"\n", pszFilename);
2761 ScmDiffStreams(pszFilename, &Stream1, pIn, g_fDiffIgnoreEol, g_fDiffIgnoreLeadingWS,
2762 g_fDiffIgnoreTrailingWS, g_fDiffSpecialChars, pBaseSettings->cchTab, g_pStdOut);
2763 }
2764 }
2765 else
2766 ScmVerbose(2, "Unchanged \"%s\"\n", pszFilename);
2767
2768 ScmStreamDelete(&Stream3);
2769 }
2770 else
2771 RTMsgError("Failed to init stream for writing: %Rrc\n", rc);
2772 ScmStreamDelete(&Stream2);
2773 }
2774 else
2775 RTMsgError("Failed to init stream for writing: %Rrc\n", rc);
2776 }
2777 else
2778 RTMsgError("scmSettingsBaseLoadFromDocument: %Rrc\n", rc);
2779 }
2780 else
2781 ScmVerbose(3, "not text file: \"%s\"\n", pszFilename);
2782 ScmStreamDelete(&Stream1);
2783
2784 return rc;
2785}
2786
2787/**
2788 * Processes a file.
2789 *
2790 * This is just a wrapper for scmProcessFileInner for avoid wasting stack in the
2791 * directory recursion method.
2792 *
2793 * @returns IPRT status code.
2794 * @param pszFilename The file name.
2795 * @param pszBasename The base name (pointer within @a pszFilename).
2796 * @param cchBasename The length of the base name. (For passing to
2797 * RTStrSimplePatternMultiMatch.)
2798 * @param pSettingsStack The settings stack (pointer to the top element).
2799 */
2800static int scmProcessFile(const char *pszFilename, const char *pszBasename, size_t cchBasename,
2801 PSCMSETTINGS pSettingsStack)
2802{
2803 SCMSETTINGSBASE Base;
2804 int rc = scmSettingsStackMakeFileBase(pSettingsStack, pszFilename, pszBasename, cchBasename, &Base);
2805 if (RT_SUCCESS(rc))
2806 {
2807 rc = scmProcessFileInner(pszFilename, pszBasename, cchBasename, &Base);
2808 scmSettingsBaseDelete(&Base);
2809 }
2810 return rc;
2811}
2812
2813
2814/**
2815 * Tries to correct RTDIRENTRY_UNKNOWN.
2816 *
2817 * @returns Corrected type.
2818 * @param pszPath The path to the object in question.
2819 */
2820static RTDIRENTRYTYPE scmFigureUnknownType(const char *pszPath)
2821{
2822 RTFSOBJINFO Info;
2823 int rc = RTPathQueryInfo(pszPath, &Info, RTFSOBJATTRADD_NOTHING);
2824 if (RT_FAILURE(rc))
2825 return RTDIRENTRYTYPE_UNKNOWN;
2826 if (RTFS_IS_DIRECTORY(Info.Attr.fMode))
2827 return RTDIRENTRYTYPE_DIRECTORY;
2828 if (RTFS_IS_FILE(Info.Attr.fMode))
2829 return RTDIRENTRYTYPE_FILE;
2830 return RTDIRENTRYTYPE_UNKNOWN;
2831}
2832
2833/**
2834 * Recurse into a sub-directory and process all the files and directories.
2835 *
2836 * @returns IPRT status code.
2837 * @param pszBuf Path buffer containing the directory path on
2838 * entry. This ends with a dot. This is passed
2839 * along when recusing in order to save stack space
2840 * and avoid needless copying.
2841 * @param cchDir Length of our path in pszbuf.
2842 * @param pEntry Directory entry buffer. This is also passed
2843 * along when recursing to save stack space.
2844 * @param pSettingsStack The settings stack (pointer to the top element).
2845 * @param iRecursion The recursion depth. This is used to restrict
2846 * the recursions.
2847 */
2848static int scmProcessDirTreeRecursion(char *pszBuf, size_t cchDir, PRTDIRENTRY pEntry,
2849 PSCMSETTINGS pSettingsStack, unsigned iRecursion)
2850{
2851 Assert(cchDir > 1 && pszBuf[cchDir - 1] == '.');
2852
2853 /*
2854 * Make sure we stop somewhere.
2855 */
2856 if (iRecursion > 128)
2857 {
2858 RTMsgError("recursion too deep: %d\n", iRecursion);
2859 return VINF_SUCCESS; /* ignore */
2860 }
2861
2862 /*
2863 * Try open and read the directory.
2864 */
2865 PRTDIR pDir;
2866 int rc = RTDirOpenFiltered(&pDir, pszBuf, RTDIRFILTER_NONE);
2867 if (RT_FAILURE(rc))
2868 {
2869 RTMsgError("Failed to enumerate directory '%s': %Rrc", pszBuf, rc);
2870 return rc;
2871 }
2872 for (;;)
2873 {
2874 /* Read the next entry. */
2875 rc = RTDirRead(pDir, pEntry, NULL);
2876 if (RT_FAILURE(rc) && rc != VERR_NO_MORE_FILES)
2877 RTMsgError("RTDirRead -> %Rrc\n", rc);
2878 if (RT_FAILURE(rc))
2879 break;
2880
2881 /* Skip '.' and '..'. */
2882 if ( pEntry->szName[0] == '.'
2883 && ( pEntry->cbName == 1
2884 || ( pEntry->cbName == 2
2885 && pEntry->szName[1] == '.')))
2886 continue;
2887
2888 /* Enter it into the buffer so we've got a full name to work
2889 with when needed. */
2890 if (pEntry->cbName + cchDir >= RTPATH_MAX)
2891 {
2892 RTMsgError("Skipping too long entry: %s", pEntry->szName);
2893 continue;
2894 }
2895 memcpy(&pszBuf[cchDir - 1], pEntry->szName, pEntry->cbName + 1);
2896
2897 /* Figure the type. */
2898 RTDIRENTRYTYPE enmType = pEntry->enmType;
2899 if (enmType == RTDIRENTRYTYPE_UNKNOWN)
2900 enmType = scmFigureUnknownType(pszBuf);
2901
2902 /* Process the file or directory, skip the rest. */
2903 if (enmType == RTDIRENTRYTYPE_FILE)
2904 rc = scmProcessFile(pszBuf, pEntry->szName, pEntry->cbName, pSettingsStack);
2905 else if (enmType == RTDIRENTRYTYPE_DIRECTORY)
2906 {
2907 /* Append the dot for the benefit of the pattern matching. */
2908 if (pEntry->cbName + cchDir + 5 >= RTPATH_MAX)
2909 {
2910 RTMsgError("Skipping too deep dir entry: %s", pEntry->szName);
2911 continue;
2912 }
2913 memcpy(&pszBuf[cchDir - 1 + pEntry->cbName], "/.", sizeof("/."));
2914 size_t cchSubDir = cchDir - 1 + pEntry->cbName + sizeof("/.") - 1;
2915
2916 if ( ( g_pszDirFilter == NULL
2917 || RTStrSimplePatternMultiMatch(g_pszDirFilter, RTSTR_MAX,
2918 pEntry->szName, pEntry->cbName, NULL))
2919 && ( g_pszDirFilterOut == NULL
2920 || ( !RTStrSimplePatternMultiMatch(g_pszDirFilterOut, RTSTR_MAX,
2921 pEntry->szName, pEntry->cbName, NULL)
2922 && !RTStrSimplePatternMultiMatch(g_pszDirFilterOut, RTSTR_MAX,
2923 pszBuf, cchSubDir, NULL)
2924 )
2925 )
2926 )
2927 {
2928 rc = scmSettingsStackPushDir(&pSettingsStack, pszBuf);
2929 if (RT_SUCCESS(rc))
2930 {
2931 rc = scmProcessDirTreeRecursion(pszBuf, cchSubDir, pEntry, pSettingsStack, iRecursion + 1);
2932 scmSettingsStackPopAndDestroy(&pSettingsStack);
2933 }
2934 }
2935 }
2936 if (RT_FAILURE(rc))
2937 break;
2938 }
2939 RTDirClose(pDir);
2940 return RT_SUCCESS(rc) ? 0 : 1;
2941
2942}
2943
2944/**
2945 * Process a directory tree.
2946 *
2947 * @returns IPRT status code.
2948 * @param pszDir The directory to start with. This is pointer to
2949 * a RTPATH_MAX sized buffer.
2950 */
2951static int scmProcessDirTree(char *pszDir, PSCMSETTINGS pSettingsStack)
2952{
2953 /*
2954 * Setup the recursion.
2955 */
2956 int rc = RTPathAppend(pszDir, RTPATH_MAX, ".");
2957 if (RT_SUCCESS(rc))
2958 {
2959 RTDIRENTRY Entry;
2960 rc = scmProcessDirTreeRecursion(pszDir, strlen(pszDir), &Entry, pSettingsStack, 0);
2961 }
2962 else
2963 RTMsgError("RTPathAppend: %Rrc\n", rc);
2964 return rc;
2965}
2966
2967
2968/**
2969 * Processes a file or directory specified as an command line argument.
2970 *
2971 * @returns IPRT status code
2972 * @param pszSomething What we found in the commad line arguments.
2973 * @param pSettingsStack The settings stack (pointer to the top element).
2974 */
2975static int scmProcessSomething(const char *pszSomething, PSCMSETTINGS pSettingsStack)
2976{
2977 char szBuf[RTPATH_MAX];
2978 int rc = RTPathAbs(pszSomething, szBuf, sizeof(szBuf));
2979 if (RT_SUCCESS(rc))
2980 {
2981 PSCMSETTINGS pSettings;
2982 rc = scmSettingsCreateForPath(&pSettings, &pSettingsStack->Base, szBuf);
2983 if (RT_SUCCESS(rc))
2984 {
2985 scmSettingsStackPush(&pSettingsStack, pSettings);
2986
2987 if (RTFileExists(szBuf))
2988 {
2989 const char *pszBasename = RTPathFilename(szBuf);
2990 if (pszBasename)
2991 {
2992 size_t cchBasename = strlen(pszBasename);
2993 rc = scmProcessFile(szBuf, pszBasename, cchBasename, pSettingsStack);
2994 }
2995 else
2996 {
2997 RTMsgError("RTPathFilename: NULL\n");
2998 rc = VERR_IS_A_DIRECTORY;
2999 }
3000 }
3001 else
3002 rc = scmProcessDirTree(szBuf, pSettingsStack);
3003
3004 PSCMSETTINGS pPopped = scmSettingsStackPop(&pSettingsStack);
3005 Assert(pPopped == pSettings);
3006 scmSettingsDestroy(pSettings);
3007 }
3008 else
3009 RTMsgError("scmSettingsInitStack: %Rrc\n", rc);
3010 }
3011 else
3012 RTMsgError("RTPathAbs: %Rrc\n", rc);
3013 return rc;
3014}
3015
3016int main(int argc, char **argv)
3017{
3018 int rc = RTR3Init();
3019 if (RT_FAILURE(rc))
3020 return 1;
3021
3022 /*
3023 * Init the settings.
3024 */
3025 PSCMSETTINGS pSettings;
3026 rc = scmSettingsCreate(&pSettings, &g_Defaults);
3027 if (RT_FAILURE(rc))
3028 {
3029 RTMsgError("scmSettingsCreate: %Rrc\n", rc);
3030 return 1;
3031 }
3032
3033 /*
3034 * Parse arguments and process input in order (because this is the only
3035 * thing that works at the moment).
3036 */
3037 static RTGETOPTDEF s_aOpts[16 + RT_ELEMENTS(g_aScmOpts)] =
3038 {
3039 { "--dry-run", 'd', RTGETOPT_REQ_NOTHING },
3040 { "--real-run", 'D', RTGETOPT_REQ_NOTHING },
3041 { "--file-filter", 'f', RTGETOPT_REQ_STRING },
3042 { "--help", 'h', RTGETOPT_REQ_NOTHING },
3043 { "--quiet", 'q', RTGETOPT_REQ_NOTHING },
3044 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
3045 { "--version", 'V', RTGETOPT_REQ_NOTHING },
3046 { "--diff-ignore-eol", SCMOPT_DIFF_IGNORE_EOL, RTGETOPT_REQ_NOTHING },
3047 { "--diff-no-ignore-eol", SCMOPT_DIFF_NO_IGNORE_EOL, RTGETOPT_REQ_NOTHING },
3048 { "--diff-ignore-space", SCMOPT_DIFF_IGNORE_SPACE, RTGETOPT_REQ_NOTHING },
3049 { "--diff-no-ignore-space", SCMOPT_DIFF_NO_IGNORE_SPACE, RTGETOPT_REQ_NOTHING },
3050 { "--diff-ignore-leading-space", SCMOPT_DIFF_IGNORE_LEADING_SPACE, RTGETOPT_REQ_NOTHING },
3051 { "--diff-no-ignore-leading-space", SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE, RTGETOPT_REQ_NOTHING },
3052 { "--diff-ignore-trailing-space", SCMOPT_DIFF_IGNORE_TRAILING_SPACE, RTGETOPT_REQ_NOTHING },
3053 { "--diff-no-ignore-trailing-space", SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE, RTGETOPT_REQ_NOTHING },
3054 { "--diff-special-chars", SCMOPT_DIFF_SPECIAL_CHARS, RTGETOPT_REQ_NOTHING },
3055 { "--diff-no-special-chars", SCMOPT_DIFF_NO_SPECIAL_CHARS, RTGETOPT_REQ_NOTHING },
3056 };
3057 memcpy(&s_aOpts[RT_ELEMENTS(s_aOpts) - RT_ELEMENTS(g_aScmOpts)], &g_aScmOpts[0], sizeof(g_aScmOpts));
3058
3059 RTGETOPTUNION ValueUnion;
3060 RTGETOPTSTATE GetOptState;
3061 rc = RTGetOptInit(&GetOptState, argc, argv, &s_aOpts[0], RT_ELEMENTS(s_aOpts), 1, 0 /*fFlags*/);
3062 AssertReleaseRCReturn(rc, 1);
3063 size_t cProcessed = 0;
3064
3065 while ((rc = RTGetOpt(&GetOptState, &ValueUnion)) != 0)
3066 {
3067 switch (rc)
3068 {
3069 case 'd':
3070 g_fDryRun = true;
3071 break;
3072
3073 case 'D':
3074 g_fDryRun = false;
3075 break;
3076
3077 case 'f':
3078 g_pszFileFilter = ValueUnion.psz;
3079 break;
3080
3081 case 'h':
3082 RTPrintf("Source Code Massager\n"
3083 "\n"
3084 "Usage: %s [options] <files & dirs>\n"
3085 "\n"
3086 "Options:\n", g_szProgName);
3087 for (size_t i = 0; i < RT_ELEMENTS(s_aOpts); i++)
3088 if ((s_aOpts[i].fFlags & RTGETOPT_REQ_MASK) == RTGETOPT_REQ_NOTHING)
3089 RTPrintf(" %s\n", s_aOpts[i].pszLong);
3090 else if ((s_aOpts[i].fFlags & RTGETOPT_REQ_MASK) == RTGETOPT_REQ_STRING)
3091 RTPrintf(" %s string\n", s_aOpts[i].pszLong);
3092 else
3093 RTPrintf(" %s value\n", s_aOpts[i].pszLong);
3094 return 1;
3095
3096 case 'q':
3097 g_iVerbosity = 0;
3098 break;
3099
3100 case 'v':
3101 g_iVerbosity++;
3102 break;
3103
3104 case 'V':
3105 {
3106 /* The following is assuming that svn does it's job here. */
3107 static const char s_szRev[] = "$Revision: 26477 $";
3108 const char *psz = RTStrStripL(strchr(s_szRev, ' '));
3109 RTPrintf("r%.*s\n", strchr(psz, ' ') - psz, psz);
3110 return 0;
3111 }
3112
3113 case SCMOPT_DIFF_IGNORE_EOL:
3114 g_fDiffIgnoreEol = true;
3115 break;
3116 case SCMOPT_DIFF_NO_IGNORE_EOL:
3117 g_fDiffIgnoreEol = false;
3118 break;
3119
3120 case SCMOPT_DIFF_IGNORE_SPACE:
3121 g_fDiffIgnoreTrailingWS = g_fDiffIgnoreLeadingWS = true;
3122 break;
3123 case SCMOPT_DIFF_NO_IGNORE_SPACE:
3124 g_fDiffIgnoreTrailingWS = g_fDiffIgnoreLeadingWS = false;
3125 break;
3126
3127 case SCMOPT_DIFF_IGNORE_LEADING_SPACE:
3128 g_fDiffIgnoreLeadingWS = true;
3129 break;
3130 case SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE:
3131 g_fDiffIgnoreLeadingWS = false;
3132 break;
3133
3134 case SCMOPT_DIFF_IGNORE_TRAILING_SPACE:
3135 g_fDiffIgnoreTrailingWS = true;
3136 break;
3137 case SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE:
3138 g_fDiffIgnoreTrailingWS = false;
3139 break;
3140
3141 case SCMOPT_DIFF_SPECIAL_CHARS:
3142 g_fDiffSpecialChars = true;
3143 break;
3144 case SCMOPT_DIFF_NO_SPECIAL_CHARS:
3145 g_fDiffSpecialChars = false;
3146 break;
3147
3148 case VINF_GETOPT_NOT_OPTION:
3149 {
3150 if (!g_fDryRun)
3151 {
3152 if (!cProcessed)
3153 {
3154 RTPrintf("%s: Warning! This program will make changes to your source files and\n"
3155 "%s: there is a slight risk that bugs or a full disk may cause\n"
3156 "%s: LOSS OF DATA. So, please make sure you have checked in\n"
3157 "%s: all your changes already. If you didn't, then don't blame\n"
3158 "%s: anyone for not warning you!\n"
3159 "%s:\n"
3160 "%s: Press any key to continue...\n",
3161 g_szProgName, g_szProgName, g_szProgName, g_szProgName, g_szProgName,
3162 g_szProgName, g_szProgName);
3163 RTStrmGetCh(g_pStdIn);
3164 }
3165 cProcessed++;
3166 }
3167 rc = scmProcessSomething(ValueUnion.psz, pSettings);
3168 if (RT_FAILURE(rc))
3169 return rc;
3170 break;
3171 }
3172
3173 default:
3174 {
3175 int rc2 = scmSettingsBaseHandleOpt(&pSettings->Base, rc, &ValueUnion);
3176 if (RT_SUCCESS(rc2))
3177 break;
3178 if (rc2 != VERR_GETOPT_UNKNOWN_OPTION)
3179 return 2;
3180 return RTGetOptPrintError(rc, &ValueUnion);
3181 }
3182 }
3183 }
3184
3185 scmSettingsDestroy(pSettings);
3186 return 0;
3187}
3188
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