Merge from vendor branch LIBSTDC++:
[dragonfly.git] / usr.sbin / pcvt / fed / misc.c
1 /*
2  * Copyright (c) 1992, 1993, 1994 by Hellmuth Michaelis
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *    This product includes software developed by Hellmuth Michaelis.
17  * 4. The name of the developer may not be used to endorse or promote
18  *    products derived from this software without specific prior written
19  *    permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  *      misc.c, 3.00, last edit-date: [Sun Jan  2 20:09:21 1994]
33  */
34
35 /*---------------------------------------------------------------------------
36  *
37  *      misc.c          font editor misc routines
38  *      -----------------------------------------
39  *
40  *      written by Hellmuth Michaelis, hm@hcshh.hcs.de
41  *
42  *      -hm     first public release
43  *      -hm     writefont routine
44  *
45  *---------------------------------------------------------------------------*/
46
47 #include "fed.h"
48
49 #include <stdlib.h>
50 #include <string.h>
51 #include <sys/types.h>
52 #include <sys/stat.h>
53
54 static unsigned char *fonttab;          /* ptr to font in core memory */
55
56 static char *bitmask[] = {
57                 "....",                 /*  0 */
58                 "...*",                 /*  1 */
59                 "..*.",                 /*  2 */
60                 "..**",                 /*  3 */
61                 ".*..",                 /*  4 */
62                 ".*.*",                 /*  5 */
63                 ".**.",                 /*  6 */
64                 ".***",                 /*  7 */
65                 "*...",                 /*  8 */
66                 "*..*",                 /*  9 */
67                 "*.*.",                 /*  A */
68                 "*.**",                 /*  B */
69                 "**..",                 /*  C */
70                 "**.*",                 /*  D */
71                 "***.",                 /*  E */
72                 "****",                 /*  F */
73                 NULL };
74
75 static char lfilename[1024];    /* current filename */
76 static unsigned int lfilesize;  /* current filename's size */
77
78 /*---------------------------------------------------------------------------*
79  *      read fontfile into memory
80  *---------------------------------------------------------------------------*/
81 void readfont(char *filename)
82 {
83         FILE *in;
84         struct stat sbuf, *sbp;
85         int ret;
86         char buffer[1024];
87
88         sbp = &sbuf;
89
90         if((in = fopen(filename, "r")) == NULL)
91         {
92                 sprintf(buffer, "cannot open file %s for reading", filename);
93                 perror(buffer);
94                 exit(1);
95         }
96
97         if((fstat(fileno(in), sbp)) != 0)
98         {
99                 sprintf(buffer, "cannot fstat file %s", filename);
100                 perror(buffer);
101                 exit(1);
102         }
103
104         switch(sbp->st_size)
105         {
106                 case FONT8X8:
107                         ch_height = HEIGHT8X8;
108                         ch_width = WIDTH8;
109                         break;
110
111                 case FONT8X10:
112                         ch_height = HEIGHT8X10;
113                         ch_width = WIDTH8;
114                         break;
115
116                 case FONT8X14:
117                         ch_height = HEIGHT8X14;
118                         ch_width = WIDTH8;
119                         break;
120
121                 case FONT8X16:
122                         ch_height = HEIGHT8X16;
123                         ch_width = WIDTH8;
124                         break;
125
126                 case FONT16X16:
127                         ch_height = HEIGHT16X16;
128                         ch_width = WIDTH16;
129                         break;
130
131                 default:
132                         fprintf(stderr,"error, file %s is no valid font file, size=%d\n",filename,sbp->st_size);
133                         exit(1);
134         }
135
136         if((fonttab = (unsigned char *)malloc((size_t)sbp->st_size)) == NULL)
137         {
138                 fprintf(stderr,"error, malloc failed\n");
139                 exit(1);
140         }
141
142         strcpy(lfilename, filename);    /* save for write */
143         lfilesize = sbp->st_size;       /* save for write */
144
145         if((ret = fread(fonttab, sizeof(*fonttab), sbp->st_size, in)) != sbp->st_size)
146         {
147                 sprintf(buffer,"error reading file %s, size = %d, ret = %d\n",filename,sbp->st_size, ret);
148                 perror(buffer);
149                 exit(1);
150         }
151 }
152
153 /*---------------------------------------------------------------------------*
154  *      write fontfile to disk
155  *---------------------------------------------------------------------------*/
156 void writefont()
157 {
158         FILE *in, *out;
159         int ret;
160         char buffer[1024];
161
162         if((in = fopen(lfilename, "r")) != NULL)
163         {
164                 int c;
165                 char wfn[1024];
166
167                 strcpy(wfn, lfilename);
168                 strcat(wfn, ".BAK");
169                 if((out = fopen(wfn, "w")) == NULL)
170                 {
171                         sprintf(buffer, "cannot open file %s for writing", wfn);
172                         perror(buffer);
173                         exit(1);
174                 }
175
176                 while(( c = fgetc(in) ) != EOF )
177                         fputc(c, out);
178
179                 fclose(out);
180                 fclose(in);
181         }
182
183         if((out = fopen(lfilename, "w")) == NULL)
184         {
185                 sprintf(buffer, "cannot open file %s for writing", lfilename);
186                 perror(buffer);
187                 exit(1);
188         }
189
190         if((ret = fwrite(fonttab, sizeof(*fonttab), lfilesize, out)) != lfilesize)
191         {
192                 sprintf(buffer,"error writing file %s, size=%d, ret=%d\n",lfilename,lfilesize, ret);
193                 perror(buffer);
194                 exit(1);
195         }
196 }
197
198 /*---------------------------------------------------------------------------*
199  *      display a string
200  *---------------------------------------------------------------------------*/
201 void dis_cmd(char *strg)
202 {
203         move(22,0);
204         clrtoeol();
205         mvaddstr(22,0,strg);
206         refresh();
207 }
208
209 /*---------------------------------------------------------------------------*
210  *      clear a command string
211  *---------------------------------------------------------------------------*/
212 void clr_cmd(void)
213 {
214         move(22,0);
215         clrtoeol();
216         refresh();
217 }
218
219 /*---------------------------------------------------------------------------*
220  *      move char from src to dest
221  *---------------------------------------------------------------------------*/
222 void move_ch(int src, int dst)
223 {
224         unsigned char *s, *d;
225         int offset = 0;
226
227         if(ch_width == WIDTH16)
228                 offset = 2;
229         else
230                 offset = 1;
231
232         s = &(fonttab[ch_height * offset * src]);
233         d = &(fonttab[ch_height * offset * dst]);
234
235         bcopy(s, d, (ch_height*offset));        /* src -> dst */
236 }
237
238 /*---------------------------------------------------------------------------*
239  *      exchange char's src and dest
240  *---------------------------------------------------------------------------*/
241 void xchg_ch(int src, int dst)
242 {
243         unsigned char *s, *d;
244         unsigned char buf[32];
245         int offset = 0;
246
247         if(ch_width == WIDTH16)
248                 offset = 2;
249         else
250                 offset = 1;
251
252         s = &(fonttab[ch_height * offset * src]);
253         d = &(fonttab[ch_height * offset * dst]);
254
255         bcopy(s, buf, (ch_height*offset));      /* src -> tmp */
256         bcopy(d, s, (ch_height*offset));        /* dst -> src */
257         bcopy(buf, d, (ch_height*offset));      /* tmp -> dst */
258 }
259
260 /*---------------------------------------------------------------------------*
261  *      display the current selected character
262  *---------------------------------------------------------------------------*/
263 void display(int no)
264 {
265         unsigned char *fontchar;
266         char line[32];
267         int ln_no;
268         unsigned char hibyte;
269         unsigned char lobyte;
270         int offset;
271         int r;
272
273         offset = 0;
274         r = 1;
275         lobyte = 0;
276
277         if(ch_width == WIDTH16)
278                 fontchar = &(fonttab[ch_height * 2 * no]);
279         else
280                 fontchar = &(fonttab[ch_height * no]);
281
282         for (ln_no = 0; ln_no < ch_height; ln_no++)
283         {
284                 hibyte = *(fontchar + (offset++));
285
286                 if(ch_width == WIDTH16)
287                 {
288                         lobyte = *(fontchar + offset++);
289                 }
290
291                 strcpy(line,bitmask[(int)((hibyte >> 4) & 0x0f)]);
292                 strcat(line,bitmask[(int)(hibyte & 0x0f)]);
293
294                 if(ch_width == WIDTH16)
295                 {
296                         strcat(line,bitmask[(int)((lobyte >> 4) & 0x0f)]);
297                         strcat(line,bitmask[(int)(lobyte & 0x0f)]);
298                         mvwprintw(ch_win, r, 1, "%16.16s", line);
299                 }
300                 else
301                 {
302                         mvwprintw(ch_win, r, 1, "%8.8s", line);
303                 }
304                 r++;
305         }
306         wmove(ch_win, 1, 1);
307         wrefresh(ch_win);
308 }
309
310 /*---------------------------------------------------------------------------*
311  *      save character
312  *---------------------------------------------------------------------------*/
313 void save_ch(void)
314 {
315         unsigned char *s;
316         int offset = 0;
317         int r, c;
318         unsigned short byte;
319         unsigned short shift;
320
321         if(ch_width == WIDTH16)
322                 offset = 2;
323         else
324                 offset = 1;
325
326         s = &(fonttab[ch_height * offset * curchar]);
327
328         r = 1;
329
330         while(r <= ch_height)
331         {
332                 c = 1;
333                 byte = 0;
334                 if(offset == 2)
335                         shift = 0x8000;
336                 else
337                         shift = 0x80;
338
339                 while(c <= ch_width)
340                 {
341                         if(mvwinch(ch_win, r, c) == BLACK)
342                                 byte |= shift;
343                         shift = (shift >> 1);
344                         c++;
345                 }
346                 *s++ = byte;
347                 r++;
348         }
349 }
350
351 /*---------------------------------- E O F ----------------------------------*/
352
353