ecuicmhist.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*+-------------------------------------------------------------------------
  2. ecuicmhist.c - ECU interactive command history handler
  3. wht@wht.net
  4. Defined functions:
  5. icmd_history_add(icmd_buf)
  6. icmd_history_manager(func, newicmd, icmdsize)
  7. I met this girl in Macy's. She was shopping, and I was putting
  8. slinkeys on the escalator. -- Steven Wright
  9. --------------------------------------------------------------------------*/
  10. /*+:EDITS:*/
  11. /*:04-26-2000-11:15-wht@bob-RELEASE 4.42 */
  12. /*:01-08-2000-14:08-wht@menlo-strdup can be a macro */
  13. /*:01-24-1997-02:37-wht@yuriatin-SOURCE RELEASE 4.00 */
  14. /*:09-11-1996-20:00-wht@yuriatin-3.48-major telnet,curses,structural overhaul */
  15. /*:11-23-1995-11:20-wht@kepler-source control 3.37 for tsx-11 */
  16. /*:11-14-1995-10:23-wht@kepler-3.37.80-source control point: SOCKETS */
  17. /*:01-12-1995-15:19-wht@n4hgf-apply Andrew Chernov 8-bit clean+FreeBSD patch */
  18. /*:05-04-1994-04:38-wht@n4hgf-ECU release 3.30 */
  19. /*:09-13-1992-02:05-wht@n4hgf-redisplay escape prompt on error exit */
  20. /*:09-10-1992-13:58-wht@n4hgf-ECU release 3.20 */
  21. /*:08-22-1992-15:38-wht@n4hgf-ECU release 3.20 BETA */
  22. /*:08-28-1991-14:07-wht@n4hgf2-SVR4 cleanup by aega84!lh */
  23. /*:08-11-1991-14:58-wht@n4hgf-new ttygets botched command history handler */
  24. /*:07-25-1991-12:56-wht@n4hgf-ECU release 3.10 */
  25. /*:07-14-1991-18:18-wht@n4hgf-new ttygets functions */
  26. /*:08-14-1990-20:40-wht@n4hgf-ecu3.00-flush old edit history */
  27. #include <string.h>
  28. #include "ecucurses.h"
  29. #define STDIO_H_INCLUDED
  30. #define OMIT_TERMIO_REFERENCES
  31. #include "ecu.h"
  32. #include "pc_scr.h"
  33. #include "ecukey.h"
  34. #include "ecuxkey.h"
  35. #include "ecutty.h"
  36. #define ICMDH_MAXCNT 50
  37. #define ICMDH_MAXLEN 72
  38. typedef struct icmd_hist
  39. {
  40. struct icmd_hist *prev;
  41. struct icmd_hist *next;
  42. uchar *icmd;
  43. } ICMDH;
  44. ICMDH *icmdh_head = (ICMDH *) 0;
  45. ICMDH *icmdh_tail = (ICMDH *) 0;
  46. int icmdh_count = 0;
  47. /*+-------------------------------------------------------------------------
  48. icmd_history_add(icmd_buf)
  49. --------------------------------------------------------------------------*/
  50. void
  51. icmd_history_add(icmd_buf)
  52. char *icmd_buf;
  53. {
  54. ICMDH *icmdh = (ICMDH *) malloc(sizeof(ICMDH));
  55. if (!icmdh)
  56. return;
  57. if (!(icmdh->icmd = (uchar *) strdup(icmd_buf)))
  58. {
  59. free((char *)icmdh);
  60. return;
  61. }
  62. if (strlen((char *)icmdh->icmd) > (unsigned)ICMDH_MAXLEN)
  63. icmdh->icmd[ICMDH_MAXLEN] = 0;
  64. if (icmdh_tail)
  65. {
  66. icmdh_tail->next = icmdh;
  67. icmdh->prev = icmdh_tail;
  68. icmdh->next = (ICMDH *) 0;
  69. icmdh_tail = icmdh;
  70. }
  71. else
  72. {
  73. icmdh->prev = (ICMDH *) 0;
  74. icmdh->next = (ICMDH *) 0;
  75. icmdh_head = icmdh;
  76. icmdh_tail = icmdh;
  77. }
  78. if (++icmdh_count > ICMDH_MAXCNT)
  79. {
  80. icmdh = icmdh_head;
  81. icmdh_head = icmdh->next;
  82. icmdh_head->prev = (ICMDH *) 0;
  83. free((char *)icmdh->icmd);
  84. free((char *)icmdh);
  85. icmdh_count--;
  86. }
  87. } /* end of icmd_history_add */
  88. /*+-------------------------------------------------------------------------
  89. icmd_history_manager(func,newicmd,icmdsize) - entered by Home Xkey
  90. return new icmd string to execute
  91. returns 0 if ok to exce new cmd, else 1 if not
  92. (returns 0 if null or ESC, so caller can handle exit condition)
  93. --------------------------------------------------------------------------*/
  94. /*ARGSUSED*/
  95. int
  96. icmd_history_manager(func, newicmd, icmdsize)
  97. uchar func;
  98. uchar *newicmd;
  99. int icmdsize;
  100. {
  101. int itmp;
  102. ICMDH *icmdh = icmdh_tail;
  103. UINT delim;
  104. if (!icmdh)
  105. {
  106. ff(se, " no interactive commands saved\r\n");
  107. show_escape_prompt();
  108. return (1);
  109. }
  110. while (1)
  111. {
  112. strncpy((char *)newicmd, (char *)icmdh->icmd, icmdsize - 1);
  113. *(newicmd + icmdsize - 1) = 0;
  114. ttygets(newicmd, icmdsize, TG_XDELIM | TG_EDIT, &delim, (int *)0);
  115. if (!newicmd[0])
  116. return (0);
  117. switch (delim)
  118. {
  119. case ESC:
  120. *newicmd = 0;
  121. return (0);
  122. case XFhome:
  123. icmdh = icmdh_head;
  124. break;
  125. case XFend:
  126. icmdh = icmdh_tail;
  127. break;
  128. case XFpgup:
  129. case XFpgdn:
  130. ring_bell();
  131. break;
  132. case XFcurup:
  133. if (icmdh->prev)
  134. icmdh = icmdh->prev;
  135. break;
  136. case XFcurdn:
  137. if (icmdh->next)
  138. icmdh = icmdh->next;
  139. break;
  140. default:
  141. return (0);
  142. }
  143. itmp = strlen((char *)newicmd);
  144. while (itmp--)
  145. fputc(BS, se);
  146. itmp = strlen((char *)newicmd);
  147. while (itmp--)
  148. fputc(' ', se);
  149. itmp = strlen((char *)newicmd);
  150. while (itmp--)
  151. fputc(BS, se);
  152. }
  153. } /* end of icmd_history_manager */
  154. /* vi: set tabstop=4 shiftwidth=4: */
  155. /* end of ecuicmhist.c */