home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / lynx2.8.1dev.10.tar.gz / lynx2.8.1dev.10.tar / lynx2-8 / src / LYClean.c < prev    next >
C/C++ Source or Header  |  1998-05-10  |  4KB  |  204 lines

  1. #include <HTUtils.h>
  2. #include <tcp.h>
  3. #include <LYCurses.h>
  4. #include <LYUtils.h>
  5. #include <LYSignal.h>
  6. #include <LYClean.h>
  7. #include <LYMainLoop.h>
  8. #include <LYGlobalDefs.h>
  9. #include <LYStrings.h>
  10. #include <LYTraversal.h>
  11. #include <UCAuto.h>
  12.  
  13. #include <LYexit.h>
  14. #include <LYLeaks.h>
  15.  
  16. #define FREE(x) if (x) {free(x); x = NULL;}
  17.  
  18. #ifdef VMS
  19. BOOLEAN HadVMSInterrupt = FALSE;
  20. #endif /* VMS */
  21.  
  22. /*
  23.  *  Interrupt handler.    Stop curses and exit gracefully.
  24.  */
  25. PUBLIC void cleanup_sig ARGS1(
  26.     int,        sig)
  27. {
  28.  
  29. #ifdef IGNORE_CTRL_C
  30.     if (sig == SIGINT)    {
  31.     /*
  32.      *    Need to rearm the signal.
  33.      */
  34.     signal(SIGINT, cleanup_sig);
  35.     sigint = TRUE;
  36.     return;
  37.     }
  38. #endif /* IGNORE_CTRL_C */
  39.  
  40. #ifdef VMS
  41.     if (!dump_output_immediately) {
  42.     int c;
  43.  
  44.     /*
  45.      *  Reassert the AST.
  46.      */
  47.     (void) signal(SIGINT, cleanup_sig);
  48.     HadVMSInterrupt = TRUE;
  49.     if (!LYCursesON)
  50.         return;
  51.  
  52.     /*
  53.      *  Refresh screen to get rid of "cancel" message, then query.
  54.      */
  55.     lynx_force_repaint();
  56.     refresh();
  57.  
  58.     /*
  59.      *  Ask if exit is intended.
  60.      */
  61.     if (LYQuitDefaultYes == TRUE) {
  62.         _statusline(REALLY_EXIT_Y);
  63.     } else {
  64.         _statusline(REALLY_EXIT_N);
  65.     }
  66.     c = LYgetch();
  67.     if (LYQuitDefaultYes == TRUE) {
  68.         if (TOUPPER(c) == 'N' ||
  69.         c == 7) {
  70.         return;
  71.         }
  72.     } else if (TOUPPER(c) != 'Y') {
  73.         return;
  74.     }
  75.     }
  76. #endif /* VMS */
  77.  
  78.     /*
  79.      *    Ignore further interrupts. - mhc: 11/2/91
  80.      */
  81. #ifndef NOSIGHUP
  82.     (void) signal(SIGHUP, SIG_IGN);
  83. #endif /* NOSIGHUP */
  84.  
  85. #ifdef VMS
  86.     /*
  87.      *    Use ttclose() from cleanup() for VMS if not dumping.
  88.      */
  89.     if (dump_output_immediately)
  90. #else /* Unix: */
  91.     (void) signal(SIGINT, SIG_IGN);
  92. #endif /* VMS */
  93.  
  94.     (void) signal(SIGTERM, SIG_IGN);
  95.  
  96.     if (traversal)
  97.     dump_traversal_history();
  98.  
  99. #ifndef NOSIGHUP
  100.     if (sig != SIGHUP) {
  101. #endif /* NOSIGHUP */
  102.  
  103.     if (!dump_output_immediately) {
  104.         /*
  105.          *    cleanup() also calls cleanup_files().
  106.          */
  107.         cleanup();
  108.     }
  109.     if (sig != 0) {
  110.         printf("\r\nExiting via interrupt: %d\r\n", sig);
  111.         fflush(stdout);
  112.     }
  113. #ifndef NOSIGHUP
  114.     } else {
  115.     cleanup_files();
  116.     }
  117. #endif /* NOSIGHUP */
  118.  
  119. #ifndef NOSIGHUP
  120.      (void) signal(SIGHUP, SIG_DFL);
  121. #endif /* NOSIGHUP */
  122.     (void) signal(SIGTERM, SIG_DFL);
  123. #ifndef VMS
  124.     (void) signal(SIGINT, SIG_DFL);
  125. #endif /* !VMS */
  126. #ifdef SIGTSTP
  127.     if (no_suspend)
  128.     (void) signal(SIGTSTP, SIG_DFL);
  129. #endif /* SIGTSTP */
  130.     if (sig != 0) {
  131.     exit(0);
  132.     }
  133. }
  134.  
  135. /*
  136.  *  Called by Interrupt handler or at quit time.
  137.  *  Erases the temporary files that lynx created
  138.  *  temporary files are removed by tempname
  139.  *  which created them.
  140.  */
  141. PUBLIC void cleanup_files NOARGS
  142. {
  143.     char filename[256];
  144.  
  145.     tempname(filename, REMOVE_FILES);
  146.     FREE(lynx_temp_space);
  147. }
  148.  
  149. PUBLIC void cleanup NOARGS
  150. {
  151.     int i;
  152. #ifdef VMS
  153.     extern BOOLEAN DidCleanup;
  154. #endif /* VMS */
  155.  
  156.     /*
  157.      *    Cleanup signals - just in case.
  158.      *    Ignore further interrupts. - mhc: 11/2/91
  159.      */
  160. #ifndef NOSIGHUP
  161.     (void) signal(SIGHUP, SIG_IGN);
  162. #endif /* NOSIGHUP */
  163.     (void) signal (SIGTERM, SIG_IGN);
  164.  
  165. #ifndef VMS  /* use ttclose() from cleanup() for VMS */
  166.     (void) signal (SIGINT, SIG_IGN);
  167. #endif /* !VMS */
  168.  
  169.     if (LYCursesON) {
  170.     move(LYlines-1, 0);
  171.     clrtoeol();
  172.  
  173.     lynx_stop_all_colors ();
  174.     refresh();
  175.  
  176.     stop_curses();
  177.     }
  178.  
  179. #ifdef EXP_CHARTRANS_AUTOSWITCH
  180. #ifdef LINUX
  181.     /*
  182.      *    Currently implemented only for LINUX: Restore original font.
  183.      */
  184.     UCChangeTerminalCodepage(-1, (LYUCcharset*)0);
  185. #endif /* LINUX */
  186. #endif /* EXP_CHARTRANS_AUTOSWITCH */
  187.  
  188.     cleanup_files();
  189.     for (i = 0; i < nhist; i++) {
  190.     FREE(history[i].title);
  191.     FREE(history[i].address);
  192.     FREE(history[i].post_data);
  193.     FREE(history[i].post_content_type);
  194.     FREE(history[i].bookmark);
  195.     }
  196.     nhist = 0;
  197. #ifdef VMS
  198.     ttclose();
  199.     DidCleanup = TRUE;
  200. #endif /* VMS */
  201.  
  202.     LYCloseTracelog();
  203. }
  204.