home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume11 / mush5.7 / part11 / signals.c < prev   
Encoding:
C/C++ Source or Header  |  1987-09-19  |  9.2 KB  |  327 lines

  1. /* @(#)signals.c    (c) copyright 10/18/86 (Dan Heller) */
  2.  
  3. #include "mush.h"
  4.  
  5. #ifndef SYSV
  6. extern char *sys_siglist[];
  7. #else
  8. /* sys-v doesn't have normal sys_siglist */
  9. static char    *sys_siglist[] = {
  10. /* no error */  "no error",
  11. /* SIGHUP */    "hangup",
  12. /* SIGINT */    "interrupt (rubout)",
  13. /* SIGQUIT */    "quit (ASCII FS)",
  14. /* SIGILL */    "illegal instruction (not reset when caught)",
  15. /* SIGTRAP */    "trace trap (not reset when caught)",
  16. /* SIGIOT */    "IOT instruction",
  17. /* SIGEMT */    "EMT instruction",
  18. /* SIGFPE */    "floating point exception",
  19. /* SIGKILL */    "kill (cannot be caught or ignored)",
  20. /* SIGBUS */    "bus error",
  21. /* SIGSEGV */    "segmentation violation",
  22. /* SIGSYS */    "bad argument to system call",
  23. /* SIGPIPE */    "write on a pipe with no one to read it",
  24. /* SIGALRM */    "alarm clock",
  25. /* SIGTERM */    "software termination signal from kill",
  26. /* SIGUSR1 */    "user defined signal 1",
  27. /* SIGUSR2 */    "user defined signal 2",
  28. /* SIGCLD */    "death of a child",
  29. /* SIGPWR */    "power-fail restart"
  30. };
  31. #endif SYSV
  32.  
  33. #ifdef SUNTOOL
  34. msgwin_handlesigwinch()
  35. {
  36.     register struct rect rect;
  37.     if (exec_pid)
  38.     return;
  39.     rect = msg_rect;
  40.     pw_damaged(msg_win);
  41.     /* this prevents old screen from being lost when editor finishes */
  42.     if (isoff(glob_flags, IS_GETTING))
  43.     gfxsw_interpretesigwinch(msg_sw->ts_data);
  44.     gfxsw_handlesigwinch(msg_sw->ts_data);
  45.     pw_repairretained(msg_win);
  46.     pw_donedamaged(msg_win);
  47.     win_getrect(msg_sw->ts_windowfd, &msg_rect);
  48.     crt = msg_rect.r_height / l_height(curfont);
  49.     if (rect.r_height != msg_rect.r_height || rect.r_width != rect.r_width)
  50.     if (getting_opts == 1)
  51.         display_opts(0);
  52.     else if (getting_opts == 2)
  53.         set_fkeys();
  54.     else if (msg_pix)
  55.         scroll_win(0);
  56. }
  57.  
  58. hdrwin_handlesigwinch()
  59. {
  60.     register struct rect rect;
  61.     rect = hdr_rect;
  62.     pw_damaged(hdr_win);
  63.     gfxsw_interpretesigwinch(hdr_sw->ts_data);
  64.     gfxsw_handlesigwinch(hdr_sw->ts_data);
  65.     pw_repairretained(hdr_win);
  66.     pw_donedamaged(hdr_win);
  67.     win_getrect(hdr_sw->ts_windowfd, &hdr_rect);
  68.     if (rect.r_width != hdr_rect.r_width || rect.r_height != hdr_rect.r_height){
  69.     pw_writebackground(hdr_win, 0,0,
  70.                hdr_rect.r_width, hdr_rect.r_height, PIX_CLR);
  71.     screen = hdr_rect.r_height/l_height(DEFAULT);
  72.     (void) do_hdrs(0, DUBL_NULL, NULL);
  73.     }
  74. }
  75.  
  76. print_sigwinch()
  77. {
  78.     pw_damaged(print_win);
  79.     gfxsw_handlesigwinch(print_sw->ts_data);
  80.     pw_writebackground(print_win, 0,0,
  81.         win_getwidth(print_sw->ts_windowfd),
  82.     win_getheight(print_sw->ts_windowfd), PIX_CLR);
  83.     pw_donedamaged(print_win);
  84.     print(NULL);  /* reprint whatever was there before damage */
  85. }
  86.  
  87. sigwinchcatcher()
  88. {
  89.     tool_sigwinch(tool);
  90. }
  91. #endif SUNTOOL
  92.  
  93. interrupt(sig)
  94. {
  95.     Debug("interrupt() caught: %d\n", sig);
  96.     turnon(glob_flags, WAS_INTR);
  97. }
  98.  
  99. /*
  100.  * catch signals to reset state of the machine.  Always print signal caught.
  101.  * If signals are ignored, return.  If we're running the shell, longjmp back.
  102.  */
  103. /*ARGSUSED*/
  104. catch(sig)
  105. {
  106.     Debug("Caught signal: %d\n", sig);
  107.     (void) signal(sig, catch);
  108.     if (ison(glob_flags, IGN_SIGS) && sig != SIGTERM && sig != SIGHUP)
  109.     return;
  110.     print("%s: %s\n", prog_name, sys_siglist[sig]);
  111.     turnoff(glob_flags, IS_PIPE);
  112.     if (istool || sig == SIGTERM || sig == SIGHUP) {
  113.     istool = 1;
  114.     (void) setjmp(jmpbuf);
  115.     if (ison(glob_flags, IS_GETTING))
  116.         rm_edfile(-1);
  117.     cleanup(sig);
  118.     }
  119.     if (ison(glob_flags, DO_SHELL))
  120.     longjmp(jmpbuf, 1);
  121. }
  122.  
  123. #ifdef SIGCONT
  124. stop_start(sig)
  125. {
  126.     Debug("Caught signal: %d", sig);
  127.     if (sig == SIGCONT) {
  128.     (void) signal(SIGTSTP, stop_start);
  129.     (void) signal(SIGCONT, stop_start);
  130.     if (istool || ison(glob_flags, IGN_SIGS) && !iscurses)
  131.         return;
  132.     echo_off();
  133.     /* we're not in an editor but we're editing a letter */
  134.     if (ison(glob_flags, IS_GETTING)) {
  135.         if (!exec_pid)
  136.         print("(Continue editing letter)\n");
  137.     }
  138. #ifdef CURSES
  139.     else if (iscurses)
  140.         if (ison(glob_flags, IGN_SIGS)) {
  141.         clr_bot_line();
  142.         if (msg_list)
  143.             puts(compose_hdr(current_msg));
  144.         mail_status(1), addstr("...continue... ");
  145.         refresh();
  146.         } else {
  147.         int curlin = max(1, current_msg - n_array[0] + 1);
  148.         redraw();
  149.         print("Continue");
  150.         move(curlin, 0);
  151.         refresh();
  152.         /* make sure we lose reverse video on continuation */
  153.         if (ison(glob_flags, REV_VIDEO) && msg_cnt) {
  154.             char buf[256];
  155.             (void) strncpy(buf, compose_hdr(current_msg), COLS-1);
  156.             buf[COLS-1] = 0; /* strncpy does not null terminate */
  157.             mvaddstr(curlin, 0, buf);
  158.         }
  159.         }
  160. #endif CURSES
  161.       else
  162.         mail_status(1), fflush(stdout);
  163.     } else {
  164. #ifdef CURSES
  165.     if (iscurses) {
  166.         /* when user stops mush, the current header is not in reverse
  167.          * video -- note that a refresh() has not been called in curses.c!
  168.          * so, make sure that when a continue is called, the reverse video
  169.          * for the current message returns.
  170.          */
  171.         if (isoff(glob_flags, IGN_SIGS) && ison(glob_flags, REV_VIDEO) &&
  172.             msg_cnt) {
  173.         int curlin = max(1, current_msg - n_array[0] + 1);
  174.         char buf[256];
  175.         (void) strncpy(buf, stdscr->_y[curlin], COLS-1);
  176.         buf[COLS-1] = 0; /* strncpy does not null terminate */
  177.         STANDOUT(curlin, 0, buf);
  178.         }
  179.         print("Stopping...");
  180.     }
  181. #endif CURSES
  182.     echo_on();
  183.     (void) signal(SIGTSTP, SIG_DFL);
  184.     (void) signal(SIGCONT, stop_start);
  185.     (void) kill(getpid(), sig);
  186.     }
  187. }
  188. #endif SIGCONT
  189.  
  190. /*ARGSUSED*/
  191. cleanup(sig)
  192. {
  193.     char buf[128], c = ison(glob_flags, IGN_SIGS)? 'n' : 'y';
  194.  
  195. #ifdef CURSES
  196.     if (iscurses)
  197.     iscurses = FALSE, endwin();
  198. #endif CURSES
  199.  
  200.     echo_on();
  201.  
  202.     if (ison(glob_flags, IS_GETTING))
  203.     turnoff(glob_flags, IS_GETTING), dead_letter();
  204.     if ((sig == SIGSEGV || sig == SIGBUS) && isoff(glob_flags, IGN_SIGS)
  205.     && *tempfile) {
  206.     fprintf(stderr, "remove %s [y]? ", tempfile), fflush(stderr);
  207.     if (fgets(buf, 128, stdin))
  208.         c = lower(*buf);
  209.     }
  210.     if (c != 'n' && *tempfile && unlink(tempfile) && !sig && errno != ENOENT)
  211.     error(tempfile);
  212. #ifdef SUNTOOL
  213.     if (istool && tool)
  214.     tool_destroy(tool);
  215. #endif SUNTOOL
  216.     if (sig == SIGSEGV || sig == SIGBUS) {
  217.     if (isoff(glob_flags, IGN_SIGS)) {
  218.         fprintf(stderr, "coredump [n]? "), fflush(stderr);
  219.         if (fgets(buf, 128, stdin))
  220.         c = lower(*buf);
  221.     }
  222.     if (c == 'y')
  223.         puts("dumping core for debugging"), abort();
  224.     }
  225.     exit(sig);
  226. }
  227.  
  228. /*
  229.  * if new mail comes in, print who it's from.  sprintf it all into one
  230.  * buffer and print that instead of separate print statements to allow
  231.  * the tool mode to make one print statment. The reason for this is that
  232.  * when the tool is refreshed (caused by a resize, reopen, move, top, etc)
  233.  * the last thing printed is displayed -- display the entire line.
  234.  */
  235. check_new_mail()
  236. {
  237.     int        ret_value;
  238.     char        buf[BUFSIZ], buf2[256];
  239.     register char  *p = buf;
  240.     static long    last_spool_size = -1;
  241.  
  242. #ifdef SUNTOOL
  243.     static int is_iconic, was_iconic;
  244.  
  245.     if (istool) {
  246.     timerclear(&(mail_timer.it_interval));
  247.     timerclear(&(mail_timer.it_value));
  248.     mail_timer.it_value.tv_sec = time_out;
  249.     setitimer(ITIMER_REAL, &mail_timer, NULL);
  250.     }
  251. #endif SUNTOOL
  252.     /* if fullscreen access in progress (help), don't do anything */
  253.     if (ret_value = mail_size()) {
  254. #ifdef CURSES
  255.     int new_hdrs = last_msg_cnt;
  256. #endif CURSES
  257. #ifdef SUNTOOL
  258.     /* if our status has changed from icon to toolform, then
  259.      * there will already be a message stating number of new
  260.      * messages.  reset `n' to msg_cnt so we don't restate
  261.      * the same # of new messages upon receipt of yet another new message.
  262.      */
  263.     if (istool && !(is_iconic = (tool->tl_flags&TOOL_ICONIC)) && was_iconic)
  264.         last_msg_cnt = msg_cnt;
  265. #endif SUNTOOL
  266.     turnon(glob_flags, NEW_MAIL);
  267.     getmail(); /* msg_cnt gets incremented here */
  268.     if (istool) {
  269.         mail_status(0);
  270.         (void) do_hdrs(0, DUBL_NULL, NULL);
  271.     }
  272.     p += Strcpy(p, "New mail ");
  273.     if (msg_cnt - last_msg_cnt <= 1)
  274.         p += strlen(sprintf(p, "(#%d) ", msg_cnt));
  275.     else
  276.         p += strlen(sprintf(p, "(#%d thru #%d)\n", last_msg_cnt+1,msg_cnt));
  277. #ifdef SUNTOOL
  278.     /*
  279.      * If mush is in tool mode and in icon form, don't update last_msg_cnt
  280.      * so that when the tool is opened, print() will print the correct
  281.      * number of "new" messages.
  282.      */
  283.     if (!istool || !(was_iconic = tool->tl_flags & TOOL_ICONIC))
  284. #endif SUNTOOL
  285.         while (last_msg_cnt < msg_cnt) {
  286.         char *p2 = reply_to(last_msg_cnt++, FALSE, buf2);
  287.         if (strlen(p2) + (p - buf) >= BUFSIZ-5) {
  288.             (void) strcat(p, "...\n");
  289.             /* force a loop break by setting last_msg_cnt correctly */
  290.             last_msg_cnt = msg_cnt;
  291.         } else
  292.             p += strlen(sprintf(p, " %s\n", p2));
  293.         }
  294. #ifdef CURSES
  295.     if (iscurses) {
  296.         if (strlen(buf) > COLS-1) {
  297.         printf("%s --more--", buf), fflush(stdout);
  298.         (void) getchar();
  299.         (void) clear();
  300.         (void) strcpy(buf+COLS-5, " ...");
  301.         new_hdrs = n_array[screen-1]; /* force new headers */
  302.         }
  303.         if (new_hdrs - n_array[screen-1] < screen)
  304.         (void) do_hdrs(0, DUBL_NULL, NULL);
  305.     }
  306. #endif CURSES
  307.     print("%s", buf); /* buf might have %'s in them!!! */
  308.     } else
  309. #ifdef SUNTOOL
  310.     if (!istool || !is_iconic)
  311. #endif SUNTOOL
  312.         turnoff(glob_flags, NEW_MAIL);
  313.     if (last_spool_size > -1 && /* handle first case */
  314.         !is_spool(mailfile) && last_spool_size < spool_size)
  315.     print("You have new mail in your system mailbox.\n"), ret_value = 1;
  316.     last_spool_size = spool_size;
  317.     return ret_value;
  318. }
  319.  
  320. /*ARGSUSED*/   /* we ignore the sigstack, cpu-usage, etc... */
  321. bus_n_seg(sig)
  322. {
  323.     fprintf(stderr, "%s: %s\n", prog_name,
  324.     (sig == SIGSEGV)? "Segmentation violation": "Bus error");
  325.     cleanup(sig);
  326. }
  327.