1 /* $XFree86: xc/lib/font/fontfile/dirfile.c,v 3.3 1997/01/27 06:56:28 dawes Exp $ */
4 "$XConsortium: dirfile.c /main/12 1995/12/08 19:02:23 gildea $";
9 Copyright (c) 1991 X Consortium
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
18 The above copyright notice and this permission notice shall be included in
19 all copies or substantial portions of the Software.
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
25 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 Except as contained in this notice, the name of the X Consortium shall not be
29 used in advertising or otherwise to promote the sale, use or other dealings
30 in this Software without prior written authorization from the X Consortium.
35 * Author: Keith Packard, MIT X Consortium
41 * Read fonts.dir and fonts.alias files
46 #include <sys/types.h>
53 static int ReadFontAlias();
56 FontFileReadDirectory (directory, pdir)
58 FontDirectoryPtr *pdir;
60 char file_name[MAXFONTNAMELEN];
61 char font_name[MAXFONTNAMELEN];
62 char dir_file[MAXFONTNAMELEN];
64 char dir_path[MAXFONTNAMELEN];
73 FontDirectoryPtr dir = NullFontDirectory;
76 /* Check for font directory attributes */
78 if (ptr = strchr(directory, ':')) {
80 /* OS/2 path might start with a drive letter, don't clip this */
81 if (ptr = strchr(directory+2, ':')) {
83 strncpy(dir_path, directory, ptr - directory);
84 dir_path[ptr - directory] = '\0';
86 strcpy(dir_path, directory);
88 strcpy(dir_file, dir_path);
90 strcpy(dir_file, directory);
92 if (dir_file[strlen(dir_file) - 1] != '/')
93 strcat(dir_file, "/");
94 strcat(dir_file, FontDirFile);
95 file = fopen(dir_file, "r");
97 if (fstat (fileno(file), &statb) == -1)
99 count = fscanf(file, "%d\n", &i);
100 if ((count == EOF) || (count != 1)) {
104 dir = FontFileMakeDir(directory, i);
109 dir->dir_mtime = statb.st_mtime;
110 while ((count = fscanf(file, "%s %[^\n]\n", file_name, font_name)) != EOF) {
112 /* strip any existing trailing CR */
113 for (i=0; i<strlen(font_name); i++) {
114 if (font_name[i]=='\r') font_name[i] = '\0';
118 FontFileFreeDir (dir);
122 if (!FontFileAddFontFile (dir, font_name, file_name))
124 FontFileFreeDir (dir);
130 } else if (errno != ENOENT) {
134 status = ReadFontAlias(dir_path, FALSE, &dir);
136 status = ReadFontAlias(directory, FALSE, &dir);
138 if (status != Successful) {
140 FontFileFreeDir (dir);
144 /*** TJR - dirty hack - this variable is set in
145 programs/Xserver/dix/dixfont.c when setting default font path */
146 extern int settingDefaultFontPath;
147 if (settingDefaultFontPath) {
148 fprintf(stderr,"Font directory '%s' not found - ignoring\n",
150 dir = FontFileMakeDir(directory, 0);
157 FontFileSortDir(dir);
164 FontFileDirectoryChanged(dir)
165 FontDirectoryPtr dir;
167 char dir_file[MAXFONTNAMELEN];
170 strcpy (dir_file, dir->directory);
171 strcat (dir_file, FontDirFile);
172 if (stat (dir_file, &statb) == -1)
174 if (errno != ENOENT || dir->dir_mtime != 0)
176 return FALSE; /* doesn't exist and never did: no change */
178 if (dir->dir_mtime != statb.st_mtime)
180 strcpy (dir_file, dir->directory);
181 strcat (dir_file, FontAliasFile);
182 if (stat (dir_file, &statb) == -1)
184 if (errno != ENOENT || dir->alias_mtime != 0)
186 return FALSE; /* doesn't exist and never did: no change */
188 if (dir->alias_mtime != statb.st_mtime)
194 * Make each of the file names an automatic alias for each of the files.
198 AddFileNameAliases(dir)
199 FontDirectoryPtr dir;
202 char copy[MAXFONTNAMELEN];
205 FontRendererPtr renderer;
209 table = &dir->nonScalable;
210 for (i = 0; i < table->used; i++) {
211 if (table->entries[i].type != FONT_ENTRY_BITMAP)
213 fileName = table->entries[i].u.bitmap.fileName;
214 renderer = FontFileMatchRenderer (fileName);
218 len = strlen (fileName) - renderer->fileSuffixLen;
219 CopyISOLatin1Lowered (copy, fileName, len);
223 name.ndashes = FontFileCountDashes (copy, len);
225 if (!FontFileFindNameInDir(table, &name)) {
226 if (!FontFileAddFontAlias (dir, copy, table->entries[i].name.name))
234 * parse the font.alias file. Format is:
238 * To imbed white-space in an alias name, enclose it like "font name"
239 * in double quotes. \ escapes and character, so
240 * "font name \"With Double Quotes\" \\ and \\ back-slashes"
243 * A line beginning with a ! denotes a newline-terminated comment.
250 static int lexAlias(), lexc();
258 ReadFontAlias(directory, isFile, pdir)
261 FontDirectoryPtr *pdir;
263 char alias[MAXFONTNAMELEN];
264 char font_name[MAXFONTNAMELEN];
265 char alias_file[MAXFONTNAMELEN];
267 FontDirectoryPtr dir;
270 int status = Successful;
274 strcpy(alias_file, directory);
276 if (directory[strlen(directory) - 1] != '/')
277 strcat(alias_file, "/");
278 strcat(alias_file, FontAliasFile);
280 file = fopen(alias_file, "r");
282 return ((errno == ENOENT) ? Successful : BadFontPath);
284 *pdir = dir = FontFileMakeDir(directory, 10);
290 if (fstat (fileno (file), &statb) == -1)
295 dir->alias_mtime = statb.st_mtime;
296 while (status == Successful) {
297 token = lexAlias(file, &lexToken);
308 strcpy(alias, lexToken);
309 token = lexAlias(file, &lexToken);
312 if (strcmp(alias, "FILE_NAMES_ALIASES"))
313 status = BadFontPath;
314 else if (!AddFileNameAliases(dir))
318 status = BadFontPath;
324 CopyISOLatin1Lowered((unsigned char *) alias,
325 (unsigned char *) alias,
327 CopyISOLatin1Lowered((unsigned char *) font_name,
328 (unsigned char *) lexToken,
330 if (!FontFileAddFontAlias (dir, alias, font_name))
347 static int charClass;
350 lexAlias(file, lexToken)
357 Begin, Normal, Quoted, Comment
361 static char *tokenBuf = (char *) NULL;
362 static int tokenSize = 0;
368 if (count == tokenSize) {
372 nsize = tokenSize ? (tokenSize << 1) : 64;
373 nbuf = (char *) xrealloc(tokenBuf, nsize);
378 t = tokenBuf + count;
402 *lexToken = tokenBuf;
424 *lexToken = (char *) NULL;
425 return charClass == END ? DONE : NEWLINE;
428 *lexToken = tokenBuf;