Home | History | Annotate | Line # | Download | only in rcorder
rcorder.c revision 1.5
      1 /*	$NetBSD: rcorder.c,v 1.5 2000/07/17 14:16:22 mrg 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 	char		*s;
    111 	strnodelist	*next;
    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);
    217 	ent->node = fnode;
    218 	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);
    373 	bf_ent->node = fnode;
    374 	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 	    directive_flag;
    467 	filenode *node;
    468 	char delims[3] = { '\\', '\\', '\0' };
    469 	struct stat st;
    470 
    471 	directive_flag = 0;
    472 
    473 	if ((fp = fopen(filename, "r")) == NULL) {
    474 		warn("could not open %s", filename);
    475 		return;
    476 	}
    477 
    478 	if (fstat(fileno(fp), &st) == -1) {
    479 		warn("could not stat %s", filename);
    480 		fclose(fp);
    481 		return;
    482 	}
    483 
    484 	if (!S_ISREG(st.st_mode)) {
    485 		warnx("%s is not a file", filename);
    486 		fclose(fp);
    487 		return;
    488 	}
    489 
    490 	node = filenode_new(filename);
    491 
    492 	/*
    493 	 * we don't care about length, line number, don't want # for comments,
    494 	 * and have no flags.
    495 	 */
    496 	while ((buf = fparseln(fp, NULL, NULL, delims, 0))) {
    497 		require_flag = provide_flag = before_flag = keywords_flag = 0;
    498 		if (strncmp(REQUIRE_STR, buf, REQUIRE_LEN) == 0)
    499 			require_flag = REQUIRE_LEN;
    500 		else if (strncmp(REQUIRES_STR, buf, REQUIRES_LEN) == 0)
    501 			require_flag = REQUIRES_LEN;
    502 		else if (strncmp(PROVIDE_STR, buf, PROVIDE_LEN) == 0)
    503 			provide_flag = PROVIDE_LEN;
    504 		else if (strncmp(PROVIDES_STR, buf, PROVIDES_LEN) == 0)
    505 			provide_flag = PROVIDES_LEN;
    506 		else if (strncmp(BEFORE_STR, buf, BEFORE_LEN) == 0)
    507 			before_flag = BEFORE_LEN;
    508 		else if (strncmp(KEYWORD_STR, buf, KEYWORD_LEN) == 0)
    509 			keywords_flag = KEYWORD_LEN;
    510 		else if (strncmp(KEYWORDS_STR, buf, KEYWORDS_LEN) == 0)
    511 			keywords_flag = KEYWORDS_LEN;
    512 
    513 		if (require_flag)
    514 			parse_require(node, buf + require_flag);
    515 
    516 		if (provide_flag)
    517 			parse_provide(node, buf + provide_flag);
    518 
    519 		if (before_flag)
    520 			parse_before(node, buf + before_flag);
    521 
    522 		if (keywords_flag)
    523 			parse_keywords(node, buf + keywords_flag);
    524 	}
    525 	fclose(fp);
    526 }
    527 
    528 Hash_Entry *
    529 make_fake_provision(node)
    530 	filenode *node;
    531 {
    532 	Hash_Entry *entry;
    533 	f_provnode *f_pnode;
    534 	provnode *head, *pnode;
    535 	static	int i = 0;
    536 	int	new;
    537 	char buffer[30];
    538 
    539 	do {
    540 		snprintf(buffer, sizeof buffer, "fake_prov_%08d", i++);
    541 		entry = Hash_CreateEntry(provide_hash, buffer, &new);
    542 	} while (new == 0);
    543 	head = emalloc(sizeof(*head));
    544 	head->head = SET;
    545 	head->in_progress = RESET;
    546 	head->fnode = NULL;
    547 	head->last = head->next = NULL;
    548 	Hash_SetValue(entry, head);
    549 
    550 	pnode = emalloc(sizeof(*pnode));
    551 	pnode->head = RESET;
    552 	pnode->in_progress = RESET;
    553 	pnode->fnode = node;
    554 	pnode->next = head->next;
    555 	pnode->last = head;
    556 	head->next = pnode;
    557 	if (pnode->next != NULL)
    558 		pnode->next->last = pnode;
    559 
    560 	f_pnode = emalloc(sizeof(*f_pnode));
    561 	f_pnode->pnode = pnode;
    562 	f_pnode->next = node->prov_list;
    563 	node->prov_list = f_pnode;
    564 
    565 	return (entry);
    566 }
    567 
    568 /*
    569  * go through the BEFORE list, inserting requirements into the graph(s)
    570  * as required.  in the before list, for each entry B, we have a file F
    571  * and a string S.  we create a "fake" provision (P) that F provides.
    572  * for each entry in the provision list for S, add a requirement to
    573  * that provisions filenode for P.
    574  */
    575 void
    576 insert_before()
    577 {
    578 	Hash_Entry *entry, *fake_prov_entry;
    579 	provnode *pnode;
    580 	f_reqnode *rnode;
    581 	strnodelist *bl;
    582 	int new;
    583 
    584 	while (bl_list != NULL) {
    585 		bl = bl_list->next;
    586 
    587 		fake_prov_entry = make_fake_provision(bl_list->node);
    588 
    589 		entry = Hash_CreateEntry(provide_hash, bl_list->s, &new);
    590 		if (new == 1)
    591 			warnx("file `%s' is before unknown provision `%s'", bl_list->node->filename, bl_list->s);
    592 
    593 		for (pnode = Hash_GetValue(entry); pnode; pnode = pnode->next) {
    594 			if (pnode->head)
    595 				continue;
    596 
    597 			rnode = emalloc(sizeof(*rnode));
    598 			rnode->entry = fake_prov_entry;
    599 			rnode->next = pnode->fnode->req_list;
    600 			pnode->fnode->req_list = rnode;
    601 		}
    602 
    603 		free(bl_list);
    604 		bl_list = bl;
    605 	}
    606 }
    607 
    608 /*
    609  * loop over all the files calling crunch_file() on them to do the
    610  * real work.  after we have built all the nodes, insert the BEFORE:
    611  * lines into graph(s).
    612  */
    613 void
    614 crunch_all_files()
    615 {
    616 	int i;
    617 
    618 	for (i = 0; i < file_count; i++)
    619 		crunch_file(file_list[i]);
    620 	insert_before();
    621 }
    622 
    623 /*
    624  * below are the functions that traverse the graphs we have built
    625  * finding out the desired ordering, printing each file in turn.
    626  * if missing requirements, or cyclic graphs are detected, a
    627  * warning will be issued, and we will continue on..
    628  */
    629 
    630 /*
    631  * given a requirement node (in a filename) we attempt to satisfy it.
    632  * we do some sanity checking first, to ensure that we have providers,
    633  * aren't already satisfied and aren't already being satisfied (ie,
    634  * cyclic).  if we pass all this, we loop over the provision list
    635  * calling do_file() (enter recursion) for each filenode in this
    636  * provision.
    637  */
    638 void
    639 satisfy_req(rnode, filename)
    640 	f_reqnode *rnode;
    641 	char *filename;
    642 {
    643 	Hash_Entry *entry;
    644 	provnode *head;
    645 
    646 	entry = rnode->entry;
    647 	head = Hash_GetValue(entry);
    648 
    649 	if (head == NULL) {
    650 		warnx("requirement `%s' in file `%s' has no providers.",
    651 		    Hash_GetKey(entry), filename);
    652 		exit_code = 1;
    653 		return;
    654 	}
    655 
    656 	/* return if the requirement is already satisfied. */
    657 	if (head->next == NULL)
    658 		return;
    659 
    660 	/*
    661 	 * if list is marked as in progress,
    662 	 *	print that there is a circular dependency on it and abort
    663 	 */
    664 	if (head->in_progress == SET) {
    665 		warnx("Circular dependency on provision `%s' in file `%s'.",
    666 		    Hash_GetKey(entry), filename);
    667 		exit_code = 1;
    668 		return;
    669 	}
    670 
    671 	head->in_progress = SET;
    672 
    673 	/*
    674 	 * while provision_list is not empty
    675 	 *	do_file(first_member_of(provision_list));
    676 	 */
    677 	while (head->next != NULL)
    678 		do_file(head->next->fnode);
    679 }
    680 
    681 int
    682 skip_ok(fnode)
    683 	filenode *fnode;
    684 {
    685 	strnodelist *s;
    686 	strnodelist *k;
    687 
    688 	for (s = skip_list; s; s = s->next)
    689 		for (k = fnode->keyword_list; k; k = k->next)
    690 			if (strcmp(k->s, s->s) == 0)
    691 				return (0);
    692 
    693 	return (1);
    694 }
    695 
    696 int
    697 keep_ok(fnode)
    698 	filenode *fnode;
    699 {
    700 	strnodelist *s;
    701 	strnodelist *k;
    702 
    703 	for (s = keep_list; s; s = s->next)
    704 		for (k = fnode->keyword_list; k; k = k->next)
    705 			if (strcmp(k->s, s->s) == 0)
    706 				return (1);
    707 
    708 	/* an empty keep_list means every one */
    709 	return (!keep_list);
    710 }
    711 
    712 /*
    713  * given a filenode, we ensure we are not a cyclic graph.  if this
    714  * is ok, we loop over the filenodes requirements, calling satisfy_req()
    715  * for each of them.. once we have done this, remove this filenode
    716  * from each provision table, as we are now done.
    717  */
    718 void
    719 do_file(fnode)
    720 	filenode *fnode;
    721 {
    722 	f_reqnode *r, *r_tmp;
    723 	f_provnode *p, *p_tmp;
    724 	provnode *pnode;
    725 	int was_set;
    726 
    727 	DPRINTF((stderr, "do_file on %s.\n", fnode->filename));
    728 
    729 	/*
    730 	 * if fnode is marked as in progress,
    731 	 *	 print that fnode; is circularly depended upon and abort.
    732 	 */
    733 	if (fnode->in_progress == SET) {
    734 		warnx("Circular dependency on file `%s'.",
    735 			fnode->filename);
    736 		was_set = exit_code = 1;
    737 	} else
    738 		was_set = 0;
    739 
    740 	/* mark fnode */
    741 	fnode->in_progress = SET;
    742 
    743 	/*
    744 	 * for each requirement of fnode -> r
    745 	 *	satisfy_req(r, filename)
    746 	 */
    747 	r = fnode->req_list;
    748 	while (r != NULL) {
    749 		r_tmp = r;
    750 		satisfy_req(r, fnode->filename);
    751 		r = r->next;
    752 		free(r_tmp);
    753 	}
    754 	fnode->req_list = NULL;
    755 
    756 	/*
    757 	 * for each provision of fnode -> p
    758 	 *	remove fnode from provision list for p in hash table
    759 	 */
    760 	p = fnode->prov_list;
    761 	while (p != NULL) {
    762 		p_tmp = p;
    763 		pnode = p->pnode;
    764 		if (pnode->next != NULL) {
    765 			pnode->next->last = pnode->last;
    766 		}
    767 		if (pnode->last != NULL) {
    768 			pnode->last->next = pnode->next;
    769 		}
    770 		free(pnode);
    771 		p = p->next;
    772 		free(p_tmp);
    773 	}
    774 	fnode->prov_list = NULL;
    775 
    776 	/* do_it(fnode) */
    777 	DPRINTF((stderr, "next do: "));
    778 
    779 	/* if we were already in progress, don't print again */
    780 	if (was_set == 0 && skip_ok(fnode) && keep_ok(fnode))
    781 		printf("%s\n", fnode->filename);
    782 
    783 	if (fnode->next != NULL) {
    784 		fnode->next->last = fnode->last;
    785 	}
    786 	if (fnode->last != NULL) {
    787 		fnode->last->next = fnode->next;
    788 	}
    789 
    790 	DPRINTF((stderr, "nuking %s\n", fnode->filename));
    791 	free(fnode->filename);
    792 	free(fnode);
    793 }
    794 
    795 void
    796 generate_ordering()
    797 {
    798 
    799 	/*
    800 	 * while there remain undone files{f},
    801 	 *	pick an arbitrary f, and do_file(f)
    802 	 * Note that the first file in the file list is perfectly
    803 	 * arbitrary, and easy to find, so we use that.
    804 	 */
    805 
    806 	/*
    807 	 * N.B.: the file nodes "self delete" after they execute, so
    808 	 * after each iteration of the loop, the head will be pointing
    809 	 * to something totally different. The loop ends up being
    810 	 * executed only once for every strongly connected set of
    811 	 * nodes.
    812 	 */
    813 	while (fn_head->next != NULL) {
    814 		DPRINTF((stderr, "generate on %s\n", fn_head->next->filename));
    815 		do_file(fn_head->next);
    816 	}
    817 }
    818