]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/lib_options.c
ncurses 4.2
[ncurses.git] / ncurses / lib_options.c
index e98c2d9d5a05d252b6c9c8f03136fb4e6bf827da..926a8aecf805a393bde445b298c42198c24cceb2 100644 (file)
@@ -1,23 +1,35 @@
-
-/***************************************************************************
-*                            COPYRIGHT NOTICE                              *
-****************************************************************************
-*                ncurses is copyright (C) 1992-1995                        *
-*                          Zeyd M. Ben-Halim                               *
-*                          zmbenhal@netcom.com                             *
-*                          Eric S. Raymond                                 *
-*                          esr@snark.thyrsus.com                           *
-*                                                                          *
-*        Permission is hereby granted to reproduce and distribute ncurses  *
-*        by any means and for any fee, whether alone or as part of a       *
-*        larger distribution, in source or in binary form, PROVIDED        *
-*        this notice is included with any such distribution, and is not    *
-*        removed from any of its header files. Mention of ncurses in any   *
-*        applications linked with it is highly appreciated.                *
-*                                                                          *
-*        ncurses comes AS IS with no warranty, implied or expressed.       *
-*                                                                          *
-***************************************************************************/
+/****************************************************************************
+ * Copyright (c) 1998 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            *
+ * "Software"), to deal in the Software without restriction, including      *
+ * without limitation the rights to use, copy, modify, merge, publish,      *
+ * distribute, distribute with modifications, sublicense, and/or sell       *
+ * copies of the Software, and to permit persons to whom the Software is    *
+ * furnished to do so, subject to the following conditions:                 *
+ *                                                                          *
+ * The above copyright notice and this permission notice shall be included  *
+ * in all copies or substantial portions of the Software.                   *
+ *                                                                          *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
+ * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
+ * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
+ *                                                                          *
+ * Except as contained in this notice, the name(s) of the above copyright   *
+ * holders shall not be used in advertising or otherwise to promote the     *
+ * sale, use or other dealings in this Software without prior written       *
+ * authorization.                                                           *
+ ****************************************************************************/
+
+/****************************************************************************
+ *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
+ *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
+ ****************************************************************************/
 
 
 /*
@@ -32,7 +44,7 @@
 #include <term.h>      /* keypad_xmit, keypad_local, meta_on, meta_off */
                        /* cursor_visible,cursor_normal,cursor_invisible */
 
