+/* Clear out and free the dependencies. */
+void clearmoduledeps() {
+ unsigned int i;
+
+ for (i=0;i<knownmodules;i++) {
+ freesstring(moduledeps[i].name);
+ if (moduledeps[i].parents) {
+ free(moduledeps[i].parents);
+ moduledeps[i].parents=NULL;
+ }
+ if (moduledeps[i].children) {
+ free(moduledeps[i].children);
+ moduledeps[i].children=NULL;
+ }
+ }
+
+ knownmodules=0;
+}
+
+/* Get a pointer to the given module's dependency record. */
+struct module_dep *getmoduledep(const char *name) {
+ unsigned int i;
+
+ for (i=0;i<knownmodules;i++) {
+ if (!strcmp(name,moduledeps[i].name->content))
+ return &(moduledeps[i]);
+ }
+
+ return NULL;
+}
+
+/* Populate the dependency array. Read the monster description above for details.
+ * This relies on moddir being set, so call this after setting it up. */
+void initmoduledeps() {
+ FILE *fp;
+ char buf[1024];
+ char *largv[100];
+ char largc;
+ char *ch;
+ struct module_dep *mdp, *tmdp;
+ unsigned int i,j;
+
+ sprintf(buf,"%s/%s",moddir->content,DEPFILE);
+
+ if (!(fp=fopen(buf,"r"))) {
+ Error("core",ERR_WARNING,"Unable to open module dependency file: %s",buf);
+ } else {
+ /* First pass */
+ while (!feof(fp)) {
+ fgets(buf, sizeof(buf), fp);
+ if (feof(fp))
+ break;
+
+ /* Chomp off that ruddy newline. */
+ for (ch=buf;*ch;ch++) {
+ if (*ch=='\n' || *ch=='\r') {
+ *ch='\0';
+ break;
+ }
+ }
+
+ /* We have a space-delimited list of things. Whatever will we do with that? :) */
+ largc=splitline(buf,largv,100,0);
+
+ if (largc<1)
+ continue;
+
+ /* Add us to the array */
+ i=knownmodules++;
+ if (i>=MAXMODULES) {
+ Error("core",ERR_ERROR,
+ "Too many modules in dependency file; rebuild with higher MAXMODULES. Module dependencies disabled.\n");
+ clearmoduledeps();
+ return;
+ }
+
+ mdp=&(moduledeps[i]);
+
+ mdp->name=getsstring(largv[0],100);
+ mdp->numparents=largc-1;
+ mdp->numchildren=0; /* peace, for now */
+ mdp->parents=malloc(mdp->numparents * sizeof(struct module_dep *));
+
+ /* Fill in the parents array */
+ for (i=0;i<(largc-1);i++) {
+ if (!(mdp->parents[i]=getmoduledep(largv[i+1]))) {
+ Error("core",ERR_WARNING,"Couldn't find parent module %s of %s. Module dependencies disabled.",
+ largv[i+1],largv[0]);
+ clearmoduledeps();
+ return;
+ }
+ mdp->parents[i]->numchildren++; /* break the bad news */
+ }
+ }
+
+ /* Second pass */
+ for (i=0;i<knownmodules;i++) {
+ mdp=&(moduledeps[i]);
+
+ /* Allocate child array */
+ if (mdp->numchildren) {
+ mdp->children=malloc(mdp->numchildren * sizeof(struct module_dep *));
+ mdp->numchildren=0; /* if only real life were this simple */
+ }
+
+ /* Now fill in the children arrays of our parents (bear with me on this) */
+ for (j=0;j<mdp->numparents;j++) {
+ tmdp=mdp->parents[j]; /* This is just... */
+
+ tmdp->children[tmdp->numchildren++]=mdp; /* ... to give this line a chance at making sense */
+ }
+ }
+ }
+}
+