hist.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* $NetBSD: hist.c,v 1.10 2002/03/18 16:00:53 christos Exp $ */
  2. /*-
  3. * Copyright (c) 1992, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Christos Zoulas of Cornell University.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. All advertising materials mentioning features or use of this software
  18. * must display the following acknowledgement:
  19. * This product includes software developed by the University of
  20. * California, Berkeley and its contributors.
  21. * 4. Neither the name of the University nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35. * SUCH DAMAGE.
  36. */
  37. #include "config.h"
  38. #if !defined(lint) && !defined(SCCSID)
  39. #if 0
  40. static char sccsid[] = "@(#)hist.c 8.1 (Berkeley) 6/4/93";
  41. #else
  42. __RCSID("$NetBSD: hist.c,v 1.10 2002/03/18 16:00:53 christos Exp $");
  43. #endif
  44. #endif /* not lint && not SCCSID */
  45. /*
  46. * hist.c: History access functions
  47. */
  48. #include <stdlib.h>
  49. #include "el.h"
  50. /* hist_init():
  51. * Initialization function.
  52. */
  53. protected int
  54. hist_init(EditLine *el)
  55. {
  56. el->el_history.fun = NULL;
  57. el->el_history.ref = NULL;
  58. el->el_history.buf = (char *) el_malloc(EL_BUFSIZ);
  59. el->el_history.sz = EL_BUFSIZ;
  60. if (el->el_history.buf == NULL)
  61. return (-1);
  62. el->el_history.last = el->el_history.buf;
  63. return (0);
  64. }
  65. /* hist_end():
  66. * clean up history;
  67. */
  68. protected void
  69. hist_end(EditLine *el)
  70. {
  71. el_free((ptr_t) el->el_history.buf);
  72. el->el_history.buf = NULL;
  73. }
  74. /* hist_set():
  75. * Set new history interface
  76. */
  77. protected int
  78. hist_set(EditLine *el, hist_fun_t fun, ptr_t ptr)
  79. {
  80. el->el_history.ref = ptr;
  81. el->el_history.fun = fun;
  82. return (0);
  83. }
  84. /* hist_get():
  85. * Get a history line and update it in the buffer.
  86. * eventno tells us the event to get.
  87. */
  88. protected el_action_t
  89. hist_get(EditLine *el)
  90. {
  91. const char *hp;
  92. int h;
  93. if (el->el_history.eventno == 0) { /* if really the current line */
  94. (void) strncpy(el->el_line.buffer, el->el_history.buf,
  95. el->el_history.sz - 1);
  96. el->el_line.lastchar = el->el_line.buffer +
  97. (el->el_history.last - el->el_history.buf);
  98. #ifdef KSHVI
  99. if (el->el_map.type == MAP_VI)
  100. el->el_line.cursor = el->el_line.buffer;
  101. else
  102. #endif /* KSHVI */
  103. el->el_line.cursor = el->el_line.lastchar;
  104. return (CC_REFRESH);
  105. }
  106. if (el->el_history.ref == NULL)
  107. return (CC_ERROR);
  108. hp = HIST_FIRST(el);
  109. if (hp == NULL)
  110. return (CC_ERROR);
  111. for (h = 1; h < el->el_history.eventno; h++)
  112. if ((hp = HIST_NEXT(el)) == NULL) {
  113. el->el_history.eventno = h;
  114. return (CC_ERROR);
  115. }
  116. (void) strncpy(el->el_line.buffer, hp,
  117. (size_t)(el->el_line.limit - el->el_line.buffer));
  118. el->el_line.lastchar = el->el_line.buffer + strlen(el->el_line.buffer);
  119. if (el->el_line.lastchar > el->el_line.buffer) {
  120. if (el->el_line.lastchar[-1] == '\n')
  121. el->el_line.lastchar--;
  122. if ((el->el_line.lastchar > el->el_line.buffer)&&(el->el_line.lastchar[-1] == ' ')) /* bill heckel */
  123. el->el_line.lastchar--;
  124. if (el->el_line.lastchar < el->el_line.buffer)
  125. el->el_line.lastchar = el->el_line.buffer;
  126. }
  127. #ifdef KSHVI
  128. if (el->el_map.type == MAP_VI)
  129. el->el_line.cursor = el->el_line.buffer;
  130. else
  131. #endif /* KSHVI */
  132. el->el_line.cursor = el->el_line.lastchar;
  133. return (CC_REFRESH);
  134. }
  135. /* hist_list()
  136. * List history entries
  137. */
  138. protected int
  139. /*ARGSUSED*/
  140. hist_list(EditLine *el, int argc, const char **argv)
  141. {
  142. const char *str;
  143. if (el->el_history.ref == NULL)
  144. return (-1);
  145. for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el))
  146. (void) fprintf(el->el_outfile, "%d %s",
  147. el->el_history.ev.num, str);
  148. return (0);
  149. }
  150. /* hist_enlargebuf()
  151. * Enlarge history buffer to specified value. Called from el_enlargebufs().
  152. * Return 0 for failure, 1 for success.
  153. */
  154. protected int
  155. /*ARGSUSED*/
  156. hist_enlargebuf(EditLine *el, size_t oldsz, size_t newsz)
  157. {
  158. char *newbuf;
  159. newbuf = realloc(el->el_history.buf, newsz);
  160. if (!newbuf)
  161. return 0;
  162. (void) memset(&newbuf[oldsz], '\0', newsz - oldsz);
  163. el->el_history.last = newbuf +
  164. (el->el_history.last - el->el_history.buf);
  165. el->el_history.buf = newbuf;
  166. el->el_history.sz = newsz;
  167. return 1;
  168. }