mirror of
https://github.com/reactos/reactos.git
synced 2025-06-20 07:36:05 +00:00
[DISKPART]
- Rewrite argument parsing. Patch by Lee Schroeder. See issue #7170 for more details. svn path=/trunk/; revision=56966
This commit is contained in:
parent
caa3183c46
commit
cbe2bd44d0
6 changed files with 127 additions and 91 deletions
|
@ -24,21 +24,38 @@ PrintResourceString(INT resID, ...)
|
||||||
va_end(arg_ptr);
|
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
|
* opens the file, reads the contents, convert the text into readable
|
||||||
* code for the computer, and then execute commands in order.
|
* code for the computer, and then execute commands in order.
|
||||||
*/
|
*/
|
||||||
BOOL
|
BOOL
|
||||||
run_script(LPCWSTR filename)
|
RunScript(LPCWSTR filename)
|
||||||
{
|
{
|
||||||
FILE *script_file;
|
FILE *script;
|
||||||
WCHAR tmp_string[MAX_STRING_SIZE];
|
WCHAR tmp_string[MAX_STRING_SIZE];
|
||||||
|
|
||||||
/* Open the file for processing */
|
/* Open the file for processing */
|
||||||
script_file = _wfopen(filename, L"r");
|
script = _wfopen(filename, L"r");
|
||||||
if (script_file == NULL)
|
if (script == NULL)
|
||||||
{
|
{
|
||||||
/* if there was problems opening the file */
|
/* if there was problems opening the file */
|
||||||
PrintResourceString(IDS_ERROR_MSG_NO_SCRIPT, filename);
|
PrintResourceString(IDS_ERROR_MSG_NO_SCRIPT, filename);
|
||||||
|
@ -46,14 +63,14 @@ run_script(LPCWSTR filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Read and process the script */
|
/* 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;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Close the file */
|
/* Close the file */
|
||||||
fclose(script_file);
|
fclose(script);
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
@ -64,79 +81,96 @@ run_script(LPCWSTR filename)
|
||||||
*/
|
*/
|
||||||
int wmain(int argc, const WCHAR *argv[])
|
int wmain(int argc, const WCHAR *argv[])
|
||||||
{
|
{
|
||||||
WCHAR szComputerName[MAX_STRING_SIZE];
|
LPCWSTR script = NULL;
|
||||||
DWORD comp_size = MAX_STRING_SIZE;
|
LPCWSTR tmpBuffer = NULL;
|
||||||
LPCWSTR file_name = NULL;
|
int index, timeout;
|
||||||
int i;
|
|
||||||
int timeout = 0;
|
|
||||||
|
|
||||||
/* Get the name of the computer for us and change the value of comp_name */
|
/* Sets the timeout value to 0 just in case the user doesn't
|
||||||
GetComputerName(szComputerName, &comp_size);
|
specify a value. */
|
||||||
|
timeout = 0;
|
||||||
|
|
||||||
/* TODO: Remove this section of code when program becomes stable enough for production use. */
|
/* If there are no command arguments, then go straight to the interpreter */
|
||||||
wprintf(L"\n*WARNING*: This program is incomplete and may not work properly.\n");
|
if (argc < 2)
|
||||||
|
|
||||||
/* 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 ((argv[i][0] == L'-') || (argv[i][0] == L'/'))
|
ShowHeader();
|
||||||
{
|
InterpretMain();
|
||||||
if (wcsicmp(&argv[i][1], L"s") == 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];
|
|
||||||
}
|
}
|
||||||
|
/* If there are command arguments, then process them */
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (index = 1; index < argc; index++)
|
||||||
|
{
|
||||||
|
/* checks for flags */
|
||||||
|
if ((argv[index][0] == '/')||
|
||||||
|
(argv[index][0] == '-'))
|
||||||
|
{
|
||||||
|
tmpBuffer = argv[index] + 1;
|
||||||
}
|
}
|
||||||
else if (wcsicmp(&argv[i][1], L"t") == 0)
|
else
|
||||||
{
|
{
|
||||||
/*
|
/* If there is no flag, then return an error */
|
||||||
* Get the timeout value only if there is at least one more
|
PrintResourceString(IDS_ERROR_MSG_BAD_ARG, argv[index]);
|
||||||
* argument and it is not another option
|
return EXIT_FAILURE;
|
||||||
*/
|
}
|
||||||
if ((i + 1 < argc) &&
|
|
||||||
(argv[i + 1][0] != L'-') &&
|
|
||||||
(argv[i + 1][0] != L'/'))
|
|
||||||
{
|
|
||||||
/* Next argument */
|
|
||||||
i++;
|
|
||||||
|
|
||||||
/* Get the timeout value */
|
/* Checks for the /? flag first since the program
|
||||||
timeout = _wtoi(argv[i]);
|
exits as soon as the usage list is shown. */
|
||||||
}
|
if (_wcsicmp(tmpBuffer, L"?") == 0)
|
||||||
}
|
|
||||||
else if (wcscmp(&argv[i][1], L"?") == 0)
|
|
||||||
{
|
{
|
||||||
PrintResourceString(IDS_APP_USAGE);
|
PrintResourceString(IDS_APP_USAGE);
|
||||||
return EXIT_SUCCESS;
|
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]);
|
||||||
|
|
||||||
|
/* 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Run the script if we got a script name or call the interpreter otherwise */
|
/* Shows the program information */
|
||||||
if (file_name != NULL)
|
ShowHeader();
|
||||||
|
|
||||||
|
/* Now we process the filename if it exists */
|
||||||
|
if (script != NULL)
|
||||||
{
|
{
|
||||||
if (run_script(file_name) == FALSE)
|
/* 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;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
interpret_main();
|
/* Exit failure since the user wanted to run a script */
|
||||||
|
PrintResourceString(IDS_ERROR_MSG_NO_SCRIPT, script);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Let the user know the program is exiting */
|
/* Let the user know the program is exiting */
|
||||||
|
|
|
@ -138,9 +138,9 @@ BOOL inactive_main(INT argc, WCHAR **argv);
|
||||||
VOID help_inactive(INT argc, WCHAR **argv);
|
VOID help_inactive(INT argc, WCHAR **argv);
|
||||||
|
|
||||||
/* interpreter.c */
|
/* interpreter.c */
|
||||||
BOOL interpret_script(WCHAR *line);
|
BOOL InterpretScript(WCHAR *line);
|
||||||
BOOL interpret_cmd(INT argc, WCHAR **argv);
|
BOOL InterpretCmd(INT argc, WCHAR **argv);
|
||||||
VOID interpret_main(VOID);
|
VOID InterpretMain(VOID);
|
||||||
|
|
||||||
/* list.c */
|
/* list.c */
|
||||||
BOOL list_main(INT argc, WCHAR **argv);
|
BOOL list_main(INT argc, WCHAR **argv);
|
||||||
|
|
|
@ -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
|
* compares the command name to a list of available commands, and
|
||||||
* determines which function to envoke.
|
* determines which function to envoke.
|
||||||
*/
|
*/
|
||||||
BOOL
|
BOOL
|
||||||
interpret_cmd(int argc, WCHAR **argv)
|
InterpretCmd(int argc, WCHAR **argv)
|
||||||
{
|
{
|
||||||
PCOMMAND cmdptr;
|
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.
|
* The main function used for when reading commands from scripts.
|
||||||
*/
|
*/
|
||||||
BOOL
|
BOOL
|
||||||
interpret_script(WCHAR *input_line)
|
InterpretScript(WCHAR *input_line)
|
||||||
{
|
{
|
||||||
WCHAR *args_vector[MAX_ARGS_COUNT];
|
WCHAR *args_vector[MAX_ARGS_COUNT];
|
||||||
INT args_count = 0;
|
INT args_count = 0;
|
||||||
|
@ -134,18 +134,18 @@ interpret_script(WCHAR *input_line)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* sends the string to find the command */
|
/* 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
|
* Contents for the main program loop as it reads each line, and then
|
||||||
* it sends the string to interpret_line, where it determines what
|
* it sends the string to interpret_line, where it determines what
|
||||||
* command to use.
|
* command to use.
|
||||||
*/
|
*/
|
||||||
VOID
|
VOID
|
||||||
interpret_main(VOID)
|
InterpretMain(VOID)
|
||||||
{
|
{
|
||||||
WCHAR input_line[MAX_STRING_SIZE];
|
WCHAR input_line[MAX_STRING_SIZE];
|
||||||
WCHAR *args_vector[MAX_ARGS_COUNT];
|
WCHAR *args_vector[MAX_ARGS_COUNT];
|
||||||
|
@ -187,6 +187,6 @@ interpret_main(VOID)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* sends the string to find the command */
|
/* sends the string to find the command */
|
||||||
bRun = interpret_cmd(args_count, args_vector);
|
bRun = InterpretCmd(args_count, args_vector);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,12 +13,12 @@ LANGUAGE LANG_ENGLISH, SUBLANG_DEFAULT
|
||||||
/* Basic application information */
|
/* Basic application information */
|
||||||
STRINGTABLE DISCARDABLE
|
STRINGTABLE DISCARDABLE
|
||||||
BEGIN
|
BEGIN
|
||||||
IDS_APP_HEADER, "\nReactOS DiskPart version %s\n"
|
IDS_APP_HEADER, "\nReactOS DiskPart\n"
|
||||||
IDS_APP_USAGE, "Diskpart command line syntax:\ndiskpart [/s <script file>] [/t <timeout value>] [/?]\n\
|
IDS_APP_USAGE, "\nDisk Partitioning Interpreter.\n\n\
|
||||||
/s <script file> - Runs the given script file.\n\
|
Usage: DISKPART [/S filename] [/T timeout] [/?]\n\n\
|
||||||
/t <timeout value> - Waits for the given time (in seconds) after running a\n\
|
/S filename\tRuns the given script.\n\
|
||||||
script file.\n\
|
/T timeout\tTimeout in seconds to prevent diskpart usage overlap.\n\
|
||||||
/? - Shows this help text."
|
/?\t\tDisplay this help message.\n\n"
|
||||||
IDS_APP_LICENSE, "Licensed under the GNU GPLv2\n"
|
IDS_APP_LICENSE, "Licensed under the GNU GPLv2\n"
|
||||||
IDS_APP_CURR_COMPUTER, "On computer: %s\n\n"
|
IDS_APP_CURR_COMPUTER, "On computer: %s\n\n"
|
||||||
IDS_APP_LEAVING, "\nLeaving DiskPart...\n"
|
IDS_APP_LEAVING, "\nLeaving DiskPart...\n"
|
||||||
|
@ -120,7 +120,8 @@ END
|
||||||
/* Common Error Messages */
|
/* Common Error Messages */
|
||||||
STRINGTABLE DISCARDABLE
|
STRINGTABLE DISCARDABLE
|
||||||
BEGIN
|
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
|
END
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -15,12 +15,11 @@ LANGUAGE LANG_ROMANIAN, SUBLANG_NEUTRAL
|
||||||
/* Basic application information */
|
/* Basic application information */
|
||||||
STRINGTABLE DISCARDABLE
|
STRINGTABLE DISCARDABLE
|
||||||
BEGIN
|
BEGIN
|
||||||
IDS_APP_HEADER, "\nReactOS DiskPart versiune %s\n"
|
IDS_APP_HEADER, "\nReactOS DiskPart\n"
|
||||||
IDS_APP_USAGE, "Sintaxa liniei de comandă pentru Diskpart:\ndiskpart [/s <fișier-script>] [/t <valoare-temporală-limită>] [/?]\n\
|
IDS_APP_USAGE, "Disk Partitioning Interpreter.\n\nUsage: DISKPART [/S filename] [/T timeout] [/?]\n\n\
|
||||||
/s <fișier-script> - Execută scriptul din fișierul dat.\n\
|
/S filename\tExecută scriptul din fișierul dat.\n\
|
||||||
/t <valoare-temporală-limită> - Așteaptă această perioadă (în secunde)\n\
|
/T timeout\tTimeout in seconds to prevent diskpart usage overlap.\n\
|
||||||
după execuția unui fișier script.\n\
|
/?\t\tAfișează acest manual.\n\n"
|
||||||
/? - Afișează acest manual."
|
|
||||||
IDS_APP_LICENSE, "Licențiere în termenii GNU GPLv2\n"
|
IDS_APP_LICENSE, "Licențiere în termenii GNU GPLv2\n"
|
||||||
IDS_APP_CURR_COMPUTER, "Pe calculatorul: %s\n\n"
|
IDS_APP_CURR_COMPUTER, "Pe calculatorul: %s\n\n"
|
||||||
IDS_APP_LEAVING, "\nÎnchiderea DiskPart...\n"
|
IDS_APP_LEAVING, "\nÎnchiderea DiskPart...\n"
|
||||||
|
@ -123,6 +122,7 @@ END
|
||||||
STRINGTABLE DISCARDABLE
|
STRINGTABLE DISCARDABLE
|
||||||
BEGIN
|
BEGIN
|
||||||
IDS_ERROR_MSG_NO_SCRIPT "Eroare la deschiderea fișierului script: %s\n"
|
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
|
END
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -88,6 +88,7 @@
|
||||||
#define IDS_HELP_CMD_DESC_UNIQUEID 97
|
#define IDS_HELP_CMD_DESC_UNIQUEID 97
|
||||||
|
|
||||||
#define IDS_ERROR_MSG_NO_SCRIPT 104
|
#define IDS_ERROR_MSG_NO_SCRIPT 104
|
||||||
|
#define IDS_ERROR_MSG_BAD_ARG 98
|
||||||
|
|
||||||
#define IDS_HELP_CMD_ACTIVE 105
|
#define IDS_HELP_CMD_ACTIVE 105
|
||||||
#define IDS_HELP_CMD_ADD 106
|
#define IDS_HELP_CMD_ADD 106
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue