Skip to content

Commit

Permalink
Fix #233: add echo control
Browse files Browse the repository at this point in the history
  • Loading branch information
terrillmoore committed Nov 28, 2019
1 parent 891d16c commit ccb526c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/Catena_CommandStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ class cCommandStream : public cPollableObject
// complete an asynchronous command.
void completeCommand(CommandStatus status);

// get the echo-enable state of the underlying stream.
bool getEcho() const
{
return this->m_pCollector->getEcho();
}

// set the echo-enable state of the underlying stream.
void setEcho(bool fEnable)
{
this->m_pCollector->setEcho(fEnable);
}

protected:

private:
Expand Down
13 changes: 13 additions & 0 deletions src/Catena_StreamLineCollector.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,18 @@ class cStreamLineCollector : public cPollableObject
// formatted print for nested callers
void vprintf(const char *pFmt, std::va_list ap);

// enable or disable echo
void setEcho(bool fEnable)
{
this->m_fNoEcho = ! fEnable;
}

// get echo-enable state
bool getEcho() const
{
return ! this->m_fNoEcho;
}

protected:
void inputEdit(std::uint8_t c);
void doEcho(std::uint8_t c);
Expand All @@ -197,6 +209,7 @@ class cStreamLineCollector : public cPollableObject
// the stream we're working with
Stream *m_pStream = nullptr;
bool m_fLastWasCr = false;
bool m_fNoEcho = false;
cStreamReady *m_pStreamReady = nullptr;

// the callback function
Expand Down
36 changes: 36 additions & 0 deletions src/lib/CatenaBase_registerCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ static cCommandStream::CommandFn doSysEUI;
\****************************************************************************/

static cCommandStream::CommandFn doConfigure;
static cCommandStream::CommandFn doEcho;
static cCommandStream::CommandFn doReset;
static cCommandStream::CommandFn doVersion;

static const cCommandStream::cEntry sDispatchEntries[] =
{
{ "configure", doConfigure },
{ "echo", doEcho },
{ "reset", doReset },
{ "version", doVersion },
};
Expand Down Expand Up @@ -369,5 +371,39 @@ doVersion(
return cCommandStream::CommandStatus::kSuccess;
}

static const char sOn[] = "on";
static const char sOff[] = "off";

static cCommandStream::CommandStatus
doEcho(
cCommandStream *pThis,
void *pContext,
int argc,
char **argv
)
{
CatenaBase * const pCatena = static_cast<CatenaBase *>(pContext);
auto result = cCommandStream::CommandStatus::kSuccess;

if (argc > 2)
{
result = cCommandStream::CommandStatus::kInvalidParameter;
}
else if (argc <= 1)
{
pThis->printf("system echo %s\n", pThis->getEcho() ? sOn : sOff);
}
else /* argc == 2 */
{
if (strcmp(argv[1], sOn) == 0)
pThis->setEcho(true);
else if (strcmp(argv[1], sOff) == 0)
pThis->setEcho(false);
else
result = cCommandStream::CommandStatus::kInvalidParameter;
}

return result;
}

/**** end of CatenaBase_registerCommands.cpp ****/
17 changes: 12 additions & 5 deletions src/lib/Catena_StreamLineCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ McciCatena::cStreamLineCollector::inputEdit(
void
McciCatena::cStreamLineCollector::doEcho(std::uint8_t c)
{
// don't echo if echo is off.
if (this->m_fNoEcho)
return;

if (c == kCr || c == kLf || c == kTab)
this->write(c);
else if (0 <= c && c <= 0x1f)
Expand Down Expand Up @@ -350,24 +354,27 @@ void
McciCatena::cStreamLineCollector::doInputCancel()
{
this->m_pInsert = this->m_pBuffer;
this->echoControl(kCancel); this->write('\n');
this->echoControl(kCancel); this->doEcho('\n');
this->realign(this->m_inputColumn);
}

void
McciCatena::cStreamLineCollector::doInputRetype()
{
this->echoControl(kRetype); this->write('\n');
this->echoControl(kRetype); this->doEcho('\n');
this->realign(this->m_inputColumn);
for (auto p = this->m_pBuffer; p < this->m_pInsert; ++p)
this->write(*p);
this->doEcho(*p);
}

void
McciCatena::cStreamLineCollector::realign(
cStreamLineCollector::Columnator &t
)
{
if (this->m_fNoEcho)
return;

while (t.getColumn() < this->m_outputColumn.getColumn())
{
this->write(kBackspace);
Expand All @@ -393,8 +400,8 @@ McciCatena::cStreamLineCollector::echoControl(
std::uint8_t c
)
{
this->write('^');
this->write(c ^ 0x40);
this->doEcho('^');
this->doEcho(c ^ 0x40);
}

void
Expand Down

0 comments on commit ccb526c

Please sign in to comment.