Home | History | Annotate | Line # | Download | only in rcorder
rcorder.c revision 1.7
      1 /*	$NetBSD: rcorder.c,v 1.7 2000/08/04 07:33:55 enami Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998, 1999 Matthew R. Green
      5  * All rights reserved.
      6  * Copyright (c) 1998
      7  * 	Perry E. Metzger.  All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed for the NetBSD Project
     20  *	by Perry E. Metzger.
     21  * 4. The name of the author may not be used to endorse or promote products
     22  *    derived from this software without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     27  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     33  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/types.h>
     37 #include <sys/stat.h>
     38 
     39 #include <err.h>
     40 #include <stdio.h>
     41 #include <stdlib.h>
     42 #include <string.h>
     43 #include <unistd.h>
     44 #include <util.h>
     45 
     46 #include "ealloc.h"
     47 #include "sprite.h"
     48 #include "hash.h"
     49 
     50 #ifdef DEBUG
     51 int debug = 0;
     52 # define	DPRINTF(args) if (debug) { fflush(stdout); fprintf args; }
     53 #else
     54 # define	DPRINTF(args)
     55 #endif
     56 
     57 #define REQUIRE_STR	"# REQUIRE:"
     58 #define REQUIRE_LEN	(sizeof(REQUIRE_STR) - 1)
     59 #define REQUIRES_STR	"# REQUIRES:"
     60 #define REQUIRES_LEN	(sizeof(REQUIRES_STR) - 1)
     61 #define PROVIDE_STR	"# PROVIDE:"
     62 #define PROVIDE_LEN	(sizeof(PROVIDE_STR) - 1)
     63 #define PROVIDES_STR	"# PROVIDES:"
     64 #define PROVIDES_LEN	(sizeof(PROVIDES_STR) - 1)
     65 #define BEFORE_STR	"# BEFORE:"
     66 #define BEFORE_LEN	(sizeof(BEFORE_STR) - 1)
     67 #define KEYWORD_STR	"# KEYWORD:"
     68 #define KEYWORD_LEN	(sizeof(KEYWORD_STR) - 1)
     69 #define KEYWORDS_STR	"# KEYWORDS:"
     70 #define KEYWORDS_LEN	(sizeof(KEYWORDS_STR) - 1)
     71 
     72 int exit_code;
     73 int file_count;
     74 char **file_list;
     75 
     76 typedef int bool;
     77 #define TRUE 1
     78 #define FALSE 0
     79 typedef bool flag;
     80 #define SET TRUE
     81 #define RESET FALSE
     82 
     83 Hash_Table provide_hash_s, *provide_hash;
     84 
     85 typedef struct provnode provnode;
     86 typedef struct filenode filenode;
     87 typedef struct f_provnode f_provnode;
     88 typedef struct f_reqnode f_reqnode;
     89 typedef struct strnodelist strnodelist;
     90 
     91 struct provnode {
     92 	flag		head;
     93 	flag		in_progress;
     94 	filenode	*fnode;
     95 	provnode	*next, *last;
     96 };
     97 
     98 struct f_provnode {
     99 	provnode	*pnode;
    100 	f_provnode	*next;
    101 };
    102 
    103 struct f_reqnode {
    104 	Hash_Entry	*entry;
    105 	f_reqnode	*next;
    106 };
    107 
    108 struct strnodelist {
    109 	filenode	*node;
    110 	strnodelist	*next;
    111 	char		s[1];
    112 };
    113 
    114 struct filenode {
    115 	char		*filename;
    116 	flag		in_progress;
    117 	filenode	*next, *last;
    118 	f_reqnode	*req_list;
    119 	f_provnode	*prov_list;
    120 	strnodelist	*keyword_list;
    121 };
    122 
    123 filenode fn_head_s, *fn_head;
    124 
    125 strnodelist *bl_list;
    126 strnodelist *keep_list;
    127 strnodelist *skip_list;
    128 
    129 void do_file __P((filenode *fnode));
    130 void strnode_add __P((strnodelist **, char *, filenode *));
    131 int skip_ok __P((filenode *fnode));
    132 int keep_ok __P((filenode *fnode));
    133 void satisfy_req __P((f_reqnode *rnode, char *filename));
    134 void crunch_file __P((char *));
    135 void parse_require __P((filenode *, char *));
    136 void parse_provide __P((filenode *, char *));
    137 void parse_before __P((filenode *, char *));
    138 void parse_keywords __P((filenode *, char *));
    139 filenode *filenode_new __P((char *));
    140 void add_require __P((filenode *, char *));
    141 void add_provide __P((filenode *, char *));
    142 void add_before __P((filenode *, char *));
    143 void add_keyword __P((filenode *, char *));
    144 void insert_before __P((void));
    145 Hash_Entry *make_fake_provision __P((filenode *));
    146 void crunch_all_files __P((void));
    147 void initialize __P((void));
    148 void generate_ordering __P((void));
    149 int main __P((int, char *[]));
    150 
    151 int
    152 main(argc, argv)
    153 	int argc;
    154 	char *argv[];
    155 {
    156 	int ch;
    157 
    158 	while ((ch = getopt(argc, argv, "dk:s:")) != -1)
    159 		switch (ch) {
    160 		case 'd':
    161 #ifdef DEBUG
    162 			debug = 1;
    163 #else
    164 			warnx("debugging not compiled in, -d ignored");
    165 #endif
    166 			break;
    167 		case 'k':
    168 			strnode_add(&keep_list, optarg, 0);
    169 			break;
    170 		case 's':
    171 			strnode_add(&skip_list, optarg, 0);
    172 			break;
    173 		default:
    174 			/* XXX should crunch it? */
    175 			break;
    176 		}
    177 	argc -= optind;
    178 	argv += optind;
    179 
    180 	file_count = argc;
    181 	file_list = argv;
    182 
    183 	DPRINTF((stderr, "parse_args\n"));
    184 	initialize();
    185 	DPRINTF((stderr, "initialize\n"));
    186 	crunch_all_files();
    187 	DPRINTF((stderr, "crunch_all_files\n"));
    188 	generate_ordering();
    189 	DPRINTF((stderr, "generate_ordering\n"));
    190 
    191 	exit(exit_code);
    192 }
    193 
    194 /*
    195  * initialise various variables.
    196  */
    197 void
    198 initialize()
    199 {
    200 
    201 	fn_head = &fn_head_s;
    202 
    203 	provide_hash = &provide_hash_s;
    204 	Hash_InitTable(provide_hash, file_count);
    205 }
    206 
    207 /* generic function to insert a new strnodelist element */
    208 void
    209 strnode_add(listp, s, fnode)
    210 	strnodelist **listp;
    211 	char *s;
    212 	filenode *fnode;
    213 {
    214 	strnodelist *ent;
    215 
    216 	ent = emalloc(sizeof *ent + strlen(s));
    217 	ent->node = fnode;
    218 	strcpy(ent->s, s);
    219 	ent->next = *listp;
    220 	*listp = ent;
    221 }
    222 
    223 /*
    224  * below are the functions that deal with creating the lists
    225  * from the filename's given and the dependancies and provisions
    226  * in each of these files.  no ordering or checking is done here.
    227  */
    228 
    229 /*
    230  * we have a new filename, create a new filenode structure.
    231  * fill in the bits, and put it in the filenode linked list
    232  */
    233 filenode *
    234 filenode_new(filename)
    235 	char *filename;
    236 {
    237 	filenode *temp;
    238 
    239 	temp = emalloc(sizeof(*temp));
    240 	memset(temp, 0, sizeof(*temp));
    241 	temp->filename = estrdup(filename);
    242 	temp->req_list = NULL;
    243 	temp->prov_list = NULL;
    244 	temp->keyword_list = NULL;
    245 	temp->in_progress = RESET;
    246 	/*
    247 	 * link the filenode into the list of filenodes.
    248 	 * note that the double linking means we can delete a
    249 	 * filenode without searching for where it belongs.
    250 	 */
    251 	temp->next = fn_head->next;
    252 	if (temp->next != NULL)
    253 		temp->next->last = temp;
    254 	temp->last = fn_head;
    255 	fn_head->next = temp;
    256 	return (temp);
    257 }
    258 
    259 /*
    260  * add a requirement to a filenode.
    261  */
    262 void
    263 add_require(fnode, s)
    264 	filenode *fnode;
    265 	char *s;
    266 {
    267 	Hash_Entry *entry;
    268 	f_reqnode *rnode;
    269 	int new;
    270 
    271 	entry = Hash_CreateEntry(provide_hash, s, &new);
    272 	if (new)
    273 		Hash_SetValue(entry, NULL);
    274 	rnode = emalloc(sizeof(*rnode));
    275 	rnode->entry = entry;
    276 	rnode->next = fnode->req_list;
    277 	fnode->req_list = rnode;
    278 }
    279 
    280 /*
    281  * add a provision to a filenode.  if this provision doesn't
    282  * have a head node, create one here.
    283  */
    284 void
    285 add_provide(fnode, s)
    286 	filenode *fnode;
    287 	char *s;
    288 {
    289 	Hash_Entry *entry;
    290 	f_provnode *f_pnode;
    291 	provnode *pnode, *head;
    292 	int new;
    293 
    294 	entry = Hash_CreateEntry(provide_hash, s, &new);
    295 	head = Hash_GetValue(entry);
    296 
    297 	/* create a head node if necessary. */
    298 	if (head == NULL) {
    299 		head = emalloc(sizeof(*head));
    300 		head->head = SET;
    301 		head->in_progress = RESET;
    302 		head->fnode = NULL;
    303 		head->last = head->next = NULL;
    304 		Hash_SetValue(entry, head);
    305 	}
    306 #if 0
    307 	/*
    308 	 * Don't warn about this.  We want to be able to support
    309 	 * scripts that do two complex things:
    310 	 *
    311 	 *	- Two independent scripts which both provide the
    312 	 *	  same thing.  Both scripts must be executed in
    313 	 *	  any order to meet the barrier.  An example:
    314 	 *
    315 	 *		Script 1:
    316 	 *
    317 	 *			PROVIDE: mail
    318 	 *			REQUIRE: LOGIN
    319 	 *
    320 	 *		Script 2:
    321 	 *
    322 	 *			PROVIDE: mail
    323 	 *			REQUIRE: LOGIN
    324 	 *
    325 	 * 	- Two interdependent scripts which both provide the
    326 	 *	  same thing.  Both scripts must be executed in
    327 	 *	  graph order to meet the barrier.  An example:
    328 	 *
    329 	 *		Script 1:
    330 	 *
    331 	 *			PROVIDE: nameservice dnscache
    332 	 *			REQUIRE: SERVERS
    333 	 *
    334 	 *		Script 2:
    335 	 *
    336 	 *			PROVIDE: nameservice nscd
    337 	 *			REQUIRE: dnscache
    338 	 */
    339 	else if (new == 0) {
    340 		warnx("file `%s' provides `%s'.", fnode->filename, s);
    341 		warnx("\tpreviously seen in `%s'.",
    342 		    head->next->fnode->filename);
    343 	}
    344 #endif
    345 
    346 	pnode = emalloc(sizeof(*pnode));
    347 	pnode->head = RESET;
    348 	pnode->in_progress = RESET;
    349 	pnode->fnode = fnode;
    350 	pnode->next = head->next;
    351 	pnode->last = head;
    352 	head->next = pnode;
    353 	if (pnode->next != NULL)
    354 		pnode->next->last = pnode;
    355 
    356 	f_pnode = emalloc(sizeof(*f_pnode));
    357 	f_pnode->pnode = pnode;
    358 	f_pnode->next = fnode->prov_list;
    359 	fnode->prov_list = f_pnode;
    360 }
    361 
    362 /*
    363  * put the BEFORE: lines to a list and handle them later.
    364  */
    365 void
    366 add_before(fnode, s)
    367 	filenode *fnode;
    368 	char *s;
    369 {
    370 	strnodelist *bf_ent;
    371 
    372 	bf_ent = emalloc(sizeof *bf_ent + strlen(s));
    373 	bf_ent->node = fnode;
    374 	strcpy(bf_ent->s, s);
    375 	bf_ent->next = bl_list;
    376 	bl_list = bf_ent;
    377 }
    378 
    379 /*
    380  * add a key to a filenode.
    381  */
    382 void
    383 add_keyword(fnode, s)
    384 	filenode *fnode;
    385 	char *s;
    386 {
    387 
    388 	strnode_add(&fnode->keyword_list, s, fnode);
    389 }
    390 
    391 /*
    392  * loop over the rest of a REQUIRE line, giving each word to
    393  * add_require() to do the real work.
    394  */
    395 void
    396 parse_require(node, buffer)
    397 	filenode *node;
    398 	char *buffer;
    399 {
    400 	char *s;
    401 
    402 	while ((s = strsep(&buffer, " \t\n")) != NULL)
    403 		if (*s != '\0')
    404 			add_require(node, s);
    405 }
    406 
    407 /*
    408  * loop over the rest of a PROVIDE line, giving each word to
    409  * add_provide() to do the real work.
    410  */
    411 void
    412 parse_provide(node, buffer)
    413 	filenode *node;
    414 	char *buffer;
    415 {
    416 	char *s;
    417 
    418 	while ((s = strsep(&buffer, " \t\n")) != NULL)
    419 		if (*s != '\0')
    420 			add_provide(node, s);
    421 }
    422 
    423 /*
    424  * loop over the rest of a BEFORE line, giving each word to
    425  * add_before() to do the real work.
    426  */
    427 void
    428 parse_before(node, buffer)
    429 	filenode *node;
    430 	char *buffer;
    431 {
    432 	char *s;
    433 
    434 	while ((s = strsep(&buffer, " \t\n")) != NULL)
    435 		if (*s != '\0')
    436 			add_before(node, s);
    437 }
    438 
    439 /*
    440  * loop over the rest of a KEYWORD line, giving each word to
    441  * add_keyword() to do the real work.
    442  */
    443 void
    444 parse_keywords(node, buffer)
    445 	filenode *node;
    446 	char *buffer;
    447 {
    448 	char *s;
    449 
    450 	while ((s = strsep(&buffer, " \t\n")) != NULL)
    451 		if (*s != '\0')
    452 			add_keyword(node, s);
    453 }
    454 
    455 /*
    456  * given a file name, create a filenode for it, read in lines looking
    457  * for provision and requirement lines, building the graphs as needed.
    458  */
    459 void
    460 crunch_file(filename)
    461 	char *filename;
    462 {
    463 	FILE *fp;
    464 	char *buf;
    465 	int require_flag, provide_flag, before_flag, keywords_flag;
    466 	enum { BEFORE_PARSING, PARSING, PARSING_DONE } state;
    467 	filenode *node;
    468 	char delims[3] = { '\\', '\\', '\0' };
    469 	struct stat st;
    470 
    471 	if ((fp = fopen(filename, "r")) == NULL) {
    472 		warn("could not open %s", filename);
    473 		return;
    474 	}
    475 
    476 	if (fstat(fileno(fp), &st) == -1) {
    477 		warn("could not stat %s", filename);
    478 		fclose(fp);
    479 		return;
    480 	}
    481 
    482 	if (!S_ISREG(st.st_mode)) {
    483 		warnx("%s is not a file", filename);
    484 		fclose(fp);
    485 		return;
    486 	}
    487 
    488 	node = filenode_new(filename);
    489 
    490 	/*
    491 	 * we don't care about length, line number, don't want # for comments,
    492 	 * and have no flags.
    493 	 */
    494 	for (state = BEFORE_PARSING; state != PARSING_DONE &&
    495 	    (buf = fparseln(fp, NULL, NULL, delims, 0)) != NULL; free(buf)) {
    496 		require_flag = provide_flag = before_flag = keywords_flag = 0;
    497 		if (strncmp(REQUIRE_STR, buf, REQUIRE_LEN) == 0)
    498 			require_flag = REQUIRE_LEN;
    499 		else if (strncmp(REQUIRES_STR, buf, REQUIRES_LEN) == 0)
    500 			require_flag = REQUIRES_LEN;
    501 		else if (strncmp(PROVIDE_STR, buf, PROVIDE_LEN) == 0)
    502 			provide_flag = PROVIDE_LEN;
    503 		else if (strncmp(PROVIDES_STR, buf, PROVIDES_LEN) == 0)
    504 			provide_flag = PROVIDES_LEN;
    505 		else if (strncmp(BEFORE_STR, buf, BEFORE_LEN) == 0)
    506 			before_flag = BEFORE_LEN;
    507 		else if (strncmp(KEYWORD_STR, buf, KEYWORD_LEN) == 0)
    508 			keywords_flag = KEYWORD_LEN;
    509 		else if (strncmp(KEYWORDS_STR, buf, KEYWORDS_LEN) == 0)
    510 			keywords_flag = KEYWORDS_LEN;
    511 		else {
    512 			if (state == PARSING)
    513 				state = PARSING_DONE;
    514 			continue;
    515 		}
    516 
    517 		state = PARSING;
    518 		if (require_flag)
    519 			parse_require(node, buf + require_flag);
    520 		else if (provide_flag)
    521 			parse_provide(node, buf + provide_flag);
    522 		else if (before_flag)
    523 			parse_before(node, buf + before_flag);
    524 		else if (keywords_flag)
    525 			parse_keywords(node, buf + keywords_flag);
    526 	}
    527 	fclose(fp);
    528 }
    529 
    530 Hash_Entry *
    531 make_fake_provision(node)
    532 	filenode *node;
    533 {
    534 	Hash_Entry *entry;
    535 	f_provnode *f_pnode;
    536 	provnode *head, *pnode;
    537 	static	int i = 0;
    538 	int	new;
    539 	char buffer[30];
    540 
    541 	do {
    542 		snprintf(buffer, sizeof buffer, "fake_prov_%08d", i++);
    543 		entry = Hash_CreateEntry(provide_hash, buffer, &new);
    544 	} while (new == 0);
    545 	head = emalloc(sizeof(*head));
    546 	head->head = SET;
    547 	head->in_progress = RESET;
    548 	head->fnode = NULL;
    549 	head->last = head->next = NULL;
    550 	Hash_SetValue(entry, head);
    551 
    552 	pnode = emalloc(sizeof(*pnode));
    553 	pnode->head = RESET;
    554 	pnode->in_progress = RESET;
    555 	pnode->fnode = node;
    556 	pnode->next = head->next;
    557 	pnode->last = head;
    558 	head->next = pnode;
    559 	if (pnode->next != NULL)
    560 		pnode->next->last = pnode;
    561 
    562 	f_pnode = emalloc(sizeof(*f_pnode));
    563 	f_pnode->pnode = pnode;
    564 	f_pnode->next = node->prov_list;
    565 	node->prov_list = f_pnode;
    566 
    567 	return (entry);
    568 }
    569 
    570 /*
    571  * go through the BEFORE list, inserting requirements into the graph(s)
    572  * as required.  in the before list, for each entry B, we have a file F
    573  * and a string S.  we create a "fake" provision (P) that F provides.
    574  * for each entry in the provision list for S, add a requirement to
    575  * that provisions filenode for P.
    576  */
    577 void
    578 insert_before()
    579 {
    580 	Hash_Entry *entry, *fake_prov_entry;
    581 	provnode *pnode;
    582 	f_reqnode *rnode;
    583 	strnodelist *bl;
    584 	int new;
    585 
    586 	while (bl_list != NULL) {
    587 		bl = bl_list->next;
    588 
    589 		fake_prov_entry = make_fake_provision(bl_list->node);
    590 
    591 		entry = Hash_CreateEntry(provide_hash, bl_list->s, &new);
    592 		if (new == 1)
    593 			warnx("file `%s' is before unknown provision `%s'", bl_list->node->filename, bl_list->s);
    594 
    595 		for (pnode = Hash_GetValue(entry); pnode; pnode = pnode->next) {
    596 			if (pnode->head)
    597 				continue;
    598 
    599 			rnode = emalloc(sizeof(*rnode));
    600 			rnode->entry = fake_prov_entry;
    601 			rnode->next = pnode->fnode->req_list;
    602 			pnode->fnode->req_list = rnode;
    603 		}
    604 
    605 		free(bl_list);
    606 		bl_list = bl;
    607 	}
    608 }
    609 
    610 /*
    611  * loop over all the files calling crunch_file() on them to do the
    612  * real work.  after we have built all the nodes, insert the BEFORE:
    613  * lines into graph(s).
    614  */
    615 void
    616 crunch_all_files()
    617 {
    618 	int i;
    619 
    620 	for (i = 0; i < file_count; i++)
    621 		crunch_file(file_list[i]);
    622 	insert_before();
    623 }
    624 
    625 /*
    626  * below are the functions that traverse the graphs we have built
    627  * finding out the desired ordering, printing each file in turn.
    628  * if missing requirements, or cyclic graphs are detected, a
    629  * warning will be issued, and we will continue on..
    630  */
    631 
    632 /*
    633  * given a requirement node (in a filename) we attempt to satisfy it.
    634  * we do some sanity checking first, to ensure that we have providers,
    635  * aren't already satisfied and aren't already being satisfied (ie,
    636  * cyclic).  if we pass all this, we loop over the provision list
    637  * calling do_file() (enter recursion) for each filenode in this
    638  * provision.
    639  */
    640 void
    641 satisfy_req(rnode, filename)
    642 	f_reqnode *rnode;
    643 	char *filename;
    644 {
    645 	Hash_Entry *entry;
    646 	provnode *head;
    647 
    648 	entry = rnode->entry;
    649 	head = Hash_GetValue(entry);
    650 
    651 	if (head == NULL) {
    652 		warnx("requirement `%s' in file `%s' has no providers.",
    653 		    Hash_GetKey(entry), filename);
    654 		exit_code = 1;
    655 		return;
    656 	}
    657 
    658 	/* return if the requirement is already satisfied. */
    659 	if (head->next == NULL)
    660 		return;
    661 
    662 	/*
    663 	 * if list is marked as in progress,
    664 	 *	print that there is a circular dependency on it and abort
    665 	 */
    666 	if (head->in_progress == SET) {
    667 		warnx("Circular dependency on provision `%s' in file `%s'.",
    668 		    Hash_GetKey(entry), filename);
    669 		exit_code = 1;
    670 		return;
    671 	}
    672 
    673 	head->in_progress = SET;
    674 
    675 	/*
    676 	 * while provision_list is not empty
    677 	 *	do_file(first_member_of(provision_list));
    678 	 */
    679 	while (head->next != NULL)
    680 		do_file(head->next->fnode);
    681 }
    682 
    683 int
    684 skip_ok(fnode)
    685 	filenode *fnode;
    686 {
    687 	strnodelist *s;
    688 	strnodelist *k;
    689 
    690 	for (s = skip_list; s; s = s->next)
    691 		for (k = fnode->keyword_list; k; k = k->next)
    692 			if (strcmp(k->s, s->s) == 0)
    693 				return (0);
    694 
    695 	return (1);
    696 }
    697 
    698 int
    699 keep_ok(fnode)
    700 	filenode *fnode;
    701 {
    702 	strnodelist *s;
    703 	strnodelist *k;
    704 
    705 	for (s = keep_list; s; s = s->next)
    706 		for (k = fnode->keyword_list; k; k = k->next)
    707 			if (strcmp(k->s, s->s) == 0)
    708 				return (1);
    709 
    710 	/* an empty keep_list means every one */
    711 	return (!keep_list);
    712 }
    713 
    714 /*
    715  * given a filenode, we ensure we are not a cyclic graph.  if this
    716  * is ok, we loop over the filenodes requirements, calling satisfy_req()
    717  * for each of them.. once we have done this, remove this filenode
    718  * from each provision table, as we are now done.
    719  */
    720 void
    721 do_file(fnode)
    722 	filenode *fnode;
    723 {
    724 	f_reqnode *r, *r_tmp;
    725 	f_provnode *p, *p_tmp;
    726 	provnode *pnode;
    727 	int was_set;
    728 
    729 	DPRINTF((stderr, "do_file on %s.\n", fnode->filename));
    730 
    731 	/*
    732 	 * if fnode is marked as in progress,
    733 	 *	 print that fnode; is circularly depended upon and abort.
    734 	 */
    735 	if (fnode->in_progress == SET) {
    736 		warnx("Circular dependency on file `%s'.",
    737 			fnode->filename);
    738 		was_set = exit_code = 1;
    739 	} else
    740 		was_set = 0;
    741 
    742 	/* mark fnode */
    743 	fnode->in_progress = SET;
    744 
    745 	/*
    746 	 * for each requirement of fnode -> r
    747 	 *	satisfy_req(r, filename)
    748 	 */
    749 	r = fnode->req_list;
    750 	while (r != NULL) {
    751 		r_tmp = r;
    752 		satisfy_req(r, fnode->filename);
    753 		r = r->next;
    754 		free(r_tmp);
    755 	}
    756 	fnode->req_list = NULL;
    757 
    758 	/*
    759 	 * for each provision of fnode -> p
    760 	 *	remove fnode from provision list for p in hash table
    761 	 */
    762 	p = fnode->prov_list;
    763 	while (p != NULL) {
    764 		p_tmp = p;
    765 		pnode = p->pnode;
    766 		if (pnode->next != NULL) {
    767 			pnode->next->last = pnode->last;
    768 		}
    769 		if (pnode->last != NULL) {
    770 			pnode->last->next = pnode->next;
    771 		}
    772 		free(pnode);
    773 		p = p->next;
    774 		free(p_tmp);
    775 	}
    776 	fnode->prov_list = NULL;
    777 
    778 	/* do_it(fnode) */
    779 	DPRINTF((stderr, "next do: "));
    780 
    781 	/* if we were already in progress, don't print again */
    782 	if (was_set == 0 && skip_ok(fnode) && keep_ok(fnode))
    783 		printf("%s\n", fnode->filename);
    784 
    785 	if (fnode->next != NULL) {
    786 		fnode->next->last = fnode->last;
    787 	}
    788 	if (fnode->last != NULL) {
    789 		fnode->last->next = fnode->next;
    790 	}
    791 
    792 	DPRINTF((stderr, "nuking %s\n", fnode->filename));
    793 	free(fnode->filename);
    794 	free(fnode);
    795 }
    796 
    797 void
    798 generate_ordering()
    799 {
    800 
    801 	/*
    802 	 * while there remain undone files{f},
    803 	 *	pick an arbitrary f, and do_file(f)
    804 	 * Note that the first file in the file list is perfectly
    805 	 * arbitrary, and easy to find, so we use that.
    806 	 */
    807 
    808 	/*
    809 	 * N.B.: the file nodes "self delete" after they execute, so
    810 	 * after each iteration of the loop, the head will be pointing
    811 	 * to something totally different. The loop ends up being
    812 	 * executed only once for every strongly connected set of
    813 	 * nodes.
    814 	 */
    815 	while (fn_head->next != NULL) {
    816 		DPRINTF((stderr, "generate on %s\n", fn_head->next->filename));
    817 		do_file(fn_head->next);
    818 	}
    819 }
    820