prompt.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* $NetBSD: prompt.c,v 1.9 2002/03/18 16:00:56 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[] = "@(#)prompt.c 8.1 (Berkeley) 6/4/93";
  41. #else
  42. __RCSID("$NetBSD: prompt.c,v 1.9 2002/03/18 16:00:56 christos Exp $");
  43. #endif
  44. #endif /* not lint && not SCCSID */
  45. /*
  46. * prompt.c: Prompt printing functions
  47. */
  48. #include <stdio.h>
  49. #include "el.h"
  50. private char *prompt_default(EditLine *);
  51. private char *prompt_default_r(EditLine *);
  52. /* prompt_default():
  53. * Just a default prompt, in case the user did not provide one
  54. */
  55. private char *
  56. /*ARGSUSED*/
  57. prompt_default(EditLine *el)
  58. {
  59. static char a[3] = {'?', ' ', '\0'};
  60. return (a);
  61. }
  62. /* prompt_default_r():
  63. * Just a default rprompt, in case the user did not provide one
  64. */
  65. private char *
  66. /*ARGSUSED*/
  67. prompt_default_r(EditLine *el)
  68. {
  69. static char a[1] = {'\0'};
  70. return (a);
  71. }
  72. /* prompt_print():
  73. * Print the prompt and update the prompt position.
  74. * We use an array of integers in case we want to pass
  75. * literal escape sequences in the prompt and we want a
  76. * bit to flag them
  77. */
  78. protected void
  79. prompt_print(EditLine *el, int op)
  80. {
  81. el_prompt_t *elp;
  82. char *p;
  83. if (op == EL_PROMPT)
  84. elp = &el->el_prompt;
  85. else
  86. elp = &el->el_rprompt;
  87. p = (elp->p_func) (el);
  88. while (*p)
  89. re_putc(el, *p++, 1);
  90. elp->p_pos.v = el->el_refresh.r_cursor.v;
  91. elp->p_pos.h = el->el_refresh.r_cursor.h;
  92. }
  93. /* prompt_init():
  94. * Initialize the prompt stuff
  95. */
  96. protected int
  97. prompt_init(EditLine *el)
  98. {
  99. el->el_prompt.p_func = prompt_default;
  100. el->el_prompt.p_pos.v = 0;
  101. el->el_prompt.p_pos.h = 0;
  102. el->el_rprompt.p_func = prompt_default_r;
  103. el->el_rprompt.p_pos.v = 0;
  104. el->el_rprompt.p_pos.h = 0;
  105. return (0);
  106. }
  107. /* prompt_end():
  108. * Clean up the prompt stuff
  109. */
  110. protected void
  111. /*ARGSUSED*/
  112. prompt_end(EditLine *el)
  113. {
  114. }
  115. /* prompt_set():
  116. * Install a prompt printing function
  117. */
  118. protected int
  119. prompt_set(EditLine *el, el_pfunc_t prf, int op)
  120. {
  121. el_prompt_t *p;
  122. if (op == EL_PROMPT)
  123. p = &el->el_prompt;
  124. else
  125. p = &el->el_rprompt;
  126. if (prf == NULL) {
  127. if (op == EL_PROMPT)
  128. p->p_func = prompt_default;
  129. else
  130. p->p_func = prompt_default_r;
  131. } else
  132. p->p_func = prf;
  133. p->p_pos.v = 0;
  134. p->p_pos.h = 0;
  135. return (0);
  136. }
  137. /* prompt_get():
  138. * Retrieve the prompt printing function
  139. */
  140. protected int
  141. prompt_get(EditLine *el, el_pfunc_t *prf, int op)
  142. {
  143. if (prf == NULL)
  144. return (-1);
  145. if (op == EL_PROMPT)
  146. *prf = el->el_prompt.p_func;
  147. else
  148. *prf = el->el_rprompt.p_func;
  149. return (0);
  150. }