Source code of Windows XP (NT5)
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.

616 lines
15 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Copyright (c) 1997-1999 Microsoft Corporation
  4. // All rights reserved.
  5. //
  6. // File Name:
  7. // cmdlines.c
  8. //
  9. // Description:
  10. // Dialog proc for the cmdlines.txt page
  11. //
  12. //----------------------------------------------------------------------------
  13. #include "pch.h"
  14. #include "resource.h"
  15. #define MAX_CMDLINE 1024
  16. #define CMD_FILENAME _T("cmdlines.txt")
  17. static NAMELIST CmdLinesList = { 0 };
  18. //----------------------------------------------------------------------------
  19. //
  20. // Function: SetPathToCmdlines
  21. //
  22. // Purpose: Determines the path to the cmdlines.txt and set PathBuffer to it.
  23. // PathBuffer is assumed to be MAX_PATH long
  24. //
  25. // Arguments: OUT TCHAR *PathBuffer - path to cmdlines.txt
  26. //
  27. // Returns: VOID
  28. //
  29. //----------------------------------------------------------------------------
  30. static VOID
  31. SetPathToCmdlines( OUT TCHAR *PathBuffer, DWORD cbPath )
  32. {
  33. if( WizGlobals.iProductInstall == PRODUCT_SYSPREP )
  34. {
  35. TCHAR szDrive[MAX_PATH];
  36. TCHAR szSysprepPath[MAX_PATH] = _T("");
  37. ExpandEnvironmentStrings( _T("%SystemDrive%"),
  38. szDrive,
  39. MAX_PATH );
  40. ConcatenatePaths( szSysprepPath,
  41. szDrive,
  42. _T("\\sysprep\\i386\\$oem$"),
  43. NULL );
  44. EnsureDirExists( szSysprepPath );
  45. lstrcpyn( PathBuffer, szSysprepPath, cbPath );
  46. }
  47. else
  48. {
  49. lstrcpyn( PathBuffer, WizGlobals.OemFilesPath, cbPath );
  50. }
  51. }
  52. //----------------------------------------------------------------------------
  53. //
  54. // Function: LoadCmdLinesFile
  55. //
  56. // Purpose: Loads the contents of cmdlines.txt into memory.
  57. //
  58. //----------------------------------------------------------------------------
  59. VOID LoadCmdLinesFile(HWND hwnd)
  60. {
  61. FILE *fp;
  62. TCHAR CmdLineBuffer[MAX_CMDLINE + 1];
  63. TCHAR PathBuffer[MAX_PATH];
  64. ResetNameList(&CmdLinesList);
  65. SetPathToCmdlines( PathBuffer, AS(PathBuffer) );
  66. ConcatenatePaths(PathBuffer, CMD_FILENAME, NULL);
  67. if ( (fp = My_fopen(PathBuffer, _T("r") )) == NULL )
  68. return;
  69. //
  70. // Add all the entries to the namelist except for the [Commands] line
  71. //
  72. while( My_fgets(CmdLineBuffer, MAX_CMDLINE, fp) != NULL ) {
  73. if( _tcsstr( CmdLineBuffer, _T("[Commands]") ) == NULL ) {
  74. AddNameToNameList( &CmdLinesList,
  75. CleanSpaceAndQuotes( CmdLineBuffer ) );
  76. }
  77. }
  78. My_fclose(fp);
  79. }
  80. //----------------------------------------------------------------------------
  81. //
  82. // Function: WriteCmdLinesFile
  83. //
  84. // Purpose: Writes the contents of our in-memory cmdlines to disk.
  85. //
  86. //----------------------------------------------------------------------------
  87. VOID WriteCmdLinesFile(HWND hwnd)
  88. {
  89. UINT i, nNames;
  90. UINT iNumCmdLinesEntries;
  91. FILE *fp;
  92. TCHAR PathBuffer[MAX_PATH], *pCommand;
  93. //
  94. // If there are no command lines to write then don't create the
  95. // cmdlines.txt file.
  96. //
  97. iNumCmdLinesEntries = GetNameListSize( &CmdLinesList );
  98. if( iNumCmdLinesEntries == 0 ) {
  99. return;
  100. }
  101. //
  102. // Keep trying to open cmdlines.txt until it's open or until the
  103. // user gives up
  104. //
  105. SetPathToCmdlines( PathBuffer, AS(PathBuffer) );
  106. ConcatenatePaths(PathBuffer, CMD_FILENAME, NULL);
  107. do {
  108. if ( (fp = My_fopen(PathBuffer, _T("w") )) == NULL ) {
  109. UINT iRet = ReportErrorId(
  110. hwnd,
  111. MSGTYPE_RETRYCANCEL | MSGTYPE_WIN32,
  112. IDS_OPEN_CMDLINES_FAILED,
  113. PathBuffer);
  114. if ( iRet != IDRETRY )
  115. return;
  116. } else
  117. break;
  118. } while ( TRUE );
  119. //
  120. // ISSUE-2002/02/28-stelo- Check return value from fputs
  121. //
  122. My_fputs( _T("[Commands]\n"), fp );
  123. //
  124. // Write out each command in CmdLinesList
  125. //
  126. for ( i = 0, nNames = GetNameListSize(&CmdLinesList);
  127. i < nNames;
  128. i++ ) {
  129. pCommand = GetNameListName(&CmdLinesList, i);
  130. My_fputs( _T("\""), fp );
  131. My_fputs(pCommand, fp);
  132. My_fputs( _T("\"\n"), fp );
  133. }
  134. My_fclose(fp);
  135. }
  136. //----------------------------------------------------------------------------
  137. //
  138. // Function: GreyCmdLinesPage
  139. //
  140. // Purpose: Greys out the remove button if nothing selected
  141. //
  142. //----------------------------------------------------------------------------
  143. VOID GreyCmdLinesPage(HWND hwnd)
  144. {
  145. INT_PTR idx;
  146. HWND hCtrl = GetDlgItem(hwnd, IDC_REMOVECMD);
  147. idx = SendDlgItemMessage(hwnd,
  148. IDC_CMDLIST,
  149. LB_GETCURSEL,
  150. (WPARAM) 0,
  151. (LPARAM) 0);
  152. //
  153. // Grey the remove button unless something is selected
  154. //
  155. EnableWindow(hCtrl, idx != LB_ERR);
  156. }
  157. //----------------------------------------------------------------------------
  158. //
  159. // Function: OnSelChangeCmdLines
  160. //
  161. // Purpose: Called when user selects an item on the cmd list. We need
  162. // to ungrey the remove button
  163. //
  164. //----------------------------------------------------------------------------
  165. VOID OnCmdLinesSelChange(HWND hwnd)
  166. {
  167. SetArrows( hwnd,
  168. IDC_CMDLIST,
  169. IDC_BUT_MOVE_UP,
  170. IDC_BUT_MOVE_DOWN );
  171. GreyCmdLinesPage(hwnd);
  172. }
  173. //----------------------------------------------------------------------------
  174. //
  175. // Function: OnSetActiveCmdLines
  176. //
  177. // Purpose: Called at SETACTIVE time. We load the current contents
  178. // of cmdline.txt into memory and display it. We read the
  179. // file at SETACTIVE time so that the user has a way of
  180. // refreshing the display.
  181. //
  182. //----------------------------------------------------------------------------
  183. VOID OnSetActiveCmdLines(HWND hwnd)
  184. {
  185. UINT i, nNames;
  186. LPTSTR pNextName;
  187. //
  188. // Load cmdlines.txt into memory
  189. //
  190. LoadCmdLinesFile(hwnd);
  191. //
  192. // Reset the display to match what is in memory
  193. //
  194. SendDlgItemMessage(hwnd,
  195. IDC_CMDLIST,
  196. LB_RESETCONTENT,
  197. (WPARAM) 0,
  198. (LPARAM) 0);
  199. for ( i = 0, nNames = GetNameListSize(&CmdLinesList);
  200. i < nNames;
  201. i++ ) {
  202. pNextName = GetNameListName(&CmdLinesList, i);
  203. SendDlgItemMessage(hwnd,
  204. IDC_CMDLIST,
  205. LB_ADDSTRING,
  206. (WPARAM) 0,
  207. (LPARAM) pNextName);
  208. }
  209. GreyCmdLinesPage(hwnd);
  210. }
  211. //----------------------------------------------------------------------------
  212. //
  213. // Function: OnAddCmdLine
  214. //
  215. // Purpose: Called when user pushes ADD button. Get command from
  216. // edit field and add it to the in-memory list.
  217. //
  218. //----------------------------------------------------------------------------
  219. VOID OnAddCmdLine(HWND hwnd)
  220. {
  221. TCHAR CmdBuffer[MAX_CMDLINE + 1];
  222. //
  223. // get the command the user typed in
  224. //
  225. GetDlgItemText(hwnd, IDT_CMDLINE, CmdBuffer, MAX_CMDLINE);
  226. //
  227. // Don't add a blank command
  228. //
  229. if( CmdBuffer[0] == _T('\0') )
  230. {
  231. return;
  232. }
  233. //
  234. // display what the user typed-in in the listbox
  235. // and clear out the name the user typed
  236. //
  237. SendDlgItemMessage(hwnd,
  238. IDC_CMDLIST,
  239. LB_ADDSTRING,
  240. (WPARAM) 0,
  241. (LPARAM) CmdBuffer);
  242. SetArrows( hwnd,
  243. IDC_CMDLIST,
  244. IDC_BUT_MOVE_UP,
  245. IDC_BUT_MOVE_DOWN );
  246. SetDlgItemText( hwnd, IDT_CMDLINE, _T("") );
  247. SetFocus(GetDlgItem(hwnd, IDT_CMDLINE));
  248. }
  249. //----------------------------------------------------------------------------
  250. //
  251. // Function: OnRemoveCmdLine
  252. //
  253. // Purpose: Called when user pushes REMOVE button. Get selected command
  254. // and remove it from display and memory.
  255. //
  256. //----------------------------------------------------------------------------
  257. VOID OnRemoveCmdLine(HWND hwnd)
  258. {
  259. TCHAR CmdBuffer[MAX_CMDLINE + 1];
  260. INT_PTR idx, Count;
  261. //
  262. // Get users selection of the command to remove
  263. //
  264. idx = SendDlgItemMessage(hwnd,
  265. IDC_CMDLIST,
  266. LB_GETCURSEL,
  267. (WPARAM) 0,
  268. (LPARAM) 0);
  269. if ( idx == LB_ERR )
  270. return;
  271. //
  272. // Retrieve the name to remove from listbox
  273. //
  274. SendDlgItemMessage(hwnd,
  275. IDC_CMDLIST,
  276. LB_GETTEXT,
  277. (WPARAM) idx,
  278. (LPARAM) CmdBuffer);
  279. //
  280. // Remove it from the listbox display
  281. //
  282. SendDlgItemMessage(hwnd,
  283. IDC_CMDLIST,
  284. LB_DELETESTRING,
  285. (WPARAM) idx,
  286. (LPARAM) 0);
  287. //
  288. // Have to set a new selection.
  289. //
  290. Count = SendDlgItemMessage(hwnd,
  291. IDC_CMDLIST,
  292. LB_GETCOUNT,
  293. (WPARAM) 0,
  294. (LPARAM) 0);
  295. if ( Count ) {
  296. if ( idx >= Count )
  297. idx--;
  298. SendDlgItemMessage(hwnd,
  299. IDC_CMDLIST,
  300. LB_SETCURSEL,
  301. (WPARAM) idx,
  302. (LPARAM) 0);
  303. }
  304. SetArrows( hwnd,
  305. IDC_CMDLIST,
  306. IDC_BUT_MOVE_UP,
  307. IDC_BUT_MOVE_DOWN );
  308. //
  309. // There might be nothing selected now
  310. //
  311. GreyCmdLinesPage(hwnd);
  312. }
  313. //----------------------------------------------------------------------------
  314. //
  315. // Function: OnCommandLinesInitDialog
  316. //
  317. // Purpose:
  318. //
  319. // Arguments: IN HWND hwnd - handle to the dialog
  320. //
  321. // Returns: VOID
  322. //
  323. //----------------------------------------------------------------------------
  324. VOID
  325. OnCommandLinesInitDialog( IN HWND hwnd )
  326. {
  327. //
  328. // Set text limit
  329. //
  330. SendDlgItemMessage(hwnd,
  331. IDT_CMDLINE,
  332. EM_LIMITTEXT,
  333. (WPARAM) MAX_CMDLINE,
  334. (LPARAM) 0);
  335. SetArrows( hwnd,
  336. IDC_CMDLIST,
  337. IDC_BUT_MOVE_UP,
  338. IDC_BUT_MOVE_DOWN );
  339. }
  340. //----------------------------------------------------------------------------
  341. //
  342. // Function: OnWizNextCmdLines
  343. //
  344. // Purpose: Called when user pushes NEXT button. Write the file out.
  345. //
  346. //----------------------------------------------------------------------------
  347. VOID OnWizNextCmdLines(HWND hwnd)
  348. {
  349. INT_PTR i;
  350. INT_PTR iRetVal;
  351. INT_PTR iNumItems;
  352. TCHAR CmdBuffer[MAX_CMDLINE + 1];
  353. BOOL bStayHere = FALSE;
  354. //
  355. // If the user typed something into the command field but failed to
  356. // ADD it, auto-add it
  357. //
  358. GetDlgItemText(hwnd, IDT_CMDLINE, CmdBuffer, MAX_CMDLINE + 1);
  359. if ( CmdBuffer[0] != _T('\0') )
  360. OnAddCmdLine(hwnd);
  361. //
  362. // Store all the entries in the list box into the Command Lines namelist
  363. //
  364. iNumItems = SendDlgItemMessage( hwnd,
  365. IDC_CMDLIST,
  366. LB_GETCOUNT,
  367. (WPARAM) 0,
  368. (LPARAM) 0 );
  369. ResetNameList( &CmdLinesList );
  370. for( i = 0; i < iNumItems; i++ )
  371. {
  372. iRetVal = SendDlgItemMessage( hwnd,
  373. IDC_CMDLIST,
  374. LB_GETTEXT,
  375. (WPARAM) i,
  376. (LPARAM) CmdBuffer );
  377. if( iRetVal == LB_ERR )
  378. {
  379. AssertMsg( FALSE,
  380. "Error adding items to namelist." );
  381. break;
  382. }
  383. AddNameToNameList(&CmdLinesList, CmdBuffer);
  384. }
  385. //
  386. // Write the cmd lines file and move the wizard on
  387. //
  388. WriteCmdLinesFile(hwnd);
  389. }
  390. //----------------------------------------------------------------------------
  391. //
  392. // Function: DlgCommandLinesPage
  393. //
  394. // Purpose: Dlg proc.
  395. //
  396. //----------------------------------------------------------------------------
  397. INT_PTR CALLBACK DlgCommandLinesPage(
  398. IN HWND hwnd,
  399. IN UINT uMsg,
  400. IN WPARAM wParam,
  401. IN LPARAM lParam)
  402. {
  403. BOOL bStatus = TRUE;
  404. switch (uMsg) {
  405. case WM_INITDIALOG:
  406. OnCommandLinesInitDialog( hwnd );
  407. break;
  408. case WM_COMMAND:
  409. {
  410. int nButtonId;
  411. switch ( nButtonId = LOWORD(wParam) ) {
  412. case IDC_ADDCMD:
  413. if ( HIWORD(wParam) == BN_CLICKED )
  414. OnAddCmdLine(hwnd);
  415. break;
  416. case IDC_REMOVECMD:
  417. if ( HIWORD(wParam) == BN_CLICKED )
  418. OnRemoveCmdLine(hwnd);
  419. break;
  420. case IDC_CMDLIST:
  421. if ( HIWORD(wParam) == LBN_SELCHANGE )
  422. OnCmdLinesSelChange(hwnd);
  423. break;
  424. case IDC_BUT_MOVE_UP:
  425. OnUpButtonPressed( hwnd, IDC_CMDLIST );
  426. SetArrows( hwnd,
  427. IDC_CMDLIST,
  428. IDC_BUT_MOVE_UP,
  429. IDC_BUT_MOVE_DOWN );
  430. break;
  431. case IDC_BUT_MOVE_DOWN:
  432. OnDownButtonPressed( hwnd, IDC_CMDLIST );
  433. SetArrows( hwnd,
  434. IDC_CMDLIST,
  435. IDC_BUT_MOVE_UP,
  436. IDC_BUT_MOVE_DOWN );
  437. break;
  438. default:
  439. bStatus = FALSE;
  440. break;
  441. }
  442. }
  443. break;
  444. case WM_NOTIFY:
  445. {
  446. LPNMHDR pnmh = (LPNMHDR)lParam;
  447. switch( pnmh->code ) {
  448. case PSN_QUERYCANCEL:
  449. WIZ_CANCEL(hwnd);
  450. break;
  451. case PSN_SETACTIVE:
  452. g_App.dwCurrentHelp = IDH_ADDL_CMND;
  453. if ( WizGlobals.iProductInstall == PRODUCT_UNATTENDED_INSTALL )
  454. WIZ_BUTTONS(hwnd, PSWIZB_BACK | PSWIZB_FINISH);
  455. else
  456. WIZ_BUTTONS(hwnd, PSWIZB_BACK | PSWIZB_NEXT);
  457. OnSetActiveCmdLines(hwnd);
  458. break;
  459. case PSN_WIZBACK:
  460. bStatus = FALSE;
  461. break;
  462. case PSN_WIZNEXT:
  463. OnWizNextCmdLines(hwnd);
  464. bStatus = FALSE;
  465. break;
  466. case PSN_HELP:
  467. WIZ_HELP();
  468. break;
  469. default:
  470. bStatus = FALSE;
  471. break;
  472. }
  473. }
  474. break;
  475. default:
  476. bStatus = FALSE;
  477. break;
  478. }
  479. return bStatus;
  480. }