Annotation of 42BSD/usr.lib/libcurses/newwin.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * allocate space for and set up defaults for a new window
                      3:  *
                      4:  * 5/9/83 (Berkeley) @(#)newwin.c      1.6
                      5:  */
                      6: 
                      7: # include      "curses.ext"
                      8: 
                      9: short  *calloc();
                     10: WINDOW *malloc();
                     11: 
                     12: static WINDOW  *makenew();
                     13: 
                     14: # undef                nl      /* don't need it here, and it interferes        */
                     15: 
                     16: WINDOW *
                     17: newwin(num_lines, num_cols, begy, begx)
                     18: int    num_lines, num_cols, begy, begx;
                     19: {
                     20:        reg WINDOW      *win;
                     21:        reg char        *sp;
                     22:        reg int         i, by, bx, nl, nc;
                     23: 
                     24:        by = begy;
                     25:        bx = begx;
                     26:        nl = num_lines;
                     27:        nc = num_cols;
                     28: 
                     29:        if (nl == 0)
                     30:                nl = LINES - by;
                     31:        if (nc == 0)
                     32:                nc = COLS - bx;
                     33:        if ((win = makenew(nl, nc, by, bx)) == NULL)
                     34:                return ERR;
                     35:        for (i = 0; i < nl; i++)
                     36:                if ((win->_y[i] = (char *) calloc(nc, sizeof (char))) == NULL) {
                     37:                        reg int         j;
                     38: 
                     39:                        for (j = 0; j < i; j++)
                     40:                                cfree(win->_y[j]);
                     41:                        cfree(win->_firstch);
                     42:                        cfree(win->_lastch);
                     43:                        cfree(win->_y);
                     44:                        cfree(win);
                     45:                        return ERR;
                     46:                }
                     47:                else
                     48:                        for (sp = win->_y[i]; sp < win->_y[i] + nc; )
                     49:                                *sp++ = ' ';
                     50:        win->_nextp = win;
                     51:        return win;
                     52: }
                     53: 
                     54: WINDOW *
                     55: subwin(orig, num_lines, num_cols, begy, begx)
                     56: reg WINDOW     *orig;
                     57: int            num_lines, num_cols, begy, begx; {
                     58: 
                     59:        reg int         i;
                     60:        reg WINDOW      *win;
                     61:        reg int         by, bx, nl, nc;
                     62:        reg int         j, k;
                     63: 
                     64:        by = begy;
                     65:        bx = begx;
                     66:        nl = num_lines;
                     67:        nc = num_cols;
                     68: 
                     69:        /*
                     70:         * make sure window fits inside the original one
                     71:         */
                     72: # ifdef        DEBUG
                     73:        fprintf(outf, "SUBWIN(%0.2o, %d, %d, %d, %d)\n", orig, nl, nc, by, bx);
                     74: # endif
                     75:        if (by < orig->_begy || bx < orig->_begx
                     76:            || by + nl > orig->_maxy + orig->_begy
                     77:            || bx + nc > orig->_maxx + orig->_begx) {
                     78: # ifdef        DEBUG
                     79:                fprintf(stderr, "returning ERR (1)\n");
                     80:                fprintf(stderr, "SUBWIN(begx = %d, begy = %d,maxx = %d, maxy = %d, nl = %d, nc = %d, by = %d, bx = %d)\n", orig->_begx,orig->_begy,orig->_maxx,orig->_maxy, nl, nc, by, bx);
                     81: # endif
                     82:                return ERR;
                     83:        }
                     84:        if (nl == 0)
                     85:                nl = orig->_maxy + orig->_begy - by;
                     86:        if (nc == 0)
                     87:                nc = orig->_maxx + orig->_begx - bx;
                     88:        if ((win = makenew(nl, nc, by, bx)) == NULL) {
                     89:                fprintf(stderr, "returning ERR (2)\n");
                     90:                return ERR;
                     91:        }
                     92:        j = by - orig->_begy;
                     93:        k = bx - orig->_begx;
                     94:        for (i = 0; i < nl; i++)
                     95:                win->_y[i] = &orig->_y[j++][k];
                     96:        win->_nextp = orig->_nextp;
                     97:        orig->_nextp = win;
                     98:        win->_orig = orig;
                     99:        return win;
                    100: }
                    101: 
                    102: /*
                    103:  *     This routine sets up a window buffer and returns a pointer to it.
                    104:  */
                    105: static WINDOW *
                    106: makenew(num_lines, num_cols, begy, begx)
                    107: int    num_lines, num_cols, begy, begx; {
                    108: 
                    109:        reg int         i;
                    110:        reg WINDOW      *win;
                    111:        reg int         by, bx, nl, nc;
                    112: 
                    113:        by = begy;
                    114:        bx = begx;
                    115:        nl = num_lines;
                    116:        nc = num_cols;
                    117: 
                    118: # ifdef        DEBUG
                    119:        fprintf(outf, "MAKENEW(%d, %d, %d, %d)\n", nl, nc, by, bx);
                    120: # endif
                    121:        if ((win = (WINDOW *) calloc(1, sizeof (WINDOW))) == NULL)
                    122:                return NULL;
                    123: # ifdef DEBUG
                    124:        fprintf(outf, "MAKENEW: nl = %d\n", nl);
                    125: # endif
                    126:        if ((win->_y = (char **) calloc(nl, sizeof (char *))) == NULL) {
                    127:                cfree(win);
                    128:                return NULL;
                    129:        }
                    130:        if ((win->_firstch = calloc(nl, sizeof (short))) == NULL) {
                    131:                cfree(win);
                    132:                cfree(win->_y);
                    133:                return NULL;
                    134:        }
                    135:        if ((win->_lastch = calloc(nl, sizeof (short))) == NULL) {
                    136:                cfree(win);
                    137:                cfree(win->_y);
                    138:                cfree(win->_firstch);
                    139:                return NULL;
                    140:        }
                    141: # ifdef DEBUG
                    142:        fprintf(outf, "MAKENEW: nc = %d\n", nc);
                    143: # endif
                    144:        win->_cury = win->_curx = 0;
                    145:        win->_clear = (nl == LINES && nc == COLS);
                    146:        win->_maxy = nl;
                    147:        win->_maxx = nc;
                    148:        win->_begy = by;
                    149:        win->_begx = bx;
                    150:        win->_flags = 0;
                    151:        win->_scroll = win->_leave = FALSE;
                    152:        for (i = 0; i < nl; i++)
                    153:                win->_firstch[i] = win->_lastch[i] = _NOCHANGE;
                    154:        if (bx + nc == COLS) {
                    155:                win->_flags |= _ENDLINE;
                    156:                if (bx == 0 && nl == LINES && by == 0)
                    157:                        win->_flags |= _FULLWIN;
                    158:                if (by + nl == LINES)
                    159:                        win->_flags |= _SCROLLWIN;
                    160:        }
                    161: # ifdef DEBUG
                    162:        fprintf(outf, "MAKENEW: win->_clear = %d\n", win->_clear);
                    163:        fprintf(outf, "MAKENEW: win->_leave = %d\n", win->_leave);
                    164:        fprintf(outf, "MAKENEW: win->_scroll = %d\n", win->_scroll);
                    165:        fprintf(outf, "MAKENEW: win->_flags = %0.2o\n", win->_flags);
                    166:        fprintf(outf, "MAKENEW: win->_maxy = %d\n", win->_maxy);
                    167:        fprintf(outf, "MAKENEW: win->_maxx = %d\n", win->_maxx);
                    168:        fprintf(outf, "MAKENEW: win->_begy = %d\n", win->_begy);
                    169:        fprintf(outf, "MAKENEW: win->_begx = %d\n", win->_begx);
                    170: # endif
                    171:        return win;
                    172: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.