shellmatta/example/main.c

123 lines
2.7 KiB
C
Raw Normal View History

/*
* main.c
*
* Created on: Jun 10, 2019
* Author: stefan
*/
#include "shellmatta.h"
#include <stdint.h>
#include <stdio.h>
#include <ncurses.h>
#include <stdbool.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <string.h>
#include <errno.h>
static bool exitRequest = false;
int f;
shellmatta_handle_t handle;
void
set_blocking (int fd, int should_block)
{
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
printf ("error %d from tggetattr", errno);
return;
}
tty.c_cc[VMIN] = should_block ? 1 : 0;
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
if (tcsetattr (fd, TCSANOW, &tty) != 0)
printf ("error %d setting term attributes", errno);
}
static shellmatta_retCode_t doSomething(shellmatta_handle_t handle, const char *arguments, uint32_t length)
{
return SHELLMATTA_OK;
}
shellmatta_cmd_t doSomethingCmd = {"doSomething", "do", "Function does something", "use me, please", doSomething, NULL};
static shellmatta_retCode_t doSome(shellmatta_handle_t handle, const char *arguments, uint32_t length)
{
shellmatta_write(handle, "blubb\r\n", 7u);
return SHELLMATTA_OK;
}
shellmatta_cmd_t doSomeCmd = {"adoSome2", "adof2", "Function does something", "use me, please", doSome, NULL};
static shellmatta_retCode_t quit(shellmatta_handle_t handle, const char *arguments, uint32_t length)
{
exitRequest = true;
return SHELLMATTA_OK;
}
shellmatta_cmd_t quitCommand = {"quit", "q", "Function quits the shell", "", quit, NULL};
shellmatta_retCode_t writeFct(const char* data, uint32_t length)
{
write(f, data, length);
return SHELLMATTA_OK;
}
int main(void)
{
static char buffer[1024];
2019-06-27 22:56:03 +02:00
static char historyBuffer[4096];
static shellmatta_instance_t instance;
f = open("/dev/pts/3", O_RDWR | O_SYNC);
if (f < 0)
{
printf("failure %d\n", errno);
return f;
}
set_blocking (f, 1);
shellmatta_doInit( &instance,
&handle,
buffer,
sizeof(buffer),
historyBuffer,
sizeof(historyBuffer),
"shellmatta->",
NULL,
writeFct);
shellmatta_addCmd(handle, &doSomethingCmd);
shellmatta_addCmd(handle, &doSomeCmd);
shellmatta_addCmd(handle, &quitCommand);
while(exitRequest == false)
{
char c;
int res = 0;
res = read (f, &c, 1);
fprintf(stdout, "0x%02x \n", c);
fflush(stdout);
shellmatta_processData(handle, &c, res);
}
close(f);
return 0;
}