1/*
2
3Copyright 1991, 1993, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included
12in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall
23not be used in advertising or otherwise to promote the sale, use or
24other dealings in this Software without prior written authorization
25from The Open Group.
26
27*/
28
29#ifdef HAVE_CONFIG_H
30# include "config.h"
31#endif
32
33#include <X11/X.h>
34#include <X11/Xproto.h>
35#include "fontmisc.h"
36#include <X11/fonts/fontstruct.h>
37#include "fntfilio.h"
38#include "fntfil.h"
39#include "bdfint.h"
40#include "pcf.h"
41#include <stdio.h>
42#include <X11/Xos.h>
43
44int
45main(int argc, char *argv[])
46{
47    FontRec font = { 0 };
48    FontFilePtr input, output;
49    char *input_name = NULL, *output_name = NULL;
50    char *program_name;
51    int bit, byte, glyph, scan;
52
53    FontDefaultFormat(&bit, &byte, &glyph, &scan);
54    program_name = argv[0];
55    argc--, argv++;
56    while (argc-- > 0) {
57        if (argv[0][0] == '-') {
58            switch (argv[0][1]) {
59            case 'p':
60                switch (argv[0][2]) {
61                case '1':
62                case '2':
63                case '4':
64                case '8':
65                    if (argv[0][3] != '\0')
66                        goto usage;
67                    glyph = argv[0][2] - '0';
68                    break;
69                default:
70                    goto usage;
71                }
72                break;
73
74            case 'u':
75                switch (argv[0][2]) {
76                case '1':
77                case '2':
78                case '4':
79                    if (argv[0][3] != '\0')
80                        goto usage;
81                    scan = argv[0][2] - '0';
82                    break;
83                default:
84                    goto usage;
85                }
86                break;
87
88            case 'm':
89                if (argv[0][2] != '\0')
90                    goto usage;
91                bit = MSBFirst;
92                break;
93
94            case 'l':
95                if (argv[0][2] != '\0')
96                    goto usage;
97                bit = LSBFirst;
98                break;
99
100            case 'M':
101                if (argv[0][2] != '\0')
102                    goto usage;
103                byte = MSBFirst;
104                break;
105
106            case 'L':
107                if (argv[0][2] != '\0')
108                    goto usage;
109                byte = LSBFirst;
110                break;
111
112            case 't':          /* attempt to make terminal fonts if possible */
113                if (argv[0][2] != '\0')
114                    goto usage;
115                break;
116
117            case 'i':          /* inhibit ink metric computation */
118                if (argv[0][2] != '\0')
119                    goto usage;
120                break;
121            case 'o':
122                if (argv[0][2])
123                    output_name = argv[0] + 2;
124                else {
125                    if (!argv[1])
126                        goto usage;
127                    argv++;
128                    argc--;
129                    output_name = argv[0];
130                }
131                break;
132
133            case 'v':
134                printf("%s\n", PACKAGE_STRING);
135                exit(0);
136
137            default:
138                goto usage;
139            }
140        }
141        else {
142            if (input_name) {
143 usage:
144                fprintf(stderr, "%s: invalid option '%s'\n",
145                        program_name, argv[0]);
146                fprintf(stderr,
147                        "usage: %s [-p#] [-u#] [-m] [-l] [-M] [-L] [-t] [-i] [-o pcf file] [bdf file]\n"
148                        "       where # for -p is 1, 2, 4, or 8\n"
149                        "       and   # for -u is 1, 2, or 4\n",
150                        program_name);
151                exit(1);
152            }
153            input_name = argv[0];
154        }
155        argv++;
156    }
157    if (input_name) {
158        input = FontFileOpen(input_name);
159        if (!input) {
160            fprintf(stderr, "%s: can't open bdf source file %s\n",
161                    program_name, input_name);
162            exit(1);
163        }
164    }
165    else
166        input = FontFileOpenFd(STDIN_FILENO);
167    if (bdfReadFont(&font, input, bit, byte, glyph, scan) != Successful) {
168        fprintf(stderr, "%s: bdf input, %s, corrupt\n",
169                program_name, input_name ? input_name : "<stdin>");
170        exit(1);
171    }
172    if (output_name) {
173        output = FontFileOpenWrite(output_name);
174        if (!output) {
175            fprintf(stderr, "%s: can't open pcf sink file %s\n",
176                    program_name, output_name);
177            exit(1);
178        }
179    }
180    else
181        output = FontFileOpenWriteFd(STDOUT_FILENO);
182    if (pcfWriteFont(&font, output) != Successful) {
183        fprintf(stderr, "%s: can't write pcf file %s\n",
184                program_name, output_name ? output_name : "<stdout>");
185        if (output_name)
186            remove(output_name);
187        exit(1);
188    }
189    else
190        FontFileClose(output);
191    return (0);
192}
193