Changeset 26488 in vbox
- Timestamp:
- Feb 14, 2010 7:08:38 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bldprogs/scm.cpp
r26483 r26488 125 125 typedef SCMSTREAM const *PCSCMSTREAM; 126 126 127 128 /** 129 * Rewriter state. 130 */ 131 typedef struct SCMRWSTATE 132 { 133 /** The filename. */ 134 const char *pszFilename; 135 /** Set after the printing the first verbose message about a file under 136 * rewrite. */ 137 bool fFirst; 138 } SCMRWSTATE; 139 /** Pointer to the rewriter state. */ 140 typedef SCMRWSTATE *PSCMRWSTATE; 141 127 142 /** 128 143 * A rewriter. … … 136 151 * @param pSettings The settings. 137 152 */ 138 typedef bool (*PFNSCMREWRITER)(PSCM STREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);153 typedef bool (*PFNSCMREWRITER)(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings); 139 154 140 155 … … 298 313 299 314 300 301 315 /******************************************************************************* 302 316 * Internal Functions * 303 317 *******************************************************************************/ 304 static bool rewrite_StripTrailingBlanks(PSCM STREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);305 static bool rewrite_ExpandTabs(PSCM STREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);306 static bool rewrite_ForceNativeEol(PSCM STREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);307 static bool rewrite_ForceLF(PSCM STREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);308 static bool rewrite_ForceCRLF(PSCM STREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);309 static bool rewrite_AdjustTrailingLines(PSCM STREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);310 static bool rewrite_Makefile_kup(PSCM STREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);311 static bool rewrite_Makefile_kmk(PSCM STREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);312 static bool rewrite_C_and_CPP(PSCM STREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings);318 static bool rewrite_StripTrailingBlanks(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings); 319 static bool rewrite_ExpandTabs(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings); 320 static bool rewrite_ForceNativeEol(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings); 321 static bool rewrite_ForceLF(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings); 322 static bool rewrite_ForceCRLF(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings); 323 static bool rewrite_AdjustTrailingLines(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings); 324 static bool rewrite_Makefile_kup(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings); 325 static bool rewrite_Makefile_kmk(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings); 326 static bool rewrite_C_and_CPP(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings); 313 327 314 328 … … 329 343 static SCMSETTINGSBASE const g_Defaults = 330 344 { 345 /* .fConvertEol = */ true, 346 /* .fConvertTabs = */ true, 347 /* .fForceFinalEol = */ true, 348 /* .fForceTrailingLine = */ false, 331 349 /* .fStripTrailingBlanks = */ true, 332 350 /* .fStripTrailingLines = */ true, 333 /* .fForceFinalEol = */ true,334 /* .fForceTrailingLine = */ false,335 /* .fConvertTabs = */ true,336 351 /* .cchTab = */ 8, 337 /* .fConvertEol = */ true,338 352 /* .pszFilterFiles = */ (char *)"", 339 353 /* .pszFilterOutFiles = */ (char *)"*.exe|*.com|20*-*-*.log", … … 2177 2191 */ 2178 2192 size_t cComponents = RTPathCountComponents(pszPath); 2179 for (size_t i = 1; i < cComponents; i++)2193 for (size_t i = 1; i <= cComponents; i++) 2180 2194 { 2181 2195 rc = RTPathCopyComponents(szFile, sizeof(szFile), pszPath, i); … … 2343 2357 * Prints a verbose message if the level is high enough. 2344 2358 * 2359 * @param pState The rewrite state. Optional. 2345 2360 * @param iLevel The required verbosity level. 2346 * @param pszFormat The message format string. 2361 * @param pszFormat The message format string. Can be NULL if we 2362 * only want to trigger the per file message. 2347 2363 * @param ... Format arguments. 2348 2364 */ 2349 static void ScmVerbose( int iLevel, const char *pszFormat, ...)2365 static void ScmVerbose(PSCMRWSTATE pState, int iLevel, const char *pszFormat, ...) 2350 2366 { 2351 2367 if (iLevel <= g_iVerbosity) 2352 2368 { 2353 RTPrintf("%s: info: ", g_szProgName); 2354 va_list va; 2355 va_start(va, pszFormat); 2356 RTPrintfV(pszFormat, va); 2357 va_end(va); 2369 if (pState && !pState->fFirst) 2370 { 2371 RTPrintf("%s: info: Rewriting '%s'...\n", g_szProgName, pState->pszFilename); 2372 pState->fFirst = true; 2373 } 2374 if (pszFormat) 2375 { 2376 RTPrintf("%s: info: ", g_szProgName); 2377 va_list va; 2378 va_start(va, pszFormat); 2379 RTPrintfV(pszFormat, va); 2380 va_end(va); 2381 } 2358 2382 } 2359 2383 } … … 2371 2395 * @param pSettings The settings. 2372 2396 */ 2373 static bool rewrite_StripTrailingBlanks(PSCM STREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)2397 static bool rewrite_StripTrailingBlanks(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings) 2374 2398 { 2375 2399 if (!pSettings->fStripTrailingBlanks) … … 2398 2422 } 2399 2423 if (fModified) 2400 ScmVerbose( 2, " * Stripped trailing blanks\n");2424 ScmVerbose(pState, 2, " * Stripped trailing blanks\n"); 2401 2425 return fModified; 2402 2426 } … … 2410 2434 * @param pSettings The settings. 2411 2435 */ 2412 static bool rewrite_ExpandTabs(PSCM STREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)2436 static bool rewrite_ExpandTabs(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings) 2413 2437 { 2414 2438 if (!pSettings->fConvertTabs) … … 2456 2480 } 2457 2481 if (fModified) 2458 ScmVerbose( 2, " * Expanded tabs\n");2482 ScmVerbose(pState, 2, " * Expanded tabs\n"); 2459 2483 return fModified; 2460 2484 } … … 2469 2493 * @param enmDesiredEol The desired end of line indicator type. 2470 2494 */ 2471 static bool rewrite_ForceEol(PSCM STREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings, SCMEOL enmDesiredEol)2495 static bool rewrite_ForceEol(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings, SCMEOL enmDesiredEol) 2472 2496 { 2473 2497 if (!pSettings->fConvertEol) … … 2491 2515 } 2492 2516 if (fModified) 2493 ScmVerbose( 2, " * Converted EOL markers\n");2517 ScmVerbose(pState, 2, " * Converted EOL markers\n"); 2494 2518 return fModified; 2495 2519 } … … 2503 2527 * @param pSettings The settings. 2504 2528 */ 2505 static bool rewrite_ForceNativeEol(PSCM STREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)2529 static bool rewrite_ForceNativeEol(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings) 2506 2530 { 2507 2531 #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2) 2508 return rewrite_ForceEol(p In, pOut, pSettings, SCMEOL_CRLF);2532 return rewrite_ForceEol(pState, pIn, pOut, pSettings, SCMEOL_CRLF); 2509 2533 #else 2510 return rewrite_ForceEol(p In, pOut, pSettings, SCMEOL_LF);2534 return rewrite_ForceEol(pState, pIn, pOut, pSettings, SCMEOL_LF); 2511 2535 #endif 2512 2536 } … … 2520 2544 * @param pSettings The settings. 2521 2545 */ 2522 static bool rewrite_ForceLF(PSCM STREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)2523 { 2524 return rewrite_ForceEol(p In, pOut, pSettings, SCMEOL_LF);2546 static bool rewrite_ForceLF(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings) 2547 { 2548 return rewrite_ForceEol(pState, pIn, pOut, pSettings, SCMEOL_LF); 2525 2549 } 2526 2550 … … 2533 2557 * @param pSettings The settings. 2534 2558 */ 2535 static bool rewrite_ForceCRLF(PSCM STREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)2536 { 2537 return rewrite_ForceEol(p In, pOut, pSettings, SCMEOL_CRLF);2559 static bool rewrite_ForceCRLF(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings) 2560 { 2561 return rewrite_ForceEol(pState, pIn, pOut, pSettings, SCMEOL_CRLF); 2538 2562 } 2539 2563 … … 2549 2573 * @remarks ASSUMES trailing white space has been removed already. 2550 2574 */ 2551 static bool rewrite_AdjustTrailingLines(PSCM STREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)2575 static bool rewrite_AdjustTrailingLines(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings) 2552 2576 { 2553 2577 if ( !pSettings->fStripTrailingLines … … 2604 2628 } 2605 2629 2606 ScmVerbose( 2, " * Adjusted trailing blank lines\n");2630 ScmVerbose(pState, 2, " * Adjusted trailing blank lines\n"); 2607 2631 return true; 2608 2632 } … … 2616 2640 * @param pSettings The settings. 2617 2641 */ 2618 static bool rewrite_Makefile_kup(PSCM STREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)2642 static bool rewrite_Makefile_kup(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings) 2619 2643 { 2620 2644 /* These files should be zero bytes. */ 2621 2645 if (pIn->cb == 0) 2622 2646 return false; 2623 ScmVerbose( 2, " * Truncated file to zero bytes\n");2647 ScmVerbose(pState, 2, " * Truncated file to zero bytes\n"); 2624 2648 return true; 2625 2649 } … … 2639 2663 * - line continuation slashes should only be preceeded by one space. 2640 2664 */ 2641 static bool rewrite_Makefile_kmk(PSCM STREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)2665 static bool rewrite_Makefile_kmk(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings) 2642 2666 { 2643 2667 return false; … … 2674 2698 * - space between functions. 2675 2699 */ 2676 static bool rewrite_C_and_CPP(PSCM STREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings)2700 static bool rewrite_C_and_CPP(PSCMRWSTATE pState, PSCMSTREAM pIn, PSCMSTREAM pOut, PCSCMSETTINGSBASE pSettings) 2677 2701 { 2678 2702 … … 2697 2721 { 2698 2722 /* 2723 * Init the rewriter state data. 2724 */ 2725 SCMRWSTATE State; 2726 State.fFirst = false; 2727 State.pszFilename = pszFilename; 2728 2729 /* 2699 2730 * Do the file level filtering. 2700 2731 */ … … 2703 2734 && !RTStrSimplePatternMultiMatch(pBaseSettings->pszFilterFiles, RTSTR_MAX, pszBasename, cchBasename, NULL)) 2704 2735 { 2705 ScmVerbose( 4, "file filter mismatch: \"%s\"\n", pszFilename);2736 ScmVerbose(NULL, 5, "file filter mismatch: \"%s\"\n", pszFilename); 2706 2737 return VINF_SUCCESS; 2707 2738 } … … 2711 2742 || RTStrSimplePatternMultiMatch(pBaseSettings->pszFilterOutFiles, RTSTR_MAX, pszFilename, RTSTR_MAX, NULL)) ) 2712 2743 { 2713 ScmVerbose( 4, "file filter out: \"%s\"\n", pszFilename);2744 ScmVerbose(NULL, 5, "file filter out: \"%s\"\n", pszFilename); 2714 2745 return VINF_SUCCESS; 2715 2746 } … … 2727 2758 if (!pCfg) 2728 2759 { 2729 ScmVerbose( 3, "No rewriters configured for \"%s\"\n", pszFilename);2760 ScmVerbose(NULL, 4, "No rewriters configured for \"%s\"\n", pszFilename); 2730 2761 return VINF_SUCCESS; 2731 2762 } 2732 ScmVerbose( 3, "\"%s\" matched \"%s\"\n", pszFilename, pCfg->pszFilePattern);2763 ScmVerbose(&State, 4, "matched \"%s\"\n", pCfg->pszFilePattern); 2733 2764 2734 2765 /* … … 2744 2775 if (ScmStreamIsText(&Stream1)) 2745 2776 { 2746 ScmVerbose( 1, "Processing '%s'...\n", pszFilename);2777 ScmVerbose(&State, 3, NULL); 2747 2778 2748 2779 /* … … 2772 2803 for (size_t iRw = 0; iRw < pCfg->cRewriters; iRw++) 2773 2804 { 2774 bool fRc = pCfg->papfnRewriter[iRw]( pIn, pOut, pBaseSettings);2805 bool fRc = pCfg->papfnRewriter[iRw](&State, pIn, pOut, pBaseSettings); 2775 2806 if (fRc) 2776 2807 { … … 2791 2822 if (!g_fDryRun) 2792 2823 { 2793 ScmVerbose( 1, "Writing modified file to \"%s%s\"\n", pszFilename, g_pszChangedSuff);2824 ScmVerbose(&State, 1, "Writing modified file to \"%s%s\"\n", pszFilename, g_pszChangedSuff); 2794 2825 rc = ScmStreamWriteToFile(pIn, "%s%s", pszFilename, g_pszChangedSuff); 2795 2826 if (RT_FAILURE(rc)) … … 2798 2829 else 2799 2830 { 2800 ScmVerbose( 1, "Would have modified file \"%s\"\n", pszFilename);2831 ScmVerbose(&State, 1, NULL); 2801 2832 ScmDiffStreams(pszFilename, &Stream1, pIn, g_fDiffIgnoreEol, g_fDiffIgnoreLeadingWS, 2802 2833 g_fDiffIgnoreTrailingWS, g_fDiffSpecialChars, pBaseSettings->cchTab, g_pStdOut); 2834 ScmVerbose(&State, 3, "Would have modified the file \"%s%s\"\n", pszFilename, g_pszChangedSuff); 2803 2835 } 2804 2836 } 2805 2837 else 2806 ScmVerbose( 2, "Unchanged \"%s\"\n", pszFilename);2838 ScmVerbose(&State, 3, "No change\n", pszFilename); 2807 2839 2808 2840 ScmStreamDelete(&Stream3); … … 2819 2851 } 2820 2852 else 2821 ScmVerbose( 3, "not text file: \"%s\"\n", pszFilename);2853 ScmVerbose(&State, 4, "not text file: \"%s\"\n", pszFilename); 2822 2854 ScmStreamDelete(&Stream1); 2823 2855 … … 3072 3104 * thing that works at the moment). 3073 3105 */ 3074 static RTGETOPTDEF s_aOpts[1 6+ RT_ELEMENTS(g_aScmOpts)] =3106 static RTGETOPTDEF s_aOpts[14 + RT_ELEMENTS(g_aScmOpts)] = 3075 3107 { 3076 3108 { "--dry-run", 'd', RTGETOPT_REQ_NOTHING }, 3077 3109 { "--real-run", 'D', RTGETOPT_REQ_NOTHING }, 3078 3110 { "--file-filter", 'f', RTGETOPT_REQ_STRING }, 3079 { "--help", 'h', RTGETOPT_REQ_NOTHING },3080 3111 { "--quiet", 'q', RTGETOPT_REQ_NOTHING }, 3081 3112 { "--verbose", 'v', RTGETOPT_REQ_NOTHING }, 3082 { "--version", 'V', RTGETOPT_REQ_NOTHING },3083 3113 { "--diff-ignore-eol", SCMOPT_DIFF_IGNORE_EOL, RTGETOPT_REQ_NOTHING }, 3084 3114 { "--diff-no-ignore-eol", SCMOPT_DIFF_NO_IGNORE_EOL, RTGETOPT_REQ_NOTHING }, … … 3096 3126 RTGETOPTUNION ValueUnion; 3097 3127 RTGETOPTSTATE GetOptState; 3098 rc = RTGetOptInit(&GetOptState, argc, argv, &s_aOpts[0], RT_ELEMENTS(s_aOpts), 1, 0 /*fFlags*/);3128 rc = RTGetOptInit(&GetOptState, argc, argv, &s_aOpts[0], RT_ELEMENTS(s_aOpts), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST); 3099 3129 AssertReleaseRCReturn(rc, 1); 3100 3130 size_t cProcessed = 0; … … 3123 3153 "Options:\n", g_szProgName); 3124 3154 for (size_t i = 0; i < RT_ELEMENTS(s_aOpts); i++) 3155 { 3156 bool fAdvanceTwo = false; 3125 3157 if ((s_aOpts[i].fFlags & RTGETOPT_REQ_MASK) == RTGETOPT_REQ_NOTHING) 3126 RTPrintf(" %s\n", s_aOpts[i].pszLong); 3158 { 3159 if ((fAdvanceTwo = i + 1 < RT_ELEMENTS(s_aOpts) && strstr(s_aOpts[i+1].pszLong, "-no-") != NULL)) 3160 RTPrintf(" %s, %s\n", s_aOpts[i].pszLong, s_aOpts[i + 1].pszLong); 3161 else 3162 RTPrintf(" %s\n", s_aOpts[i].pszLong); 3163 } 3127 3164 else if ((s_aOpts[i].fFlags & RTGETOPT_REQ_MASK) == RTGETOPT_REQ_STRING) 3128 3165 RTPrintf(" %s string\n", s_aOpts[i].pszLong); 3129 3166 else 3130 3167 RTPrintf(" %s value\n", s_aOpts[i].pszLong); 3168 switch (s_aOpts[i].iShort) 3169 { 3170 case SCMOPT_CONVERT_EOL: RTPrintf(" Default: %RTbool\n", g_Defaults.fConvertEol); break; 3171 case SCMOPT_CONVERT_TABS: RTPrintf(" Default: %RTbool\n", g_Defaults.fConvertTabs); break; 3172 case SCMOPT_FORCE_FINAL_EOL: RTPrintf(" Default: %RTbool\n", g_Defaults.fForceFinalEol); break; 3173 case SCMOPT_FORCE_TRAILING_LINE: RTPrintf(" Default: %RTbool\n", g_Defaults.fForceTrailingLine); break; 3174 case SCMOPT_STRIP_TRAILING_BLANKS: RTPrintf(" Default: %RTbool\n", g_Defaults.fStripTrailingBlanks); break; 3175 case SCMOPT_STRIP_TRAILING_LINES: RTPrintf(" Default: %RTbool\n", g_Defaults.fStripTrailingLines); break; 3176 case SCMOPT_TAB_SIZE: RTPrintf(" Default: %u\n", g_Defaults.cchTab); break; 3177 case SCMOPT_FILTER_OUT_DIRS: RTPrintf(" Default: %s\n", g_Defaults.pszFilterOutDirs); break; 3178 case SCMOPT_FILTER_FILES: RTPrintf(" Default: %s\n", g_Defaults.pszFilterFiles); break; 3179 case SCMOPT_FILTER_OUT_FILES: RTPrintf(" Default: %s\n", g_Defaults.pszFilterOutFiles); break; 3180 } 3181 i += fAdvanceTwo; 3182 } 3131 3183 return 1; 3132 3184
Note:
See TracChangeset
for help on using the changeset viewer.