[DISKPART]

- Rewrite argument parsing. Patch by Lee Schroeder.
See issue #7170 for more details.

svn path=/trunk/; revision=56966
This commit is contained in:
Thomas Faber 2012-07-24 20:23:24 +00:00
parent caa3183c46
commit cbe2bd44d0
6 changed files with 127 additions and 91 deletions

View file

@ -24,21 +24,38 @@ PrintResourceString(INT resID, ...)
va_end(arg_ptr);
}
VOID
ShowHeader(VOID)
{
WCHAR szComputerName[MAX_STRING_SIZE];
DWORD comp_size = MAX_STRING_SIZE;
/* Get the name of the computer for us and change the value of comp_name */
GetComputerName(szComputerName, &comp_size);
/* TODO: Remove this section of code when program becomes stable enough for production use. */
wprintf(L"\n*WARNING*: This program is incomplete and may not work properly.\n");
/* Print the header information */
PrintResourceString(IDS_APP_HEADER);
PrintResourceString(IDS_APP_LICENSE);
PrintResourceString(IDS_APP_CURR_COMPUTER, szComputerName);
}
/*
* run_script(const char *filename):
* RunScript(const char *filename):
* opens the file, reads the contents, convert the text into readable
* code for the computer, and then execute commands in order.
*/
BOOL
run_script(LPCWSTR filename)
RunScript(LPCWSTR filename)
{
FILE *script_file;
FILE *script;
WCHAR tmp_string[MAX_STRING_SIZE];
/* Open the file for processing */
script_file = _wfopen(filename, L"r");
if (script_file == NULL)
script = _wfopen(filename, L"r");
if (script == NULL)
{
/* if there was problems opening the file */
PrintResourceString(IDS_ERROR_MSG_NO_SCRIPT, filename);
@ -46,14 +63,14 @@ run_script(LPCWSTR filename)
}
/* Read and process the script */
while (fgetws(tmp_string, MAX_STRING_SIZE, script_file) != NULL)
while (fgetws(tmp_string, MAX_STRING_SIZE, script) != NULL)
{
if (interpret_script(tmp_string) == FALSE)
if (InterpretScript(tmp_string) == FALSE)
return FALSE;
}
/* Close the file */
fclose(script_file);
fclose(script);
return TRUE;
}
@ -64,79 +81,96 @@ run_script(LPCWSTR filename)
*/
int wmain(int argc, const WCHAR *argv[])
{
WCHAR szComputerName[MAX_STRING_SIZE];
DWORD comp_size = MAX_STRING_SIZE;
LPCWSTR file_name = NULL;
int i;
int timeout = 0;
LPCWSTR script = NULL;
LPCWSTR tmpBuffer = NULL;
int index, timeout;
/* Get the name of the computer for us and change the value of comp_name */
GetComputerName(szComputerName, &comp_size);
/* Sets the timeout value to 0 just in case the user doesn't
specify a value. */
timeout = 0;
/* TODO: Remove this section of code when program becomes stable enough for production use. */
wprintf(L"\n*WARNING*: This program is incomplete and may not work properly.\n");
/* Print the header information */
PrintResourceString(IDS_APP_HEADER, DISKPART_VERSION);
PrintResourceString(IDS_APP_LICENSE);
PrintResourceString(IDS_APP_CURR_COMPUTER, szComputerName);
/* Process arguments */
for (i = 1; i < argc; i++)
/* If there are no command arguments, then go straight to the interpreter */
if (argc < 2)
{
if ((argv[i][0] == L'-') || (argv[i][0] == L'/'))
ShowHeader();
InterpretMain();
}
/* If there are command arguments, then process them */
else
{
for (index = 1; index < argc; index++)
{
if (wcsicmp(&argv[i][1], L"s") == 0)
/* checks for flags */
if ((argv[index][0] == '/')||
(argv[index][0] == '-'))
{
/*
* Get the file name only if there is at least one more
* argument and it is not another option
*/
if ((i + 1 < argc) &&
(argv[i + 1][0] != L'-') &&
(argv[i + 1][0] != L'/'))
{
/* Next argument */
i++;
/* Get the file name */
file_name = argv[i];
}
tmpBuffer = argv[index] + 1;
}
else if (wcsicmp(&argv[i][1], L"t") == 0)
else
{
/*
* Get the timeout value only if there is at least one more
* argument and it is not another option
*/
if ((i + 1 < argc) &&
(argv[i + 1][0] != L'-') &&
(argv[i + 1][0] != L'/'))
{
/* Next argument */
i++;
/* Get the timeout value */
timeout = _wtoi(argv[i]);
}
/* If there is no flag, then return an error */
PrintResourceString(IDS_ERROR_MSG_BAD_ARG, argv[index]);
return EXIT_FAILURE;
}
else if (wcscmp(&argv[i][1], L"?") == 0)
/* Checks for the /? flag first since the program
exits as soon as the usage list is shown. */
if (_wcsicmp(tmpBuffer, L"?") == 0)
{
PrintResourceString(IDS_APP_USAGE);
return EXIT_SUCCESS;
}
}
}
/* Checks for the script flag */
else if (_wcsicmp(tmpBuffer, L"s") == 0)
{
if ((index + 1) < argc)
{
index++;
script = argv[index];
}
}
/* Checks for the timeout flag */
else if (_wcsicmp(tmpBuffer, L"t") == 0)
{
if ((index + 1) < argc)
{
index++;
timeout = _wtoi(argv[index]);
/* Run the script if we got a script name or call the interpreter otherwise */
if (file_name != NULL)
{
if (run_script(file_name) == FALSE)
/* If the number is a negative number, then
change it so that the time is executed properly. */
if (timeout < 0)
timeout = 0;
}
}
else
{
/* Assume that the flag doesn't exist. */
PrintResourceString(IDS_ERROR_MSG_BAD_ARG, tmpBuffer);
return EXIT_FAILURE;
}
}
/* Shows the program information */
ShowHeader();
/* Now we process the filename if it exists */
if (script != NULL)
{
/* if the timeout is greater than 0, then assume
that the user specified a specific time. */
if (timeout > 0)
Sleep(timeout * 1000);
if (RunScript(script) == FALSE)
return EXIT_FAILURE;
}
else
{
/* Exit failure since the user wanted to run a script */
PrintResourceString(IDS_ERROR_MSG_NO_SCRIPT, script);
return EXIT_FAILURE;
}
else
{
interpret_main();
}
}
/* Let the user know the program is exiting */

