FontNames.c revision c555af55
1/*
2
3Copyright 1986, 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 in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21Except as contained in this notice, the name of The Open Group shall not be
22used in advertising or otherwise to promote the sale, use or other dealings
23in this Software without prior written authorization from The Open Group.
24
25*/
26
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31#include "Xlibint.h"
32#include <limits.h>
33
34char **
35XListFonts(
36register Display *dpy,
37_Xconst char *pattern,  /* null-terminated */
38int maxNames,
39int *actualCount)	/* RETURN */
40{
41    register long nbytes;
42    register unsigned i;
43    register int length;
44    char **flist = NULL;
45    char *ch = NULL;
46    char *chend;
47    int count = 0;
48    xListFontsReply rep;
49    register xListFontsReq *req;
50    unsigned long rlen;
51
52    LockDisplay(dpy);
53    GetReq(ListFonts, req);
54    req->maxNames = maxNames;
55    nbytes = req->nbytes = pattern ? strlen (pattern) : 0;
56    req->length += (nbytes + 3) >> 2;
57    _XSend (dpy, pattern, nbytes);
58    /* use _XSend instead of Data, since following _XReply will flush buffer */
59
60    if (!_XReply (dpy, (xReply *)&rep, 0, xFalse)) {
61	*actualCount = 0;
62	UnlockDisplay(dpy);
63	SyncHandle();
64	return (char **) NULL;
65    }
66
67    if (rep.nFonts) {
68	flist = Xmalloc (rep.nFonts * sizeof(char *));
69	if (rep.length < (INT_MAX >> 2)) {
70	    rlen = rep.length << 2;
71	    ch = Xmalloc(rlen + 1);
72	    /* +1 to leave room for last null-terminator */
73	}
74
75	if ((! flist) || (! ch)) {
76	    if (flist) Xfree(flist);
77	    if (ch) Xfree(ch);
78	    _XEatDataWords(dpy, rep.length);
79	    *actualCount = 0;
80	    UnlockDisplay(dpy);
81	    SyncHandle();
82	    return (char **) NULL;
83	}
84
85	_XReadPad (dpy, ch, rlen);
86	/*
87	 * unpack into null terminated strings.
88	 */
89	chend = ch + (rlen + 1);
90	length = *(unsigned char *)ch;
91	*ch = 1; /* make sure it is non-zero for XFreeFontNames */
92	for (i = 0; i < rep.nFonts; i++) {
93	    if (ch + length < chend) {
94		flist[i] = ch + 1;  /* skip over length */
95		ch += length + 1;  /* find next length ... */
96		length = *(unsigned char *)ch;
97		*ch = '\0';  /* and replace with null-termination */
98		count++;
99	    } else
100		flist[i] = NULL;
101	}
102    }
103    *actualCount = count;
104    UnlockDisplay(dpy);
105    SyncHandle();
106    return (flist);
107}
108
109int
110XFreeFontNames(char **list)
111{
112	if (list) {
113		if (!*(list[0]-1)) { /* from ListFontsWithInfo */
114			register char **names;
115			for (names = list+1; *names; names++)
116				Xfree (*names);
117		}
118		Xfree (list[0]-1);
119		Xfree (list);
120	}
121	return 1;
122}
123