2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: fontpath.c /main/4 1995/10/27 16:13:29 rswiston $ */
25 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
26 * (c) Copyright 1993, 1994 International Business Machines Corp. *
27 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
28 * (c) Copyright 1993, 1994 Novell, Inc. *
31 ** fontpath.c - font path modification routines
33 ** $fontpath.c,v 1.1 93/06/24 14:52:10 bill Exp $
35 ** Copyright 1993 Hewlett-Packard Company
40 # include <X11/Xatom.h>
50 int ApplyFontPathMods( struct display *d, Display *dpy );
51 void GetSysParms( char **tzpp, char **fhpp, char **ftpp );
52 static int PathInPList(char *path, char **fplist, int listlen);
53 static int PathInZList(char *path, char *fplist, int listlen);
54 static int SeparateParts( char **path );
58 DebugFontPath(char *note, char **path, int nelems)
61 Debug(" %s: %d elements\n",note,nelems);
62 for (i=0; i<nelems; i++)
63 Debug(" %s\n",path[i]);
67 ErrorHandler(Display *dpy, XErrorEvent *event)
69 XmuPrintDefaultErrorMessage(dpy, event, stderr);
70 return 0; /* nonfatal */
73 /* ________________________________________________________________
75 **| ApplyFontPathMods(d) - |
77 **| If new font path mods are mandated, fabricate and apply |
78 **| an appropriate new complete X server font path. |
80 **| Specify: d = pointer to display structure |
82 **| Returns: nothing |
83 **|________________________________________________________________|
87 ApplyFontPathMods( struct display *d, Display *dpy )
93 char **fontPath,**newList;
98 Debug("ApplyFontPathMods() for %s\n",d->name);
100 if (d->displayType.location == Foreign) {
101 Debug(" Foreign display\n");
105 if (!(fontPath=XGetFontPath(dpy, &numPaths))) {
106 Debug(" Can't get font path\n");
111 ** Font path mods can come from (in priority order):
113 ** 1. FONT_PATH_HEAD/TAIL definitions in /etc/src.sh
114 ** 2. fontPathHead/Tail resources
117 GetSysParms(0,&fph,&fpt);
119 if (fph) { s = "sys parm file"; }
120 else if (fpHead) { s = "resource"; fph = strdup(fpHead); }
121 if (fph && !*fph) { free(fph); fph = NULL; }
122 if (fph) Debug(" +fp (%s) %s\n",s,fph);
124 if (fpt) { s = "sys parm file"; }
125 else if (fpTail) { s = "resource"; fpt = strdup(fpTail); }
126 if (fpt && !*fpt) { free(fpt); fpt = NULL; }
127 if (fpt) Debug(" fp+ (%s) %s\n",s,fpt);
130 ** Break up fph and fpt into constituent parts and
131 ** then reconstruct the complete, modified font path.
132 ** During reconstruction we also eliminate redundancies.
135 numHeads = SeparateParts(&fph);
136 numTails = SeparateParts(&fpt);
137 if (numHeads || numTails) {
138 newList = (char **) malloc((numHeads+numPaths+numTails)
141 for (s=fph, i=j=0; j<numHeads; j++) {
142 if (!PathInPList(s,newList,i))
146 for (j=0; j<numPaths; j++) {
147 if (!PathInPList(fontPath[j],newList,i) &&
148 !PathInZList(fontPath[j],fpt,numTails)) {
149 newList[i++] = fontPath[j];
152 for (s=fpt, j=0; j<numTails; j++) {
153 if (!PathInPList(s,newList,i))
158 DebugFontPath("Request (XSetFontPath)",newList,i);
161 ** Tell X server to set new font path now. Log failure,
162 ** but don't let it be fatal. (Note that caller should
163 ** reset error handler to elsewhere when we return.)
166 (void)XSetErrorHandler(ErrorHandler);
167 XSetFontPath(dpy, newList, i);
170 if (debugLevel > 0) {
171 newList = XGetFontPath(dpy, &i);
172 DebugFontPath("Confirm (XGetFontPath)",newList,i);
173 XFreeFontPath(newList);
180 XFreeFontPath(fontPath);
183 /* ___________________________________________________________________
185 **| PathInPList(path,fplist,listlen) - |
187 **| Determine if a specific fontpath element is in a list, |
188 **| taking into account that identical elements may be formed |
189 **| differently (with multiple embedded and trailing slashes). |
191 **| Specify: (char *)path = the single element to be tested |
192 **| (char **)fplist = list of ptrs to asciz elements |
193 **| (int)listlen = number of pointers in the list |
195 **| Returns: TRUE if element is in the list |
196 **|___________________________________________________________________|
200 PathInPList(char *path, char **fplist, int listlen)
203 while (listlen-- > 0) {
204 for (s=path, t=fplist[listlen]; *s && (*s == *t); ) {
205 t++; while (*t == '/') t++;
206 s++; while (*s == '/') s++;
208 if (!*s && !*t) return 1;
213 /* ___________________________________________________________________
215 **| PathInZList(path,fplist,listlen) - |
217 **| Determine if a specific fontpath element is in a list, |
218 **| taking into account that identical elements may be formed |
219 **| differently (with multiple embedded and trailing slashes). |
221 **| Specify: (char *)path = the single element to be tested |
222 **| (char *)fplist = list of concatenated asciz elements |
223 **| (int)listlen = number of elements in the list |
225 **| Returns: TRUE if element is in the list |
226 **|___________________________________________________________________|
230 PathInZList(char *path, char *fplist, int listlen)
233 for (t=fplist; listlen > 0; listlen--) {
234 for (s=path; *s && (*s == *t); ) {
235 t++; while (*t == '/') t++;
236 s++; while (*s == '/') s++;
238 if (!*s && !*t) return 1;
244 /* ________________________________________________________________
246 **| SeparateParts(path) |
248 **| Break a comma-delimited asciz path string into its |
249 **| separate asciz constituent parts. |
251 **| Specify: path = ptr to asciz path string (e.g., "as,df,jk") |
253 **| Returns: number of constituent parts, with path string |
254 **| converted into as many sequential asciz strings |
255 **| (e.g., "as\0df\0jk"). |
256 **|________________________________________________________________|
260 SeparateParts( char **path )
265 for (s=*path; t=strtok(s,","); s=NULL, nparts++);
269 /* ___________________________________________________________________
271 **| GetSysParms(tzpp,fhpp,ftpp) - |
273 **| Extract TZ, FONT_PATH_HEAD, and FONT_PATH_TAIL definitions |
274 **| from the sys parms file (typically /etc/src.sh). |
276 **| Specify: (char **)tzpp = where to put ptr to TZ string |
277 **| (char **)fhpp = where to put ptr to FONT_PATH_HEAD |
278 **| (char **)ftpp = where to put ptr to FONT_PATH_TAIL |
280 **| Specify a NULL (char **) for any undesired strings. |
282 **| Returns: Appropriate pointers to malloc'ed strings (NULL pointer |
283 **| if a string is neither requested nor defined). |
284 **|___________________________________________________________________|
288 GetSysParms( char **tzpp, char **fhpp, char **ftpp )
293 if (tzpp) *tzpp = NULL;
294 if (fhpp) *fhpp = NULL;
295 if (ftpp) *ftpp = NULL;
296 if ((*sysParmsFile != '/') || !(fin=fopen(sysParmsFile,"r"))) {
297 Debug("(GetSysParms) Can't open sys parms file\n");
300 Debug("(GetSysParms) Reading sys parms file\n");
301 while (fgets(buf,255,fin)) {
302 for (t=buf; *t && *t<=' '; t++); /* t -> EOS or nonblank */
303 if (!*t || *t=='#') continue; /* ignore comment lines */
304 while (*t && *t!='\n') t++; /* t -> EOS or newline */
305 if (*t) *t = '\0'; /* discard newline char */
307 if ((s=strstr(buf,"TZ=")) && (t=strtok(s+3,"; \t")))
308 *tzpp = (char *) strdup(t);
310 if ((s=strstr(buf,"FONT_PATH_HEAD=")) && (t=strtok(s+15,"; \t")))
311 *fhpp = (char *) strdup(t);
313 if ((s=strstr(buf,"FONT_PATH_TAIL=")) && (t=strtok(s+15,"; \t")))
314 *ftpp = (char *) strdup(t);