2 /* $NetBSD: rcorder.c,v 1.7 2000/08/04 07:33:55 enami Exp $ */
6 * Copyright (c) 1998, 1999 Matthew R. Green
9 * Perry E. Metzger. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed for the NetBSD Project
22 * by Perry E. Metzger.
23 * 4. The name of the author may not be used to endorse or promote products
24 * derived from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/types.h>
39 __FBSDID("$FreeBSD$");
56 # define DPRINTF(args) if (debug) { fflush(stdout); fprintf args; }
58 # define DPRINTF(args)
61 #define REQUIRE_STR "# REQUIRE:"
62 #define REQUIRE_LEN (sizeof(REQUIRE_STR) - 1)
63 #define REQUIRES_STR "# REQUIRES:"
64 #define REQUIRES_LEN (sizeof(REQUIRES_STR) - 1)
65 #define PROVIDE_STR "# PROVIDE:"
66 #define PROVIDE_LEN (sizeof(PROVIDE_STR) - 1)
67 #define PROVIDES_STR "# PROVIDES:"
68 #define PROVIDES_LEN (sizeof(PROVIDES_STR) - 1)
69 #define BEFORE_STR "# BEFORE:"
70 #define BEFORE_LEN (sizeof(BEFORE_STR) - 1)
71 #define KEYWORD_STR "# KEYWORD:"
72 #define KEYWORD_LEN (sizeof(KEYWORD_STR) - 1)
73 #define KEYWORDS_STR "# KEYWORDS:"
74 #define KEYWORDS_LEN (sizeof(KEYWORDS_STR) - 1)
87 Hash_Table provide_hash_s, *provide_hash;
89 typedef struct provnode provnode;
90 typedef struct filenode filenode;
91 typedef struct f_provnode f_provnode;
92 typedef struct f_reqnode f_reqnode;
93 typedef struct strnodelist strnodelist;
99 provnode *next, *last;
121 filenode *next, *last;
123 f_provnode *prov_list;
124 strnodelist *keyword_list;
127 filenode fn_head_s, *fn_head;
129 strnodelist *bl_list;
130 strnodelist *keep_list;
131 strnodelist *skip_list;
133 void do_file __P((filenode *fnode));
134 void strnode_add __P((strnodelist **, char *, filenode *));
135 int skip_ok __P((filenode *fnode));
136 int keep_ok __P((filenode *fnode));
137 void satisfy_req __P((f_reqnode *rnode, char *filename));
138 void crunch_file __P((char *));
139 void parse_require __P((filenode *, char *));
140 void parse_provide __P((filenode *, char *));
141 void parse_before __P((filenode *, char *));
142 void parse_keywords __P((filenode *, char *));
143 filenode *filenode_new __P((char *));
144 void add_require __P((filenode *, char *));
145 void add_provide __P((filenode *, char *));
146 void add_before __P((filenode *, char *));
147 void add_keyword __P((filenode *, char *));
148 void insert_before __P((void));
149 Hash_Entry *make_fake_provision __P((filenode *));
150 void crunch_all_files __P((void));
151 void initialize __P((void));
152 void generate_ordering __P((void));
153 int main __P((int, char *[]));
162 while ((ch = getopt(argc, argv, "dk:s:")) != -1)
168 warnx("debugging not compiled in, -d ignored");
172 strnode_add(&keep_list, optarg, 0);
175 strnode_add(&skip_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(listp, s, fnode)
220 ent = emalloc(sizeof *ent + strlen(s));
228 * below are the functions that deal with creating the lists
229 * from the filename's given and the dependancies and provisions
230 * in each of these files. no ordering or checking is done here.
234 * we have a new filename, create a new filenode structure.
235 * fill in the bits, and put it in the filenode linked list
238 filenode_new(filename)
243 temp = emalloc(sizeof(*temp));
244 memset(temp, 0, sizeof(*temp));
245 temp->filename = estrdup(filename);
246 temp->req_list = NULL;
247 temp->prov_list = NULL;
248 temp->keyword_list = NULL;
249 temp->in_progress = RESET;
251 * link the filenode into the list of filenodes.
252 * note that the double linking means we can delete a
253 * filenode without searching for where it belongs.
255 temp->next = fn_head->next;
256 if (temp->next != NULL)
257 temp->next->last = temp;
258 temp->last = fn_head;
259 fn_head->next = temp;
264 * add a requirement to a filenode.
267 add_require(fnode, s)
275 entry = Hash_CreateEntry(provide_hash, s, &new);
277 Hash_SetValue(entry, NULL);
278 rnode = emalloc(sizeof(*rnode));
279 rnode->entry = entry;
280 rnode->next = fnode->req_list;
281 fnode->req_list = rnode;
285 * add a provision to a filenode. if this provision doesn't
286 * have a head node, create one here.
289 add_provide(fnode, s)
295 provnode *pnode, *head;
298 entry = Hash_CreateEntry(provide_hash, s, &new);
299 head = Hash_GetValue(entry);
301 /* create a head node if necessary. */
303 head = emalloc(sizeof(*head));
305 head->in_progress = RESET;
307 head->last = head->next = NULL;
308 Hash_SetValue(entry, head);
312 * Don't warn about this. We want to be able to support
313 * scripts that do two complex things:
315 * - Two independent scripts which both provide the
316 * same thing. Both scripts must be executed in
317 * any order to meet the barrier. An example:
329 * - Two interdependent scripts which both provide the
330 * same thing. Both scripts must be executed in
331 * graph order to meet the barrier. An example:
335 * PROVIDE: nameservice dnscache
340 * PROVIDE: nameservice nscd
344 warnx("file `%s' provides `%s'.", fnode->filename, s);
345 warnx("\tpreviously seen in `%s'.",
346 head->next->fnode->filename);
350 pnode = emalloc(sizeof(*pnode));
352 pnode->in_progress = RESET;
353 pnode->fnode = fnode;
354 pnode->next = head->next;
357 if (pnode->next != NULL)
358 pnode->next->last = pnode;
360 f_pnode = emalloc(sizeof(*f_pnode));
361 f_pnode->pnode = pnode;
362 f_pnode->next = fnode->prov_list;
363 fnode->prov_list = f_pnode;
367 * put the BEFORE: lines to a list and handle them later.
376 bf_ent = emalloc(sizeof *bf_ent + strlen(s));
377 bf_ent->node = fnode;
378 strcpy(bf_ent->s, s);
379 bf_ent->next = bl_list;
384 * add a key to a filenode.
387 add_keyword(fnode, s)
392 strnode_add(&fnode->keyword_list, s, fnode);
396 * loop over the rest of a REQUIRE line, giving each word to
397 * add_require() to do the real work.
400 parse_require(node, buffer)
406 while ((s = strsep(&buffer, " \t\n")) != NULL)
408 add_require(node, s);
412 * loop over the rest of a PROVIDE line, giving each word to
413 * add_provide() to do the real work.
416 parse_provide(node, buffer)
422 while ((s = strsep(&buffer, " \t\n")) != NULL)
424 add_provide(node, s);
428 * loop over the rest of a BEFORE line, giving each word to
429 * add_before() to do the real work.
432 parse_before(node, buffer)
438 while ((s = strsep(&buffer, " \t\n")) != NULL)
444 * loop over the rest of a KEYWORD line, giving each word to
445 * add_keyword() to do the real work.
448 parse_keywords(node, buffer)
454 while ((s = strsep(&buffer, " \t\n")) != NULL)
456 add_keyword(node, s);
460 * given a file name, create a filenode for it, read in lines looking
461 * for provision and requirement lines, building the graphs as needed.
464 crunch_file(filename)
469 int require_flag, provide_flag, before_flag, keywords_flag;
470 enum { BEFORE_PARSING, PARSING, PARSING_DONE } state;
472 char delims[3] = { '\\', '\\', '\0' };
475 if ((fp = fopen(filename, "r")) == NULL) {
476 warn("could not open %s", filename);
480 if (fstat(fileno(fp), &st) == -1) {
481 warn("could not stat %s", filename);
486 if (!S_ISREG(st.st_mode)) {
488 warnx("%s is not a file", filename);
494 node = filenode_new(filename);
497 * we don't care about length, line number, don't want # for comments,
500 for (state = BEFORE_PARSING; state != PARSING_DONE &&
501 (buf = fparseln(fp, NULL, NULL, delims, 0)) != NULL; free(buf)) {
502 require_flag = provide_flag = before_flag = keywords_flag = 0;
503 if (strncmp(REQUIRE_STR, buf, REQUIRE_LEN) == 0)
504 require_flag = REQUIRE_LEN;
505 else if (strncmp(REQUIRES_STR, buf, REQUIRES_LEN) == 0)
506 require_flag = REQUIRES_LEN;
507 else if (strncmp(PROVIDE_STR, buf, PROVIDE_LEN) == 0)
508 provide_flag = PROVIDE_LEN;
509 else if (strncmp(PROVIDES_STR, buf, PROVIDES_LEN) == 0)
510 provide_flag = PROVIDES_LEN;
511 else if (strncmp(BEFORE_STR, buf, BEFORE_LEN) == 0)
512 before_flag = BEFORE_LEN;
513 else if (strncmp(KEYWORD_STR, buf, KEYWORD_LEN) == 0)
514 keywords_flag = KEYWORD_LEN;
515 else if (strncmp(KEYWORDS_STR, buf, KEYWORDS_LEN) == 0)
516 keywords_flag = KEYWORDS_LEN;
518 if (state == PARSING)
519 state = PARSING_DONE;
525 parse_require(node, buf + require_flag);
526 else if (provide_flag)
527 parse_provide(node, buf + provide_flag);
528 else if (before_flag)
529 parse_before(node, buf + before_flag);
530 else if (keywords_flag)
531 parse_keywords(node, buf + keywords_flag);
537 make_fake_provision(node)
542 provnode *head, *pnode;
548 snprintf(buffer, sizeof buffer, "fake_prov_%08d", i++);
549 entry = Hash_CreateEntry(provide_hash, buffer, &new);
551 head = emalloc(sizeof(*head));
553 head->in_progress = RESET;
555 head->last = head->next = NULL;
556 Hash_SetValue(entry, head);
558 pnode = emalloc(sizeof(*pnode));
560 pnode->in_progress = RESET;
562 pnode->next = head->next;
565 if (pnode->next != NULL)
566 pnode->next->last = pnode;
568 f_pnode = emalloc(sizeof(*f_pnode));
569 f_pnode->pnode = pnode;
570 f_pnode->next = node->prov_list;
571 node->prov_list = f_pnode;
577 * go through the BEFORE list, inserting requirements into the graph(s)
578 * as required. in the before list, for each entry B, we have a file F
579 * and a string S. we create a "fake" provision (P) that F provides.
580 * for each entry in the provision list for S, add a requirement to
581 * that provisions filenode for P.
586 Hash_Entry *entry, *fake_prov_entry;
592 while (bl_list != NULL) {
595 fake_prov_entry = make_fake_provision(bl_list->node);
597 entry = Hash_CreateEntry(provide_hash, bl_list->s, &new);
599 warnx("file `%s' is before unknown provision `%s'", bl_list->node->filename, bl_list->s);
601 for (pnode = Hash_GetValue(entry); pnode; pnode = pnode->next) {
605 rnode = emalloc(sizeof(*rnode));
606 rnode->entry = fake_prov_entry;
607 rnode->next = pnode->fnode->req_list;
608 pnode->fnode->req_list = rnode;
617 * loop over all the files calling crunch_file() on them to do the
618 * real work. after we have built all the nodes, insert the BEFORE:
619 * lines into graph(s).
626 for (i = 0; i < file_count; i++)
627 crunch_file(file_list[i]);
632 * below are the functions that traverse the graphs we have built
633 * finding out the desired ordering, printing each file in turn.
634 * if missing requirements, or cyclic graphs are detected, a
635 * warning will be issued, and we will continue on..
639 * given a requirement node (in a filename) we attempt to satisfy it.
640 * we do some sanity checking first, to ensure that we have providers,
641 * aren't already satisfied and aren't already being satisfied (ie,
642 * cyclic). if we pass all this, we loop over the provision list
643 * calling do_file() (enter recursion) for each filenode in this
647 satisfy_req(rnode, filename)
654 entry = rnode->entry;
655 head = Hash_GetValue(entry);
658 warnx("requirement `%s' in file `%s' has no providers.",
659 Hash_GetKey(entry), filename);
664 /* return if the requirement is already satisfied. */
665 if (head->next == NULL)
669 * if list is marked as in progress,
670 * print that there is a circular dependency on it and abort
672 if (head->in_progress == SET) {
673 warnx("Circular dependency on provision `%s' in file `%s'.",
674 Hash_GetKey(entry), filename);
679 head->in_progress = SET;
682 * while provision_list is not empty
683 * do_file(first_member_of(provision_list));
685 while (head->next != NULL)
686 do_file(head->next->fnode);
696 for (s = skip_list; s; s = s->next)
697 for (k = fnode->keyword_list; k; k = k->next)
698 if (strcmp(k->s, s->s) == 0)
711 for (s = keep_list; s; s = s->next)
712 for (k = fnode->keyword_list; k; k = k->next)
713 if (strcmp(k->s, s->s) == 0)
716 /* an empty keep_list means every one */
721 * given a filenode, we ensure we are not a cyclic graph. if this
722 * is ok, we loop over the filenodes requirements, calling satisfy_req()
723 * for each of them.. once we have done this, remove this filenode
724 * from each provision table, as we are now done.
730 f_reqnode *r, *r_tmp;
731 f_provnode *p, *p_tmp;
735 DPRINTF((stderr, "do_file on %s.\n", fnode->filename));
738 * if fnode is marked as in progress,
739 * print that fnode; is circularly depended upon and abort.
741 if (fnode->in_progress == SET) {
742 warnx("Circular dependency on file `%s'.",
744 was_set = exit_code = 1;
749 fnode->in_progress = SET;
752 * for each requirement of fnode -> r
753 * satisfy_req(r, filename)
758 satisfy_req(r, fnode->filename);
763 fnode->req_list = NULL;
766 * for each provision of fnode -> p
767 * remove fnode from provision list for p in hash table
769 p = fnode->prov_list;
773 if (pnode->next != NULL) {
774 pnode->next->last = pnode->last;
776 if (pnode->last != NULL) {
777 pnode->last->next = pnode->next;
783 fnode->prov_list = NULL;
786 DPRINTF((stderr, "next do: "));
788 /* if we were already in progress, don't print again */
789 if (was_set == 0 && skip_ok(fnode) && keep_ok(fnode))
790 printf("%s\n", fnode->filename);
792 if (fnode->next != NULL) {
793 fnode->next->last = fnode->last;
795 if (fnode->last != NULL) {
796 fnode->last->next = fnode->next;
799 DPRINTF((stderr, "nuking %s\n", fnode->filename));
801 free(fnode->filename);
811 * while there remain undone files{f},
812 * pick an arbitrary f, and do_file(f)
813 * Note that the first file in the file list is perfectly
814 * arbitrary, and easy to find, so we use that.
818 * N.B.: the file nodes "self delete" after they execute, so
819 * after each iteration of the loop, the head will be pointing
820 * to something totally different. The loop ends up being
821 * executed only once for every strongly connected set of
824 while (fn_head->next != NULL) {
825 DPRINTF((stderr, "generate on %s\n", fn_head->next->filename));
826 do_file(fn_head->next);