This is the second part of cmd's engine rewrite. It mainly contains code cleanup and simplifications.
From: Eric Pouech epouech@codeweavers.com
Signed-off-by: Eric Pouech epouech@codeweavers.com --- programs/cmd/wcmdmain.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)
diff --git a/programs/cmd/wcmdmain.c b/programs/cmd/wcmdmain.c index eb66b29836e..ffd63d9771a 100644 --- a/programs/cmd/wcmdmain.c +++ b/programs/cmd/wcmdmain.c @@ -1319,11 +1319,10 @@ void WCMD_execute (const WCHAR *command, const WCHAR *redirects, while (IsCharAlphaNumericW(whichcmd[count])) { count++; } - for (i=0; i<=WCMD_EXIT; i++) { + for (cmd_index=0; cmd_index<=WCMD_EXIT; cmd_index++) { if (CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE | SORT_STRINGSORT, - whichcmd, count, inbuilt[i], -1) == CSTR_EQUAL) break; + whichcmd, count, inbuilt[cmd_index], -1) == CSTR_EQUAL) break; } - cmd_index = i; parms_start = WCMD_skip_leading_spaces (&whichcmd[count]);
/* If the next command is a pipe then we implement pipes by redirecting @@ -1491,15 +1490,15 @@ void WCMD_execute (const WCHAR *command, const WCHAR *redirects, WCMD_parse (parms_start, quals, param1, param2); WINE_TRACE("param1: %s, param2: %s\n", wine_dbgstr_w(param1), wine_dbgstr_w(param2));
- if (i <= WCMD_EXIT && (parms_start[0] == '/') && (parms_start[1] == '?')) { + if (cmd_index <= WCMD_EXIT && (parms_start[0] == '/') && (parms_start[1] == '?')) { /* this is a help request for a builtin program */ - i = WCMD_HELP; + cmd_index = WCMD_HELP; memcpy(parms_start, whichcmd, count * sizeof(WCHAR)); parms_start[count] = '\0';
}
- switch (i) { + switch (cmd_index) {
case WCMD_CALL: WCMD_call (parms_start); @@ -1633,8 +1632,8 @@ void WCMD_execute (const WCHAR *command, const WCHAR *redirects, these two commands, neither 'for' nor 'if' is supported when called, i.e. 'call if 1==1...' will fail. */ if (!retrycall) { - if (i==WCMD_FOR) WCMD_for (parms_start, cmdList); - else if (i==WCMD_IF) WCMD_if (parms_start, cmdList); + if (cmd_index==WCMD_FOR) WCMD_for (parms_start, cmdList); + else if (cmd_index==WCMD_IF) WCMD_if (parms_start, cmdList); break; } /* else: drop through */
From: Eric Pouech epouech@codeweavers.com
Signed-off-by: Eric Pouech epouech@codeweavers.com --- programs/cmd/wcmdmain.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-)
diff --git a/programs/cmd/wcmdmain.c b/programs/cmd/wcmdmain.c index ffd63d9771a..059920c9608 100644 --- a/programs/cmd/wcmdmain.c +++ b/programs/cmd/wcmdmain.c @@ -1699,7 +1699,7 @@ static void WCMD_addCommand(WCHAR *command, int *commandLen, WCHAR *redirs, int *redirLen, WCHAR **copyTo, int **copyToLen, CMD_DELIMITERS prevDelim, int curDepth, - CMD_LIST **lastEntry, CMD_LIST **output) { + CMD_LIST **output) {
CMD_LIST *thisEntry = NULL;
@@ -1734,12 +1734,9 @@ static void WCMD_addCommand(WCHAR *command, int *commandLen, thisEntry->nextcommand = NULL; thisEntry->prevDelim = prevDelim; thisEntry->bracketDepth = curDepth; - if (*lastEntry) { - (*lastEntry)->nextcommand = thisEntry; - } else { - *output = thisEntry; - } - *lastEntry = thisEntry; + + for (; *output; output = &((*output)->nextcommand)) {} + *output = thisEntry; }
@@ -1822,7 +1819,6 @@ WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, HANDLE WCHAR *curCopyTo; int *curLen; int curDepth = 0; - CMD_LIST *lastEntry = NULL; CMD_DELIMITERS prevDelim = CMD_NONE; static WCHAR *extraSpace = NULL; /* Deliberately never freed */ BOOL inOneLine = FALSE; @@ -1842,6 +1838,7 @@ WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, HANDLE int lineCurDepth; /* Bracket depth when line was read in */ BOOL resetAtEndOfLine = FALSE; /* Do we need to reset curdepth at EOL */
+ *output = NULL; /* Allocate working space for a command read from keyboard, file etc */ if (!extraSpace) extraSpace = xalloc((MAXSTRING + 1) * sizeof(WCHAR)); @@ -2093,7 +2090,7 @@ WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, HANDLE curRedirs, &curRedirsLen, &curCopyTo, &curLen, prevDelim, curDepth, - &lastEntry, output); + output);
}
@@ -2167,7 +2164,7 @@ WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, HANDLE curRedirs, &curRedirsLen, &curCopyTo, &curLen, prevDelim, curDepth, - &lastEntry, output); + output);
curDepth++; } else { @@ -2198,7 +2195,7 @@ WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, HANDLE curRedirs, &curRedirsLen, &curCopyTo, &curLen, prevDelim, curDepth, - &lastEntry, output); + output);
}
@@ -2231,7 +2228,7 @@ WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, HANDLE curRedirs, &curRedirsLen, &curCopyTo, &curLen, prevDelim, curDepth, - &lastEntry, output); + output); }
/* Add an empty entry to the command list */ @@ -2240,7 +2237,7 @@ WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, HANDLE curRedirs, &curRedirsLen, &curCopyTo, &curLen, prevDelim, curDepth, - &lastEntry, output); + output); curDepth--;
/* Leave inIn if necessary */ @@ -2276,7 +2273,7 @@ WCHAR *WCMD_ReadAndParseLine(const WCHAR *optionalcmd, CMD_LIST **output, HANDLE curRedirs, &curRedirsLen, &curCopyTo, &curLen, prevDelim, curDepth, - &lastEntry, output); + output);
/* If we had a single line if or else, and we pretended to add brackets, end them now */
From: Eric Pouech epouech@codeweavers.com
Signed-off-by: Eric Pouech epouech@codeweavers.com --- programs/cmd/directory.c | 8 +------- programs/cmd/wcmd.h | 7 ++++++- programs/cmd/wcmdmain.c | 6 +++--- 3 files changed, 10 insertions(+), 11 deletions(-)
diff --git a/programs/cmd/directory.c b/programs/cmd/directory.c index 709bbb52287..afec5f6ed75 100644 --- a/programs/cmd/directory.c +++ b/programs/cmd/directory.c @@ -271,13 +271,7 @@ static DIRECTORY_STACK *WCMD_list_directory (DIRECTORY_STACK *inputparms, int le if (tmpLen > widest) widest = tmpLen; }
- fd = realloc(fd, (entry_count + 1) * sizeof(WIN32_FIND_DATAW)); - if (fd == NULL) { - FindClose (hff); - WINE_ERR("Out of memory\n"); - errorlevel = 1; - return parms->next; - } + fd = xrealloc(fd, (entry_count + 1) * sizeof(WIN32_FIND_DATAW)); } while (FindNextFileW(hff, &fd[entry_count]) != 0); FindClose (hff); } diff --git a/programs/cmd/wcmd.h b/programs/cmd/wcmd.h index 1935bbcdcd2..72e5ef1ad36 100644 --- a/programs/cmd/wcmd.h +++ b/programs/cmd/wcmd.h @@ -124,7 +124,12 @@ void WCMD_free_commands(CMD_LIST *cmds); void WCMD_execute (const WCHAR *orig_command, const WCHAR *redirects, CMD_LIST **cmdList, BOOL retrycall);
-void *xalloc(size_t) __WINE_ALLOC_SIZE(1) __WINE_DEALLOC(free) __WINE_MALLOC; +void *xrealloc(void *, size_t) __WINE_ALLOC_SIZE(2) __WINE_DEALLOC(free) __WINE_MALLOC; + +static inline void *xalloc(size_t sz) +{ + return xrealloc(NULL, sz); +}
static inline WCHAR *xstrdupW(const WCHAR *str) { diff --git a/programs/cmd/wcmdmain.c b/programs/cmd/wcmdmain.c index 059920c9608..128610b0800 100644 --- a/programs/cmd/wcmdmain.c +++ b/programs/cmd/wcmdmain.c @@ -423,12 +423,12 @@ static void WCMD_show_prompt (BOOL newLine) { WCMD_output_asis (out_string); }
-void *xalloc(size_t size) +void *xrealloc(void *ptr, size_t size) { void *ret;
- ret = malloc(size); - if(!ret) { + if (!(ret = realloc(ptr, size))) + { ERR("Out of memory\n"); ExitProcess(1); }
From: Eric Pouech epouech@codeweavers.com
Signed-off-by: Eric Pouech epouech@codeweavers.com --- programs/cmd/builtins.c | 69 ++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 39 deletions(-)
diff --git a/programs/cmd/builtins.c b/programs/cmd/builtins.c index ca703af52ec..779df2b65b0 100644 --- a/programs/cmd/builtins.c +++ b/programs/cmd/builtins.c @@ -2062,47 +2062,38 @@ static void WCMD_parse_line(CMD_LIST *cmdStart, * * Returns a file handle which can be used to read the input lines from. */ -static FILE* WCMD_forf_getinput(BOOL usebackq, WCHAR *itemstr, BOOL iscmd) { - WCHAR temp_str[MAX_PATH]; - WCHAR temp_file[MAX_PATH]; - WCHAR temp_cmd[MAXSTRING]; - WCHAR *trimmed = NULL; - FILE* ret; - - /* Remove leading and trailing character (but there may be trailing whitespace too) */ - if ((iscmd && (itemstr[0] == '`' && usebackq)) || - (iscmd && (itemstr[0] == ''' && !usebackq)) || - (!iscmd && (itemstr[0] == '"' && usebackq))) - { - trimmed = WCMD_strtrim(itemstr); - if (trimmed) { - itemstr = trimmed; - } - itemstr[lstrlenW(itemstr)-1] = 0x00; - itemstr++; - } - - if (iscmd) { - /* Get temp filename */ - GetTempPathW(ARRAY_SIZE(temp_str), temp_str); - GetTempFileNameW(temp_str, L"CMD", 0, temp_file); +static FILE* WCMD_forf_getinput(BOOL usebackq, WCHAR *itemstr, BOOL iscmd) +{ + WCHAR *trimmed = NULL; + FILE* ret;
- /* Redirect output to the temporary file */ - wsprintfW(temp_str, L">%s", temp_file); - wsprintfW(temp_cmd, L"CMD.EXE /C %s", itemstr); - WINE_TRACE("Issuing '%s' with redirs '%s'\n", - wine_dbgstr_w(temp_cmd), wine_dbgstr_w(temp_str)); - WCMD_execute (temp_cmd, temp_str, NULL, FALSE); + /* Remove leading and trailing character (but there may be trailing whitespace too) */ + if ((iscmd && (itemstr[0] == '`' && usebackq)) || + (iscmd && (itemstr[0] == ''' && !usebackq)) || + (!iscmd && (itemstr[0] == '"' && usebackq))) + { + trimmed = WCMD_strtrim(itemstr); + if (trimmed) + itemstr = trimmed; + itemstr[lstrlenW(itemstr)-1] = 0x00; + itemstr++; + }
- /* Open the file, read line by line and process */ - ret = _wfopen(temp_file, L"rt,ccs=unicode"); - } else { - /* Open the file, read line by line and process */ - WINE_TRACE("Reading input to parse from '%s'\n", wine_dbgstr_w(itemstr)); - ret = _wfopen(itemstr, L"rt,ccs=unicode"); - } - free(trimmed); - return ret; + if (iscmd) + { + WCHAR temp_cmd[MAXSTRING]; + wsprintfW(temp_cmd, L"CMD.EXE /C %s", itemstr); + WINE_TRACE("Reading output of '%s'\n", wine_dbgstr_w(temp_cmd)); + ret = _wpopen(temp_cmd, L"rt,ccs=unicode"); + } + else + { + /* Open the file, read line by line and process */ + WINE_TRACE("Reading input to parse from '%s'\n", wine_dbgstr_w(itemstr)); + ret = _wfopen(itemstr, L"rt,ccs=unicode"); + } + free(trimmed); + return ret; }
/**************************************************************************