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

  1 kumpf 1.2 /*
  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 kumpf 1.2 // 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           #include "Globals.h"
 42           #include "Assert.h"
 43 kumpf 1.2 
 44           /*
 45           **==============================================================================
 46           **
 47           ** ConfigParameter
 48           **
 49           **     The _config[] array below defines default values for the
 50           **     various configuration items.
 51           **
 52           **==============================================================================
 53           */
 54           
 55           struct ConfigParameter
 56           {
 57               const char* name;
 58               const char* value;
 59           };
 60           
 61           static struct ConfigParameter _config[] =
 62           {
 63           #include <Pegasus/Config/FixedPropertyTable.h>
 64 kumpf 1.2 };
 65           
 66           static size_t _configSize =
 67               sizeof(_config) / sizeof(_config[0]);
 68           
 69           /*
 70           **==============================================================================
 71           **
 72           ** GetConfigParamFromCommandLine()
 73           **
 74           **     Attempt to find a command line configuratin parameter of the form
 75           **     name=value. For example:
 76           **         cimservermain option=value
 77           **     Return 0 if found.
 78           **
 79           **==============================================================================
 80           */
 81           
 82           int GetConfigParamFromCommandLine(
 83               const char* name,
 84               char value[EXECUTOR_BUFFER_SIZE])
 85 kumpf 1.2 {
 86               size_t n = strlen(name);
 87               int i;
 88           
 89               EXECUTOR_ASSERT(globals.argv != NULL);
 90           
 91               for (i = 1; i < globals.argc; i++)
 92               {
 93                   if (strncmp(globals.argv[i], name, n) == 0 && globals.argv[i][n] == '=')
 94                   {
 95                       Strlcpy(value, globals.argv[i] + n + 1, EXECUTOR_BUFFER_SIZE);
 96                       return 0;
 97                   }
 98               }
 99           
100               return -1;
101           }
102           
103           /*
104           **==============================================================================
105           **
106 kumpf 1.2 ** GetConfigParamFromFile()
107           **
108           **     Attempt to find the named option in the configuration file. If found,
109           **     set value and return 0.
110           **
111           **==============================================================================
112           */
113           
114           int GetConfigParamFromFile(
115               const char* path,
116               const char* name,
117               char value[EXECUTOR_BUFFER_SIZE])
118           {
119               char buffer[EXECUTOR_BUFFER_SIZE];
120               FILE* is;
121               size_t n;
122           
123               if ((is = fopen(path, "r")) == NULL)
124                   return -1;
125           
126               n = strlen(name);
127 kumpf 1.2 
128               while (fgets(buffer, sizeof(buffer), is) != 0)
129               {
130                   size_t r;
131           
132                   /* Skip comments. */
133           
134                   if (buffer[0] == '#')
135                       continue;
136           
137                   /* Remove trailing whitespace. */
138           
139                   r = strlen(buffer);
140           
141                   while (r--)
142                   {
143                       if (isspace(buffer[r]))
144                           buffer[r] = '\0';
145                   }
146           
147                   /* Skip blank lines. */
148 kumpf 1.2 
149                   if (buffer[0] == '\0')
150                       continue;
151           
152                   /* Check option. */
153           
154                   if (strncmp(buffer, name, n) == 0 &&  buffer[n] == '=')
155                   {
156                       Strlcpy(value, buffer + n + 1, EXECUTOR_BUFFER_SIZE);
157                       fclose(is);
158                       return 0;
159                   }
160               }
161           
162               /* Not found! */
163               fclose(is);
164               return -1;
165           }
166           
167           /*
168           **==============================================================================
169 kumpf 1.2 **
170           ** GetConfigParam()
171           **
172           **     Attempt to find a configuration setting for the given name. First,
173           **     search the command line and then the config file.
174           **
175           **==============================================================================
176           */
177           
178           int GetConfigParam(
179               const char* name,
180               char value[EXECUTOR_BUFFER_SIZE])
181           {
182 kumpf 1.3     const char* configFileName = 0;
183 kumpf 1.2     char path[EXECUTOR_BUFFER_SIZE];
184               size_t i;
185           
186               /* (1) First check command line. */
187           
188               if (GetConfigParamFromCommandLine(name, value) == 0)
189                   return 0;
190           
191 kumpf 1.3     /* (2) Next check config file. */
192 kumpf 1.2 
193 kumpf 1.3     if (strcmp(name, "shutdownTimeout") == 0)
194               {
195                   configFileName = PEGASUS_CURRENT_CONFIG_FILE_PATH;
196               }
197               else
198               {
199                   configFileName = PEGASUS_PLANNED_CONFIG_FILE_PATH;
200               }
201           
202               if (GetHomedPath(configFileName, path) == 0 &&
203 kumpf 1.2         GetConfigParamFromFile(path, name, value) == 0)
204                   return 0;
205           
206               /* (3) Finally check the default configuration table. */
207           
208               for (i = 0; i < _configSize; i++)
209               {
210                   if (strcmp(_config[i].name, name) == 0)
211                   {
212                       Strlcpy(value, _config[i].value, EXECUTOR_BUFFER_SIZE);
213                       return 0;
214                   }
215               }
216           
217               /* Not found! */
218               return -1;
219           }

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2