Main Page   Modules   Alphabetical List   Compound List   File List   Compound Members   File Members  

config.c

Go to the documentation of this file.
00001 /*  $Id: font.c,v 1.1 2000/12/02 07:26:03 dbryson Exp $
00002 **
00003 **  Copyright (c) 2000 Derry Bryson <derry@techass.com>
00004 **
00005 **  Description:
00006 **  
00007 **    Postscript Library
00008 **
00009 **
00010 **  License:
00011 **
00012 **    This library is free software; you can redistribute it and/or
00013 **    modify it under the terms of the GNU Lesser General Public
00014 **    License as published by the Free Software Foundation; either
00015 **    version 2.1 of the License, or (at your option) any later version.
00016 **
00017 **    This library is distributed in the hope that it will be useful,
00018 **    but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 **    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020 **    Lesser General Public License for more details.
00021 **
00022 **    You should have received a copy of the GNU Lesser General Public
00023 **    License along with this library; if not, write to the Free Software
00024 **    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 **
00026 **
00027 **  Contact:
00028 **
00029 **      Mail:
00030 **
00031 **        Technology Associates, Inc.
00032 **        LIBPS Project
00033 **        1455 Deming Way #11
00034 **        Sparks, NV  89431
00035 **        USA
00036 **
00037 **      Email:
00038 **
00039 **        libps@techass.com
00040 **
00041 **      See our website at:
00042 **
00043 **        libps.sourceforge.net
00044 **
00045 */
00046 
00056 #ifdef HAVE_CONFIG_H
00057 # include "psconfig.h"
00058 #endif
00059 
00060 #include <stdio.h>
00061 #include <stdlib.h>
00062 #include <string.h>
00063 #include <unistd.h>
00064 #include <sys/stat.h>
00065 #include <ctype.h>
00066 #include "ps/ps.h"
00067 
00068 static char*
00069 MakeSysConfigFileName(char *path)
00070 {
00071   char
00072     *fname = NULL;
00073     
00074   if(!path)
00075     path = PS_DEFAULT_SYSCONFIG_DIR;
00076     
00077   fname = malloc(strlen(path) + strlen(PS_SYSCONFIG_FILENAME) + 2);
00078   if(fname)
00079   {
00080     strcpy(fname, path);
00081     if(fname[strlen(fname) - 1] != '/')
00082       strcat(fname, "/");
00083     strcat(fname, PS_SYSCONFIG_FILENAME);
00084   }
00085   return fname;
00086 }
00087 
00088 static char*
00089 MakeUserConfigFileName(void)
00090 {
00091   char
00092     *fname = NULL,
00093     *path;
00094     
00095   path = getenv("HOME");
00096   if(!path)
00097     return strdup(PS_USERCONFIG_FILENAME);
00098   
00099   fname = malloc(strlen(path) + strlen(PS_USERCONFIG_FILENAME) + 2);
00100   if(fname)
00101   {
00102     strcpy(fname, path);
00103     if(fname[strlen(fname) - 1] != '/')
00104       strcat(fname, "/");
00105     strcat(fname, PS_USERCONFIG_FILENAME);
00106   }
00107   return fname;
00108 }
00109 
00136 int
00137 psConfigGetValue(char *fileName, char *section, char *name,
00138                  char *value, char *deflt)
00139 {
00140   FILE
00141     *fp;
00142 
00143   int
00144     done = 0,
00145     retVal = 2;
00146 
00147   char
00148     *p,
00149     tStr[PS_CONFIG_MAX_LINE_LEN];
00150 
00151   *value = 0;
00152 
00153   if((fp = fopen(fileName, "r")) == NULL)
00154     goto CreateFile;
00155 
00156 
00157   while(!done && fgets(tStr, PS_CONFIG_MAX_LINE_LEN, fp))
00158   {
00159     psEraseWhiteBegin(tStr);
00160     psEraseWhiteEnd(tStr);                       /* remove whitespace */
00161 
00162     if(tStr[strlen(tStr) - 1] == '\n')           /* remove linefeed */
00163       tStr[strlen(tStr) - 1] = 0;
00164 
00165     if(!tStr[0] || tStr[0] == ';')               /* comment or blank line */
00166       continue;
00167 
00168     switch(retVal)
00169     {
00170       case 2 : /* looking for section */
00171         if(tStr[0] == '[')
00172         {
00173           if(*strchr(tStr, ']'))
00174           {
00175             *strchr(tStr, ']') = 0;
00176             if(!strcasecmp(section, &tStr[1]))
00177               retVal = 3;
00178           }
00179         }
00180       break;
00181 
00182       case 3 : /* looking for name in section */
00183         if(tStr[0] == '[')
00184         {
00185           retVal = 2;
00186           done = 1;
00187         }
00188         else
00189         {
00190           if((p = strchr(tStr, '=')) != NULL)
00191           {
00192             *p = 0;
00193             p++;
00194             psEraseWhiteEnd(tStr);
00195             if(!strcasecmp(tStr, name))
00196             {
00197               strcpy(value, p);
00198               psEraseWhiteBegin(value);
00199               retVal = 0;
00200               done = 1;
00201             }
00202           }
00203         }
00204       break;
00205     }
00206   }
00207 
00208   fclose(fp);
00209 
00210 CreateFile:
00211   if(!done && deflt)
00212   {
00213     psConfigSetValue(fileName, section, name, deflt);
00214     strcpy(value, deflt);
00215     retVal = 0;
00216   }
00217 
00218   return(retVal);
00219 }
00220 
00245 int
00246 psConfigSetValue(char *fileName, char *section, char *name, char *value)
00247 {
00248   FILE
00249     *fpIn,
00250     *fpOut;
00251 
00252   char
00253     *p,
00254     *tValue,
00255     tName[100],
00256     cName[PS_CONFIG_MAX_LINE_LEN],
00257     tStr[PS_CONFIG_MAX_LINE_LEN];
00258 
00259   int
00260     state,
00261     found;
00262 
00263   tValue = calloc(1, strlen(value) + 1);
00264   strcpy(tValue, value);
00265   
00266   psEraseWhiteBegin(value);
00267   psEraseWhiteEnd(value);
00268 
00269   fpIn = fopen(fileName, "r");
00270 
00271   sprintf(tName, "/tmp/%d.cfg", getpid());
00272   if((fpOut = fopen(tName, "w")) == NULL)
00273   {
00274     fclose(fpIn);
00275     free(tValue);
00276     return(1);
00277   }
00278 
00279   found = 0;
00280   if(fpIn)
00281   {
00282     state = 0;
00283     while(fgets(tStr, PS_CONFIG_MAX_LINE_LEN, fpIn))
00284     {
00285       psEraseWhiteBegin(tStr);
00286       psEraseWhiteEnd(tStr);
00287 
00288       if(tStr[strlen(tStr) - 1] == '\n')
00289         tStr[strlen(tStr) - 1] = 0;
00290 
00291       if(!tStr[0])
00292         continue;
00293 
00294       if(tStr[0] != ';')
00295       {
00296         switch(state)
00297         {
00298           case 0 : /* looking for section */
00299             if(tStr[0] == '[')
00300             {
00301               if((p = strchr(tStr, ']')) != NULL)
00302               {
00303                 *p = 0;
00304                 if(!strcasecmp(section, &tStr[1]))
00305                   state = 1;                              /* found it */
00306                 *p = ']';
00307               }
00308             }
00309           break;
00310 
00311           case 1 : /* looking for name */
00312             if(tStr[0] == '[')
00313             {
00314               fprintf(fpOut, "%s=%s\n", name, tValue);
00315               found = 1;                                  /* white lie */
00316               state = 2;
00317             }
00318             else
00319             {
00320               p = strchr(tStr, '=');
00321               if(p)
00322               {
00323                 *p = 0;
00324                 strcpy(cName, tStr);
00325                 psEraseWhiteEnd(cName);
00326                 if(!strcasecmp(cName, name))
00327                 {
00328                   sprintf(tStr, "%s=%s", name, tValue);
00329                   found = 1;
00330                   state = 2;
00331                 }
00332                 else
00333                   *p = '=';
00334               }
00335             }
00336           break;
00337 
00338           case 2 : /* finish copying file */
00339           break;
00340         }
00341       }
00342       if(tStr[0] == '[')
00343         fputs("\n", fpOut);
00344       fprintf(fpOut, "%s\n", tStr);
00345     }
00346   }
00347 
00348   if(!fpIn || !found)
00349     if(state == 1)
00350       fprintf(fpOut, "%s=%s\n", name, tValue);
00351     else
00352       fprintf(fpOut, "[%s]\n%s=%s\n", section, name, tValue);
00353 
00354   fclose(fpIn);
00355   fclose(fpOut);
00356   unlink(fileName);
00357   rename(tName, fileName);
00358   free(tValue);
00359   return(0);
00360 }
00361 
00362 static int
00363 ReadConfigValue(char *userFileName, char *sysFileName, char *section, 
00364                 char *name, char *value, char *deflt)
00365 {
00366   int
00367     retVal;
00368     
00369   retVal = psConfigGetValue(userFileName, section, name, value, deflt);
00370   if(retVal)
00371     retVal = psConfigGetValue(sysFileName, section, name, value, deflt);
00372     
00373   return retVal;
00374 }
00375            
00390 int
00391 psConfigRead(char *sysConfDir)
00392 {
00393   char
00394     *sysConfName,
00395     *userConfName,
00396     value[PS_CONFIG_MAX_LINE_LEN];
00397     
00398   sysConfName = MakeSysConfigFileName(sysConfDir);
00399   if(!sysConfName)
00400   {
00401     pserrno = PSERR_ERRNO;
00402     return 0;
00403   }
00404     
00405   userConfName = MakeUserConfigFileName();
00406   if(!userConfName)
00407   {
00408     pserrno = PSERR_ERRNO;
00409     free(sysConfName);
00410     return 0;
00411   }
00412   
00413   if(!ReadConfigValue(userConfName, sysConfName, PS_CONFIG_FONT_SECTION,
00414                       PS_CONFIG_FONT_PATH, value, NULL))
00415     psFontListSetPath(value);
00416     
00417   return 1;
00418 }

Generated at Mon Dec 11 22:46:27 2000 for Postscript Utility Library by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000