#include #include #include /* for access */ #include #include #include #include "ant.h" /* for LinkList */ #include "libCfg_local.h" #include "libCfg.h" static LinkList _projects = NULL; const int MAXARGS = 5; char *GetGoodPath (char *fileName) { static char path [256]; char *config; short GoodPath; GoodPath = 0; if ((config = getenv("SDDAS_CONFIG")) != NULL) { snprintf(path, 256, "%s/%s", config, fileName); if (access(path, R_OK) != 0) fprintf(stderr, "Defaulting to $SDDAS_HOME for %s.\n", fileName); else GoodPath = 1; } if (!GoodPath && (config = getenv("SDDAS_HOME")) != NULL) { snprintf(path, 256, "%s/config/%s", config, fileName); if (access(path, R_OK) == 0) GoodPath = 1; } if (!GoodPath) return NULL; return path; } void CfgInit () { if (_projects == NULL) { char *path; /* This is a non fatal error! */ /* Must be before reading the DB.cfg file because we want to use the values in this file for expanding environment variables used in that file */ path = GetGoodPath ("sddas.cfg"); if (path != NULL) { InitSDDASCfg (path); } path = GetGoodPath (PROJECTScfg); if (path == NULL) { fprintf (stderr, "Cannot read PROJECTS configuration [%s]\n", path); exit (2); } _projects = (LinkList) P_Config (path); path = GetGoodPath (ITEMcfg); if (path == NULL) { fprintf (stderr, "Cannot read DB configuration [%s]\n", path); exit (2); } DB_Config (path, _projects); } } void AddSCFs () { char tmp [256]; LinkList l; StrHier S, S_SCF; int i; for (i = 0; i < 2; i++) { switch (i) { case 0 : if (getenv ("SDDAS_HOME") != NULL) snprintf (tmp, sizeof (tmp), "%s/SCF", getenv ("SDDAS_HOME")); break; case 1 : if (getenv ("HOME") != NULL) snprintf (tmp, sizeof (tmp), "%s/SCF", getenv ("HOME")); break; } /* switch */ if ((l = LoadSCFs (tmp)) != NULL) { switch (i) { case 0 : strcpy (tmp, "public-SCFs"); break; case 1 : snprintf (tmp, sizeof (tmp), "%s-SCFs", GetUserName ()); break; } /* switch */ S = StrHierCreateNodes (1); S->str = strdup (tmp); S->sublist = l; S->GetNextItems = IDFSNextItems; while (l) { S_SCF = (StrHier) l->data; S_SCF->parent = S; l = l->next; } /* while */ _projects = LinkAppend (_projects, S); } /* if */ } /* for */ } void CfgInitSCF () { CfgInit (); AddSCFs (); } /* * char *item - what is being queried. * int elmt - index into array of return values for desired string pointer. * char *... - the hierarchy of what is desired. * * return a pointer to the string value associated with the 'item'. There may be * more than one string value, therefore elmt acts as an index into the array of * string values. */ char *CfgGetItem (const char *item, int elmt, ...) { va_list ap; char *args [MAXARGS]; int argno = 0; char *foundItem = NULL; StrHier Source = NULL; StrHier S = NULL; assert (_projects != NULL); va_start (ap, elmt); while ((args [argno++] = va_arg (ap, char *)) != NULL); va_end (ap); Source = SourceByStr (_projects, args [0], args [1], args [2], args [3], args [4], NULL); S = Source; while (S != NULL) { if ((foundItem = ValueFromItem (S, item, elmt))) break; S = S->parent; } if (!S) return NULL; return foundItem; } char *CfgGetPath (const char *item, ...) { va_list ap; char *args [MAXARGS]; int argno = 0; char *p; StrHier S, Source; static char path [256]; int i, j, level; assert (_projects != NULL); for (i = 0; i < MAXARGS; i++) args [i] = NULL; va_start (ap, item); while ((args [argno++] = va_arg (ap, char *)) != NULL); va_end (ap); /* Look for the item in the config file */ /* if ((p = CfgItem (item, 0, args [0], args [1], args [2], args [3], args [4])) == NULL) return NULL; Source = SourceByStr (_projects, args [0], args [1], args [2], args [3], args [4], NULL); */ Source = SourceByStr (_projects, args [0], args [1], args [2], args [3], args [4], NULL); S = Source; while (S != NULL) { if ((p = ValueFromItem (S, item, 0))) break; S = S->parent; } if (!S) return NULL; memset (path, 0, 256); strcpy (path, p); if (path [strlen (path) - 1] != '%') { for (i = 0, S = Source; StrHierParent (S); S = StrHierParent (S), i++); i--; level = 3; while (level >= 0) { strcat (path, "/"); strcat (path, SNAME (S)); for (j = --i, S = Source; j >= 0; S = StrHierParent (S), j--); level--; } } else path [strlen (path) - 1] = '\0'; return path; } void CfgTerm () { aP_Free (_projects); _projects = NULL; } LinkList CfgProjects () { return (_projects); } LinkList CfgHierarchy (int numargs, ...) { va_list ap; char *args [MAXARGS]; char *str = NULL; int count = 0; LinkList l = _projects; StrHier S; int i; assert (_projects != NULL); memset (args, 0, sizeof (args)); va_start (ap, numargs); for (i = 0; i < numargs; i++) args [i] = va_arg (ap, char *); va_end (ap); while (l != NULL) { S = (StrHier) l->data; switch (count) { case 0 : str = args [0]; break; case 1 : str = args [1]; break; case 2 : str = args [2]; break; case 3 : str = args [3]; break; case 4 : str = args [4]; break; } if (str == NULL) break; /* we found the limit of what we were looking */ else if (strcmp (SNAME (S), str) == 0) { l = SUBLIST (S); count++; } else l = l->next; } return (l); } char *CfgListName (void *data) { StrHier S = (StrHier) data; return (S->str); } StrHier CfgStrHier (int numargs, ...) { va_list ap; char *args [MAXARGS]; int i; assert (_projects != NULL); memset (args, 0, sizeof (args)); va_start (ap, numargs); for (i = 0; i < numargs; i++) args [i] = va_arg (ap, char *); va_end (ap); return (SourceByStr (_projects, args [0], args [1], args [2], args [3], args [4], NULL)); }