1 /* $OpenBSD: lib_screen.c,v 1.3 2001/01/22 18:01:43 millert Exp $ */ 2 3 /**************************************************************************** 4 * Copyright (c) 1998,2000 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 ****************************************************************************/ 35 36 #include <curses.priv.h> 37 38 #include <sys/stat.h> 39 #include <time.h> 40 #include <term.h> /* exit_ca_mode, non_rev_rmcup */ 41 42 MODULE_ID("$From: lib_screen.c,v 1.17 2000/12/10 02:43:27 tom Exp $") 43 44 static time_t dumptime; 45 46 NCURSES_EXPORT(WINDOW *) 47 getwin(FILE * filep) 48 { 49 WINDOW tmp, *nwin; 50 int n; 51 52 T((T_CALLED("getwin(%p)"), filep)); 53 54 (void) fread(&tmp, sizeof(WINDOW), 1, filep); 55 if (ferror(filep)) 56 returnWin(0); 57 58 if ((nwin = newwin(tmp._maxy + 1, tmp._maxx + 1, 0, 0)) == 0) 59 returnWin(0); 60 61 /* 62 * We deliberately do not restore the _parx, _pary, or _parent 63 * fields, because the window hierarchy within which they 64 * made sense is probably gone. 65 */ 66 nwin->_curx = tmp._curx; 67 nwin->_cury = tmp._cury; 68 nwin->_maxy = tmp._maxy; 69 nwin->_maxx = tmp._maxx; 70 nwin->_begy = tmp._begy; 71 nwin->_begx = tmp._begx; 72 nwin->_yoffset = tmp._yoffset; 73 nwin->_flags = tmp._flags & ~(_SUBWIN | _ISPAD); 74 75 nwin->_attrs = tmp._attrs; 76 nwin->_bkgd = tmp._bkgd; 77 78 nwin->_clear = tmp._clear; 79 nwin->_scroll = tmp._scroll; 80 nwin->_leaveok = tmp._leaveok; 81 nwin->_use_keypad = tmp._use_keypad; 82 nwin->_delay = tmp._delay; 83 nwin->_immed = tmp._immed; 84 nwin->_sync = tmp._sync; 85 86 nwin->_regtop = tmp._regtop; 87 nwin->_regbottom = tmp._regbottom; 88 89 for (n = 0; n < nwin->_maxy + 1; n++) { 90 (void) fread(nwin->_line[n].text, 91 sizeof(chtype), (size_t) (nwin->_maxx + 1), filep); 92 if (ferror(filep)) { 93 delwin(nwin); 94 returnWin(0); 95 } 96 } 97 touchwin(nwin); 98 99 returnWin(nwin); 100 } 101 102 NCURSES_EXPORT(int) 103 putwin(WINDOW *win, FILE * filep) 104 { 105 int code = ERR; 106 int n; 107 108 T((T_CALLED("putwin(%p,%p)"), win, filep)); 109 110 if (win) { 111 (void) fwrite(win, sizeof(WINDOW), 1, filep); 112 if (ferror(filep)) 113 returnCode(code); 114 115 for (n = 0; n < win->_maxy + 1; n++) { 116 (void) fwrite(win->_line[n].text, 117 sizeof(chtype), (size_t) (win->_maxx + 1), filep); 118 if (ferror(filep)) 119 returnCode(code); 120 } 121 code = OK; 122 } 123 returnCode(code); 124 } 125 126 NCURSES_EXPORT(int) 127 scr_restore(const char *file) 128 { 129 FILE *fp = 0; 130 131 T((T_CALLED("scr_restore(%s)"), _nc_visbuf(file))); 132 133 if (_nc_access(file, R_OK) < 0 134 || (fp = fopen(file, "rb")) == 0) 135 returnCode(ERR); 136 else { 137 delwin(newscr); 138 newscr = getwin(fp); 139 (void) fclose(fp); 140 returnCode(OK); 141 } 142 } 143 144 NCURSES_EXPORT(int) 145 scr_dump(const char *file) 146 { 147 FILE *fp = 0; 148 149 T((T_CALLED("scr_dump(%s)"), _nc_visbuf(file))); 150 151 if (_nc_access(file, W_OK) < 0 152 || (fp = fopen(file, "wb")) == 0) 153 returnCode(ERR); 154 else { 155 (void) putwin(newscr, fp); 156 (void) fclose(fp); 157 dumptime = time((time_t *) 0); 158 returnCode(OK); 159 } 160 } 161 162 NCURSES_EXPORT(int) 163 scr_init(const char *file) 164 { 165 FILE *fp = 0; 166 struct stat stb; 167 168 T((T_CALLED("scr_init(%s)"), _nc_visbuf(file))); 169 170 if (exit_ca_mode && non_rev_rmcup) 171 returnCode(ERR); 172 173 if (_nc_access(file, R_OK) < 0 174 || (fp = fopen(file, "rb")) == 0) 175 returnCode(ERR); 176 else if (fstat(STDOUT_FILENO, &stb) || stb.st_mtime > dumptime) 177 returnCode(ERR); 178 else { 179 delwin(curscr); 180 curscr = getwin(fp); 181 (void) fclose(fp); 182 returnCode(OK); 183 } 184 } 185 186 NCURSES_EXPORT(int) 187 scr_set(const char *file) 188 { 189 T((T_CALLED("scr_set(%s)"), _nc_visbuf(file))); 190 191 if (scr_init(file) == ERR) 192 returnCode(ERR); 193 else { 194 delwin(newscr); 195 newscr = dupwin(curscr); 196 returnCode(OK); 197 } 198 } 199