4 martin 1.4 // Licensed to The Open Group (TOG) under one or more contributor license
5 // agreements. Refer to the OpenPegasusNOTICE.txt file distributed with
6 // this work for additional information regarding copyright ownership.
7 // Each contributor licenses this file to you under the OpenPegasus Open
8 // Source License; you may not use this file except in compliance with the
9 // License.
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining a
12 // copy of this software and associated documentation files (the "Software"),
13 // to deal in the Software without restriction, including without limitation
14 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 // and/or sell copies of the Software, and to permit persons to whom the
16 // Software is furnished to do so, subject to the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be included
19 // in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
25 martin 1.4 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 //////////////////////////////////////////////////////////////////////////
30 /*
31
|
32 kumpf 1.2 #include <string.h>
33 #include <ctype.h>
34 #include <stdio.h>
35 #include "Config.h"
36 #include "Strlcpy.h"
37 #include "Strlcat.h"
38 #include "Path.h"
39 #include "Defines.h"
40 #include "Globals.h"
41 #include "Assert.h"
42
43 /*
44 **==============================================================================
45 **
46 ** ConfigParameter
47 **
48 ** The _config[] array below defines default values for the
49 ** various configuration items.
50 **
51 **==============================================================================
52 */
53 kumpf 1.2
54 struct ConfigParameter
55 {
56 const char* name;
57 const char* value;
58 };
59
60 static struct ConfigParameter _config[] =
61 {
62 #include <Pegasus/Config/FixedPropertyTable.h>
63 };
64
65 static size_t _configSize =
66 sizeof(_config) / sizeof(_config[0]);
67
68 /*
69 **==============================================================================
70 **
71 ** GetConfigParamFromCommandLine()
72 **
73 ** Attempt to find a command line configuratin parameter of the form
74 kumpf 1.2 ** name=value. For example:
75 ** cimservermain option=value
76 ** Return 0 if found.
77 **
78 **==============================================================================
79 */
80
81 int GetConfigParamFromCommandLine(
82 const char* name,
83 char value[EXECUTOR_BUFFER_SIZE])
84 {
85 size_t n = strlen(name);
86 int i;
87
88 EXECUTOR_ASSERT(globals.argv != NULL);
89
90 for (i = 1; i < globals.argc; i++)
91 {
92 if (strncmp(globals.argv[i], name, n) == 0 && globals.argv[i][n] == '=')
93 {
94 Strlcpy(value, globals.argv[i] + n + 1, EXECUTOR_BUFFER_SIZE);
95 kumpf 1.2 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
113 int GetConfigParamFromFile(
114 const char* path,
115 const char* name,
116 kumpf 1.2 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 if (buffer[0] == '#')
134 continue;
135
136 /* Remove trailing whitespace. */
137 kumpf 1.2
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 {
155 Strlcpy(value, buffer + n + 1, EXECUTOR_BUFFER_SIZE);
156 fclose(is);
157 return 0;
158 kumpf 1.2 }
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 */
176
177 int GetConfigParam(
178 const char* name,
179 kumpf 1.2 char value[EXECUTOR_BUFFER_SIZE])
180 {
|
202 kumpf 1.2 GetConfigParamFromFile(path, name, value) == 0)
203 return 0;
204
205 /* (3) Finally check the default configuration table. */
206
207 for (i = 0; i < _configSize; i++)
208 {
209 if (strcmp(_config[i].name, name) == 0)
210 {
211 Strlcpy(value, _config[i].value, EXECUTOR_BUFFER_SIZE);
212 return 0;
213 }
214 }
215
216 /* Not found! */
217 return -1;
218 }
|