Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Plugin access to TMPn: pseudo-devices

Charles Dye

Super Moderator
May
5,853
217
Staff member
Is there an easy way for plugins to read the TMPn: devices? Write to them?

How are they stored, anyway? In memory? Temp files?

I suppose we could call the internal COPY command to copy them to/from regular files, but that just seems kludgey.
 
They are CAtlStrings.

TakeCmd.dll does have some exports for handling them. The relevant code in TakeCmd.dll is:

Code:
// 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;
}
 
Well, that helps explain why I have never been able to do this without an error;

Code:
@SETLOCAL
@ECHO OFF
TMP /c tmp8:
TMP /c tmp7:
Gosub Testing > tmp8:
dir r:\ > tmp7:
type tmp8:
type tmp7:
ENDLOCAL
quit

:Testing
echo ISODate: %_ISODate
echo Line 2
echo Line 3
Return

Code:
R:\>test.btm > tmp9:
TCC: (Sys) R:\test.btm [5]  The process cannot access the file because it is being used by another process.
 "r:\temp\COUT5250.JPS"
TCC: (Sys) R:\test.btm [6]  The process cannot access the file because it is being used by another process.
 "r:\temp\COUT5250.JPS"

Joe
 
Back
Top