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;
|
|
|
|
|
|
|
|
m_Flag = Flag;
|
|
|
|
|
|
|
|
mkfifo(pFifoFile, 0600);
|
|
|
|
|
2020-10-27 17:57:14 +00:00
|
|
|
struct stat Attribute;
|
|
|
|
stat(pFifoFile, &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
|
|
|
{
|
|
|
|
dbg_msg("fifo", "'%s' is not a fifo, removing", pFifoFile);
|
|
|
|
fs_remove(pFifoFile);
|
|
|
|
mkfifo(pFifoFile, 0600);
|
2020-10-27 17:57:14 +00:00
|
|
|
stat(pFifoFile, &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
|
|
|
{
|
|
|
|
dbg_msg("fifo", "can't remove file '%s', quitting", pFifoFile);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
m_File = open(pFifoFile, O_RDONLY | O_NONBLOCK);
|
2016-05-05 21:32:55 +00:00
|
|
|
if(m_File < 0)
|
2016-05-02 21:36:21 +00:00
|
|
|
dbg_msg("fifo", "can't open file '%s'", pFifoFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CFifo::Shutdown()
|
|
|
|
{
|
2016-05-05 21:32:55 +00:00
|
|
|
if(m_File >= 0)
|
|
|
|
close(m_File);
|
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
|