fontpath.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  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)
  10. * any later version.
  11. *
  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
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: fontpath.c /main/4 1995/10/27 16:13:29 rswiston $ */
  24. /* *
  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. *
  29. */
  30. /*
  31. ** fontpath.c - font path modification routines
  32. **
  33. ** $fontpath.c,v 1.1 93/06/24 14:52:10 bill Exp $
  34. **
  35. ** Copyright 1993 Hewlett-Packard Company
  36. */
  37. # include "dm.h"
  38. # include <signal.h>
  39. # include <X11/Xatom.h>
  40. # include <X11/Xmu/Error.h>
  41. #if defined(__FreeBSD__)
  42. # include <utmpx.h>
  43. #else
  44. # include <utmp.h>
  45. #endif
  46. # include "vgproto.h"
  47. /*
  48. ** Prototypes
  49. */
  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 );
  55. void GenerateAuthData(char *auth, int len); // genauth.c
  56. static void
  57. DebugFontPath(char *note, char **path, int nelems)
  58. {
  59. int i;
  60. Debug(" %s: %d elements\n",note,nelems);
  61. for (i=0; i<nelems; i++)
  62. Debug(" %s\n",path[i]);
  63. }
  64. static int
  65. ErrorHandler(Display *dpy, XErrorEvent *event)
  66. {
  67. XmuPrintDefaultErrorMessage(dpy, event, stderr);
  68. return 0; /* nonfatal */
  69. }
  70. /* ________________________________________________________________
  71. **| |
  72. **| ApplyFontPathMods(d) - |
  73. **| |
  74. **| If new font path mods are mandated, fabricate and apply |
  75. **| an appropriate new complete X server font path. |
  76. **| |
  77. **| Specify: d = pointer to display structure |
  78. **| |
  79. **| Returns: nothing |
  80. **|________________________________________________________________|
  81. */
  82. int
  83. ApplyFontPathMods( struct display *d, Display *dpy )
  84. {
  85. FILE *fin;
  86. char *s,*t;
  87. char *fph = NULL;
  88. char *fpt = NULL;
  89. char **fontPath,**newList;
  90. int numHeads = 0;
  91. int numTails = 0;
  92. int i,j,k,numPaths;
  93. Debug("ApplyFontPathMods() for %s\n",d->name);
  94. if (d->displayType.location == Foreign) {
  95. Debug(" Foreign display\n");
  96. return 0;
  97. }
  98. if (!(fontPath=XGetFontPath(dpy, &numPaths))) {
  99. Debug(" Can't get font path\n");
  100. return 0;
  101. }
  102. /*
  103. ** Font path mods can come from (in priority order):
  104. **
  105. ** 1. FONT_PATH_HEAD/TAIL definitions in /etc/src.sh
  106. ** 2. fontPathHead/Tail resources
  107. */
  108. GetSysParms(0,&fph,&fpt);
  109. if (fph) { s = "sys parm file"; }
  110. else if (fpHead) { s = "resource"; fph = strdup(fpHead); }
  111. if (fph && !*fph) { free(fph); fph = NULL; }
  112. if (fph) Debug(" +fp (%s) %s\n",s,fph);
  113. if (fpt) { s = "sys parm file"; }
  114. else if (fpTail) { s = "resource"; fpt = strdup(fpTail); }
  115. if (fpt && !*fpt) { free(fpt); fpt = NULL; }
  116. if (fpt) Debug(" fp+ (%s) %s\n",s,fpt);
  117. /*
  118. ** Break up fph and fpt into constituent parts and
  119. ** then reconstruct the complete, modified font path.
  120. ** During reconstruction we also eliminate redundancies.
  121. */
  122. numHeads = SeparateParts(&fph);
  123. numTails = SeparateParts(&fpt);
  124. if (numHeads || numTails) {
  125. newList = (char **) malloc((numHeads+numPaths+numTails)
  126. * sizeof(char *));
  127. if (newList) {
  128. for (s=fph, i=j=0; j<numHeads; j++) {
  129. if (!PathInPList(s,newList,i))
  130. newList[i++] = s;
  131. while (*s) s++; s++;
  132. }
  133. for (j=0; j<numPaths; j++) {
  134. if (!PathInPList(fontPath[j],newList,i) &&
  135. !PathInZList(fontPath[j],fpt,numTails)) {
  136. newList[i++] = fontPath[j];
  137. }
  138. }
  139. for (s=fpt, j=0; j<numTails; j++) {
  140. if (!PathInPList(s,newList,i))
  141. newList[i++] = s;
  142. while (*s) s++; s++;
  143. }
  144. if (debugLevel > 0)
  145. DebugFontPath("Request (XSetFontPath)",newList,i);
  146. /*
  147. ** Tell X server to set new font path now. Log failure,
  148. ** but don't let it be fatal. (Note that caller should
  149. ** reset error handler to elsewhere when we return.)
  150. */
  151. (void)XSetErrorHandler(ErrorHandler);
  152. XSetFontPath(dpy, newList, i);
  153. XSync(dpy, True);
  154. free(newList);
  155. if (debugLevel > 0) {
  156. newList = XGetFontPath(dpy, &i);
  157. DebugFontPath("Confirm (XGetFontPath)",newList,i);
  158. XFreeFontPath(newList);
  159. }
  160. }
  161. }
  162. if (fph) free(fph);
  163. if (fpt) free(fpt);
  164. XFreeFontPath(fontPath);
  165. return 1;
  166. }
  167. /* ___________________________________________________________________
  168. **| |
  169. **| PathInPList(path,fplist,listlen) - |
  170. **| |
  171. **| Determine if a specific fontpath element is in a list, |
  172. **| taking into account that identical elements may be formed |
  173. **| differently (with multiple embedded and trailing slashes). |
  174. **| |
  175. **| Specify: (char *)path = the single element to be tested |
  176. **| (char **)fplist = list of ptrs to asciz elements |
  177. **| (int)listlen = number of pointers in the list |
  178. **| |
  179. **| Returns: TRUE if element is in the list |
  180. **|___________________________________________________________________|
  181. */
  182. static int
  183. PathInPList(char *path, char **fplist, int listlen)
  184. {
  185. char *s,*t;
  186. while (listlen-- > 0) {
  187. for (s=path, t=fplist[listlen]; *s && (*s == *t); ) {
  188. t++; while (*t == '/') t++;
  189. s++; while (*s == '/') s++;
  190. }
  191. if (!*s && !*t) return 1;
  192. }
  193. return 0;
  194. }
  195. /* ___________________________________________________________________
  196. **| |
  197. **| PathInZList(path,fplist,listlen) - |
  198. **| |
  199. **| Determine if a specific fontpath element is in a list, |
  200. **| taking into account that identical elements may be formed |
  201. **| differently (with multiple embedded and trailing slashes). |
  202. **| |
  203. **| Specify: (char *)path = the single element to be tested |
  204. **| (char *)fplist = list of concatenated asciz elements |
  205. **| (int)listlen = number of elements in the list |
  206. **| |
  207. **| Returns: TRUE if element is in the list |
  208. **|___________________________________________________________________|
  209. */
  210. static int
  211. PathInZList(char *path, char *fplist, int listlen)
  212. {
  213. char *s,*t;
  214. for (t=fplist; listlen > 0; listlen--) {
  215. for (s=path; *s && (*s == *t); ) {
  216. t++; while (*t == '/') t++;
  217. s++; while (*s == '/') s++;
  218. }
  219. if (!*s && !*t) return 1;
  220. while (*t) t++; t++;
  221. }
  222. return 0;
  223. }
  224. /* ________________________________________________________________
  225. **| |
  226. **| SeparateParts(path) |
  227. **| |
  228. **| Break a comma-delimited asciz path string into its |
  229. **| separate asciz constituent parts. |
  230. **| |
  231. **| Specify: path = ptr to asciz path string (e.g., "as,df,jk") |
  232. **| |
  233. **| Returns: number of constituent parts, with path string |
  234. **| converted into as many sequential asciz strings |
  235. **| (e.g., "as\0df\0jk"). |
  236. **|________________________________________________________________|
  237. */
  238. static int
  239. SeparateParts( char **path )
  240. {
  241. char *t,*s;
  242. int nparts = 0;
  243. if (path && *path)
  244. for (s=*path; t=strtok(s,","); s=NULL, nparts++);
  245. return nparts;
  246. }
  247. /* ___________________________________________________________________
  248. **| |
  249. **| GetSysParms(tzpp,fhpp,ftpp) - |
  250. **| |
  251. **| Extract TZ, FONT_PATH_HEAD, and FONT_PATH_TAIL definitions |
  252. **| from the sys parms file (typically /etc/src.sh). |
  253. **| |
  254. **| Specify: (char **)tzpp = where to put ptr to TZ string |
  255. **| (char **)fhpp = where to put ptr to FONT_PATH_HEAD |
  256. **| (char **)ftpp = where to put ptr to FONT_PATH_TAIL |
  257. **| |
  258. **| Specify a NULL (char **) for any undesired strings. |
  259. **| |
  260. **| Returns: Appropriate pointers to malloc'ed strings (NULL pointer |
  261. **| if a string is neither requested nor defined). |
  262. **|___________________________________________________________________|
  263. */
  264. void
  265. GetSysParms( char **tzpp, char **fhpp, char **ftpp )
  266. {
  267. FILE *fin;
  268. char *s,*t,buf[256];
  269. if (tzpp) *tzpp = NULL;
  270. if (fhpp) *fhpp = NULL;
  271. if (ftpp) *ftpp = NULL;
  272. if ((*sysParmsFile != '/') || !(fin=fopen(sysParmsFile,"r"))) {
  273. Debug("(GetSysParms) Can't open sys parms file\n");
  274. return;
  275. }
  276. Debug("(GetSysParms) Reading sys parms file\n");
  277. while (fgets(buf,255,fin)) {
  278. for (t=buf; *t && *t<=' '; t++); /* t -> EOS or nonblank */
  279. if (!*t || *t=='#') continue; /* ignore comment lines */
  280. while (*t && *t!='\n') t++; /* t -> EOS or newline */
  281. if (*t) *t = '\0'; /* discard newline char */
  282. if (tzpp && !*tzpp)
  283. if ((s=strstr(buf,"TZ=")) && (t=strtok(s+3,"; \t")))
  284. *tzpp = (char *) strdup(t);
  285. if (fhpp && !*fhpp)
  286. if ((s=strstr(buf,"FONT_PATH_HEAD=")) && (t=strtok(s+15,"; \t")))
  287. *fhpp = (char *) strdup(t);
  288. if (ftpp && !*ftpp)
  289. if ((s=strstr(buf,"FONT_PATH_TAIL=")) && (t=strtok(s+15,"; \t")))
  290. *ftpp = (char *) strdup(t);
  291. }
  292. fclose(fin);
  293. }