* Sync up to trunk head (r60691).
[reactos.git] / base / system / diskpart / diskpart.c
1 /*
2 * PROJECT: ReactOS DiskPart
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/system/diskpart/diskpart.c
5 * PURPOSE: Manages all the partitions of the OS in
6 * an interactive way
7 * PROGRAMMERS: Lee Schroeder
8 */
9
10 /* INCLUDES ******************************************************************/
11 #include "diskpart.h"
12
13 /* FUNCTIONS ******************************************************************/
14 VOID
15 PrintResourceString(INT resID, ...)
16 {
17 WCHAR szMsg[3072];
18 va_list arg_ptr;
19
20 va_start(arg_ptr, resID);
21 LoadStringW(GetModuleHandle(NULL), resID, szMsg, 3072);
22 vwprintf(szMsg, arg_ptr);
23 va_end(arg_ptr);
24 }
25
26 VOID
27 ShowHeader(VOID)
28 {
29 WCHAR szComputerName[MAX_STRING_SIZE];
30 DWORD comp_size = MAX_STRING_SIZE;
31
32 /* Get the name of the computer for us and change the value of comp_name */
33 GetComputerNameW(szComputerName, &comp_size);
34
35 /* TODO: Remove this section of code when program becomes stable enough for production use. */
36 wprintf(L"\n*WARNING*: This program is incomplete and may not work properly.\n");
37
38 /* Print the header information */
39 wprintf(L"\n");
40 PrintResourceString(IDS_APP_HEADER);
41 wprintf(L"\n");
42 PrintResourceString(IDS_APP_LICENSE);
43 PrintResourceString(IDS_APP_CURR_COMPUTER, szComputerName);
44 }
45
46 /*
47 * RunScript(const char *filename):
48 * opens the file, reads the contents, convert the text into readable
49 * code for the computer, and then execute commands in order.
50 */
51 BOOL
52 RunScript(LPCWSTR filename)
53 {
54 FILE *script;
55 WCHAR tmp_string[MAX_STRING_SIZE];
56
57 /* Open the file for processing */
58 script = _wfopen(filename, L"r");
59 if (script == NULL)
60 {
61 /* if there was problems opening the file */
62 PrintResourceString(IDS_ERROR_MSG_NO_SCRIPT, filename);
63 return FALSE; /* if there is no script, exit the program */
64 }
65
66 /* Read and process the script */
67 while (fgetws(tmp_string, MAX_STRING_SIZE, script) != NULL)
68 {
69 if (InterpretScript(tmp_string) == FALSE)
70 return FALSE;
71 }
72
73 /* Close the file */
74 fclose(script);
75
76 return TRUE;
77 }
78
79 /*
80 * wmain():
81 * Main entry point of the application.
82 */
83 int wmain(int argc, const LPWSTR argv[])
84 {
85 LPCWSTR script = NULL;
86 LPCWSTR tmpBuffer = NULL;
87 WCHAR appTitle[50];
88 int index, timeout;
89
90 /* Sets the title of the program so the user will have an easier time
91 determining the current program, especially if diskpart is running a
92 script */
93 LoadStringW(GetModuleHandle(NULL), IDS_APP_HEADER, (LPWSTR)appTitle, 50);
94 SetConsoleTitleW(appTitle);
95
96 /* Sets the timeout value to 0 just in case the user doesn't
97 specify a value */
98 timeout = 0;
99
100 /* If there are no command arguments, then go straight to the interpreter */
101 if (argc < 2)
102 {
103 ShowHeader();
104 InterpretMain();
105 }
106 /* If there are command arguments, then process them */
107 else
108 {
109 for (index = 1; index < argc; index++)
110 {
111 /* checks for flags */
112 if ((argv[index][0] == '/')||
113 (argv[index][0] == '-'))
114 {
115 tmpBuffer = argv[index] + 1;
116 }
117 else
118 {
119 /* If there is no flag, then return an error */
120 PrintResourceString(IDS_ERROR_MSG_BAD_ARG, argv[index]);
121 return EXIT_FAILURE;
122 }
123
124 /* Checks for the /? flag first since the program
125 exits as soon as the usage list is shown. */
126 if (_wcsicmp(tmpBuffer, L"?") == 0)
127 {
128 PrintResourceString(IDS_APP_USAGE);
129 return EXIT_SUCCESS;
130 }
131 /* Checks for the script flag */
132 else if (_wcsicmp(tmpBuffer, L"s") == 0)
133 {
134 if ((index + 1) < argc)
135 {
136 index++;
137 script = argv[index];
138 }
139 }
140 /* Checks for the timeout flag */
141 else if (_wcsicmp(tmpBuffer, L"t") == 0)
142 {
143 if ((index + 1) < argc)
144 {
145 index++;
146 timeout = _wtoi(argv[index]);
147
148 /* If the number is a negative number, then
149 change it so that the time is executed properly. */
150 if (timeout < 0)
151 timeout = 0;
152 }
153 }
154 else
155 {
156 /* Assume that the flag doesn't exist. */
157 PrintResourceString(IDS_ERROR_MSG_BAD_ARG, tmpBuffer);
158 return EXIT_FAILURE;
159 }
160 }
161
162 /* Shows the program information */
163 ShowHeader();
164
165 /* Now we process the filename if it exists */
166 if (script != NULL)
167 {
168 /* if the timeout is greater than 0, then assume
169 that the user specified a specific time. */
170 if (timeout > 0)
171 Sleep(timeout * 1000);
172
173 if (RunScript(script) == FALSE)
174 return EXIT_FAILURE;
175 }
176 else
177 {
178 /* Exit failure since the user wanted to run a script */
179 PrintResourceString(IDS_ERROR_MSG_NO_SCRIPT, script);
180 return EXIT_FAILURE;
181 }
182 }
183
184 /* Let the user know the program is exiting */
185 PrintResourceString(IDS_APP_LEAVING);
186
187 return EXIT_SUCCESS;
188 }