📄 t1env.c
字号:
/*-------------------------------------------------------------------------- ----- File: t1env.c ----- Author: Rainer Menzner (Rainer.Menzner@web.de) ----- Date: 2001-10-18 ----- Description: This file is part of the t1-library. It implements the reading of a configuration file and path-searching of type1-, afm- and encoding files. ----- Copyright: t1lib is copyrighted (c) Rainer Menzner, 1996-2001. As of version 0.5, t1lib is distributed under the GNU General Public Library Lincense. The conditions can be found in the files LICENSE and LGPL, which should reside in the toplevel directory of the distribution. Please note that there are parts of t1lib that are subject to other licenses: The parseAFM-package is copyrighted by Adobe Systems Inc. The type1 rasterizer is copyrighted by IBM and the X11-consortium. ----- Warranties: Of course, there's NO WARRANTY OF ANY KIND :-) ----- Credits: I want to thank IBM and the X11-consortium for making their rasterizer freely available. Also thanks to Piet Tutelaers for his ps2pk, from which I took the rasterizer sources in a format independ from X11. Thanks to all people who make free software living!--------------------------------------------------------------------------*/ #define T1ENV_C#include <stdio.h>#include <stdlib.h>#include <string.h>#if defined(_MSC_VER)# include <io.h># include <sys/types.h># include <sys/stat.h>#else# include <unistd.h>#endif#include <sys/types.h>#include <sys/stat.h>#include <ctype.h>#include "../type1/types.h"#include "parseAFM.h" #include "../type1/objects.h" #include "../type1/spaces.h" #include "../type1/util.h" #include "../type1/fontfcn.h"#include "../type1/fontmisc.h"#include "sysconf.h"#include "t1types.h"#include "t1extern.h"#include "t1env.h"#include "t1misc.h"#include "t1base.h"/* The following static variables are used to store information on the distinct file search paths: -1 t1lib has not yet been initialized! 0 t1lib has been initialized and default paths have been setup n (>0) there are n path elements for current search path type, either built from a FontDataBase file or from explicit fucntion calls.*/ static int pfab_no=-1;static int afm_no=-1;static int enc_no=-1;static int fdb_no=-1;static char path_sep_char='\0';static char path_sep_string[2];static char pathbuf[2048];/* Define some default search paths */#ifndef VMSstatic char T1_pfab[]=".";static char T1_afm[]=".";static char T1_enc[]=".";#elsestatic char T1_pfab[]="sys$disk:[]";static char T1_afm[]="sys$disk:[]";static char T1_enc[]="sys$disk:[]";#endifchar T1_fdb[]="FontDataBase";/* keywords recognized in config file */static const char enc_key[]="ENCODING";static const char pfab_key[]="TYPE1";static const char afm_key[]="AFM";static const char fdb_key[]="FONTDATABASE";/* qstrncpy(): Copy bytes from srcP to to destP. srcP is count bytes long and destP is the number of quoted characters shorter. That is, count refers to the number of characters including the escapement chars in srcP! */static void qstrncpy( char *destP, const char *srcP, long nochars){ long i; long j; i=0; /* dest-index */ j=0; /* src-index */ while (j<nochars) { if (srcP[j]=='\\') { if (srcP[j+1]=='"') { j++; /* escaped quotation character --> omit escape char. */ } } else { /* normal character */ destP[i++]=srcP[j++]; } }}/* Setup the default paths for searching the distinct file types. If paths have been setup explicitly, skip the step of setting up a default path. */void intT1_SetupDefaultSearchPaths( void) { path_sep_char=PATH_SEP_CHAR; sprintf( path_sep_string, "%c", path_sep_char); /* We set the number of stored path elements 0 so that we can distiguish between explicitly setup paths and default paths in intT1_ScanConfigFile(). */ if (pfab_no==-1) { T1_PFAB_ptr=(char**) calloc( 2, sizeof(char*)); T1_PFAB_ptr[0]=(char*)malloc(strlen(T1_pfab)+1); strcpy(T1_PFAB_ptr[0],T1_pfab); pfab_no=0; } if (afm_no==-1) { T1_AFM_ptr=(char**) calloc( 2, sizeof(char*)); T1_AFM_ptr[0]=(char*)malloc(strlen(T1_afm)+1); strcpy(T1_AFM_ptr[0],T1_afm); afm_no=0; } if (enc_no==-1) { T1_ENC_ptr=(char**) calloc( 2, sizeof(char*)); T1_ENC_ptr[0]=(char*)malloc(strlen(T1_enc)+1); strcpy(T1_ENC_ptr[0],T1_enc); enc_no=0; } if (fdb_no==-1) { T1_FDB_ptr=(char**) calloc( 2, sizeof(char*)); T1_FDB_ptr[0]=(char*)malloc(strlen(T1_fdb)+1); strcpy(T1_FDB_ptr[0],T1_fdb); fdb_no=0; }}/* This function is called from T1_CloseLib(). We have to indicate the state of a non-initialzed t1lib! */void intT1_FreeSearchPaths( void) { int i; i=0; if (T1_PFAB_ptr!=NULL) { while (T1_PFAB_ptr[i]!=NULL) { free(T1_PFAB_ptr[i]); T1_PFAB_ptr[i]=NULL; } free( T1_PFAB_ptr); } i=0; if (T1_AFM_ptr!=NULL) { while (T1_AFM_ptr[i]!=NULL) { free(T1_AFM_ptr[i]); T1_AFM_ptr[i]=NULL; } free( T1_AFM_ptr); } i=0; if (T1_ENC_ptr!=NULL) { while (T1_ENC_ptr[i]!=NULL) { free(T1_ENC_ptr[i]); T1_ENC_ptr[i]=NULL; } free( T1_ENC_ptr); } i=0; if (T1_FDB_ptr!=NULL) { while (T1_FDB_ptr[i]!=NULL) { free(T1_FDB_ptr[i]); T1_FDB_ptr[i]=NULL; } free( T1_FDB_ptr); } /* indicate t1lib non-initialized */ pfab_no=-1; afm_no=-1; enc_no=-1; fdb_no=-1; return;}/* ScanConfigFile(): Read a configuration file and scan and save the environment strings used for searching pfa/pfb-, afm- and encoding files as well as the name of the font database file. */int intT1_ScanConfigFile( void){ char *env_str; char *linebuf; char *usershome; char *cnffilepath; char *globalcnffilepath; static int linecnt; char local_path_sep_char; int quoted=0; int quotecnt=0; FILE *cfg_fp; int filesize, i, j, k; int ignoreline=0; char*** destP=NULL; int *idestP=NULL; char* curr_key=NULL; /* First, get the string stored in the environment variable: */ env_str=getenv(ENV_CONF_STRING); linecnt=1; if (env_str==NULL) { /* environment variable not set, try to open default file in user's home directory and afterwards global config file */ if ((usershome=getenv("HOME"))!=NULL) { cnffilepath=(char *)malloc((strlen(usershome) + strlen(T1_CONFIGFILENAME) + 2 ) * sizeof(char)); if (cnffilepath==NULL){ T1_errno=T1ERR_ALLOC_MEM; return(-1); } strcpy( cnffilepath, usershome); } else { cnffilepath=(char *)malloc((strlen(T1_CONFIGFILENAME) + 2 ) * sizeof(char)); } strcat( cnffilepath, DIRECTORY_SEP); strcat( cnffilepath, T1_CONFIGFILENAME); globalcnffilepath=(char*)malloc((strlen(GLOBAL_CONFIG_DIR) + strlen(GLOBAL_CONFIG_FILE) + 2 ) * sizeof(char)); if (globalcnffilepath==NULL){ T1_errno=T1ERR_ALLOC_MEM; return(-1); } strcpy( globalcnffilepath, GLOBAL_CONFIG_DIR); strcat( globalcnffilepath, DIRECTORY_SEP); strcat( globalcnffilepath, GLOBAL_CONFIG_FILE); if ((cfg_fp=fopen( cnffilepath, "r"))==NULL){ sprintf( err_warn_msg_buf, "Could not open configfile %s", cnffilepath); T1_PrintLog( "ScanConfigFile()", err_warn_msg_buf, T1LOG_STATISTIC); /* Try global config file */ if ((cfg_fp=fopen( globalcnffilepath, "r"))==NULL){ sprintf( err_warn_msg_buf, "Could not open global configfile %s", globalcnffilepath); T1_PrintLog( "ScanConfigFile()", err_warn_msg_buf, T1LOG_WARNING); } else{ sprintf( err_warn_msg_buf, "Using %s as Configfile (global)", cnffilepath); T1_PrintLog( "ScanConfigFile()", err_warn_msg_buf, T1LOG_STATISTIC); } } else{ sprintf( err_warn_msg_buf, "Using %s as Configfile (user's)", cnffilepath); T1_PrintLog( "ScanConfigFile()", err_warn_msg_buf, T1LOG_STATISTIC); } free( cnffilepath); free( globalcnffilepath); if (cfg_fp==NULL){ T1_PrintLog( "ScanConfigFile()", "Neither user's nor global Configfile has been found", T1LOG_WARNING); return(0); } } else { /* open specified file for reading the configuration */ if ((cfg_fp=fopen(env_str,"r"))==NULL){ T1_PrintLog( "ScanConfigFile()", "Configfile as specified by Environment has not been found", T1LOG_WARNING); return(0); /* specified file could not be openend => no config paths read */ } else { sprintf( err_warn_msg_buf, "Using %s as Configfile (environment)", env_str); T1_PrintLog( "ScanConfigFile()", err_warn_msg_buf, T1LOG_STATISTIC); } } /* cfg_fp points now to a valid config file */ /* Get the file size */ fseek( cfg_fp, 0, SEEK_END); filesize=ftell(cfg_fp); /* Reset fileposition to start */ fseek( cfg_fp, 0, SEEK_SET); if ((linebuf=(char *)calloc( filesize+1, sizeof(char)))==NULL){ T1_errno=T1ERR_ALLOC_MEM; return(-1); } fread((char *)linebuf, sizeof(char), filesize, cfg_fp); fclose(cfg_fp); i=0; /* this might be overwritten on a per file basis */ local_path_sep_char=path_sep_char; while(i<filesize) { ignoreline=0; j=i; /* Save index of beginning of line */ while ((linebuf[i]!='=') && (linebuf[i]!='\n') && (i<filesize)) { i++; } if (i==filesize) { free( linebuf); return(i); } if (strncmp( enc_key, &linebuf[j], 8)==0) { /* setup target */ destP=&T1_ENC_ptr; idestP=&enc_no; curr_key=(char*)enc_key; } else if (strncmp( "TYPE1", &linebuf[j], 5)==0) { /* setup target */ destP=&T1_PFAB_ptr; idestP=&pfab_no; curr_key=(char*)pfab_key; } else if (strncmp( afm_key, &linebuf[j], 3)==0) { /* setup target */ destP=&T1_AFM_ptr; idestP=&afm_no; curr_key=(char*)afm_key; } else if (strncmp( fdb_key, &linebuf[j], 12)==0) { /* setup target */ destP=&T1_FDB_ptr; idestP=&fdb_no; curr_key=(char*)fdb_key; } else { ignoreline=1; T1_PrintLog( "ScanConfigFile()", "Ignoring line %d", T1LOG_DEBUG, linecnt); } /* If appropriate, scan this line. */ if (ignoreline==0) { /* Check for an explicitly assigned value */ if (*idestP==0) { /* default paths are currently setup, get rid of them */ free((*destP)[0]); } else { /* append to existing paths */ T1_PrintLog( "ScanConfigFile()", "Appending to existing %s search path", T1LOG_DEBUG, curr_key); } while ( (!isspace((int)linebuf[i])) && (i<filesize) ) { k=++i; /* index to current path element */ (*idestP)++; quotecnt=0; if (linebuf[i]=='"') { /* We have a quoted string */ quoted=1; k=++i; while ( 1) { if ( linebuf[i]=='"' ) { /* we find a quote-char */ if ( linebuf[i-1]!='\\' ) break; /* not escaped --> end of path specification */ else quotecnt++; } /* some other char */ if (linebuf[i]=='\n') { /* a newline in a quoted string? Perhabs, quotes do not match! */ T1_PrintLog( "ScanConfigFile()", "Newline in quoted %s-string in line %d, column %d, of config file! Closing quote missing?", T1LOG_WARNING, curr_key, linecnt, i-j+1); j=i+1; /* resynchronize linecount */ linecnt++; } if (i<filesize) { /* filesize not exceeded? */ i++; } else { /* issue error msg because end of quotation is missing */ T1_PrintLog( "ScanConfigFile()", "Unterminated quoted string in config file", T1LOG_ERROR); return -1; } } } else { quoted=0; while ( (linebuf[i]!=local_path_sep_char) && (!isspace((int)linebuf[i])) && (i<filesize) ) i++; } if (((*destP)=(char**)realloc( (*destP), ((*idestP)+1)*sizeof(char*)))==NULL) { T1_errno=T1ERR_ALLOC_MEM; return(-1); } if (((*destP)[(*idestP)-1]=(char*)malloc((i-k-quotecnt+1)*sizeof(char)))==NULL) { T1_errno=T1ERR_ALLOC_MEM; return(-1); } if (quoted==0) { strncpy( (*destP)[*idestP-1], &(linebuf[k]), i-k); (*destP)[(*idestP)-1][i-k]='\0'; } else { qstrncpy( (*destP)[(*idestP)-1], &(linebuf[k]), i-k); (*destP)[(*idestP)-1][i-k-quotecnt]='\0'; i++; /* step over closing quote */ } (*destP)[(*idestP)]=NULL; /* indicate end of string list */ } } /* skip remaining of line or file */ while ((linebuf[i]!='\n')&&(i<filesize)) i++; i++; linecnt++; } /* file should now be read in */ free( linebuf); return(i); }/* intT1_Env_GetCompletePath( ): Get a full path name from the file specified by argument 1 in the environment specified by argument 2. Return the pointer to the path string or NULL if no file was found.*/char *intT1_Env_GetCompletePath( char *FileName, char **env_ptr ){ struct stat filestats; /* A structure where fileinfo is stored */ int fnamelen, i, j; char *FullPathName; char *StrippedName; if (FileName==NULL) return(NULL); fnamelen=strlen(FileName); /* We check whether absolute or relative pathname is given. If so, stat() it and if appropriate, return that string immediately. */ if ( (FileName[0]==DIRECTORY_SEP_CHAR) || ((fnamelen>1) && (FileName[0]=='.') && (FileName[1]==DIRECTORY_SEP_CHAR)) || ((fnamelen>2) && (FileName[0]=='.') && (FileName[1]=='.') && (FileName[2]==DIRECTORY_SEP_CHAR))#if defined(MSDOS) | defined(_WIN32) | defined (__EMX__) || ((isalpha(FileName[0])) && (FileName[1]==':'))#endif#ifdef VMS || (strchr(FileName,':') != NULL)#endif ) { /* Check for existence of the path: */ if (!stat( FileName, &filestats)) { if (t1lib_log_file!=NULL) { sprintf( err_warn_msg_buf, "stat()'ing complete path %s successful", FileName); T1_PrintLog( "intT1_Env_GetCompletePath()", err_warn_msg_buf, T1LOG_DEBUG); } /* Return a copy of the string */ if ((FullPathName=(char *)malloc( fnamelen + 1))==NULL) { T1_errno=T1ERR_ALLOC_MEM; return(NULL); } strcpy( FullPathName, FileName); return(FullPathName); } if (t1lib_log_file!=NULL){ sprintf( err_warn_msg_buf, "stat()'ing complete path %s failed",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -