6 * See Copyright for the status of this software.
8 * Copyright (C) 2002-2003 Aleksey Sanin <aleksey@aleksey.com>
11 #define snprintf _snprintf
22 static int xmlSecAppCmdLineMatchParam (const char* argvParam,
23 const char* paramName,
24 int canHaveNameString);
25 static xmlSecAppCmdLineParamPtr xmlSecAppCmdLineParamsListFind (xmlSecAppCmdLineParamPtr* params,
26 xmlSecAppCmdLineParamTopic topics,
28 static int xmlSecAppCmdLineParamRead (xmlSecAppCmdLineParamPtr param,
32 static int xmlSecAppCmdLineTimeParamRead (const char* str,
36 xmlSecAppCmdLineParamIsSet(xmlSecAppCmdLineParamPtr param) {
37 return(((param != NULL) && (param->value != NULL)) ? 1 : 0);
41 xmlSecAppCmdLineParamGetString(xmlSecAppCmdLineParamPtr param) {
42 if(param->type != xmlSecAppCmdLineParamTypeString) {
43 fprintf(stderr, "Error: parameter \"%s\" is not string.\n", param->fullName);
46 return((param->value != NULL) ? param->value->strValue : NULL);
50 xmlSecAppCmdLineParamGetStringList(xmlSecAppCmdLineParamPtr param) {
51 if(param->type != xmlSecAppCmdLineParamTypeStringList) {
52 fprintf(stderr, "Error: parameter \"%s\" is not string list.\n", param->fullName);
55 return((param->value != NULL) ? param->value->strListValue : NULL);
59 xmlSecAppCmdLineParamGetInt(xmlSecAppCmdLineParamPtr param, int def) {
60 if(param->type != xmlSecAppCmdLineParamTypeNumber) {
61 fprintf(stderr, "Error: parameter \"%s\" is not integer.\n", param->fullName);
64 return((param->value != NULL) ? param->value->intValue : def);
68 xmlSecAppCmdLineParamGetTime(xmlSecAppCmdLineParamPtr param, time_t def) {
69 if(param->type != xmlSecAppCmdLineParamTypeTime) {
70 fprintf(stderr, "Error: parameter \"%s\" is not time.\n", param->fullName);
73 return((param->value != NULL) ? param->value->timeValue : def);
77 xmlSecAppCmdLineParamsListParse(xmlSecAppCmdLineParamPtr* params,
78 xmlSecAppCmdLineParamTopic topics,
79 const char** argv, int argc, int pos) {
80 xmlSecAppCmdLineParamPtr param;
84 assert(params != NULL);
87 while((pos < argc) && (argv[pos][0] == '-')) {
88 param = xmlSecAppCmdLineParamsListFind(params, topics, argv[pos]);
90 fprintf(stderr, "Error: parameter \"%s\" is not supported or the requested\nfeature might have been disabled during compilation.\n", argv[pos]);
94 ret = xmlSecAppCmdLineParamRead(param, argv, argc, pos);
96 fprintf(stderr, "Error: failed to parse parameter \"%s\".\n", argv[pos]);
102 /* check that all parameters at the end are filenames */
103 for(ii = pos; (ii < argc); ++ii) {
104 if(argv[ii][0] == '-') {
105 fprintf(stderr, "Error: filename is expected instead of parameter \"%s\".\n", argv[ii]);
117 xmlSecAppCmdLineParamsListClean(xmlSecAppCmdLineParamPtr* params) {
118 xmlSecAppCmdLineValuePtr tmp;
121 assert(params != NULL);
123 for(i = 0; params[i] != NULL; ++i) {
124 while(params[i]->value != NULL) {
125 tmp = params[i]->value;
126 params[i]->value = params[i]->value->next;
127 xmlSecAppCmdLineValueDestroy(tmp);
133 xmlSecAppCmdLineParamsListPrint(xmlSecAppCmdLineParamPtr* params,
134 xmlSecAppCmdLineParamTopic topics,
138 assert(params != NULL);
139 assert(output != NULL);
141 for(i = 0; params[i] != NULL; ++i) {
142 if(((params[i]->topics & topics) != 0) && (params[i]->help != NULL)) {
143 fprintf(output, " %s\n", params[i]->help);
148 xmlSecAppCmdLineValuePtr
149 xmlSecAppCmdLineValueCreate(xmlSecAppCmdLineParamPtr param, int pos) {
150 xmlSecAppCmdLineValuePtr value;
152 assert(param != NULL);
153 value = (xmlSecAppCmdLineValuePtr) malloc(sizeof(xmlSecAppCmdLineValue));
155 fprintf(stderr, "Error: malloc failed (%d bytes).\n", sizeof(xmlSecAppCmdLineValue));
158 memset(value, 0, sizeof(xmlSecAppCmdLineValue));
160 value->param = param;
166 xmlSecAppCmdLineValueDestroy(xmlSecAppCmdLineValuePtr value) {
167 assert(value != NULL);
169 if(value->strListValue != NULL) {
170 free((void*)value->strListValue);
176 xmlSecAppCmdLineMatchParam(const char* argvParam, const char* paramName,
177 int canHaveNameString) {
178 assert(argvParam != NULL);
179 assert(paramName != NULL);
181 if(canHaveNameString != 0) {
182 int len = strlen(paramName);
184 if((strncmp(argvParam, paramName, len) == 0) &&
185 ((argvParam[len] == '\0') || (argvParam[len] == ':'))) {
189 } else if(strcmp(argvParam, paramName) == 0) {
195 static xmlSecAppCmdLineParamPtr
196 xmlSecAppCmdLineParamsListFind(xmlSecAppCmdLineParamPtr* params, xmlSecAppCmdLineParamTopic topics,
199 int canHaveNameString;
201 assert(params != NULL);
202 assert(name != NULL);
204 for(i = 0; params[i] != NULL; ++i) {
205 if((params[i]->topics & topics) == 0) {
209 canHaveNameString = ((params[i]->flags & xmlSecAppCmdLineParamFlagParamNameValue) != 0) ? 1 : 0;
210 if((params[i]->fullName != NULL) &&
211 (xmlSecAppCmdLineMatchParam(name, params[i]->fullName, canHaveNameString) == 1)) {
216 if((params[i]->shortName != NULL) &&
217 (xmlSecAppCmdLineMatchParam(name, params[i]->shortName, canHaveNameString) == 1)) {
227 xmlSecAppCmdLineParamRead(xmlSecAppCmdLineParamPtr param, const char** argv, int argc, int pos) {
228 xmlSecAppCmdLineValuePtr value;
229 xmlSecAppCmdLineValuePtr prev = NULL;
232 assert(param != NULL);
233 assert(argv != NULL);
236 /* first find the previous value in the list */
237 if((param->flags & xmlSecAppCmdLineParamFlagMultipleValues) != 0) {
239 while((prev != NULL) && (prev->next != NULL)) {
242 } else if(param->value != NULL) {
243 fprintf(stderr, "Error: only one parameter \"%s\" is allowed.\n", argv[pos]);
247 /* create new value and add to the list */
248 value = xmlSecAppCmdLineValueCreate(param, pos);
250 fprintf(stderr, "Error: failed to create value for parameter \"%s\".\n", argv[pos]);
254 assert(prev->next == NULL);
257 param->value = value;
260 /* if we can have a string value after the name, parse it */
261 if((param->flags & xmlSecAppCmdLineParamFlagParamNameValue) != 0) {
262 value->paramNameValue = strchr(argv[pos], ':');
263 if(value->paramNameValue != NULL) {
264 ++value->paramNameValue;
268 switch(param->type) {
269 case xmlSecAppCmdLineParamTypeFlag:
272 case xmlSecAppCmdLineParamTypeString:
273 if(pos + 1 >= argc) {
274 fprintf(stderr, "Error: string argument expected for parameter \"%s\".\n", argv[pos]);
277 value->strValue = argv[++pos];
279 case xmlSecAppCmdLineParamTypeStringList:
280 if(pos + 1 >= argc) {
281 fprintf(stderr, "Error: string list argument expected for parameter \"%s\".\n", argv[pos]);
284 value->strValue = argv[++pos];
285 buf = (char*)malloc(strlen(value->strValue) + 2);
287 fprintf(stderr, "Error: failed to allocate memory (%d bytes).\n", strlen(value->strValue) + 2);
290 memset(buf, 0, strlen(value->strValue) + 2);
291 memcpy(buf, value->strValue, strlen(value->strValue));
292 value->strListValue = buf;
293 while((*buf) != '\0') {
300 case xmlSecAppCmdLineParamTypeNumber:
301 if(pos + 1 >= argc) {
302 fprintf(stderr, "Error: integer argument expected for parameter \"%s\".\n", argv[pos]);
305 value->strValue = argv[++pos];
306 if(sscanf(value->strValue, "%d", &(value->intValue)) != 1) {
307 fprintf(stderr, "Error: integer argument \"%s\" is invalid.\n", value->strValue);
311 case xmlSecAppCmdLineParamTypeTime:
312 if(pos + 1 >= argc) {
313 fprintf(stderr, "Error: time argument expected for parameter \"%s\".\n", argv[pos]);
316 value->strValue = argv[++pos];
317 if(xmlSecAppCmdLineTimeParamRead(value->strValue, &(value->timeValue)) < 0) {
318 fprintf(stderr, "Error: time argument \"%s\" is invalid, expected format is \"YYYY-MM-DD HH:MM:SS\").\n", value->strValue);
327 xmlSecAppCmdLineTimeParamRead(const char* str, time_t* t) {
331 if((str == NULL) || (t == NULL)) {
334 memset(&tm, 0, sizeof(tm));
337 n = sscanf(str, "%4d-%2d-%2d%*c%2d:%2d:%2d",
338 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
339 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
340 if((n != 6) || (tm.tm_year < 1900)
341 || (tm.tm_mon < 1) || (tm.tm_mon > 12)
342 || (tm.tm_mday < 1) || (tm.tm_mday > 31)
343 || (tm.tm_hour < 0) || (tm.tm_hour > 23)
344 || (tm.tm_min < 0) || (tm.tm_min > 59)
345 || (tm.tm_sec < 0) || (tm.tm_sec > 61)) {
349 tm.tm_year -= 1900; /* tm relative format year */
350 tm.tm_mon -= 1; /* tm relative format month */