View file

@ -138,9 +138,9 @@ BOOL inactive_main(INT argc, WCHAR **argv);
VOID help_inactive(INT argc, WCHAR **argv);
/* interpreter.c */
BOOL interpret_script(WCHAR *line);
BOOL interpret_cmd(INT argc, WCHAR **argv);
VOID interpret_main(VOID);
BOOL InterpretScript(WCHAR *line);
BOOL InterpretCmd(INT argc, WCHAR **argv);
VOID InterpretMain(VOID);
/* list.c */
BOOL list_main(INT argc, WCHAR **argv);

View file

@ -73,12 +73,12 @@ rem_main(INT argc, WCHAR **argv)
/*
* interpret_cmd(char *cmd_line, char *arg_line):
* InterpretCmd(char *cmd_line, char *arg_line):
* compares the command name to a list of available commands, and
* determines which function to envoke.
*/
BOOL
interpret_cmd(int argc, WCHAR **argv)
InterpretCmd(int argc, WCHAR **argv)
{
PCOMMAND cmdptr;
@ -98,11 +98,11 @@ interpret_cmd(int argc, WCHAR **argv)
/*
* interpret_script(char *line):
* InterpretScript(char *line):
* The main function used for when reading commands from scripts.
*/
BOOL
interpret_script(WCHAR *input_line)
InterpretScript(WCHAR *input_line)
{
WCHAR *args_vector[MAX_ARGS_COUNT];
INT args_count = 0;
@ -134,18 +134,18 @@ interpret_script(WCHAR *input_line)
}
/* sends the string to find the command */
return interpret_cmd(args_count, args_vector);
return InterpretCmd(args_count, args_vector);
}
/*
* interpret_main():
* InterpretMain():
* Contents for the main program loop as it reads each line, and then
* it sends the string to interpret_line, where it determines what
* command to use.
*/
VOID
interpret_main(VOID)
InterpretMain(VOID)
{
WCHAR input_line[MAX_STRING_SIZE];
WCHAR *args_vector[MAX_ARGS_COUNT];
@ -187,6 +187,6 @@ interpret_main(VOID)
}
/* sends the string to find the command */
bRun = interpret_cmd(args_count, args_vector);
bRun = InterpretCmd(args_count, args_vector);
}
}

