[ENHANCEMENT] Change commandline handling, relative paths are now properly handled.
Fix a small bug when failed open of file would still change the language. git-svn-id: svn://svn.tuxfamily.org/svnroot/notepadplus/repository@184 f5eea248-9336-0410-98b8-ebc06183d4e3
This commit is contained in:
parent
cd2cc80ede
commit
d074522b7b
|
@ -6709,7 +6709,9 @@ LRESULT Notepad_plus::runProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lPa
|
||||||
for (int i = 0 ; i < fnss.size() ; i++)
|
for (int i = 0 ; i < fnss.size() ; i++)
|
||||||
{
|
{
|
||||||
pFn = (char *)fnss.getFileName(i);
|
pFn = (char *)fnss.getFileName(i);
|
||||||
doOpen((const char *)pFn, cmdLineParams._isReadOnly);
|
bool res = doOpen((const char *)pFn, cmdLineParams._isReadOnly);
|
||||||
|
if (!res)
|
||||||
|
continue;
|
||||||
if (lt != L_TXT)
|
if (lt != L_TXT)
|
||||||
{
|
{
|
||||||
_pEditView->setCurrentDocType(lt);
|
_pEditView->setCurrentDocType(lt);
|
||||||
|
|
|
@ -23,113 +23,75 @@
|
||||||
|
|
||||||
//const char localConfFile[] = "doLocalConf.xml";
|
//const char localConfFile[] = "doLocalConf.xml";
|
||||||
|
|
||||||
static bool isInList(const char *token2Find, char *list2Clean) {
|
typedef std::vector<const char*> ParamVector;
|
||||||
char word[1024];
|
void parseCommandLine(char * commandLine, ParamVector & paramVector) {
|
||||||
bool isFileNamePart = false;
|
bool isInFile = false;
|
||||||
|
bool isInWhiteSpace = true;
|
||||||
|
paramVector.clear();
|
||||||
|
size_t commandLength = strlen(commandLine);
|
||||||
|
for(size_t i = 0; i < commandLength; i++) {
|
||||||
|
switch(commandLine[i]) {
|
||||||
|
case '\"': { //quoted filename, ignore any following whitespace
|
||||||
|
if (!isInFile) { //" will always be treated as start or end of param, in case the user forgot to add an space
|
||||||
|
paramVector.push_back(commandLine+i+1); //add next param(since zero terminated string original, no overflow of +1)
|
||||||
|
}
|
||||||
|
isInFile = !isInFile;
|
||||||
|
isInWhiteSpace = false;
|
||||||
|
//because we dont want to leave in any quotes in the filename, remove them now (with zero terminator)
|
||||||
|
commandLine[i] = 0;
|
||||||
|
break; }
|
||||||
|
case '\t': //also treat tab as whitespace
|
||||||
|
case ' ': {
|
||||||
|
isInWhiteSpace = true;
|
||||||
|
if (!isInFile)
|
||||||
|
commandLine[i] = 0; //zap spaces into zero terminators, unless its part of a filename
|
||||||
|
break; }
|
||||||
|
default: { //default char, if beginning of word, add it
|
||||||
|
if (!isInFile && isInWhiteSpace) {
|
||||||
|
paramVector.push_back(commandLine+i); //add next param
|
||||||
|
isInWhiteSpace = false;
|
||||||
|
}
|
||||||
|
break; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//the commandline string is now a list of zero terminated strings concatenated, and the vector contains all the substrings
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0, j = 0 ; i <= int(strlen(list2Clean)) ; i++)
|
bool isInList(const char *token2Find, ParamVector & params) {
|
||||||
|
int nrItems = params.size();
|
||||||
|
|
||||||
|
for (int i = 0; i < nrItems; i++)
|
||||||
{
|
{
|
||||||
if ((list2Clean[i] == ' ') || (list2Clean[i] == '\0'))
|
if (!strcmp(token2Find, params.at(i))) {
|
||||||
{
|
params.erase(params.begin() + i);
|
||||||
if ((j) && (!isFileNamePart))
|
|
||||||
{
|
|
||||||
word[j] = '\0';
|
|
||||||
j = 0;
|
|
||||||
bool bingo = !strcmp(token2Find, word);
|
|
||||||
|
|
||||||
if (bingo)
|
|
||||||
{
|
|
||||||
int wordLen = int(strlen(word));
|
|
||||||
int prevPos = i - wordLen;
|
|
||||||
|
|
||||||
for (i = i + 1 ; i <= int(strlen(list2Clean)) ; i++, prevPos++)
|
|
||||||
list2Clean[prevPos] = list2Clean[i];
|
|
||||||
|
|
||||||
list2Clean[prevPos] = '\0';
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else if (list2Clean[i] == '"')
|
|
||||||
{
|
|
||||||
isFileNamePart = !isFileNamePart;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
word[j++] = list2Clean[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
static string getParamVal(char c, char *list2Clean) {
|
string getParamVal(char c, ParamVector & params) {
|
||||||
char word[1024];
|
int nrItems = params.size();
|
||||||
bool checkDash = true;
|
|
||||||
bool checkCh = false;
|
|
||||||
bool action = false;
|
|
||||||
bool isFileNamePart = false;
|
|
||||||
int pos2Erase = 0;
|
|
||||||
|
|
||||||
for (int i = 0, j = 0 ; i <= int(strlen(list2Clean)) ; i++)
|
for (int i = 0; i < nrItems; i++)
|
||||||
{
|
{
|
||||||
if ((list2Clean[i] == ' ') || (list2Clean[i] == '\0'))
|
const char * token = params.at(i);
|
||||||
{
|
if (token[0] == '-' && strlen(token) >= 2 && token[1] == c) { //dash, and enough chars
|
||||||
if (action)
|
string retval(token+2);
|
||||||
{
|
params.erase(params.begin() + i);
|
||||||
word[j] = '\0';
|
return retval;
|
||||||
j = 0;
|
|
||||||
action = false;
|
|
||||||
|
|
||||||
for (i = i + 1 ; i <= int(strlen(list2Clean)) ; i++, pos2Erase++)
|
|
||||||
list2Clean[pos2Erase] = list2Clean[i];
|
|
||||||
|
|
||||||
list2Clean[pos2Erase] = '\0';
|
|
||||||
|
|
||||||
return word;
|
|
||||||
}
|
}
|
||||||
checkDash = true;
|
|
||||||
}
|
}
|
||||||
else if (list2Clean[i] == '"')
|
return string("");
|
||||||
{
|
|
||||||
isFileNamePart = !isFileNamePart;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isFileNamePart)
|
LangType getLangTypeFromParam(ParamVector & params) {
|
||||||
{
|
string langStr = getParamVal('l', params);
|
||||||
if (action)
|
|
||||||
{
|
|
||||||
word[j++] = list2Clean[i];
|
|
||||||
}
|
|
||||||
else if (checkDash)
|
|
||||||
{
|
|
||||||
if (list2Clean[i] == '-')
|
|
||||||
checkCh = true;
|
|
||||||
|
|
||||||
if (list2Clean[i] != ' ')
|
|
||||||
checkDash = false;
|
|
||||||
}
|
|
||||||
else if (checkCh)
|
|
||||||
{
|
|
||||||
if (list2Clean[i] == c)
|
|
||||||
{
|
|
||||||
action = true;
|
|
||||||
pos2Erase = i-1;
|
|
||||||
}
|
|
||||||
checkCh = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
};
|
|
||||||
|
|
||||||
static LangType getLangTypeFromParam(char *list2Clean) {
|
|
||||||
string langStr = getParamVal('l', list2Clean);
|
|
||||||
return NppParameters::getLangIDFromStr(langStr.c_str());
|
return NppParameters::getLangIDFromStr(langStr.c_str());
|
||||||
};
|
};
|
||||||
|
|
||||||
static int getLn2GoFromParam(char *list2Clean) {
|
int getLn2GoFromParam(ParamVector & params) {
|
||||||
string lineNumStr = getParamVal('n', list2Clean);
|
string lineNumStr = getParamVal('n', params);
|
||||||
return atoi(lineNumStr.c_str());
|
return atoi(lineNumStr.c_str());
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -148,14 +110,17 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpszCmdLine, int nCmdSh
|
||||||
if (::GetLastError() == ERROR_ALREADY_EXISTS)
|
if (::GetLastError() == ERROR_ALREADY_EXISTS)
|
||||||
TheFirstOne = false;
|
TheFirstOne = false;
|
||||||
|
|
||||||
|
ParamVector params;
|
||||||
|
parseCommandLine(lpszCmdLine, params);
|
||||||
|
|
||||||
CmdLineParams cmdLineParams;
|
CmdLineParams cmdLineParams;
|
||||||
bool isMultiInst = isInList(FLAG_MULTI_INSTANCE, lpszCmdLine);
|
bool isMultiInst = isInList(FLAG_MULTI_INSTANCE, params);
|
||||||
cmdLineParams._isNoTab = isInList(FLAG_NOTABBAR, lpszCmdLine);
|
cmdLineParams._isNoTab = isInList(FLAG_NOTABBAR, params);
|
||||||
cmdLineParams._isNoPlugin = isInList(FLAG_NO_PLUGIN, lpszCmdLine);
|
cmdLineParams._isNoPlugin = isInList(FLAG_NO_PLUGIN, params);
|
||||||
cmdLineParams._isReadOnly = isInList(FLAG_READONLY, lpszCmdLine);
|
cmdLineParams._isReadOnly = isInList(FLAG_READONLY, params);
|
||||||
cmdLineParams._isNoSession = isInList(FLAG_NOSESSION, lpszCmdLine);
|
cmdLineParams._isNoSession = isInList(FLAG_NOSESSION, params);
|
||||||
cmdLineParams._langType = getLangTypeFromParam(lpszCmdLine);
|
cmdLineParams._langType = getLangTypeFromParam(params);
|
||||||
cmdLineParams._line2go = getLn2GoFromParam(lpszCmdLine);
|
cmdLineParams._line2go = getLn2GoFromParam(params);
|
||||||
|
|
||||||
NppParameters *pNppParameters = NppParameters::getInstance();
|
NppParameters *pNppParameters = NppParameters::getInstance();
|
||||||
|
|
||||||
|
@ -167,6 +132,25 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpszCmdLine, int nCmdSh
|
||||||
cmdLineParams._isNoSession = true;
|
cmdLineParams._isNoSession = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string quotFileName = "";
|
||||||
|
// tell the running instance the FULL path to the new files to load
|
||||||
|
size_t nrFilesToOpen = params.size();
|
||||||
|
const char * currentFile;
|
||||||
|
char fullFileName[MAX_PATH];
|
||||||
|
for(size_t i = 0; i < nrFilesToOpen; i++) {
|
||||||
|
currentFile = params.at(i);
|
||||||
|
//check if relative or full path. Relative paths dont have a colon for driveletter
|
||||||
|
BOOL isRelative = ::PathIsRelative(currentFile);
|
||||||
|
quotFileName += "\"";
|
||||||
|
if (isRelative) {
|
||||||
|
::GetFullPathName(currentFile, MAX_PATH, fullFileName, NULL);
|
||||||
|
quotFileName += fullFileName;
|
||||||
|
} else {
|
||||||
|
quotFileName += currentFile;
|
||||||
|
}
|
||||||
|
quotFileName += "\"";
|
||||||
|
}
|
||||||
|
|
||||||
if ((!isMultiInst) && (!TheFirstOne))
|
if ((!isMultiInst) && (!TheFirstOne))
|
||||||
{
|
{
|
||||||
HWND hNotepad_plus = ::FindWindow(Notepad_plus::getClassName(), NULL);
|
HWND hNotepad_plus = ::FindWindow(Notepad_plus::getClassName(), NULL);
|
||||||
|
@ -190,33 +174,17 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpszCmdLine, int nCmdSh
|
||||||
|
|
||||||
::SetForegroundWindow(hNotepad_plus);
|
::SetForegroundWindow(hNotepad_plus);
|
||||||
|
|
||||||
if (lpszCmdLine[0])
|
if (params.size() > 0) //if there are files to open, use the WM_COPYDATA system
|
||||||
{
|
{
|
||||||
COPYDATASTRUCT paramData;
|
COPYDATASTRUCT paramData;
|
||||||
paramData.dwData = COPYDATA_PARAMS;
|
paramData.dwData = COPYDATA_PARAMS;
|
||||||
|
paramData.lpData = &cmdLineParams;
|
||||||
|
paramData.cbData = sizeof(cmdLineParams);
|
||||||
|
|
||||||
COPYDATASTRUCT fileNamesData;
|
COPYDATASTRUCT fileNamesData;
|
||||||
fileNamesData.dwData = COPYDATA_FILENAMES;
|
fileNamesData.dwData = COPYDATA_FILENAMES;
|
||||||
|
|
||||||
string quotFileName = "\"";
|
|
||||||
// tell the other running instance the FULL path to the new file to load
|
|
||||||
if (lpszCmdLine[0] == '"')
|
|
||||||
{
|
|
||||||
fileNamesData.lpData = (void *)lpszCmdLine;
|
|
||||||
fileNamesData.cbData = long(strlen(lpszCmdLine) + 1);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
char longFileName[MAX_PATH];
|
|
||||||
::GetFullPathName(lpszCmdLine, MAX_PATH, longFileName, NULL);
|
|
||||||
quotFileName += longFileName;
|
|
||||||
quotFileName += "\"";
|
|
||||||
|
|
||||||
fileNamesData.lpData = (void *)quotFileName.c_str();
|
fileNamesData.lpData = (void *)quotFileName.c_str();
|
||||||
fileNamesData.cbData = long(quotFileName.length() + 1);
|
fileNamesData.cbData = long(quotFileName.length() + 1);
|
||||||
}
|
|
||||||
paramData.lpData = &cmdLineParams;
|
|
||||||
paramData.cbData = sizeof(cmdLineParams);
|
|
||||||
|
|
||||||
::SendMessage(hNotepad_plus, WM_COPYDATA, (WPARAM)hInstance, (LPARAM)¶mData);
|
::SendMessage(hNotepad_plus, WM_COPYDATA, (WPARAM)hInstance, (LPARAM)¶mData);
|
||||||
::SendMessage(hNotepad_plus, WM_COPYDATA, (WPARAM)hInstance, (LPARAM)&fileNamesData);
|
::SendMessage(hNotepad_plus, WM_COPYDATA, (WPARAM)hInstance, (LPARAM)&fileNamesData);
|
||||||
|
@ -258,12 +226,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpszCmdLine, int nCmdSh
|
||||||
MSG msg;
|
MSG msg;
|
||||||
msg.wParam = 0;
|
msg.wParam = 0;
|
||||||
try {
|
try {
|
||||||
char *pPathNames = NULL;
|
notepad_plus_plus.init(hInstance, NULL, quotFileName.c_str(), &cmdLineParams);
|
||||||
if (lpszCmdLine[0])
|
|
||||||
{
|
|
||||||
pPathNames = lpszCmdLine;
|
|
||||||
}
|
|
||||||
notepad_plus_plus.init(hInstance, NULL, pPathNames, &cmdLineParams);
|
|
||||||
|
|
||||||
bool unicodeSupported = notepad_plus_plus.getWinVersion() >= WV_NT;
|
bool unicodeSupported = notepad_plus_plus.getWinVersion() >= WV_NT;
|
||||||
bool going = true;
|
bool going = true;
|
||||||
|
|
Loading…
Reference in New Issue