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