BSD 4_3_Net_2 release
[unix-history] / usr / src / lib / libcurses / newwin.c
index c182d73..fc1ae4a 100644 (file)
@@ -1,13 +1,50 @@
+/*
+ * Copyright (c) 1981 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef lint
+static char sccsid[] = "@(#)newwin.c   5.4 (Berkeley) 6/1/90";
+#endif /* not lint */
+
 /*
  * allocate space for and set up defaults for a new window
  *
 /*
  * allocate space for and set up defaults for a new window
  *
- * %G% (Berkeley) %W
  */
 
 # include      "curses.ext"
 
  */
 
 # include      "curses.ext"
 
-short  *calloc();
-WINDOW *malloc();
+char   *malloc();
+
+# define       SMALLOC (short *) malloc
 
 static WINDOW  *makenew();
 
 
 static WINDOW  *makenew();
 
@@ -20,6 +57,7 @@ int   num_lines, num_cols, begy, begx;
        reg WINDOW      *win;
        reg char        *sp;
        reg int         i, by, bx, nl, nc;
        reg WINDOW      *win;
        reg char        *sp;
        reg int         i, by, bx, nl, nc;
+       reg int         j;
 
        by = begy;
        bx = begx;
 
        by = begy;
        bx = begx;
@@ -32,33 +70,50 @@ int num_lines, num_cols, begy, begx;
                nc = COLS - bx;
        if ((win = makenew(nl, nc, by, bx)) == NULL)
                return ERR;
                nc = COLS - bx;
        if ((win = makenew(nl, nc, by, bx)) == NULL)
                return ERR;
+       if ((win->_firstch = SMALLOC(nl * sizeof win->_firstch[0])) == NULL) {
+               free(win->_y);
+               free(win);
+               return NULL;
+       }
+       if ((win->_lastch = SMALLOC(nl * sizeof win->_lastch[0])) == NULL) {
+               free(win->_y);
+               free(win->_firstch);
+               free(win);
+               return NULL;
+       }
+       win->_nextp = win;
+       for (i = 0; i < nl; i++) {
+               win->_firstch[i] = _NOCHANGE;
+               win->_lastch[i] = _NOCHANGE;
+       }
        for (i = 0; i < nl; i++)
        for (i = 0; i < nl; i++)
-               if ((win->_y[i] = (char *) calloc(nc, sizeof (char))) == NULL) {
-                       reg int         j;
-
+               if ((win->_y[i] = malloc(nc * sizeof win->_y[0])) == NULL) {
                        for (j = 0; j < i; j++)
                        for (j = 0; j < i; j++)
-                               cfree(win->_y[j]);
-                       cfree(win->_firstch);
-                       cfree(win->_lastch);
-                       cfree(win->_y);
-                       cfree(win);
+                               free(win->_y[j]);
+                       free(win->_firstch);
+                       free(win->_lastch);
+                       free(win->_y);
+                       free(win);
                        return ERR;
                }
                else
                        for (sp = win->_y[i]; sp < win->_y[i] + nc; )
                                *sp++ = ' ';
                        return ERR;
                }
                else
                        for (sp = win->_y[i]; sp < win->_y[i] + nc; )
                                *sp++ = ' ';
+       win->_ch_off = 0;
+# ifdef DEBUG
+       fprintf(outf, "NEWWIN: win->_ch_off = %d\n", win->_ch_off);
+# endif
        return win;
 }
 
 WINDOW *
 subwin(orig, num_lines, num_cols, begy, begx)
 reg WINDOW     *orig;
        return win;
 }
 
 WINDOW *
 subwin(orig, num_lines, num_cols, begy, begx)
 reg WINDOW     *orig;
