Revert "dtudcfonted, dtudcexch: delete from repository"
[oweals/cde.git] / cde / programs / dtudcexch / exportbdf.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: exportbdf.c /main/4 1996/04/10 13:49:20 ageorge $ */
24 /*
25  *  (c) Copyright 1995 FUJITSU LIMITED
26  *  This is source code modified by FUJITSU LIMITED under the Joint
27  *  Development Agreement for the CDEnext PST.
28  *  This is unpublished proprietary source code of FUJITSU LIMITED
29  *
30  *  Authors: Seiya Miyazaki     FUJITSU LIMITED
31  *           Hiroyuki Chiba     FUJITSU LIMITED
32  */
33 #include <stdio.h>
34 #include <fcntl.h>
35 #include <signal.h>
36 #include <stdlib.h>
37 #ifndef SVR4
38 #if !defined( SYSV )
39 #include <sys/resource.h>
40 #endif
41 #include <sys/wait.h>
42 #else
43 #include <wait.h>
44 #endif
45 #include <memory.h>
46 #include <unistd.h>
47 #include <sys/stat.h>
48 #include <sys/types.h>
49 #include <errno.h>
50
51 #include "bdfgpf.h"
52
53 static  int     writeBdfHeader();
54 static  int     readBdfToMemory();
55 static  void    sigint_out();
56 static char     buf[BUFSIZE];
57 static  struct  ptobhead        w_bdf ;
58
59 static  void
60 sigint_out()
61 {
62     fclose(w_bdf.output) ;
63     fclose(w_bdf.input) ;       /* stream */
64     exit( 0 );
65 }
66
67 int
68 expCheckCode( code, code_num, code_list )
69 unsigned int    code ;
70 int     code_num ;
71 int     *code_list ;
72 {
73         int     i ;
74
75         if( code < MIN_CODE || code > MAX_CODE )  return -1 ;
76         for( i=0; i<code_num; i++ ){
77             if( code == code_list[i] )  return 0 ;
78         }
79         return -1 ;
80 }
81
82 int
83 ExpGpftoBDF( gpf_name, bdf_name, code_num, code_list, comment_num, comment_list, make_all )
84 char    *gpf_name ;     /* pointer to GPF file name area        */
85 char    *bdf_name ;     /* pointer to BDF file name area        */
86 int     code_num ;      /* number of GPF code                   */
87 int     *code_list ;    /* pointer to GPF code lists            */
88 int     comment_num ;   /* number comments                      */
89 char    **comment_list ;/* pointer to the list of comments      */
90 int     make_all ;      /* convert whole GPF fomat file to BDF  */
91 {
92         struct  stat    statbuf ;
93         struct  btophead        r_gpf ;
94         int                     rtn, i, num_chars ;
95
96         /*
97          * parameter check
98          */
99         if( gpf_name == NULL || bdf_name == NULL ){
100             fprintf(stderr, "GPF or BDF file name is not specified.\n" ) ;
101             return -1 ;
102         }
103         /*
104          * initiation
105          */
106         memset( &w_bdf, 0, sizeof(struct ptobhead) ) ;
107         memset( &r_gpf, 0, sizeof(struct btophead) ) ;
108
109         if ( stat( SNFTOBDF, &statbuf ) ) {
110             if (!( oakgtobdf = get_cmd_path( getenv( "PATH" ), SNFTOBDF_CMD ))) {
111                 fprintf( stderr, "There is not %s command.\n", SNFTOBDF_CMD ) ;
112                 return -1 ;
113             }
114         }else{
115             oakgtobdf = SNFTOBDF;
116         }
117
118         /*
119          * export glyphs in BDF format
120          */
121         w_bdf.snf_file = gpf_name ;
122         if( (w_bdf.output = fopen( bdf_name, "w" )) == NULL ){
123             fprintf(stderr, "\"%s\" cannot open.\n", bdf_name ) ;
124             return -1 ;
125         }
126
127         signal( SIGHUP , (void(*)())sigint_out );
128         signal( SIGINT , (void(*)())sigint_out );
129         signal( SIGQUIT, (void(*)())sigint_out );
130         signal( SIGTERM, (void(*)())sigint_out );
131
132         if( (rtn = writeBdfHeader(&w_bdf, comment_num, comment_list)) ){
133             fprintf(stderr, "\"%s\" cannot write header.\n", bdf_name ) ;
134             fclose(w_bdf.output) ;
135             fclose(w_bdf.input) ;       /* stream */
136             return rtn ;
137         }
138
139         r_gpf.bdf_width         = w_bdf.bdf_width ;
140         r_gpf.bdf_height        = w_bdf.bdf_height ;
141         r_gpf.input             = w_bdf.input ;
142
143         num_chars = ((make_all) ? w_bdf.num_chars : code_num) ;
144
145         if( (r_gpf.code = (int *)malloc( sizeof(int) * num_chars)) == NULL ) {
146             fclose(w_bdf.output) ;
147             fclose(w_bdf.input) ;
148             return(MALLOC_ERROR);
149         }
150
151         if( (r_gpf.ptn = (char **)calloc( num_chars, sizeof(char *) )) == NULL ) {
152             fclose(w_bdf.output) ;
153             fclose(w_bdf.input) ;
154             return(MALLOC_ERROR);
155         }
156
157         if( (rtn = readBdfToMemory(&r_gpf, buf, code_num, code_list, make_all)) ){
158             fprintf(stderr, "\"%s\" cannot read glyph.\n", bdf_name ) ;
159             fclose(w_bdf.output) ;
160             fclose(w_bdf.input) ;
161             return rtn ;
162         }
163         fclose(w_bdf.input) ;
164         wait(0) ;
165
166         w_bdf.zoomf     = 0 ;
167         w_bdf.num_chars = r_gpf.num_chars ;
168         w_bdf.code      = r_gpf.code ;
169         w_bdf.ptn       = r_gpf.ptn  ;
170
171         if( (rtn = WritePtnToBdf( &w_bdf, buf )) ){
172             fprintf(stderr, "\"%s\" cannot write glyph.\n", bdf_name ) ;
173             fclose(w_bdf.output) ;
174             return rtn ;
175         }
176         fclose(w_bdf.output) ;
177
178         signal( SIGHUP , SIG_IGN );
179         signal( SIGINT , SIG_IGN );
180         signal( SIGQUIT, SIG_IGN );
181         signal( SIGTERM, SIG_IGN );
182
183         /*
184          * free memories
185          */
186         free( r_gpf.code ) ;
187         for(i=0; i<r_gpf.num_chars; i++){
188             if(r_gpf.ptn[i])    free(r_gpf.ptn[i]) ;
189         }
190         free( r_gpf.ptn ) ;
191
192         return 0 ;
193 }
194
195 static  int
196 writeBdfHeader(head, comment_num, comment_list)
197 struct ptobhead *head;
198 int     comment_num ;   /* number comments                      */
199 char    **comment_list ;/* pointer to the list of comments      */
200 {
201         FILE            *fp;
202         int             fd[2];
203         unsigned int    getstat;
204         char            buf[BUFSIZE], *p;
205
206         int             cnt ;
207         int             comflg ;
208         pid_t           chld_pid = 0;
209 #if defined( SVR4 ) || defined( SYSV ) || defined(CSRG_BASED) || defined(__linux__)
210         int             chld_stat ;
211 #else
212         union   wait    chld_stat ;
213 #endif
214
215         if (head->snf_file != NULL) {
216                 if (pipe(fd) != 0) {
217                         return  PIPE_ERROR;
218                 }
219                 switch (chld_pid = fork()) {
220                 case    0:
221                         close(1);
222                         if(dup(fd[1]) < 0) {
223                                 return(DUP_ERROR);
224                         }
225                         close(fd[0]);
226                         close(fd[1]);
227                         execl( oakgtobdf, oakgtobdf, head->snf_file, 0);
228                         return  EXEC_ERROR;
229                 case    -1:
230                         return(FORK_ERROR);
231                 default:
232                         break;
233                 }
234                 close(fd[1]);
235                 if((fp = (FILE *)fdopen(fd[0], "r")) == NULL) {
236                         close( fd[0] );
237                         kill( chld_pid, SIGKILL );
238                         WaitID( chld_pid, chld_stat ) ;
239                         return  FDOPEN_ERROR;
240                 }
241         } else {
242                 return(BDF_OPEN_HEAD);
243         }
244         head->input = fp ;
245         getstat = 0;
246         comflg = 0 ;
247
248         while ( 1 ) {
249                 if (fgets(buf, BUFSIZE, fp) == NULL) {
250                     fclose( fp );
251                     if (head->snf_file != NULL) {
252                         close(fd[0]);
253                         kill( chld_pid, SIGKILL );
254                         WaitID( chld_pid, chld_stat ) ;
255                     }
256                     return(BDF_INVAL);
257                 }
258                 p = buf;
259                 SCAN_TO_NONSP(p);
260
261                 if (!strncmp(p, CHARS, CHARSsz)) {
262                     if ((sscanf(p, "CHARS %d", &(head->num_chars))) != 1 ){
263                         return(BDF_INVAL);
264                     }
265                     getstat |= 0x04;
266                     break;
267                 }
268                 /*
269                  * write user comments
270                  */
271                 if ( !strncmp(p, "FONT", strlen("FONT"))
272                     && comment_list && !comflg
273                 ) {
274                     int i ;
275                     for( i=0; i<comment_num; i++ ){
276                         char    *ep ;
277                         if( (ep = (char *)strchr( comment_list[i], '\n' )) != NULL )
278                             *ep = '\0' ;
279                         if( comment_list[i] == '\0' )   continue ;
280                         fprintf(head->output, "COMMENT %s\n", comment_list[i]);
281                     }
282                     fprintf(head->output, "COMMENT\n");
283                     comflg++ ;
284                 }
285
286                 fprintf(head->output, "%s", buf);
287
288                 if (!strncmp(p, SIZE, SIZEsz)) {
289                     if ((sscanf(p, "SIZE %f%d",
290                         &(head->bdf_point), &(head->bdf_xdpi))) != 2) {
291                         fclose(fp);
292                         if (head->snf_file != NULL) {
293                             close(fd[0]);
294                             kill( chld_pid, SIGKILL );
295                             WaitID( chld_pid, chld_stat ) ;
296                         }
297                         return(BDF_INVAL);
298                     }
299                     getstat |= 0x01;
300                     continue;
301                 }
302                 if (!strncmp(p, FONTBOUNDINGBOX, FONTBOUNDINGBOXsz)) {
303                     if (( cnt = sscanf( p, "FONTBOUNDINGBOX %d%d%d%d",
304                                 &(head->bdf_width), &(head->bdf_height),
305                                 &(head->bdf_x), &(head->bdf_y))) != 4
306                     ) {
307                         fclose(fp);
308                         if (head->snf_file != NULL) {
309                             close(fd[0]);
310                             kill( chld_pid, SIGKILL );
311                             WaitID( chld_pid, chld_stat ) ;
312                         }
313                         return  BDF_INVAL;
314                     }
315                     getstat |= 0x02;
316                     continue;
317                 }
318                 get_charset_registry(head, p) ;
319         }
320
321         if (getstat != 0x07) {
322                 return  BDF_INVAL;
323         }
324
325         return  0;
326 }
327
328
329 static  int
330 readBdfToMemory(head, buf, code_num, code_list, make_all)
331 struct btophead *head;
332 char    *buf;
333 int     code_num ;      /* number of GPF code                   */
334 int     *code_list ;    /* pointer to GPF code lists            */
335 int     make_all ;      /* convert whole GPF fomat file to BDF  */
336 {
337         int     code, mwidth, num_char, bsize, rtn;
338         char    *ptn;
339
340         num_char = 0;
341         mwidth = (head->bdf_width + 7) / 8;
342         bsize = mwidth * head->bdf_height;
343         while(1) {
344             if ((rtn = GetBdfCode(head, buf, &code)) < 0) {
345                 return(rtn);    /* contain BDF_INVAL */
346             } else if (rtn == FILE_END) {
347                 head->num_chars = num_char;
348                 break;
349             }
350             if ( !make_all ) {
351                 if ( expCheckCode(code, code_num, code_list) ) {
352                     continue;
353                 }
354             }
355
356             head->code[num_char] = code;
357             if ( ( ptn = head->ptn[num_char++] = (char *)malloc( bsize ) ) == NULL ) {
358                 return(MALLOC_ERROR);
359             }
360
361             if ((rtn = GetBdfPtn(head, buf, ptn, mwidth, bsize)) != 0) {
362                 return(rtn);
363             }
364         }
365         return(0);
366 }