2 * Copyright (c) 1998, 1999 Matthew R. Green
5 * Perry E. Metzger. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the NetBSD Project
18 * by Perry E. Metzger.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * $NetBSD: rcorder.c,v 1.7 2000/08/04 07:33:55 enami Exp $
36 #include <sys/types.h>
52 # define DPRINTF(args) if (debug) { fflush(stdout); fprintf args; }
54 # define DPRINTF(args)
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)
73 static int file_count;
74 static char **file_list;
80 static Hash_Table provide_hash_s, *provide_hash;
82 typedef struct provnode provnode;
83 typedef struct filenode filenode;
84 typedef struct f_provnode f_provnode;
85 typedef struct f_reqnode f_reqnode;
86 typedef struct strnodelist strnodelist;
92 provnode *next, *last;
115 filenode *next, *last;
117 f_provnode *prov_list;
118 strnodelist *keyword_list;
121 static filenode fn_head_s, *fn_head;
123 static strnodelist *bl_list;
124 static strnodelist *keep_list;
125 static strnodelist *skip_list;
126 static strnodelist *onetime_list;
130 static void do_file(filenode *fnode);
131 static void strnode_add(strnodelist **, char *, filenode *);
132 static int skip_ok(filenode *fnode);
133 static int keep_ok(filenode *fnode);
134 static void satisfy_req(f_reqnode *rnode, char *filename);
135 static void crunch_file(char *);
136 static void parse_require(filenode *, char *);
137 static void parse_provide(filenode *, char *);
138 static void parse_before(filenode *, char *);
139 static void parse_keywords(filenode *, char *);
140 static filenode *filenode_new(char *);
141 static void add_require(filenode *, char *);
142 static void add_provide(filenode *, char *);
143 static void add_before(filenode *, char *);
144 static void add_keyword(filenode *, char *);
145 static void insert_before(void);
146 static Hash_Entry *make_fake_provision(filenode *);
147 static void crunch_all_files(void);
148 static void initialize(void);
149 static void generate_ordering(void);
152 main(int argc, char **argv)
156 while ((ch = getopt(argc, argv, "dpk:s:o:")) != -1)
162 warnx("debugging not compiled in, -d ignored");
166 strnode_add(&keep_list, optarg, 0);
169 strnode_add(&skip_list, optarg, 0);
172 strnode_add(&onetime_list, optarg, 0);
178 /* XXX should crunch it? */
187 DPRINTF((stderr, "parse_args\n"));
189 DPRINTF((stderr, "initialize\n"));
191 DPRINTF((stderr, "crunch_all_files\n"));
193 DPRINTF((stderr, "generate_ordering\n"));
199 * initialise various variables.
205 fn_head = &fn_head_s;
207 provide_hash = &provide_hash_s;
208 Hash_InitTable(provide_hash, file_count);
211 /* generic function to insert a new strnodelist element */
213 strnode_add(strnodelist **listp, char *s, filenode *fnode)
217 ent = emalloc(sizeof *ent + strlen(s));
225 * below are the functions that deal with creating the lists
226 * from the filename's given dependencies and provisions
227 * in each of these files. no ordering or checking is done here.
231 * we have a new filename, create a new filenode structure.
232 * fill in the bits, and put it in the filenode linked list
235 filenode_new(char *filename)
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;
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.
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;
260 * add a requirement to a filenode.
263 add_require(filenode *fnode, char *s)
269 entry = Hash_CreateEntry(provide_hash, s, &new);
271 Hash_SetValue(entry, NULL);
272 rnode = emalloc(sizeof(*rnode));
273 rnode->entry = entry;
274 rnode->next = fnode->req_list;
275 fnode->req_list = rnode;
279 * add a provision to a filenode. if this provision doesn't
280 * have a head node, create one here.
283 add_provide(filenode *fnode, char *s)
287 provnode *pnode, *head;
290 entry = Hash_CreateEntry(provide_hash, s, &new);
291 head = Hash_GetValue(entry);
293 /* create a head node if necessary. */
295 head = emalloc(sizeof(*head));
297 head->in_progress = RESET;
299 head->last = head->next = NULL;
300 Hash_SetValue(entry, head);
304 * Don't warn about this. We want to be able to support
305 * scripts that do two complex things:
307 * - Two independent scripts which both provide the
308 * same thing. Both scripts must be executed in
309 * any order to meet the barrier. An example:
321 * - Two interdependent scripts which both provide the
322 * same thing. Both scripts must be executed in
323 * graph order to meet the barrier. An example:
327 * PROVIDE: nameservice dnscache
332 * PROVIDE: nameservice nscd
336 warnx("file `%s' provides `%s'.", fnode->filename, s);
337 warnx("\tpreviously seen in `%s'.",
338 head->next->fnode->filename);
342 pnode = emalloc(sizeof(*pnode));
344 pnode->in_progress = RESET;
345 pnode->fnode = fnode;
346 pnode->next = head->next;
348 pnode->name = strdup(s);
350 if (pnode->next != NULL)
351 pnode->next->last = pnode;
353 f_pnode = emalloc(sizeof(*f_pnode));
354 f_pnode->pnode = pnode;
355 f_pnode->next = fnode->prov_list;
356 fnode->prov_list = f_pnode;
360 * put the BEFORE: lines to a list and handle them later.
363 add_before(filenode *fnode, char *s)
367 bf_ent = emalloc(sizeof *bf_ent + strlen(s));
368 bf_ent->node = fnode;
369 strcpy(bf_ent->s, s);
370 bf_ent->next = bl_list;
375 * add a key to a filenode.
378 add_keyword(filenode *fnode, char *s)
381 strnode_add(&fnode->keyword_list, s, fnode);
385 * loop over the rest of a REQUIRE line, giving each word to
386 * add_require() to do the real work.
389 parse_require(filenode *node, char *buffer)
393 while ((s = strsep(&buffer, " \t\n")) != NULL)
395 add_require(node, s);
399 * loop over the rest of a PROVIDE line, giving each word to
400 * add_provide() to do the real work.
403 parse_provide(filenode *node, char *buffer)
407 while ((s = strsep(&buffer, " \t\n")) != NULL)
409 add_provide(node, s);
413 * loop over the rest of a BEFORE line, giving each word to
414 * add_before() to do the real work.
417 parse_before(filenode *node, char *buffer)
421 while ((s = strsep(&buffer, " \t\n")) != NULL)
427 * loop over the rest of a KEYWORD line, giving each word to
428 * add_keyword() to do the real work.
431 parse_keywords(filenode *node, char *buffer)
435 while ((s = strsep(&buffer, " \t\n")) != NULL)
437 add_keyword(node, s);
441 * given a file name, create a filenode for it, read in lines looking
442 * for provision and requirement lines, building the graphs as needed.
445 crunch_file(char *filename)
449 int require_flag, provide_flag, before_flag, keywords_flag;
450 enum { BEFORE_PARSING, PARSING, PARSING_DONE } state;
452 char delims[3] = { '\\', '\\', '\0' };
455 if ((fp = fopen(filename, "r")) == NULL) {
456 warn("could not open %s", filename);
460 if (fstat(fileno(fp), &st) == -1) {
461 warn("could not stat %s", filename);
466 if (!S_ISREG(st.st_mode)) {
468 warnx("%s is not a file", filename);
474 node = filenode_new(filename);
477 * we don't care about length, line number, don't want # for comments,
480 for (state = BEFORE_PARSING; state != PARSING_DONE &&
481 (buf = fparseln(fp, NULL, NULL, delims, 0)) != NULL; free(buf)) {
482 require_flag = provide_flag = before_flag = keywords_flag = 0;
483 if (strncmp(REQUIRE_STR, buf, REQUIRE_LEN) == 0)
484 require_flag = REQUIRE_LEN;
485 else if (strncmp(REQUIRES_STR, buf, REQUIRES_LEN) == 0)
486 require_flag = REQUIRES_LEN;
487 else if (strncmp(PROVIDE_STR, buf, PROVIDE_LEN) == 0)
488 provide_flag = PROVIDE_LEN;
489 else if (strncmp(PROVIDES_STR, buf, PROVIDES_LEN) == 0)
490 provide_flag = PROVIDES_LEN;
491 else if (strncmp(BEFORE_STR, buf, BEFORE_LEN) == 0)
492 before_flag = BEFORE_LEN;
493 else if (strncmp(KEYWORD_STR, buf, KEYWORD_LEN) == 0)
494 keywords_flag = KEYWORD_LEN;
495 else if (strncmp(KEYWORDS_STR, buf, KEYWORDS_LEN) == 0)
496 keywords_flag = KEYWORDS_LEN;
498 if (state == PARSING)
499 state = PARSING_DONE;
505 parse_require(node, buf + require_flag);
506 else if (provide_flag)
507 parse_provide(node, buf + provide_flag);
508 else if (before_flag)
509 parse_before(node, buf + before_flag);
510 else if (keywords_flag)
511 parse_keywords(node, buf + keywords_flag);
517 make_fake_provision(filenode *node)
521 provnode *head, *pnode;
527 snprintf(buffer, sizeof buffer, "fake_prov_%08d", i++);
528 entry = Hash_CreateEntry(provide_hash, buffer, &new);
530 head = emalloc(sizeof(*head));
532 head->in_progress = RESET;
534 head->last = head->next = NULL;
535 Hash_SetValue(entry, head);
537 pnode = emalloc(sizeof(*pnode));
539 pnode->in_progress = RESET;
541 pnode->next = head->next;
543 pnode->name = strdup(buffer);
545 if (pnode->next != NULL)
546 pnode->next->last = pnode;
548 f_pnode = emalloc(sizeof(*f_pnode));
549 f_pnode->pnode = pnode;
550 f_pnode->next = node->prov_list;
551 node->prov_list = f_pnode;
557 * go through the BEFORE list, inserting requirements into the graph(s)
558 * as required. in the before list, for each entry B, we have a file F
559 * and a string S. we create a "fake" provision (P) that F provides.
560 * for each entry in the provision list for S, add a requirement to
561 * that provisions filenode for P.
566 Hash_Entry *entry, *fake_prov_entry;
572 while (bl_list != NULL) {
575 fake_prov_entry = make_fake_provision(bl_list->node);
577 entry = Hash_CreateEntry(provide_hash, bl_list->s, &new);
578 if (new == 1 && !provide)
579 warnx("file `%s' is before unknown provision `%s'",
580 bl_list->node->filename, bl_list->s);
582 for (pnode = Hash_GetValue(entry); pnode; pnode = pnode->next) {
586 rnode = emalloc(sizeof(*rnode));
587 rnode->entry = fake_prov_entry;
588 rnode->next = pnode->fnode->req_list;
589 pnode->fnode->req_list = rnode;
598 * loop over all the files calling crunch_file() on them to do the
599 * real work. after we have built all the nodes, insert the BEFORE:
600 * lines into graph(s).
603 crunch_all_files(void)
607 for (i = 0; i < file_count; i++)
608 crunch_file(file_list[i]);
613 * below are the functions that traverse the graphs we have built
614 * finding out the desired ordering, printing each file in turn.
615 * if missing requirements, or cyclic graphs are detected, a
616 * warning will be issued, and we will continue on..
620 * given a requirement node (in a filename) we attempt to satisfy it.
621 * we do some sanity checking first, to ensure that we have providers,
622 * aren't already satisfied and aren't already being satisfied (ie,
623 * cyclic). if we pass all this, we loop over the provision list
624 * calling do_file() (enter recursion) for each filenode in this
628 satisfy_req(f_reqnode *rnode, char *filename)
633 entry = rnode->entry;
634 head = Hash_GetValue(entry);
637 warnx("requirement `%s' in file `%s' has no providers.",
638 Hash_GetKey(entry), filename);
643 /* return if the requirement is already satisfied. */
644 if (head->next == NULL)
648 * if list is marked as in progress,
649 * print that there is a circular dependency on it and abort
651 if (head->in_progress == SET) {
652 warnx("Circular dependency on provision `%s' in file `%s'.",
653 Hash_GetKey(entry), filename);
658 head->in_progress = SET;
661 * while provision_list is not empty
662 * do_file(first_member_of(provision_list));
664 while (head->next != NULL)
665 do_file(head->next->fnode);
669 skip_ok(filenode *fnode)
674 for (s = skip_list; s; s = s->next)
675 for (k = fnode->keyword_list; k; k = k->next)
676 if (strcmp(k->s, s->s) == 0)
683 keep_ok(filenode *fnode)
688 for (s = keep_list; s; s = s->next)
689 for (k = fnode->keyword_list; k; k = k->next)
690 if (strcmp(k->s, s->s) == 0)
693 /* an empty keep_list means every one */
698 * given a filenode, we ensure we are not a cyclic graph. if this
699 * is ok, we loop over the filenodes requirements, calling satisfy_req()
700 * for each of them.. once we have done this, remove this filenode
701 * from each provision table, as we are now done.
703 * NOTE: do_file() is called recursively from several places and cannot
704 * safely free() anything related to items that may be recursed on.
705 * Circular dependencies will cause problems if we do.
708 do_file(filenode *fnode)
711 f_provnode *p, *p_tmp;
715 DPRINTF((stderr, "do_file on %s.\n", fnode->filename));
718 * if fnode is marked as in progress,
719 * print that fnode; is circularly depended upon and abort.
721 if (fnode->in_progress == SET) {
722 warnx("Circular dependency on file `%s'.",
724 was_set = exit_code = 1;
729 fnode->in_progress = SET;
732 * for each requirement of fnode -> r
733 * satisfy_req(r, filename)
737 satisfy_req(r, fnode->filename);
740 fnode->req_list = NULL;
743 * for each provision of fnode -> p
744 * remove fnode from provision list for p in hash table
746 p = fnode->prov_list;
750 if (pnode->next != NULL) {
751 pnode->next->last = pnode->last;
753 if (pnode->last != NULL) {
754 pnode->last->next = pnode->next;
760 fnode->prov_list = NULL;
763 DPRINTF((stderr, "next do: "));
765 /* if we were already in progress, don't print again */
766 if (was_set == 0 && skip_ok(fnode) && keep_ok(fnode))
767 printf("%s\n", fnode->filename);
769 if (fnode->next != NULL) {
770 fnode->next->last = fnode->last;
772 if (fnode->last != NULL) {
773 fnode->last->next = fnode->next;
776 DPRINTF((stderr, "nuking %s\n", fnode->filename));
780 generate_ordering(void)
784 * while there remain undone files{f},
785 * pick an arbitrary f, and do_file(f)
786 * Note that the first file in the file list is perfectly
787 * arbitrary, and easy to find, so we use that.
791 * N.B.: the file nodes "self delete" after they execute, so
792 * after each iteration of the loop, the head will be pointing
793 * to something totally different. The loop ends up being
794 * executed only once for every strongly connected set of
799 * List all keywords provided by the listed files
804 for (file = fn_head->next; file; file = file->next) {
805 for (f_prov = file->prov_list; f_prov; f_prov = f_prov->next) {
806 if (strncmp(f_prov->pnode->name, "fake_", 5) != 0)
807 printf("%s\n", f_prov->pnode->name);
810 } else if (onetime_list) {
812 * Only list dependanacies required to start particular
819 for (scan = onetime_list; scan; scan = scan->next) {
820 for (file = fn_head->next; file; file = file->next) {
821 for (f_prov = file->prov_list; f_prov; f_prov = f_prov->next) {
822 if (strcmp(scan->s, f_prov->pnode->name) == 0) {
832 while (fn_head->next != NULL) {
833 DPRINTF((stderr, "generate on %s\n",
834 fn_head->next->filename));
835 do_file(fn_head->next);