121c2f794Smrg/*
2a5c37dfeSmrg
321c2f794SmrgCopyright 1990, 1998  The Open Group
421c2f794Smrg
521c2f794SmrgPermission to use, copy, modify, distribute, and sell this software and its
621c2f794Smrgdocumentation for any purpose is hereby granted without fee, provided that
721c2f794Smrgthe above copyright notice appear in all copies and that both that
821c2f794Smrgcopyright notice and this permission notice appear in supporting
921c2f794Smrgdocumentation.
1021c2f794Smrg
1121c2f794SmrgThe above copyright notice and this permission notice shall be included in
1221c2f794Smrgall copies or substantial portions of the Software.
1321c2f794Smrg
1421c2f794SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1521c2f794SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1621c2f794SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
1721c2f794SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
1821c2f794SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1921c2f794SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2021c2f794Smrg
2121c2f794SmrgExcept as contained in this notice, the name of The Open Group shall not be
2221c2f794Smrgused in advertising or otherwise to promote the sale, use or other dealings
2321c2f794Smrgin this Software without prior written authorization from The Open Group.
2421c2f794Smrg
2521c2f794Smrg * Copyright 1990 Network Computing Devices;
26a5c37dfeSmrg * Portions Copyright 1987 by Digital Equipment Corporation
2721c2f794Smrg *
2821c2f794Smrg * Permission to use, copy, modify, distribute, and sell this software and
2921c2f794Smrg * its documentation for any purpose is hereby granted without fee, provided
3021c2f794Smrg * that the above copyright notice appear in all copies and that both that
3121c2f794Smrg * copyright notice and this permission notice appear in supporting
3221c2f794Smrg * documentation, and that the names of Network Computing Devices, or Digital
3321c2f794Smrg * not be used in advertising or publicity pertaining to distribution
3421c2f794Smrg * of the software without specific, written prior permission.
3521c2f794Smrg *
3621c2f794Smrg * NETWORK COMPUTING DEVICES, AND DIGITAL DISCLAIM ALL WARRANTIES WITH
3721c2f794Smrg * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
3821c2f794Smrg * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES,
3921c2f794Smrg * OR DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
4021c2f794Smrg * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
4121c2f794Smrg * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
4221c2f794Smrg * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
4321c2f794Smrg * THIS SOFTWARE.
4421c2f794Smrg */
4521c2f794Smrg
46a8918085Smrg#ifdef HAVE_CONFIG_H
47c1ca42f1Smrg#include "config.h"
48a8918085Smrg#endif
49a8918085Smrg
5021c2f794Smrg#include	<stdio.h>
5121c2f794Smrg#include	<stdlib.h>
5221c2f794Smrg#include        <string.h>
5321c2f794Smrg#include	"fstobdf.h"
54a5c37dfeSmrg
55a5c37dfeSmrg
56a8918085Smrgstatic void _X_NORETURN _X_COLD
57a8918085Smrgusage(const char *progName, const char *msg)
5821c2f794Smrg{
59a8918085Smrg    if (msg)
60a8918085Smrg        fprintf(stderr, "%s: %s\n", progName, msg);
61a8918085Smrg    fprintf(stderr,
62c1ca42f1Smrg            "Usage: %s [-server <font server>] -fn <font name>\n"
63c1ca42f1Smrg            "	or: %s -version\n", progName, progName);
6421c2f794Smrg    exit(0);
6521c2f794Smrg}
6621c2f794Smrg
67a8918085Smrgstatic void _X_NORETURN _X_COLD
68a8918085SmrgFail(const char *progName)
6921c2f794Smrg{
7021c2f794Smrg    fprintf(stderr, "%s: unable to dump font\n", progName);
7121c2f794Smrg    exit(1);
7221c2f794Smrg}
7321c2f794Smrg
7421c2f794Smrgint
7521c2f794Smrgmain(int argc, char *argv[])
7621c2f794Smrg{
77c1ca42f1Smrg    FSServer *fontServer;
78c1ca42f1Smrg    Font fontID, dummy;
7921c2f794Smrg    FSBitmapFormat bitmapFormat;
8021c2f794Smrg    FSXFontInfoHeader fontHeader;
81c1ca42f1Smrg    FSPropInfo propInfo;
8221c2f794Smrg    FSPropOffset *propOffsets;
8321c2f794Smrg    unsigned char *propData;
8421c2f794Smrg
85c1ca42f1Smrg    FILE *outFile;
86c1ca42f1Smrg    char *fontName;
87c1ca42f1Smrg    char *serverName;
88c1ca42f1Smrg    int i;
8921c2f794Smrg
9021c2f794Smrg    fontName = NULL;
9121c2f794Smrg    serverName = NULL;
9221c2f794Smrg    outFile = stdout;
9321c2f794Smrg
9421c2f794Smrg    for (i = 1; i < argc; i++) {
95c1ca42f1Smrg        if (!strncmp(argv[i], "-s", 2)) {
96c1ca42f1Smrg            if (argv[++i])
97c1ca42f1Smrg                serverName = argv[i];
98c1ca42f1Smrg            else
99c1ca42f1Smrg                usage(argv[0], "-server requires an argument");
100c1ca42f1Smrg        }
101c1ca42f1Smrg        else if (!strncmp(argv[i], "-fn", 3)) {
102c1ca42f1Smrg            if (argv[++i])
103c1ca42f1Smrg                fontName = argv[i];
104c1ca42f1Smrg            else
105c1ca42f1Smrg                usage(argv[0], "-fn requires an argument");
106c1ca42f1Smrg        }
107c1ca42f1Smrg        else if (!strcmp(argv[i], "-version")) {
108c1ca42f1Smrg            printf("%s\n", PACKAGE_STRING);
109c1ca42f1Smrg            exit(0);
110c1ca42f1Smrg        }
111c1ca42f1Smrg        else {
112c1ca42f1Smrg            fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], argv[i]);
113c1ca42f1Smrg            usage(argv[0], NULL);
114c1ca42f1Smrg        }
11521c2f794Smrg    }
11621c2f794Smrg
11721c2f794Smrg    if (fontName == NULL)
118c1ca42f1Smrg        usage(argv[0], "No font name specified");
11921c2f794Smrg
12021c2f794Smrg    fontServer = FSOpenServer(serverName);
12121c2f794Smrg    if (!fontServer) {
122c1ca42f1Smrg        const char *sn = FSServerName(serverName);
123c1ca42f1Smrg
124c1ca42f1Smrg        if (sn)
125c1ca42f1Smrg            fprintf(stderr, "%s: can't open font server \"%s\"\n", argv[0], sn);
126c1ca42f1Smrg        else
127c1ca42f1Smrg            usage(argv[0], "No font server specified.");
128c1ca42f1Smrg        exit(0);
12921c2f794Smrg    }
13021c2f794Smrg    bitmapFormat = 0;
13121c2f794Smrg    fontID = FSOpenBitmapFont(fontServer, bitmapFormat, (FSBitmapFormatMask) 0,
132c1ca42f1Smrg                              fontName, &dummy);
13321c2f794Smrg    if (!fontID) {
134c1ca42f1Smrg        printf("can't open font \"%s\"\n", fontName);
135c1ca42f1Smrg        exit(0);
13621c2f794Smrg    }
13721c2f794Smrg    FSQueryXInfo(fontServer, fontID, &fontHeader, &propInfo, &propOffsets,
138c1ca42f1Smrg                 &propData);
13921c2f794Smrg
14021c2f794Smrg    if (!EmitHeader(outFile, &fontHeader, &propInfo, propOffsets, propData))
141c1ca42f1Smrg        Fail(argv[0]);
14221c2f794Smrg    if (!EmitProperties(outFile, &fontHeader, &propInfo, propOffsets, propData))
143c1ca42f1Smrg        Fail(argv[0]);
14421c2f794Smrg    if (!EmitCharacters(outFile, fontServer, &fontHeader, fontID))
145c1ca42f1Smrg        Fail(argv[0]);
14621c2f794Smrg    fprintf(outFile, "ENDFONT\n");
14721c2f794Smrg
14821c2f794Smrg    FSFree((char *) propOffsets);
14921c2f794Smrg    FSFree((char *) propData);
150c1ca42f1Smrg    exit(0);
15121c2f794Smrg}
152