Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

324 lines
8.0 KiB

  1. #include "priv.h"
  2. HINSTANCE g_hinst;
  3. #define APP_VERSION "Version 0.4"
  4. // Don't link to shlwapi.dll so this is a stand-alone tool
  5. /*----------------------------------------------------------
  6. Purpose: If a path is contained in quotes then remove them.
  7. */
  8. void
  9. PathUnquoteSpaces(
  10. LPTSTR lpsz)
  11. {
  12. int cch;
  13. cch = lstrlen(lpsz);
  14. // Are the first and last chars quotes?
  15. if (lpsz[0] == TEXT('"') && lpsz[cch-1] == TEXT('"'))
  16. {
  17. // Yep, remove them.
  18. lpsz[cch-1] = TEXT('\0');
  19. hmemcpy(lpsz, lpsz+1, (cch-1) * SIZEOF(TCHAR));
  20. }
  21. }
  22. #define CH_WHACK TEXT(FILENAME_SEPARATOR)
  23. LPTSTR
  24. PathFindExtension(
  25. LPCTSTR pszPath)
  26. {
  27. LPCTSTR pszDot = NULL;
  28. if (pszPath)
  29. {
  30. for (; *pszPath; pszPath = CharNext(pszPath))
  31. {
  32. switch (*pszPath) {
  33. case TEXT('.'):
  34. pszDot = pszPath; // remember the last dot
  35. break;
  36. case CH_WHACK:
  37. case TEXT(' '): // extensions can't have spaces
  38. pszDot = NULL; // forget last dot, it was in a directory
  39. break;
  40. }
  41. }
  42. }
  43. // if we found the extension, return ptr to the dot, else
  44. // ptr to end of the string (NULL extension) (cast->non const)
  45. return pszDot ? (LPTSTR)pszDot : (LPTSTR)pszPath;
  46. }
  47. __inline BOOL ChrCmpA_inline(WORD w1, WORD wMatch)
  48. {
  49. /* Most of the time this won't match, so test it first for speed.
  50. */
  51. if (LOBYTE(w1) == LOBYTE(wMatch))
  52. {
  53. if (IsDBCSLeadByte(LOBYTE(w1)))
  54. {
  55. return(w1 != wMatch);
  56. }
  57. return FALSE;
  58. }
  59. return TRUE;
  60. }
  61. LPSTR FAR PASCAL StrChrA(LPCSTR lpStart, WORD wMatch)
  62. {
  63. for ( ; *lpStart; lpStart = AnsiNext(lpStart))
  64. {
  65. if (!ChrCmpA_inline(*(UNALIGNED WORD FAR *)lpStart, wMatch))
  66. return((LPSTR)lpStart);
  67. }
  68. return (NULL);
  69. }
  70. BOOL
  71. StrTrim(
  72. IN OUT LPSTR pszTrimMe,
  73. IN LPCSTR pszTrimChars)
  74. {
  75. BOOL bRet = FALSE;
  76. LPSTR psz;
  77. LPSTR pszStartMeat;
  78. LPSTR pszMark = NULL;
  79. ASSERT(IS_VALID_STRING_PTRA(pszTrimMe, -1));
  80. ASSERT(IS_VALID_STRING_PTRA(pszTrimChars, -1));
  81. if (pszTrimMe)
  82. {
  83. /* Trim leading characters. */
  84. psz = pszTrimMe;
  85. while (*psz && StrChrA(pszTrimChars, *psz))
  86. psz = CharNextA(psz);
  87. pszStartMeat = psz;
  88. /* Trim trailing characters. */
  89. // (The old algorithm used to start from the end and go
  90. // backwards, but that is piggy because DBCS version of
  91. // CharPrev iterates from the beginning of the string
  92. // on every call.)
  93. while (*psz)
  94. {
  95. if (StrChrA(pszTrimChars, *psz))
  96. {
  97. pszMark = psz;
  98. }
  99. else
  100. {
  101. pszMark = NULL;
  102. }
  103. psz = CharNextA(psz);
  104. }
  105. // Any trailing characters to clip?
  106. if (pszMark)
  107. {
  108. // Yes
  109. *pszMark = '\0';
  110. bRet = TRUE;
  111. }
  112. /* Relocate stripped string. */
  113. if (pszStartMeat > pszTrimMe)
  114. {
  115. /* (+ 1) for null terminator. */
  116. MoveMemory(pszTrimMe, pszStartMeat, CbFromCchA(lstrlenA(pszStartMeat) + 1));
  117. bRet = TRUE;
  118. }
  119. else
  120. ASSERT(pszStartMeat == pszTrimMe);
  121. ASSERT(IS_VALID_STRING_PTRA(pszTrimMe, -1));
  122. }
  123. return bRet;
  124. }
  125. void PrintSyntax(void)
  126. {
  127. fprintf(stderr, "cleaninf.exe " APP_VERSION "\n\n"
  128. "Cleans up an inf, html, or script file for public distribution or for packing\n"
  129. "into a resource. Without any options, this removes all comments. This\n"
  130. "tool recognizes .inf, .htm, .hta, .js and .htc files by default.\n\n"
  131. "Syntax: cleaninf [-w] [-inf | -htm | -js | -htc] sourceFile destFile\n\n"
  132. " -w Strip whitespace\n\n"
  133. " These flags are mutually exclusive, and will treat the file\n"
  134. " accordingly, regardless of extension:\n"
  135. " -inf Treat file as a .inf file\n"
  136. " -htm Treat file as a .htm file\n"
  137. " -js Treat file as a .js file\n"
  138. " -htc Treat file as a .htc file\n");
  139. }
  140. /*----------------------------------------------------------
  141. Purpose: Worker function to do the work
  142. */
  143. int
  144. DoWork(int cArgs, char * rgszArgs[])
  145. {
  146. LPSTR psz;
  147. LPSTR pszSrc = NULL;
  148. LPSTR pszDest = NULL;
  149. DWORD dwFlags = 0;
  150. int i;
  151. int nRet = 0;
  152. // (The first arg is actually the exe. Skip that.)
  153. for (i = 1; i < cArgs; i++)
  154. {
  155. psz = rgszArgs[i];
  156. // Check for options
  157. if ('/' == *psz || '-' == *psz)
  158. {
  159. psz++;
  160. switch (*psz)
  161. {
  162. case '?':
  163. // Help
  164. PrintSyntax();
  165. return 0;
  166. case 'w':
  167. dwFlags |= PFF_WHITESPACE;
  168. break;
  169. default:
  170. if (0 == strncmp(psz, "inf", 3))
  171. {
  172. dwFlags |= PFF_INF;
  173. }
  174. else if (0 == strncmp(psz, "htm", 3))
  175. {
  176. dwFlags |= PFF_HTML;
  177. }
  178. else if (0 == strncmp(psz, "js", 2))
  179. {
  180. dwFlags |= PFF_JS;
  181. }
  182. else if (0 == strncmp(psz, "htc", 3))
  183. {
  184. dwFlags |= PFF_HTC;
  185. }
  186. else
  187. {
  188. // unknown
  189. fprintf(stderr, "Invalid option -%c\n", *psz);
  190. return -1;
  191. }
  192. break;
  193. }
  194. }
  195. else if (!pszSrc)
  196. pszSrc = rgszArgs[i];
  197. else if (!pszDest)
  198. pszDest = rgszArgs[i];
  199. else
  200. {
  201. fprintf(stderr, "Ignoring invalid parameter - %s\n", rgszArgs[i]);
  202. }
  203. }
  204. if (!pszSrc || !pszDest)
  205. {
  206. PrintSyntax();
  207. return -2;
  208. }
  209. // Has the file type already been explicitly specified?
  210. if ( !(dwFlags & (PFF_INF | PFF_HTML | PFF_JS | PFF_HTC)) )
  211. {
  212. // No; determine it based on the extension
  213. LPTSTR pszExt = PathFindExtension(pszSrc);
  214. if (pszExt)
  215. {
  216. if (0 == lstrcmpi(pszExt, ".htm") || 0 == lstrcmpi(pszExt, ".hta"))
  217. dwFlags |= PFF_HTML;
  218. else if (0 == lstrcmpi(pszExt, ".js"))
  219. dwFlags |= PFF_JS;
  220. else if (0 == lstrcmpi(pszExt, ".htc"))
  221. dwFlags |= PFF_HTC;
  222. }
  223. }
  224. // Open the files
  225. PathUnquoteSpaces(pszSrc);
  226. PathUnquoteSpaces(pszDest);
  227. FILE * pfileSrc = fopen(pszSrc, "r");
  228. if (NULL == pfileSrc)
  229. {
  230. fprintf(stderr, "\"%s\" could not be opened", pszSrc);
  231. nRet = -3;
  232. }
  233. else
  234. {
  235. FILE * pfileDest = fopen(pszDest, "w");
  236. if (NULL == pfileDest)
  237. {
  238. fprintf(stderr, "\"%s\" could not be created", pszDest);
  239. nRet = -4;
  240. }
  241. else
  242. {
  243. CParseFile parsefile;
  244. parsefile.Parse(pfileSrc, pfileDest, dwFlags);
  245. fclose(pfileDest);
  246. }
  247. fclose(pfileSrc);
  248. }
  249. return nRet;
  250. }
  251. #ifdef UNIX
  252. EXTERN_C
  253. HINSTANCE MwMainwinInitLite(int argc, char *argv[], void* lParam);
  254. EXTERN_C
  255. HINSTANCE mainwin_init(int argc, char *argv[])
  256. {
  257. return MwMainwinInitLite(argc, argv, NULL);
  258. }
  259. #endif
  260. int __cdecl main(int argc, char * argv[])
  261. {
  262. return DoWork(argc, argv);
  263. }