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