Author: tfaber Date: Tue Jul 24 20:23:24 2012 New Revision: 56966
URL: http://svn.reactos.org/svn/reactos?rev=56966&view=rev Log: [DISKPART] - Rewrite argument parsing. Patch by Lee Schroeder. See issue #7170 for more details.
Modified: trunk/reactos/base/system/diskpart/diskpart.c trunk/reactos/base/system/diskpart/diskpart.h trunk/reactos/base/system/diskpart/interpreter.c trunk/reactos/base/system/diskpart/lang/en-US.rc trunk/reactos/base/system/diskpart/lang/ro-RO.rc trunk/reactos/base/system/diskpart/resource.h
Modified: trunk/reactos/base/system/diskpart/diskpart.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/base/system/diskpart/diskpa... ============================================================================== --- trunk/reactos/base/system/diskpart/diskpart.c [iso-8859-1] (original) +++ trunk/reactos/base/system/diskpart/diskpart.c [iso-8859-1] Tue Jul 24 20:23:24 2012 @@ -24,21 +24,38 @@ 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 @@ }
/* 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 @@ */ 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"); + /* If there are no command arguments, then go straight to the interpreter */ + if (argc < 2) + { + ShowHeader(); + InterpretMain(); + } + /* 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 there is no flag, then return an error */ + PrintResourceString(IDS_ERROR_MSG_BAD_ARG, argv[index]); + return EXIT_FAILURE; + }
- /* 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'/')) - { - 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]; - } - } - else if (wcsicmp(&argv[i][1], L"t") == 0) - { - /* - * 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]); - } - } - 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]); + + /* 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 */ - if (file_name != NULL) - { - if (run_script(file_name) == FALSE) + /* 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 */
Modified: trunk/reactos/base/system/diskpart/diskpart.h URL: http://svn.reactos.org/svn/reactos/trunk/reactos/base/system/diskpart/diskpa... ============================================================================== --- trunk/reactos/base/system/diskpart/diskpart.h [iso-8859-1] (original) +++ trunk/reactos/base/system/diskpart/diskpart.h [iso-8859-1] Tue Jul 24 20:23:24 2012 @@ -138,9 +138,9 @@ 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);
Modified: trunk/reactos/base/system/diskpart/interpreter.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/base/system/diskpart/interp... ============================================================================== --- trunk/reactos/base/system/diskpart/interpreter.c [iso-8859-1] (original) +++ trunk/reactos/base/system/diskpart/interpreter.c [iso-8859-1] Tue Jul 24 20:23:24 2012 @@ -73,12 +73,12 @@
/* - * 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_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 @@ }
/* 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 @@ }
/* sends the string to find the command */ - bRun = interpret_cmd(args_count, args_vector); + bRun = InterpretCmd(args_count, args_vector); } }
Modified: trunk/reactos/base/system/diskpart/lang/en-US.rc URL: http://svn.reactos.org/svn/reactos/trunk/reactos/base/system/diskpart/lang/e... ============================================================================== --- trunk/reactos/base/system/diskpart/lang/en-US.rc [iso-8859-1] (original) +++ trunk/reactos/base/system/diskpart/lang/en-US.rc [iso-8859-1] Tue Jul 24 20:23:24 2012 @@ -13,12 +13,12 @@ /* 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 @@ /* 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
Modified: trunk/reactos/base/system/diskpart/lang/ro-RO.rc URL: http://svn.reactos.org/svn/reactos/trunk/reactos/base/system/diskpart/lang/r... ============================================================================== --- trunk/reactos/base/system/diskpart/lang/ro-RO.rc [iso-8859-1] (original) +++ trunk/reactos/base/system/diskpart/lang/ro-RO.rc [iso-8859-1] Tue Jul 24 20:23:24 2012 @@ -15,12 +15,11 @@ /* 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 @@ 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
Modified: trunk/reactos/base/system/diskpart/resource.h URL: http://svn.reactos.org/svn/reactos/trunk/reactos/base/system/diskpart/resour... ============================================================================== --- trunk/reactos/base/system/diskpart/resource.h [iso-8859-1] (original) +++ trunk/reactos/base/system/diskpart/resource.h [iso-8859-1] Tue Jul 24 20:23:24 2012 @@ -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