main.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * main.c
  3. *
  4. * Created on: Jun 10, 2019
  5. * Author: stefan
  6. */
  7. #include "shellmatta.h"
  8. #include <stdint.h>
  9. #include <stdio.h>
  10. #include <ncurses.h>
  11. #include <stdbool.h>
  12. #include <fcntl.h>
  13. #include <unistd.h>
  14. #include <termios.h>
  15. #include <string.h>
  16. #include <errno.h>
  17. static bool exitRequest = false;
  18. int f;
  19. shellmatta_handle_t handle;
  20. void
  21. set_blocking (int fd, int should_block)
  22. {
  23. struct termios tty;
  24. memset (&tty, 0, sizeof tty);
  25. if (tcgetattr (fd, &tty) != 0)
  26. {
  27. printf ("error %d from tggetattr", errno);
  28. return;
  29. }
  30. tty.c_cc[VMIN] = should_block ? 1 : 0;
  31. tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
  32. if (tcsetattr (fd, TCSANOW, &tty) != 0)
  33. printf ("error %d setting term attributes", errno);
  34. }
  35. static shellmatta_retCode_t doSomething(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  36. {
  37. //shellmatta_printf(handle, "blubb\n\r");
  38. return SHELLMATTA_OK;
  39. }
  40. shellmatta_cmd_t doSomethingCmd = {"doSomething", "do", "Function does something", "use me, please", doSomething, NULL};
  41. static shellmatta_retCode_t doSome(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  42. {
  43. //shellmatta_printf(handle, "blubb\n\r");
  44. shellmatta_write(handle, "blubb\r\n", 7u);
  45. return SHELLMATTA_OK;
  46. }
  47. shellmatta_cmd_t doSomeCmd = {"adoSome2", "adof2", "Function does something", "use me, please", doSome, NULL};
  48. static shellmatta_retCode_t quit(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  49. {
  50. //shellmatta_printf(handle, "Bye\n\r");
  51. exitRequest = true;
  52. return SHELLMATTA_OK;
  53. }
  54. shellmatta_cmd_t quitCommand = {"quit", "q", "Function quits the shell", "", quit, NULL};
  55. shellmatta_retCode_t writeFct(const char* data, uint32_t length)
  56. {
  57. // for(uint32_t i = 0u; i < length; i ++)
  58. // {
  59. write(f, data, length);
  60. //printf("%c", data[i]);
  61. // }
  62. //fflush(stdout);
  63. return SHELLMATTA_OK;
  64. }
  65. int main(void)
  66. {
  67. static char buffer[1024];
  68. static char historyBuffer[4096];
  69. static shellmatta_instance_t instance;
  70. // initscr();
  71. // raw();
  72. // keypad(stdscr, TRUE);
  73. // noecho();
  74. f = open("/dev/pts/2", O_RDWR | O_SYNC);
  75. if (f < 0)
  76. {
  77. printf("failure %d\n", errno);
  78. return f;
  79. }
  80. set_blocking (f, 1);
  81. shellmatta_doInit( &instance,
  82. &handle,
  83. buffer,
  84. sizeof(buffer),
  85. historyBuffer,
  86. sizeof(historyBuffer),
  87. "shellmatta->",
  88. NULL,
  89. writeFct);
  90. shellmatta_addCmd(&instance, &doSomethingCmd);
  91. shellmatta_addCmd(&instance, &doSomeCmd);
  92. shellmatta_addCmd(&instance, &quitCommand);
  93. while(exitRequest == false)
  94. {
  95. char c;
  96. int res = 0;
  97. res = read (f, &c, 1);
  98. fprintf(stdout, "0x%02x \n", c);
  99. fflush(stdout);
  100. shellmatta_processData(&instance, &c, res);
  101. }
  102. // refresh();
  103. // endwin();
  104. close(f);
  105. return 0;
  106. }