KISS Data Aquisition and Control System
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.6 KiB

  1. /*
  2. *
  3. * PRU Debug Program - command input function
  4. * (c) Copyright 2011, 2013 by Arctica Technologies
  5. * Written by Steven Anderson
  6. *
  7. */
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <fcntl.h>
  12. #include <sys/mman.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <errno.h>
  16. #include <termios.h>
  17. #include <sys/ioctl.h>
  18. #include "prudbg.h"
  19. int cmd_input(char *prompt, char *cmd, char *cmdargs, unsigned int *argptrs, unsigned int *numargs)
  20. {
  21. unsigned int i, j, full_len, on_zero;
  22. char c, last_char;
  23. char buf[MAX_COMMAND_LINE];
  24. // print prompt
  25. printf("%s", prompt);
  26. // collect command until space or return
  27. i = 0;
  28. do {
  29. c = getchar();
  30. // check for backspace
  31. if (c == 0x08 || c == 0x7F) {
  32. if (i != 0) {
  33. putchar(0x08);
  34. putchar(' ');
  35. putchar(0x08);
  36. i--;
  37. }
  38. // just a normal character
  39. } else {
  40. buf[i] = c;
  41. printf("%c", buf[i]);
  42. if (i < (MAX_COMMAND_LINE - 1)) i++;
  43. }
  44. } while (c != '\n');
  45. buf[i-1] = 0;
  46. // replace spaces and return with zeros
  47. full_len = strlen(buf);
  48. for (i=0; i<full_len; i++) if (buf[i] == ' ') buf[i] = 0;
  49. // copy command (first word) to cmd argument and shift to upper case
  50. for (i=0; i<(strlen(buf)+1); i++) cmd[i] = toupper(buf[i]);
  51. // build index array and count number of arguments
  52. for (i=strlen(cmd), on_zero=TRUE, numargs[0]=0; i<full_len; i++) {
  53. if (on_zero) {
  54. if (buf[i] != 0) {
  55. on_zero = FALSE;
  56. argptrs[numargs[0]++] = i;
  57. }
  58. } else {
  59. if (buf[i] == 0) on_zero = TRUE;
  60. }
  61. }
  62. for (i=0; i<full_len+1; i++) cmdargs[i] = buf[i];
  63. return 0;
  64. }