X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=ncurses%2Fbase%2Flib_screen.c;h=7404caacf490580a7ce9a22fc8e362f86e62c896;hp=3038393c226676bb3957f43c7a72642a3794e61e;hb=f3eb40315f9e5eff503a172aed6d325b4e1f20c6;hpb=0eb88fc5281804773e2a0c7a488a4452463535ce diff --git a/ncurses/base/lib_screen.c b/ncurses/base/lib_screen.c index 3038393c..7404caac 100644 --- a/ncurses/base/lib_screen.c +++ b/ncurses/base/lib_screen.c @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright (c) 1998 Free Software Foundation, Inc. * + * Copyright (c) 1998-2009,2011 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 * @@ -29,170 +29,243 @@ /**************************************************************************** * Author: Zeyd M. Ben-Halim 1992,1995 * * and: Eric S. Raymond * + * and: Thomas E. Dickey 1996 on * + * and: Juergen Pfeifer 2009 * ****************************************************************************/ - #include -#include -#include -#include /* exit_ca_mode, non_rev_rmcup */ +#ifndef CUR +#define CUR SP_TERMTYPE +#endif -MODULE_ID("$Id: lib_screen.c,v 1.15 1999/07/24 20:05:29 tom Exp $") +MODULE_ID("$Id: lib_screen.c,v 1.40 2011/05/28 23:03:44 tom Exp $") -static time_t dumptime; +#define MAX_SIZE 0x3fff /* 16k is big enough for a window or pad */ -WINDOW *getwin(FILE *filep) +NCURSES_EXPORT(WINDOW *) +NCURSES_SP_NAME(getwin) (NCURSES_SP_DCLx FILE *filep) { - WINDOW tmp, *nwin; - int n; + WINDOW tmp, *nwin; + int n; - T((T_CALLED("getwin(%p)"), filep)); + T((T_CALLED("getwin(%p)"), (void *) filep)); - (void) fread(&tmp, sizeof(WINDOW), 1, filep); - if (ferror(filep)) - returnWin(0); + if (filep == 0) { + returnWin(0); + } + clearerr(filep); + if (fread(&tmp, 1, sizeof(WINDOW), filep) < sizeof(WINDOW) + || ferror(filep) + || tmp._maxy == 0 + || tmp._maxy > MAX_SIZE + || tmp._maxx == 0 + || tmp._maxx > MAX_SIZE) { + returnWin(0); + } - if ((nwin = newwin(tmp._maxy+1, tmp._maxx+1, 0, 0)) == 0) - returnWin(0); + if (tmp._flags & _ISPAD) { + nwin = NCURSES_SP_NAME(newpad) (NCURSES_SP_ARGx + tmp._maxy + 1, + tmp._maxx + 1); + } else { + nwin = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx + tmp._maxy + 1, + tmp._maxx + 1, 0, 0); + } - /* - * We deliberately do not restore the _parx, _pary, or _parent - * fields, because the window hierarchy within which they - * made sense is probably gone. - */ - nwin->_curx = tmp._curx; - nwin->_cury = tmp._cury; - nwin->_maxy = tmp._maxy; - nwin->_maxx = tmp._maxx; - nwin->_begy = tmp._begy; - nwin->_begx = tmp._begx; - nwin->_yoffset = tmp._yoffset; - nwin->_flags = tmp._flags & ~(_SUBWIN|_ISPAD); - - nwin->_attrs = tmp._attrs; - nwin->_bkgd = tmp._bkgd; - - nwin->_clear = tmp._clear; - nwin->_scroll = tmp._scroll; - nwin->_leaveok = tmp._leaveok; + /* + * We deliberately do not restore the _parx, _pary, or _parent + * fields, because the window hierarchy within which they + * made sense is probably gone. + */ + if (nwin != 0) { + size_t linesize = sizeof(NCURSES_CH_T) * (size_t) (tmp._maxx + 1); + + nwin->_curx = tmp._curx; + nwin->_cury = tmp._cury; + nwin->_maxy = tmp._maxy; + nwin->_maxx = tmp._maxx; + nwin->_begy = tmp._begy; + nwin->_begx = tmp._begx; + nwin->_yoffset = tmp._yoffset; + nwin->_flags = tmp._flags & ~(_SUBWIN); + + WINDOW_ATTRS(nwin) = WINDOW_ATTRS(&tmp); + nwin->_nc_bkgd = tmp._nc_bkgd; + + nwin->_notimeout = tmp._notimeout; + nwin->_clear = tmp._clear; + nwin->_leaveok = tmp._leaveok; + nwin->_idlok = tmp._idlok; + nwin->_idcok = tmp._idcok; + nwin->_immed = tmp._immed; + nwin->_scroll = tmp._scroll; + nwin->_sync = tmp._sync; nwin->_use_keypad = tmp._use_keypad; - nwin->_delay = tmp._delay; - nwin->_immed = tmp._immed; - nwin->_sync = tmp._sync; - - nwin->_regtop = tmp._regtop; - nwin->_regbottom = tmp._regbottom; - - for (n = 0; n < nwin->_maxy + 1; n++) - { - (void) fread(nwin->_line[n].text, - sizeof(chtype), (size_t)(nwin->_maxx + 1), filep); - if (ferror(filep)) - { - delwin(nwin); - returnWin(0); - } + nwin->_delay = tmp._delay; + + nwin->_regtop = tmp._regtop; + nwin->_regbottom = tmp._regbottom; + + if (tmp._flags & _ISPAD) + nwin->_pad = tmp._pad; + + for (n = 0; n <= nwin->_maxy; n++) { + clearerr(filep); + if (fread(nwin->_line[n].text, 1, linesize, filep) < linesize + || ferror(filep)) { + delwin(nwin); + returnWin(0); + } } touchwin(nwin); + } + returnWin(nwin); +} - returnWin(nwin); +#if NCURSES_SP_FUNCS +NCURSES_EXPORT(WINDOW *) +getwin(FILE *filep) +{ + return NCURSES_SP_NAME(getwin) (CURRENT_SCREEN, filep); } +#endif -int putwin(WINDOW *win, FILE *filep) +NCURSES_EXPORT(int) +putwin(WINDOW *win, FILE *filep) { - int code = ERR; - int n; + int code = ERR; + int n; - T((T_CALLED("putwin(%p,%p)"), win, filep)); + T((T_CALLED("putwin(%p,%p)"), (void *) win, (void *) filep)); - if (win) { - (void) fwrite(win, sizeof(WINDOW), 1, filep); - if (ferror(filep)) - returnCode(code); + if (win != 0) { + size_t len = (size_t) (win->_maxx + 1); - for (n = 0; n < win->_maxy + 1; n++) - { - (void) fwrite(win->_line[n].text, - sizeof(chtype), (size_t)(win->_maxx + 1), filep); - if (ferror(filep)) + clearerr(filep); + if (fwrite(win, sizeof(WINDOW), 1, filep) != 1 + || ferror(filep)) + returnCode(code); + + for (n = 0; n <= win->_maxy; n++) { + if (fwrite(win->_line[n].text, + sizeof(NCURSES_CH_T), len, filep) != len + || ferror(filep)) { returnCode(code); } - code = OK; } - returnCode(code); + code = OK; + } + returnCode(code); } -int scr_restore(const char *file) +NCURSES_EXPORT(int) +NCURSES_SP_NAME(scr_restore) (NCURSES_SP_DCLx const char *file) { - FILE *fp = 0; + FILE *fp = 0; - T((T_CALLED("scr_restore(%s)"), _nc_visbuf(file))); + T((T_CALLED("scr_restore(%p,%s)"), (void *) SP_PARM, _nc_visbuf(file))); - if (_nc_access(file, R_OK) < 0 - || (fp = fopen(file, "rb")) == 0) - returnCode(ERR); - else - { - delwin(newscr); - newscr = getwin(fp); - (void) fclose(fp); - returnCode(OK); - } + if (_nc_access(file, R_OK) < 0 + || (fp = fopen(file, "rb")) == 0) { + returnCode(ERR); + } else { + delwin(NewScreen(SP_PARM)); + NewScreen(SP_PARM) = getwin(fp); +#if !USE_REENTRANT + newscr = NewScreen(SP_PARM); +#endif + (void) fclose(fp); + returnCode(OK); + } } -int scr_dump(const char *file) +#if NCURSES_SP_FUNCS +NCURSES_EXPORT(int) +scr_restore(const char *file) { - FILE *fp = 0; + return NCURSES_SP_NAME(scr_restore) (CURRENT_SCREEN, file); +} +#endif - T((T_CALLED("scr_dump(%s)"), _nc_visbuf(file))); +NCURSES_EXPORT(int) +scr_dump(const char *file) +{ + int result; + FILE *fp = 0; + + T((T_CALLED("scr_dump(%s)"), _nc_visbuf(file))); + + if (_nc_access(file, W_OK) < 0 + || (fp = fopen(file, "wb")) == 0) { + result = ERR; + } else { + (void) putwin(newscr, fp); + (void) fclose(fp); + result = OK; + } + returnCode(result); +} - if (_nc_access(file, W_OK) < 0 - || (fp = fopen(file, "wb")) == 0) - returnCode(ERR); - else - { - (void) putwin(newscr, fp); +NCURSES_EXPORT(int) +NCURSES_SP_NAME(scr_init) (NCURSES_SP_DCLx const char *file) +{ + FILE *fp = 0; + int code = ERR; + + T((T_CALLED("scr_init(%p,%s)"), (void *) SP_PARM, _nc_visbuf(file))); + + if (SP_PARM != 0 && +#ifdef USE_TERM_DRIVER + InfoOf(SP_PARM).caninit +#else + !(exit_ca_mode && non_rev_rmcup) +#endif + ) { + if (_nc_access(file, R_OK) >= 0 + && (fp = fopen(file, "rb")) != 0) { + delwin(CurScreen(SP_PARM)); + CurScreen(SP_PARM) = getwin(fp); +#if !USE_REENTRANT + curscr = CurScreen(SP_PARM); +#endif (void) fclose(fp); - dumptime = time((time_t *)0); - returnCode(OK); + code = OK; } + } + returnCode(code); } -int scr_init(const char *file) +#if NCURSES_SP_FUNCS +NCURSES_EXPORT(int) +scr_init(const char *file) { - FILE *fp = 0; - struct stat stb; - - T((T_CALLED("scr_init(%s)"), _nc_visbuf(file))); - - if (exit_ca_mode && non_rev_rmcup) - returnCode(ERR); - - if (_nc_access(file, R_OK) < 0 - || (fp = fopen(file, "rb")) == 0) - returnCode(ERR); - else if (fstat(STDOUT_FILENO, &stb) || stb.st_mtime > dumptime) - returnCode(ERR); - else - { - delwin(curscr); - curscr = getwin(fp); - (void) fclose(fp); - returnCode(OK); - } + return NCURSES_SP_NAME(scr_init) (CURRENT_SCREEN, file); } +#endif -int scr_set(const char *file) +NCURSES_EXPORT(int) +NCURSES_SP_NAME(scr_set) (NCURSES_SP_DCLx const char *file) { - T((T_CALLED("scr_set(%s)"), _nc_visbuf(file))); + T((T_CALLED("scr_set(%p,%s)"), (void *) SP_PARM, _nc_visbuf(file))); - if (scr_init(file) == ERR) + if (NCURSES_SP_NAME(scr_init) (NCURSES_SP_ARGx file) == ERR) { returnCode(ERR); - else - { - delwin(newscr); - newscr = dupwin(curscr); + } else { + delwin(NewScreen(SP_PARM)); + NewScreen(SP_PARM) = dupwin(curscr); +#if !USE_REENTRANT + newscr = NewScreen(SP_PARM); +#endif returnCode(OK); } } + +#if NCURSES_SP_FUNCS +NCURSES_EXPORT(int) +scr_set(const char *file) +{ + return NCURSES_SP_NAME(scr_set) (CURRENT_SCREEN, file); +} +#endif