// Redirecting to Tmp: - build filename in TMP directory
void RedirToTmp(CAtlString& strName, int fStd)
{
LPTSTR pszTmpName;
CAtlString strBuffer;
// look for TEMP disk area defined in environment, or default to boot
GetTempDirectory(strName);
// if no TMP directory, use TCMD directory
if (strName[0] == _T('\0'))
PathPart(aGlobals.strShellName, strName);
if (fStd == 0)
pszTmpName = _TEXT("CIN");
else if (fStd == 1)
pszTmpName = _TEXT("COUT");
else if (fStd == 2)
pszTmpName = _TEXT("CERR");
else
pszTmpName = _TEXT("CTMP");
strBuffer.Format(_TEXT("%s%x.JPS"), pszTmpName, GetCurrentProcessId());
MakeDirectoryName(strName, strBuffer);
}
// copy the file to the tmp buffer
DLLExports int WINAPI CopyToTmp(HANDLE hFile, int nTmp)
{
LPSTR lpGlobalMemory = nullptr;
LONG lMemSize;
DWORD dwBytesRead;
int nReturn = 0;
if ((nTmp < 0) || (nTmp > 9))
return ERROR_EXIT;
// set tmp buffer size (we have to accept 0-byte files to avoid problems with commands like ">tmp:")
if ((lMemSize = (ULONG)QuerySeekSize(hFile)) >= 0L) {
// it might be a Unicode file
if ((lpGlobalMemory = (LPSTR)AllocMem( (lMemSize * sizeof(TCHAR)) + 2)) == nullptr) {
return TCError(GetLastError(), NULL);
}
// save file
RewindFile(hFile);
int fUTF8 = 0;
int fUnicode = QueryIsFileUnicode(hFile);
if (fUnicode == 0) {
fUTF8 = QueryIsFileUTF8(hFile);
}
if (ReadFile(hFile, lpGlobalMemory, (UINT)lMemSize, &dwBytesRead, NULL) != 0) {
lpGlobalMemory[dwBytesRead] = _TEXT('\0');
lpGlobalMemory[dwBytesRead+1] = _TEXT('\0');
if (fUnicode) {
aGlobals.m_Tmps[nTmp] = (LPTSTR)lpGlobalMemory;
}
else if (fUTF8) {
aGlobals.m_Tmps[nTmp] = CA2W(lpGlobalMemory, CP_UTF8);
}
else {
// convert to UTF16
aGlobals.m_Tmps[nTmp] = CA2W(lpGlobalMemory);
}
FreeMem(lpGlobalMemory);
return 0;
}
FreeMem(lpGlobalMemory);
}
return nReturn;
}
DLLExports int WINAPI CopyTextToTmp(LPCTSTR pszLine, int nLength, int nTmp)
{
if ((nTmp < 0) || (nTmp > 9) || (pszLine == NULL))
return ERROR_EXIT;
if (nLength >= 0)
aGlobals.m_Tmps[nTmp].SetString(pszLine, nLength);
else
aGlobals.m_Tmps[nTmp] = pszLine;
return 0;
}
DLLExports int WINAPI AppendTextToTmp(LPCTSTR pszLine, int nLength, int nTmp)
{
if ((nTmp < 0) || (nTmp > 9) || (pszLine == NULL))
return ERROR_EXIT;
if (nLength >= 0)
aGlobals.m_Tmps[nTmp].Append(pszLine, nLength);
else
aGlobals.m_Tmps[nTmp] += pszLine;
return 0;
}
// Redirecting from TMP: - copy from TMP: buffer to the specified file
DLLExports int WINAPI CopyFromTmp(LPCTSTR pszFilename, int nTmp)
{
LPTSTR lpTmpMemory;
int nReturn = 0;
HANDLE hFile = INVALID_HANDLE_VALUE;
if ((nTmp < 0) || (nTmp > 9))
return ERROR_EXIT;
_wremove(pszFilename);
// Open the file
hFile = CreateFile(pszFilename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
nReturn = TCError(GetLastError(), pszFilename);
else {
// save TMP to file
lpTmpMemory = (LPTSTR)(LPCTSTR)aGlobals.m_Tmps[nTmp];
wwriteXP(hFile, lpTmpMemory, lstrlen(lpTmpMemory));
CloseHandle(hFile);
}
return nReturn;
}
// get the specified line from tmp
DLLExports int WINAPI GetTmpLine(int nLine, CAtlString& strLine, int nTmp)
{
LPCTSTR lpUnicode;
LPCTSTR pszEOL;
strLine.Empty();
if ((nTmp < 0) || (nTmp > 9) || (nLine < 0))
return ERROR_EXIT;
lpUnicode = (LPCTSTR)aGlobals.m_Tmps[nTmp];
// copy tmp to the buffer
for (; (nLine >= 0); nLine--) {
if (*lpUnicode == _TEXT('\0')) {
return ERROR_EXIT;
}
for (pszEOL = lpUnicode; (*pszEOL != _TEXT('\0')) && (*pszEOL != _TEXT('\r')) && (*pszEOL != _TEXT('\n')); pszEOL++)
;
if (nLine == 0) {
strLine.SetString(lpUnicode, (int)(pszEOL - lpUnicode));
return 0;
}
lpUnicode = pszEOL;
if (*lpUnicode == _TEXT('\r'))
lpUnicode++;
if (*lpUnicode == _TEXT('\n'))
lpUnicode++;
if ((nLine % 32) == 0)
tty_yield(0);
}
return ERROR_EXIT;
}
// get the number of lines in TMP:
DLLExports int WINAPI GetTmpLines(int nTmp)
{
unsigned int uLines = 0;
LPCTSTR lpUnicode;
if ((nTmp >= 0) && (nTmp <= 9)) {
lpUnicode = (LPCTSTR)aGlobals.m_Tmps[nTmp];
// copy TMP to the buffer
for (; ; uLines++) {
if (*lpUnicode == _TEXT('\0')) {
break;
}
while ((*lpUnicode != _TEXT('\0')) && (*lpUnicode != _TEXT('\r')) && (*lpUnicode != _TEXT('\n')))
lpUnicode++;
if (*lpUnicode == _TEXT('\r'))
lpUnicode++;
if (*lpUnicode == _TEXT('\n'))
lpUnicode++;
if ((uLines % 256) == 0)
tty_yield(0);
}
return uLines;
}
return 0;
}