diff --git a/lib/server/console.js b/lib/server/console.js index 231312da..657d64e4 100644 --- a/lib/server/console.js +++ b/lib/server/console.js @@ -106,8 +106,8 @@ connName, ret, isVolume = win.isChangeVolume(command), - isCD = command.match(new RegExp('^cd ')), - isCDWin = command.match(new RegExp('^cd ', 'i')), + isCD = command.match(new RegExp('^cd ?')), + isCDWin = command.match(new RegExp('^cd ?', 'i')), symbolsExec = ['*', '&', '{', '}', '|', '\'', '"'], isSymbol = Util.isContainStr(command, symbolsExec), @@ -276,21 +276,35 @@ } function onCD(command, currDir, callback) { - var isChangeVolume = win.isChangeVolume(command), + var CD = 'cd ', + isChangeVolume = win.isChangeVolume(command), isVolume = win.isVolume(command), - paramDir = Util.rmStr(command, 'cd '), - isRootOrHome = equalPart(paramDir, ['/', '~']); + paramDir = Util.rmStrOnce(command, [CD, 'cd']), + isRoot = paramDir === '/', + isHome = paramDir === '~' && !WIN; - if (!isRootOrHome && !isChangeVolume || isVolume) { - command = dropFirstWord(paramDir); + if (isHome) { + paramDir = process.env.HOME; + command = 'cd ' + paramDir; + } + + if (!paramDir && !WIN) + paramDir = '.'; + + if (!isHome && !isRoot && !isChangeVolume || isVolume) { paramDir = getFirstWord(paramDir); + command = Util.rmStrOnce(command, [ + CD, + '\'' + paramDir + '\'', + '"' + paramDir + '"', + ]); paramDir = path.join(currDir, paramDir); - command = 'cd ' + paramDir + command; + command = CD + '"' + paramDir + '" ' + command; } - exec(command, {cwd : paramDir}, function (error, stdout, stderr) { + exec(command, {cwd: paramDir}, function (error, stdout, stderr) { var path = paramDir, errorStr = ''; @@ -310,41 +324,28 @@ } function getFirstWord(str) { - var number, word, - is = Util.isString(str); - - Util.checkArgs(arguments, ['str']); + var word, result, + regExp = /^(.*?)\s/, + regExpQuotes = /^"(.*)"\s?/, + is = Util.isString(str); if (is) { - number = str.indexOf(' '); + result = str.match(regExpQuotes); - if (~number) - word = str.substr(0, number); - else + if (result) { + word = result[1]; + } else { + result = str.match(regExp); + word = result && result[1]; + } + + if (!word) word = str; } return word; } - function dropFirstWord(str) { - var number, - is = Util.isString(str); - - Util.checkArgs(arguments, ['str']); - - if (is) { - number = str.indexOf(' '); - - if (~number) - str = str.substr(number + 1); - else - str = ''; - } - - return str; - } - function log(connNum, str, typeParam) { var ret, type = ' ';