-int            num_lines, num_cols, begy, begx; {
-
+int            num_lines, num_cols, begy, begx;
+{
        reg int         i;
        reg WINDOW      *win;
        reg int         by, bx, nl, nc;
        reg int         i;
        reg WINDOW      *win;
        reg int         by, bx, nl, nc;
-       reg int         j, k;
 
        by = begy;
        bx = begx;
 
        by = begy;
        bx = begx;
@@ -81,14 +136,34 @@ int                num_lines, num_cols, begy, begx; {
                nc = orig->_maxx + orig->_begx - bx;
        if ((win = makenew(nl, nc, by, bx)) == NULL)
                return ERR;
                nc = orig->_maxx + orig->_begx - bx;
        if ((win = makenew(nl, nc, by, bx)) == NULL)
                return ERR;
-       j = by - orig->_begy;
-       k = bx - orig->_begx;
-       for (i = 0; i < nl; i++)
-               win->_y[i] = &orig->_y[j++][k];
-       win->_flags = _SUBWIN;
+       win->_nextp = orig->_nextp;
+       orig->_nextp = win;
+       win->_orig = orig;
+       _set_subwin_(orig, win);
        return win;
 }
 
        return win;
 }
 
+/*
+ * this code is shared with mvwin()
+ */
+_set_subwin_(orig, win)
+register WINDOW        *orig, *win;
+{
+       register int    i, j, k;
+
+       j = win->_begy - orig->_begy;
+       k = win->_begx - orig->_begx;
+       win->_ch_off = k;
+# ifdef DEBUG
+       fprintf(outf, "_SET_SUBWIN_: win->_ch_off = %d\n", win->_ch_off);
+# endif
+       win->_firstch = &orig->_firstch[j];
+       win->_lastch = &orig->_lastch[j];
+       for (i = 0; i < win->_maxy; i++, j++)
+               win->_y[i] = &orig->_y[j][k];
+
+}
+
 /*
  *     This routine sets up a window buffer and returns a pointer to it.
  */
 /*
  *     This routine sets up a window buffer and returns a pointer to it.
  */
@@ -108,46 +183,27 @@ int       num_lines, num_cols, begy, begx; {
 # ifdef        DEBUG
        fprintf(outf, "MAKENEW(%d, %d, %d, %d)\n", nl, nc, by, bx);
 # endif
 # ifdef        DEBUG
        fprintf(outf, "MAKENEW(%d, %d, %d, %d)\n", nl, nc, by, bx);
 # endif
-       if ((win = (WINDOW *) calloc(1, sizeof (WINDOW))) == NULL)
+       if ((win = (WINDOW *) malloc(sizeof *win)) == NULL)
                return NULL;
 # ifdef DEBUG
        fprintf(outf, "MAKENEW: nl = %d\n", nl);
 # endif
                return NULL;
 # ifdef DEBUG
        fprintf(outf, "MAKENEW: nl = %d\n", nl);
 # endif
-       if ((win->_y = (char **) calloc(nl, sizeof (char *))) == NULL) {
-               cfree(win);
-               return NULL;
-       }
-       if ((win->_firstch = calloc(nl, sizeof (short))) == NULL) {
-               cfree(win);
-               cfree(win->_y);
-               return NULL;
-       }
-       if ((win->_lastch = calloc(nl, sizeof (short))) == NULL) {
-               cfree(win);
-               cfree(win->_y);
-               cfree(win->_firstch);
+       if ((win->_y = (char **) malloc(nl * sizeof win->_y[0])) == NULL) {
+               free(win);
                return NULL;
        }
 # ifdef DEBUG
        fprintf(outf, "MAKENEW: nc = %d\n", nc);
 # endif
        win->_cury = win->_curx = 0;
                return NULL;
        }
 # ifdef DEBUG
        fprintf(outf, "MAKENEW: nc = %d\n", nc);
 # endif
        win->_cury = win->_curx = 0;
-       win->_clear = (nl == LINES && nc == COLS);
+       win->_clear = FALSE;
        win->_maxy = nl;
        win->_maxx = nc;
        win->_begy = by;
        win->_begx = bx;
        win->_flags = 0;
        win->_scroll = win->_leave = FALSE;
        win->_maxy = nl;
        win->_maxx = nc;
        win->_begy = by;
        win->_begx = bx;
        win->_flags = 0;
        win->_scroll = win->_leave = FALSE;
-       for (i = 0; i < nl; i++)
-               win->_firstch[i] = win->_lastch[i] = _NOCHANGE;
-       if (bx + nc == COLS) {
-               win->_flags |= _ENDLINE;
-               if (bx == 0 && nl == LINES && by == 0)
-                       win->_flags |= _FULLWIN;
-               if (by + nl == LINES)
-                       win->_flags |= _SCROLLWIN;
-       }
+       _swflags_(win);
 # ifdef DEBUG
        fprintf(outf, "MAKENEW: win->_clear = %d\n", win->_clear);
        fprintf(outf, "MAKENEW: win->_leave = %d\n", win->_leave);
 # ifdef DEBUG
        fprintf(outf, "MAKENEW: win->_clear = %d\n", win->_clear);
        fprintf(outf, "MAKENEW: win->_leave = %d\n", win->_leave);
@@ -160,3 +216,20 @@ int        num_lines, num_cols, begy, begx; {
 # endif
        return win;
 }
 # endif
        return win;
 }
+
+_swflags_(win)
+register WINDOW        *win;
+{
+       win->_flags &= ~(_ENDLINE|_FULLLINE|_FULLWIN|_SCROLLWIN);
+       if (win->_begx + win->_maxx == COLS) {
+               win->_flags |= _ENDLINE;
+               if (win->_begx == 0) {
+                       if (AL && DL)
+                               win->_flags |= _FULLLINE;
+                       if (win->_maxy == LINES && win->_begy == 0)
+                               win->_flags |= _FULLWIN;
+               }
+               if (win->_begy + win->_maxy == LINES)
+                       win->_flags |= _SCROLLWIN;
+       }
+}