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.

416 lines
11 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. regutil.c
  5. Abstract:
  6. Utility routines for use by REGINI and REGDMP programs.
  7. Author:
  8. Steve Wood (stevewo) 10-Mar-92
  9. Revision History:
  10. --*/
  11. #include "regutil.h"
  12. ULONG NumberOfLinesOutput;
  13. BOOLEAN RegBackSwitches;
  14. LPSTR SavedModuleName;
  15. LPSTR SavedModuleUsage1;
  16. LPSTR SavedModuleUsage2;
  17. PHANDLER_ROUTINE SavedCtrlCHandler;
  18. BOOL
  19. CommonCtrlCHandler(
  20. IN ULONG CtrlType
  21. )
  22. {
  23. if (CtrlType == CTRL_C_EVENT || CtrlType == CTRL_BREAK_EVENT) {
  24. if (SavedCtrlCHandler != NULL) {
  25. (*SavedCtrlCHandler)( CtrlType );
  26. }
  27. ExitProcess( 1 );
  28. }
  29. return FALSE;
  30. }
  31. void
  32. InitCommonCode(
  33. PHANDLER_ROUTINE CtrlCHandler,
  34. LPSTR ModuleName,
  35. LPSTR ModuleUsage1,
  36. LPSTR ModuleUsage2
  37. )
  38. {
  39. CONSOLE_SCREEN_BUFFER_INFO ConsoleScreenBufferInfo;
  40. if (!RTInitialize()) {
  41. FatalError( "Unable to initialize registry access functions (%u)",
  42. GetLastError(), 0 );
  43. }
  44. if (_isatty( _fileno( stdout ) )) {
  45. if (GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ),
  46. &ConsoleScreenBufferInfo
  47. )
  48. ) {
  49. OutputHeight = ConsoleScreenBufferInfo.srWindow.Bottom -
  50. ConsoleScreenBufferInfo.srWindow.Top + 1;
  51. OutputWidth = ConsoleScreenBufferInfo.srWindow.Right -
  52. ConsoleScreenBufferInfo.srWindow.Left + 1;
  53. }
  54. else {
  55. OutputHeight = 24;
  56. OutputWidth = 80;
  57. }
  58. }
  59. else {
  60. OutputHeight = 0; // Dont pause on redirected output
  61. OutputWidth = 240; // Larger width for redirected output
  62. }
  63. IndentMultiple = 4;
  64. SavedModuleName = ModuleName;
  65. if (!_stricmp( SavedModuleName, "REGBACK" )) {
  66. RegBackSwitches = TRUE;
  67. }
  68. else {
  69. RegBackSwitches = FALSE;
  70. }
  71. SavedModuleUsage1 = ModuleUsage1;
  72. SavedModuleUsage2 = ModuleUsage2;
  73. SetConsoleCtrlHandler( CtrlCHandler, TRUE );
  74. OldValueBufferSize = 30 * 4096;
  75. OldValueBuffer = VirtualAlloc( NULL, OldValueBufferSize, MEM_COMMIT, PAGE_READWRITE );
  76. if (OldValueBuffer == NULL) {
  77. FatalError( "Unable to allocate large value buffer (%u)",
  78. GetLastError(), 0 );
  79. }
  80. return;
  81. }
  82. void
  83. DisplayIndentedString(
  84. ULONG IndentAmount,
  85. PCHAR sBegin
  86. )
  87. {
  88. PCHAR sEnd;
  89. while (sBegin != NULL) {
  90. sEnd = sBegin;
  91. while (*sEnd && *sEnd != '\n') {
  92. sEnd += 1;
  93. }
  94. MsgFprintf( stderr, "%.*s%.*s\n",
  95. IndentAmount,
  96. " ",
  97. sEnd - sBegin, sBegin
  98. );
  99. NumberOfLinesOutput += 1;
  100. if (OutputHeight != 0 && NumberOfLinesOutput >= OutputHeight) {
  101. MsgFprintf( stderr, "more...press any key to continue" );
  102. TSGetch();
  103. MsgFprintf( stderr, "\r \r" );
  104. NumberOfLinesOutput = 2;
  105. }
  106. if (*sEnd == '\0') {
  107. break;
  108. }
  109. else {
  110. sBegin = ++sEnd;
  111. }
  112. }
  113. return;
  114. }
  115. void
  116. Usage(
  117. LPSTR Message,
  118. ULONG_PTR MessageParameter
  119. )
  120. {
  121. ULONG n;
  122. LPSTR sBegin, sEnd;
  123. NumberOfLinesOutput += 1;
  124. n = MsgFprintf( stderr, "usage: %s ", SavedModuleName );
  125. MsgFprintf( stderr, "[-m \\\\machinename" );
  126. if (!RegBackSwitches) {
  127. MsgFprintf( stderr, " | -h hivefile hiveroot | -w Win95 Directory" );
  128. }
  129. MsgFprintf( stderr, "]\n" );
  130. NumberOfLinesOutput += 1;
  131. if (!RegBackSwitches) {
  132. DisplayIndentedString( n, "[-i n] [-o outputWidth]" );
  133. }
  134. DisplayIndentedString( n, SavedModuleUsage1 );
  135. MsgFprintf( stderr, "\n" );
  136. NumberOfLinesOutput += 1;
  137. n = MsgFprintf( stderr, "where: " );
  138. MsgFprintf( stderr, "-m specifies a remote Windows NT machine whose registry is to be manipulated.\n" );
  139. NumberOfLinesOutput += 1;
  140. if (!RegBackSwitches) {
  141. DisplayIndentedString( n,
  142. "-h specifies a specify local hive to manipulate.\n"
  143. "-w specifies the paths to a Windows 95 system.dat and user.dat files\n"
  144. "-i n specifies the display indentation multiple. Default is 4\n"
  145. "-o outputWidth specifies how wide the output is to be. By default the\n"
  146. " outputWidth is set to the width of the console window if standard\n"
  147. " output has not been redirected to a file. In the latter case, an\n"
  148. " outputWidth of 240 is used."
  149. );
  150. MsgFprintf( stderr, "\n" );
  151. NumberOfLinesOutput += 1;
  152. }
  153. DisplayIndentedString( n, SavedModuleUsage2 );
  154. if (!RegBackSwitches) {
  155. DisplayIndentedString( n,
  156. "Whenever specifying a registry path, either on the command line\n"
  157. "or in an input file, the following prefix strings can be used:\n"
  158. "\n"
  159. " HKEY_LOCAL_MACHINE\n"
  160. " HKEY_USERS\n"
  161. " HKEY_CURRENT_USER\n"
  162. " USER:\n"
  163. "\n"
  164. " Each of these strings can stand alone as the key name or be followed\n"
  165. " a backslash and a subkey path."
  166. );
  167. MsgFprintf( stderr, "\n" );
  168. NumberOfLinesOutput += 1;
  169. }
  170. //
  171. // No return from FatalError
  172. //
  173. if (Message != NULL) {
  174. MsgFprintf( stderr, "\n" );
  175. NumberOfLinesOutput += 1;
  176. }
  177. FatalError( Message, MessageParameter, 0 );
  178. }
  179. void
  180. FatalError(
  181. LPSTR Message,
  182. ULONG_PTR MessageParameter1,
  183. ULONG_PTR MessageParameter2
  184. )
  185. {
  186. if (Message != NULL) {
  187. MsgFprintf( stderr, "%s: ", SavedModuleName );
  188. MsgFprintf( stderr, Message, MessageParameter1, MessageParameter2 );
  189. MsgFprintf( stderr, "\n" );
  190. }
  191. if (SavedCtrlCHandler != NULL) {
  192. (*SavedCtrlCHandler)( CTRL_BREAK_EVENT );
  193. }
  194. exit( 1 );
  195. }
  196. void
  197. InputMessage(
  198. PWSTR FileName,
  199. ULONG LineNumber,
  200. BOOLEAN Error,
  201. LPSTR Message,
  202. ULONG_PTR MessageParameter1,
  203. ULONG_PTR MessageParameter2
  204. )
  205. {
  206. MsgFprintf( stderr, "%ws(%u) : %s: ", FileName, LineNumber, Error ? "error" : "warning" );
  207. MsgFprintf( stderr, Message, MessageParameter1, MessageParameter2 );
  208. MsgFprintf( stderr, "\n" );
  209. return;
  210. }
  211. PWSTR
  212. GetArgAsUnicode(
  213. LPSTR s
  214. )
  215. {
  216. ULONG n;
  217. PWSTR ps;
  218. n = strlen( s );
  219. ps = HeapAlloc( GetProcessHeap(),
  220. 0,
  221. (n + 1) * sizeof( WCHAR )
  222. );
  223. if (ps == NULL) {
  224. FatalError( "Out of memory", 0, 0 );
  225. }
  226. if (MultiByteToWideChar( CP_ACP,
  227. MB_PRECOMPOSED,
  228. s,
  229. n,
  230. ps,
  231. n
  232. ) != (LONG)n
  233. ) {
  234. FatalError( "Unable to convert parameter '%s' to Unicode (%u)",
  235. (ULONG_PTR)s, GetLastError() );
  236. }
  237. ps[ n ] = UNICODE_NULL;
  238. return ps;
  239. }
  240. void
  241. CommonSwitchProcessing(
  242. PULONG argc,
  243. PCHAR **argv,
  244. CHAR c
  245. )
  246. {
  247. c = (char)tolower( c );
  248. switch( c ) {
  249. case 'd':
  250. DebugOutput = TRUE;
  251. break;
  252. case 'o':
  253. if (--*argc) {
  254. OutputWidth = atoi( *++(*argv) );
  255. break;
  256. }
  257. else {
  258. Usage( "Missing argument to -o switch", 0 );
  259. }
  260. case 'i':
  261. if (--*argc) {
  262. IndentMultiple = atoi( *++(*argv) );
  263. break;
  264. }
  265. else {
  266. Usage( "Missing parameter for -%c switch", (ULONG)c );
  267. }
  268. case 'm':
  269. if (HiveFileName != NULL || HiveRootName != NULL ||
  270. Win95Path != NULL || Win95UserPath != NULL
  271. ) {
  272. Usage( "May only specify one of -h, -m or -w switches", 0 );
  273. }
  274. if (--*argc) {
  275. MachineName = GetArgAsUnicode( *++(*argv) );
  276. if (_wcsnicmp( MachineName, L"\\\\", 2 )) {
  277. FatalError( "Invalid machine name - '%ws'",
  278. (ULONG_PTR)MachineName, 0 );
  279. }
  280. }
  281. else {
  282. Usage( "Missing parameter for -%c switch", (ULONG)c );
  283. }
  284. break;
  285. case 'w':
  286. if (MachineName != NULL || HiveFileName != NULL || HiveRootName != NULL) {
  287. Usage( "May only specify one of -h, -m or -w switches", 0 );
  288. }
  289. if (--*argc && --*argc) {
  290. Win95Path = GetArgAsUnicode( *++(*argv) );
  291. Win95UserPath = GetArgAsUnicode( *++(*argv) );
  292. }
  293. else {
  294. Usage( "Missing parameter(s) for -%c switch", (ULONG)c );
  295. }
  296. break;
  297. case 'h':
  298. if (MachineName != NULL || Win95Path != NULL || Win95UserPath != NULL) {
  299. Usage( "May only specify one of -h, -m or -w switches", 0 );
  300. }
  301. if (--*argc && --*argc) {
  302. HiveFileName = GetArgAsUnicode( *++(*argv) );
  303. HiveRootName = GetArgAsUnicode( *++(*argv) );
  304. }
  305. else {
  306. Usage( "Missing parameter(s) for -%c switch", (ULONG)c );
  307. }
  308. break;
  309. case '?':
  310. Usage( NULL, 0 );
  311. break;
  312. default:
  313. Usage( "Invalid switch (-%c)", (ULONG)c );
  314. break;
  315. }
  316. return;
  317. }
  318. /*
  319. int MsgFprintf (
  320. FILE *str,
  321. LPSTR format,
  322. ...
  323. )
  324. if we ever need to display messages for english versions we can add the following portion of code.
  325. [
  326. #ifdef OUTPUT_ENGLISH_MSGS
  327. va_list va;
  328. if (PRIMARYLANGID(GetUserDefaultLangID()) == LANG_ENGLISH ) {
  329. va_start(va, format);
  330. i = vfprintf (str, format, va);
  331. va_end(va);
  332. }
  333. #endif // OUTPUT_ENGLISH_MSGS
  334. ]
  335. */
  336. int
  337. __cdecl
  338. MsgFprintf (
  339. FILE *str,
  340. LPSTR format,
  341. ...
  342. )
  343. {
  344. int i=0;
  345. //#ifndef SILENT_TS_TOOL
  346. va_list va;
  347. va_start(va, format);
  348. i = vfprintf (str, format, va);
  349. va_end(va);
  350. //#endif // SILENT_TS_TOOL
  351. return (i);
  352. }
  353. void TSGetch()
  354. {
  355. #ifndef SILENT_TS_TOOL
  356. _getch();
  357. #endif // SILENT_TS_TOOL
  358. }