sig.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* $NetBSD: sig.c,v 1.9 2002/03/18 16:00:58 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[] = "@(#)sig.c 8.1 (Berkeley) 6/4/93";
  41. #else
  42. __RCSID("$NetBSD: sig.c,v 1.9 2002/03/18 16:00:58 christos Exp $");
  43. #endif
  44. #endif /* not lint && not SCCSID */
  45. /*
  46. * sig.c: Signal handling stuff.
  47. * our policy is to trap all signals, set a good state
  48. * and pass the ball to our caller.
  49. */
  50. #include "el.h"
  51. #include <stdlib.h>
  52. private EditLine *sel = NULL;
  53. private const int sighdl[] = {
  54. #define _DO(a) (a),
  55. ALLSIGS
  56. #undef _DO
  57. - 1
  58. };
  59. private void sig_handler(int);
  60. /* sig_handler():
  61. * This is the handler called for all signals
  62. * XXX: we cannot pass any data so we just store the old editline
  63. * state in a private variable
  64. */
  65. private void
  66. sig_handler(int signo)
  67. {
  68. int i;
  69. sigset_t nset, oset;
  70. (void) sigemptyset(&nset);
  71. (void) sigaddset(&nset, signo);
  72. (void) sigprocmask(SIG_BLOCK, &nset, &oset);
  73. switch (signo) {
  74. case SIGCONT:
  75. tty_rawmode(sel);
  76. if (ed_redisplay(sel, 0) == CC_REFRESH)
  77. re_refresh(sel);
  78. term__flush();
  79. break;
  80. case SIGWINCH:
  81. el_resize(sel);
  82. break;
  83. default:
  84. tty_cookedmode(sel);
  85. break;
  86. }
  87. for (i = 0; sighdl[i] != -1; i++)
  88. if (signo == sighdl[i])
  89. break;
  90. (void) signal(signo, sel->el_signal[i]);
  91. (void) sigprocmask(SIG_SETMASK, &oset, NULL);
  92. (void) kill(0, signo);
  93. }
  94. /* sig_init():
  95. * Initialize all signal stuff
  96. */
  97. protected int
  98. sig_init(EditLine *el)
  99. {
  100. int i;
  101. sigset_t nset, oset;
  102. (void) sigemptyset(&nset);
  103. #define _DO(a) (void) sigaddset(&nset, a);
  104. ALLSIGS
  105. #undef _DO
  106. (void) sigprocmask(SIG_BLOCK, &nset, &oset);
  107. #define SIGSIZE (sizeof(sighdl) / sizeof(sighdl[0]) * sizeof(sig_t))
  108. el->el_signal = (sig_t *) el_malloc(SIGSIZE);
  109. if (el->el_signal == NULL)
  110. return (-1);
  111. for (i = 0; sighdl[i] != -1; i++)
  112. el->el_signal[i] = SIG_ERR;
  113. (void) sigprocmask(SIG_SETMASK, &oset, NULL);
  114. return (0);
  115. }
  116. /* sig_end():
  117. * Clear all signal stuff
  118. */
  119. protected void
  120. sig_end(EditLine *el)
  121. {
  122. el_free((ptr_t) el->el_signal);
  123. el->el_signal = NULL;
  124. }
  125. /* sig_set():
  126. * set all the signal handlers
  127. */
  128. protected void
  129. sig_set(EditLine *el)
  130. {
  131. int i;
  132. sigset_t nset, oset;
  133. (void) sigemptyset(&nset);
  134. #define _DO(a) (void) sigaddset(&nset, a);
  135. ALLSIGS
  136. #undef _DO
  137. (void) sigprocmask(SIG_BLOCK, &nset, &oset);
  138. for (i = 0; sighdl[i] != -1; i++) {
  139. sig_t s;
  140. /* This could happen if we get interrupted */
  141. if ((s = signal(sighdl[i], sig_handler)) != sig_handler)
  142. el->el_signal[i] = s;
  143. }
  144. sel = el;
  145. (void) sigprocmask(SIG_SETMASK, &oset, NULL);
  146. }
  147. /* sig_clr():
  148. * clear all the signal handlers
  149. */
  150. protected void
  151. sig_clr(EditLine *el)
  152. {
  153. int i;
  154. sigset_t nset, oset;
  155. (void) sigemptyset(&nset);
  156. #define _DO(a) (void) sigaddset(&nset, a);
  157. ALLSIGS
  158. #undef _DO
  159. (void) sigprocmask(SIG_BLOCK, &nset, &oset);
  160. for (i = 0; sighdl[i] != -1; i++)
  161. if (el->el_signal[i] != SIG_ERR)
  162. (void) signal(sighdl[i], el->el_signal[i]);
  163. sel = NULL; /* we are going to die if the handler is
  164. * called */
  165. (void) sigprocmask(SIG_SETMASK, &oset, NULL);
  166. }