util/dttypes: remove register keyword
[oweals/cde.git] / cde / programs / dtudcfonted / dtbdftocpf / bdftocpf.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: bdftocpf.c /main/6 1996/11/08 02:01:17 cde-fuj $ */
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
31
32 #include        <stdio.h>
33 #include        <signal.h>
34 #include        <sys/types.h>
35 #include        <sys/stat.h>
36 #include        <string.h>
37 #include        <stdlib.h>
38 #include        <unistd.h>
39 #include        "bdfgpf.h"
40 #include        "udcutil.h"
41
42 static void put_error();
43 static void put_help() ;
44 static void Usage() ;
45 static  void    sigint_out() ;
46 static  int     CnvBDFtoGPF() ;
47
48 static struct btophead Head;
49
50 static  void
51 sigint_out()
52 {
53     if (Head.out_file) {
54         unlink(Head.out_file);
55     }
56     exit(0);
57 }
58
59 main(argc, argv)
60 int     argc;
61 char    *argv[];
62 {
63     int rtn, i;
64     char   *GetTmpPath();
65     struct stat st;
66
67     for (i=1; i<argc; i++) {
68         if (!strcmp(argv[i], "-help")) {
69             put_help(argv[0]);
70             exit(0);
71         }
72     }
73
74     if (!(argc % 2)) {
75         Usage(argv[0]);
76     }
77         
78     Head.in_file = Head.out_file = Head.text_file = NULL;
79     Head.p_width = Head.p_height = USE_ORG_SIZE;
80
81
82     Head.start_code = MIN_CODE ;
83     Head.end_code = MAX_CODE ;
84     Head.code_category = ALL_CODE;
85
86     for (i=1; i<argc; i+=2) {
87         if (!strcmp(argv[i], "-p")) {
88             Head.out_file = argv[i+1];
89         } else if (!strcmp(argv[i], "-bdf")) {
90             Head.in_file = argv[i+1];
91         } else if (!strcmp(argv[i], "-width")) {
92             Head.p_width = atoi(argv[i+1]);
93             if ((Head.p_width > MAX_FONT_WIDTH) ||
94                 (Head.p_width < MIN_FONT_WIDTH)) {
95                 USAGE2("%s: The specification of the character width is improper.\"%s\"\n",
96                         argv[0], argv[i+1]);
97                 exit(PARAM_ERROR * (-1));
98             }
99         } else if (!strcmp(argv[i], "-height")) {
100             Head.p_height = atoi(argv[i+1]);
101             if ((Head.p_height > MAX_FONT_HEIGHT) ||
102                 (Head.p_height < MIN_FONT_HEIGHT)) {
103                 USAGE2("%s: The specification of the character height is improper.\"%s\"\n",
104                         argv[0], argv[i+1]);
105                 exit(PARAM_ERROR * (-1));
106             }
107         } else {
108             Usage(argv[0]);
109         }
110     }
111
112     if (Head.in_file == NULL) {
113         Head.input = stdin;
114     } else {
115         char    *spacing ;
116         char    *xlfdname ;
117         spacing = xlfdname = NULL ;
118         /* refuse proportional fonts */
119         if ( GetUdcFontName( NULL, Head.in_file, &xlfdname ) ) {
120             USAGE1("%s : This font cannot get XLFD. Terminates abnormally.\n", argv[0]);
121             exit( 1 );
122         }
123         GETSPACINGSTR( spacing, xlfdname ) ;
124         if ( !strcmp( "p", spacing ) || !strcmp( "P", spacing ) ) {
125             USAGE2("%s cannot edit proportional fonts.(SPACING \"%s\")\n", argv[0], spacing );
126             exit( 1 );
127         }
128         if ((Head.input = fopen(Head.in_file, "r")) == NULL) {
129             USAGE2("%s:  The BDF file cannot be opened.\"%s\"\n",
130                 argv[0], Head.in_file); 
131             exit(BDF_OPEN * (-1));
132         }
133     }
134
135     signal(SIGHUP, (void(*)())sigint_out);
136     signal(SIGINT, (void(*)())sigint_out);
137     signal(SIGQUIT, (void(*)())sigint_out);
138     signal(SIGTERM, (void(*)())sigint_out);
139
140     if (Head.out_file == NULL) {
141         Head.output = stdout;
142     } else {
143         if (stat(Head.out_file, &st) != 0) {
144             if ((Head.output = fopen(Head.out_file, "w")) == NULL) {
145                 USAGE2("%s: The character pattern file cannot be opened. \"%s\"\n",
146                     argv[0], Head.out_file); 
147                 exit(GPF_OPEN *(-1));
148             }
149         } else {
150             Head.text_file = Head.out_file;
151             if ((Head.out_file = GetTmpPath( Head.out_file )) == NULL) {
152                 USAGE1("%s: Failed convert.\n", argv[0]); 
153                 exit(FATAL_ERROR * (-1));
154             }
155             if ((Head.output = fopen(Head.out_file, "w")) == NULL) {
156                 USAGE2("%s: The character pattern file cannot be opened. \"%s\"\n",
157                     argv[0], Head.out_file); 
158                 exit(GPF_OPEN * (-1));
159             }
160         }
161     }
162
163     if ((rtn = CnvBDFtoGPF(&Head))) {
164         if (Head.in_file != NULL) {
165             fclose(Head.input);
166         }
167         if (Head.out_file != NULL) {
168             fclose(Head.output);
169             unlink(Head.out_file);
170         }
171         put_error(&Head, rtn, argv[0]);
172         exit(rtn * (-1));
173     }
174
175     if (Head.in_file != NULL) {
176         fclose(Head.input);
177     }
178     if (Head.out_file != NULL) {
179         fclose(Head.output);
180         signal(SIGHUP, SIG_IGN);
181         signal(SIGINT, SIG_IGN);
182         signal(SIGQUIT, SIG_IGN);
183         signal(SIGTERM, SIG_IGN);
184         if (Head.text_file != NULL) {
185             Link_NewFile(Head.out_file, Head.text_file);
186             unlink(Head.out_file);
187         }
188     }
189     exit(0);
190 }
191
192 static
193 CnvBDFtoGPF(head)
194 struct btophead *head;
195 {
196     char    bdfbuf[BUFSIZE];
197     int     rtn;
198
199     if ((rtn = ReadBdfHeader(head, bdfbuf)) < 0) {
200         return(rtn);
201     }
202
203     if (head->p_width == USE_ORG_SIZE) {
204         head->p_width = head->bdf_width;
205     }
206     if (head->p_height == USE_ORG_SIZE) {
207         head->p_height = head->bdf_height;
208     }
209     if ((head->p_width != head->bdf_width) ||
210          (head->p_height != head->bdf_height)) {
211         head->zoomf = 1;
212     } else {
213         head->zoomf = 0;
214     }
215
216     if ((head->code = (int *)malloc(sizeof(int)*head->num_chars)) == NULL) {
217         return(FATAL_ERROR);
218     }
219
220     if ((head->ptn = (char **)malloc(sizeof(char *)*head->num_chars)) == NULL) {
221         return(FATAL_ERROR);
222     }
223
224     if ((rtn = ReadBdfToMemory(head, bdfbuf))) {
225         return(rtn);
226     }
227
228     WriteGpfHeader(head);
229
230     if ((rtn = WritePtnToGpf(head))) {
231         return(rtn);
232     }
233
234     return(0);
235 }
236
237 static void
238 put_error(head, er_no, prog_name)
239 struct btophead *head;
240 int    er_no;
241 char   *prog_name;
242 {
243     switch(er_no) {
244         case FATAL_ERROR :
245             USAGE1("%s: Failed convert.\n", prog_name);
246             break;
247
248         case BDF_OPEN :
249             USAGE2("%s: The BDF file cannot be opened. \"%s\"\n",
250                 prog_name, head->in_file);
251             break;
252
253         case BDF_READ :
254             USAGE2("%s:  The BDF file cannot be read. \"%s\"\n",
255                 prog_name, head->in_file);
256             break;
257
258         case BDF_INVAL :
259             USAGE2("%s: The format of the BDF file is illegal. \"%s\"\n",
260                 prog_name, head->in_file);
261             break;
262
263         case GPF_OPEN :
264             USAGE2("%s: The character pattern file cannot be opened. \"%s\"\n",
265                 prog_name, head->out_file);
266             break;
267
268         case GPF_WRITE :
269             USAGE2("%s: It is not possible to write to the character pattern file. \"%s\"\n",
270                 prog_name, head->out_file);
271             break;
272
273         default :
274             break;
275     }
276 }
277
278 static  void
279 put_help(prog_name)
280 char    *prog_name;
281 {
282     USAGE1("Usage: %s [-p character_pattern_file_name] [-bdf BDF_file_name]\n", prog_name); 
283     USAGE("\t\t[-width character_width] [-height character_height] [-help]\n\n");
284 }
285
286 static  void
287 Usage(prog_name)
288 char    *prog_name;
289 {
290     put_help(prog_name);
291     exit(PARAM_ERROR * (-1));
292 }