View file

@ -13,12 +13,12 @@ LANGUAGE LANG_ENGLISH, SUBLANG_DEFAULT
/* Basic application information */
STRINGTABLE DISCARDABLE
BEGIN
IDS_APP_HEADER, "\nReactOS DiskPart version %s\n"
IDS_APP_USAGE, "Diskpart command line syntax:\ndiskpart [/s <script file>] [/t <timeout value>] [/?]\n\
/s <script file> - Runs the given script file.\n\
/t <timeout value> - Waits for the given time (in seconds) after running a\n\
script file.\n\
/? - Shows this help text."
IDS_APP_HEADER, "\nReactOS DiskPart\n"
IDS_APP_USAGE, "\nDisk Partitioning Interpreter.\n\n\
Usage: DISKPART [/S filename] [/T timeout] [/?]\n\n\
/S filename\tRuns the given script.\n\
/T timeout\tTimeout in seconds to prevent diskpart usage overlap.\n\
/?\t\tDisplay this help message.\n\n"
IDS_APP_LICENSE, "Licensed under the GNU GPLv2\n"
IDS_APP_CURR_COMPUTER, "On computer: %s\n\n"
IDS_APP_LEAVING, "\nLeaving DiskPart...\n"
@ -120,7 +120,8 @@ END
/* Common Error Messages */
STRINGTABLE DISCARDABLE
BEGIN
IDS_ERROR_MSG_NO_SCRIPT "Error opening script file: %s\n"
IDS_ERROR_MSG_NO_SCRIPT "Error opening script: %s\n"
IDS_ERROR_MSG_BAD_ARG "Error processing argument: %s\n"
END

View file

@ -15,12 +15,11 @@ LANGUAGE LANG_ROMANIAN, SUBLANG_NEUTRAL
/* Basic application information */
STRINGTABLE DISCARDABLE
BEGIN
IDS_APP_HEADER, "\nReactOS DiskPart versiune %s\n"
IDS_APP_USAGE, "Sintaxa liniei de comandă pentru Diskpart:\ndiskpart [/s <fișier-script>] [/t <valoare-temporală-limită>] [/?]\n\
/s <fișier-script> - Execută scriptul din fișierul dat.\n\
/t <valoare-temporală-limită> - Așteaptă această perioadă (în secunde)\n\
după execuția unui fișier script.\n\
/? - Afișează acest manual."
IDS_APP_HEADER, "\nReactOS DiskPart\n"
IDS_APP_USAGE, "Disk Partitioning Interpreter.\n\nUsage: DISKPART [/S filename] [/T timeout] [/?]\n\n\
/S filename\tExecută scriptul din fișierul dat.\n\
/T timeout\tTimeout in seconds to prevent diskpart usage overlap.\n\
/?\t\tAfișează acest manual.\n\n"
IDS_APP_LICENSE, "Licențiere în termenii GNU GPLv2\n"
IDS_APP_CURR_COMPUTER, "Pe calculatorul: %s\n\n"
IDS_APP_LEAVING, "\nÎnchiderea DiskPart...\n"
@ -123,6 +122,7 @@ END
STRINGTABLE DISCARDABLE
BEGIN
IDS_ERROR_MSG_NO_SCRIPT "Eroare la deschiderea fișierului script: %s\n"
IDS_ERROR_MSG_BAD_ARG "De prelucrare de eroare argumente: %s\n"
END

View file

@ -88,6 +88,7 @@
#define IDS_HELP_CMD_DESC_UNIQUEID 97
#define IDS_ERROR_MSG_NO_SCRIPT 104
#define IDS_ERROR_MSG_BAD_ARG 98
#define IDS_HELP_CMD_ACTIVE 105
#define IDS_HELP_CMD_ADD 106