#include <stterm/stterm.h>
#include <stterm/commander.h>


#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include <stdio.h>

#ifdef STM32
   #include <biwak/biwak.h>
   #include <biwak/exti.h>       // keeper
   //#include <arena/arena.h>
#endif

#include <lepto/lepto.h>
#include <lepto/checks.h>
//#include "gitversion.h"

using namespace std;
#include <errno.h>   // _sbrk

CCommander *pCommander=nullptr;

int slotExecCommand( int foo, char *line )
{
   int argc=1;
   const char *argv[10];
   argv[0]=line;

   for(int i1=0; line[i1] && argc<10; i1++)
      if(line[i1]==' ')
      {
         line[i1]=0;
         argv[argc++]=&line[i1+1];
      }

   return ( pCommander->execCommand( argc, argv) );
}

#if 0

const int a_secsec __attribute__ ((section ("commands"), used)) __attribute__ ((used))  = 0x1234;
const int b_secsec __attribute__ ((section ("commands"), used)) __attribute__ ((used))  = 0x5678;

extern const int __start_commands asm("__start_commands");
extern const int __stop_commands  asm("__stop_commands");;


static void print_all()
{
    const int *iter = &__start_secsec;

    printf("Iters: \n");
    for ( ; iter < &__stop_secsec; ++iter) {
      printf("Formatting with 0x%X: %p\n", *iter, iter);
      //iter->fn(txt);
    }
    printf("Iters done.\n");
}
#endif

int main(int argc, char *argv[])
{
   #ifdef STM32
      biwakInit( false );
      //arenaInit();
   #endif

   CSterm *pSterm=new CSterm;
   pCommander=new CCommander;
   pSterm->signalExecCommand.connect( slotExecCommand );

   pCommander->registerCommands();

   while(true)
   {
      pSterm->eventLoop();
   }
   return (0);
}


//---fin----------------------------------------------------------------------