-MODULE_ID("$Id: lib_options.c,v 1.22 1997/05/01 23:46:18 Alexander.V.Lukyanov Exp $")
+MODULE_ID("$Id: lib_options.c,v 1.29 1998/02/11 12:13:55 tom Exp $")
 
 int has_ic(void)
 {
@@ -53,8 +65,12 @@ int idlok(WINDOW *win,  bool flag)
 {
        T((T_CALLED("idlok(%p,%d)"), win, flag));
 
-       _nc_idlok = win->_idlok = flag && (has_il() || change_scroll_region);
-       returnCode(OK);
+       if (win) {
+         _nc_idlok = win->_idlok = flag && (has_il() || change_scroll_region);
+         returnCode(OK);
+       }
+       else
+         returnCode(ERR);
 }
 
 
@@ -62,51 +78,12 @@ void idcok(WINDOW *win, bool flag)
 {
        T((T_CALLED("idcok(%p,%d)"), win, flag));
 
-       _nc_idcok = win->_idcok = flag && has_ic();
+       if (win)
+         _nc_idcok = win->_idcok = flag && has_ic();
 
        returnVoid;
 }
 
-
-int clearok(WINDOW *win, bool flag)
-{
-       T((T_CALLED("clearok(%p,%d)"), win, flag));
-
-       win->_clear = flag;
-       returnCode(OK);
-}
-
-
-void immedok(WINDOW *win, bool flag)
-{
-       T((T_CALLED("immedok(%p,%d)"), win, flag));
-
-       win->_immed = flag;
-
-       returnVoid;
-}
-
-int leaveok(WINDOW *win, bool flag)
-{
-       T((T_CALLED("leaveok(%p,%d)"), win, flag));
-
-       win->_leaveok = flag;
-       if (flag == TRUE)
-               curs_set(0);
-       else
-               curs_set(1);
-       returnCode(OK);
-}
-
-
-int scrollok(WINDOW *win, bool flag)
-{
-       T((T_CALLED("scrollok(%p,%d)"), win, flag));
-
-       win->_scroll = flag;
-       returnCode(OK);
-}
-
 int halfdelay(int t)
 {
        T((T_CALLED("halfdelay(%d)"), t));
@@ -123,69 +100,56 @@ int nodelay(WINDOW *win, bool flag)
 {
        T((T_CALLED("nodelay(%p,%d)"), win, flag));
 
-       if (flag == TRUE)
-               win->_delay = 0;
-       else win->_delay = -1;
-       returnCode(OK);
+       if (win) {
+         if (flag == TRUE)
+           win->_delay = 0;
+         else win->_delay = -1;
+         returnCode(OK);
+       }
+       else
+         returnCode(ERR);
 }
 
 int notimeout(WINDOW *win, bool f)
 {
        T((T_CALLED("notimout(%p,%d)"), win, f));
 
-       win->_notimeout = f;
-       returnCode(OK);
+       if (win) {
+         win->_notimeout = f;
+         returnCode(OK);
+       }
+       else
+         returnCode(ERR);
 }
 
 int wtimeout(WINDOW *win, int delay)
 {
        T((T_CALLED("wtimeout(%p,%d)"), win, delay));
 
-       win->_delay = delay;
-       returnCode(OK);
-}
-
-static void init_keytry(void);
-static void add_to_try(char *, short);
-
-/* Turn the keypad on/off
- *
- * Note:  we flush the output because changing this mode causes some terminals
- * to emit different escape sequences for cursor and keypad keys.  If we don't
- * flush, then the next wgetch may get the escape sequence that corresponds to
- * the terminal state _before_ switching modes.
- */
-int _nc_keypad(bool flag)
-{
-       if (flag  &&  keypad_xmit)
-       {
-           TPUTS_TRACE("keypad_xmit");
-           putp(keypad_xmit);
-           (void) fflush(SP->_ofp);
+       if (win) {
+         win->_delay = delay;
+         returnCode(OK);
        }
-       else if (! flag  &&  keypad_local)
-       {
-           TPUTS_TRACE("keypad_local");
-           putp(keypad_local);
-           (void) fflush(SP->_ofp);
-       }
-
-       if (SP->_keytry == UNINITIALISED)
-           init_keytry();
-       return(OK);
+       else
+         returnCode(ERR);
 }
 
 int keypad(WINDOW *win, bool flag)
 {
        T((T_CALLED("keypad(%p,%d)"), win, flag));
 
-       win->_use_keypad = flag;
-       returnCode(_nc_keypad(flag));
+       if (win) {
+         win->_use_keypad = flag;
+         returnCode(_nc_keypad(flag));
+       }
+       else
+         returnCode(ERR);
 }
 
 
 int meta(WINDOW *win GCC_UNUSED, bool flag)
 {
+        /* Ok, we stay relaxed and don't signal an error if win is NULL */
        T((T_CALLED("meta(%p,%d)"), win, flag));
 
        SP->_use_meta = flag;
@@ -252,121 +216,6 @@ int cursor = SP->_cursor;
        returnCode(cursor==-1 ? 1 : cursor);
 }
 
-/*
-**      init_keytry()
-**
-**      Construct the try for the current terminal's keypad keys.
-**
-*/
-
-
-static struct  tries *newtry;
-
-static void init_keytry(void)
-{
-       newtry = 0;
-
-/* LINT_PREPRO
-#if 0*/
-#include <keys.tries>
-/* LINT_PREPRO
-#endif*/
-
-       SP->_keytry = newtry;
-}
-
-
-static void add_to_try(char *str, short code)
-{
-static bool     out_of_memory = FALSE;
-struct tries    *ptr, *savedptr;
-
-       if (! str  ||  out_of_memory)
-               return;
-
-       if (newtry != 0) {
-               ptr = savedptr = newtry;
-
-               for (;;) {
-                       while (ptr->ch != (unsigned char) *str
-                              &&  ptr->sibling != 0)
-                               ptr = ptr->sibling;
-       
-                       if (ptr->ch == (unsigned char) *str) {
-                               if (*(++str)) {
-                                       if (ptr->child != 0)
-                                               ptr = ptr->child;
-                                       else
-                                               break;
-                               } else {
-                                       ptr->value = code;
-                                       return;
-                               }
-                       } else {
-                               if ((ptr->sibling = typeCalloc(struct tries,1)) == 0) {
-                                       out_of_memory = TRUE;
-                                       return;
-                               }
-
-                               savedptr = ptr = ptr->sibling;
-                               if (*str == '\200')
-                                       ptr->ch = '\0';
-                               else
-                                       ptr->ch = (unsigned char) *str;
-                               str++;
-                               ptr->value = 0;
-
-                               break;
-                       }
-               } /* end for (;;) */
-       } else {   /* newtry == 0 :: First sequence to be added */
-               savedptr = ptr = newtry = typeCalloc(struct tries,1);
-
-               if (ptr == 0) {
-                       out_of_memory = TRUE;
-                               return;
-               }
-
-               if (*str == '\200')
-                       ptr->ch = '\0';
-               else
-                       ptr->ch = (unsigned char) *str;
-               str++;
-               ptr->value = 0;
-       }
-
-           /* at this point, we are adding to the try.  ptr->child == 0 */
-
-       while (*str) {
-               ptr->child = typeCalloc(struct tries,1);
-
-               ptr = ptr->child;
-
-               if (ptr == 0) {
-                       out_of_memory = TRUE;
-
-                       ptr = savedptr;
-                       while (ptr != 0) {
-                               savedptr = ptr->child;
-                               free(ptr);
-                               ptr = savedptr;
-                       }
-
-                       return;
-               }
-
-               if (*str == '\200')
-                       ptr->ch = '\0';
-               else
-                       ptr->ch = (unsigned char) *str;
-               str++;
-               ptr->value = 0;
-       }
-
-       ptr->value = code;
-       return;
-}
-
 int typeahead(int fd)
 {
        T((T_CALLED("typeahead(%d)"), fd));
@@ -384,7 +233,7 @@ int typeahead(int fd)
 
 static int has_key_internal(int keycode, struct tries *tp)
 {
-    if (!tp)
+    if (tp == 0)
        return(FALSE);
     else if (tp->value == keycode)
        return(TRUE);
@@ -398,3 +247,60 @@ int has_key(int keycode)
     T((T_CALLED("has_key(%d)"), keycode));
     returnCode(has_key_internal(keycode, SP->_keytry));
 }
+
+/*
+**      init_keytry()
+**
+**      Construct the try for the current terminal's keypad keys.
+**
+*/
+
+static void init_keytry(void)
+{
+/* LINT_PREPRO
+#if 0*/
+#include <keys.tries>
+/* LINT_PREPRO
+#endif*/
+       size_t n;
+
+       /* The SP->_keytry value is initialized in newterm(), where the SP
+        * structure is created, because we can not tell where keypad() or
+        * mouse_activate() (which will call keyok()) are first called.
+        */
+
+       for (n = 0; n < SIZEOF(table); n++)
+               if (table[n].offset < STRCOUNT)
+               _nc_add_to_try(&(SP->_keytry),
+                       CUR Strings[table[n].offset],
+                       table[n].code);
+}
+
+/* Turn the keypad on/off
+ *
+ * Note:  we flush the output because changing this mode causes some terminals
+ * to emit different escape sequences for cursor and keypad keys.  If we don't
+ * flush, then the next wgetch may get the escape sequence that corresponds to
+ * the terminal state _before_ switching modes.
+ */
+int _nc_keypad(bool flag)
+{
+       if (flag  &&  keypad_xmit)
+       {
+           TPUTS_TRACE("keypad_xmit");
+           putp(keypad_xmit);
+           (void) fflush(SP->_ofp);
+       }
+       else if (! flag  &&  keypad_local)
+       {
+           TPUTS_TRACE("keypad_local");
+           putp(keypad_local);
+           (void) fflush(SP->_ofp);
+       }
+
+       if (!SP->_tried) {
+           init_keytry();
+           SP->_tried = TRUE;
+       }
+       return(OK);
+}