--- chkfontpath.c.orig 2003-05-30 04:02:39.000000000 +0900 +++ chkfontpath.c 2003-05-30 14:33:34.000000000 +0900 @@ -36,10 +36,14 @@ #define XFS_CONFIGBACKUP "/etc/X11/fs/config-" #define XFS_PIDFILE "/var/run/xfs.pid" #define XFS_SUBSYSLOCK "/var/lock/subsys/xfs" +#define XF86_CONFIGFILE "/etc/X11/XF86Config" +#define XF86_CONFIGBACKUP "/etc/X11/XF86Config-" static char *progName; static const char **fpList; static int fpCount; +static const char **fpListXF86; +static int fpCountXF86; static int quiet; static int firstdir; @@ -55,6 +59,19 @@ exit((quiet) ? EXIT_SUCCESS : EXIT_FAILURE); } + +void fatalwarn(const char *format, ...) +{ + va_list args; + + if(!quiet) { + va_start(args, format); + vfprintf(stderr, format, args); + va_end(args); + } +} + + void readFontPath(void) { FILE *f; @@ -137,6 +154,55 @@ fclose(f); } +void readFontPathXF86() +{ + FILE *f; + char buf[250]; + char *s, *p, *q; + + if (NULL == (f = fopen(XF86_CONFIGFILE, "r"))) + fatalerror("%s: error opening %s\n", progName, XF86_CONFIGFILE); + + while ((q = s = fgets(buf, sizeof(buf), f)) != NULL) { + p = s; + + /* strip leading white space */ + while (isblank(*s)) s++; + /* strip trailing white space */ + p = s + strlen(s)-2; + while (isspace(*p)) { + *p = '\0'; + p--; + } + *(p+1) = '\n'; + *(p+2) = '\0'; + + /* skip all comment lines */ + if (*s == '#') + continue; + + /* find the FontPath line */ + if (strstr(s, "FontPath")) { + if (NULL == (p = strchr(s, '\"'))) + fatalerror("%s: error locating '\"' after FontPath token\n", progName); + s = ++p; + } else + continue; + + fpCountXF86++; + p = strdup(s); + if (p[strlen(p)-1] == '\n') + p[strlen(p)-1] = '\0'; + if (p[strlen(p)-1] == '\"') + p[strlen(p)-1] = '\0'; + + fpListXF86 = (const char **) realloc(fpListXF86, sizeof(char *) * fpCountXF86); + fpListXF86[fpCountXF86 - 1] = p; + } + + fclose(f); +} + void writeNewConfig(void) { FILE *f, *f1; @@ -198,6 +264,48 @@ chmod(XFS_CONFIGFILE, sb.st_mode); } +void writeNewConfigXF86() +{ + FILE *f, *f1; + char buf[250]; + char *s; + int catFlag = 0, i; + struct stat sb; + + stat(XF86_CONFIGFILE, &sb); + + if (NULL == (f = fopen(XF86_CONFIGFILE, "r"))) + fatalerror("%s: error opening %s for reading\n", progName, XF86_CONFIGFILE); + + if (NULL == (f1 = fopen(XF86_CONFIGBACKUP, "w"))) + fatalerror("%s: error opening %s for writing\n", progName, XF86_CONFIGBACKUP); + + while ((s = fgets(buf, sizeof(buf), f)) != NULL) { + /* skip all comment lines */ + if (*s == '#') { + fputs(s, f1); + continue; + } + + if (strstr(s, "FontPath") && !catFlag) { + catFlag = 1; + for (i = 0; i < fpCountXF86; i++) { + fprintf(f1, "\tFontPath \"%s\"\n",fpListXF86[i]); + } + } else { + if (!strstr(s, "FontPath")) { + fputs(s, f1); + } + } + } + + fclose(f); + fclose(f1); + unlink(XF86_CONFIGFILE); + rename(XF86_CONFIGBACKUP, XF86_CONFIGFILE); + chmod(XF86_CONFIGFILE, sb.st_mode); +} + void addDir(const char *newDir) { int i, last = -1; @@ -252,7 +360,7 @@ if (strncmp(fpList[i], fixDir, fixDirLen) == 0 && (fpList[i][fixDirLen] == '\0' || strcmp(fpList[i] + fixDirLen, ":unscaled") == 0)) { - fatalerror("%s: %s already in list\n",progName, fixDir); + fatalwarn("%s: %s already in list for xfs\n",progName, fixDir); } last = i; if (!strcmp (fpList[i] + prefixLen, "misc:unscaled")) type = 0; @@ -294,6 +402,103 @@ fpList[second] = fixDir; } +void addDirXF86(const char *newDir) +{ + int i, last = -1; + int firstType, secondType; + int first = -1, second = -1; + int type, prefixLen, fixDirLen; + FILE *f; + char *fontsdir; + char *fixDir = strdup(newDir); + + fixDirLen = strlen(fixDir); + if (fixDir[fixDirLen - 1] == '/') { + fixDir[fixDirLen - 1] = '\0'; + fixDirLen--; + } + if (fixDirLen > 10 && !strcmp(fixDir + fixDirLen - 9, ":unscaled")) { + fixDir[fixDirLen - 9] = '\0'; + fixDirLen -= 9; + } + + if (fixDir[0] != '/') + fatalerror("%s: font directories must be absolute, not adding %s\n", + progName, fixDir); + + fontsdir = (char *) malloc(sizeof(char *) * (strlen(fixDir) + 12)); + sprintf(fontsdir, "%s/fonts.dir", fixDir); + if (NULL == (f = fopen(fontsdir, "r"))) + fatalerror("%s: error opening %s, unwilling to add path\n", + progName, fontsdir); + + fclose(f); + free(fontsdir); + + prefixLen = strrchr(fixDir, '/') + 1 - fixDir; + + if (!strcmp (fixDir + prefixLen, "misc")) { + firstType = 0; + secondType = 3; + } else if (!strcmp (fixDir + prefixLen, "75dpi")) { + firstType = 1; + secondType = 5; + } else if (!strcmp (fixDir + prefixLen, "100dpi")) { + firstType = 2; + secondType = 6; + } else { + firstType = 100; + secondType = 4; + } + + for (i = 0; i < fpCountXF86; i++) { + if (strncmp(fpListXF86[i], fixDir, prefixLen) != 0) continue; + if (strncmp(fpListXF86[i], fixDir, fixDirLen) == 0 && + (fpListXF86[i][fixDirLen] == '\0' || + strcmp(fpListXF86[i] + fixDirLen, ":unscaled") == 0)) { + fatalwarn("%s: %s already in list for XF86Config\n",progName, fixDir); + } + last = i; + if (!strcmp (fpListXF86[i] + prefixLen, "misc:unscaled")) type = 0; + else if (!strcmp (fpListXF86[i] + prefixLen, "75dpi:unscaled")) type = 1; + else if (!strcmp (fpListXF86[i] + prefixLen, "100dpi:unscaled")) type = 2; + else if (!strcmp (fpListXF86[i] + prefixLen, "misc")) type = 3; + else if (!strcmp (fpListXF86[i] + prefixLen, "75dpi")) type = 5; + else if (!strcmp (fpListXF86[i] + prefixLen, "100dpi")) type = 6; + else type = 4; + if (first == -1 && firstType < type) + first = i; + if (second == -1 && secondType < type) + second = i; + } + if (last == -1) + last = fpCountXF86; + else + last++; + if (first == -1 && firstType < 10) + first = last; + if (second == -1) + second = last; + if (firstdir) { + if (firstType < 10) + first = 0; + second = 0; + } + fpCountXF86 += (firstType < 10) ? 2 : 1; + fpListXF86 = (const char **) realloc(fpListXF86, sizeof(char *) * fpCountXF86); + if (firstType < 10) { + char *unspecDir; + memmove(fpListXF86 + first + 1, fpListXF86 + first, sizeof(char *) * (fpCountXF86 - 1 - first)); + unspecDir = (char *) malloc(fixDirLen + 11); + sprintf(unspecDir, "%s:unscaled", fixDir); + fpListXF86[first] = unspecDir; + second++; + } + memmove(fpListXF86 + second + 1, fpListXF86 + second, sizeof(char *) * (fpCountXF86 - 1 - second)); + fpListXF86[second] = fixDir; + +} + void removeDir(const char *delDir) { int found = 0; @@ -317,7 +522,35 @@ } } if (!found) - fatalerror("%s: %s not found in list\n",progName, newDir); + fatalwarn("%s: %s not found in list for xfs\n",progName, newDir); + + free(newDir); +} + +void removeDirXF86(const char *delDir) +{ + int found = 0; + int i; + char *newDir = strdup(delDir); + int newDirLen; + + if (newDir[strlen(newDir)-1] == '/') + newDir[strlen(newDir)-1] = '\0'; + + newDirLen = strlen(newDir); + for (i = 0; i < fpCountXF86; i++) { + if (strncmp(fpListXF86[i], newDir, newDirLen) == 0 && + (fpListXF86[i][newDirLen] == '\0' || + strcmp(fpListXF86[i] + newDirLen, ":unscaled") == 0)) { + found = 1; + if (i < fpCountXF86-1) + memmove(fpListXF86 + i, fpListXF86 + i + 1, sizeof(char *) * (fpCountXF86-i-1)); + fpCountXF86--; + i--; + } + } + if (!found) + fatalwarn("%s: %s not found in list for XF86Config\n",progName, newDir); free(newDir); } @@ -350,14 +583,26 @@ { int i; if (fpCount == 0) - printf("No directories currently in font path."); + printf("No directories currently in font path for xfs."); else { - printf("Current directories in font path:\n"); + printf("Current directories in font path for xfs:\n"); for (i = 0; i < fpCount; i++) printf("%d: %s\n",i+1, fpList[i]); } } +void listPathsXF86() +{ + int i; + if (fpCountXF86 == 0) + printf("No directories currently in font path for XF86Config."); + else { + printf("Current directories in font path for XF86Config:\n"); + for (i = 0; i < fpCountXF86; i++) + printf("%d: %s\n",i+1, fpListXF86[i]); + } +} + int main(int argc, const char **argv) { int rc, list = 0, help = 0; @@ -404,20 +649,26 @@ poptFreeContext(optCon); readFontPath(); + readFontPathXF86(); - if (argc == 1 || list ) + if (argc == 1 || list ) { listPaths(); + listPathsXF86(); + } if (newDir != NULL) { addDir(newDir); + addDirXF86(newDir); } if (delDir != NULL) { removeDir(delDir); + removeDirXF86(delDir); } if (newDir != NULL || delDir != NULL) { writeNewConfig(); + writeNewConfigXF86(); restartXfs(); }