shellmatta/example/main.c

141 lines
3.1 KiB
C

/*
* 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)
{
//shellmatta_printf(handle, "blubb\n\r");
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_printf(handle, "blubb\n\r");
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)
{
//shellmatta_printf(handle, "Bye\n\r");
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)
{
// for(uint32_t i = 0u; i < length; i ++)
// {
write(f, data, length);
//printf("%c", data[i]);
// }
//fflush(stdout);
return SHELLMATTA_OK;
}
int main(void)
{
static char buffer[1024];
static char historyBuffer[0];
static shellmatta_instance_t instance;
// initscr();
// raw();
// keypad(stdscr, TRUE);
// noecho();
f = open("/dev/pts/5", 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(&instance, &doSomethingCmd);
shellmatta_addCmd(&instance, &doSomeCmd);
shellmatta_addCmd(&instance, &quitCommand);
while(exitRequest == false)
{
char c;
int res = 0;
res = read (f, &c, 1);
fprintf(stdout, "0x%02x \n", c);
fflush(stdout);
shellmatta_processData(&instance, &c, res);
}
// refresh();
// endwin();
close(f);
return 0;
}