0704e6c3f13006ec1e19f4077cd56654c75769eb
[platform/upstream/kbd.git] / src / psfstriptable.c
1 /*
2  * psfgettable.c
3  *
4  * Extract a Unicode character table from a PSF font
5  *
6  * Copyright (C) 1994 H. Peter Anvin
7  *
8  * This program may be freely copied under the terms of the GNU
9  * General Public License (GPL), version 2, or at your option
10  * any later version.
11  *
12  * fix, aeb, 970316
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <sysexits.h>
18 #include <string.h>
19 #include "psf.h"
20 #include "nls.h"
21
22 typedef unsigned short unicode;
23 void usage(char *argv0)
24 {
25   fprintf(stderr, _("Usage: \n"
26                     "        %s psffont [outfile]\n"), argv0);
27   exit(EX_USAGE);
28 }
29
30 int
31 main(int argc, char *argv[])
32 {
33   FILE *in, *out;
34   char *inname;
35   struct psf_header psfhdr;
36   int fontlen;
37   char buffer[65536];           /* Font data, is scratch only */
38
39   setlocale(LC_ALL, "");
40   bindtextdomain(PACKAGE, LOCALEDIR);
41   textdomain(PACKAGE);
42
43   if ( argc < 2 || argc > 3 )
44     usage(argv[0]);
45
46   if ( !strcmp(argv[1],"-") )
47     {
48       in = stdin;
49       inname = "stdin";
50     }
51   else
52     {
53       in = fopen(inname = argv[1], "r");
54       if ( !in )
55         {
56           perror(inname);
57           exit(EX_NOINPUT);
58         }
59     }
60
61   if ( argc < 3 || !strcmp(argv[2],"-") )
62     out = stdout;
63   else
64     {
65       out = fopen(argv[2], "w");
66       if ( !out )
67         {
68           perror(argv[2]);
69           exit(EX_CANTCREAT);
70         }
71     }
72
73   if ( fread(&psfhdr, sizeof(struct psf_header), 1, in) < 1 )
74     {
75       fprintf(stderr, _("%s: Cannot read psf header\n"), inname);
76       exit(EX_DATAERR);
77     }
78   
79   if (! PSF_MAGIC_OK(psfhdr) )
80     {
81       fprintf(stderr, _("%s: Bad magic number\n"), inname);
82       exit(EX_DATAERR);
83     }
84   
85   if ( psfhdr.mode > PSF_MAXMODE )
86     {
87       fprintf(stderr, _("%s: Unknown mode number (%d)\n"),
88               inname, psfhdr.mode);
89       exit(EX_DATAERR);
90     }
91   
92   fontlen = ( psfhdr.mode & PSF_MODE512 ) ? 512 : 256;
93
94   if ( ! (psfhdr.mode & PSF_MODEHASTAB ) )
95     {
96       fprintf(stderr, _("%s: Font already had no character table\n"), inname);
97     }
98
99   psfhdr.mode &= ~PSF_MODEHASTAB; /* Clear the bit */
100   
101   /* Read font data */
102   if ( fread(buffer, psfhdr.charsize, fontlen, in) < fontlen )
103     {
104       perror(inname);
105       exit(EX_DATAERR);
106     }
107
108   fclose(in);
109
110   /* Write new font file */
111   fwrite(&psfhdr, sizeof(struct psf_header), 1, out);
112   fwrite(buffer, psfhdr.charsize, fontlen, out);
113   fclose(out);
114
115   exit(EX_OK);
116 }