home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume14 / mush6.0 / part03 / signals.c < prev   
Encoding:
C/C++ Source or Header  |  1988-04-12  |  9.4 KB  |  330 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.     if (istool) /* istool is 2 if tool is complete */
  114.         istool = 1;
  115.     (void) setjmp(jmpbuf);
  116.     if (ison(glob_flags, IS_GETTING))
  117.         rm_edfile(-1);
  118.     cleanup(sig);
  119.     }
  120.     if (ison(glob_flags, DO_SHELL)) {
  121.     turnoff(glob_flags, IS_GETTING);
  122.     longjmp(jmpbuf, 1);
  123.     } else
  124.     puts("exiting"), cleanup(sig);
  125. }
  126.  
  127. #ifdef SIGCONT
  128. stop_start(sig)
  129. {
  130.     extern FILE *ed_fp;
  131.  
  132.     Debug("Caught signal: %d", sig);
  133.     if (sig == SIGCONT) {
  134.     (void) signal(SIGTSTP, stop_start);
  135.     (void) signal(SIGCONT, stop_start);
  136.     echo_off();
  137.     if (istool || ison(glob_flags, IGN_SIGS) && !iscurses)
  138.         return;
  139.     /* we're not in an editor but we're editing a letter */
  140.     if (ison(glob_flags, IS_GETTING)) {
  141.         if (ed_fp)
  142.         print("(Continue editing letter)\n");
  143.     }
  144. #ifdef CURSES
  145.     else if (iscurses)
  146.         if (ison(glob_flags, IGN_SIGS)) {
  147.         clr_bot_line();
  148.         if (msg_list)
  149.             puts(compose_hdr(current_msg));
  150.         mail_status(1), addstr("...continue... ");
  151.         refresh();
  152.         } else {
  153.         int curlin = max(1, current_msg - n_array[0] + 1);
  154.         redraw();
  155.         print("Continue");
  156.         move(curlin, 0);
  157.         refresh();
  158.         /* make sure we lose reverse video on continuation */
  159.         if (ison(glob_flags, REV_VIDEO) && msg_cnt) {
  160.             char buf[256];
  161.             (void) strncpy(buf, compose_hdr(current_msg), COLS-1);
  162.             buf[COLS-1] = 0; /* strncpy does not null terminate */
  163.             mvaddstr(curlin, 0, buf);
  164.         }
  165.         }
  166. #endif /* CURSES */
  167.       else
  168.         mail_status(1), fflush(stdout);
  169.     } else {
  170. #ifdef CURSES
  171.     if (iscurses) {
  172.         /* when user stops mush, the current header is not in reverse
  173.          * video -- note that a refresh() has not been called in curses.c!
  174.          * so, make sure that when a continue is called, the reverse video
  175.          * for the current message returns.
  176.          */
  177.         turnon(glob_flags, WAS_INTR);
  178.         if (isoff(glob_flags, IGN_SIGS) && ison(glob_flags, REV_VIDEO) &&
  179.             msg_cnt) {
  180.         int curlin = max(1, current_msg - n_array[0] + 1);
  181.         char buf[256];
  182.         (void) strncpy(buf, stdscr->_y[curlin], COLS-1);
  183.         buf[COLS-1] = 0; /* strncpy does not null terminate */
  184.         STANDOUT(curlin, 0, buf);
  185.         }
  186.         print("Stopping...");
  187.     }
  188. #endif /* CURSES */
  189.     echo_on();
  190.     (void) signal(SIGTSTP, SIG_DFL);
  191.     (void) signal(SIGCONT, stop_start);
  192.     (void) kill(getpid(), sig);
  193.     }
  194. }
  195. #endif /* SIGCONT */
  196.  
  197. /*ARGSUSED*/
  198. cleanup(sig)
  199. {
  200.     char buf[128], c = ison(glob_flags, IGN_SIGS)? 'n' : 'y';
  201.  
  202. #ifdef CURSES
  203.     if (iscurses)
  204.     iscurses = FALSE, endwin();
  205. #endif /* CURSES */
  206.  
  207.     echo_on();
  208.  
  209.     if (ison(glob_flags, IS_GETTING))
  210.     turnoff(glob_flags, IS_GETTING), dead_letter();
  211.     if ((sig == SIGSEGV || sig == SIGBUS) && isoff(glob_flags, IGN_SIGS)
  212.     && *tempfile) {
  213.     fprintf(stderr, "remove %s [y]? ", tempfile), fflush(stderr);
  214.     if (fgets(buf, 128, stdin))
  215.         c = lower(*buf);
  216.     }
  217.     if (c != 'n' && *tempfile && unlink(tempfile) && !sig && errno != ENOENT)
  218.     error(tempfile);
  219. #ifdef SUNTOOL
  220.     if (istool && tool)
  221.     tool_destroy(tool);
  222. #endif /* SUNTOOL */
  223.     if (sig == SIGSEGV || sig == SIGBUS) {
  224.     if (isoff(glob_flags, IGN_SIGS)) {
  225.         fprintf(stderr, "coredump [n]? "), fflush(stderr);
  226.         if (fgets(buf, 128, stdin))
  227.         c = lower(*buf);
  228.     }
  229.     if (c == 'y')
  230.         puts("dumping core for debugging"), abort();
  231.     }
  232.     exit(sig);
  233. }
  234.  
  235. /*
  236.  * if new mail comes in, print who it's from.  sprintf it all into one
  237.  * buffer and print that instead of separate print statements to allow
  238.  * the tool mode to make one print statment. The reason for this is that
  239.  * when the tool is refreshed (caused by a resize, reopen, move, top, etc)
  240.  * the last thing printed is displayed -- display the entire line.
  241.  */
  242. check_new_mail()
  243. {
  244.     int        ret_value;
  245.     char        buf[BUFSIZ];
  246.     register char  *p = buf;
  247.     static long    last_spool_size = -1;
  248.  
  249. #ifdef SUNTOOL
  250.     static int is_iconic, was_iconic;
  251.  
  252.     if (istool) {
  253.     timerclear(&(mail_timer.it_interval));
  254.     timerclear(&(mail_timer.it_value));
  255.     mail_timer.it_value.tv_sec = time_out;
  256.     setitimer(ITIMER_REAL, &mail_timer, NULL);
  257.     }
  258. #endif /* SUNTOOL */
  259.     /* if fullscreen access in progress (help), don't do anything */
  260.     if (ret_value = mail_size()) {
  261. #ifdef CURSES
  262.     int new_hdrs = last_msg_cnt;
  263. #endif /* CURSES */
  264. #ifdef SUNTOOL
  265.     /* if our status has changed from icon to toolform, then
  266.      * there will already be a message stating number of new
  267.      * messages.  reset `n' to msg_cnt so we don't restate
  268.      * the same # of new messages upon receipt of yet another new message.
  269.      */
  270.     if (istool && !(is_iconic = (tool->tl_flags&TOOL_ICONIC)) && was_iconic)
  271.         last_msg_cnt = msg_cnt;
  272. #endif /* SUNTOOL */
  273.     turnon(glob_flags, NEW_MAIL);
  274.     getmail(); /* msg_cnt gets incremented here */
  275.     if (istool) {
  276.         mail_status(0);
  277.         (void) do_hdrs(0, DUBL_NULL, NULL);
  278.     }
  279.     p += Strcpy(p, "New mail ");
  280.     if (msg_cnt - last_msg_cnt <= 1)
  281.         p += strlen(sprintf(p, "(#%d) ", msg_cnt));
  282.     else
  283.         p += strlen(sprintf(p, "(#%d thru #%d)\n", last_msg_cnt+1,msg_cnt));
  284. #ifdef SUNTOOL
  285.     /*
  286.      * If mush is in tool mode and in icon form, don't update
  287.      * last_msg_cnt so that when the tool is opened, print() will
  288.      * print the correct number of "new" messages.
  289.      */
  290.     if (!istool || !(was_iconic = tool->tl_flags & TOOL_ICONIC))
  291. #endif /* SUNTOOL */
  292.         if (iscurses && isoff(glob_flags, CNTD_CMD))
  293.         last_msg_cnt = msg_cnt;
  294.         else while (last_msg_cnt < msg_cnt) {
  295.         char *p2 = compose_hdr(last_msg_cnt++) + 9;
  296.         if (strlen(p2) + (p - buf) >= BUFSIZ-5) {
  297.             (void) strcat(p, "...\n");
  298.             /* force a break by setting last_msg_cnt correctly */
  299.             last_msg_cnt = msg_cnt;
  300.         } else
  301.             p += strlen(sprintf(p, " %s\n", p2));
  302.         }
  303. #ifdef CURSES
  304.     if (iscurses && isoff(glob_flags, CNTD_CMD)) {
  305.         if (new_hdrs - n_array[screen-1] < screen)
  306.         (void) do_hdrs(0, DUBL_NULL, NULL);
  307.         print("%s ...", buf);
  308.     } else
  309. #endif /* CURSES */
  310.         print("%s", buf); /* buf might have %'s in them!!! */
  311.     } else
  312. #ifdef SUNTOOL
  313.     if (!istool || !is_iconic)
  314. #endif /* SUNTOOL */
  315.         turnoff(glob_flags, NEW_MAIL);
  316.     if (last_spool_size > -1 && /* handle first case */
  317.         strcmp(mailfile, spoolfile) && last_spool_size < spool_size)
  318.     print("You have new mail in your system mailbox.\n"), ret_value = 1;
  319.     last_spool_size = spool_size;
  320.     return ret_value;
  321. }
  322.  
  323. /*ARGSUSED*/   /* we ignore the sigstack, cpu-usage, etc... */
  324. bus_n_seg(sig)
  325. {
  326.     fprintf(stderr, "%s: %s\n", prog_name,
  327.     (sig == SIGSEGV)? "Segmentation violation": "Bus error");
  328.     cleanup(sig);
  329. }
  330.