Main Page | Class List | Directories | File List | Class Members | File Members

demolib.c

Go to the documentation of this file.
00001 /* These routines are used by several of the demos. */
00002 #include "config.h"
00003 #include <stdio.h>
00004 #include <signal.h>
00005 
00006 #ifdef HAVE_STDLIB_H
00007 # include <stdlib.h>
00008 #endif
00009 
00010 #include <slang.h>
00011 
00012 static void demolib_exit (int sig)
00013 {
00014    SLang_reset_tty ();
00015    SLsmg_reset_smg ();
00016 
00017    if (sig)
00018      {
00019         fprintf (stderr, "Exiting on signal %d\n", sig);
00020         exit (1);
00021      }
00022    exit (sig);
00023 }
00024 
00025 #ifdef SIGTSTP
00026 static void sigtstp_handler (int sig)
00027 {
00028    demolib_exit (sig);
00029 }
00030 #endif
00031 
00032 #ifdef SIGINT
00033 static void sigint_handler (int sig)
00034 {
00035    demolib_exit (sig);
00036 }
00037 #endif
00038 
00039 static void init_signals (void)
00040 {
00041 #ifdef SIGTSTP
00042    SLsignal (SIGTSTP, sigtstp_handler);
00043 #endif
00044 #ifdef SIGINT
00045    SLsignal (SIGINT, sigint_handler);
00046 #endif
00047 }
00048 
00049 static void exit_error_hook (char *fmt, va_list ap)
00050 {
00051    SLang_reset_tty ();
00052    SLsmg_reset_smg ();
00053    
00054    vfprintf (stderr, fmt, ap);
00055    fputc ('\n', stderr);
00056    exit (1);
00057 }
00058 
00059    
00060 
00061 static int demolib_init_terminal (int tty, int smg)
00062 {
00063    SLang_Exit_Error_Hook = exit_error_hook;
00064    
00065    /* It is wise to block the occurance of display  related signals while we are 
00066     * initializing. 
00067     */
00068    SLsig_block_signals ();
00069    
00070    SLtt_get_terminfo ();
00071    
00072    /* SLkp_init assumes that SLtt_get_terminfo has been called. */
00073    if (tty && (-1 == SLkp_init ()))
00074      {
00075         SLsig_unblock_signals ();
00076         return -1;
00077      }
00078    
00079    init_signals ();
00080    
00081    if (tty) SLang_init_tty (-1, 0, 1);
00082 #ifdef REAL_UNIX_SYSTEM
00083    if (tty) SLtty_set_suspend_state (1);
00084 #endif
00085    if (smg)
00086      {
00087         (void) SLutf8_enable (-1);
00088 
00089         if (-1 == SLsmg_init_smg ())
00090           {
00091              SLsig_unblock_signals ();
00092              return -1;
00093           }
00094      }
00095 
00096         
00097    SLsig_unblock_signals ();
00098 
00099    return 0;
00100 }
00101 

© sourcejam.com 2005-2008