1 /* $OpenBSD: lib_screen.c,v 1.4 2010/01/12 23:22:06 nicm Exp $ */ 2 3 /**************************************************************************** 4 * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc. * 5 * * 6 * Permission is hereby granted, free of charge, to any person obtaining a * 7 * copy of this software and associated documentation files (the * 8 * "Software"), to deal in the Software without restriction, including * 9 * without limitation the rights to use, copy, modify, merge, publish, * 10 * distribute, distribute with modifications, sublicense, and/or sell * 11 * copies of the Software, and to permit persons to whom the Software is * 12 * furnished to do so, subject to the following conditions: * 13 * * 14 * The above copyright notice and this permission notice shall be included * 15 * in all copies or substantial portions of the Software. * 16 * * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 20 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 23 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 24 * * 25 * Except as contained in this notice, the name(s) of the above copyright * 26 * holders shall not be used in advertising or otherwise to promote the * 27 * sale, use or other dealings in this Software without prior written * 28 * authorization. * 29 ****************************************************************************/ 30 31 /**************************************************************************** 32 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 33 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 34 * and: Thomas E. Dickey 1996 on * 35 ****************************************************************************/ 36 37 #include <curses.priv.h> 38 39 MODULE_ID("$Id: lib_screen.c,v 1.4 2010/01/12 23:22:06 nicm Exp $") 40 41 #define MAX_SIZE 0x3fff /* 16k is big enough for a window or pad */ 42 43 NCURSES_EXPORT(WINDOW *) 44 getwin(FILE *filep) 45 { 46 WINDOW tmp, *nwin; 47 int n; 48 49 T((T_CALLED("getwin(%p)"), filep)); 50 51 clearerr(filep); 52 (void) fread(&tmp, sizeof(WINDOW), 1, filep); 53 if (ferror(filep) 54 || tmp._maxy == 0 55 || tmp._maxy > MAX_SIZE 56 || tmp._maxx == 0 57 || tmp._maxx > MAX_SIZE) 58 returnWin(0); 59 60 if (tmp._flags & _ISPAD) { 61 nwin = newpad(tmp._maxy + 1, tmp._maxx + 1); 62 } else { 63 nwin = newwin(tmp._maxy + 1, tmp._maxx + 1, 0, 0); 64 } 65 66 /* 67 * We deliberately do not restore the _parx, _pary, or _parent 68 * fields, because the window hierarchy within which they 69 * made sense is probably gone. 70 */ 71 if (nwin != 0) { 72 nwin->_curx = tmp._curx; 73 nwin->_cury = tmp._cury; 74 nwin->_maxy = tmp._maxy; 75 nwin->_maxx = tmp._maxx; 76 nwin->_begy = tmp._begy; 77 nwin->_begx = tmp._begx; 78 nwin->_yoffset = tmp._yoffset; 79 nwin->_flags = tmp._flags & ~(_SUBWIN); 80 81 WINDOW_ATTRS(nwin) = WINDOW_ATTRS(&tmp); 82 nwin->_nc_bkgd = tmp._nc_bkgd; 83 84 nwin->_notimeout = tmp._notimeout; 85 nwin->_clear = tmp._clear; 86 nwin->_leaveok = tmp._leaveok; 87 nwin->_idlok = tmp._idlok; 88 nwin->_idcok = tmp._idcok; 89 nwin->_immed = tmp._immed; 90 nwin->_scroll = tmp._scroll; 91 nwin->_sync = tmp._sync; 92 nwin->_use_keypad = tmp._use_keypad; 93 nwin->_delay = tmp._delay; 94 95 nwin->_regtop = tmp._regtop; 96 nwin->_regbottom = tmp._regbottom; 97 98 if (tmp._flags & _ISPAD) 99 nwin->_pad = tmp._pad; 100 101 for (n = 0; n <= nwin->_maxy; n++) { 102 clearerr(filep); 103 (void) fread(nwin->_line[n].text, 104 sizeof(NCURSES_CH_T), 105 (size_t) (nwin->_maxx + 1), 106 filep); 107 if (ferror(filep)) { 108 delwin(nwin); 109 returnWin(0); 110 } 111 } 112 touchwin(nwin); 113 } 114 returnWin(nwin); 115 } 116 117 NCURSES_EXPORT(int) 118 putwin(WINDOW *win, FILE *filep) 119 { 120 int code = ERR; 121 int n; 122 123 T((T_CALLED("putwin(%p,%p)"), win, filep)); 124 125 if (win != 0) { 126 size_t len = (size_t) (win->_maxx + 1); 127 128 clearerr(filep); 129 if (fwrite(win, sizeof(WINDOW), 1, filep) != 1 130 || ferror(filep)) 131 returnCode(code); 132 133 for (n = 0; n <= win->_maxy; n++) { 134 if (fwrite(win->_line[n].text, 135 sizeof(NCURSES_CH_T), len, filep) != len 136 || ferror(filep)) { 137 returnCode(code); 138 } 139 } 140 code = OK; 141 } 142 returnCode(code); 143 } 144 145 NCURSES_EXPORT(int) 146 scr_restore(const char *file) 147 { 148 FILE *fp = 0; 149 150 T((T_CALLED("scr_restore(%s)"), _nc_visbuf(file))); 151 152 if (_nc_access(file, R_OK) < 0 153 || (fp = fopen(file, "rb")) == 0) { 154 returnCode(ERR); 155 } else { 156 delwin(newscr); 157 SP->_newscr = getwin(fp); 158 #if !USE_REENTRANT 159 newscr = SP->_newscr; 160 #endif 161 (void) fclose(fp); 162 returnCode(OK); 163 } 164 } 165 166 NCURSES_EXPORT(int) 167 scr_dump(const char *file) 168 { 169 FILE *fp = 0; 170 171 T((T_CALLED("scr_dump(%s)"), _nc_visbuf(file))); 172 173 if (_nc_access(file, W_OK) < 0 174 || (fp = fopen(file, "wb")) == 0) { 175 returnCode(ERR); 176 } else { 177 (void) putwin(newscr, fp); 178 (void) fclose(fp); 179 returnCode(OK); 180 } 181 } 182 183 NCURSES_EXPORT(int) 184 scr_init(const char *file) 185 { 186 FILE *fp = 0; 187 188 T((T_CALLED("scr_init(%s)"), _nc_visbuf(file))); 189 190 if (exit_ca_mode && non_rev_rmcup) 191 returnCode(ERR); 192 193 if (_nc_access(file, R_OK) < 0 194 || (fp = fopen(file, "rb")) == 0) { 195 returnCode(ERR); 196 } else { 197 delwin(curscr); 198 SP->_curscr = getwin(fp); 199 #if !USE_REENTRANT 200 curscr = SP->_curscr; 201 #endif 202 (void) fclose(fp); 203 returnCode(OK); 204 } 205 } 206 207 NCURSES_EXPORT(int) 208 scr_set(const char *file) 209 { 210 T((T_CALLED("scr_set(%s)"), _nc_visbuf(file))); 211 212 if (scr_init(file) == ERR) { 213 returnCode(ERR); 214 } else { 215 delwin(newscr); 216 SP->_newscr = dupwin(curscr); 217 #if !USE_REENTRANT 218 newscr = SP->_newscr; 219 #endif 220 returnCode(OK); 221 } 222 } 223