(file) Return to Config.c CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Executor

  1 mike  1.1.2.1 /*
  2               //%2006////////////////////////////////////////////////////////////////////////
  3               //
  4               // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
  5               // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
  6               // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
  7               // IBM Corp.; EMC Corporation, The Open Group.
  8               // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
  9               // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
 10               // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 11               // EMC Corporation; VERITAS Software Corporation; The Open Group.
 12               // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 13               // EMC Corporation; Symantec Corporation; The Open Group.
 14               //
 15               // Permission is hereby granted, free of charge, to any person obtaining a copy
 16               // of this software and associated documentation files (the "Software"), to
 17               // deal in the Software without restriction, including without limitation the
 18               // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 19               // sell copies of the Software, and to permit persons to whom the Software is
 20               // furnished to do so, subject to the following conditions:
 21               // 
 22 mike  1.1.2.1 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 23               // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 24               // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 25               // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 26               // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 27               // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 28               // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 29               // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 30               //
 31               //%/////////////////////////////////////////////////////////////////////////////
 32               */
 33               #include <string.h>
 34               #include <ctype.h>
 35               #include <stdio.h>
 36               #include "Config.h"
 37               #include "Strlcpy.h"
 38               #include "Strlcat.h"
 39               #include "Path.h"
 40               #include "Defines.h"
 41               
 42               /*
 43 mike  1.1.2.1 **==============================================================================
 44               **
 45 mike  1.1.2.2 ** ConfigParameter
 46               **
 47               **     The _config[] array below defines default values for the
 48               **     various configuration items.
 49               **
 50               **==============================================================================
 51               */
 52               
 53               struct ConfigParameter
 54               {
 55                   const char* name;
 56                   const char* value;
 57               };
 58               
 59               static struct ConfigParameter _config[] =
 60               {
 61               #include <Pegasus/Config/FixedPropertyTable.h>
 62               };
 63               
 64               static size_t _configSize = 
 65                   sizeof(_config) / sizeof(_config[0]);
 66 mike  1.1.2.2 
 67               /*
 68               **==============================================================================
 69               **
 70 mike  1.1.2.1 ** GetConfigParamFromCommandLine()
 71               **
 72               **     Attempt to find a command line configuratin parameter of the form 
 73               **     name=value. For example: 
 74               **         cimservermain repositoryDir=/opt/pegasus/repository. 
 75               **     Return 0 if found.
 76               **
 77               **==============================================================================
 78               */
 79               
 80               int GetConfigParamFromCommandLine(
 81                   int argc,
 82                   char** argv,
 83                   const char* name,
 84                   char value[EXECUTOR_BUFFER_SIZE])
 85               {
 86                   size_t n = strlen(name);
 87                   int i;
 88               
 89                   for (i = 1; i < argc; i++)
 90                   {
 91 mike  1.1.2.1         if (strncmp(argv[i], name, n) == 0 && argv[i][n] == '=')
 92                       {
 93                           const char* p = argv[i] + n + 1;
 94                           Strlcpy(value, argv[i] + n + 1, EXECUTOR_BUFFER_SIZE);
 95                           return 0;
 96                       }
 97                   }
 98               
 99                   return -1;
100               }
101               
102               /*
103               **==============================================================================
104               **
105               ** GetConfigParamFromFile()
106               **
107               **     Attempt to find the named option in the configuration file. If found,
108               **     set value and return 0.
109               **
110               **==============================================================================
111               */
112 mike  1.1.2.1 
113               int GetConfigParamFromFile(
114                   const char* path,
115                   const char* name,
116                   char value[EXECUTOR_BUFFER_SIZE])
117               {
118                   char buffer[EXECUTOR_BUFFER_SIZE];
119                   FILE* is;
120                   size_t n;
121                   
122                   if ((is = fopen(path, "r")) == NULL)
123                       return -1;
124               
125                   n = strlen(name);
126               
127                   while (fgets(buffer, sizeof(buffer), is) != 0)
128                   {
129                       size_t r;
130               
131                       /* Skip comments. */
132               
133 mike  1.1.2.1         if (buffer[0] == '#')
134                           continue;
135               
136                       /* Remove trailing whitespace. */
137               
138                       r = strlen(buffer);
139               
140                       while (r--)
141                       {
142                           if (isspace(buffer[r]))
143                               buffer[r] = '\0';
144                       }
145               
146                       /* Skip blank lines. */
147               
148                       if (buffer[0] == '\0')
149                           continue;
150               
151                       /* Check option. */
152               
153                       if (strncmp(buffer, name, n) == 0 &&  buffer[n] == '=')
154 mike  1.1.2.1         {
155                           Strlcpy(value, buffer + n + 1, EXECUTOR_BUFFER_SIZE);
156                           fclose(is);
157                           return 0;
158                       }
159                   }
160               
161                   /* Not found! */
162                   fclose(is);
163                   return -1;
164               }
165               
166               /*
167               **==============================================================================
168               **
169               ** GetConfigParam()
170               **
171               **     Attempt to find a configuration setting for the given name. First,
172               **     search the command line and then the config file.
173               **
174               **==============================================================================
175 mike  1.1.2.1 */
176               
177               int GetConfigParam(
178                   int argc,
179                   char** argv,
180                   const char* name,
181                   char value[EXECUTOR_BUFFER_SIZE])
182               {
183                   char path[EXECUTOR_BUFFER_SIZE];
184 mike  1.1.2.2     size_t i;
185 mike  1.1.2.1 
186                   /* (1) First check command line. */
187               
188                   if (GetConfigParamFromCommandLine(argc, argv, name, value) == 0)
189                       return 0;
190               
191 mike  1.1.2.2     /* (2) Next check planned config file. */
192 mike  1.1.2.1 
193                   if (GetHomedPath(PEGASUS_PLANNED_CONFIG_FILE_PATH, path) == 0 &&
194                       GetConfigParamFromFile(path, name, value) == 0)
195                       return 0;
196               
197 mike  1.1.2.2     /* (3) Finally check the default configuration table. */
198               
199                   for (i = 0; i < _configSize; i++)
200                   {
201                       if (strcmp(_config[i].name, name) == 0)
202                       {
203                           Strlcpy(value, _config[i].value, EXECUTOR_BUFFER_SIZE);
204                           return 0;
205                       }
206                   }
207               
208 mike  1.1.2.1     /* Not found! */
209                   return -1;
210               }

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2