2016-05-02 21:36:21 +00:00
|
|
|
#include "fifo.h"
|
|
|
|
|
2016-05-03 17:17:44 +00:00
|
|
|
#include <base/system.h>
|
2016-05-02 21:36:21 +00:00
|
|
|
#if defined(CONF_FAMILY_UNIX)
|
|
|
|
|
|
|
|
#include <engine/shared/config.h>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
2020-09-26 19:41:58 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2016-05-05 21:32:55 +00:00
|
|
|
#include <unistd.h>
|
2016-05-02 21:36:21 +00:00
|
|
|
|
|
|
|
void CFifo::Init(IConsole *pConsole, char *pFifoFile, int Flag)
|
|
|
|
{
|
2016-05-05 21:32:55 +00:00
|
|
|
m_File = -1;
|
2016-05-02 21:36:21 +00:00
|
|
|
|
|
|
|
m_pConsole = pConsole;
|
|
|
|
if(pFifoFile[0] == '\0')
|
|
|
|
return;
|
|
|
|
|
2021-02-21 16:31:25 +00:00
|
|
|
str_copy(m_aFilename, pFifoFile, sizeof(m_aFilename));
|
2016-05-02 21:36:21 +00:00
|
|
|
m_Flag = Flag;
|
|
|
|
|
2021-02-21 16:31:25 +00:00
|
|
|
mkfifo(m_aFilename, 0600);
|
2016-05-02 21:36:21 +00:00
|
|
|
|
2020-10-27 17:57:14 +00:00
|
|
|
struct stat Attribute;
|
2021-02-21 16:31:25 +00:00
|
|
|
stat(m_aFilename, &Attribute);
|
2016-05-02 21:36:21 +00:00
|
|
|
|
2020-10-27 17:57:14 +00:00
|
|
|
if(!S_ISFIFO(Attribute.st_mode))
|
2016-05-02 21:36:21 +00:00
|
|
|
{
|
2021-02-21 16:31:25 +00:00
|
|
|
dbg_msg("fifo", "'%s' is not a fifo, removing", m_aFilename);
|
|
|
|
fs_remove(m_aFilename);
|
|
|
|
mkfifo(m_aFilename, 0600);
|
|
|
|
stat(m_aFilename, &Attribute);
|
2016-05-02 21:36:21 +00:00
|
|
|
|
2020-10-27 17:57:14 +00:00
|
|
|
if(!S_ISFIFO(Attribute.st_mode))
|
2016-05-02 21:36:21 +00:00
|
|
|
{
|
2021-02-21 16:31:25 +00:00
|
|
|
dbg_msg("fifo", "can't remove file '%s', quitting", m_aFilename);
|
2016-05-02 21:36:21 +00:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-21 16:31:25 +00:00
|
|
|
m_File = open(m_aFilename, O_RDONLY | O_NONBLOCK);
|
2016-05-05 21:32:55 +00:00
|
|
|
if(m_File < 0)
|
2021-02-21 16:31:25 +00:00
|
|
|
dbg_msg("fifo", "can't open file '%s'", m_aFilename);
|
2016-05-02 21:36:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CFifo::Shutdown()
|
|
|
|
{
|
2016-05-05 21:32:55 +00:00
|
|
|
if(m_File >= 0)
|
2021-02-21 16:31:25 +00:00
|
|
|
{
|
2016-05-05 21:32:55 +00:00
|
|
|
close(m_File);
|
2021-02-21 16:31:25 +00:00
|
|
|
fs_remove(m_aFilename);
|
|
|
|
}
|
2016-05-02 21:36:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CFifo::Update()
|
|
|
|
{
|
2016-05-05 21:32:55 +00:00
|
|
|
if(m_File < 0)
|
2016-05-02 21:36:21 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
char aBuf[8192];
|
2016-05-05 21:32:55 +00:00
|
|
|
int Length = read(m_File, aBuf, sizeof(aBuf));
|
|
|
|
if(Length <= 0)
|
|
|
|
return;
|
2016-05-02 21:36:21 +00:00
|
|
|
|
2016-05-05 21:32:55 +00:00
|
|
|
char *pCur = aBuf;
|
|
|
|
for(int i = 0; i < Length; ++i)
|
2016-05-02 21:36:21 +00:00
|
|
|
{
|
2016-05-05 21:32:55 +00:00
|
|
|
if(aBuf[i] != '\n')
|
|
|
|
continue;
|
|
|
|
aBuf[i] = '\0';
|
|
|
|
m_pConsole->ExecuteLineFlag(pCur, m_Flag, -1);
|
2020-09-26 19:41:58 +00:00
|
|
|
pCur = aBuf + i + 1;
|
2016-05-02 21:36:21 +00:00
|
|
|
}
|
2020-09-26 19:41:58 +00:00
|
|
|
if(pCur < aBuf + Length) // missed the last line
|
2016-05-05 21:32:55 +00:00
|
|
|
m_pConsole->ExecuteLineFlag(pCur, m_Flag, -1);
|
2016-05-02 21:36:21 +00:00
|
|
|
}
|
|
|
|
#endif
|