[CLT2012]
[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
28 /*
29 * run_script(const char *filename):
30 * opens the file, reads the contents, convert the text into readable
31 * code for the computer, and then execute commands in order.
32 */
33 BOOL
34 run_script(LPCWSTR filename)
35 {
36 FILE *script_file;
37 WCHAR tmp_string[MAX_STRING_SIZE];
38
39 /* Open the file for processing */
40 script_file = _wfopen(filename, L"r");
41 if (script_file == NULL)
42 {
43 /* if there was problems opening the file */
44 PrintResourceString(IDS_ERROR_MSG_NO_SCRIPT, filename);
45 return FALSE; /* if there is no script, exit the program */
46 }
47
48 /* Read and process the script */
49 while (fgetws(tmp_string, MAX_STRING_SIZE, script_file) != NULL)
50 {
51 if (interpret_script(tmp_string) == FALSE)
52 return FALSE;
53 }
54
55 /* Close the file */
56 fclose(script_file);
57
58 return TRUE;
59 }
60
61 /*
62 * wmain():
63 * Main entry point of the application.
64 */
65 int wmain(int argc, const WCHAR *argv[])
66 {
67 WCHAR szComputerName[MAX_STRING_SIZE];
68 DWORD comp_size = MAX_STRING_SIZE;
69 LPCWSTR file_name = NULL;
70 int i;
71 int timeout = 0;
72
73 /* Get the name of the computer for us and change the value of comp_name */
74 GetComputerName(szComputerName, &comp_size);
75
76 /* TODO: Remove this section of code when program becomes stable enough for production use. */
77 wprintf(L"\n*WARNING*: This program is incomplete and may not work properly.\n");
78
79 /* Print the header information */
80 PrintResourceString(IDS_APP_HEADER, DISKPART_VERSION);
81 PrintResourceString(IDS_APP_LICENSE);
82 PrintResourceString(IDS_APP_CURR_COMPUTER, szComputerName);
83
84 /* Process arguments */
85 for (i = 1; i < argc; i++)
86 {
87 if ((argv[i][0] == L'-') || (argv[i][0] == L'/'))
88 {
89 if (wcsicmp(&argv[i][1], L"s") == 0)
90 {
91 /*
92 * Get the file name only if there is at least one more
93 * argument and it is not another option
94 */
95 if ((i + 1 < argc) &&
96 (argv[i + 1][0] != L'-') &&
97 (argv[i + 1][0] != L'/'))
98 {
99 /* Next argument */
100 i++;
101
102 /* Get the file name */
103 file_name = argv[i];
104 }
105 }
106 else if (wcsicmp(&argv[i][1], L"t") == 0)
107 {
108 /*
109 * Get the timeout value only if there is at least one more
110 * argument and it is not another option
111 */
112 if ((i + 1 < argc) &&
113 (argv[i + 1][0] != L'-') &&
114 (argv[i + 1][0] != L'/'))
115 {
116 /* Next argument */
117 i++;
118
119 /* Get the timeout value */
120 timeout = _wtoi(argv[i]);
121 }
122 }
123 else if (wcscmp(&argv[i][1], L"?") == 0)
124 {
125 PrintResourceString(IDS_APP_USAGE);
126 return EXIT_SUCCESS;
127 }
128 }
129 }
130
131 /* Run the script if we got a script name or call the interpreter otherwise */
132 if (file_name != NULL)
133 {
134 if (run_script(file_name) == FALSE)
135 return EXIT_FAILURE;
136 }
137 else
138 {
139 interpret_main();
140 }
141
142 /* Let the user know the program is exiting */
143 PrintResourceString(IDS_APP_LEAVING);
144
145 return EXIT_SUCCESS;
146 }