#ident "$URL: svn://elmer/devel/SVN/SDDAS/trunk/libCfg/DB_Config.c $ %D% SwRI" #include #include #include #include #include "ant.h" /* for GetEnv, BasenameOf */ #include "libCfg_local.h" static int NCP = 0; typedef struct GlobalItems_s { char *Item; char **Values; int Nvalues; } GlobalItems_t; LinkList FindStr(char *str, LinkList L) { LinkList l; StrHier S; for (l=L; l != NULL; l = l->next) { S = (StrHier) l->data; if (strcmp(str, S->str) == 0) return l; } return NULL; } /* * char *file - configuration file name. * LinkList P - root pointer to the Projects tree. * * Initialize the item-value information to the defined nodes of the Projects tree. */ int DB_Config(char *file, LinkList P) { config_t *p_handle; int NGlobalItems; GlobalItems_t *GlobalItems; int silent = 1; static int ncp = -1; static def_t def_items[] = { {"NCP", T_INT, (void *)&ncp, IS_OPTIONAL}, {"ITEMS", T_STRPTR_ARRY, (void *)NULL, IS_REQUIRED}, {NULL}, }; static def_t def_values[] = { {"VALUE", T_STRPTR_ARRY, (void *)NULL, IS_REQUIRED}, {NULL}, }; int NLevels; static def_t def_levels[] = { {"LEVELS", T_STRPTR_ARRY, (void *)NULL, IS_REQUIRED}, {NULL}, }; def_t *DefItems; char prefix[80], *token; LinkList L, I; StrHier S = NULL; int i, j, k, frst=1; ItemValue_t *ItemValue; /* | || get: 1. the number of concurrent processes defined || 2. the defined items | */ if ((p_handle = open_config(file, 0)) == CNULL) { if (!silent) fprintf(stderr, "Error OPENing '%s'\n", file); return 0; } /* | need to initialize these just in case this routine is called more that once */ def_items[0].required = IS_OPTIONAL; def_items[1].value = NULL; def_items[1].required = IS_REQUIRED; if (parse_config(p_handle, NULL, def_items) != 0) { if (!silent) fprintf(stderr, "Error w/ %s\n", file); close_config(p_handle); return 0; } /* | default the number of concurrent processes to 1 if it was not defined */ if (ncp > 1) NCP = ncp; /* | the number of defined global items and allocate memory for each */ NGlobalItems = def_items[1].n_values; GlobalItems = (GlobalItems_t *) calloc(NGlobalItems, sizeof(GlobalItems_t)); if (!(DefItems = (def_t *)calloc((NGlobalItems + 1), sizeof(def_t)))) { free(GlobalItems); close_config(p_handle); return 0; } /* | get each value for each global item */ for (i=0; i < NGlobalItems; i++) { char param[64]; GlobalItems[i].Item = ((char **)def_items[1].value)[i]; snprintf(prefix, sizeof (prefix), "%s.", GlobalItems[i].Item); def_values[0].required = IS_REQUIRED; def_values[0].value = NULL; if (parse_config(p_handle, prefix, def_values) != 0) { if (!silent) fprintf(stderr, "No definition for %s\n", GlobalItems[i].Item); } GlobalItems[i].Nvalues = def_values[0].n_values; GlobalItems[i].Values = (char **)def_values[0].value; snprintf(param, sizeof (param), "%s.VALUE", GlobalItems[i].Item); DefItems[i].param = strdup(param); DefItems[i].type = T_STRPTR_ARRY; DefItems[i].value = NULL; DefItems[i].required = IS_OPTIONAL; DefItems[i].n_values = 0; } DefItems[i].param = NULL; /* | the number of defined levels for each global item */ def_levels[0].value = NULL; def_levels[0].required = IS_REQUIRED; if (parse_config(p_handle, NULL, def_levels) != 0) { if (!silent) fprintf(stderr, "Error finding LEVELS in %s\n", file); for (i=0; i 60) { if (!silent) fprintf(stderr, "environ var too long: '%s' in %s\n", *ItemValue[j].Value, file); break; } } if (cntr == 0) { if (!silent) fprintf(stderr, "illformed environ var: '%s' in %s\n", *ItemValue[j].Value, file); continue; } else env[cntr] = '\0'; if ((c = GetEnv(env))) { len = strlen(&ItemValue[j].Value[k][cntr+1]); len += strlen(c) + 1; s = (char *) calloc(len, 1); snprintf(s, len, "%s%s", c, &ItemValue[j].Value[k][cntr+1]); free(ItemValue[j].Value[k]); ItemValue[j].Value[k] = s; } else { if (!silent) fprintf(stderr, "environ var '%s' not set [%s]\n", env, BasenameOf(file)); } } } } /* block */ } /* for (j=0; j< NGlobalItems; j++) */ I = P; token = strtok(Level, ":"); do { L = FindStr(token, I); if (L == NULL) { if (frst) { if (!silent) fprintf(stderr, "Undefined token(s) in %s:\n", BasenameOf(file)); frst = 0; } if (!silent) fprintf(stderr, " %s\n", token); break; } S = (StrHier)L->data; I = S->sublist; } while ((token = strtok(NULL, ":"))); if (L == NULL || S->info != NULL) { for (j=0; jDestroy); free(ItemValue); continue; } S->info = (void *)ItemValue; } for (i=0; i