/* reverse.c * last revised at 2, Feb. 1994 * * Copyright (C) 1994, All rights reserved. * written by Gyudong Kim(chilly@eleceng.adelaide.edu.au) * */ #include "findhier.h" extern struct branch *trees; extern int topspecified; extern int interactive; extern char *targ, *top; extern FILE *OUTPUT; static struct names *parents; static void add_parent(name) char *name; { struct names *tmp; for (tmp=parents;tmp!=NULL;tmp=tmp->next) { if (tmp->name == name) return; } tmp = (struct names *)myalloc(sizeof(struct names),"add_parent"); tmp->name = name; tmp->next = parents; parents = tmp; } static int look_parent(parent,name) struct branch *parent; char *name; { struct branch *tmpb; int found = 0; if (parent->name == name) return(1); if (parent->offspring == NULL) return(0); push(parent->name); for (tmpb=parent->offspring;tmpb!=NULL;tmpb=tmpb->next) { if (check(tmpb->name)) continue; if (look_parent(tmpb,name)) { add_parent(parent->name); found = 1; } } pop(); return(found); } static struct branch *look_top(bran,mytop) struct branch *bran; char *mytop; { struct branch *tmpb; struct branch *result; if (bran->name == mytop) return(bran); if (bran->offspring == NULL) return((struct branch *)NULL); push(bran->name); for(tmpb=bran->offspring;tmpb!=NULL;tmpb=tmpb->next) { if ((result = look_top(tmpb,mytop)) == NULL) continue; pop(); return(result); } pop(); return((struct branch *)NULL); } struct branch *find_top(mytop) char *mytop; { struct branch *tmp; static struct branch *result; for(tmp=trees;tmp!=NULL;tmp=tmp->next) { result = look_top(tmp,mytop); if (result!=NULL) return(result); } #ifdef __OLD__ if (!interactive) { warning("Cell |%s| not found.\n",mytop); exit(-1); } #endif /* __OLD__ */ warning("Cell |%s| not found.\n",mytop); return((struct branch *)NULL); } int reverse() { struct branch *tmp,*tmpb; struct names *tmpn; if (topspecified) { tmpb = find_top(top); if (tmpb==NULL) return(1); if (look_parent(tmpb,targ)) add_parent(tmpb->name); } else { for(tmp=trees;tmp!=NULL;tmp=tmp->next) { if (look_parent(tmp,targ)) add_parent(tmp->name); } } for(tmpn=parents;tmpn!=NULL;tmpn=tmpn->next) (void)fprintf(OUTPUT,"%s\n",tmpn->name); freelink(&parents); return(0); } /* reverse.c */