]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/lib_screen.c
ncurses 4.1
[ncurses.git] / ncurses / lib_screen.c
1
2 /***************************************************************************
3 *                            COPYRIGHT NOTICE                              *
4 ****************************************************************************
5 *                ncurses is copyright (C) 1992-1995                        *
6 *                          Zeyd M. Ben-Halim                               *
7 *                          zmbenhal@netcom.com                             *
8 *                          Eric S. Raymond                                 *
9 *                          esr@snark.thyrsus.com                           *
10 *                                                                          *
11 *        Permission is hereby granted to reproduce and distribute ncurses  *
12 *        by any means and for any fee, whether alone or as part of a       *
13 *        larger distribution, in source or in binary form, PROVIDED        *
14 *        this notice is included with any such distribution, and is not    *
15 *        removed from any of its header files. Mention of ncurses in any   *
16 *        applications linked with it is highly appreciated.                *
17 *                                                                          *
18 *        ncurses comes AS IS with no warranty, implied or expressed.       *
19 *                                                                          *
20 ***************************************************************************/
21
22
23 #include <curses.priv.h>
24
25 #include <sys/stat.h>
26 #include <time.h>
27 #include <term.h>       /* exit_ca_mode, non_rev_rmcup */
28
29 MODULE_ID("$Id: lib_screen.c,v 1.7 1997/02/02 00:41:10 tom Exp $")
30
31 static time_t   dumptime;
32
33 WINDOW *getwin(FILE *filep)
34 {
35         WINDOW  try, *nwin;
36         int     n;
37
38         T((T_CALLED("getwin(%p)"), filep));
39
40         (void) fread(&try, sizeof(WINDOW), 1, filep);
41         if (ferror(filep))
42                 returnWin(0);
43
44         if ((nwin = newwin(try._maxy+1, try._maxx+1, 0, 0)) == 0)
45                 returnWin(0);
46
47         /*
48          * We deliberately do not restore the _parx, _pary, or _parent
49          * fields, because the window hierarchy within which they
50          * made sense is probably gone.
51          */
52         nwin->_curx       = try._curx;
53         nwin->_cury       = try._cury;
54         nwin->_maxy       = try._maxy;
55         nwin->_maxx       = try._maxx;
56         nwin->_begy       = try._begy;
57         nwin->_begx       = try._begx;
58         nwin->_yoffset    = try._yoffset;
59         nwin->_flags      = try._flags & ~(_SUBWIN|_ISPAD);
60
61         nwin->_attrs      = try._attrs;
62         nwin->_bkgd       = try._bkgd;
63
64         nwin->_clear      = try._clear;
65         nwin->_scroll     = try._scroll;
66         nwin->_leaveok    = try._leaveok;
67         nwin->_use_keypad = try._use_keypad;
68         nwin->_delay      = try._delay;
69         nwin->_immed      = try._immed;
70         nwin->_sync       = try._sync;
71
72         nwin->_regtop     = try._regtop;
73         nwin->_regbottom  = try._regbottom;
74
75         for (n = 0; n < nwin->_maxy + 1; n++)
76         {
77                 (void) fread(nwin->_line[n].text,
78                               sizeof(chtype), (size_t)(nwin->_maxx + 1), filep);
79                 if (ferror(filep))
80                 {
81                         delwin(nwin);
82                         returnWin(0);
83                 }
84         }
85         touchwin(nwin);
86
87         returnWin(nwin);
88 }
89
90 int putwin(WINDOW *win, FILE *filep)
91 {
92         int     n;
93
94         T((T_CALLED("putwin(%p,%p)"), win, filep));
95
96         (void) fwrite(win, sizeof(WINDOW), 1, filep);
97         if (ferror(filep))
98                 returnCode(ERR);
99
100         for (n = 0; n < win->_maxy + 1; n++)
101         {
102                 (void) fwrite(win->_line[n].text,
103                               sizeof(chtype), (size_t)(win->_maxx + 1), filep);
104                 if (ferror(filep))
105                         returnCode(ERR);
106         }
107
108         returnCode(OK);
109 }
110
111 int scr_restore(const char *file)
112 {
113         FILE    *fp;
114
115         T((T_CALLED("scr_restore(%s)"), _nc_visbuf(file)));
116
117         if ((fp = fopen(file, "r")) == 0)
118             returnCode(ERR);
119         else
120         {
121             delwin(newscr);
122             newscr = getwin(fp);
123             (void) fclose(fp);
124             returnCode(OK);
125         }
126 }
127
128 int scr_dump(const char *file)
129 {
130         FILE    *fp;
131
132         T((T_CALLED("scr_dump(%s)"), _nc_visbuf(file)));
133
134         if ((fp = fopen(file, "w")) == 0)
135             returnCode(ERR);
136         else
137         {
138             (void) putwin(newscr, fp);
139             (void) fclose(fp);
140             dumptime = time((time_t *)0);
141             returnCode(OK);
142         }
143 }
144
145 int scr_init(const char *file)
146 {
147         FILE    *fp;
148         struct stat     stb;
149
150         T((T_CALLED("scr_init(%s)"), _nc_visbuf(file)));
151
152 #ifdef exit_ca_mode
153         if (exit_ca_mode && non_rev_rmcup)
154             returnCode(ERR);
155 #endif /* exit_ca_mode */
156
157         if ((fp = fopen(file, "r")) == 0)
158             returnCode(ERR);
159         else if (fstat(STDOUT_FILENO, &stb) || stb.st_mtime > dumptime)
160             returnCode(ERR);
161         else
162         {
163             delwin(curscr);
164             curscr = getwin(fp);
165             (void) fclose(fp);
166             returnCode(OK);
167         }
168 }
169
170 int scr_set(const char *file)
171 {
172     T((T_CALLED("scr_set(%s)"), _nc_visbuf(file)));
173
174     if (scr_init(file) == ERR)
175         returnCode(ERR);
176     else
177     {
178         delwin(newscr);
179         newscr = dupwin(curscr);
180         returnCode(OK);
181     }
182 }