Skip to content

Commit

Permalink
Fixed 3rd argument of lua_pcall function.
Browse files Browse the repository at this point in the history
  • Loading branch information
nathancorvussolis committed Jan 3, 2020
1 parent 79c03b2 commit 0b32212
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 87 deletions.
15 changes: 10 additions & 5 deletions imcrvmgr/SearchDictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ int lua_search_skk_dictionary(lua_State *lua)
{
std::wstring candidate;

if (lua_isstring(lua, 1) && lua_isstring(lua, 2))
if (lua_isstring(lua, 1) &&
lua_isstring(lua, 2))
{
std::wstring searchkey = U8TOWC(lua_tostring(lua, 1));
std::wstring okurikey = U8TOWC(lua_tostring(lua, 2));
Expand All @@ -343,7 +344,8 @@ int lua_search_user_dictionary(lua_State *lua)
{
std::wstring candidate;

if (lua_isstring(lua, 1) && lua_isstring(lua, 2))
if (lua_isstring(lua, 1) &&
lua_isstring(lua, 2))
{
std::wstring searchkey = U8TOWC(lua_tostring(lua, 1));
std::wstring okurikey = U8TOWC(lua_tostring(lua, 2));
Expand Down Expand Up @@ -476,8 +478,10 @@ int lua_complement(lua_State *lua)
int lua_add(lua_State *lua)
{
if (lua_isboolean(lua, 1) &&
lua_isstring(lua, 2) && lua_isstring(lua, 3) &&
lua_isstring(lua, 4) && lua_isstring(lua, 5))
lua_isstring(lua, 2) &&
lua_isstring(lua, 3) &&
lua_isstring(lua, 4) &&
lua_isstring(lua, 5))
{
int okuriari = lua_toboolean(lua, 1);
WCHAR command = (okuriari ? REQ_USER_ADD_A : REQ_USER_ADD_N);
Expand All @@ -495,7 +499,8 @@ int lua_add(lua_State *lua)
int lua_delete(lua_State *lua)
{
if (lua_isboolean(lua, 1) &&
lua_isstring(lua, 2) && lua_isstring(lua, 3))
lua_isstring(lua, 2) &&
lua_isstring(lua, 3))
{
int okuriari = lua_toboolean(lua, 1);
WCHAR command = (okuriari ? REQ_USER_DEL_A : REQ_USER_DEL_N);
Expand Down
135 changes: 78 additions & 57 deletions imcrvmgr/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,53 @@
#include "utf8.h"
#include "imcrvmgr.h"

//
// request and reply commands
//
//search candidate
// request "1\n<key>\t<key(original)>\t<okuri>\n"
// reply "T\n<candidate(display)>\t<candidate(register)>\t<annotation(display)>\t<annotation(register)>\n...\n":hit
// "F\n":nothing
//search key for complement
// request "4\n<key prefix>\t<candidate max>\t\n"
// reply "T\n<key>\t\t<candidates>\t\n...\n":hit
// "F\n":nothing
//convert key
// request "5\n<key>\t\t<okuri>\n"
// reply "T\n<key converted>\n...\n":hit
// "F\n":nothing
//convert candidate
// request "6\n<key>\t<candidate>\t<okuri>\n"
// reply "T\n<candidate converted>\n":hit
// "F\n":nothing
//add candidate (complement off)
// request "A\n<key>\t<candidate>\t<annotation>\t<okuri>\n"
// reply "T\n"
//add candidate (complement on)
// request "B\n<key>\t<candidate>\t<annotation>\t\n"
// reply "T\n"
//delete candidate (complement off)
// request "C\n<key>\t<candidate>\n"
// reply "T\n"
//delete candidate (complement on)
// request "D\n<key>\t<candidate>\n"
// reply "T\n"
//save user dictionary
// request "S\n"
// reply "T\n"
//create configuration dialog process
// request "P\n"
// reply "T\n"
//caps lock
// request "I\n"
// reply "T\n"
//kana lock
// request "J\n"
// reply "T\n"
//

void SrvProc(WCHAR command, const std::wstring &argument, std::wstring &result)
{
/*
request and reply commands
search candidate
request "1\n<key>\t<key(original)>\t<okuri>\n"
reply "T\n<candidate(display)>\t<candidate(register)>\t<annotation(display)>\t<annotation(register)>\n...\n":hit
"F\n":nothing
search key for complement
request "4\n<key prefix>\t<candidate max>\t\n"
reply "T\n<key>\t\t<candidates>\t\n...\n":hit
"F\n":nothing
convert key
request "5\n<key>\t\t<okuri>\n"
reply "T\n<key converted>\n...\n":hit
"F\n":nothing
convert candidate
request "6\n<key>\t<candidate>\t<okuri>\n"
reply "T\n<candidate converted>\n":hit
"F\n":nothing
add candidate (complement off)
request "A\n<key>\t<candidate>\t<annotation>\t<okuri>\n"
reply "T\n"
add candidate (complement on)
request "B\n<key>\t<candidate>\t<annotation>\t\n"
reply "T\n"
delete candidate (complement off)
request "C\n<key>\t<candidate>\n"
reply "T\n"
delete candidate (complement on)
request "D\n<key>\t<candidate>\n"
reply "T\n"
save user dictionary
request "S\n"
reply "T\n"
create configuration dialog process
request "P\n"
reply "T\n"
caps lock
request "I\n"
reply "T\n"
kana lock
request "J\n"
reply "T\n"
*/

SKKDICCANDIDATES sc;
std::wstring fmt, key, keyorg, okuri, candidate, annotation, conv;
std::wregex re;
Expand Down Expand Up @@ -98,6 +98,7 @@ void SrvProc(WCHAR command, const std::wstring &argument, std::wstring &result)
{
lua_getglobal(lua, u8"lua_skk_complement");
lua_pushstring(lua, WCTOU8(key));

if (lua_pcall(lua, 1, 1, 0) == LUA_OK)
{
if (lua_isstring(lua, -1))
Expand All @@ -109,8 +110,8 @@ void SrvProc(WCHAR command, const std::wstring &argument, std::wstring &result)
sc_itr->first = ParseConcat(sc_itr->first);
}
}
lua_pop(lua, 1);
}
lua_pop(lua, 1);
}
else
{
Expand Down Expand Up @@ -182,6 +183,8 @@ void SrvProc(WCHAR command, const std::wstring &argument, std::wstring &result)
fmt.assign(L"$4");
okuri = std::regex_replace(argument, re, fmt);

result = REP_OK;

if (lua != nullptr)
{
lua_getglobal(lua, u8"lua_skk_add");
Expand All @@ -190,14 +193,17 @@ void SrvProc(WCHAR command, const std::wstring &argument, std::wstring &result)
lua_pushstring(lua, WCTOU8(candidate));
lua_pushstring(lua, WCTOU8(annotation));
lua_pushstring(lua, WCTOU8(okuri));
lua_pcall(lua, 5, 1, 0);

if (lua_pcall(lua, 5, 0, 0) != LUA_OK)
{
result = REP_FALSE;
}
}
else
{
AddUserDic(command, key, candidate, annotation, okuri);
}

result = REP_OK;
result += L"\n";
break;

Expand All @@ -209,35 +215,45 @@ void SrvProc(WCHAR command, const std::wstring &argument, std::wstring &result)
fmt.assign(L"$2");
candidate = std::regex_replace(argument, re, fmt);

result = REP_OK;

if (lua != nullptr)
{
lua_getglobal(lua, u8"lua_skk_delete");
lua_pushboolean(lua, (command == REQ_USER_DEL_A ? 1 : 0));
lua_pushstring(lua, WCTOU8(key));
lua_pushstring(lua, WCTOU8(candidate));
lua_pcall(lua, 3, 0, 0);

if (lua_pcall(lua, 3, 0, 0) != LUA_OK)
{
result = REP_FALSE;
}
}
else
{
DelUserDic(command, key, candidate);
}

result = REP_OK;
result += L"\n";
break;

case REQ_USER_SAVE:
result = REP_OK;

if (lua != nullptr)
{
lua_getglobal(lua, u8"lua_skk_save");
lua_pcall(lua, 0, 0, 0);

if (lua_pcall(lua, 0, 0, 0) != LUA_OK)
{
result = REP_FALSE;
}
}
else
{
StartSaveUserDic(TRUE);
}

result = REP_OK;
result += L"\n";
break;

Expand Down Expand Up @@ -268,15 +284,17 @@ unsigned __stdcall SrvThread(void *p)
HANDLE hPipe = (HANDLE)p;
DWORD bytesRead, bytesWrite;
BOOL bRet;
std::wstring wspipebuf;
WCHAR command;
std::wstring argument;
std::wstring wspipebuf;

#ifdef _DEBUG
std::wstring dedit, tedit;
std::wregex re;
std::wstring fmt;
#endif

WCHAR *pipebuf = (PWCHAR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR) * PIPEBUFSIZE);
PWCHAR pipebuf = (PWCHAR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR) * PIPEBUFSIZE);
if (pipebuf == nullptr)
{
return -1;
Expand Down Expand Up @@ -318,8 +336,6 @@ unsigned __stdcall SrvThread(void *p)
continue;
}

command = pipebuf[0];

#ifdef _DEBUG
tedit.assign(pipebuf);
re.assign(L"\n");
Expand All @@ -333,7 +349,12 @@ unsigned __stdcall SrvThread(void *p)
SetWindowTextW(hWndEdit, dedit.c_str());
#endif

SrvProc(command, &pipebuf[2], wspipebuf);
command = pipebuf[0];
argument.assign(&pipebuf[2]);
wspipebuf.clear();

SrvProc(command, argument, wspipebuf);

wcsncpy_s(pipebuf, PIPEBUFSIZE, wspipebuf.c_str(), _TRUNCATE);

#ifdef _DEBUG
Expand Down
Loading

0 comments on commit 0b32212

Please sign in to comment.