main.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 set_blocking (int fd, int should_block)
  21. {
  22. struct termios tty;
  23. memset (&tty, 0, sizeof tty);
  24. if (tcgetattr (fd, &tty) != 0)
  25. {
  26. printf ("error %d from tggetattr", errno);
  27. return;
  28. }
  29. tty.c_cc[VMIN] = should_block ? 1 : 0;
  30. tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
  31. if (tcsetattr (fd, TCSANOW, &tty) != 0)
  32. printf ("error %d setting term attributes", errno);
  33. }
  34. static shellmatta_retCode_t doSomething(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  35. {
  36. return SHELLMATTA_OK;
  37. }
  38. shellmatta_cmd_t doSomethingCmd = {"doSomething", "do", "Function does something", "use me, please", doSomething, NULL};
  39. static shellmatta_retCode_t doSome(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  40. {
  41. shellmatta_write(handle, "blubb\r\n", 7u);
  42. return SHELLMATTA_OK;
  43. }
  44. shellmatta_cmd_t doSomeCmd = {"adoSome2", "adof2", "Function does something", "use me, please", doSome, NULL};
  45. static shellmatta_retCode_t quit(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  46. {
  47. exitRequest = true;
  48. return SHELLMATTA_OK;
  49. }
  50. shellmatta_cmd_t quitCommand = {"quit", "q", "Function quits the shell", "", quit, NULL};
  51. shellmatta_retCode_t writeFct(const char* data, uint32_t length)
  52. {
  53. write(f, data, length);
  54. return SHELLMATTA_OK;
  55. }
  56. int main(void)
  57. {
  58. static char buffer[1024];
  59. static char historyBuffer[4096];
  60. static shellmatta_instance_t instance;
  61. f = open("/dev/pts/1", O_RDWR | O_SYNC);
  62. if (f < 0)
  63. {
  64. printf("failure %d\n", errno);
  65. return f;
  66. }
  67. set_blocking (f, 1);
  68. shellmatta_doInit( &instance,
  69. &handle,
  70. buffer,
  71. sizeof(buffer),
  72. historyBuffer,
  73. sizeof(historyBuffer),
  74. "shellmatta->",
  75. NULL,
  76. writeFct);
  77. shellmatta_addCmd(handle, &doSomethingCmd);
  78. shellmatta_addCmd(handle, &doSomeCmd);
  79. shellmatta_addCmd(handle, &quitCommand);
  80. while(exitRequest == false)
  81. {
  82. char c;
  83. int res = 0;
  84. res = read (f, &c, 1);
  85. fprintf(stdout, "0x%02x \n", c);
  86. fflush(stdout);
  87. shellmatta_processData(handle, &c, res);
  88. }
  89. close(f);
  90. return 0;
  91. }