1 | /* $Id: scm.cpp 28767 2010-04-26 16:34:05Z 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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | /*******************************************************************************
|
---|
23 | * Header Files *
|
---|
24 | *******************************************************************************/
|
---|
25 | #include <iprt/assert.h>
|
---|
26 | #include <iprt/ctype.h>
|
---|
27 | #include <iprt/dir.h>
|
---|
28 | #include <iprt/env.h>
|
---|
29 | #include <iprt/file.h>
|
---|
30 | #include <iprt/err.h>
|
---|
31 | #include <iprt/getopt.h>
|
---|
32 | #include <iprt/initterm.h>
|
---|
33 | #include <iprt/mem.h>
|
---|
34 | #include <iprt/message.h>
|
---|
35 | #include <iprt/param.h>
|
---|
36 | #include <iprt/path.h>
|
---|
37 | #include <iprt/process.h>
|
---|
38 | #include <iprt/stream.h>
|
---|
39 | #include <iprt/string.h>
|
---|
40 |
|
---|
41 |
|
---|
42 | /*******************************************************************************
|
---|
43 | * Defined Constants And Macros *
|
---|
44 | *******************************************************************************/
|
---|
45 | /** The name of the settings files. */
|
---|
46 | #define SCM_SETTINGS_FILENAME ".scm-settings"
|
---|
47 |
|
---|
48 |
|
---|
49 | /*******************************************************************************
|
---|
50 | * Structures and Typedefs *
|
---|
51 | *******************************************************************************/
|
---|
52 | /** Pointer to const massager settings. */
|
---|
53 | typedef struct SCMSETTINGSBASE const *PCSCMSETTINGSBASE;
|
---|
54 |
|
---|
55 | /** End of line marker type. */
|
---|
56 | typedef enum SCMEOL
|
---|
57 | {
|
---|
58 | SCMEOL_NONE = 0,
|
---|
59 | SCMEOL_LF = 1,
|
---|
60 | SCMEOL_CRLF = 2
|
---|
61 | } SCMEOL;
|
---|
62 | /** Pointer to an end of line marker type. */
|
---|
63 | typedef SCMEOL *PSCMEOL;
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Line record.
|
---|
67 | */
|
---|
68 | typedef struct SCMSTREAMLINE
|
---|
69 | {
|
---|
70 | /** The offset of the line. */
|
---|
71 | size_t off;
|
---|
72 | /** The line length, excluding the LF character.
|
---|
73 | * @todo This could be derived from the offset of the next line if that wasn't
|
---|
74 | * so tedious. */
|
---|
75 | size_t cch;
|
---|
76 | /** The end of line marker type. */
|
---|
77 | SCMEOL enmEol;
|
---|
78 | } SCMSTREAMLINE;
|
---|
79 | /** Pointer to a line record. */
|
---|
80 | typedef SCMSTREAMLINE *PSCMSTREAMLINE;
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Source code massager stream.
|
---|
84 | */
|
---|
85 | typedef struct SCMSTREAM
|
---|
86 | {
|
---|
87 | /** Pointer to the file memory. */
|
---|
88 | char *pch;
|
---|
89 | /** The current stream position. */
|
---|
90 | size_t off;
|
---|
91 | /** The current stream size. */
|
---|
92 | size_t cb;
|
---|
93 | /** The size of the memory pb points to. */
|
---|
94 | size_t cbAllocated;
|
---|
95 |
|
---|
96 | /** Line records. */
|
---|
97 | PSCMSTREAMLINE paLines;
|
---|
98 | /** The current line. */
|
---|
99 | size_t iLine;
|
---|
100 | /** The current stream size given in lines. */
|
---|
101 | size_t cLines;
|
---|
102 | /** The sizeof the the memory backing paLines. */
|
---|
103 | size_t cLinesAllocated;
|
---|
104 |
|
---|
105 | /** Set if write-only, clear if read-only. */
|
---|
106 | bool fWriteOrRead;
|
---|
107 | /** Set if the memory pb points to is from RTFileReadAll. */
|
---|
108 | bool fFileMemory;
|
---|
109 | /** Set if fully broken into lines. */
|
---|
110 | bool fFullyLineated;
|
---|
111 |
|
---|
112 | /** Stream status code (IPRT). */
|
---|
113 | int rc;
|
---|
114 | } SCMSTREAM;
|
---|
115 | /** Pointer to a SCM stream. */
|
---|
116 | typedef SCMSTREAM *PSCMSTREAM;
|
---|
117 | /** Pointer to a const SCM stream. */
|
---|
118 | typedef SCMSTREAM const *PCSCMSTREAM;
|
---|
119 |
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * SVN property.
|
---|
123 | */
|
---|
124 | typedef struct SCMSVNPROP
|
---|
125 | {
|
---|
126 | /** The property. */
|
---|
127 | char *pszName;
|
---|
128 | /** The value.
|
---|
129 | * When used to record updates, this can be set to NULL to trigger the
|
---|
130 | * deletion of the property. */
|
---|
131 | char *pszValue;
|
---|
132 | } SCMSVNPROP;
|
---|
133 | /** Pointer to a SVN property. */
|
---|
134 | typedef SCMSVNPROP *PSCMSVNPROP;
|
---|
135 | /** Pointer to a const SVN property. */
|
---|
136 | typedef SCMSVNPROP const *PCSCMSVNPROP;
|
---|
137 |
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Rewriter state.
|
---|
141 | */
|
---|
142 | typedef struct SCMRWSTATE
|
---|
143 | {
|
---|
144 | /** The filename. */
|
---|
145 | const char *pszFilename;
|
---|
146 | /** Set after the printing the first verbose message about a file under
|
---|
147 | * rewrite. */
|
---|
148 | bool fFirst;
|
---|
149 | /** The number of SVN property changes. */
|
---|
150 | size_t cSvnPropChanges;
|
---|
151 | /** Pointer to an array of SVN property changes. */
|
---|
152 | PSCMSVNPROP paSvnPropChanges;
|
---|
153 | } SCMRWSTATE;
|
---|
154 | /** Pointer to the rewriter state. */
|
---|
155 | typedef SCMRWSTATE *PSCMRWSTATE;
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * A rewriter.
|
---|
159 | *
|
---|
160 | * This works like a stream editor, reading @a pIn, modifying it and writing it
|
---|
161 | * to @a pOut.
|
---|
162 | *
|
---|
163 | * @returns true if any changes were made, false if not.
|
---|
164 | * @param pIn The input stream.
|
---|
165 | * @param pOut The output stream.
|
---|
166 | * @param pSettings The settings.
|
---|
167 | */
|
---|
168 | typedef bool (*PFNSCMREWRITER)(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
|
---|
169 |
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Configuration entry.
|
---|
173 | */
|
---|
174 | typedef struct SCMCFGENTRY
|
---|
175 | {
|
---|
176 | /** Number of rewriters. */
|
---|
177 | size_t cRewriters;
|
---|
178 | /** Pointer to an array of rewriters. */
|
---|
179 | PFNSCMREWRITER const *papfnRewriter;
|
---|
180 | /** File pattern (simple). */
|
---|
181 | const char *pszFilePattern;
|
---|
182 | } SCMCFGENTRY;
|
---|
183 | typedef SCMCFGENTRY *PSCMCFGENTRY;
|
---|
184 | typedef SCMCFGENTRY const *PCSCMCFGENTRY;
|
---|
185 |
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Diff state.
|
---|
189 | */
|
---|
190 | typedef struct SCMDIFFSTATE
|
---|
191 | {
|
---|
192 | size_t cDiffs;
|
---|
193 | const char *pszFilename;
|
---|
194 |
|
---|
195 | PSCMSTREAM pLeft;
|
---|
196 | PSCMSTREAM pRight;
|
---|
197 |
|
---|
198 | /** Whether to ignore end of line markers when diffing. */
|
---|
199 | bool fIgnoreEol;
|
---|
200 | /** Whether to ignore trailing whitespace. */
|
---|
201 | bool fIgnoreTrailingWhite;
|
---|
202 | /** Whether to ignore leading whitespace. */
|
---|
203 | bool fIgnoreLeadingWhite;
|
---|
204 | /** Whether to print special characters in human readable form or not. */
|
---|
205 | bool fSpecialChars;
|
---|
206 | /** The tab size. */
|
---|
207 | size_t cchTab;
|
---|
208 | /** Where to push the diff. */
|
---|
209 | PRTSTREAM pDiff;
|
---|
210 | } SCMDIFFSTATE;
|
---|
211 | /** Pointer to a diff state. */
|
---|
212 | typedef SCMDIFFSTATE *PSCMDIFFSTATE;
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Source Code Massager Settings.
|
---|
216 | */
|
---|
217 | typedef struct SCMSETTINGSBASE
|
---|
218 | {
|
---|
219 | bool fConvertEol;
|
---|
220 | bool fConvertTabs;
|
---|
221 | bool fForceFinalEol;
|
---|
222 | bool fForceTrailingLine;
|
---|
223 | bool fStripTrailingBlanks;
|
---|
224 | bool fStripTrailingLines;
|
---|
225 | /** Only process files that are part of a SVN working copy. */
|
---|
226 | bool fOnlySvnFiles;
|
---|
227 | /** Only recurse into directories containing an .svn dir. */
|
---|
228 | bool fOnlySvnDirs;
|
---|
229 | /** Set svn:eol-style if missing or incorrect. */
|
---|
230 | bool fSetSvnEol;
|
---|
231 | /** Set svn:executable according to type (unually this means deleting it). */
|
---|
232 | bool fSetSvnExecutable;
|
---|
233 | /** Set svn:keyword if completely or partially missing. */
|
---|
234 | bool fSetSvnKeywords;
|
---|
235 | /** */
|
---|
236 | unsigned cchTab;
|
---|
237 | /** Only consider files matcihng these patterns. This is only applied to the
|
---|
238 | * base names. */
|
---|
239 | char *pszFilterFiles;
|
---|
240 | /** Filter out files matching the following patterns. This is applied to base
|
---|
241 | * names as well as the aboslute paths. */
|
---|
242 | char *pszFilterOutFiles;
|
---|
243 | /** Filter out directories matching the following patterns. This is applied
|
---|
244 | * to base names as well as the aboslute paths. All absolute paths ends with a
|
---|
245 | * slash and dot ("/."). */
|
---|
246 | char *pszFilterOutDirs;
|
---|
247 | } SCMSETTINGSBASE;
|
---|
248 | /** Pointer to massager settings. */
|
---|
249 | typedef SCMSETTINGSBASE *PSCMSETTINGSBASE;
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * Option identifiers.
|
---|
253 | *
|
---|
254 | * @note The first chunk, down to SCMOPT_TAB_SIZE, are alternately set &
|
---|
255 | * clear. So, the option setting a flag (boolean) will have an even
|
---|
256 | * number and the one clearing it will have an odd number.
|
---|
257 | * @note Down to SCMOPT_LAST_SETTINGS corresponds exactly to SCMSETTINGSBASE.
|
---|
258 | */
|
---|
259 | typedef enum SCMOPT
|
---|
260 | {
|
---|
261 | SCMOPT_CONVERT_EOL = 10000,
|
---|
262 | SCMOPT_NO_CONVERT_EOL,
|
---|
263 | SCMOPT_CONVERT_TABS,
|
---|
264 | SCMOPT_NO_CONVERT_TABS,
|
---|
265 | SCMOPT_FORCE_FINAL_EOL,
|
---|
266 | SCMOPT_NO_FORCE_FINAL_EOL,
|
---|
267 | SCMOPT_FORCE_TRAILING_LINE,
|
---|
268 | SCMOPT_NO_FORCE_TRAILING_LINE,
|
---|
269 | SCMOPT_STRIP_TRAILING_BLANKS,
|
---|
270 | SCMOPT_NO_STRIP_TRAILING_BLANKS,
|
---|
271 | SCMOPT_STRIP_TRAILING_LINES,
|
---|
272 | SCMOPT_NO_STRIP_TRAILING_LINES,
|
---|
273 | SCMOPT_ONLY_SVN_DIRS,
|
---|
274 | SCMOPT_NOT_ONLY_SVN_DIRS,
|
---|
275 | SCMOPT_ONLY_SVN_FILES,
|
---|
276 | SCMOPT_NOT_ONLY_SVN_FILES,
|
---|
277 | SCMOPT_SET_SVN_EOL,
|
---|
278 | SCMOPT_DONT_SET_SVN_EOL,
|
---|
279 | SCMOPT_SET_SVN_EXECUTABLE,
|
---|
280 | SCMOPT_DONT_SET_SVN_EXECUTABLE,
|
---|
281 | SCMOPT_SET_SVN_KEYWORDS,
|
---|
282 | SCMOPT_DONT_SET_SVN_KEYWORDS,
|
---|
283 | SCMOPT_TAB_SIZE,
|
---|
284 | SCMOPT_FILTER_OUT_DIRS,
|
---|
285 | SCMOPT_FILTER_FILES,
|
---|
286 | SCMOPT_FILTER_OUT_FILES,
|
---|
287 | SCMOPT_LAST_SETTINGS = SCMOPT_FILTER_OUT_FILES,
|
---|
288 | //
|
---|
289 | SCMOPT_DIFF_IGNORE_EOL,
|
---|
290 | SCMOPT_DIFF_NO_IGNORE_EOL,
|
---|
291 | SCMOPT_DIFF_IGNORE_SPACE,
|
---|
292 | SCMOPT_DIFF_NO_IGNORE_SPACE,
|
---|
293 | SCMOPT_DIFF_IGNORE_LEADING_SPACE,
|
---|
294 | SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE,
|
---|
295 | SCMOPT_DIFF_IGNORE_TRAILING_SPACE,
|
---|
296 | SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE,
|
---|
297 | SCMOPT_DIFF_SPECIAL_CHARS,
|
---|
298 | SCMOPT_DIFF_NO_SPECIAL_CHARS,
|
---|
299 | SCMOPT_END
|
---|
300 | } SCMOPT;
|
---|
301 |
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * File/dir pattern + options.
|
---|
305 | */
|
---|
306 | typedef struct SCMPATRNOPTPAIR
|
---|
307 | {
|
---|
308 | char *pszPattern;
|
---|
309 | char *pszOptions;
|
---|
310 | } SCMPATRNOPTPAIR;
|
---|
311 | /** Pointer to a pattern + option pair. */
|
---|
312 | typedef SCMPATRNOPTPAIR *PSCMPATRNOPTPAIR;
|
---|
313 |
|
---|
314 |
|
---|
315 | /** Pointer to a settings set. */
|
---|
316 | typedef struct SCMSETTINGS *PSCMSETTINGS;
|
---|
317 | /**
|
---|
318 | * Settings set.
|
---|
319 | *
|
---|
320 | * This structure is constructed from the command line arguments or any
|
---|
321 | * .scm-settings file found in a directory we recurse into. When recusing in
|
---|
322 | * and out of a directory, we push and pop a settings set for it.
|
---|
323 | *
|
---|
324 | * The .scm-settings file has two kinds of setttings, first there are the
|
---|
325 | * unqualified base settings and then there are the settings which applies to a
|
---|
326 | * set of files or directories. The former are lines with command line options.
|
---|
327 | * For the latter, the options are preceeded by a string pattern and a colon.
|
---|
328 | * The pattern specifies which files (and/or directories) the options applies
|
---|
329 | * to.
|
---|
330 | *
|
---|
331 | * We parse the base options into the Base member and put the others into the
|
---|
332 | * paPairs array.
|
---|
333 | */
|
---|
334 | typedef struct SCMSETTINGS
|
---|
335 | {
|
---|
336 | /** Pointer to the setting file below us in the stack. */
|
---|
337 | PSCMSETTINGS pDown;
|
---|
338 | /** Pointer to the setting file above us in the stack. */
|
---|
339 | PSCMSETTINGS pUp;
|
---|
340 | /** File/dir patterns and their options. */
|
---|
341 | PSCMPATRNOPTPAIR paPairs;
|
---|
342 | /** The number of entires in paPairs. */
|
---|
343 | uint32_t cPairs;
|
---|
344 | /** The base settings that was read out of the file. */
|
---|
345 | SCMSETTINGSBASE Base;
|
---|
346 | } SCMSETTINGS;
|
---|
347 | /** Pointer to a const settings set. */
|
---|
348 | typedef SCMSETTINGS const *PCSCMSETTINGS;
|
---|
349 |
|
---|
350 |
|
---|
351 | /*******************************************************************************
|
---|
352 | * Internal Functions *
|
---|
353 | *******************************************************************************/
|
---|
354 | static bool rewrite_StripTrailingBlanks(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
|
---|
355 | static bool rewrite_ExpandTabs(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
|
---|
356 | static bool rewrite_ForceNativeEol(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
|
---|
357 | static bool rewrite_ForceLF(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
|
---|
358 | static bool rewrite_ForceCRLF(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
|
---|
359 | static bool rewrite_AdjustTrailingLines(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
|
---|
360 | static bool rewrite_SvnNoExecutable(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
|
---|
361 | static bool rewrite_SvnKeywords(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
|
---|
362 | static bool rewrite_Makefile_kup(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
|
---|
363 | static bool rewrite_Makefile_kmk(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
|
---|
364 | static bool rewrite_C_and_CPP(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);
|
---|
365 |
|
---|
366 |
|
---|
367 | /*******************************************************************************
|
---|
368 | * Global Variables *
|
---|
369 | *******************************************************************************/
|
---|
370 | static const char g_szProgName[] = "scm";
|
---|
371 | static const char *g_pszChangedSuff = "";
|
---|
372 | static const char g_szTabSpaces[16+1] = " ";
|
---|
373 | static bool g_fDryRun = true;
|
---|
374 | static bool g_fDiffSpecialChars = true;
|
---|
375 | static bool g_fDiffIgnoreEol = false;
|
---|
376 | static bool g_fDiffIgnoreLeadingWS = false;
|
---|
377 | static bool g_fDiffIgnoreTrailingWS = false;
|
---|
378 | static int g_iVerbosity = 2;//99; //0;
|
---|
379 |
|
---|
380 | /** The global settings. */
|
---|
381 | static SCMSETTINGSBASE const g_Defaults =
|
---|
382 | {
|
---|
383 | /* .fConvertEol = */ true,
|
---|
384 | /* .fConvertTabs = */ true,
|
---|
385 | /* .fForceFinalEol = */ true,
|
---|
386 | /* .fForceTrailingLine = */ false,
|
---|
387 | /* .fStripTrailingBlanks = */ true,
|
---|
388 | /* .fStripTrailingLines = */ true,
|
---|
389 | /* .fOnlySvnFiles = */ false,
|
---|
390 | /* .fOnlySvnDirs = */ false,
|
---|
391 | /* .fSetSvnEol = */ false,
|
---|
392 | /* .fSetSvnExecutable = */ false,
|
---|
393 | /* .fSetSvnKeywords = */ false,
|
---|
394 | /* .cchTab = */ 8,
|
---|
395 | /* .pszFilterFiles = */ (char *)"",
|
---|
396 | /* .pszFilterOutFiles = */ (char *)"*.exe|*.com|20*-*-*.log",
|
---|
397 | /* .pszFilterOutDirs = */ (char *)".svn|.hg|.git|CVS",
|
---|
398 | };
|
---|
399 |
|
---|
400 | /** Option definitions for the base settings. */
|
---|
401 | static RTGETOPTDEF g_aScmOpts[] =
|
---|
402 | {
|
---|
403 | { "--convert-eol", SCMOPT_CONVERT_EOL, RTGETOPT_REQ_NOTHING },
|
---|
404 | { "--no-convert-eol", SCMOPT_NO_CONVERT_EOL, RTGETOPT_REQ_NOTHING },
|
---|
405 | { "--convert-tabs", SCMOPT_CONVERT_TABS, RTGETOPT_REQ_NOTHING },
|
---|
406 | { "--no-convert-tabs", SCMOPT_NO_CONVERT_TABS, RTGETOPT_REQ_NOTHING },
|
---|
407 | { "--force-final-eol", SCMOPT_FORCE_FINAL_EOL, RTGETOPT_REQ_NOTHING },
|
---|
408 | { "--no-force-final-eol", SCMOPT_NO_FORCE_FINAL_EOL, RTGETOPT_REQ_NOTHING },
|
---|
409 | { "--force-trailing-line", SCMOPT_FORCE_TRAILING_LINE, RTGETOPT_REQ_NOTHING },
|
---|
410 | { "--no-force-trailing-line", SCMOPT_NO_FORCE_TRAILING_LINE, RTGETOPT_REQ_NOTHING },
|
---|
411 | { "--strip-trailing-blanks", SCMOPT_STRIP_TRAILING_BLANKS, RTGETOPT_REQ_NOTHING },
|
---|
412 | { "--no-strip-trailing-blanks", SCMOPT_NO_STRIP_TRAILING_BLANKS, RTGETOPT_REQ_NOTHING },
|
---|
413 | { "--strip-trailing-lines", SCMOPT_STRIP_TRAILING_LINES, RTGETOPT_REQ_NOTHING },
|
---|
414 | { "--strip-no-trailing-lines", SCMOPT_NO_STRIP_TRAILING_LINES, RTGETOPT_REQ_NOTHING },
|
---|
415 | { "--only-svn-dirs", SCMOPT_ONLY_SVN_DIRS, RTGETOPT_REQ_NOTHING },
|
---|
416 | { "--not-only-svn-dirs", SCMOPT_NOT_ONLY_SVN_DIRS, RTGETOPT_REQ_NOTHING },
|
---|
417 | { "--only-svn-files", SCMOPT_ONLY_SVN_FILES, RTGETOPT_REQ_NOTHING },
|
---|
418 | { "--not-only-svn-files", SCMOPT_NOT_ONLY_SVN_FILES, RTGETOPT_REQ_NOTHING },
|
---|
419 | { "--set-svn-eol", SCMOPT_SET_SVN_EOL, RTGETOPT_REQ_NOTHING },
|
---|
420 | { "--dont-set-svn-eol", SCMOPT_DONT_SET_SVN_EOL, RTGETOPT_REQ_NOTHING },
|
---|
421 | { "--set-svn-executable", SCMOPT_SET_SVN_EXECUTABLE, RTGETOPT_REQ_NOTHING },
|
---|
422 | { "--dont-set-svn-executable", SCMOPT_DONT_SET_SVN_EXECUTABLE, RTGETOPT_REQ_NOTHING },
|
---|
423 | { "--set-svn-keywords", SCMOPT_SET_SVN_KEYWORDS, RTGETOPT_REQ_NOTHING },
|
---|
424 | { "--dont-set-svn-keywords", SCMOPT_DONT_SET_SVN_KEYWORDS, RTGETOPT_REQ_NOTHING },
|
---|
425 | { "--tab-size", SCMOPT_TAB_SIZE, RTGETOPT_REQ_UINT8 },
|
---|
426 | { "--filter-out-dirs", SCMOPT_FILTER_OUT_DIRS, RTGETOPT_REQ_STRING },
|
---|
427 | { "--filter-files", SCMOPT_FILTER_FILES, RTGETOPT_REQ_STRING },
|
---|
428 | { "--filter-out-files", SCMOPT_FILTER_OUT_FILES, RTGETOPT_REQ_STRING },
|
---|
429 | };
|
---|
430 |
|
---|
431 | /** Consider files matching the following patterns (base names only). */
|
---|
432 | static const char *g_pszFileFilter = NULL;
|
---|
433 |
|
---|
434 | static PFNSCMREWRITER const g_aRewritersFor_Makefile_kup[] =
|
---|
435 | {
|
---|
436 | rewrite_SvnNoExecutable,
|
---|
437 | rewrite_Makefile_kup
|
---|
438 | };
|
---|
439 |
|
---|
440 | static PFNSCMREWRITER const g_aRewritersFor_Makefile_kmk[] =
|
---|
441 | {
|
---|
442 | rewrite_ForceNativeEol,
|
---|
443 | rewrite_StripTrailingBlanks,
|
---|
444 | rewrite_AdjustTrailingLines,
|
---|
445 | rewrite_SvnNoExecutable,
|
---|
446 | rewrite_SvnKeywords,
|
---|
447 | rewrite_Makefile_kmk
|
---|
448 | };
|
---|
449 |
|
---|
450 | static PFNSCMREWRITER const g_aRewritersFor_C_and_CPP[] =
|
---|
451 | {
|
---|
452 | rewrite_ForceNativeEol,
|
---|
453 | rewrite_ExpandTabs,
|
---|
454 | rewrite_StripTrailingBlanks,
|
---|
455 | rewrite_AdjustTrailingLines,
|
---|
456 | rewrite_SvnNoExecutable,
|
---|
457 | rewrite_SvnKeywords,
|
---|
458 | rewrite_C_and_CPP
|
---|
459 | };
|
---|
460 |
|
---|
461 | static PFNSCMREWRITER const g_aRewritersFor_H_and_HPP[] =
|
---|
462 | {
|
---|
463 | rewrite_ForceNativeEol,
|
---|
464 | rewrite_ExpandTabs,
|
---|
465 | rewrite_StripTrailingBlanks,
|
---|
466 | rewrite_AdjustTrailingLines,
|
---|
467 | rewrite_SvnNoExecutable,
|
---|
468 | rewrite_C_and_CPP
|
---|
469 | };
|
---|
470 |
|
---|
471 | static PFNSCMREWRITER const g_aRewritersFor_RC[] =
|
---|
472 | {
|
---|
473 | rewrite_ForceNativeEol,
|
---|
474 | rewrite_ExpandTabs,
|
---|
475 | rewrite_StripTrailingBlanks,
|
---|
476 | rewrite_AdjustTrailingLines,
|
---|
477 | rewrite_SvnNoExecutable,
|
---|
478 | rewrite_SvnKeywords
|
---|
479 | };
|
---|
480 |
|
---|
481 | static PFNSCMREWRITER const g_aRewritersFor_ShellScripts[] =
|
---|
482 | {
|
---|
483 | rewrite_ForceLF,
|
---|
484 | rewrite_ExpandTabs,
|
---|
485 | rewrite_StripTrailingBlanks
|
---|
486 | };
|
---|
487 |
|
---|
488 | static PFNSCMREWRITER const g_aRewritersFor_BatchFiles[] =
|
---|
489 | {
|
---|
490 | rewrite_ForceCRLF,
|
---|
491 | rewrite_ExpandTabs,
|
---|
492 | rewrite_StripTrailingBlanks
|
---|
493 | };
|
---|
494 |
|
---|
495 | static SCMCFGENTRY const g_aConfigs[] =
|
---|
496 | {
|
---|
497 | { RT_ELEMENTS(g_aRewritersFor_Makefile_kup), &g_aRewritersFor_Makefile_kup[0], "Makefile.kup" },
|
---|
498 | { RT_ELEMENTS(g_aRewritersFor_Makefile_kmk), &g_aRewritersFor_Makefile_kmk[0], "Makefile.kmk|Config.kmk" },
|
---|
499 | { RT_ELEMENTS(g_aRewritersFor_C_and_CPP), &g_aRewritersFor_C_and_CPP[0], "*.c|*.cpp|*.C|*.CPP|*.cxx|*.cc" },
|
---|
500 | { RT_ELEMENTS(g_aRewritersFor_H_and_HPP), &g_aRewritersFor_H_and_HPP[0], "*.h|*.hpp" },
|
---|
501 | { RT_ELEMENTS(g_aRewritersFor_RC), &g_aRewritersFor_RC[0], "*.rc" },
|
---|
502 | { RT_ELEMENTS(g_aRewritersFor_ShellScripts), &g_aRewritersFor_ShellScripts[0], "*.sh|configure" },
|
---|
503 | { RT_ELEMENTS(g_aRewritersFor_BatchFiles), &g_aRewritersFor_BatchFiles[0], "*.bat|*.cmd|*.btm|*.vbs|*.ps1" },
|
---|
504 | };
|
---|
505 |
|
---|
506 |
|
---|
507 | /* -=-=-=-=-=- memory streams -=-=-=-=-=- */
|
---|
508 |
|
---|
509 |
|
---|
510 | /**
|
---|
511 | * Initializes the stream structure.
|
---|
512 | *
|
---|
513 | * @param pStream The stream structure.
|
---|
514 | * @param fWriteOrRead The value of the fWriteOrRead stream member.
|
---|
515 | */
|
---|
516 | static void scmStreamInitInternal(PSCMSTREAM pStream, bool fWriteOrRead)
|
---|
517 | {
|
---|
518 | pStream->pch = NULL;
|
---|
519 | pStream->off = 0;
|
---|
520 | pStream->cb = 0;
|
---|
521 | pStream->cbAllocated = 0;
|
---|
522 |
|
---|
523 | pStream->paLines = NULL;
|
---|
524 | pStream->iLine = 0;
|
---|
525 | pStream->cLines = 0;
|
---|
526 | pStream->cLinesAllocated = 0;
|
---|
527 |
|
---|
528 | pStream->fWriteOrRead = fWriteOrRead;
|
---|
529 | pStream->fFileMemory = false;
|
---|
530 | pStream->fFullyLineated = false;
|
---|
531 |
|
---|
532 | pStream->rc = VINF_SUCCESS;
|
---|
533 | }
|
---|
534 |
|
---|
535 | /**
|
---|
536 | * Initialize an input stream.
|
---|
537 | *
|
---|
538 | * @returns IPRT status code.
|
---|
539 | * @param pStream The stream to initialize.
|
---|
540 | * @param pszFilename The file to take the stream content from.
|
---|
541 | */
|
---|
542 | int ScmStreamInitForReading(PSCMSTREAM pStream, const char *pszFilename)
|
---|
543 | {
|
---|
544 | scmStreamInitInternal(pStream, false /*fWriteOrRead*/);
|
---|
545 |
|
---|
546 | void *pvFile;
|
---|
547 | size_t cbFile;
|
---|
548 | int rc = pStream->rc = RTFileReadAll(pszFilename, &pvFile, &cbFile);
|
---|
549 | if (RT_SUCCESS(rc))
|
---|
550 | {
|
---|
551 | pStream->pch = (char *)pvFile;
|
---|
552 | pStream->cb = cbFile;
|
---|
553 | pStream->cbAllocated = cbFile;
|
---|
554 | pStream->fFileMemory = true;
|
---|
555 | }
|
---|
556 | return rc;
|
---|
557 | }
|
---|
558 |
|
---|
559 | /**
|
---|
560 | * Initialize an output stream.
|
---|
561 | *
|
---|
562 | * @returns IPRT status code
|
---|
563 | * @param pStream The stream to initialize.
|
---|
564 | * @param pRelatedStream Pointer to a related stream. NULL is fine.
|
---|
565 | */
|
---|
566 | int ScmStreamInitForWriting(PSCMSTREAM pStream, PCSCMSTREAM pRelatedStream)
|
---|
567 | {
|
---|
568 | scmStreamInitInternal(pStream, true /*fWriteOrRead*/);
|
---|
569 |
|
---|
570 | /* allocate stuff */
|
---|
571 | size_t cbEstimate = pRelatedStream
|
---|
572 | ? pRelatedStream->cb + pRelatedStream->cb / 10
|
---|
573 | : _64K;
|
---|
574 | cbEstimate = RT_ALIGN(cbEstimate, _4K);
|
---|
575 | pStream->pch = (char *)RTMemAlloc(cbEstimate);
|
---|
576 | if (pStream->pch)
|
---|
577 | {
|
---|
578 | size_t cLinesEstimate = pRelatedStream && pRelatedStream->fFullyLineated
|
---|
579 | ? pRelatedStream->cLines + pRelatedStream->cLines / 10
|
---|
580 | : cbEstimate / 24;
|
---|
581 | cLinesEstimate = RT_ALIGN(cLinesEstimate, 512);
|
---|
582 | pStream->paLines = (PSCMSTREAMLINE)RTMemAlloc(cLinesEstimate * sizeof(SCMSTREAMLINE));
|
---|
583 | if (pStream->paLines)
|
---|
584 | {
|
---|
585 | pStream->paLines[0].off = 0;
|
---|
586 | pStream->paLines[0].cch = 0;
|
---|
587 | pStream->paLines[0].enmEol = SCMEOL_NONE;
|
---|
588 | pStream->cbAllocated = cbEstimate;
|
---|
589 | pStream->cLinesAllocated = cLinesEstimate;
|
---|
590 | return VINF_SUCCESS;
|
---|
591 | }
|
---|
592 |
|
---|
593 | RTMemFree(pStream->pch);
|
---|
594 | pStream->pch = NULL;
|
---|
595 | }
|
---|
596 | return pStream->rc = VERR_NO_MEMORY;
|
---|
597 | }
|
---|
598 |
|
---|
599 | /**
|
---|
600 | * Frees the resources associated with the stream.
|
---|
601 | *
|
---|
602 | * Nothing is happens to whatever the stream was initialized from or dumped to.
|
---|
603 | *
|
---|
604 | * @param pStream The stream to delete.
|
---|
605 | */
|
---|
606 | void ScmStreamDelete(PSCMSTREAM pStream)
|
---|
607 | {
|
---|
608 | if (pStream->pch)
|
---|
609 | {
|
---|
610 | if (pStream->fFileMemory)
|
---|
611 | RTFileReadAllFree(pStream->pch, pStream->cbAllocated);
|
---|
612 | else
|
---|
613 | RTMemFree(pStream->pch);
|
---|
614 | pStream->pch = NULL;
|
---|
615 | }
|
---|
616 | pStream->cbAllocated = 0;
|
---|
617 |
|
---|
618 | if (pStream->paLines)
|
---|
619 | {
|
---|
620 | RTMemFree(pStream->paLines);
|
---|
621 | pStream->paLines = NULL;
|
---|
622 | }
|
---|
623 | pStream->cLinesAllocated = 0;
|
---|
624 | }
|
---|
625 |
|
---|
626 | /**
|
---|
627 | * Get the stream status code.
|
---|
628 | *
|
---|
629 | * @returns IPRT status code.
|
---|
630 | * @param pStream The stream.
|
---|
631 | */
|
---|
632 | int ScmStreamGetStatus(PCSCMSTREAM pStream)
|
---|
633 | {
|
---|
634 | return pStream->rc;
|
---|
635 | }
|
---|
636 |
|
---|
637 | /**
|
---|
638 | * Grows the buffer of a write stream.
|
---|
639 | *
|
---|
640 | * @returns IPRT status code.
|
---|
641 | * @param pStream The stream. Must be in write mode.
|
---|
642 | * @param cbAppending The minimum number of bytes to grow the buffer
|
---|
643 | * with.
|
---|
644 | */
|
---|
645 | static int scmStreamGrowBuffer(PSCMSTREAM pStream, size_t cbAppending)
|
---|
646 | {
|
---|
647 | size_t cbAllocated = pStream->cbAllocated;
|
---|
648 | cbAllocated += RT_MAX(0x1000 + cbAppending, cbAllocated);
|
---|
649 | cbAllocated = RT_ALIGN(cbAllocated, 0x1000);
|
---|
650 | void *pvNew;
|
---|
651 | if (!pStream->fFileMemory)
|
---|
652 | {
|
---|
653 | pvNew = RTMemRealloc(pStream->pch, cbAllocated);
|
---|
654 | if (!pvNew)
|
---|
655 | return pStream->rc = VERR_NO_MEMORY;
|
---|
656 | }
|
---|
657 | else
|
---|
658 | {
|
---|
659 | pvNew = RTMemDupEx(pStream->pch, pStream->off, cbAllocated - pStream->off);
|
---|
660 | if (!pvNew)
|
---|
661 | return pStream->rc = VERR_NO_MEMORY;
|
---|
662 | RTFileReadAllFree(pStream->pch, pStream->cbAllocated);
|
---|
663 | pStream->fFileMemory = false;
|
---|
664 | }
|
---|
665 | pStream->pch = (char *)pvNew;
|
---|
666 | pStream->cbAllocated = cbAllocated;
|
---|
667 |
|
---|
668 | return VINF_SUCCESS;
|
---|
669 | }
|
---|
670 |
|
---|
671 | /**
|
---|
672 | * Grows the line array of a stream.
|
---|
673 | *
|
---|
674 | * @returns IPRT status code.
|
---|
675 | * @param pStream The stream.
|
---|
676 | * @param iMinLine Minimum line number.
|
---|
677 | */
|
---|
678 | static int scmStreamGrowLines(PSCMSTREAM pStream, size_t iMinLine)
|
---|
679 | {
|
---|
680 | size_t cLinesAllocated = pStream->cLinesAllocated;
|
---|
681 | cLinesAllocated += RT_MAX(512 + iMinLine, cLinesAllocated);
|
---|
682 | cLinesAllocated = RT_ALIGN(cLinesAllocated, 512);
|
---|
683 | void *pvNew = RTMemRealloc(pStream->paLines, cLinesAllocated * sizeof(SCMSTREAMLINE));
|
---|
684 | if (!pvNew)
|
---|
685 | return pStream->rc = VERR_NO_MEMORY;
|
---|
686 |
|
---|
687 | pStream->paLines = (PSCMSTREAMLINE)pvNew;
|
---|
688 | pStream->cLinesAllocated = cLinesAllocated;
|
---|
689 | return VINF_SUCCESS;
|
---|
690 | }
|
---|
691 |
|
---|
692 | /**
|
---|
693 | * Rewinds the stream and sets the mode to read.
|
---|
694 | *
|
---|
695 | * @param pStream The stream.
|
---|
696 | */
|
---|
697 | void ScmStreamRewindForReading(PSCMSTREAM pStream)
|
---|
698 | {
|
---|
699 | pStream->off = 0;
|
---|
700 | pStream->iLine = 0;
|
---|
701 | pStream->fWriteOrRead = false;
|
---|
702 | pStream->rc = VINF_SUCCESS;
|
---|
703 | }
|
---|
704 |
|
---|
705 | /**
|
---|
706 | * Rewinds the stream and sets the mode to write.
|
---|
707 | *
|
---|
708 | * @param pStream The stream.
|
---|
709 | */
|
---|
710 | void ScmStreamRewindForWriting(PSCMSTREAM pStream)
|
---|
711 | {
|
---|
712 | pStream->off = 0;
|
---|
713 | pStream->iLine = 0;
|
---|
714 | pStream->cLines = 0;
|
---|
715 | pStream->fWriteOrRead = true;
|
---|
716 | pStream->fFullyLineated = true;
|
---|
717 | pStream->rc = VINF_SUCCESS;
|
---|
718 | }
|
---|
719 |
|
---|
720 | /**
|
---|
721 | * Checks if it's a text stream.
|
---|
722 | *
|
---|
723 | * Not 100% proof.
|
---|
724 | *
|
---|
725 | * @returns true if it probably is a text file, false if not.
|
---|
726 | * @param pStream The stream. Write or read, doesn't matter.
|
---|
727 | */
|
---|
728 | bool ScmStreamIsText(PSCMSTREAM pStream)
|
---|
729 | {
|
---|
730 | if (memchr(pStream->pch, '\0', pStream->cb))
|
---|
731 | return false;
|
---|
732 | if (!pStream->cb)
|
---|
733 | return false;
|
---|
734 | return true;
|
---|
735 | }
|
---|
736 |
|
---|
737 | /**
|
---|
738 | * Performs an integrity check of the stream.
|
---|
739 | *
|
---|
740 | * @returns IPRT status code.
|
---|
741 | * @param pStream The stream.
|
---|
742 | */
|
---|
743 | int ScmStreamCheckItegrity(PSCMSTREAM pStream)
|
---|
744 | {
|
---|
745 | /*
|
---|
746 | * Perform sanity checks.
|
---|
747 | */
|
---|
748 | size_t const cbFile = pStream->cb;
|
---|
749 | for (size_t iLine = 0; iLine < pStream->cLines; iLine++)
|
---|
750 | {
|
---|
751 | size_t offEol = pStream->paLines[iLine].off + pStream->paLines[iLine].cch;
|
---|
752 | AssertReturn(offEol + pStream->paLines[iLine].enmEol <= cbFile, VERR_INTERNAL_ERROR_2);
|
---|
753 | switch (pStream->paLines[iLine].enmEol)
|
---|
754 | {
|
---|
755 | case SCMEOL_LF:
|
---|
756 | AssertReturn(pStream->pch[offEol] == '\n', VERR_INTERNAL_ERROR_3);
|
---|
757 | break;
|
---|
758 | case SCMEOL_CRLF:
|
---|
759 | AssertReturn(pStream->pch[offEol] == '\r', VERR_INTERNAL_ERROR_3);
|
---|
760 | AssertReturn(pStream->pch[offEol + 1] == '\n', VERR_INTERNAL_ERROR_3);
|
---|
761 | break;
|
---|
762 | case SCMEOL_NONE:
|
---|
763 | AssertReturn(iLine + 1 >= pStream->cLines, VERR_INTERNAL_ERROR_4);
|
---|
764 | break;
|
---|
765 | default:
|
---|
766 | AssertReturn(iLine + 1 >= pStream->cLines, VERR_INTERNAL_ERROR_5);
|
---|
767 | }
|
---|
768 | }
|
---|
769 | return VINF_SUCCESS;
|
---|
770 | }
|
---|
771 |
|
---|
772 | /**
|
---|
773 | * Writes the stream to a file.
|
---|
774 | *
|
---|
775 | * @returns IPRT status code
|
---|
776 | * @param pStream The stream.
|
---|
777 | * @param pszFilenameFmt The filename format string.
|
---|
778 | * @param ... Format arguments.
|
---|
779 | */
|
---|
780 | int ScmStreamWriteToFile(PSCMSTREAM pStream, const char *pszFilenameFmt, ...)
|
---|
781 | {
|
---|
782 | int rc;
|
---|
783 |
|
---|
784 | #ifdef RT_STRICT
|
---|
785 | /*
|
---|
786 | * Check that what we're going to write makes sense first.
|
---|
787 | */
|
---|
788 | rc = ScmStreamCheckItegrity(pStream);
|
---|
789 | if (RT_FAILURE(rc))
|
---|
790 | return rc;
|
---|
791 | #endif
|
---|
792 |
|
---|
793 | /*
|
---|
794 | * Do the actual writing.
|
---|
795 | */
|
---|
796 | RTFILE hFile;
|
---|
797 | va_list va;
|
---|
798 | va_start(va, pszFilenameFmt);
|
---|
799 | rc = RTFileOpenV(&hFile, RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_WRITE, pszFilenameFmt, va);
|
---|
800 | if (RT_SUCCESS(rc))
|
---|
801 | {
|
---|
802 | rc = RTFileWrite(hFile, pStream->pch, pStream->cb, NULL);
|
---|
803 | RTFileClose(hFile);
|
---|
804 | }
|
---|
805 | return rc;
|
---|
806 | }
|
---|
807 |
|
---|
808 | /**
|
---|
809 | * Worker for ScmStreamGetLine that builds the line number index while parsing
|
---|
810 | * the stream.
|
---|
811 | *
|
---|
812 | * @returns Same as SCMStreamGetLine.
|
---|
813 | * @param pStream The stream. Must be in read mode.
|
---|
814 | * @param pcchLine Where to return the line length.
|
---|
815 | * @param penmEol Where to return the kind of end of line marker.
|
---|
816 | */
|
---|
817 | static const char *scmStreamGetLineInternal(PSCMSTREAM pStream, size_t *pcchLine, PSCMEOL penmEol)
|
---|
818 | {
|
---|
819 | AssertReturn(!pStream->fWriteOrRead, NULL);
|
---|
820 | if (RT_FAILURE(pStream->rc))
|
---|
821 | return NULL;
|
---|
822 |
|
---|
823 | size_t off = pStream->off;
|
---|
824 | size_t cb = pStream->cb;
|
---|
825 | if (RT_UNLIKELY(off >= cb))
|
---|
826 | {
|
---|
827 | pStream->fFullyLineated = true;
|
---|
828 | return NULL;
|
---|
829 | }
|
---|
830 |
|
---|
831 | size_t iLine = pStream->iLine;
|
---|
832 | if (RT_UNLIKELY(iLine >= pStream->cLinesAllocated))
|
---|
833 | {
|
---|
834 | int rc = scmStreamGrowLines(pStream, iLine);
|
---|
835 | if (RT_FAILURE(rc))
|
---|
836 | return NULL;
|
---|
837 | }
|
---|
838 | pStream->paLines[iLine].off = off;
|
---|
839 |
|
---|
840 | cb -= off;
|
---|
841 | const char *pchRet = &pStream->pch[off];
|
---|
842 | const char *pch = (const char *)memchr(pchRet, '\n', cb);
|
---|
843 | if (RT_LIKELY(pch))
|
---|
844 | {
|
---|
845 | cb = pch - pchRet;
|
---|
846 | pStream->off = off + cb + 1;
|
---|
847 | if ( cb < 1
|
---|
848 | || pch[-1] != '\r')
|
---|
849 | pStream->paLines[iLine].enmEol = *penmEol = SCMEOL_LF;
|
---|
850 | else
|
---|
851 | {
|
---|
852 | pStream->paLines[iLine].enmEol = *penmEol = SCMEOL_CRLF;
|
---|
853 | cb--;
|
---|
854 | }
|
---|
855 | }
|
---|
856 | else
|
---|
857 | {
|
---|
858 | pStream->off = off + cb;
|
---|
859 | pStream->paLines[iLine].enmEol = *penmEol = SCMEOL_NONE;
|
---|
860 | }
|
---|
861 | *pcchLine = cb;
|
---|
862 | pStream->paLines[iLine].cch = cb;
|
---|
863 | pStream->cLines = pStream->iLine = ++iLine;
|
---|
864 |
|
---|
865 | return pchRet;
|
---|
866 | }
|
---|
867 |
|
---|
868 | /**
|
---|
869 | * Internal worker that lineates a stream.
|
---|
870 | *
|
---|
871 | * @returns IPRT status code.
|
---|
872 | * @param pStream The stream. Caller must check that it is in
|
---|
873 | * read mode.
|
---|
874 | */
|
---|
875 | static int scmStreamLineate(PSCMSTREAM pStream)
|
---|
876 | {
|
---|
877 | /* Save the stream position. */
|
---|
878 | size_t const offSaved = pStream->off;
|
---|
879 | size_t const iLineSaved = pStream->iLine;
|
---|
880 |
|
---|
881 | /* Get each line. */
|
---|
882 | size_t cchLine;
|
---|
883 | SCMEOL enmEol;
|
---|
884 | while (scmStreamGetLineInternal(pStream, &cchLine, &enmEol))
|
---|
885 | /* nothing */;
|
---|
886 | Assert(RT_FAILURE(pStream->rc) || pStream->fFullyLineated);
|
---|
887 |
|
---|
888 | /* Restore the position */
|
---|
889 | pStream->off = offSaved;
|
---|
890 | pStream->iLine = iLineSaved;
|
---|
891 |
|
---|
892 | return pStream->rc;
|
---|
893 | }
|
---|
894 |
|
---|
895 | /**
|
---|
896 | * Get the current stream position as an byte offset.
|
---|
897 | *
|
---|
898 | * @returns The current byte offset
|
---|
899 | * @param pStream The stream.
|
---|
900 | */
|
---|
901 | size_t ScmStreamTell(PSCMSTREAM pStream)
|
---|
902 | {
|
---|
903 | return pStream->off;
|
---|
904 | }
|
---|
905 |
|
---|
906 | /**
|
---|
907 | * Get the current stream position as a line number.
|
---|
908 | *
|
---|
909 | * @returns The current line (0-based).
|
---|
910 | * @param pStream The stream.
|
---|
911 | */
|
---|
912 | size_t ScmStreamTellLine(PSCMSTREAM pStream)
|
---|
913 | {
|
---|
914 | return pStream->iLine;
|
---|
915 | }
|
---|
916 |
|
---|
917 | /**
|
---|
918 | * Get the current stream size in bytes.
|
---|
919 | *
|
---|
920 | * @returns Count of bytes.
|
---|
921 | * @param pStream The stream.
|
---|
922 | */
|
---|
923 | size_t ScmStreamSize(PSCMSTREAM pStream)
|
---|
924 | {
|
---|
925 | return pStream->cb;
|
---|
926 | }
|
---|
927 |
|
---|
928 | /**
|
---|
929 | * Gets the number of lines in the stream.
|
---|
930 | *
|
---|
931 | * @returns The number of lines.
|
---|
932 | * @param pStream The stream.
|
---|
933 | */
|
---|
934 | size_t ScmStreamCountLines(PSCMSTREAM pStream)
|
---|
935 | {
|
---|
936 | if (!pStream->fFullyLineated)
|
---|
937 | scmStreamLineate(pStream);
|
---|
938 | return pStream->cLines;
|
---|
939 | }
|
---|
940 |
|
---|
941 | /**
|
---|
942 | * Seeks to a given byte offset in the stream.
|
---|
943 | *
|
---|
944 | * @returns IPRT status code.
|
---|
945 | * @retval VERR_SEEK if the new stream position is the middle of an EOL marker.
|
---|
946 | * This is a temporary restriction.
|
---|
947 | *
|
---|
948 | * @param pStream The stream. Must be in read mode.
|
---|
949 | * @param offAbsolute The offset to seek to. If this is beyond the
|
---|
950 | * end of the stream, the position is set to the
|
---|
951 | * end.
|
---|
952 | */
|
---|
953 | int ScmStreamSeekAbsolute(PSCMSTREAM pStream, size_t offAbsolute)
|
---|
954 | {
|
---|
955 | AssertReturn(!pStream->fWriteOrRead, VERR_ACCESS_DENIED);
|
---|
956 | if (RT_FAILURE(pStream->rc))
|
---|
957 | return pStream->rc;
|
---|
958 |
|
---|
959 | /* Must be fully lineated. (lazy bird) */
|
---|
960 | if (RT_UNLIKELY(!pStream->fFullyLineated))
|
---|
961 | {
|
---|
962 | int rc = scmStreamLineate(pStream);
|
---|
963 | if (RT_FAILURE(rc))
|
---|
964 | return rc;
|
---|
965 | }
|
---|
966 |
|
---|
967 | /* Ok, do the job. */
|
---|
968 | if (offAbsolute < pStream->cb)
|
---|
969 | {
|
---|
970 | /** @todo Should do a binary search here, but I'm too darn lazy tonight. */
|
---|
971 | pStream->off = ~(size_t)0;
|
---|
972 | for (size_t i = 0; i < pStream->cLines; i++)
|
---|
973 | {
|
---|
974 | if (offAbsolute < pStream->paLines[i].off + pStream->paLines[i].cch + pStream->paLines[i].enmEol)
|
---|
975 | {
|
---|
976 | pStream->off = offAbsolute;
|
---|
977 | pStream->iLine = i;
|
---|
978 | if (offAbsolute > pStream->paLines[i].off + pStream->paLines[i].cch)
|
---|
979 | return pStream->rc = VERR_SEEK;
|
---|
980 | break;
|
---|
981 | }
|
---|
982 | }
|
---|
983 | AssertReturn(pStream->off != ~(size_t)0, pStream->rc = VERR_INTERNAL_ERROR_3);
|
---|
984 | }
|
---|
985 | else
|
---|
986 | {
|
---|
987 | pStream->off = pStream->cb;
|
---|
988 | pStream->iLine = pStream->cLines;
|
---|
989 | }
|
---|
990 | return VINF_SUCCESS;
|
---|
991 | }
|
---|
992 |
|
---|
993 |
|
---|
994 | /**
|
---|
995 | * Seeks a number of bytes relative to the current stream position.
|
---|
996 | *
|
---|
997 | * @returns IPRT status code.
|
---|
998 | * @retval VERR_SEEK if the new stream position is the middle of an EOL marker.
|
---|
999 | * This is a temporary restriction.
|
---|
1000 | *
|
---|
1001 | * @param pStream The stream. Must be in read mode.
|
---|
1002 | * @param offRelative The offset to seek to. A negative offset
|
---|
1003 | * rewinds and positive one fast forwards the
|
---|
1004 | * stream. Will quietly stop at the begining and
|
---|
1005 | * end of the stream.
|
---|
1006 | */
|
---|
1007 | int ScmStreamSeekRelative(PSCMSTREAM pStream, ssize_t offRelative)
|
---|
1008 | {
|
---|
1009 | size_t offAbsolute;
|
---|
1010 | if (offRelative >= 0)
|
---|
1011 | offAbsolute = pStream->off + offRelative;
|
---|
1012 | else if ((size_t)-offRelative <= pStream->off)
|
---|
1013 | offAbsolute = pStream->off + offRelative;
|
---|
1014 | else
|
---|
1015 | offAbsolute = 0;
|
---|
1016 | return ScmStreamSeekAbsolute(pStream, offAbsolute);
|
---|
1017 | }
|
---|
1018 |
|
---|
1019 | /**
|
---|
1020 | * Seeks to a given line in the stream.
|
---|
1021 | *
|
---|
1022 | * @returns IPRT status code.
|
---|
1023 | *
|
---|
1024 | * @param pStream The stream. Must be in read mode.
|
---|
1025 | * @param iLine The line to seek to. If this is beyond the end
|
---|
1026 | * of the stream, the position is set to the end.
|
---|
1027 | */
|
---|
1028 | int ScmStreamSeekByLine(PSCMSTREAM pStream, size_t iLine)
|
---|
1029 | {
|
---|
1030 | AssertReturn(!pStream->fWriteOrRead, VERR_ACCESS_DENIED);
|
---|
1031 | if (RT_FAILURE(pStream->rc))
|
---|
1032 | return pStream->rc;
|
---|
1033 |
|
---|
1034 | /* Must be fully lineated. (lazy bird) */
|
---|
1035 | if (RT_UNLIKELY(!pStream->fFullyLineated))
|
---|
1036 | {
|
---|
1037 | int rc = scmStreamLineate(pStream);
|
---|
1038 | if (RT_FAILURE(rc))
|
---|
1039 | return rc;
|
---|
1040 | }
|
---|
1041 |
|
---|
1042 | /* Ok, do the job. */
|
---|
1043 | if (iLine < pStream->cLines)
|
---|
1044 | {
|
---|
1045 | pStream->off = pStream->paLines[iLine].off;
|
---|
1046 | pStream->iLine = iLine;
|
---|
1047 | }
|
---|
1048 | else
|
---|
1049 | {
|
---|
1050 | pStream->off = pStream->cb;
|
---|
1051 | pStream->iLine = pStream->cLines;
|
---|
1052 | }
|
---|
1053 | return VINF_SUCCESS;
|
---|
1054 | }
|
---|
1055 |
|
---|
1056 | /**
|
---|
1057 | * Get a numbered line from the stream (changes the position).
|
---|
1058 | *
|
---|
1059 | * A line is always delimited by a LF character or the end of the stream. The
|
---|
1060 | * delimiter is not included in returned line length, but instead returned via
|
---|
1061 | * the @a penmEol indicator.
|
---|
1062 | *
|
---|
1063 | * @returns Pointer to the first character in the line, not NULL terminated.
|
---|
1064 | * NULL if the end of the stream has been reached or some problem
|
---|
1065 | * occured.
|
---|
1066 | *
|
---|
1067 | * @param pStream The stream. Must be in read mode.
|
---|
1068 | * @param iLine The line to get (0-based).
|
---|
1069 | * @param pcchLine The length.
|
---|
1070 | * @param penmEol Where to return the end of line type indicator.
|
---|
1071 | */
|
---|
1072 | static const char *ScmStreamGetLineByNo(PSCMSTREAM pStream, size_t iLine, size_t *pcchLine, PSCMEOL penmEol)
|
---|
1073 | {
|
---|
1074 | AssertReturn(!pStream->fWriteOrRead, NULL);
|
---|
1075 | if (RT_FAILURE(pStream->rc))
|
---|
1076 | return NULL;
|
---|
1077 |
|
---|
1078 | /* Make sure it's fully lineated so we can use the index. */
|
---|
1079 | if (RT_UNLIKELY(!pStream->fFullyLineated))
|
---|
1080 | {
|
---|
1081 | int rc = scmStreamLineate(pStream);
|
---|
1082 | if (RT_FAILURE(rc))
|
---|
1083 | return NULL;
|
---|
1084 | }
|
---|
1085 |
|
---|
1086 | /* End of stream? */
|
---|
1087 | if (RT_UNLIKELY(iLine >= pStream->cLines))
|
---|
1088 | {
|
---|
1089 | pStream->off = pStream->cb;
|
---|
1090 | pStream->iLine = pStream->cLines;
|
---|
1091 | return NULL;
|
---|
1092 | }
|
---|
1093 |
|
---|
1094 | /* Get the data. */
|
---|
1095 | const char *pchRet = &pStream->pch[pStream->paLines[iLine].off];
|
---|
1096 | *pcchLine = pStream->paLines[iLine].cch;
|
---|
1097 | *penmEol = pStream->paLines[iLine].enmEol;
|
---|
1098 |
|
---|
1099 | /* update the stream position. */
|
---|
1100 | pStream->off = pStream->paLines[iLine].off + pStream->paLines[iLine].cch + pStream->paLines[iLine].enmEol;
|
---|
1101 | pStream->iLine = iLine + 1;
|
---|
1102 |
|
---|
1103 | return pchRet;
|
---|
1104 | }
|
---|
1105 |
|
---|
1106 | /**
|
---|
1107 | * Get a line from the stream.
|
---|
1108 | *
|
---|
1109 | * A line is always delimited by a LF character or the end of the stream. The
|
---|
1110 | * delimiter is not included in returned line length, but instead returned via
|
---|
1111 | * the @a penmEol indicator.
|
---|
1112 | *
|
---|
1113 | * @returns Pointer to the first character in the line, not NULL terminated.
|
---|
1114 | * NULL if the end of the stream has been reached or some problem
|
---|
1115 | * occured.
|
---|
1116 | *
|
---|
1117 | * @param pStream The stream. Must be in read mode.
|
---|
1118 | * @param pcchLine The length.
|
---|
1119 | * @param penmEol Where to return the end of line type indicator.
|
---|
1120 | */
|
---|
1121 | static const char *ScmStreamGetLine(PSCMSTREAM pStream, size_t *pcchLine, PSCMEOL penmEol)
|
---|
1122 | {
|
---|
1123 | /** @todo this doesn't work when pStream->off !=
|
---|
1124 | * pStream->paLines[pStream->iLine-1].pff. */
|
---|
1125 | if (!pStream->fFullyLineated)
|
---|
1126 | return scmStreamGetLineInternal(pStream, pcchLine, penmEol);
|
---|
1127 | return ScmStreamGetLineByNo(pStream, pStream->iLine, pcchLine, penmEol);
|
---|
1128 | }
|
---|
1129 |
|
---|
1130 | /**
|
---|
1131 | * Reads @a cbToRead bytes into @a pvBuf.
|
---|
1132 | *
|
---|
1133 | * Will fail if end of stream is encountered before the entire read has been
|
---|
1134 | * completed.
|
---|
1135 | *
|
---|
1136 | * @returns IPRT status code.
|
---|
1137 | * @retval VERR_EOF if there isn't @a cbToRead bytes left to read. Stream
|
---|
1138 | * position will be unchanged.
|
---|
1139 | *
|
---|
1140 | * @param pStream The stream. Must be in read mode.
|
---|
1141 | * @param pvBuf The buffer to read into.
|
---|
1142 | * @param cbToRead The number of bytes to read.
|
---|
1143 | */
|
---|
1144 | static int ScmStreamRead(PSCMSTREAM pStream, void *pvBuf, size_t cbToRead)
|
---|
1145 | {
|
---|
1146 | AssertReturn(!pStream->fWriteOrRead, VERR_PERMISSION_DENIED);
|
---|
1147 | if (RT_FAILURE(pStream->rc))
|
---|
1148 | return pStream->rc;
|
---|
1149 |
|
---|
1150 | /* If there isn't enough stream left, fail already. */
|
---|
1151 | if (RT_UNLIKELY(pStream->cb - pStream->cb < cbToRead))
|
---|
1152 | return VERR_EOF;
|
---|
1153 |
|
---|
1154 | /* Copy the data and simply seek to the new stream position. */
|
---|
1155 | memcpy(pvBuf, &pStream->pch[pStream->off], cbToRead);
|
---|
1156 | return ScmStreamSeekAbsolute(pStream, pStream->off + cbToRead);
|
---|
1157 | }
|
---|
1158 |
|
---|
1159 | /**
|
---|
1160 | * Checks if the given line is empty or full of white space.
|
---|
1161 | *
|
---|
1162 | * @returns true if white space only, false if not (or if non-existant).
|
---|
1163 | * @param pStream The stream. Must be in read mode.
|
---|
1164 | * @param iLine The line in question.
|
---|
1165 | */
|
---|
1166 | static bool ScmStreamIsWhiteLine(PSCMSTREAM pStream, size_t iLine)
|
---|
1167 | {
|
---|
1168 | SCMEOL enmEol;
|
---|
1169 | size_t cchLine;
|
---|
1170 | const char *pchLine = ScmStreamGetLineByNo(pStream, iLine, &cchLine, &enmEol);
|
---|
1171 | if (!pchLine)
|
---|
1172 | return false;
|
---|
1173 | while (cchLine && RT_C_IS_SPACE(*pchLine))
|
---|
1174 | pchLine++, cchLine--;
|
---|
1175 | return cchLine == 0;
|
---|
1176 | }
|
---|
1177 |
|
---|
1178 | /**
|
---|
1179 | * Try figure out the end of line style of the give stream.
|
---|
1180 | *
|
---|
1181 | * @returns Most likely end of line style.
|
---|
1182 | * @param pStream The stream.
|
---|
1183 | */
|
---|
1184 | SCMEOL ScmStreamGetEol(PSCMSTREAM pStream)
|
---|
1185 | {
|
---|
1186 | SCMEOL enmEol;
|
---|
1187 | if (pStream->cLines > 0)
|
---|
1188 | enmEol = pStream->paLines[0].enmEol;
|
---|
1189 | else if (pStream->cb == 0)
|
---|
1190 | enmEol = SCMEOL_NONE;
|
---|
1191 | else
|
---|
1192 | {
|
---|
1193 | const char *pchLF = (const char *)memchr(pStream->pch, '\n', pStream->cb);
|
---|
1194 | if (pchLF && pchLF != pStream->pch && pchLF[-1] == '\r')
|
---|
1195 | enmEol = SCMEOL_CRLF;
|
---|
1196 | else
|
---|
1197 | enmEol = SCMEOL_LF;
|
---|
1198 | }
|
---|
1199 |
|
---|
1200 | if (enmEol == SCMEOL_NONE)
|
---|
1201 | #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|
---|
1202 | enmEol = SCMEOL_CRLF;
|
---|
1203 | #else
|
---|
1204 | enmEol = SCMEOL_LF;
|
---|
1205 | #endif
|
---|
1206 | return enmEol;
|
---|
1207 | }
|
---|
1208 |
|
---|
1209 | /**
|
---|
1210 | * Get the end of line indicator type for a line.
|
---|
1211 | *
|
---|
1212 | * @returns The EOL indicator. If the line isn't found, the default EOL
|
---|
1213 | * indicator is return.
|
---|
1214 | * @param pStream The stream.
|
---|
1215 | * @param iLine The line (0-base).
|
---|
1216 | */
|
---|
1217 | SCMEOL ScmStreamGetEolByLine(PSCMSTREAM pStream, size_t iLine)
|
---|
1218 | {
|
---|
1219 | SCMEOL enmEol;
|
---|
1220 | if (iLine < pStream->cLines)
|
---|
1221 | enmEol = pStream->paLines[iLine].enmEol;
|
---|
1222 | else
|
---|
1223 | #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|
---|
1224 | enmEol = SCMEOL_CRLF;
|
---|
1225 | #else
|
---|
1226 | enmEol = SCMEOL_LF;
|
---|
1227 | #endif
|
---|
1228 | return enmEol;
|
---|
1229 | }
|
---|
1230 |
|
---|
1231 | /**
|
---|
1232 | * Appends a line to the stream.
|
---|
1233 | *
|
---|
1234 | * @returns IPRT status code.
|
---|
1235 | * @param pStream The stream. Must be in write mode.
|
---|
1236 | * @param pchLine Pointer to the line.
|
---|
1237 | * @param cchLine Line length.
|
---|
1238 | * @param enmEol Which end of line indicator to use.
|
---|
1239 | */
|
---|
1240 | int ScmStreamPutLine(PSCMSTREAM pStream, const char *pchLine, size_t cchLine, SCMEOL enmEol)
|
---|
1241 | {
|
---|
1242 | AssertReturn(pStream->fWriteOrRead, VERR_ACCESS_DENIED);
|
---|
1243 | if (RT_FAILURE(pStream->rc))
|
---|
1244 | return pStream->rc;
|
---|
1245 |
|
---|
1246 | /*
|
---|
1247 | * Make sure the previous line has a new-line indicator.
|
---|
1248 | */
|
---|
1249 | size_t off = pStream->off;
|
---|
1250 | size_t iLine = pStream->iLine;
|
---|
1251 | if (RT_UNLIKELY( iLine != 0
|
---|
1252 | && pStream->paLines[iLine - 1].enmEol == SCMEOL_NONE))
|
---|
1253 | {
|
---|
1254 | AssertReturn(pStream->paLines[iLine].cch == 0, VERR_INTERNAL_ERROR_3);
|
---|
1255 | SCMEOL enmEol2 = enmEol != SCMEOL_NONE ? enmEol : ScmStreamGetEol(pStream);
|
---|
1256 | if (RT_UNLIKELY(off + cchLine + enmEol + enmEol2 > pStream->cbAllocated))
|
---|
1257 | {
|
---|
1258 | int rc = scmStreamGrowBuffer(pStream, cchLine + enmEol + enmEol2);
|
---|
1259 | if (RT_FAILURE(rc))
|
---|
1260 | return rc;
|
---|
1261 | }
|
---|
1262 | if (enmEol2 == SCMEOL_LF)
|
---|
1263 | pStream->pch[off++] = '\n';
|
---|
1264 | else
|
---|
1265 | {
|
---|
1266 | pStream->pch[off++] = '\r';
|
---|
1267 | pStream->pch[off++] = '\n';
|
---|
1268 | }
|
---|
1269 | pStream->paLines[iLine - 1].enmEol = enmEol2;
|
---|
1270 | pStream->paLines[iLine].off = off;
|
---|
1271 | pStream->off = off;
|
---|
1272 | pStream->cb = off;
|
---|
1273 | }
|
---|
1274 |
|
---|
1275 | /*
|
---|
1276 | * Ensure we've got sufficient buffer space.
|
---|
1277 | */
|
---|
1278 | if (RT_UNLIKELY(off + cchLine + enmEol > pStream->cbAllocated))
|
---|
1279 | {
|
---|
1280 | int rc = scmStreamGrowBuffer(pStream, cchLine + enmEol);
|
---|
1281 | if (RT_FAILURE(rc))
|
---|
1282 | return rc;
|
---|
1283 | }
|
---|
1284 |
|
---|
1285 | /*
|
---|
1286 | * Add a line record.
|
---|
1287 | */
|
---|
1288 | if (RT_UNLIKELY(iLine + 1 >= pStream->cLinesAllocated))
|
---|
1289 | {
|
---|
1290 | int rc = scmStreamGrowLines(pStream, iLine);
|
---|
1291 | if (RT_FAILURE(rc))
|
---|
1292 | return rc;
|
---|
1293 | }
|
---|
1294 |
|
---|
1295 | pStream->paLines[iLine].cch = off - pStream->paLines[iLine].off + cchLine;
|
---|
1296 | pStream->paLines[iLine].enmEol = enmEol;
|
---|
1297 |
|
---|
1298 | iLine++;
|
---|
1299 | pStream->cLines = iLine;
|
---|
1300 | pStream->iLine = iLine;
|
---|
1301 |
|
---|
1302 | /*
|
---|
1303 | * Copy the line
|
---|
1304 | */
|
---|
1305 | memcpy(&pStream->pch[off], pchLine, cchLine);
|
---|
1306 | off += cchLine;
|
---|
1307 | if (enmEol == SCMEOL_LF)
|
---|
1308 | pStream->pch[off++] = '\n';
|
---|
1309 | else if (enmEol == SCMEOL_CRLF)
|
---|
1310 | {
|
---|
1311 | pStream->pch[off++] = '\r';
|
---|
1312 | pStream->pch[off++] = '\n';
|
---|
1313 | }
|
---|
1314 | pStream->off = off;
|
---|
1315 | pStream->cb = off;
|
---|
1316 |
|
---|
1317 | /*
|
---|
1318 | * Start a new line.
|
---|
1319 | */
|
---|
1320 | pStream->paLines[iLine].off = off;
|
---|
1321 | pStream->paLines[iLine].cch = 0;
|
---|
1322 | pStream->paLines[iLine].enmEol = SCMEOL_NONE;
|
---|
1323 |
|
---|
1324 | return VINF_SUCCESS;
|
---|
1325 | }
|
---|
1326 |
|
---|
1327 | /**
|
---|
1328 | * Writes to the stream.
|
---|
1329 | *
|
---|
1330 | * @returns IPRT status code
|
---|
1331 | * @param pStream The stream. Must be in write mode.
|
---|
1332 | * @param pchBuf What to write.
|
---|
1333 | * @param cchBuf How much to write.
|
---|
1334 | */
|
---|
1335 | int ScmStreamWrite(PSCMSTREAM pStream, const char *pchBuf, size_t cchBuf)
|
---|
1336 | {
|
---|
1337 | AssertReturn(pStream->fWriteOrRead, VERR_ACCESS_DENIED);
|
---|
1338 | if (RT_FAILURE(pStream->rc))
|
---|
1339 | return pStream->rc;
|
---|
1340 |
|
---|
1341 | /*
|
---|
1342 | * Ensure we've got sufficient buffer space.
|
---|
1343 | */
|
---|
1344 | size_t off = pStream->off;
|
---|
1345 | if (RT_UNLIKELY(off + cchBuf > pStream->cbAllocated))
|
---|
1346 | {
|
---|
1347 | int rc = scmStreamGrowBuffer(pStream, cchBuf);
|
---|
1348 | if (RT_FAILURE(rc))
|
---|
1349 | return rc;
|
---|
1350 | }
|
---|
1351 |
|
---|
1352 | /*
|
---|
1353 | * Deal with the odd case where we've already pushed a line with SCMEOL_NONE.
|
---|
1354 | */
|
---|
1355 | size_t iLine = pStream->iLine;
|
---|
1356 | if (RT_UNLIKELY( iLine > 0
|
---|
1357 | && pStream->paLines[iLine - 1].enmEol == SCMEOL_NONE))
|
---|
1358 | {
|
---|
1359 | iLine--;
|
---|
1360 | pStream->cLines = iLine;
|
---|
1361 | pStream->iLine = iLine;
|
---|
1362 | }
|
---|
1363 |
|
---|
1364 | /*
|
---|
1365 | * Deal with lines.
|
---|
1366 | */
|
---|
1367 | const char *pchLF = (const char *)memchr(pchBuf, '\n', cchBuf);
|
---|
1368 | if (!pchLF)
|
---|
1369 | pStream->paLines[iLine].cch += cchBuf;
|
---|
1370 | else
|
---|
1371 | {
|
---|
1372 | const char *pchLine = pchBuf;
|
---|
1373 | for (;;)
|
---|
1374 | {
|
---|
1375 | if (RT_UNLIKELY(iLine + 1 >= pStream->cLinesAllocated))
|
---|
1376 | {
|
---|
1377 | int rc = scmStreamGrowLines(pStream, iLine);
|
---|
1378 | if (RT_FAILURE(rc))
|
---|
1379 | {
|
---|
1380 | iLine = pStream->iLine;
|
---|
1381 | pStream->paLines[iLine].cch = off - pStream->paLines[iLine].off;
|
---|
1382 | pStream->paLines[iLine].enmEol = SCMEOL_NONE;
|
---|
1383 | return rc;
|
---|
1384 | }
|
---|
1385 | }
|
---|
1386 |
|
---|
1387 | size_t cchLine = pchLF - pchLine;
|
---|
1388 | if ( cchLine
|
---|
1389 | ? pchLF[-1] != '\r'
|
---|
1390 | : !pStream->paLines[iLine].cch
|
---|
1391 | || pStream->pch[pStream->paLines[iLine].off + pStream->paLines[iLine].cch - 1] != '\r')
|
---|
1392 | pStream->paLines[iLine].enmEol = SCMEOL_LF;
|
---|
1393 | else
|
---|
1394 | {
|
---|
1395 | pStream->paLines[iLine].enmEol = SCMEOL_CRLF;
|
---|
1396 | cchLine--;
|
---|
1397 | }
|
---|
1398 | pStream->paLines[iLine].cch += cchLine;
|
---|
1399 |
|
---|
1400 | iLine++;
|
---|
1401 | size_t offBuf = pchLF + 1 - pchBuf;
|
---|
1402 | pStream->paLines[iLine].off = off + offBuf;
|
---|
1403 | pStream->paLines[iLine].cch = 0;
|
---|
1404 | pStream->paLines[iLine].enmEol = SCMEOL_NONE;
|
---|
1405 |
|
---|
1406 | size_t cchLeft = cchBuf - offBuf;
|
---|
1407 | pchLF = (const char *)memchr(pchLF + 1, '\n', cchLeft);
|
---|
1408 | if (!pchLF)
|
---|
1409 | {
|
---|
1410 | pStream->paLines[iLine].cch = cchLeft;
|
---|
1411 | break;
|
---|
1412 | }
|
---|
1413 | }
|
---|
1414 |
|
---|
1415 | pStream->iLine = iLine;
|
---|
1416 | pStream->cLines = iLine;
|
---|
1417 | }
|
---|
1418 |
|
---|
1419 | /*
|
---|
1420 | * Copy the data and update position and size.
|
---|
1421 | */
|
---|
1422 | memcpy(&pStream->pch[off], pchBuf, cchBuf);
|
---|
1423 | off += cchBuf;
|
---|
1424 | pStream->off = off;
|
---|
1425 | pStream->cb = off;
|
---|
1426 |
|
---|
1427 | return VINF_SUCCESS;
|
---|
1428 | }
|
---|
1429 |
|
---|
1430 | /**
|
---|
1431 | * Write a character to the stream.
|
---|
1432 | *
|
---|
1433 | * @returns IPRT status code
|
---|
1434 | * @param pStream The stream. Must be in write mode.
|
---|
1435 | * @param pchBuf What to write.
|
---|
1436 | * @param cchBuf How much to write.
|
---|
1437 | */
|
---|
1438 | int ScmStreamPutCh(PSCMSTREAM pStream, char ch)
|
---|
1439 | {
|
---|
1440 | AssertReturn(pStream->fWriteOrRead, VERR_ACCESS_DENIED);
|
---|
1441 | if (RT_FAILURE(pStream->rc))
|
---|
1442 | return pStream->rc;
|
---|
1443 |
|
---|
1444 | /*
|
---|
1445 | * Only deal with the simple cases here, use ScmStreamWrite for the
|
---|
1446 | * annyoing stuff.
|
---|
1447 | */
|
---|
1448 | size_t off = pStream->off;
|
---|
1449 | if ( ch == '\n'
|
---|
1450 | || RT_UNLIKELY(off + 1 > pStream->cbAllocated))
|
---|
1451 | return ScmStreamWrite(pStream, &ch, 1);
|
---|
1452 |
|
---|
1453 | /*
|
---|
1454 | * Just append it.
|
---|
1455 | */
|
---|
1456 | pStream->pch[off] = ch;
|
---|
1457 | pStream->off = off + 1;
|
---|
1458 | pStream->paLines[pStream->iLine].cch++;
|
---|
1459 |
|
---|
1460 | return VINF_SUCCESS;
|
---|
1461 | }
|
---|
1462 |
|
---|
1463 | /**
|
---|
1464 | * Copies @a cLines from the @a pSrc stream onto the @a pDst stream.
|
---|
1465 | *
|
---|
1466 | * The stream positions will be used and changed in both streams.
|
---|
1467 | *
|
---|
1468 | * @returns IPRT status code.
|
---|
1469 | * @param pDst The destionation stream. Must be in write mode.
|
---|
1470 | * @param cLines The number of lines. (0 is accepted.)
|
---|
1471 | * @param pSrc The source stream. Must be in read mode.
|
---|
1472 | */
|
---|
1473 | int ScmStreamCopyLines(PSCMSTREAM pDst, PSCMSTREAM pSrc, size_t cLines)
|
---|
1474 | {
|
---|
1475 | AssertReturn(pDst->fWriteOrRead, VERR_ACCESS_DENIED);
|
---|
1476 | if (RT_FAILURE(pDst->rc))
|
---|
1477 | return pDst->rc;
|
---|
1478 |
|
---|
1479 | AssertReturn(!pSrc->fWriteOrRead, VERR_ACCESS_DENIED);
|
---|
1480 | if (RT_FAILURE(pSrc->rc))
|
---|
1481 | return pSrc->rc;
|
---|
1482 |
|
---|
1483 | while (cLines-- > 0)
|
---|
1484 | {
|
---|
1485 | SCMEOL enmEol;
|
---|
1486 | size_t cchLine;
|
---|
1487 | const char *pchLine = ScmStreamGetLine(pSrc, &cchLine, &enmEol);
|
---|
1488 | if (!pchLine)
|
---|
1489 | return pDst->rc = (RT_FAILURE(pSrc->rc) ? pSrc->rc : VERR_EOF);
|
---|
1490 |
|
---|
1491 | int rc = ScmStreamPutLine(pDst, pchLine, cchLine, enmEol);
|
---|
1492 | if (RT_FAILURE(rc))
|
---|
1493 | return rc;
|
---|
1494 | }
|
---|
1495 |
|
---|
1496 | return VINF_SUCCESS;
|
---|
1497 | }
|
---|
1498 |
|
---|
1499 | /* -=-=-=-=-=- diff -=-=-=-=-=- */
|
---|
1500 |
|
---|
1501 |
|
---|
1502 | /**
|
---|
1503 | * Prints a range of lines with a prefix.
|
---|
1504 | *
|
---|
1505 | * @param pState The diff state.
|
---|
1506 | * @param chPrefix The prefix.
|
---|
1507 | * @param pStream The stream to get the lines from.
|
---|
1508 | * @param iLine The first line.
|
---|
1509 | * @param cLines The number of lines.
|
---|
1510 | */
|
---|
1511 | static void scmDiffPrintLines(PSCMDIFFSTATE pState, char chPrefix, PSCMSTREAM pStream, size_t iLine, size_t cLines)
|
---|
1512 | {
|
---|
1513 | while (cLines-- > 0)
|
---|
1514 | {
|
---|
1515 | SCMEOL enmEol;
|
---|
1516 | size_t cchLine;
|
---|
1517 | const char *pchLine = ScmStreamGetLineByNo(pStream, iLine, &cchLine, &enmEol);
|
---|
1518 |
|
---|
1519 | RTStrmPutCh(pState->pDiff, chPrefix);
|
---|
1520 | if (pchLine && cchLine)
|
---|
1521 | {
|
---|
1522 | if (!pState->fSpecialChars)
|
---|
1523 | RTStrmWrite(pState->pDiff, pchLine, cchLine);
|
---|
1524 | else
|
---|
1525 | {
|
---|
1526 | size_t offVir = 0;
|
---|
1527 | const char *pchStart = pchLine;
|
---|
1528 | const char *pchTab = (const char *)memchr(pchLine, '\t', cchLine);
|
---|
1529 | while (pchTab)
|
---|
1530 | {
|
---|
1531 | RTStrmWrite(pState->pDiff, pchStart, pchTab - pchStart);
|
---|
1532 | offVir += pchTab - pchStart;
|
---|
1533 |
|
---|
1534 | size_t cchTab = pState->cchTab - offVir % pState->cchTab;
|
---|
1535 | switch (cchTab)
|
---|
1536 | {
|
---|
1537 | case 1: RTStrmPutStr(pState->pDiff, "."); break;
|
---|
1538 | case 2: RTStrmPutStr(pState->pDiff, ".."); break;
|
---|
1539 | case 3: RTStrmPutStr(pState->pDiff, "[T]"); break;
|
---|
1540 | case 4: RTStrmPutStr(pState->pDiff, "[TA]"); break;
|
---|
1541 | case 5: RTStrmPutStr(pState->pDiff, "[TAB]"); break;
|
---|
1542 | default: RTStrmPrintf(pState->pDiff, "[TAB%.*s]", cchTab - 5, g_szTabSpaces); break;
|
---|
1543 | }
|
---|
1544 | offVir += cchTab;
|
---|
1545 |
|
---|
1546 | /* next */
|
---|
1547 | pchStart = pchTab + 1;
|
---|
1548 | pchTab = (const char *)memchr(pchStart, '\t', cchLine - (pchStart - pchLine));
|
---|
1549 | }
|
---|
1550 | size_t cchLeft = cchLine - (pchStart - pchLine);
|
---|
1551 | if (cchLeft)
|
---|
1552 | RTStrmWrite(pState->pDiff, pchStart, cchLeft);
|
---|
1553 | }
|
---|
1554 | }
|
---|
1555 |
|
---|
1556 | if (!pState->fSpecialChars)
|
---|
1557 | RTStrmPutCh(pState->pDiff, '\n');
|
---|
1558 | else if (enmEol == SCMEOL_LF)
|
---|
1559 | RTStrmPutStr(pState->pDiff, "[LF]\n");
|
---|
1560 | else if (enmEol == SCMEOL_CRLF)
|
---|
1561 | RTStrmPutStr(pState->pDiff, "[CRLF]\n");
|
---|
1562 | else
|
---|
1563 | RTStrmPutStr(pState->pDiff, "[NONE]\n");
|
---|
1564 |
|
---|
1565 | iLine++;
|
---|
1566 | }
|
---|
1567 | }
|
---|
1568 |
|
---|
1569 |
|
---|
1570 | /**
|
---|
1571 | * Reports a difference and propells the streams to the lines following the
|
---|
1572 | * resync.
|
---|
1573 | *
|
---|
1574 | *
|
---|
1575 | * @returns New pState->cDiff value (just to return something).
|
---|
1576 | * @param pState The diff state. The cDiffs member will be
|
---|
1577 | * incremented.
|
---|
1578 | * @param cMatches The resync length.
|
---|
1579 | * @param iLeft Where the difference starts on the left side.
|
---|
1580 | * @param cLeft How long it is on this side. ~(size_t)0 is used
|
---|
1581 | * to indicate that it goes all the way to the end.
|
---|
1582 | * @param iRight Where the difference starts on the right side.
|
---|
1583 | * @param cRight How long it is.
|
---|
1584 | */
|
---|
1585 | static size_t scmDiffReport(PSCMDIFFSTATE pState, size_t cMatches,
|
---|
1586 | size_t iLeft, size_t cLeft,
|
---|
1587 | size_t iRight, size_t cRight)
|
---|
1588 | {
|
---|
1589 | /*
|
---|
1590 | * Adjust the input.
|
---|
1591 | */
|
---|
1592 | if (cLeft == ~(size_t)0)
|
---|
1593 | {
|
---|
1594 | size_t c = ScmStreamCountLines(pState->pLeft);
|
---|
1595 | if (c >= iLeft)
|
---|
1596 | cLeft = c - iLeft;
|
---|
1597 | else
|
---|
1598 | {
|
---|
1599 | iLeft = c;
|
---|
1600 | cLeft = 0;
|
---|
1601 | }
|
---|
1602 | }
|
---|
1603 |
|
---|
1604 | if (cRight == ~(size_t)0)
|
---|
1605 | {
|
---|
1606 | size_t c = ScmStreamCountLines(pState->pRight);
|
---|
1607 | if (c >= iRight)
|
---|
1608 | cRight = c - iRight;
|
---|
1609 | else
|
---|
1610 | {
|
---|
1611 | iRight = c;
|
---|
1612 | cRight = 0;
|
---|
1613 | }
|
---|
1614 | }
|
---|
1615 |
|
---|
1616 | /*
|
---|
1617 | * Print header if it's the first difference
|
---|
1618 | */
|
---|
1619 | if (!pState->cDiffs)
|
---|
1620 | RTStrmPrintf(pState->pDiff, "diff %s %s\n", pState->pszFilename, pState->pszFilename);
|
---|
1621 |
|
---|
1622 | /*
|
---|
1623 | * Emit the change description.
|
---|
1624 | */
|
---|
1625 | char ch = cLeft == 0
|
---|
1626 | ? 'a'
|
---|
1627 | : cRight == 0
|
---|
1628 | ? 'd'
|
---|
1629 | : 'c';
|
---|
1630 | if (cLeft > 1 && cRight > 1)
|
---|
1631 | RTStrmPrintf(pState->pDiff, "%zu,%zu%c%zu,%zu\n", iLeft + 1, iLeft + cLeft, ch, iRight + 1, iRight + cRight);
|
---|
1632 | else if (cLeft > 1)
|
---|
1633 | RTStrmPrintf(pState->pDiff, "%zu,%zu%c%zu\n", iLeft + 1, iLeft + cLeft, ch, iRight + 1);
|
---|
1634 | else if (cRight > 1)
|
---|
1635 | RTStrmPrintf(pState->pDiff, "%zu%c%zu,%zu\n", iLeft + 1, ch, iRight + 1, iRight + cRight);
|
---|
1636 | else
|
---|
1637 | RTStrmPrintf(pState->pDiff, "%zu%c%zu\n", iLeft + 1, ch, iRight + 1);
|
---|
1638 |
|
---|
1639 | /*
|
---|
1640 | * And the lines.
|
---|
1641 | */
|
---|
1642 | if (cLeft)
|
---|
1643 | scmDiffPrintLines(pState, '<', pState->pLeft, iLeft, cLeft);
|
---|
1644 | if (cLeft && cRight)
|
---|
1645 | RTStrmPrintf(pState->pDiff, "---\n");
|
---|
1646 | if (cRight)
|
---|
1647 | scmDiffPrintLines(pState, '>', pState->pRight, iRight, cRight);
|
---|
1648 |
|
---|
1649 | /*
|
---|
1650 | * Reposition the streams (safely ignores return value).
|
---|
1651 | */
|
---|
1652 | ScmStreamSeekByLine(pState->pLeft, iLeft + cLeft + cMatches);
|
---|
1653 | ScmStreamSeekByLine(pState->pRight, iRight + cRight + cMatches);
|
---|
1654 |
|
---|
1655 | pState->cDiffs++;
|
---|
1656 | return pState->cDiffs;
|
---|
1657 | }
|
---|
1658 |
|
---|
1659 | /**
|
---|
1660 | * Helper for scmDiffCompare that takes care of trailing spaces and stuff
|
---|
1661 | * like that.
|
---|
1662 | */
|
---|
1663 | static bool scmDiffCompareSlow(PSCMDIFFSTATE pState,
|
---|
1664 | const char *pchLeft, size_t cchLeft, SCMEOL enmEolLeft,
|
---|
1665 | const char *pchRight, size_t cchRight, SCMEOL enmEolRight)
|
---|
1666 | {
|
---|
1667 | if (pState->fIgnoreTrailingWhite)
|
---|
1668 | {
|
---|
1669 | while (cchLeft > 0 && RT_C_IS_SPACE(pchLeft[cchLeft - 1]))
|
---|
1670 | cchLeft--;
|
---|
1671 | while (cchRight > 0 && RT_C_IS_SPACE(pchRight[cchRight - 1]))
|
---|
1672 | cchRight--;
|
---|
1673 | }
|
---|
1674 |
|
---|
1675 | if (pState->fIgnoreLeadingWhite)
|
---|
1676 | {
|
---|
1677 | while (cchLeft > 0 && RT_C_IS_SPACE(*pchLeft))
|
---|
1678 | pchLeft++, cchLeft--;
|
---|
1679 | while (cchRight > 0 && RT_C_IS_SPACE(*pchRight))
|
---|
1680 | pchRight++, cchRight--;
|
---|
1681 | }
|
---|
1682 |
|
---|
1683 | if ( cchLeft != cchRight
|
---|
1684 | || (enmEolLeft != enmEolRight && !pState->fIgnoreEol)
|
---|
1685 | || memcmp(pchLeft, pchRight, cchLeft))
|
---|
1686 | return false;
|
---|
1687 | return true;
|
---|
1688 | }
|
---|
1689 |
|
---|
1690 | /**
|
---|
1691 | * Compare two lines.
|
---|
1692 | *
|
---|
1693 | * @returns true if the are equal, false if not.
|
---|
1694 | */
|
---|
1695 | DECLINLINE(bool) scmDiffCompare(PSCMDIFFSTATE pState,
|
---|
1696 | const char *pchLeft, size_t cchLeft, SCMEOL enmEolLeft,
|
---|
1697 | const char *pchRight, size_t cchRight, SCMEOL enmEolRight)
|
---|
1698 | {
|
---|
1699 | if ( cchLeft != cchRight
|
---|
1700 | || (enmEolLeft != enmEolRight && !pState->fIgnoreEol)
|
---|
1701 | || memcmp(pchLeft, pchRight, cchLeft))
|
---|
1702 | {
|
---|
1703 | if ( pState->fIgnoreTrailingWhite
|
---|
1704 | || pState->fIgnoreTrailingWhite)
|
---|
1705 | return scmDiffCompareSlow(pState,
|
---|
1706 | pchLeft, cchLeft, enmEolLeft,
|
---|
1707 | pchRight, cchRight, enmEolRight);
|
---|
1708 | return false;
|
---|
1709 | }
|
---|
1710 | return true;
|
---|
1711 | }
|
---|
1712 |
|
---|
1713 | /**
|
---|
1714 | * Compares two sets of lines from the two files.
|
---|
1715 | *
|
---|
1716 | * @returns true if they matches, false if they don't.
|
---|
1717 | * @param pState The diff state.
|
---|
1718 | * @param iLeft Where to start in the left stream.
|
---|
1719 | * @param iRight Where to start in the right stream.
|
---|
1720 | * @param cLines How many lines to compare.
|
---|
1721 | */
|
---|
1722 | static bool scmDiffCompareLines(PSCMDIFFSTATE pState, size_t iLeft, size_t iRight, size_t cLines)
|
---|
1723 | {
|
---|
1724 | for (size_t iLine = 0; iLine < cLines; iLine++)
|
---|
1725 | {
|
---|
1726 | SCMEOL enmEolLeft;
|
---|
1727 | size_t cchLeft;
|
---|
1728 | const char *pchLeft = ScmStreamGetLineByNo(pState->pLeft, iLeft + iLine, &cchLeft, &enmEolLeft);
|
---|
1729 |
|
---|
1730 | SCMEOL enmEolRight;
|
---|
1731 | size_t cchRight;
|
---|
1732 | const char *pchRight = ScmStreamGetLineByNo(pState->pRight, iRight + iLine, &cchRight, &enmEolRight);
|
---|
1733 |
|
---|
1734 | if (!scmDiffCompare(pState, pchLeft, cchLeft, enmEolLeft, pchRight, cchRight, enmEolRight))
|
---|
1735 | return false;
|
---|
1736 | }
|
---|
1737 | return true;
|
---|
1738 | }
|
---|
1739 |
|
---|
1740 |
|
---|
1741 | /**
|
---|
1742 | * Resynchronize the two streams and reports the difference.
|
---|
1743 | *
|
---|
1744 | * Upon return, the streams will be positioned after the block of @a cMatches
|
---|
1745 | * lines where it resynchronized them.
|
---|
1746 | *
|
---|
1747 | * @returns pState->cDiffs (just so we can use it in a return statement).
|
---|
1748 | * @param pState The state.
|
---|
1749 | * @param cMatches The number of lines that needs to match for the
|
---|
1750 | * stream to be considered synchronized again.
|
---|
1751 | */
|
---|
1752 | static size_t scmDiffSynchronize(PSCMDIFFSTATE pState, size_t cMatches)
|
---|
1753 | {
|
---|
1754 | size_t const iStartLeft = ScmStreamTellLine(pState->pLeft) - 1;
|
---|
1755 | size_t const iStartRight = ScmStreamTellLine(pState->pRight) - 1;
|
---|
1756 | Assert(cMatches > 0);
|
---|
1757 |
|
---|
1758 | /*
|
---|
1759 | * Compare each new line from each of the streams will all the preceding
|
---|
1760 | * ones, including iStartLeft/Right.
|
---|
1761 | */
|
---|
1762 | for (size_t iRange = 1; ; iRange++)
|
---|
1763 | {
|
---|
1764 | /*
|
---|
1765 | * Get the next line in the left stream and compare it against all the
|
---|
1766 | * preceding lines on the right side.
|
---|
1767 | */
|
---|
1768 | SCMEOL enmEol;
|
---|
1769 | size_t cchLine;
|
---|
1770 | const char *pchLine = ScmStreamGetLineByNo(pState->pLeft, iStartLeft + iRange, &cchLine, &enmEol);
|
---|
1771 | if (!pchLine)
|
---|
1772 | return scmDiffReport(pState, 0, iStartLeft, ~(size_t)0, iStartRight, ~(size_t)0);
|
---|
1773 |
|
---|
1774 | for (size_t iRight = cMatches - 1; iRight < iRange; iRight++)
|
---|
1775 | {
|
---|
1776 | SCMEOL enmEolRight;
|
---|
1777 | size_t cchRight;
|
---|
1778 | const char *pchRight = ScmStreamGetLineByNo(pState->pRight, iStartRight + iRight,
|
---|
1779 | &cchRight, &enmEolRight);
|
---|
1780 | if ( scmDiffCompare(pState, pchLine, cchLine, enmEol, pchRight, cchRight, enmEolRight)
|
---|
1781 | && scmDiffCompareLines(pState,
|
---|
1782 | iStartLeft + iRange + 1 - cMatches,
|
---|
1783 | iStartRight + iRight + 1 - cMatches,
|
---|
1784 | cMatches - 1)
|
---|
1785 | )
|
---|
1786 | return scmDiffReport(pState, cMatches,
|
---|
1787 | iStartLeft, iRange + 1 - cMatches,
|
---|
1788 | iStartRight, iRight + 1 - cMatches);
|
---|
1789 | }
|
---|
1790 |
|
---|
1791 | /*
|
---|
1792 | * Get the next line in the right stream and compare it against all the
|
---|
1793 | * lines on the right side.
|
---|
1794 | */
|
---|
1795 | pchLine = ScmStreamGetLineByNo(pState->pRight, iStartRight + iRange, &cchLine, &enmEol);
|
---|
1796 | if (!pchLine)
|
---|
1797 | return scmDiffReport(pState, 0, iStartLeft, ~(size_t)0, iStartRight, ~(size_t)0);
|
---|
1798 |
|
---|
1799 | for (size_t iLeft = cMatches - 1; iLeft <= iRange; iLeft++)
|
---|
1800 | {
|
---|
1801 | SCMEOL enmEolLeft;
|
---|
1802 | size_t cchLeft;
|
---|
1803 | const char *pchLeft = ScmStreamGetLineByNo(pState->pLeft, iStartLeft + iLeft,
|
---|
1804 | &cchLeft, &enmEolLeft);
|
---|
1805 | if ( scmDiffCompare(pState, pchLeft, cchLeft, enmEolLeft, pchLine, cchLine, enmEol)
|
---|
1806 | && scmDiffCompareLines(pState,
|
---|
1807 | iStartLeft + iLeft + 1 - cMatches,
|
---|
1808 | iStartRight + iRange + 1 - cMatches,
|
---|
1809 | cMatches - 1)
|
---|
1810 | )
|
---|
1811 | return scmDiffReport(pState, cMatches,
|
---|
1812 | iStartLeft, iLeft + 1 - cMatches,
|
---|
1813 | iStartRight, iRange + 1 - cMatches);
|
---|
1814 | }
|
---|
1815 | }
|
---|
1816 | }
|
---|
1817 |
|
---|
1818 | /**
|
---|
1819 | * Creates a diff of the changes between the streams @a pLeft and @a pRight.
|
---|
1820 | *
|
---|
1821 | * This currently only implements the simplest diff format, so no contexts.
|
---|
1822 | *
|
---|
1823 | * Also, note that we won't detect differences in the final newline of the
|
---|
1824 | * streams.
|
---|
1825 | *
|
---|
1826 | * @returns The number of differences.
|
---|
1827 | * @param pszFilename The filename.
|
---|
1828 | * @param pLeft The left side stream.
|
---|
1829 | * @param pRight The right side stream.
|
---|
1830 | * @param fIgnoreEol Whether to ignore end of line markers.
|
---|
1831 | * @param fIgnoreLeadingWhite Set if leading white space should be ignored.
|
---|
1832 | * @param fIgnoreTrailingWhite Set if trailing white space should be ignored.
|
---|
1833 | * @param fSpecialChars Whether to print special chars in a human
|
---|
1834 | * readable form or not.
|
---|
1835 | * @param cchTab The tab size.
|
---|
1836 | * @param pDiff Where to write the diff.
|
---|
1837 | */
|
---|
1838 | size_t ScmDiffStreams(const char *pszFilename, PSCMSTREAM pLeft, PSCMSTREAM pRight, bool fIgnoreEol,
|
---|
1839 | bool fIgnoreLeadingWhite, bool fIgnoreTrailingWhite, bool fSpecialChars,
|
---|
1840 | size_t cchTab, PRTSTREAM pDiff)
|
---|
1841 | {
|
---|
1842 | #ifdef RT_STRICT
|
---|
1843 | ScmStreamCheckItegrity(pLeft);
|
---|
1844 | ScmStreamCheckItegrity(pRight);
|
---|
1845 | #endif
|
---|
1846 |
|
---|
1847 | /*
|
---|
1848 | * Set up the diff state.
|
---|
1849 | */
|
---|
1850 | SCMDIFFSTATE State;
|
---|
1851 | State.cDiffs = 0;
|
---|
1852 | State.pszFilename = pszFilename;
|
---|
1853 | State.pLeft = pLeft;
|
---|
1854 | State.pRight = pRight;
|
---|
1855 | State.fIgnoreEol = fIgnoreEol;
|
---|
1856 | State.fIgnoreLeadingWhite = fIgnoreLeadingWhite;
|
---|
1857 | State.fIgnoreTrailingWhite = fIgnoreTrailingWhite;
|
---|
1858 | State.fSpecialChars = fSpecialChars;
|
---|
1859 | State.cchTab = cchTab;
|
---|
1860 | State.pDiff = pDiff;
|
---|
1861 |
|
---|
1862 | /*
|
---|
1863 | * Compare them line by line.
|
---|
1864 | */
|
---|
1865 | ScmStreamRewindForReading(pLeft);
|
---|
1866 | ScmStreamRewindForReading(pRight);
|
---|
1867 | const char *pchLeft;
|
---|
1868 | const char *pchRight;
|
---|
1869 |
|
---|
1870 | for (;;)
|
---|
1871 | {
|
---|
1872 | SCMEOL enmEolLeft;
|
---|
1873 | size_t cchLeft;
|
---|
1874 | pchLeft = ScmStreamGetLine(pLeft, &cchLeft, &enmEolLeft);
|
---|
1875 |
|
---|
1876 | SCMEOL enmEolRight;
|
---|
1877 | size_t cchRight;
|
---|
1878 | pchRight = ScmStreamGetLine(pRight, &cchRight, &enmEolRight);
|
---|
1879 | if (!pchLeft || !pchRight)
|
---|
1880 | break;
|
---|
1881 |
|
---|
1882 | if (!scmDiffCompare(&State, pchLeft, cchLeft, enmEolLeft, pchRight, cchRight, enmEolRight))
|
---|
1883 | scmDiffSynchronize(&State, 3);
|
---|
1884 | }
|
---|
1885 |
|
---|
1886 | /*
|
---|
1887 | * Deal with any remaining differences.
|
---|
1888 | */
|
---|
1889 | if (pchLeft)
|
---|
1890 | scmDiffReport(&State, 0, ScmStreamTellLine(pLeft) - 1, ~(size_t)0, ScmStreamTellLine(pRight), 0);
|
---|
1891 | else if (pchRight)
|
---|
1892 | scmDiffReport(&State, 0, ScmStreamTellLine(pLeft), 0, ScmStreamTellLine(pRight) - 1, ~(size_t)0);
|
---|
1893 |
|
---|
1894 | /*
|
---|
1895 | * Report any errors.
|
---|
1896 | */
|
---|
1897 | if (RT_FAILURE(ScmStreamGetStatus(pLeft)))
|
---|
1898 | RTMsgError("Left diff stream error: %Rrc\n", ScmStreamGetStatus(pLeft));
|
---|
1899 | if (RT_FAILURE(ScmStreamGetStatus(pRight)))
|
---|
1900 | RTMsgError("Right diff stream error: %Rrc\n", ScmStreamGetStatus(pRight));
|
---|
1901 |
|
---|
1902 | return State.cDiffs;
|
---|
1903 | }
|
---|
1904 |
|
---|
1905 |
|
---|
1906 |
|
---|
1907 | /* -=-=-=-=-=- settings -=-=-=-=-=- */
|
---|
1908 |
|
---|
1909 | /**
|
---|
1910 | * Init a settings structure with settings from @a pSrc.
|
---|
1911 | *
|
---|
1912 | * @returns IPRT status code
|
---|
1913 | * @param pSettings The settings.
|
---|
1914 | * @param pSrc The source settings.
|
---|
1915 | */
|
---|
1916 | static int scmSettingsBaseInitAndCopy(PSCMSETTINGSBASE pSettings, PCSCMSETTINGSBASE pSrc)
|
---|
1917 | {
|
---|
1918 | *pSettings = *pSrc;
|
---|
1919 |
|
---|
1920 | int rc = RTStrDupEx(&pSettings->pszFilterFiles, pSrc->pszFilterFiles);
|
---|
1921 | if (RT_SUCCESS(rc))
|
---|
1922 | {
|
---|
1923 | rc = RTStrDupEx(&pSettings->pszFilterOutFiles, pSrc->pszFilterOutFiles);
|
---|
1924 | if (RT_SUCCESS(rc))
|
---|
1925 | {
|
---|
1926 | rc = RTStrDupEx(&pSettings->pszFilterOutDirs, pSrc->pszFilterOutDirs);
|
---|
1927 | if (RT_SUCCESS(rc))
|
---|
1928 | return VINF_SUCCESS;
|
---|
1929 |
|
---|
1930 | RTStrFree(pSettings->pszFilterOutFiles);
|
---|
1931 | }
|
---|
1932 | RTStrFree(pSettings->pszFilterFiles);
|
---|
1933 | }
|
---|
1934 |
|
---|
1935 | pSettings->pszFilterFiles = NULL;
|
---|
1936 | pSettings->pszFilterOutFiles = NULL;
|
---|
1937 | pSettings->pszFilterOutDirs = NULL;
|
---|
1938 | return rc;
|
---|
1939 | }
|
---|
1940 |
|
---|
1941 | /**
|
---|
1942 | * Init a settings structure.
|
---|
1943 | *
|
---|
1944 | * @returns IPRT status code
|
---|
1945 | * @param pSettings The settings.
|
---|
1946 | */
|
---|
1947 | static int scmSettingsBaseInit(PSCMSETTINGSBASE pSettings)
|
---|
1948 | {
|
---|
1949 | return scmSettingsBaseInitAndCopy(pSettings, &g_Defaults);
|
---|
1950 | }
|
---|
1951 |
|
---|
1952 | /**
|
---|
1953 | * Deletes the settings, i.e. free any dynamically allocated content.
|
---|
1954 | *
|
---|
1955 | * @param pSettings The settings.
|
---|
1956 | */
|
---|
1957 | static void scmSettingsBaseDelete(PSCMSETTINGSBASE pSettings)
|
---|
1958 | {
|
---|
1959 | if (pSettings)
|
---|
1960 | {
|
---|
1961 | Assert(pSettings->cchTab != ~(unsigned)0);
|
---|
1962 | pSettings->cchTab = ~(unsigned)0;
|
---|
1963 |
|
---|
1964 | RTStrFree(pSettings->pszFilterFiles);
|
---|
1965 | pSettings->pszFilterFiles = NULL;
|
---|
1966 |
|
---|
1967 | RTStrFree(pSettings->pszFilterOutFiles);
|
---|
1968 | pSettings->pszFilterOutFiles = NULL;
|
---|
1969 |
|
---|
1970 | RTStrFree(pSettings->pszFilterOutDirs);
|
---|
1971 | pSettings->pszFilterOutDirs = NULL;
|
---|
1972 | }
|
---|
1973 | }
|
---|
1974 |
|
---|
1975 |
|
---|
1976 | /**
|
---|
1977 | * Processes a RTGetOpt result.
|
---|
1978 | *
|
---|
1979 | * @retval VINF_SUCCESS if handled.
|
---|
1980 | * @retval VERR_OUT_OF_RANGE if the option value was out of range.
|
---|
1981 | * @retval VERR_GETOPT_UNKNOWN_OPTION if the option was not recognized.
|
---|
1982 | *
|
---|
1983 | * @param pSettings The settings to change.
|
---|
1984 | * @param rc The RTGetOpt return value.
|
---|
1985 | * @param pValueUnion The RTGetOpt value union.
|
---|
1986 | */
|
---|
1987 | static int scmSettingsBaseHandleOpt(PSCMSETTINGSBASE pSettings, int rc, PRTGETOPTUNION pValueUnion)
|
---|
1988 | {
|
---|
1989 | switch (rc)
|
---|
1990 | {
|
---|
1991 | case SCMOPT_CONVERT_EOL:
|
---|
1992 | pSettings->fConvertEol = true;
|
---|
1993 | return VINF_SUCCESS;
|
---|
1994 | case SCMOPT_NO_CONVERT_EOL:
|
---|
1995 | pSettings->fConvertEol = false;
|
---|
1996 | return VINF_SUCCESS;
|
---|
1997 |
|
---|
1998 | case SCMOPT_CONVERT_TABS:
|
---|
1999 | pSettings->fConvertTabs = true;
|
---|
2000 | return VINF_SUCCESS;
|
---|
2001 | case SCMOPT_NO_CONVERT_TABS:
|
---|
2002 | pSettings->fConvertTabs = false;
|
---|
2003 | return VINF_SUCCESS;
|
---|
2004 |
|
---|
2005 | case SCMOPT_FORCE_FINAL_EOL:
|
---|
2006 | pSettings->fForceFinalEol = true;
|
---|
2007 | return VINF_SUCCESS;
|
---|
2008 | case SCMOPT_NO_FORCE_FINAL_EOL:
|
---|
2009 | pSettings->fForceFinalEol = false;
|
---|
2010 | return VINF_SUCCESS;
|
---|
2011 |
|
---|
2012 | case SCMOPT_FORCE_TRAILING_LINE:
|
---|
2013 | pSettings->fForceTrailingLine = true;
|
---|
2014 | return VINF_SUCCESS;
|
---|
2015 | case SCMOPT_NO_FORCE_TRAILING_LINE:
|
---|
2016 | pSettings->fForceTrailingLine = false;
|
---|
2017 | return VINF_SUCCESS;
|
---|
2018 |
|
---|
2019 | case SCMOPT_STRIP_TRAILING_BLANKS:
|
---|
2020 | pSettings->fStripTrailingBlanks = true;
|
---|
2021 | return VINF_SUCCESS;
|
---|
2022 | case SCMOPT_NO_STRIP_TRAILING_BLANKS:
|
---|
2023 | pSettings->fStripTrailingBlanks = false;
|
---|
2024 | return VINF_SUCCESS;
|
---|
2025 |
|
---|
2026 | case SCMOPT_STRIP_TRAILING_LINES:
|
---|
2027 | pSettings->fStripTrailingLines = true;
|
---|
2028 | return VINF_SUCCESS;
|
---|
2029 | case SCMOPT_NO_STRIP_TRAILING_LINES:
|
---|
2030 | pSettings->fStripTrailingLines = false;
|
---|
2031 | return VINF_SUCCESS;
|
---|
2032 |
|
---|
2033 | case SCMOPT_ONLY_SVN_DIRS:
|
---|
2034 | pSettings->fOnlySvnDirs = true;
|
---|
2035 | return VINF_SUCCESS;
|
---|
2036 | case SCMOPT_NOT_ONLY_SVN_DIRS:
|
---|
2037 | pSettings->fOnlySvnDirs = false;
|
---|
2038 | return VINF_SUCCESS;
|
---|
2039 |
|
---|
2040 | case SCMOPT_ONLY_SVN_FILES:
|
---|
2041 | pSettings->fOnlySvnFiles = true;
|
---|
2042 | return VINF_SUCCESS;
|
---|
2043 | case SCMOPT_NOT_ONLY_SVN_FILES:
|
---|
2044 | pSettings->fOnlySvnFiles = false;
|
---|
2045 | return VINF_SUCCESS;
|
---|
2046 |
|
---|
2047 | case SCMOPT_SET_SVN_EOL:
|
---|
2048 | pSettings->fSetSvnEol = true;
|
---|
2049 | return VINF_SUCCESS;
|
---|
2050 | case SCMOPT_DONT_SET_SVN_EOL:
|
---|
2051 | pSettings->fSetSvnEol = false;
|
---|
2052 | return VINF_SUCCESS;
|
---|
2053 |
|
---|
2054 | case SCMOPT_SET_SVN_EXECUTABLE:
|
---|
2055 | pSettings->fSetSvnExecutable = true;
|
---|
2056 | return VINF_SUCCESS;
|
---|
2057 | case SCMOPT_DONT_SET_SVN_EXECUTABLE:
|
---|
2058 | pSettings->fSetSvnExecutable = false;
|
---|
2059 | return VINF_SUCCESS;
|
---|
2060 |
|
---|
2061 | case SCMOPT_SET_SVN_KEYWORDS:
|
---|
2062 | pSettings->fSetSvnKeywords = true;
|
---|
2063 | return VINF_SUCCESS;
|
---|
2064 | case SCMOPT_DONT_SET_SVN_KEYWORDS:
|
---|
2065 | pSettings->fSetSvnKeywords = false;
|
---|
2066 | return VINF_SUCCESS;
|
---|
2067 |
|
---|
2068 | case SCMOPT_TAB_SIZE:
|
---|
2069 | if ( pValueUnion->u8 < 1
|
---|
2070 | || pValueUnion->u8 >= RT_ELEMENTS(g_szTabSpaces))
|
---|
2071 | {
|
---|
2072 | RTMsgError("Invalid tab size: %u - must be in {1..%u}\n",
|
---|
2073 | pValueUnion->u8, RT_ELEMENTS(g_szTabSpaces) - 1);
|
---|
2074 | return VERR_OUT_OF_RANGE;
|
---|
2075 | }
|
---|
2076 | pSettings->cchTab = pValueUnion->u8;
|
---|
2077 | return VINF_SUCCESS;
|
---|
2078 |
|
---|
2079 | case SCMOPT_FILTER_OUT_DIRS:
|
---|
2080 | case SCMOPT_FILTER_FILES:
|
---|
2081 | case SCMOPT_FILTER_OUT_FILES:
|
---|
2082 | {
|
---|
2083 | char **ppsz;
|
---|
2084 | switch (rc)
|
---|
2085 | {
|
---|
2086 | case SCMOPT_FILTER_OUT_DIRS: ppsz = &pSettings->pszFilterOutDirs; break;
|
---|
2087 | case SCMOPT_FILTER_FILES: ppsz = &pSettings->pszFilterFiles; break;
|
---|
2088 | case SCMOPT_FILTER_OUT_FILES: ppsz = &pSettings->pszFilterOutFiles; break;
|
---|
2089 | }
|
---|
2090 |
|
---|
2091 | /*
|
---|
2092 | * An empty string zaps the current list.
|
---|
2093 | */
|
---|
2094 | if (!*pValueUnion->psz)
|
---|
2095 | return RTStrATruncate(ppsz, 0);
|
---|
2096 |
|
---|
2097 | /*
|
---|
2098 | * Non-empty strings are appended to the pattern list.
|
---|
2099 | *
|
---|
2100 | * Strip leading and trailing pattern separators before attempting
|
---|
2101 | * to append it. If it's just separators, don't do anything.
|
---|
2102 | */
|
---|
2103 | const char *pszSrc = pValueUnion->psz;
|
---|
2104 | while (*pszSrc == '|')
|
---|
2105 | pszSrc++;
|
---|
2106 | size_t cchSrc = strlen(pszSrc);
|
---|
2107 | while (cchSrc > 0 && pszSrc[cchSrc - 1] == '|')
|
---|
2108 | cchSrc--;
|
---|
2109 | if (!cchSrc)
|
---|
2110 | return VINF_SUCCESS;
|
---|
2111 |
|
---|
2112 | return RTStrAAppendExN(ppsz, 2,
|
---|
2113 | "|", *ppsz && **ppsz ? 1 : 0,
|
---|
2114 | pszSrc, cchSrc);
|
---|
2115 | }
|
---|
2116 |
|
---|
2117 | default:
|
---|
2118 | return VERR_GETOPT_UNKNOWN_OPTION;
|
---|
2119 | }
|
---|
2120 | }
|
---|
2121 |
|
---|
2122 | /**
|
---|
2123 | * Parses an option string.
|
---|
2124 | *
|
---|
2125 | * @returns IPRT status code.
|
---|
2126 | * @param pBase The base settings structure to apply the options
|
---|
2127 | * to.
|
---|
2128 | * @param pszOptions The options to parse.
|
---|
2129 | */
|
---|
2130 | static int scmSettingsBaseParseString(PSCMSETTINGSBASE pBase, const char *pszLine)
|
---|
2131 | {
|
---|
2132 | int cArgs;
|
---|
2133 | char **papszArgs;
|
---|
2134 | int rc = RTGetOptArgvFromString(&papszArgs, &cArgs, pszLine, NULL);
|
---|
2135 | if (RT_SUCCESS(rc))
|
---|
2136 | {
|
---|
2137 | RTGETOPTUNION ValueUnion;
|
---|
2138 | RTGETOPTSTATE GetOptState;
|
---|
2139 | rc = RTGetOptInit(&GetOptState, cArgs, papszArgs, &g_aScmOpts[0], RT_ELEMENTS(g_aScmOpts), 0, 0 /*fFlags*/);
|
---|
2140 | if (RT_SUCCESS(rc))
|
---|
2141 | {
|
---|
2142 | while ((rc = RTGetOpt(&GetOptState, &ValueUnion)) != 0)
|
---|
2143 | {
|
---|
2144 | rc = scmSettingsBaseHandleOpt(pBase, rc, &ValueUnion);
|
---|
2145 | if (RT_FAILURE(rc))
|
---|
2146 | break;
|
---|
2147 | }
|
---|
2148 | }
|
---|
2149 | RTGetOptArgvFree(papszArgs);
|
---|
2150 | }
|
---|
2151 |
|
---|
2152 | return rc;
|
---|
2153 | }
|
---|
2154 |
|
---|
2155 | /**
|
---|
2156 | * Parses an unterminated option string.
|
---|
2157 | *
|
---|
2158 | * @returns IPRT status code.
|
---|
2159 | * @param pBase The base settings structure to apply the options
|
---|
2160 | * to.
|
---|
2161 | * @param pchLine The line.
|
---|
2162 | * @param cchLine The line length.
|
---|
2163 | */
|
---|
2164 | static int scmSettingsBaseParseStringN(PSCMSETTINGSBASE pBase, const char *pchLine, size_t cchLine)
|
---|
2165 | {
|
---|
2166 | char *pszLine = RTStrDupN(pchLine, cchLine);
|
---|
2167 | if (!pszLine)
|
---|
2168 | return VERR_NO_MEMORY;
|
---|
2169 | int rc = scmSettingsBaseParseString(pBase, pszLine);
|
---|
2170 | RTStrFree(pszLine);
|
---|
2171 | return rc;
|
---|
2172 | }
|
---|
2173 |
|
---|
2174 | /**
|
---|
2175 | * Verifies the options string.
|
---|
2176 | *
|
---|
2177 | * @returns IPRT status code.
|
---|
2178 | * @param pszOptions The options to verify .
|
---|
2179 | */
|
---|
2180 | static int scmSettingsBaseVerifyString(const char *pszOptions)
|
---|
2181 | {
|
---|
2182 | SCMSETTINGSBASE Base;
|
---|
2183 | int rc = scmSettingsBaseInit(&Base);
|
---|
2184 | if (RT_SUCCESS(rc))
|
---|
2185 | {
|
---|
2186 | rc = scmSettingsBaseParseString(&Base, pszOptions);
|
---|
2187 | scmSettingsBaseDelete(&Base);
|
---|
2188 | }
|
---|
2189 | return rc;
|
---|
2190 | }
|
---|
2191 |
|
---|
2192 | /**
|
---|
2193 | * Loads settings found in editor and SCM settings directives within the
|
---|
2194 | * document (@a pStream).
|
---|
2195 | *
|
---|
2196 | * @returns IPRT status code.
|
---|
2197 | * @param pBase The settings base to load settings into.
|
---|
2198 | * @param pStream The stream to scan for settings directives.
|
---|
2199 | */
|
---|
2200 | static int scmSettingsBaseLoadFromDocument(PSCMSETTINGSBASE pBase, PSCMSTREAM pStream)
|
---|
2201 | {
|
---|
2202 | /** @todo Editor and SCM settings directives in documents. */
|
---|
2203 | return VINF_SUCCESS;
|
---|
2204 | }
|
---|
2205 |
|
---|
2206 | /**
|
---|
2207 | * Creates a new settings file struct, cloning @a pSettings.
|
---|
2208 | *
|
---|
2209 | * @returns IPRT status code.
|
---|
2210 | * @param ppSettings Where to return the new struct.
|
---|
2211 | * @param pSettingsBase The settings to inherit from.
|
---|
2212 | */
|
---|
2213 | static int scmSettingsCreate(PSCMSETTINGS *ppSettings, PCSCMSETTINGSBASE pSettingsBase)
|
---|
2214 | {
|
---|
2215 | PSCMSETTINGS pSettings = (PSCMSETTINGS)RTMemAlloc(sizeof(*pSettings));
|
---|
2216 | if (!pSettings)
|
---|
2217 | return VERR_NO_MEMORY;
|
---|
2218 | int rc = scmSettingsBaseInitAndCopy(&pSettings->Base, pSettingsBase);
|
---|
2219 | if (RT_SUCCESS(rc))
|
---|
2220 | {
|
---|
2221 | pSettings->pDown = NULL;
|
---|
2222 | pSettings->pUp = NULL;
|
---|
2223 | pSettings->paPairs = NULL;
|
---|
2224 | pSettings->cPairs = 0;
|
---|
2225 | *ppSettings = pSettings;
|
---|
2226 | return VINF_SUCCESS;
|
---|
2227 | }
|
---|
2228 | RTMemFree(pSettings);
|
---|
2229 | return rc;
|
---|
2230 | }
|
---|
2231 |
|
---|
2232 | /**
|
---|
2233 | * Destroys a settings structure.
|
---|
2234 | *
|
---|
2235 | * @param pSettings The settgins structure to destroy. NULL is OK.
|
---|
2236 | */
|
---|
2237 | static void scmSettingsDestroy(PSCMSETTINGS pSettings)
|
---|
2238 | {
|
---|
2239 | if (pSettings)
|
---|
2240 | {
|
---|
2241 | scmSettingsBaseDelete(&pSettings->Base);
|
---|
2242 | for (size_t i = 0; i < pSettings->cPairs; i++)
|
---|
2243 | {
|
---|
2244 | RTStrFree(pSettings->paPairs[i].pszPattern);
|
---|
2245 | RTStrFree(pSettings->paPairs[i].pszOptions);
|
---|
2246 | pSettings->paPairs[i].pszPattern = NULL;
|
---|
2247 | pSettings->paPairs[i].pszOptions = NULL;
|
---|
2248 | }
|
---|
2249 | RTMemFree(pSettings->paPairs);
|
---|
2250 | pSettings->paPairs = NULL;
|
---|
2251 | RTMemFree(pSettings);
|
---|
2252 | }
|
---|
2253 | }
|
---|
2254 |
|
---|
2255 | /**
|
---|
2256 | * Adds a pattern/options pair to the settings structure.
|
---|
2257 | *
|
---|
2258 | * @returns IPRT status code.
|
---|
2259 | * @param pSettings The settings.
|
---|
2260 | * @param pchLine The line containing the unparsed pair.
|
---|
2261 | * @param cchLine The length of the line.
|
---|
2262 | */
|
---|
2263 | static int scmSettingsAddPair(PSCMSETTINGS pSettings, const char *pchLine, size_t cchLine)
|
---|
2264 | {
|
---|
2265 | /*
|
---|
2266 | * Split the string.
|
---|
2267 | */
|
---|
2268 | const char *pchOptions = (const char *)memchr(pchLine, ':', cchLine);
|
---|
2269 | if (!pchOptions)
|
---|
2270 | return VERR_INVALID_PARAMETER;
|
---|
2271 | size_t cchPattern = pchOptions - pchLine;
|
---|
2272 | size_t cchOptions = cchLine - cchPattern - 1;
|
---|
2273 | pchOptions++;
|
---|
2274 |
|
---|
2275 | /* strip spaces everywhere */
|
---|
2276 | while (cchPattern > 0 && RT_C_IS_SPACE(pchLine[cchPattern - 1]))
|
---|
2277 | cchPattern--;
|
---|
2278 | while (cchPattern > 0 && RT_C_IS_SPACE(*pchLine))
|
---|
2279 | cchPattern--, pchLine++;
|
---|
2280 |
|
---|
2281 | while (cchOptions > 0 && RT_C_IS_SPACE(pchOptions[cchOptions - 1]))
|
---|
2282 | cchOptions--;
|
---|
2283 | while (cchOptions > 0 && RT_C_IS_SPACE(*pchOptions))
|
---|
2284 | cchOptions--, pchOptions++;
|
---|
2285 |
|
---|
2286 | /* Quietly ignore empty patterns and empty options. */
|
---|
2287 | if (!cchOptions || !cchPattern)
|
---|
2288 | return VINF_SUCCESS;
|
---|
2289 |
|
---|
2290 | /*
|
---|
2291 | * Add the pair and verify the option string.
|
---|
2292 | */
|
---|
2293 | uint32_t iPair = pSettings->cPairs;
|
---|
2294 | if ((iPair % 32) == 0)
|
---|
2295 | {
|
---|
2296 | void *pvNew = RTMemRealloc(pSettings->paPairs, (iPair + 32) * sizeof(pSettings->paPairs[0]));
|
---|
2297 | if (!pvNew)
|
---|
2298 | return VERR_NO_MEMORY;
|
---|
2299 | pSettings->paPairs = (PSCMPATRNOPTPAIR)pvNew;
|
---|
2300 | }
|
---|
2301 |
|
---|
2302 | pSettings->paPairs[iPair].pszPattern = RTStrDupN(pchLine, cchPattern);
|
---|
2303 | pSettings->paPairs[iPair].pszOptions = RTStrDupN(pchOptions, cchOptions);
|
---|
2304 | int rc;
|
---|
2305 | if ( pSettings->paPairs[iPair].pszPattern
|
---|
2306 | && pSettings->paPairs[iPair].pszOptions)
|
---|
2307 | rc = scmSettingsBaseVerifyString(pSettings->paPairs[iPair].pszOptions);
|
---|
2308 | else
|
---|
2309 | rc = VERR_NO_MEMORY;
|
---|
2310 | if (RT_SUCCESS(rc))
|
---|
2311 | pSettings->cPairs = iPair + 1;
|
---|
2312 | else
|
---|
2313 | {
|
---|
2314 | RTStrFree(pSettings->paPairs[iPair].pszPattern);
|
---|
2315 | RTStrFree(pSettings->paPairs[iPair].pszOptions);
|
---|
2316 | }
|
---|
2317 | return rc;
|
---|
2318 | }
|
---|
2319 |
|
---|
2320 | /**
|
---|
2321 | * Loads in the settings from @a pszFilename.
|
---|
2322 | *
|
---|
2323 | * @returns IPRT status code.
|
---|
2324 | * @param pSettings Where to load the settings file.
|
---|
2325 | * @param pszFilename The file to load.
|
---|
2326 | */
|
---|
2327 | static int scmSettingsLoadFile(PSCMSETTINGS pSettings, const char *pszFilename)
|
---|
2328 | {
|
---|
2329 | SCMSTREAM Stream;
|
---|
2330 | int rc = ScmStreamInitForReading(&Stream, pszFilename);
|
---|
2331 | if (RT_FAILURE(rc))
|
---|
2332 | {
|
---|
2333 | RTMsgError("%s: ScmStreamInitForReading -> %Rrc\n", pszFilename, rc);
|
---|
2334 | return rc;
|
---|
2335 | }
|
---|
2336 |
|
---|
2337 | SCMEOL enmEol;
|
---|
2338 | const char *pchLine;
|
---|
2339 | size_t cchLine;
|
---|
2340 | while ((pchLine = ScmStreamGetLine(&Stream, &cchLine, &enmEol)) != NULL)
|
---|
2341 | {
|
---|
2342 | /* Ignore leading spaces. */
|
---|
2343 | while (cchLine > 0 && RT_C_IS_SPACE(*pchLine))
|
---|
2344 | pchLine++, cchLine--;
|
---|
2345 |
|
---|
2346 | /* Ignore empty lines and comment lines. */
|
---|
2347 | if (cchLine < 1 || *pchLine == '#')
|
---|
2348 | continue;
|
---|
2349 |
|
---|
2350 | /* What kind of line is it? */
|
---|
2351 | const char *pchColon = (const char *)memchr(pchLine, ':', cchLine);
|
---|
2352 | if (pchColon)
|
---|
2353 | rc = scmSettingsAddPair(pSettings, pchLine, cchLine);
|
---|
2354 | else
|
---|
2355 | rc = scmSettingsBaseParseStringN(&pSettings->Base, pchLine, cchLine);
|
---|
2356 | if (RT_FAILURE(rc))
|
---|
2357 | {
|
---|
2358 | RTMsgError("%s:%d: %Rrc\n", pszFilename, ScmStreamTellLine(&Stream), rc);
|
---|
2359 | break;
|
---|
2360 | }
|
---|
2361 | }
|
---|
2362 |
|
---|
2363 | if (RT_SUCCESS(rc))
|
---|
2364 | {
|
---|
2365 | rc = ScmStreamGetStatus(&Stream);
|
---|
2366 | if (RT_FAILURE(rc))
|
---|
2367 | RTMsgError("%s: ScmStreamGetStatus- > %Rrc\n", pszFilename, rc);
|
---|
2368 | }
|
---|
2369 |
|
---|
2370 | ScmStreamDelete(&Stream);
|
---|
2371 | return rc;
|
---|
2372 | }
|
---|
2373 |
|
---|
2374 | /**
|
---|
2375 | * Parse the specified settings file creating a new settings struct from it.
|
---|
2376 | *
|
---|
2377 | * @returns IPRT status code
|
---|
2378 | * @param ppSettings Where to return the new settings.
|
---|
2379 | * @param pszFilename The file to parse.
|
---|
2380 | * @param pSettingsBase The base settings we inherit from.
|
---|
2381 | */
|
---|
2382 | static int scmSettingsCreateFromFile(PSCMSETTINGS *ppSettings, const char *pszFilename, PCSCMSETTINGSBASE pSettingsBase)
|
---|
2383 | {
|
---|
2384 | PSCMSETTINGS pSettings;
|
---|
2385 | int rc = scmSettingsCreate(&pSettings, pSettingsBase);
|
---|
2386 | if (RT_SUCCESS(rc))
|
---|
2387 | {
|
---|
2388 | rc = scmSettingsLoadFile(pSettings, pszFilename);
|
---|
2389 | if (RT_SUCCESS(rc))
|
---|
2390 | {
|
---|
2391 | *ppSettings = pSettings;
|
---|
2392 | return VINF_SUCCESS;
|
---|
2393 | }
|
---|
2394 |
|
---|
2395 | scmSettingsDestroy(pSettings);
|
---|
2396 | }
|
---|
2397 | *ppSettings = NULL;
|
---|
2398 | return rc;
|
---|
2399 | }
|
---|
2400 |
|
---|
2401 |
|
---|
2402 | /**
|
---|
2403 | * Create an initial settings structure when starting processing a new file or
|
---|
2404 | * directory.
|
---|
2405 | *
|
---|
2406 | * This will look for .scm-settings files from the root and down to the
|
---|
2407 | * specified directory, combining them into the returned settings structure.
|
---|
2408 | *
|
---|
2409 | * @returns IPRT status code.
|
---|
2410 | * @param ppSettings Where to return the pointer to the top stack
|
---|
2411 | * object.
|
---|
2412 | * @param pBaseSettings The base settings we inherit from (globals
|
---|
2413 | * typically).
|
---|
2414 | * @param pszPath The absolute path to the new directory or file.
|
---|
2415 | */
|
---|
2416 | static int scmSettingsCreateForPath(PSCMSETTINGS *ppSettings, PCSCMSETTINGSBASE pBaseSettings, const char *pszPath)
|
---|
2417 | {
|
---|
2418 | /*
|
---|
2419 | * We'll be working with a stack copy of the path.
|
---|
2420 | */
|
---|
2421 | char szFile[RTPATH_MAX];
|
---|
2422 | size_t cchDir = strlen(pszPath);
|
---|
2423 | if (cchDir >= sizeof(szFile) - sizeof(SCM_SETTINGS_FILENAME))
|
---|
2424 | return VERR_FILENAME_TOO_LONG;
|
---|
2425 |
|
---|
2426 | /*
|
---|
2427 | * Create the bottom-most settings.
|
---|
2428 | */
|
---|
2429 | PSCMSETTINGS pSettings;
|
---|
2430 | int rc = scmSettingsCreate(&pSettings, pBaseSettings);
|
---|
2431 | if (RT_FAILURE(rc))
|
---|
2432 | return rc;
|
---|
2433 |
|
---|
2434 | /*
|
---|
2435 | * Enumerate the path components from the root and down. Load any setting
|
---|
2436 | * files we find.
|
---|
2437 | */
|
---|
2438 | size_t cComponents = RTPathCountComponents(pszPath);
|
---|
2439 | for (size_t i = 1; i <= cComponents; i++)
|
---|
2440 | {
|
---|
2441 | rc = RTPathCopyComponents(szFile, sizeof(szFile), pszPath, i);
|
---|
2442 | if (RT_SUCCESS(rc))
|
---|
2443 | rc = RTPathAppend(szFile, sizeof(szFile), SCM_SETTINGS_FILENAME);
|
---|
2444 | if (RT_FAILURE(rc))
|
---|
2445 | break;
|
---|
2446 | if (RTFileExists(szFile))
|
---|
2447 | {
|
---|
2448 | rc = scmSettingsLoadFile(pSettings, szFile);
|
---|
2449 | if (RT_FAILURE(rc))
|
---|
2450 | break;
|
---|
2451 | }
|
---|
2452 | }
|
---|
2453 |
|
---|
2454 | if (RT_SUCCESS(rc))
|
---|
2455 | *ppSettings = pSettings;
|
---|
2456 | else
|
---|
2457 | scmSettingsDestroy(pSettings);
|
---|
2458 | return rc;
|
---|
2459 | }
|
---|
2460 |
|
---|
2461 | /**
|
---|
2462 | * Pushes a new settings set onto the stack.
|
---|
2463 | *
|
---|
2464 | * @param ppSettingsStack The pointer to the pointer to the top stack
|
---|
2465 | * element. This will be used as input and output.
|
---|
2466 | * @param pSettings The settings to push onto the stack.
|
---|
2467 | */
|
---|
2468 | static void scmSettingsStackPush(PSCMSETTINGS *ppSettingsStack, PSCMSETTINGS pSettings)
|
---|
2469 | {
|
---|
2470 | PSCMSETTINGS pOld = *ppSettingsStack;
|
---|
2471 | pSettings->pDown = pOld;
|
---|
2472 | pSettings->pUp = NULL;
|
---|
2473 | if (pOld)
|
---|
2474 | pOld->pUp = pSettings;
|
---|
2475 | *ppSettingsStack = pSettings;
|
---|
2476 | }
|
---|
2477 |
|
---|
2478 | /**
|
---|
2479 | * Pushes the settings of the specified directory onto the stack.
|
---|
2480 | *
|
---|
2481 | * We will load any .scm-settings in the directory. A stack entry is added even
|
---|
2482 | * if no settings file was found.
|
---|
2483 | *
|
---|
2484 | * @returns IPRT status code.
|
---|
2485 | * @param ppSettingsStack The pointer to the pointer to the top stack
|
---|
2486 | * element. This will be used as input and output.
|
---|
2487 | * @param pszDir The directory to do this for.
|
---|
2488 | */
|
---|
2489 | static int scmSettingsStackPushDir(PSCMSETTINGS *ppSettingsStack, const char *pszDir)
|
---|
2490 | {
|
---|
2491 | char szFile[RTPATH_MAX];
|
---|
2492 | int rc = RTPathJoin(szFile, sizeof(szFile), pszDir, SCM_SETTINGS_FILENAME);
|
---|
2493 | if (RT_SUCCESS(rc))
|
---|
2494 | {
|
---|
2495 | PSCMSETTINGS pSettings;
|
---|
2496 | rc = scmSettingsCreate(&pSettings, &(*ppSettingsStack)->Base);
|
---|
2497 | if (RT_SUCCESS(rc))
|
---|
2498 | {
|
---|
2499 | if (RTFileExists(szFile))
|
---|
2500 | rc = scmSettingsLoadFile(pSettings, szFile);
|
---|
2501 | if (RT_SUCCESS(rc))
|
---|
2502 | {
|
---|
2503 | scmSettingsStackPush(ppSettingsStack, pSettings);
|
---|
2504 | return VINF_SUCCESS;
|
---|
2505 | }
|
---|
2506 |
|
---|
2507 | scmSettingsDestroy(pSettings);
|
---|
2508 | }
|
---|
2509 | }
|
---|
2510 | return rc;
|
---|
2511 | }
|
---|
2512 |
|
---|
2513 |
|
---|
2514 | /**
|
---|
2515 | * Pops a settings set off the stack.
|
---|
2516 | *
|
---|
2517 | * @returns The popped setttings.
|
---|
2518 | * @param ppSettingsStack The pointer to the pointer to the top stack
|
---|
2519 | * element. This will be used as input and output.
|
---|
2520 | */
|
---|
2521 | static PSCMSETTINGS scmSettingsStackPop(PSCMSETTINGS *ppSettingsStack)
|
---|
2522 | {
|
---|
2523 | PSCMSETTINGS pRet = *ppSettingsStack;
|
---|
2524 | PSCMSETTINGS pNew = pRet ? pRet->pDown : NULL;
|
---|
2525 | *ppSettingsStack = pNew;
|
---|
2526 | if (pNew)
|
---|
2527 | pNew->pUp = NULL;
|
---|
2528 | if (pRet)
|
---|
2529 | {
|
---|
2530 | pRet->pUp = NULL;
|
---|
2531 | pRet->pDown = NULL;
|
---|
2532 | }
|
---|
2533 | return pRet;
|
---|
2534 | }
|
---|
2535 |
|
---|
2536 | /**
|
---|
2537 | * Pops and destroys the top entry of the stack.
|
---|
2538 | *
|
---|
2539 | * @param ppSettingsStack The pointer to the pointer to the top stack
|
---|
2540 | * element. This will be used as input and output.
|
---|
2541 | */
|
---|
2542 | static void scmSettingsStackPopAndDestroy(PSCMSETTINGS *ppSettingsStack)
|
---|
2543 | {
|
---|
2544 | scmSettingsDestroy(scmSettingsStackPop(ppSettingsStack));
|
---|
2545 | }
|
---|
2546 |
|
---|
2547 | /**
|
---|
2548 | * Constructs the base settings for the specified file name.
|
---|
2549 | *
|
---|
2550 | * @returns IPRT status code.
|
---|
2551 | * @param pSettingsStack The top element on the settings stack.
|
---|
2552 | * @param pszFilename The file name.
|
---|
2553 | * @param pszBasename The base name (pointer within @a pszFilename).
|
---|
2554 | * @param cchBasename The length of the base name. (For passing to
|
---|
2555 | * RTStrSimplePatternMultiMatch.)
|
---|
2556 | * @param pBase Base settings to initialize.
|
---|
2557 | */
|
---|
2558 | static int scmSettingsStackMakeFileBase(PCSCMSETTINGS pSettingsStack, const char *pszFilename,
|
---|
2559 | const char *pszBasename, size_t cchBasename, PSCMSETTINGSBASE pBase)
|
---|
2560 | {
|
---|
2561 | int rc = scmSettingsBaseInitAndCopy(pBase, &pSettingsStack->Base);
|
---|
2562 | if (RT_SUCCESS(rc))
|
---|
2563 | {
|
---|
2564 | /* find the bottom entry in the stack. */
|
---|
2565 | PCSCMSETTINGS pCur = pSettingsStack;
|
---|
2566 | while (pCur->pDown)
|
---|
2567 | pCur = pCur->pDown;
|
---|
2568 |
|
---|
2569 | /* Work our way up thru the stack and look for matching pairs. */
|
---|
2570 | while (pCur)
|
---|
2571 | {
|
---|
2572 | size_t const cPairs = pCur->cPairs;
|
---|
2573 | if (cPairs)
|
---|
2574 | {
|
---|
2575 | for (size_t i = 0; i < cPairs; i++)
|
---|
2576 | if ( RTStrSimplePatternMultiMatch(pCur->paPairs[i].pszPattern, RTSTR_MAX,
|
---|
2577 | pszBasename, cchBasename, NULL)
|
---|
2578 | || RTStrSimplePatternMultiMatch(pCur->paPairs[i].pszPattern, RTSTR_MAX,
|
---|
2579 | pszFilename, RTSTR_MAX, NULL))
|
---|
2580 | {
|
---|
2581 | rc = scmSettingsBaseParseString(pBase, pCur->paPairs[i].pszOptions);
|
---|
2582 | if (RT_FAILURE(rc))
|
---|
2583 | break;
|
---|
2584 | }
|
---|
2585 | if (RT_FAILURE(rc))
|
---|
2586 | break;
|
---|
2587 | }
|
---|
2588 |
|
---|
2589 | /* advance */
|
---|
2590 | pCur = pCur->pUp;
|
---|
2591 | }
|
---|
2592 | }
|
---|
2593 | if (RT_FAILURE(rc))
|
---|
2594 | scmSettingsBaseDelete(pBase);
|
---|
2595 | return rc;
|
---|
2596 | }
|
---|
2597 |
|
---|
2598 |
|
---|
2599 | /* -=-=-=-=-=- misc -=-=-=-=-=- */
|
---|
2600 |
|
---|
2601 |
|
---|
2602 | /**
|
---|
2603 | * Prints a verbose message if the level is high enough.
|
---|
2604 | *
|
---|
2605 | * @param pState The rewrite state. Optional.
|
---|
2606 | * @param iLevel The required verbosity level.
|
---|
2607 | * @param pszFormat The message format string. Can be NULL if we
|
---|
2608 | * only want to trigger the per file message.
|
---|
2609 | * @param ... Format arguments.
|
---|
2610 | */
|
---|
2611 | static void ScmVerbose(PSCMRWSTATE pState, int iLevel, const char *pszFormat, ...)
|
---|
2612 | {
|
---|
2613 | if (iLevel <= g_iVerbosity)
|
---|
2614 | {
|
---|
2615 | if (pState && !pState->fFirst)
|
---|
2616 | {
|
---|
2617 | RTPrintf("%s: info: --= Rewriting '%s' =--\n", g_szProgName, pState->pszFilename);
|
---|
2618 | pState->fFirst = true;
|
---|
2619 | }
|
---|
2620 | if (pszFormat)
|
---|
2621 | {
|
---|
2622 | RTPrintf(pState
|
---|
2623 | ? "%s: info: "
|
---|
2624 | : "%s: info: ",
|
---|
2625 | g_szProgName);
|
---|
2626 | va_list va;
|
---|
2627 | va_start(va, pszFormat);
|
---|
2628 | RTPrintfV(pszFormat, va);
|
---|
2629 | va_end(va);
|
---|
2630 | }
|
---|
2631 | }
|
---|
2632 | }
|
---|
2633 |
|
---|
2634 |
|
---|
2635 | /* -=-=-=-=-=- subversion -=-=-=-=-=- */
|
---|
2636 |
|
---|
2637 | #define SCM_WITHOUT_LIBSVN
|
---|
2638 |
|
---|
2639 | #ifdef SCM_WITHOUT_LIBSVN
|
---|
2640 |
|
---|
2641 | /**
|
---|
2642 | * Callback that is call for each path to search.
|
---|
2643 | */
|
---|
2644 | static DECLCALLBACK(int) scmSvnFindSvnBinaryCallback(char const *pchPath, size_t cchPath, void *pvUser1, void *pvUser2)
|
---|
2645 | {
|
---|
2646 | char *pszDst = (char *)pvUser1;
|
---|
2647 | size_t cchDst = (size_t)pvUser2;
|
---|
2648 | if (cchDst > cchPath)
|
---|
2649 | {
|
---|
2650 | memcpy(pszDst, pchPath, cchPath);
|
---|
2651 | pszDst[cchPath] = '\0';
|
---|
2652 | #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|
---|
2653 | int rc = RTPathAppend(pszDst, cchDst, "svn.exe");
|
---|
2654 | #else
|
---|
2655 | int rc = RTPathAppend(pszDst, cchDst, "svn");
|
---|
2656 | #endif
|
---|
2657 | if ( RT_SUCCESS(rc)
|
---|
2658 | && RTFileExists(pszDst))
|
---|
2659 | return VINF_SUCCESS;
|
---|
2660 | }
|
---|
2661 | return VERR_TRY_AGAIN;
|
---|
2662 | }
|
---|
2663 |
|
---|
2664 |
|
---|
2665 | /**
|
---|
2666 | * Finds the svn binary.
|
---|
2667 | *
|
---|
2668 | * @param pszPath Where to store it. Worst case, we'll return
|
---|
2669 | * "svn" here.
|
---|
2670 | * @param cchPath The size of the buffer pointed to by @a pszPath.
|
---|
2671 | */
|
---|
2672 | static void scmSvnFindSvnBinary(char *pszPath, size_t cchPath)
|
---|
2673 | {
|
---|
2674 | /** @todo code page fun... */
|
---|
2675 | Assert(cchPath >= sizeof("svn"));
|
---|
2676 | #ifdef RT_OS_WINDOWS
|
---|
2677 | const char *pszEnvVar = RTEnvGet("Path");
|
---|
2678 | #else
|
---|
2679 | const char *pszEnvVar = RTEnvGet("PATH");
|
---|
2680 | #endif
|
---|
2681 | if (pszPath)
|
---|
2682 | {
|
---|
2683 | #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|
---|
2684 | int rc = RTPathTraverseList(pszEnvVar, ';', scmSvnFindSvnBinaryCallback, pszPath, (void *)cchPath);
|
---|
2685 | #else
|
---|
2686 | int rc = RTPathTraverseList(pszEnvVar, ':', scmSvnFindSvnBinaryCallback, pszPath, (void *)cchPath);
|
---|
2687 | #endif
|
---|
2688 | if (RT_SUCCESS(rc))
|
---|
2689 | return;
|
---|
2690 | }
|
---|
2691 | strcpy(pszPath, "svn");
|
---|
2692 | }
|
---|
2693 |
|
---|
2694 |
|
---|
2695 | /**
|
---|
2696 | * Construct a dot svn filename for the file being rewritten.
|
---|
2697 | *
|
---|
2698 | * @returns IPRT status code.
|
---|
2699 | * @param pState The rewrite state (for the name).
|
---|
2700 | * @param pszDir The directory, including ".svn/".
|
---|
2701 | * @param pszSuff The filename suffix.
|
---|
2702 | * @param pszDst The output buffer. RTPATH_MAX in size.
|
---|
2703 | */
|
---|
2704 | static int scmSvnConstructName(PSCMRWSTATE pState, const char *pszDir, const char *pszSuff, char *pszDst)
|
---|
2705 | {
|
---|
2706 | strcpy(pszDst, pState->pszFilename); /* ASSUMES sizeof(szBuf) <= sizeof(szPath) */
|
---|
2707 | RTPathStripFilename(pszDst);
|
---|
2708 |
|
---|
2709 | int rc = RTPathAppend(pszDst, RTPATH_MAX, pszDir);
|
---|
2710 | if (RT_SUCCESS(rc))
|
---|
2711 | {
|
---|
2712 | rc = RTPathAppend(pszDst, RTPATH_MAX, RTPathFilename(pState->pszFilename));
|
---|
2713 | if (RT_SUCCESS(rc))
|
---|
2714 | {
|
---|
2715 | size_t cchDst = strlen(pszDst);
|
---|
2716 | size_t cchSuff = strlen(pszSuff);
|
---|
2717 | if (cchDst + cchSuff < RTPATH_MAX)
|
---|
2718 | {
|
---|
2719 | memcpy(&pszDst[cchDst], pszSuff, cchSuff + 1);
|
---|
2720 | return VINF_SUCCESS;
|
---|
2721 | }
|
---|
2722 | else
|
---|
2723 | rc = VERR_BUFFER_OVERFLOW;
|
---|
2724 | }
|
---|
2725 | }
|
---|
2726 | return rc;
|
---|
2727 | }
|
---|
2728 |
|
---|
2729 | /**
|
---|
2730 | * Interprets the specified string as decimal numbers.
|
---|
2731 | *
|
---|
2732 | * @returns true if parsed successfully, false if not.
|
---|
2733 | * @param pch The string (not terminated).
|
---|
2734 | * @param cch The string length.
|
---|
2735 | * @param pu Where to return the value.
|
---|
2736 | */
|
---|
2737 | static bool scmSvnReadNumber(const char *pch, size_t cch, size_t *pu)
|
---|
2738 | {
|
---|
2739 | size_t u = 0;
|
---|
2740 | while (cch-- > 0)
|
---|
2741 | {
|
---|
2742 | char ch = *pch++;
|
---|
2743 | if (ch < '0' || ch > '9')
|
---|
2744 | return false;
|
---|
2745 | u *= 10;
|
---|
2746 | u += ch - '0';
|
---|
2747 | }
|
---|
2748 | *pu = u;
|
---|
2749 | return true;
|
---|
2750 | }
|
---|
2751 |
|
---|
2752 | #endif /* SCM_WITHOUT_LIBSVN */
|
---|
2753 |
|
---|
2754 | /**
|
---|
2755 | * Checks if the file we're operating on is part of a SVN working copy.
|
---|
2756 | *
|
---|
2757 | * @returns true if it is, false if it isn't or we cannot tell.
|
---|
2758 | * @param pState The rewrite state to work on.
|
---|
2759 | */
|
---|
2760 | static bool scmSvnIsInWorkingCopy(PSCMRWSTATE pState)
|
---|
2761 | {
|
---|
2762 | #ifdef SCM_WITHOUT_LIBSVN
|
---|
2763 | /*
|
---|
2764 | * Hack: check if the .svn/text-base/<file>.svn-base file exists.
|
---|
2765 | */
|
---|
2766 | char szPath[RTPATH_MAX];
|
---|
2767 | int rc = scmSvnConstructName(pState, ".svn/text-base/", ".svn-base", szPath);
|
---|
2768 | if (RT_SUCCESS(rc))
|
---|
2769 | return RTFileExists(szPath);
|
---|
2770 |
|
---|
2771 | #else
|
---|
2772 | NOREF(pState);
|
---|
2773 | #endif
|
---|
2774 | return false;
|
---|
2775 | }
|
---|
2776 |
|
---|
2777 | /**
|
---|
2778 | * Queries the value of an SVN property.
|
---|
2779 | *
|
---|
2780 | * This will automatically adjust for scheduled changes.
|
---|
2781 | *
|
---|
2782 | * @returns IPRT status code.
|
---|
2783 | * @retval VERR_INVALID_STATE if not a SVN WC file.
|
---|
2784 | * @retval VERR_NOT_FOUND if the property wasn't found.
|
---|
2785 | * @param pState The rewrite state to work on.
|
---|
2786 | * @param pszName The property name.
|
---|
2787 | * @param ppszValue Where to return the property value. Free this
|
---|
2788 | * using RTStrFree. Optional.
|
---|
2789 | */
|
---|
2790 | static int scmSvnQueryProperty(PSCMRWSTATE pState, const char *pszName, char **ppszValue)
|
---|
2791 | {
|
---|
2792 | /*
|
---|
2793 | * Look it up in the scheduled changes.
|
---|
2794 | */
|
---|
2795 | uint32_t i = pState->cSvnPropChanges;
|
---|
2796 | while (i-- > 0)
|
---|
2797 | if (!strcmp(pState->paSvnPropChanges[i].pszName, pszName))
|
---|
2798 | {
|
---|
2799 | const char *pszValue = pState->paSvnPropChanges[i].pszValue;
|
---|
2800 | if (!pszValue)
|
---|
2801 | return VERR_NOT_FOUND;
|
---|
2802 | if (ppszValue)
|
---|
2803 | return RTStrDupEx(ppszValue, pszValue);
|
---|
2804 | return VINF_SUCCESS;
|
---|
2805 | }
|
---|
2806 |
|
---|
2807 | #ifdef SCM_WITHOUT_LIBSVN
|
---|
2808 | /*
|
---|
2809 | * Hack: Read the .svn/props/<file>.svn-work file exists.
|
---|
2810 | */
|
---|
2811 | char szPath[RTPATH_MAX];
|
---|
2812 | int rc = scmSvnConstructName(pState, ".svn/props/", ".svn-work", szPath);
|
---|
2813 | if (RT_SUCCESS(rc) && !RTFileExists(szPath))
|
---|
2814 | rc = scmSvnConstructName(pState, ".svn/prop-base/", ".svn-base", szPath);
|
---|
2815 | if (RT_SUCCESS(rc))
|
---|
2816 | {
|
---|
2817 | SCMSTREAM Stream;
|
---|
2818 | rc = ScmStreamInitForReading(&Stream, szPath);
|
---|
2819 | if (RT_SUCCESS(rc))
|
---|
2820 | {
|
---|
2821 | /*
|
---|
2822 | * The current format is K len\n<name>\nV len\n<value>\n" ... END.
|
---|
2823 | */
|
---|
2824 | rc = VERR_NOT_FOUND;
|
---|
2825 | size_t const cchName = strlen(pszName);
|
---|
2826 | SCMEOL enmEol;
|
---|
2827 | size_t cchLine;
|
---|
2828 | const char *pchLine;
|
---|
2829 | while ((pchLine = ScmStreamGetLine(&Stream, &cchLine, &enmEol)) != NULL)
|
---|
2830 | {
|
---|
2831 | /*
|
---|
2832 | * Parse the 'K num' / 'END' line.
|
---|
2833 | */
|
---|
2834 | if ( cchLine == 3
|
---|
2835 | && !memcmp(pchLine, "END", 3))
|
---|
2836 | break;
|
---|
2837 | size_t cchKey;
|
---|
2838 | if ( cchLine < 3
|
---|
2839 | || pchLine[0] != 'K'
|
---|
2840 | || pchLine[1] != ' '
|
---|
2841 | || !scmSvnReadNumber(&pchLine[2], cchLine - 2, &cchKey)
|
---|
2842 | || cchKey == 0
|
---|
2843 | || cchKey > 4096)
|
---|
2844 | {
|
---|
2845 | RTMsgError("%s:%u: Unexpected data '%.*s'\n", szPath, ScmStreamTellLine(&Stream), cchLine, pchLine);
|
---|
2846 | rc = VERR_PARSE_ERROR;
|
---|
2847 | break;
|
---|
2848 | }
|
---|
2849 |
|
---|
2850 | /*
|
---|
2851 | * Match the key and skip to the value line. Don't bother with
|
---|
2852 | * names containing EOL markers.
|
---|
2853 | */
|
---|
2854 | size_t const offKey = ScmStreamTell(&Stream);
|
---|
2855 | bool fMatch = cchName == cchKey;
|
---|
2856 | if (fMatch)
|
---|
2857 | {
|
---|
2858 | pchLine = ScmStreamGetLine(&Stream, &cchLine, &enmEol);
|
---|
2859 | if (!pchLine)
|
---|
2860 | break;
|
---|
2861 | fMatch = cchLine == cchName
|
---|
2862 | && !memcmp(pchLine, pszName, cchName);
|
---|
2863 | }
|
---|
2864 |
|
---|
2865 | if (RT_FAILURE(ScmStreamSeekAbsolute(&Stream, offKey + cchKey)))
|
---|
2866 | break;
|
---|
2867 | if (RT_FAILURE(ScmStreamSeekByLine(&Stream, ScmStreamTellLine(&Stream) + 1)))
|
---|
2868 | break;
|
---|
2869 |
|
---|
2870 | /*
|
---|
2871 | * Read and Parse the 'V num' line.
|
---|
2872 | */
|
---|
2873 | pchLine = ScmStreamGetLine(&Stream, &cchLine, &enmEol);
|
---|
2874 | if (!pchLine)
|
---|
2875 | break;
|
---|
2876 | size_t cchValue;
|
---|
2877 | if ( cchLine < 3
|
---|
2878 | || pchLine[0] != 'V'
|
---|
2879 | || pchLine[1] != ' '
|
---|
2880 | || !scmSvnReadNumber(&pchLine[2], cchLine - 2, &cchValue)
|
---|
2881 | || cchValue > _1M)
|
---|
2882 | {
|
---|
2883 | RTMsgError("%s:%u: Unexpected data '%.*s'\n", szPath, ScmStreamTellLine(&Stream), cchLine, pchLine);
|
---|
2884 | rc = VERR_PARSE_ERROR;
|
---|
2885 | break;
|
---|
2886 | }
|
---|
2887 |
|
---|
2888 | /*
|
---|
2889 | * If we have a match, allocate a return buffer and read the
|
---|
2890 | * value into it. Otherwise skip this value and continue
|
---|
2891 | * searching.
|
---|
2892 | */
|
---|
2893 | if (fMatch)
|
---|
2894 | {
|
---|
2895 | if (!ppszValue)
|
---|
2896 | rc = VINF_SUCCESS;
|
---|
2897 | else
|
---|
2898 | {
|
---|
2899 | char *pszValue;
|
---|
2900 | rc = RTStrAllocEx(&pszValue, cchValue + 1);
|
---|
2901 | if (RT_SUCCESS(rc))
|
---|
2902 | {
|
---|
2903 | rc = ScmStreamRead(&Stream, pszValue, cchValue);
|
---|
2904 | if (RT_SUCCESS(rc))
|
---|
2905 | *ppszValue = pszValue;
|
---|
2906 | else
|
---|
2907 | RTStrFree(pszValue);
|
---|
2908 | }
|
---|
2909 | }
|
---|
2910 | break;
|
---|
2911 | }
|
---|
2912 |
|
---|
2913 | if (RT_FAILURE(ScmStreamSeekRelative(&Stream, cchValue)))
|
---|
2914 | break;
|
---|
2915 | if (RT_FAILURE(ScmStreamSeekByLine(&Stream, ScmStreamTellLine(&Stream) + 1)))
|
---|
2916 | break;
|
---|
2917 | }
|
---|
2918 |
|
---|
2919 | if (RT_FAILURE(ScmStreamGetStatus(&Stream)))
|
---|
2920 | {
|
---|
2921 | rc = ScmStreamGetStatus(&Stream);
|
---|
2922 | RTMsgError("%s: stream error %Rrc\n", szPath, rc);
|
---|
2923 | }
|
---|
2924 | ScmStreamDelete(&Stream);
|
---|
2925 | }
|
---|
2926 | }
|
---|
2927 |
|
---|
2928 | if (rc == VERR_FILE_NOT_FOUND)
|
---|
2929 | rc = VERR_NOT_FOUND;
|
---|
2930 | return rc;
|
---|
2931 |
|
---|
2932 | #else
|
---|
2933 | NOREF(pState);
|
---|
2934 | #endif
|
---|
2935 | return VERR_NOT_FOUND;
|
---|
2936 | }
|
---|
2937 |
|
---|
2938 |
|
---|
2939 | /**
|
---|
2940 | * Schedules the setting of a property.
|
---|
2941 | *
|
---|
2942 | * @returns IPRT status code.
|
---|
2943 | * @retval VERR_INVALID_STATE if not a SVN WC file.
|
---|
2944 | * @param pState The rewrite state to work on.
|
---|
2945 | * @param pszName The name of the property to set.
|
---|
2946 | * @param pszValue The value. NULL means deleting it.
|
---|
2947 | */
|
---|
2948 | static int scmSvnSetProperty(PSCMRWSTATE pState, const char *pszName, const char *pszValue)
|
---|
2949 | {
|
---|
2950 | /*
|
---|
2951 | * Update any existing entry first.
|
---|
2952 | */
|
---|
2953 | size_t i = pState->cSvnPropChanges;
|
---|
2954 | while (i-- > 0)
|
---|
2955 | if (!strcmp(pState->paSvnPropChanges[i].pszName, pszName))
|
---|
2956 | {
|
---|
2957 | if (!pszValue)
|
---|
2958 | {
|
---|
2959 | RTStrFree(pState->paSvnPropChanges[i].pszValue);
|
---|
2960 | pState->paSvnPropChanges[i].pszValue = NULL;
|
---|
2961 | }
|
---|
2962 | else
|
---|
2963 | {
|
---|
2964 | char *pszCopy;
|
---|
2965 | int rc = RTStrDupEx(&pszCopy, pszValue);
|
---|
2966 | if (RT_FAILURE(rc))
|
---|
2967 | return rc;
|
---|
2968 | pState->paSvnPropChanges[i].pszValue = pszCopy;
|
---|
2969 | }
|
---|
2970 | return VINF_SUCCESS;
|
---|
2971 | }
|
---|
2972 |
|
---|
2973 | /*
|
---|
2974 | * Insert a new entry.
|
---|
2975 | */
|
---|
2976 | i = pState->cSvnPropChanges;
|
---|
2977 | if ((i % 32) == 0)
|
---|
2978 | {
|
---|
2979 | void *pvNew = RTMemRealloc(pState->paSvnPropChanges, (i + 32) * sizeof(SCMSVNPROP));
|
---|
2980 | if (!pvNew)
|
---|
2981 | return VERR_NO_MEMORY;
|
---|
2982 | pState->paSvnPropChanges = (PSCMSVNPROP)pvNew;
|
---|
2983 | }
|
---|
2984 |
|
---|
2985 | pState->paSvnPropChanges[i].pszName = RTStrDup(pszName);
|
---|
2986 | pState->paSvnPropChanges[i].pszValue = pszValue ? RTStrDup(pszValue) : NULL;
|
---|
2987 | if ( pState->paSvnPropChanges[i].pszName
|
---|
2988 | && (pState->paSvnPropChanges[i].pszValue || !pszValue) )
|
---|
2989 | pState->cSvnPropChanges = i + 1;
|
---|
2990 | else
|
---|
2991 | {
|
---|
2992 | RTStrFree(pState->paSvnPropChanges[i].pszName);
|
---|
2993 | pState->paSvnPropChanges[i].pszName = NULL;
|
---|
2994 | RTStrFree(pState->paSvnPropChanges[i].pszValue);
|
---|
2995 | pState->paSvnPropChanges[i].pszValue = NULL;
|
---|
2996 | return VERR_NO_MEMORY;
|
---|
2997 | }
|
---|
2998 | return VINF_SUCCESS;
|
---|
2999 | }
|
---|
3000 |
|
---|
3001 |
|
---|
3002 | /**
|
---|
3003 | * Schedules a property deletion.
|
---|
3004 | *
|
---|
3005 | * @returns IPRT status code.
|
---|
3006 | * @param pState The rewrite state to work on.
|
---|
3007 | * @param pszName The name of the property to delete.
|
---|
3008 | */
|
---|
3009 | static int scmSvnDelProperty(PSCMRWSTATE pState, const char *pszName)
|
---|
3010 | {
|
---|
3011 | return scmSvnSetProperty(pState, pszName, NULL);
|
---|
3012 | }
|
---|
3013 |
|
---|
3014 |
|
---|
3015 | /**
|
---|
3016 | * Applies any SVN property changes to the work copy of the file.
|
---|
3017 | *
|
---|
3018 | * @returns IPRT status code.
|
---|
3019 | * @param pState The rewrite state which SVN property changes
|
---|
3020 | * should be applied.
|
---|
3021 | */
|
---|
3022 | static int scmSvnDisplayChanges(PSCMRWSTATE pState)
|
---|
3023 | {
|
---|
3024 | size_t i = pState->cSvnPropChanges;
|
---|
3025 | while (i-- > 0)
|
---|
3026 | {
|
---|
3027 | const char *pszName = pState->paSvnPropChanges[i].pszName;
|
---|
3028 | const char *pszValue = pState->paSvnPropChanges[i].pszValue;
|
---|
3029 | if (pszValue)
|
---|
3030 | ScmVerbose(pState, 0, "svn ps '%s' '%s' %s\n", pszName, pszValue, pState->pszFilename);
|
---|
3031 | else
|
---|
3032 | ScmVerbose(pState, 0, "svn pd '%s' %s\n", pszName, pszValue, pState->pszFilename);
|
---|
3033 | }
|
---|
3034 |
|
---|
3035 | return VINF_SUCCESS;
|
---|
3036 | }
|
---|
3037 |
|
---|
3038 | /**
|
---|
3039 | * Applies any SVN property changes to the work copy of the file.
|
---|
3040 | *
|
---|
3041 | * @returns IPRT status code.
|
---|
3042 | * @param pState The rewrite state which SVN property changes
|
---|
3043 | * should be applied.
|
---|
3044 | */
|
---|
3045 | static int scmSvnApplyChanges(PSCMRWSTATE pState)
|
---|
3046 | {
|
---|
3047 | #ifdef SCM_WITHOUT_LIBSVN
|
---|
3048 | /*
|
---|
3049 | * This sucks. We gotta find svn(.exe).
|
---|
3050 | */
|
---|
3051 | static char s_szSvnPath[RTPATH_MAX];
|
---|
3052 | if (s_szSvnPath[0] == '\0')
|
---|
3053 | scmSvnFindSvnBinary(s_szSvnPath, sizeof(s_szSvnPath));
|
---|
3054 |
|
---|
3055 | /*
|
---|
3056 | * Iterate thru the changes and apply them by starting the svn client.
|
---|
3057 | */
|
---|
3058 | for (size_t i = 0; i <pState->cSvnPropChanges; i++)
|
---|
3059 | {
|
---|
3060 | const char *apszArgv[6];
|
---|
3061 | apszArgv[0] = s_szSvnPath;
|
---|
3062 | apszArgv[1] = pState->paSvnPropChanges[i].pszValue ? "ps" : "pd";
|
---|
3063 | apszArgv[2] = pState->paSvnPropChanges[i].pszName;
|
---|
3064 | int iArg = 3;
|
---|
3065 | if (pState->paSvnPropChanges[i].pszValue)
|
---|
3066 | apszArgv[iArg++] = pState->paSvnPropChanges[i].pszValue;
|
---|
3067 | apszArgv[iArg++] = pState->pszFilename;
|
---|
3068 | apszArgv[iArg++] = NULL;
|
---|
3069 | ScmVerbose(pState, 2, "executing: %s %s %s %s %s\n",
|
---|
3070 | apszArgv[0], apszArgv[1], apszArgv[2], apszArgv[3], apszArgv[4]);
|
---|
3071 |
|
---|
3072 | RTPROCESS pid;
|
---|
3073 | int rc = RTProcCreate(s_szSvnPath, apszArgv, RTENV_DEFAULT, 0 /*fFlags*/, &pid);
|
---|
3074 | if (RT_SUCCESS(rc))
|
---|
3075 | {
|
---|
3076 | RTPROCSTATUS Status;
|
---|
3077 | rc = RTProcWait(pid, RTPROCWAIT_FLAGS_BLOCK, &Status);
|
---|
3078 | if ( RT_SUCCESS(rc)
|
---|
3079 | && ( Status.enmReason != RTPROCEXITREASON_NORMAL
|
---|
3080 | || Status.iStatus != 0) )
|
---|
3081 | {
|
---|
3082 | RTMsgError("%s: %s %s %s %s %s -> %s %u\n",
|
---|
3083 | pState->pszFilename, apszArgv[0], apszArgv[1], apszArgv[2], apszArgv[3], apszArgv[4],
|
---|
3084 | Status.enmReason == RTPROCEXITREASON_NORMAL ? "exit code"
|
---|
3085 | : Status.enmReason == RTPROCEXITREASON_SIGNAL ? "signal"
|
---|
3086 | : Status.enmReason == RTPROCEXITREASON_ABEND ? "abnormal end"
|
---|
3087 | : "abducted by alien",
|
---|
3088 | Status.iStatus);
|
---|
3089 | return VERR_GENERAL_FAILURE;
|
---|
3090 | }
|
---|
3091 | }
|
---|
3092 | if (RT_FAILURE(rc))
|
---|
3093 | {
|
---|
3094 | RTMsgError("%s: error executing %s %s %s %s %s: %Rrc\n",
|
---|
3095 | pState->pszFilename, apszArgv[0], apszArgv[1], apszArgv[2], apszArgv[3], apszArgv[4], rc);
|
---|
3096 | return rc;
|
---|
3097 | }
|
---|
3098 | }
|
---|
3099 |
|
---|
3100 | return VINF_SUCCESS;
|
---|
3101 | #else
|
---|
3102 | return VERR_NOT_IMPLEMENTED;
|
---|
3103 | #endif
|
---|
3104 | }
|
---|
3105 |
|
---|
3106 |
|
---|
3107 | /* -=-=-=-=-=- rewriters -=-=-=-=-=- */
|
---|
3108 |
|
---|
3109 |
|
---|
3110 | /**
|
---|
3111 | * Strip trailing blanks (space & tab).
|
---|
3112 | *
|
---|
3113 | * @returns True if modified, false if not.
|
---|
3114 | * @param pIn The input stream.
|
---|
3115 | * @param pOut The output stream.
|
---|
3116 | * @param pSettings The settings.
|
---|
3117 | */
|
---|
3118 | static bool rewrite_StripTrailingBlanks(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
|
---|
3119 | {
|
---|
3120 | if (!pSettings->fStripTrailingBlanks)
|
---|
3121 | return false;
|
---|
3122 |
|
---|
3123 | bool fModified = false;
|
---|
3124 | SCMEOL enmEol;
|
---|
3125 | size_t cchLine;
|
---|
3126 | const char *pchLine;
|
---|
3127 | while ((pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol)) != NULL)
|
---|
3128 | {
|
---|
3129 | int rc;
|
---|
3130 | if ( cchLine == 0
|
---|
3131 | || !RT_C_IS_BLANK(pchLine[cchLine - 1]) )
|
---|
3132 | rc = ScmStreamPutLine(pOut, pchLine, cchLine, enmEol);
|
---|
3133 | else
|
---|
3134 | {
|
---|
3135 | cchLine--;
|
---|
3136 | while (cchLine > 0 && RT_C_IS_BLANK(pchLine[cchLine - 1]))
|
---|
3137 | cchLine--;
|
---|
3138 | rc = ScmStreamPutLine(pOut, pchLine, cchLine, enmEol);
|
---|
3139 | fModified = true;
|
---|
3140 | }
|
---|
3141 | if (RT_FAILURE(rc))
|
---|
3142 | return false;
|
---|
3143 | }
|
---|
3144 | if (fModified)
|
---|
3145 | ScmVerbose(pState, 2, " * Stripped trailing blanks\n");
|
---|
3146 | return fModified;
|
---|
3147 | }
|
---|
3148 |
|
---|
3149 | /**
|
---|
3150 | * Expand tabs.
|
---|
3151 | *
|
---|
3152 | * @returns True if modified, false if not.
|
---|
3153 | * @param pIn The input stream.
|
---|
3154 | * @param pOut The output stream.
|
---|
3155 | * @param pSettings The settings.
|
---|
3156 | */
|
---|
3157 | static bool rewrite_ExpandTabs(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
|
---|
3158 | {
|
---|
3159 | if (!pSettings->fConvertTabs)
|
---|
3160 | return false;
|
---|
3161 |
|
---|
3162 | size_t const cchTab = pSettings->cchTab;
|
---|
3163 | bool fModified = false;
|
---|
3164 | SCMEOL enmEol;
|
---|
3165 | size_t cchLine;
|
---|
3166 | const char *pchLine;
|
---|
3167 | while ((pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol)) != NULL)
|
---|
3168 | {
|
---|
3169 | int rc;
|
---|
3170 | const char *pchTab = (const char *)memchr(pchLine, '\t', cchLine);
|
---|
3171 | if (!pchTab)
|
---|
3172 | rc = ScmStreamPutLine(pOut, pchLine, cchLine, enmEol);
|
---|
3173 | else
|
---|
3174 | {
|
---|
3175 | size_t offTab = 0;
|
---|
3176 | const char *pchChunk = pchLine;
|
---|
3177 | for (;;)
|
---|
3178 | {
|
---|
3179 | size_t cchChunk = pchTab - pchChunk;
|
---|
3180 | offTab += cchChunk;
|
---|
3181 | ScmStreamWrite(pOut, pchChunk, cchChunk);
|
---|
3182 |
|
---|
3183 | size_t cchToTab = cchTab - offTab % cchTab;
|
---|
3184 | ScmStreamWrite(pOut, g_szTabSpaces, cchToTab);
|
---|
3185 | offTab += cchToTab;
|
---|
3186 |
|
---|
3187 | pchChunk = pchTab + 1;
|
---|
3188 | size_t cchLeft = cchLine - (pchChunk - pchLine);
|
---|
3189 | pchTab = (const char *)memchr(pchChunk, '\t', cchLeft);
|
---|
3190 | if (!pchTab)
|
---|
3191 | {
|
---|
3192 | rc = ScmStreamPutLine(pOut, pchChunk, cchLeft, enmEol);
|
---|
3193 | break;
|
---|
3194 | }
|
---|
3195 | }
|
---|
3196 |
|
---|
3197 | fModified = true;
|
---|
3198 | }
|
---|
3199 | if (RT_FAILURE(rc))
|
---|
3200 | return false;
|
---|
3201 | }
|
---|
3202 | if (fModified)
|
---|
3203 | ScmVerbose(pState, 2, " * Expanded tabs\n");
|
---|
3204 | return fModified;
|
---|
3205 | }
|
---|
3206 |
|
---|
3207 | /**
|
---|
3208 | * Worker for rewrite_ForceNativeEol, rewrite_ForceLF and rewrite_ForceCRLF.
|
---|
3209 | *
|
---|
3210 | * @returns true if modifications were made, false if not.
|
---|
3211 | * @param pIn The input stream.
|
---|
3212 | * @param pOut The output stream.
|
---|
3213 | * @param pSettings The settings.
|
---|
3214 | * @param enmDesiredEol The desired end of line indicator type.
|
---|
3215 | * @param pszDesiredSvnEol The desired svn:eol-style.
|
---|
3216 | */
|
---|
3217 | static bool rewrite_ForceEol(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings,
|
---|
3218 | SCMEOL enmDesiredEol, const char *pszDesiredSvnEol)
|
---|
3219 | {
|
---|
3220 | if (!pSettings->fConvertEol)
|
---|
3221 | return false;
|
---|
3222 |
|
---|
3223 | bool fModified = false;
|
---|
3224 | SCMEOL enmEol;
|
---|
3225 | size_t cchLine;
|
---|
3226 | const char *pchLine;
|
---|
3227 | while ((pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol)) != NULL)
|
---|
3228 | {
|
---|
3229 | if ( enmEol != enmDesiredEol
|
---|
3230 | && enmEol != SCMEOL_NONE)
|
---|
3231 | {
|
---|
3232 | fModified = true;
|
---|
3233 | enmEol = enmDesiredEol;
|
---|
3234 | }
|
---|
3235 | int rc = ScmStreamPutLine(pOut, pchLine, cchLine, enmEol);
|
---|
3236 | if (RT_FAILURE(rc))
|
---|
3237 | return false;
|
---|
3238 | }
|
---|
3239 | if (fModified)
|
---|
3240 | ScmVerbose(pState, 2, " * Converted EOL markers\n");
|
---|
3241 |
|
---|
3242 | /* Check svn:eol-style if appropriate */
|
---|
3243 | if ( pSettings->fSetSvnEol
|
---|
3244 | && scmSvnIsInWorkingCopy(pState))
|
---|
3245 | {
|
---|
3246 | char *pszEol;
|
---|
3247 | int rc = scmSvnQueryProperty(pState, "svn:eol-style", &pszEol);
|
---|
3248 | if ( (RT_SUCCESS(rc) && strcmp(pszEol, pszDesiredSvnEol))
|
---|
3249 | || rc == VERR_NOT_FOUND)
|
---|
3250 | {
|
---|
3251 | if (rc == VERR_NOT_FOUND)
|
---|
3252 | ScmVerbose(pState, 2, " * Setting svn:eol-style to %s (missing)\n", pszDesiredSvnEol);
|
---|
3253 | else
|
---|
3254 | ScmVerbose(pState, 2, " * Setting svn:eol-style to %s (was: %s)\n", pszDesiredSvnEol, pszEol);
|
---|
3255 | int rc2 = scmSvnSetProperty(pState, "svn:eol-style", pszDesiredSvnEol);
|
---|
3256 | if (RT_FAILURE(rc2))
|
---|
3257 | RTMsgError("scmSvnSetProperty: %Rrc\n", rc2); /** @todo propagate the error somehow... */
|
---|
3258 | }
|
---|
3259 | if (RT_SUCCESS(rc))
|
---|
3260 | RTStrFree(pszEol);
|
---|
3261 | }
|
---|
3262 |
|
---|
3263 | /** @todo also check the subversion svn:eol-style state! */
|
---|
3264 | return fModified;
|
---|
3265 | }
|
---|
3266 |
|
---|
3267 | /**
|
---|
3268 | * Force native end of line indicator.
|
---|
3269 | *
|
---|
3270 | * @returns true if modifications were made, false if not.
|
---|
3271 | * @param pIn The input stream.
|
---|
3272 | * @param pOut The output stream.
|
---|
3273 | * @param pSettings The settings.
|
---|
3274 | */
|
---|
3275 | static bool rewrite_ForceNativeEol(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
|
---|
3276 | {
|
---|
3277 | #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|
---|
3278 | return rewrite_ForceEol(pState, pIn, pOut, pSettings, SCMEOL_CRLF, "native");
|
---|
3279 | #else
|
---|
3280 | return rewrite_ForceEol(pState, pIn, pOut, pSettings, SCMEOL_LF, "native");
|
---|
3281 | #endif
|
---|
3282 | }
|
---|
3283 |
|
---|
3284 | /**
|
---|
3285 | * Force the stream to use LF as the end of line indicator.
|
---|
3286 | *
|
---|
3287 | * @returns true if modifications were made, false if not.
|
---|
3288 | * @param pIn The input stream.
|
---|
3289 | * @param pOut The output stream.
|
---|
3290 | * @param pSettings The settings.
|
---|
3291 | */
|
---|
3292 | static bool rewrite_ForceLF(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
|
---|
3293 | {
|
---|
3294 | return rewrite_ForceEol(pState, pIn, pOut, pSettings, SCMEOL_LF, "LF");
|
---|
3295 | }
|
---|
3296 |
|
---|
3297 | /**
|
---|
3298 | * Force the stream to use CRLF as the end of line indicator.
|
---|
3299 | *
|
---|
3300 | * @returns true if modifications were made, false if not.
|
---|
3301 | * @param pIn The input stream.
|
---|
3302 | * @param pOut The output stream.
|
---|
3303 | * @param pSettings The settings.
|
---|
3304 | */
|
---|
3305 | static bool rewrite_ForceCRLF(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
|
---|
3306 | {
|
---|
3307 | return rewrite_ForceEol(pState, pIn, pOut, pSettings, SCMEOL_CRLF, "CRLF");
|
---|
3308 | }
|
---|
3309 |
|
---|
3310 | /**
|
---|
3311 | * Strip trailing blank lines and/or make sure there is exactly one blank line
|
---|
3312 | * at the end of the file.
|
---|
3313 | *
|
---|
3314 | * @returns true if modifications were made, false if not.
|
---|
3315 | * @param pIn The input stream.
|
---|
3316 | * @param pOut The output stream.
|
---|
3317 | * @param pSettings The settings.
|
---|
3318 | *
|
---|
3319 | * @remarks ASSUMES trailing white space has been removed already.
|
---|
3320 | */
|
---|
3321 | static bool rewrite_AdjustTrailingLines(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
|
---|
3322 | {
|
---|
3323 | if ( !pSettings->fStripTrailingLines
|
---|
3324 | && !pSettings->fForceTrailingLine
|
---|
3325 | && !pSettings->fForceFinalEol)
|
---|
3326 | return false;
|
---|
3327 |
|
---|
3328 | size_t const cLines = ScmStreamCountLines(pIn);
|
---|
3329 |
|
---|
3330 | /* Empty files remains empty. */
|
---|
3331 | if (cLines <= 1)
|
---|
3332 | return false;
|
---|
3333 |
|
---|
3334 | /* Figure out if we need to adjust the number of lines or not. */
|
---|
3335 | size_t cLinesNew = cLines;
|
---|
3336 |
|
---|
3337 | if ( pSettings->fStripTrailingLines
|
---|
3338 | && ScmStreamIsWhiteLine(pIn, cLinesNew - 1))
|
---|
3339 | {
|
---|
3340 | while ( cLinesNew > 1
|
---|
3341 | && ScmStreamIsWhiteLine(pIn, cLinesNew - 2))
|
---|
3342 | cLinesNew--;
|
---|
3343 | }
|
---|
3344 |
|
---|
3345 | if ( pSettings->fForceTrailingLine
|
---|
3346 | && !ScmStreamIsWhiteLine(pIn, cLinesNew - 1))
|
---|
3347 | cLinesNew++;
|
---|
3348 |
|
---|
3349 | bool fFixMissingEol = pSettings->fForceFinalEol
|
---|
3350 | && ScmStreamGetEolByLine(pIn, cLinesNew - 1) == SCMEOL_NONE;
|
---|
3351 |
|
---|
3352 | if ( !fFixMissingEol
|
---|
3353 | && cLines == cLinesNew)
|
---|
3354 | return false;
|
---|
3355 |
|
---|
3356 | /* Copy the number of lines we've arrived at. */
|
---|
3357 | ScmStreamRewindForReading(pIn);
|
---|
3358 |
|
---|
3359 | size_t cCopied = RT_MIN(cLinesNew, cLines);
|
---|
3360 | ScmStreamCopyLines(pOut, pIn, cCopied);
|
---|
3361 |
|
---|
3362 | if (cCopied != cLinesNew)
|
---|
3363 | {
|
---|
3364 | while (cCopied++ < cLinesNew)
|
---|
3365 | ScmStreamPutLine(pOut, "", 0, ScmStreamGetEol(pIn));
|
---|
3366 | }
|
---|
3367 | /* Fix missing EOL if required. */
|
---|
3368 | else if (fFixMissingEol)
|
---|
3369 | {
|
---|
3370 | if (ScmStreamGetEol(pIn) == SCMEOL_LF)
|
---|
3371 | ScmStreamWrite(pOut, "\n", 1);
|
---|
3372 | else
|
---|
3373 | ScmStreamWrite(pOut, "\r\n", 2);
|
---|
3374 | }
|
---|
3375 |
|
---|
3376 | ScmVerbose(pState, 2, " * Adjusted trailing blank lines\n");
|
---|
3377 | return true;
|
---|
3378 | }
|
---|
3379 |
|
---|
3380 | /**
|
---|
3381 | * Make sure there is no svn:executable keyword on the current file.
|
---|
3382 | *
|
---|
3383 | * @returns false - the state carries these kinds of changes.
|
---|
3384 | * @param pState The rewriter state.
|
---|
3385 | * @param pIn The input stream.
|
---|
3386 | * @param pOut The output stream.
|
---|
3387 | * @param pSettings The settings.
|
---|
3388 | */
|
---|
3389 | static bool rewrite_SvnNoExecutable(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
|
---|
3390 | {
|
---|
3391 | if ( !pSettings->fSetSvnExecutable
|
---|
3392 | || !scmSvnIsInWorkingCopy(pState))
|
---|
3393 | return false;
|
---|
3394 |
|
---|
3395 | int rc = scmSvnQueryProperty(pState, "svn:executable", NULL);
|
---|
3396 | if (RT_SUCCESS(rc))
|
---|
3397 | {
|
---|
3398 | ScmVerbose(pState, 2, " * removing svn:executable\n");
|
---|
3399 | rc = scmSvnDelProperty(pState, "svn:executable");
|
---|
3400 | if (RT_FAILURE(rc))
|
---|
3401 | RTMsgError("scmSvnSetProperty: %Rrc\n", rc); /** @todo error propagation here.. */
|
---|
3402 | }
|
---|
3403 | return false;
|
---|
3404 | }
|
---|
3405 |
|
---|
3406 | /**
|
---|
3407 | * Make sure the Id and Revision keywords are expanded.
|
---|
3408 | *
|
---|
3409 | * @returns false - the state carries these kinds of changes.
|
---|
3410 | * @param pState The rewriter state.
|
---|
3411 | * @param pIn The input stream.
|
---|
3412 | * @param pOut The output stream.
|
---|
3413 | * @param pSettings The settings.
|
---|
3414 | */
|
---|
3415 | static bool rewrite_SvnKeywords(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
|
---|
3416 | {
|
---|
3417 | if ( !pSettings->fSetSvnKeywords
|
---|
3418 | || !scmSvnIsInWorkingCopy(pState))
|
---|
3419 | return false;
|
---|
3420 |
|
---|
3421 | char *pszKeywords;
|
---|
3422 | int rc = scmSvnQueryProperty(pState, "svn:keywords", &pszKeywords);
|
---|
3423 | if ( RT_SUCCESS(rc)
|
---|
3424 | && ( !strstr(pszKeywords, "Id") /** @todo need some function for finding a word in a string. */
|
---|
3425 | || !strstr(pszKeywords, "Revision")) )
|
---|
3426 | {
|
---|
3427 | if (!strstr(pszKeywords, "Id") && !strstr(pszKeywords, "Revision"))
|
---|
3428 | rc = RTStrAAppend(&pszKeywords, " Id Revision");
|
---|
3429 | else if (!strstr(pszKeywords, "Id"))
|
---|
3430 | rc = RTStrAAppend(&pszKeywords, " Id");
|
---|
3431 | else
|
---|
3432 | rc = RTStrAAppend(&pszKeywords, " Revision");
|
---|
3433 | if (RT_SUCCESS(rc))
|
---|
3434 | {
|
---|
3435 | ScmVerbose(pState, 2, " * changing svn:keywords to '%s'\n", pszKeywords);
|
---|
3436 | rc = scmSvnSetProperty(pState, "svn:keywords", pszKeywords);
|
---|
3437 | if (RT_FAILURE(rc))
|
---|
3438 | RTMsgError("scmSvnSetProperty: %Rrc\n", rc); /** @todo error propagation here.. */
|
---|
3439 | }
|
---|
3440 | else
|
---|
3441 | RTMsgError("RTStrAppend: %Rrc\n", rc); /** @todo error propagation here.. */
|
---|
3442 | RTStrFree(pszKeywords);
|
---|
3443 | }
|
---|
3444 | else if (rc == VERR_NOT_FOUND)
|
---|
3445 | {
|
---|
3446 | ScmVerbose(pState, 2, " * setting svn:keywords to 'Id Revision'\n");
|
---|
3447 | rc = scmSvnSetProperty(pState, "svn:keywords", "Id Revision");
|
---|
3448 | if (RT_FAILURE(rc))
|
---|
3449 | RTMsgError("scmSvnSetProperty: %Rrc\n", rc); /** @todo error propagation here.. */
|
---|
3450 | }
|
---|
3451 | else if (RT_SUCCESS(rc))
|
---|
3452 | RTStrFree(pszKeywords);
|
---|
3453 |
|
---|
3454 | return false;
|
---|
3455 | }
|
---|
3456 |
|
---|
3457 | /**
|
---|
3458 | * Makefile.kup are empty files, enforce this.
|
---|
3459 | *
|
---|
3460 | * @returns true if modifications were made, false if not.
|
---|
3461 | * @param pIn The input stream.
|
---|
3462 | * @param pOut The output stream.
|
---|
3463 | * @param pSettings The settings.
|
---|
3464 | */
|
---|
3465 | static bool rewrite_Makefile_kup(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
|
---|
3466 | {
|
---|
3467 | /* These files should be zero bytes. */
|
---|
3468 | if (pIn->cb == 0)
|
---|
3469 | return false;
|
---|
3470 | ScmVerbose(pState, 2, " * Truncated file to zero bytes\n");
|
---|
3471 | return true;
|
---|
3472 | }
|
---|
3473 |
|
---|
3474 | /**
|
---|
3475 | * Rewrite a kBuild makefile.
|
---|
3476 | *
|
---|
3477 | * @returns true if modifications were made, false if not.
|
---|
3478 | * @param pIn The input stream.
|
---|
3479 | * @param pOut The output stream.
|
---|
3480 | * @param pSettings The settings.
|
---|
3481 | *
|
---|
3482 | * @todo
|
---|
3483 | *
|
---|
3484 | * Ideas for Makefile.kmk and Config.kmk:
|
---|
3485 | * - sort if1of/ifn1of sets.
|
---|
3486 | * - line continuation slashes should only be preceeded by one space.
|
---|
3487 | */
|
---|
3488 | static bool rewrite_Makefile_kmk(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
|
---|
3489 | {
|
---|
3490 | return false;
|
---|
3491 | }
|
---|
3492 |
|
---|
3493 | /**
|
---|
3494 | * Rewrite a C/C++ source or header file.
|
---|
3495 | *
|
---|
3496 | * @returns true if modifications were made, false if not.
|
---|
3497 | * @param pIn The input stream.
|
---|
3498 | * @param pOut The output stream.
|
---|
3499 | * @param pSettings The settings.
|
---|
3500 | *
|
---|
3501 | * @todo
|
---|
3502 | *
|
---|
3503 | * Ideas for C/C++:
|
---|
3504 | * - space after if, while, for, switch
|
---|
3505 | * - spaces in for (i=0;i<x;i++)
|
---|
3506 | * - complex conditional, bird style.
|
---|
3507 | * - remove unnecessary parentheses.
|
---|
3508 | * - sort defined RT_OS_*|| and RT_ARCH
|
---|
3509 | * - sizeof without parenthesis.
|
---|
3510 | * - defined without parenthesis.
|
---|
3511 | * - trailing spaces.
|
---|
3512 | * - parameter indentation.
|
---|
3513 | * - space after comma.
|
---|
3514 | * - while (x--); -> multi line + comment.
|
---|
3515 | * - else statement;
|
---|
3516 | * - space between function and left parenthesis.
|
---|
3517 | * - TODO, XXX, @todo cleanup.
|
---|
3518 | * - Space before/after '*'.
|
---|
3519 | * - ensure new line at end of file.
|
---|
3520 | * - Indentation of precompiler statements (#ifdef, #defines).
|
---|
3521 | * - space between functions.
|
---|
3522 | */
|
---|
3523 | static bool rewrite_C_and_CPP(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)
|
---|
3524 | {
|
---|
3525 |
|
---|
3526 | return false;
|
---|
3527 | }
|
---|
3528 |
|
---|
3529 | /* -=-=-=-=-=- file and directory processing -=-=-=-=-=- */
|
---|
3530 |
|
---|
3531 | /**
|
---|
3532 | * Processes a file.
|
---|
3533 | *
|
---|
3534 | * @returns IPRT status code.
|
---|
3535 | * @param pState The rewriter state.
|
---|
3536 | * @param pszFilename The file name.
|
---|
3537 | * @param pszBasename The base name (pointer within @a pszFilename).
|
---|
3538 | * @param cchBasename The length of the base name. (For passing to
|
---|
3539 | * RTStrSimplePatternMultiMatch.)
|
---|
3540 | * @param pBaseSettings The base settings to use. It's OK to modify
|
---|
3541 | * these.
|
---|
3542 | */
|
---|
3543 | static int scmProcessFileInner(PSCMRWSTATE pState, const char *pszFilename, const char *pszBasename, size_t cchBasename,
|
---|
3544 | PSCMSETTINGSBASE pBaseSettings)
|
---|
3545 | {
|
---|
3546 | /*
|
---|
3547 | * Do the file level filtering.
|
---|
3548 | */
|
---|
3549 | if ( pBaseSettings->pszFilterFiles
|
---|
3550 | && *pBaseSettings->pszFilterFiles
|
---|
3551 | && !RTStrSimplePatternMultiMatch(pBaseSettings->pszFilterFiles, RTSTR_MAX, pszBasename, cchBasename, NULL))
|
---|
3552 | {
|
---|
3553 | ScmVerbose(NULL, 5, "skipping '%s': file filter mismatch\n", pszFilename);
|
---|
3554 | return VINF_SUCCESS;
|
---|
3555 | }
|
---|
3556 | if ( pBaseSettings->pszFilterOutFiles
|
---|
3557 | && *pBaseSettings->pszFilterOutFiles
|
---|
3558 | && ( RTStrSimplePatternMultiMatch(pBaseSettings->pszFilterOutFiles, RTSTR_MAX, pszBasename, cchBasename, NULL)
|
---|
3559 | || RTStrSimplePatternMultiMatch(pBaseSettings->pszFilterOutFiles, RTSTR_MAX, pszFilename, RTSTR_MAX, NULL)) )
|
---|
3560 | {
|
---|
3561 | ScmVerbose(NULL, 5, "skipping '%s': filterd out\n", pszFilename);
|
---|
3562 | return VINF_SUCCESS;
|
---|
3563 | }
|
---|
3564 | if ( pBaseSettings->fOnlySvnFiles
|
---|
3565 | && !scmSvnIsInWorkingCopy(pState))
|
---|
3566 | {
|
---|
3567 | ScmVerbose(NULL, 5, "skipping '%s': not in SVN WC\n", pszFilename);
|
---|
3568 | return VINF_SUCCESS;
|
---|
3569 | }
|
---|
3570 |
|
---|
3571 | /*
|
---|
3572 | * Try find a matching rewrite config for this filename.
|
---|
3573 | */
|
---|
3574 | PCSCMCFGENTRY pCfg = NULL;
|
---|
3575 | for (size_t iCfg = 0; iCfg < RT_ELEMENTS(g_aConfigs); iCfg++)
|
---|
3576 | if (RTStrSimplePatternMultiMatch(g_aConfigs[iCfg].pszFilePattern, RTSTR_MAX, pszBasename, cchBasename, NULL))
|
---|
3577 | {
|
---|
3578 | pCfg = &g_aConfigs[iCfg];
|
---|
3579 | break;
|
---|
3580 | }
|
---|
3581 | if (!pCfg)
|
---|
3582 | {
|
---|
3583 | ScmVerbose(NULL, 4, "skipping '%s': no rewriters configured\n", pszFilename);
|
---|
3584 | return VINF_SUCCESS;
|
---|
3585 | }
|
---|
3586 | ScmVerbose(pState, 4, "matched \"%s\"\n", pCfg->pszFilePattern);
|
---|
3587 |
|
---|
3588 | /*
|
---|
3589 | * Create an input stream from the file and check that it's text.
|
---|
3590 | */
|
---|
3591 | SCMSTREAM Stream1;
|
---|
3592 | int rc = ScmStreamInitForReading(&Stream1, pszFilename);
|
---|
3593 | if (RT_FAILURE(rc))
|
---|
3594 | {
|
---|
3595 | RTMsgError("Failed to read '%s': %Rrc\n", pszFilename, rc);
|
---|
3596 | return rc;
|
---|
3597 | }
|
---|
3598 | if (ScmStreamIsText(&Stream1))
|
---|
3599 | {
|
---|
3600 | ScmVerbose(pState, 3, NULL);
|
---|
3601 |
|
---|
3602 | /*
|
---|
3603 | * Gather SCM and editor settings from the stream.
|
---|
3604 | */
|
---|
3605 | rc = scmSettingsBaseLoadFromDocument(pBaseSettings, &Stream1);
|
---|
3606 | if (RT_SUCCESS(rc))
|
---|
3607 | {
|
---|
3608 | ScmStreamRewindForReading(&Stream1);
|
---|
3609 |
|
---|
3610 | /*
|
---|
3611 | * Create two more streams for output and push the text thru all the
|
---|
3612 | * rewriters, switching the two streams around when something is
|
---|
3613 | * actually rewritten. Stream1 remains unchanged.
|
---|
3614 | */
|
---|
3615 | SCMSTREAM Stream2;
|
---|
3616 | rc = ScmStreamInitForWriting(&Stream2, &Stream1);
|
---|
3617 | if (RT_SUCCESS(rc))
|
---|
3618 | {
|
---|
3619 | SCMSTREAM Stream3;
|
---|
3620 | rc = ScmStreamInitForWriting(&Stream3, &Stream1);
|
---|
3621 | if (RT_SUCCESS(rc))
|
---|
3622 | {
|
---|
3623 | bool fModified = false;
|
---|
3624 | PSCMSTREAM pIn = &Stream1;
|
---|
3625 | PSCMSTREAM pOut = &Stream2;
|
---|
3626 | for (size_t iRw = 0; iRw < pCfg->cRewriters; iRw++)
|
---|
3627 | {
|
---|
3628 | bool fRc = pCfg->papfnRewriter[iRw](pState, pIn, pOut, pBaseSettings);
|
---|
3629 | if (fRc)
|
---|
3630 | {
|
---|
3631 | PSCMSTREAM pTmp = pOut;
|
---|
3632 | pOut = pIn == &Stream1 ? &Stream3 : pIn;
|
---|
3633 | pIn = pTmp;
|
---|
3634 | fModified = true;
|
---|
3635 | }
|
---|
3636 | ScmStreamRewindForReading(pIn);
|
---|
3637 | ScmStreamRewindForWriting(pOut);
|
---|
3638 | }
|
---|
3639 |
|
---|
3640 | rc = ScmStreamGetStatus(&Stream1);
|
---|
3641 | if (RT_SUCCESS(rc))
|
---|
3642 | rc = ScmStreamGetStatus(&Stream2);
|
---|
3643 | if (RT_SUCCESS(rc))
|
---|
3644 | rc = ScmStreamGetStatus(&Stream3);
|
---|
3645 | if (RT_SUCCESS(rc))
|
---|
3646 | {
|
---|
3647 | /*
|
---|
3648 | * If rewritten, write it back to disk.
|
---|
3649 | */
|
---|
3650 | if (fModified)
|
---|
3651 | {
|
---|
3652 | if (!g_fDryRun)
|
---|
3653 | {
|
---|
3654 | ScmVerbose(pState, 1, "writing modified file to \"%s%s\"\n", pszFilename, g_pszChangedSuff);
|
---|
3655 | rc = ScmStreamWriteToFile(pIn, "%s%s", pszFilename, g_pszChangedSuff);
|
---|
3656 | if (RT_FAILURE(rc))
|
---|
3657 | RTMsgError("Error writing '%s%s': %Rrc\n", pszFilename, g_pszChangedSuff, rc);
|
---|
3658 | }
|
---|
3659 | else
|
---|
3660 | {
|
---|
3661 | ScmVerbose(pState, 1, NULL);
|
---|
3662 | ScmDiffStreams(pszFilename, &Stream1, pIn, g_fDiffIgnoreEol, g_fDiffIgnoreLeadingWS,
|
---|
3663 | g_fDiffIgnoreTrailingWS, g_fDiffSpecialChars, pBaseSettings->cchTab, g_pStdOut);
|
---|
3664 | ScmVerbose(pState, 2, "would have modified the file \"%s%s\"\n", pszFilename, g_pszChangedSuff);
|
---|
3665 | }
|
---|
3666 | }
|
---|
3667 |
|
---|
3668 | /*
|
---|
3669 | * If pending SVN property changes, apply them.
|
---|
3670 | */
|
---|
3671 | if (pState->cSvnPropChanges && RT_SUCCESS(rc))
|
---|
3672 | {
|
---|
3673 | if (!g_fDryRun)
|
---|
3674 | {
|
---|
3675 | rc = scmSvnApplyChanges(pState);
|
---|
3676 | if (RT_FAILURE(rc))
|
---|
3677 | RTMsgError("%s: failed to apply SVN property changes (%Rrc)\n", pszFilename, rc);
|
---|
3678 | }
|
---|
3679 | else
|
---|
3680 | scmSvnDisplayChanges(pState);
|
---|
3681 | }
|
---|
3682 |
|
---|
3683 | if (!fModified && !pState->cSvnPropChanges)
|
---|
3684 | ScmVerbose(pState, 3, "no change\n", pszFilename);
|
---|
3685 | }
|
---|
3686 | else
|
---|
3687 | RTMsgError("%s: stream error %Rrc\n", pszFilename);
|
---|
3688 | ScmStreamDelete(&Stream3);
|
---|
3689 | }
|
---|
3690 | else
|
---|
3691 | RTMsgError("Failed to init stream for writing: %Rrc\n", rc);
|
---|
3692 | ScmStreamDelete(&Stream2);
|
---|
3693 | }
|
---|
3694 | else
|
---|
3695 | RTMsgError("Failed to init stream for writing: %Rrc\n", rc);
|
---|
3696 | }
|
---|
3697 | else
|
---|
3698 | RTMsgError("scmSettingsBaseLoadFromDocument: %Rrc\n", rc);
|
---|
3699 | }
|
---|
3700 | else
|
---|
3701 | ScmVerbose(pState, 4, "not text file: \"%s\"\n", pszFilename);
|
---|
3702 | ScmStreamDelete(&Stream1);
|
---|
3703 |
|
---|
3704 | return rc;
|
---|
3705 | }
|
---|
3706 |
|
---|
3707 | /**
|
---|
3708 | * Processes a file.
|
---|
3709 | *
|
---|
3710 | * This is just a wrapper for scmProcessFileInner for avoid wasting stack in the
|
---|
3711 | * directory recursion method.
|
---|
3712 | *
|
---|
3713 | * @returns IPRT status code.
|
---|
3714 | * @param pszFilename The file name.
|
---|
3715 | * @param pszBasename The base name (pointer within @a pszFilename).
|
---|
3716 | * @param cchBasename The length of the base name. (For passing to
|
---|
3717 | * RTStrSimplePatternMultiMatch.)
|
---|
3718 | * @param pSettingsStack The settings stack (pointer to the top element).
|
---|
3719 | */
|
---|
3720 | static int scmProcessFile(const char *pszFilename, const char *pszBasename, size_t cchBasename,
|
---|
3721 | PSCMSETTINGS pSettingsStack)
|
---|
3722 | {
|
---|
3723 | SCMSETTINGSBASE Base;
|
---|
3724 | int rc = scmSettingsStackMakeFileBase(pSettingsStack, pszFilename, pszBasename, cchBasename, &Base);
|
---|
3725 | if (RT_SUCCESS(rc))
|
---|
3726 | {
|
---|
3727 | SCMRWSTATE State;
|
---|
3728 | State.fFirst = false;
|
---|
3729 | State.pszFilename = pszFilename;
|
---|
3730 | State.cSvnPropChanges = 0;
|
---|
3731 | State.paSvnPropChanges = NULL;
|
---|
3732 |
|
---|
3733 | rc = scmProcessFileInner(&State, pszFilename, pszBasename, cchBasename, &Base);
|
---|
3734 |
|
---|
3735 | size_t i = State.cSvnPropChanges;
|
---|
3736 | while (i-- > 0)
|
---|
3737 | {
|
---|
3738 | RTStrFree(State.paSvnPropChanges[i].pszName);
|
---|
3739 | RTStrFree(State.paSvnPropChanges[i].pszValue);
|
---|
3740 | }
|
---|
3741 | RTMemFree(State.paSvnPropChanges);
|
---|
3742 |
|
---|
3743 | scmSettingsBaseDelete(&Base);
|
---|
3744 | }
|
---|
3745 | return rc;
|
---|
3746 | }
|
---|
3747 |
|
---|
3748 |
|
---|
3749 | /**
|
---|
3750 | * Tries to correct RTDIRENTRY_UNKNOWN.
|
---|
3751 | *
|
---|
3752 | * @returns Corrected type.
|
---|
3753 | * @param pszPath The path to the object in question.
|
---|
3754 | */
|
---|
3755 | static RTDIRENTRYTYPE scmFigureUnknownType(const char *pszPath)
|
---|
3756 | {
|
---|
3757 | RTFSOBJINFO Info;
|
---|
3758 | int rc = RTPathQueryInfo(pszPath, &Info, RTFSOBJATTRADD_NOTHING);
|
---|
3759 | if (RT_FAILURE(rc))
|
---|
3760 | return RTDIRENTRYTYPE_UNKNOWN;
|
---|
3761 | if (RTFS_IS_DIRECTORY(Info.Attr.fMode))
|
---|
3762 | return RTDIRENTRYTYPE_DIRECTORY;
|
---|
3763 | if (RTFS_IS_FILE(Info.Attr.fMode))
|
---|
3764 | return RTDIRENTRYTYPE_FILE;
|
---|
3765 | return RTDIRENTRYTYPE_UNKNOWN;
|
---|
3766 | }
|
---|
3767 |
|
---|
3768 | /**
|
---|
3769 | * Recurse into a sub-directory and process all the files and directories.
|
---|
3770 | *
|
---|
3771 | * @returns IPRT status code.
|
---|
3772 | * @param pszBuf Path buffer containing the directory path on
|
---|
3773 | * entry. This ends with a dot. This is passed
|
---|
3774 | * along when recusing in order to save stack space
|
---|
3775 | * and avoid needless copying.
|
---|
3776 | * @param cchDir Length of our path in pszbuf.
|
---|
3777 | * @param pEntry Directory entry buffer. This is also passed
|
---|
3778 | * along when recursing to save stack space.
|
---|
3779 | * @param pSettingsStack The settings stack (pointer to the top element).
|
---|
3780 | * @param iRecursion The recursion depth. This is used to restrict
|
---|
3781 | * the recursions.
|
---|
3782 | */
|
---|
3783 | static int scmProcessDirTreeRecursion(char *pszBuf, size_t cchDir, PRTDIRENTRY pEntry,
|
---|
3784 | PSCMSETTINGS pSettingsStack, unsigned iRecursion)
|
---|
3785 | {
|
---|
3786 | int rc;
|
---|
3787 | Assert(cchDir > 1 && pszBuf[cchDir - 1] == '.');
|
---|
3788 |
|
---|
3789 | /*
|
---|
3790 | * Make sure we stop somewhere.
|
---|
3791 | */
|
---|
3792 | if (iRecursion > 128)
|
---|
3793 | {
|
---|
3794 | RTMsgError("recursion too deep: %d\n", iRecursion);
|
---|
3795 | return VINF_SUCCESS; /* ignore */
|
---|
3796 | }
|
---|
3797 |
|
---|
3798 | /*
|
---|
3799 | * Check if it's excluded by --only-svn-dir.
|
---|
3800 | */
|
---|
3801 | if (pSettingsStack->Base.fOnlySvnDirs)
|
---|
3802 | {
|
---|
3803 | rc = RTPathAppend(pszBuf, RTPATH_MAX, ".svn");
|
---|
3804 | if (RT_FAILURE(rc))
|
---|
3805 | {
|
---|
3806 | RTMsgError("RTPathAppend: %Rrc\n", rc);
|
---|
3807 | return rc;
|
---|
3808 | }
|
---|
3809 | if (!RTDirExists(pszBuf))
|
---|
3810 | return VINF_SUCCESS;
|
---|
3811 |
|
---|
3812 | Assert(RTPATH_IS_SLASH(pszBuf[cchDir]));
|
---|
3813 | pszBuf[cchDir] = '\0';
|
---|
3814 | pszBuf[cchDir - 1] = '.';
|
---|
3815 | }
|
---|
3816 |
|
---|
3817 | /*
|
---|
3818 | * Try open and read the directory.
|
---|
3819 | */
|
---|
3820 | PRTDIR pDir;
|
---|
3821 | rc = RTDirOpenFiltered(&pDir, pszBuf, RTDIRFILTER_NONE);
|
---|
3822 | if (RT_FAILURE(rc))
|
---|
3823 | {
|
---|
3824 | RTMsgError("Failed to enumerate directory '%s': %Rrc", pszBuf, rc);
|
---|
3825 | return rc;
|
---|
3826 | }
|
---|
3827 | for (;;)
|
---|
3828 | {
|
---|
3829 | /* Read the next entry. */
|
---|
3830 | rc = RTDirRead(pDir, pEntry, NULL);
|
---|
3831 | if (RT_FAILURE(rc))
|
---|
3832 | {
|
---|
3833 | if (rc == VERR_NO_MORE_FILES)
|
---|
3834 | rc = VINF_SUCCESS;
|
---|
3835 | else
|
---|
3836 | RTMsgError("RTDirRead -> %Rrc\n", rc);
|
---|
3837 | break;
|
---|
3838 | }
|
---|
3839 |
|
---|
3840 | /* Skip '.' and '..'. */
|
---|
3841 | if ( pEntry->szName[0] == '.'
|
---|
3842 | && ( pEntry->cbName == 1
|
---|
3843 | || ( pEntry->cbName == 2
|
---|
3844 | && pEntry->szName[1] == '.')))
|
---|
3845 | continue;
|
---|
3846 |
|
---|
3847 | /* Enter it into the buffer so we've got a full name to work
|
---|
3848 | with when needed. */
|
---|
3849 | if (pEntry->cbName + cchDir >= RTPATH_MAX)
|
---|
3850 | {
|
---|
3851 | RTMsgError("Skipping too long entry: %s", pEntry->szName);
|
---|
3852 | continue;
|
---|
3853 | }
|
---|
3854 | memcpy(&pszBuf[cchDir - 1], pEntry->szName, pEntry->cbName + 1);
|
---|
3855 |
|
---|
3856 | /* Figure the type. */
|
---|
3857 | RTDIRENTRYTYPE enmType = pEntry->enmType;
|
---|
3858 | if (enmType == RTDIRENTRYTYPE_UNKNOWN)
|
---|
3859 | enmType = scmFigureUnknownType(pszBuf);
|
---|
3860 |
|
---|
3861 | /* Process the file or directory, skip the rest. */
|
---|
3862 | if (enmType == RTDIRENTRYTYPE_FILE)
|
---|
3863 | rc = scmProcessFile(pszBuf, pEntry->szName, pEntry->cbName, pSettingsStack);
|
---|
3864 | else if (enmType == RTDIRENTRYTYPE_DIRECTORY)
|
---|
3865 | {
|
---|
3866 | /* Append the dot for the benefit of the pattern matching. */
|
---|
3867 | if (pEntry->cbName + cchDir + 5 >= RTPATH_MAX)
|
---|
3868 | {
|
---|
3869 | RTMsgError("Skipping too deep dir entry: %s", pEntry->szName);
|
---|
3870 | continue;
|
---|
3871 | }
|
---|
3872 | memcpy(&pszBuf[cchDir - 1 + pEntry->cbName], "/.", sizeof("/."));
|
---|
3873 | size_t cchSubDir = cchDir - 1 + pEntry->cbName + sizeof("/.") - 1;
|
---|
3874 |
|
---|
3875 | if ( !pSettingsStack->Base.pszFilterOutDirs
|
---|
3876 | || !*pSettingsStack->Base.pszFilterOutDirs
|
---|
3877 | || ( !RTStrSimplePatternMultiMatch(pSettingsStack->Base.pszFilterOutDirs, RTSTR_MAX,
|
---|
3878 | pEntry->szName, pEntry->cbName, NULL)
|
---|
3879 | && !RTStrSimplePatternMultiMatch(pSettingsStack->Base.pszFilterOutDirs, RTSTR_MAX,
|
---|
3880 | pszBuf, cchSubDir, NULL)
|
---|
3881 | )
|
---|
3882 | )
|
---|
3883 | {
|
---|
3884 | rc = scmSettingsStackPushDir(&pSettingsStack, pszBuf);
|
---|
3885 | if (RT_SUCCESS(rc))
|
---|
3886 | {
|
---|
3887 | rc = scmProcessDirTreeRecursion(pszBuf, cchSubDir, pEntry, pSettingsStack, iRecursion + 1);
|
---|
3888 | scmSettingsStackPopAndDestroy(&pSettingsStack);
|
---|
3889 | }
|
---|
3890 | }
|
---|
3891 | }
|
---|
3892 | if (RT_FAILURE(rc))
|
---|
3893 | break;
|
---|
3894 | }
|
---|
3895 | RTDirClose(pDir);
|
---|
3896 | return rc;
|
---|
3897 |
|
---|
3898 | }
|
---|
3899 |
|
---|
3900 | /**
|
---|
3901 | * Process a directory tree.
|
---|
3902 | *
|
---|
3903 | * @returns IPRT status code.
|
---|
3904 | * @param pszDir The directory to start with. This is pointer to
|
---|
3905 | * a RTPATH_MAX sized buffer.
|
---|
3906 | */
|
---|
3907 | static int scmProcessDirTree(char *pszDir, PSCMSETTINGS pSettingsStack)
|
---|
3908 | {
|
---|
3909 | /*
|
---|
3910 | * Setup the recursion.
|
---|
3911 | */
|
---|
3912 | int rc = RTPathAppend(pszDir, RTPATH_MAX, ".");
|
---|
3913 | if (RT_SUCCESS(rc))
|
---|
3914 | {
|
---|
3915 | RTDIRENTRY Entry;
|
---|
3916 | rc = scmProcessDirTreeRecursion(pszDir, strlen(pszDir), &Entry, pSettingsStack, 0);
|
---|
3917 | }
|
---|
3918 | else
|
---|
3919 | RTMsgError("RTPathAppend: %Rrc\n", rc);
|
---|
3920 | return rc;
|
---|
3921 | }
|
---|
3922 |
|
---|
3923 |
|
---|
3924 | /**
|
---|
3925 | * Processes a file or directory specified as an command line argument.
|
---|
3926 | *
|
---|
3927 | * @returns IPRT status code
|
---|
3928 | * @param pszSomething What we found in the commad line arguments.
|
---|
3929 | * @param pSettingsStack The settings stack (pointer to the top element).
|
---|
3930 | */
|
---|
3931 | static int scmProcessSomething(const char *pszSomething, PSCMSETTINGS pSettingsStack)
|
---|
3932 | {
|
---|
3933 | char szBuf[RTPATH_MAX];
|
---|
3934 | int rc = RTPathAbs(pszSomething, szBuf, sizeof(szBuf));
|
---|
3935 | if (RT_SUCCESS(rc))
|
---|
3936 | {
|
---|
3937 | RTPathChangeToUnixSlashes(szBuf, false /*fForce*/);
|
---|
3938 |
|
---|
3939 | PSCMSETTINGS pSettings;
|
---|
3940 | rc = scmSettingsCreateForPath(&pSettings, &pSettingsStack->Base, szBuf);
|
---|
3941 | if (RT_SUCCESS(rc))
|
---|
3942 | {
|
---|
3943 | scmSettingsStackPush(&pSettingsStack, pSettings);
|
---|
3944 |
|
---|
3945 | if (RTFileExists(szBuf))
|
---|
3946 | {
|
---|
3947 | const char *pszBasename = RTPathFilename(szBuf);
|
---|
3948 | if (pszBasename)
|
---|
3949 | {
|
---|
3950 | size_t cchBasename = strlen(pszBasename);
|
---|
3951 | rc = scmProcessFile(szBuf, pszBasename, cchBasename, pSettingsStack);
|
---|
3952 | }
|
---|
3953 | else
|
---|
3954 | {
|
---|
3955 | RTMsgError("RTPathFilename: NULL\n");
|
---|
3956 | rc = VERR_IS_A_DIRECTORY;
|
---|
3957 | }
|
---|
3958 | }
|
---|
3959 | else
|
---|
3960 | rc = scmProcessDirTree(szBuf, pSettingsStack);
|
---|
3961 |
|
---|
3962 | PSCMSETTINGS pPopped = scmSettingsStackPop(&pSettingsStack);
|
---|
3963 | Assert(pPopped == pSettings);
|
---|
3964 | scmSettingsDestroy(pSettings);
|
---|
3965 | }
|
---|
3966 | else
|
---|
3967 | RTMsgError("scmSettingsInitStack: %Rrc\n", rc);
|
---|
3968 | }
|
---|
3969 | else
|
---|
3970 | RTMsgError("RTPathAbs: %Rrc\n", rc);
|
---|
3971 | return rc;
|
---|
3972 | }
|
---|
3973 |
|
---|
3974 | int main(int argc, char **argv)
|
---|
3975 | {
|
---|
3976 | int rc = RTR3Init();
|
---|
3977 | if (RT_FAILURE(rc))
|
---|
3978 | return 1;
|
---|
3979 |
|
---|
3980 | /*
|
---|
3981 | * Init the settings.
|
---|
3982 | */
|
---|
3983 | PSCMSETTINGS pSettings;
|
---|
3984 | rc = scmSettingsCreate(&pSettings, &g_Defaults);
|
---|
3985 | if (RT_FAILURE(rc))
|
---|
3986 | {
|
---|
3987 | RTMsgError("scmSettingsCreate: %Rrc\n", rc);
|
---|
3988 | return 1;
|
---|
3989 | }
|
---|
3990 |
|
---|
3991 | /*
|
---|
3992 | * Parse arguments and process input in order (because this is the only
|
---|
3993 | * thing that works at the moment).
|
---|
3994 | */
|
---|
3995 | static RTGETOPTDEF s_aOpts[14 + RT_ELEMENTS(g_aScmOpts)] =
|
---|
3996 | {
|
---|
3997 | { "--dry-run", 'd', RTGETOPT_REQ_NOTHING },
|
---|
3998 | { "--real-run", 'D', RTGETOPT_REQ_NOTHING },
|
---|
3999 | { "--file-filter", 'f', RTGETOPT_REQ_STRING },
|
---|
4000 | { "--quiet", 'q', RTGETOPT_REQ_NOTHING },
|
---|
4001 | { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
|
---|
4002 | { "--diff-ignore-eol", SCMOPT_DIFF_IGNORE_EOL, RTGETOPT_REQ_NOTHING },
|
---|
4003 | { "--diff-no-ignore-eol", SCMOPT_DIFF_NO_IGNORE_EOL, RTGETOPT_REQ_NOTHING },
|
---|
4004 | { "--diff-ignore-space", SCMOPT_DIFF_IGNORE_SPACE, RTGETOPT_REQ_NOTHING },
|
---|
4005 | { "--diff-no-ignore-space", SCMOPT_DIFF_NO_IGNORE_SPACE, RTGETOPT_REQ_NOTHING },
|
---|
4006 | { "--diff-ignore-leading-space", SCMOPT_DIFF_IGNORE_LEADING_SPACE, RTGETOPT_REQ_NOTHING },
|
---|
4007 | { "--diff-no-ignore-leading-space", SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE, RTGETOPT_REQ_NOTHING },
|
---|
4008 | { "--diff-ignore-trailing-space", SCMOPT_DIFF_IGNORE_TRAILING_SPACE, RTGETOPT_REQ_NOTHING },
|
---|
4009 | { "--diff-no-ignore-trailing-space", SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE, RTGETOPT_REQ_NOTHING },
|
---|
4010 | { "--diff-special-chars", SCMOPT_DIFF_SPECIAL_CHARS, RTGETOPT_REQ_NOTHING },
|
---|
4011 | { "--diff-no-special-chars", SCMOPT_DIFF_NO_SPECIAL_CHARS, RTGETOPT_REQ_NOTHING },
|
---|
4012 | };
|
---|
4013 | memcpy(&s_aOpts[RT_ELEMENTS(s_aOpts) - RT_ELEMENTS(g_aScmOpts)], &g_aScmOpts[0], sizeof(g_aScmOpts));
|
---|
4014 |
|
---|
4015 | RTGETOPTUNION ValueUnion;
|
---|
4016 | RTGETOPTSTATE GetOptState;
|
---|
4017 | rc = RTGetOptInit(&GetOptState, argc, argv, &s_aOpts[0], RT_ELEMENTS(s_aOpts), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
|
---|
4018 | AssertReleaseRCReturn(rc, 1);
|
---|
4019 | size_t cProcessed = 0;
|
---|
4020 |
|
---|
4021 | while ((rc = RTGetOpt(&GetOptState, &ValueUnion)) != 0)
|
---|
4022 | {
|
---|
4023 | switch (rc)
|
---|
4024 | {
|
---|
4025 | case 'd':
|
---|
4026 | g_fDryRun = true;
|
---|
4027 | break;
|
---|
4028 | case 'D':
|
---|
4029 | g_fDryRun = false;
|
---|
4030 | break;
|
---|
4031 |
|
---|
4032 | case 'f':
|
---|
4033 | g_pszFileFilter = ValueUnion.psz;
|
---|
4034 | break;
|
---|
4035 |
|
---|
4036 | case 'h':
|
---|
4037 | RTPrintf("VirtualBox Source Code Massager\n"
|
---|
4038 | "\n"
|
---|
4039 | "Usage: %s [options] <files & dirs>\n"
|
---|
4040 | "\n"
|
---|
4041 | "Options:\n", g_szProgName);
|
---|
4042 | for (size_t i = 0; i < RT_ELEMENTS(s_aOpts); i++)
|
---|
4043 | {
|
---|
4044 | bool fAdvanceTwo = false;
|
---|
4045 | if ((s_aOpts[i].fFlags & RTGETOPT_REQ_MASK) == RTGETOPT_REQ_NOTHING)
|
---|
4046 | {
|
---|
4047 | fAdvanceTwo = i + 1 < RT_ELEMENTS(s_aOpts)
|
---|
4048 | && ( strstr(s_aOpts[i+1].pszLong, "-no-") != NULL
|
---|
4049 | || strstr(s_aOpts[i+1].pszLong, "-not-") != NULL
|
---|
4050 | || strstr(s_aOpts[i+1].pszLong, "-dont-") != NULL
|
---|
4051 | );
|
---|
4052 | if (fAdvanceTwo)
|
---|
4053 | RTPrintf(" %s, %s\n", s_aOpts[i].pszLong, s_aOpts[i + 1].pszLong);
|
---|
4054 | else
|
---|
4055 | RTPrintf(" %s\n", s_aOpts[i].pszLong);
|
---|
4056 | }
|
---|
4057 | else if ((s_aOpts[i].fFlags & RTGETOPT_REQ_MASK) == RTGETOPT_REQ_STRING)
|
---|
4058 | RTPrintf(" %s string\n", s_aOpts[i].pszLong);
|
---|
4059 | else
|
---|
4060 | RTPrintf(" %s value\n", s_aOpts[i].pszLong);
|
---|
4061 | switch (s_aOpts[i].iShort)
|
---|
4062 | {
|
---|
4063 | case SCMOPT_CONVERT_EOL: RTPrintf(" Default: %RTbool\n", g_Defaults.fConvertEol); break;
|
---|
4064 | case SCMOPT_CONVERT_TABS: RTPrintf(" Default: %RTbool\n", g_Defaults.fConvertTabs); break;
|
---|
4065 | case SCMOPT_FORCE_FINAL_EOL: RTPrintf(" Default: %RTbool\n", g_Defaults.fForceFinalEol); break;
|
---|
4066 | case SCMOPT_FORCE_TRAILING_LINE: RTPrintf(" Default: %RTbool\n", g_Defaults.fForceTrailingLine); break;
|
---|
4067 | case SCMOPT_STRIP_TRAILING_BLANKS: RTPrintf(" Default: %RTbool\n", g_Defaults.fStripTrailingBlanks); break;
|
---|
4068 | case SCMOPT_STRIP_TRAILING_LINES: RTPrintf(" Default: %RTbool\n", g_Defaults.fStripTrailingLines); break;
|
---|
4069 | case SCMOPT_ONLY_SVN_DIRS: RTPrintf(" Default: %RTbool\n", g_Defaults.fOnlySvnDirs); break;
|
---|
4070 | case SCMOPT_ONLY_SVN_FILES: RTPrintf(" Default: %RTbool\n", g_Defaults.fOnlySvnFiles); break;
|
---|
4071 | case SCMOPT_SET_SVN_EOL: RTPrintf(" Default: %RTbool\n", g_Defaults.fSetSvnEol); break;
|
---|
4072 | case SCMOPT_SET_SVN_EXECUTABLE: RTPrintf(" Default: %RTbool\n", g_Defaults.fSetSvnExecutable); break;
|
---|
4073 | case SCMOPT_SET_SVN_KEYWORDS: RTPrintf(" Default: %RTbool\n", g_Defaults.fSetSvnKeywords); break;
|
---|
4074 | case SCMOPT_TAB_SIZE: RTPrintf(" Default: %u\n", g_Defaults.cchTab); break;
|
---|
4075 | case SCMOPT_FILTER_OUT_DIRS: RTPrintf(" Default: %s\n", g_Defaults.pszFilterOutDirs); break;
|
---|
4076 | case SCMOPT_FILTER_FILES: RTPrintf(" Default: %s\n", g_Defaults.pszFilterFiles); break;
|
---|
4077 | case SCMOPT_FILTER_OUT_FILES: RTPrintf(" Default: %s\n", g_Defaults.pszFilterOutFiles); break;
|
---|
4078 | }
|
---|
4079 | i += fAdvanceTwo;
|
---|
4080 | }
|
---|
4081 | return 1;
|
---|
4082 |
|
---|
4083 | case 'q':
|
---|
4084 | g_iVerbosity = 0;
|
---|
4085 | break;
|
---|
4086 |
|
---|
4087 | case 'v':
|
---|
4088 | g_iVerbosity++;
|
---|
4089 | break;
|
---|
4090 |
|
---|
4091 | case 'V':
|
---|
4092 | {
|
---|
4093 | /* The following is assuming that svn does it's job here. */
|
---|
4094 | static const char s_szRev[] = "$Revision: 28767 $";
|
---|
4095 | const char *psz = RTStrStripL(strchr(s_szRev, ' '));
|
---|
4096 | RTPrintf("r%.*s\n", strchr(psz, ' ') - psz, psz);
|
---|
4097 | return 0;
|
---|
4098 | }
|
---|
4099 |
|
---|
4100 | case SCMOPT_DIFF_IGNORE_EOL:
|
---|
4101 | g_fDiffIgnoreEol = true;
|
---|
4102 | break;
|
---|
4103 | case SCMOPT_DIFF_NO_IGNORE_EOL:
|
---|
4104 | g_fDiffIgnoreEol = false;
|
---|
4105 | break;
|
---|
4106 |
|
---|
4107 | case SCMOPT_DIFF_IGNORE_SPACE:
|
---|
4108 | g_fDiffIgnoreTrailingWS = g_fDiffIgnoreLeadingWS = true;
|
---|
4109 | break;
|
---|
4110 | case SCMOPT_DIFF_NO_IGNORE_SPACE:
|
---|
4111 | g_fDiffIgnoreTrailingWS = g_fDiffIgnoreLeadingWS = false;
|
---|
4112 | break;
|
---|
4113 |
|
---|
4114 | case SCMOPT_DIFF_IGNORE_LEADING_SPACE:
|
---|
4115 | g_fDiffIgnoreLeadingWS = true;
|
---|
4116 | break;
|
---|
4117 | case SCMOPT_DIFF_NO_IGNORE_LEADING_SPACE:
|
---|
4118 | g_fDiffIgnoreLeadingWS = false;
|
---|
4119 | break;
|
---|
4120 |
|
---|
4121 | case SCMOPT_DIFF_IGNORE_TRAILING_SPACE:
|
---|
4122 | g_fDiffIgnoreTrailingWS = true;
|
---|
4123 | break;
|
---|
4124 | case SCMOPT_DIFF_NO_IGNORE_TRAILING_SPACE:
|
---|
4125 | g_fDiffIgnoreTrailingWS = false;
|
---|
4126 | break;
|
---|
4127 |
|
---|
4128 | case SCMOPT_DIFF_SPECIAL_CHARS:
|
---|
4129 | g_fDiffSpecialChars = true;
|
---|
4130 | break;
|
---|
4131 | case SCMOPT_DIFF_NO_SPECIAL_CHARS:
|
---|
4132 | g_fDiffSpecialChars = false;
|
---|
4133 | break;
|
---|
4134 |
|
---|
4135 | case VINF_GETOPT_NOT_OPTION:
|
---|
4136 | {
|
---|
4137 | if (!g_fDryRun)
|
---|
4138 | {
|
---|
4139 | if (!cProcessed)
|
---|
4140 | {
|
---|
4141 | RTPrintf("%s: Warning! This program will make changes to your source files and\n"
|
---|
4142 | "%s: there is a slight risk that bugs or a full disk may cause\n"
|
---|
4143 | "%s: LOSS OF DATA. So, please make sure you have checked in\n"
|
---|
4144 | "%s: all your changes already. If you didn't, then don't blame\n"
|
---|
4145 | "%s: anyone for not warning you!\n"
|
---|
4146 | "%s:\n"
|
---|
4147 | "%s: Press any key to continue...\n",
|
---|
4148 | g_szProgName, g_szProgName, g_szProgName, g_szProgName, g_szProgName,
|
---|
4149 | g_szProgName, g_szProgName);
|
---|
4150 | RTStrmGetCh(g_pStdIn);
|
---|
4151 | }
|
---|
4152 | cProcessed++;
|
---|
4153 | }
|
---|
4154 | rc = scmProcessSomething(ValueUnion.psz, pSettings);
|
---|
4155 | if (RT_FAILURE(rc))
|
---|
4156 | return rc;
|
---|
4157 | break;
|
---|
4158 | }
|
---|
4159 |
|
---|
4160 | default:
|
---|
4161 | {
|
---|
4162 | int rc2 = scmSettingsBaseHandleOpt(&pSettings->Base, rc, &ValueUnion);
|
---|
4163 | if (RT_SUCCESS(rc2))
|
---|
4164 | break;
|
---|
4165 | if (rc2 != VERR_GETOPT_UNKNOWN_OPTION)
|
---|
4166 | return 2;
|
---|
4167 | return RTGetOptPrintError(rc, &ValueUnion);
|
---|
4168 | }
|
---|
4169 | }
|
---|
4170 | }
|
---|
4171 |
|
---|
4172 | scmSettingsDestroy(pSettings);
|
---|
4173 | return 0;
|
---|
4174 | }
|
---|
4175 |
|
---|