]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/tinfo/lib_ttyflags.c
ncurses 5.6 - patch 20080621
[ncurses.git] / ncurses / tinfo / lib_ttyflags.c
index 77c35fa5842677f90c2af535858a5ecc01c425c0..0435bc9f0ce9e707b65c0d482798525a545c0e1c 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998-2005,2006 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc.              *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
 #include <curses.priv.h>
 #include <term.h>              /* cur_term */
 
-MODULE_ID("$Id: lib_ttyflags.c,v 1.13 2006/12/10 01:31:54 tom Exp $")
+MODULE_ID("$Id: lib_ttyflags.c,v 1.17 2008/06/21 20:53:06 tom Exp $")
 
 NCURSES_EXPORT(int)
 _nc_get_tty_mode(TTY * buf)
 {
     int result = OK;
 
-    if (cur_term == 0) {
+    if (buf == 0) {
        result = ERR;
     } else {
-       for (;;) {
-           if (GET_TTY(cur_term->Filedes, buf) != 0) {
-               if (errno == EINTR)
-                   continue;
-               result = ERR;
+       if (cur_term == 0) {
+           result = ERR;
+       } else {
+           for (;;) {
+               if (GET_TTY(cur_term->Filedes, buf) != 0) {
+                   if (errno == EINTR)
+                       continue;
+                   result = ERR;
+               }
+               break;
            }
-           break;
        }
-    }
 
-    if (result == ERR)
-       memset(buf, 0, sizeof(*buf));
+       if (result == ERR)
+           memset(buf, 0, sizeof(*buf));
 
-    TR(TRACE_BITS, ("_nc_get_tty_mode(%d): %s",
-                   cur_term->Filedes, _nc_trace_ttymode(buf)));
+       TR(TRACE_BITS, ("_nc_get_tty_mode(%d): %s",
+                       cur_term->Filedes, _nc_trace_ttymode(buf)));
+    }
     return (result);
 }
 
@@ -71,63 +75,75 @@ _nc_set_tty_mode(TTY * buf)
 {
     int result = OK;
 
-    if (cur_term == 0) {
+    if (buf == 0) {
        result = ERR;
     } else {
-       for (;;) {
-           if (SET_TTY(cur_term->Filedes, buf) != 0) {
-               if (errno == EINTR)
-                   continue;
-               if ((errno == ENOTTY) && (SP != 0))
-                   SP->_notty = TRUE;
-               result = ERR;
+       if (cur_term == 0) {
+           result = ERR;
+       } else {
+           for (;;) {
+               if (SET_TTY(cur_term->Filedes, buf) != 0) {
+                   if (errno == EINTR)
+                       continue;
+                   if ((errno == ENOTTY) && (SP != 0))
+                       SP->_notty = TRUE;
+                   result = ERR;
+               }
+               break;
            }
-           break;
        }
+       TR(TRACE_BITS, ("_nc_set_tty_mode(%d): %s",
+                       cur_term->Filedes, _nc_trace_ttymode(buf)));
     }
-    TR(TRACE_BITS, ("_nc_set_tty_mode(%d): %s",
-                   cur_term->Filedes, _nc_trace_ttymode(buf)));
     return (result);
 }
 
 NCURSES_EXPORT(int)
 def_shell_mode(void)
 {
-    T((T_CALLED("def_shell_mode()")));
+    int rc = ERR;
 
-    /*
-     * If XTABS was on, remove the tab and backtab capabilities.
-     */
+    T((T_CALLED("def_shell_mode()")));
 
-    if (_nc_get_tty_mode(&cur_term->Ottyb) != OK)
-       returnCode(ERR);
+    if (cur_term != 0) {
+       /*
+        * If XTABS was on, remove the tab and backtab capabilities.
+        */
+       if (_nc_get_tty_mode(&cur_term->Ottyb) == OK) {
 #ifdef TERMIOS
-    if (cur_term->Ottyb.c_oflag & OFLAGS_TABS)
-       tab = back_tab = NULL;
+           if (cur_term->Ottyb.c_oflag & OFLAGS_TABS)
+               tab = back_tab = NULL;
 #else
-    if (cur_term->Ottyb.sg_flags & XTABS)
-       tab = back_tab = NULL;
+           if (cur_term->Ottyb.sg_flags & XTABS)
+               tab = back_tab = NULL;
 #endif
-    returnCode(OK);
+           rc = OK;
+       }
+    }
+    returnCode(rc);
 }
 
 NCURSES_EXPORT(int)
 def_prog_mode(void)
 {
-    T((T_CALLED("def_prog_mode()")));
+    int rc = ERR;
 
-    /*
-     * Turn off the XTABS bit in the tty structure if it was on.
-     */
+    T((T_CALLED("def_prog_mode()")));
 
-    if (_nc_get_tty_mode(&cur_term->Nttyb) != OK)
-       returnCode(ERR);
+    if (cur_term != 0) {
+       /*
+        * Turn off the XTABS bit in the tty structure if it was on.
+        */
+       if (_nc_get_tty_mode(&cur_term->Nttyb) == OK) {
 #ifdef TERMIOS
-    cur_term->Nttyb.c_oflag &= ~OFLAGS_TABS;
+           cur_term->Nttyb.c_oflag &= ~OFLAGS_TABS;
 #else
-    cur_term->Nttyb.sg_flags &= ~XTABS;
+           cur_term->Nttyb.sg_flags &= ~XTABS;
 #endif
-    returnCode(OK);
+           rc = OK;
+       }
+    }
+    returnCode(rc);
 }
 
 NCURSES_EXPORT(int)
@@ -139,7 +155,7 @@ reset_prog_mode(void)
        if (_nc_set_tty_mode(&cur_term->Nttyb) == OK) {
            if (SP) {
                if (SP->_keypad_on)
-                   _nc_keypad(TRUE);
+                   _nc_keypad(SP, TRUE);
                NC_BUFFERED(TRUE);
            }
            returnCode(OK);
@@ -155,7 +171,7 @@ reset_shell_mode(void)
 
     if (cur_term != 0) {
        if (SP) {
-           _nc_keypad(FALSE);
+           _nc_keypad(SP, FALSE);
            _nc_flush();
            NC_BUFFERED(FALSE);
        }
@@ -164,19 +180,33 @@ reset_shell_mode(void)
     returnCode(ERR);
 }
 
+static TTY *
+saved_tty(void)
+{
+    TTY *result = 0;
+
+    if (SP != 0) {
+       result = &(SP->_saved_tty);
+    } else {
+       if (_nc_prescreen.saved_tty == 0) {
+           _nc_prescreen.saved_tty = typeCalloc(TTY, 1);
+       }
+       result = _nc_prescreen.saved_tty;
+    }
+    return result;
+}
+
 /*
 **     savetty()  and  resetty()
 **
 */
 
-static TTY buf;
-
 NCURSES_EXPORT(int)
 savetty(void)
 {
     T((T_CALLED("savetty()")));
 
-    returnCode(_nc_get_tty_mode(&buf));
+    returnCode(_nc_get_tty_mode(saved_tty()));
 }
 
 NCURSES_EXPORT(int)
@@ -184,5 +214,5 @@ resetty(void)
 {
     T((T_CALLED("resetty()")));
 
-    returnCode(_nc_set_tty_mode(&buf));
+    returnCode(_nc_set_tty_mode(saved_tty()));
 }