libpayload: remove trailing whitespace and run dos2unix
[coreboot.git] / payloads / libpayload / curses / PDCurses-3.4 / pdcurses / scr_dump.c
1 /* Public Domain Curses */
2
3 #include <curspriv.h>
4
5 RCSID("$Id: scr_dump.c,v 1.30 2008/07/13 16:08:18 wmcbrine Exp $")
6
7 /*man-start**************************************************************
8
9   Name:                                                         scr_dump
10
11   Synopsis:
12         int putwin(WINDOW *win, FILE *filep);
13         WINDOW *getwin(FILE *filep);
14         int scr_dump(const char *filename);
15         int scr_init(const char *filename);
16         int scr_restore(const char *filename);
17         int scr_set(const char *filename);
18
19   Description:
20         getwin() reads window-related data previously stored in a file
21         by putwin(). It then creates and initialises a new window using
22         that data.
23
24         putwin() writes all data associated with a window into a file,
25         using an unspecified format. This information can be retrieved
26         later using getwin().
27
28         scr_dump() writes the current contents of the virtual screen to
29         the file named by filename in an unspecified format.
30
31         scr_restore() function sets the virtual screen to the contents
32         of the file named by filename, which must have been written
33         using scr_dump(). The next refresh operation restores the screen
34         to the way it looked in the dump file.
35
36         In PDCurses, scr_init() does nothing, and scr_set() is a synonym
37         for scr_restore(). Also, scr_dump() and scr_restore() save and
38         load from curscr. This differs from some other implementations,
39         where scr_init() works with curscr, and scr_restore() works with
40         newscr; but the effect should be the same. (PDCurses has no
41         newscr.)
42
43   Return Value:
44         On successful completion, getwin() returns a pointer to the
45         window it created. Otherwise, it returns a null pointer. Other
46         functions return OK or ERR.
47
48   Portability                                X/Open    BSD    SYS V
49         putwin                                  Y
50         getwin                                  Y
51         scr_dump                                Y
52         scr_init                                Y
53         scr_restore                             Y
54         scr_set                                 Y
55
56 **man-end****************************************************************/
57
58 #include <stdlib.h>
59 #include <string.h>
60
61 #define DUMPVER 1   /* Should be updated whenever the WINDOW struct is
62                        changed */
63
64 int putwin(WINDOW *win, FILE *filep)
65 {
66     static const char *marker = "PDC";
67     static const unsigned char version = DUMPVER;
68
69     PDC_LOG(("putwin() - called\n"));
70
71     /* write the marker and the WINDOW struct */
72
73     if (filep && fwrite(marker, strlen(marker), 1, filep)
74               && fwrite(&version, 1, 1, filep)
75               && fwrite(win, sizeof(WINDOW), 1, filep))
76     {
77         int i;
78
79         /* write each line */
80
81         for (i = 0; i < win->_maxy && win->_y[i]; i++)
82             if (!fwrite(win->_y[i], win->_maxx * sizeof(chtype), 1, filep))
83                 return ERR;
84
85         return OK;
86     }
87
88     return ERR;
89 }
90
91 WINDOW *getwin(FILE *filep)
92 {
93     WINDOW *win;
94     char marker[4];
95     int i, nlines, ncols;
96
97     PDC_LOG(("getwin() - called\n"));
98
99     if ( !(win = malloc(sizeof(WINDOW))) )
100         return (WINDOW *)NULL;
101
102     /* check for the marker, and load the WINDOW struct */
103
104     if (!filep || !fread(marker, 4, 1, filep) || strncmp(marker, "PDC", 3)
105         || marker[3] != DUMPVER || !fread(win, sizeof(WINDOW), 1, filep))
106     {
107         free(win);
108         return (WINDOW *)NULL;
109     }
110
111     nlines = win->_maxy;
112     ncols = win->_maxx;
113
114     /* allocate the line pointer array */
115
116     if ( !(win->_y = malloc(nlines * sizeof(chtype *))) )
117     {
118         free(win);
119         return (WINDOW *)NULL;
120     }
121
122     /* allocate the minchng and maxchng arrays */
123
124     if ( !(win->_firstch = malloc(nlines * sizeof(int))) )
125     {
126         free(win->_y);
127         free(win);
128         return (WINDOW *)NULL;
129     }
130
131     if ( !(win->_lastch = malloc(nlines * sizeof(int))) )
132     {
133         free(win->_firstch);
134         free(win->_y);
135         free(win);
136         return (WINDOW *)NULL;
137     }
138
139     /* allocate the lines */
140
141     if ( !(win = PDC_makelines(win)) )
142         return (WINDOW *)NULL;
143
144     /* read them */
145
146     for (i = 0; i < nlines; i++)
147     {
148         if (!fread(win->_y[i], ncols * sizeof(chtype), 1, filep))
149         {
150             delwin(win);
151             return (WINDOW *)NULL;
152         }
153     }
154
155     touchwin(win);
156
157     return win;
158 }
159
160 int scr_dump(const char *filename)
161 {
162     FILE *filep;
163
164     PDC_LOG(("scr_dump() - called: filename %s\n", filename));
165
166     if (filename && (filep = fopen(filename, "wb")) != NULL)
167     {
168         int result = putwin(curscr, filep);
169         fclose(filep);
170         return result;
171     }
172
173     return ERR;
174 }
175
176 int scr_init(const char *filename)
177 {
178     PDC_LOG(("scr_init() - called: filename %s\n", filename));
179
180     return OK;
181 }
182
183 int scr_restore(const char *filename)
184 {
185     FILE *filep;
186
187     PDC_LOG(("scr_restore() - called: filename %s\n", filename));
188
189     if (filename && (filep = fopen(filename, "rb")) != NULL)
190     {
191         WINDOW *replacement = getwin(filep);
192         fclose(filep);
193
194         if (replacement)
195         {
196             int result = overwrite(replacement, curscr);
197             delwin(replacement);
198             return result;
199         }
200     }
201
202     return ERR;
203 }
204
205 int scr_set(const char *filename)
206 {
207     PDC_LOG(("scr_set() - called: filename %s\n", filename));
208
209     return scr_restore(filename);
210 }