1 /* $NetBSD: rcorder.c,v 1.7 2000/08/04 07:33:55 enami Exp $ */
4 * Copyright (c) 1998, 1999 Matthew R. Green
7 * Perry E. Metzger. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
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)
83 Hash_Table provide_hash_s, *provide_hash;
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;
95 provnode *next, *last;
117 filenode *next, *last;
119 f_provnode *prov_list;
120 strnodelist *keyword_list;
123 filenode fn_head_s, *fn_head;
125 strnodelist *bl_list;
126 strnodelist *keep_list;
127 strnodelist *skip_list;
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 *[]));
158 while ((ch = getopt(argc, argv, "dk:s:")) != -1)
164 warnx("debugging not compiled in, -d ignored");
168 strnode_add(&keep_list, optarg, 0);
171 strnode_add(&skip_list, optarg, 0);
174 /* XXX should crunch it? */
183 DPRINTF((stderr, "parse_args\n"));
185 DPRINTF((stderr, "initialize\n"));
187 DPRINTF((stderr, "crunch_all_files\n"));
189 DPRINTF((stderr, "generate_ordering\n"));
195 * initialise various variables.
201 fn_head = &fn_head_s;
203 provide_hash = &provide_hash_s;
204 Hash_InitTable(provide_hash, file_count);
207 /* generic function to insert a new strnodelist element */
209 strnode_add(listp, s, fnode)
216 ent = emalloc(sizeof *ent + strlen(s));
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.
230 * we have a new filename, create a new filenode structure.
231 * fill in the bits, and put it in the filenode linked list
234 filenode_new(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(fnode, s)
271 entry = Hash_CreateEntry(provide_hash, s, &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;
281 * add a provision to a filenode. if this provision doesn't
282 * have a head node, create one here.
285 add_provide(fnode, s)
291 provnode *pnode, *head;
294 entry = Hash_CreateEntry(provide_hash, s, &new);
295 head = Hash_GetValue(entry);
297 /* create a head node if necessary. */
299 head = emalloc(sizeof(*head));
301 head->in_progress = RESET;
303 head->last = head->next = NULL;
304 Hash_SetValue(entry, head);
308 * Don't warn about this. We want to be able to support
309 * scripts that do two complex things:
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:
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:
331 * PROVIDE: nameservice dnscache
336 * PROVIDE: nameservice nscd
340 warnx("file `%s' provides `%s'.", fnode->filename, s);
341 warnx("\tpreviously seen in `%s'.",
342 head->next->fnode->filename);
346 pnode = emalloc(sizeof(*pnode));
348 pnode->in_progress = RESET;
349 pnode->fnode = fnode;
350 pnode->next = head->next;
353 if (pnode->next != NULL)
354 pnode->next->last = pnode;
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;
363 * put the BEFORE: lines to a list and handle them later.
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;
380 * add a key to a filenode.
383 add_keyword(fnode, s)
388 strnode_add(&fnode->keyword_list, s, fnode);
392 * loop over the rest of a REQUIRE line, giving each word to
393 * add_require() to do the real work.
396 parse_require(node, buffer)
402 while ((s = strsep(&buffer, " \t\n")) != NULL)
404 add_require(node, s);
408 * loop over the rest of a PROVIDE line, giving each word to
409 * add_provide() to do the real work.
412 parse_provide(node, buffer)
418 while ((s = strsep(&buffer, " \t\n")) != NULL)
420 add_provide(node, s);
424 * loop over the rest of a BEFORE line, giving each word to
425 * add_before() to do the real work.
428 parse_before(node, buffer)
434 while ((s = strsep(&buffer, " \t\n")) != NULL)
440 * loop over the rest of a KEYWORD line, giving each word to
441 * add_keyword() to do the real work.
444 parse_keywords(node, buffer)
450 while ((s = strsep(&buffer, " \t\n")) != NULL)
452 add_keyword(node, s);
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.
460 crunch_file(filename)
465 int require_flag, provide_flag, before_flag, keywords_flag;
466 enum { BEFORE_PARSING, PARSING, PARSING_DONE } state;
468 char delims[3] = { '\\', '\\', '\0' };
471 if ((fp = fopen(filename, "r")) == NULL) {
472 warn("could not open %s", filename);
476 if (fstat(fileno(fp), &st) == -1) {
477 warn("could not stat %s", filename);
482 if (!S_ISREG(st.st_mode)) {
484 warnx("%s is not a file", filename);
490 node = filenode_new(filename);
493 * we don't care about length, line number, don't want # for comments,
496 for (state = BEFORE_PARSING; state != PARSING_DONE &&
497 (buf = fparseln(fp, NULL, NULL, delims, 0)) != NULL; free(buf)) {
498 require_flag = provide_flag = before_flag = keywords_flag = 0;
499 if (strncmp(REQUIRE_STR, buf, REQUIRE_LEN) == 0)
500 require_flag = REQUIRE_LEN;
501 else if (strncmp(REQUIRES_STR, buf, REQUIRES_LEN) == 0)
502 require_flag = REQUIRES_LEN;
503 else if (strncmp(PROVIDE_STR, buf, PROVIDE_LEN) == 0)
504 provide_flag = PROVIDE_LEN;
505 else if (strncmp(PROVIDES_STR, buf, PROVIDES_LEN) == 0)
506 provide_flag = PROVIDES_LEN;
507 else if (strncmp(BEFORE_STR, buf, BEFORE_LEN) == 0)
508 before_flag = BEFORE_LEN;
509 else if (strncmp(KEYWORD_STR, buf, KEYWORD_LEN) == 0)
510 keywords_flag = KEYWORD_LEN;
511 else if (strncmp(KEYWORDS_STR, buf, KEYWORDS_LEN) == 0)
512 keywords_flag = KEYWORDS_LEN;
514 if (state == PARSING)
515 state = PARSING_DONE;
521 parse_require(node, buf + require_flag);
522 else if (provide_flag)
523 parse_provide(node, buf + provide_flag);
524 else if (before_flag)
525 parse_before(node, buf + before_flag);
526 else if (keywords_flag)
527 parse_keywords(node, buf + keywords_flag);
533 make_fake_provision(node)
538 provnode *head, *pnode;
544 snprintf(buffer, sizeof buffer, "fake_prov_%08d", i++);
545 entry = Hash_CreateEntry(provide_hash, buffer, &new);
547 head = emalloc(sizeof(*head));
549 head->in_progress = RESET;
551 head->last = head->next = NULL;
552 Hash_SetValue(entry, head);
554 pnode = emalloc(sizeof(*pnode));
556 pnode->in_progress = RESET;
558 pnode->next = head->next;
561 if (pnode->next != NULL)
562 pnode->next->last = pnode;
564 f_pnode = emalloc(sizeof(*f_pnode));
565 f_pnode->pnode = pnode;
566 f_pnode->next = node->prov_list;
567 node->prov_list = f_pnode;
573 * go through the BEFORE list, inserting requirements into the graph(s)
574 * as required. in the before list, for each entry B, we have a file F
575 * and a string S. we create a "fake" provision (P) that F provides.
576 * for each entry in the provision list for S, add a requirement to
577 * that provisions filenode for P.
582 Hash_Entry *entry, *fake_prov_entry;
588 while (bl_list != NULL) {
591 fake_prov_entry = make_fake_provision(bl_list->node);
593 entry = Hash_CreateEntry(provide_hash, bl_list->s, &new);
595 warnx("file `%s' is before unknown provision `%s'", bl_list->node->filename, bl_list->s);
597 for (pnode = Hash_GetValue(entry); pnode; pnode = pnode->next) {
601 rnode = emalloc(sizeof(*rnode));
602 rnode->entry = fake_prov_entry;
603 rnode->next = pnode->fnode->req_list;
604 pnode->fnode->req_list = rnode;
613 * loop over all the files calling crunch_file() on them to do the
614 * real work. after we have built all the nodes, insert the BEFORE:
615 * lines into graph(s).
622 for (i = 0; i < file_count; i++)
623 crunch_file(file_list[i]);
628 * below are the functions that traverse the graphs we have built
629 * finding out the desired ordering, printing each file in turn.
630 * if missing requirements, or cyclic graphs are detected, a
631 * warning will be issued, and we will continue on..
635 * given a requirement node (in a filename) we attempt to satisfy it.
636 * we do some sanity checking first, to ensure that we have providers,
637 * aren't already satisfied and aren't already being satisfied (ie,
638 * cyclic). if we pass all this, we loop over the provision list
639 * calling do_file() (enter recursion) for each filenode in this
643 satisfy_req(rnode, filename)
650 entry = rnode->entry;
651 head = Hash_GetValue(entry);
654 warnx("requirement `%s' in file `%s' has no providers.",
655 Hash_GetKey(entry), filename);
660 /* return if the requirement is already satisfied. */
661 if (head->next == NULL)
665 * if list is marked as in progress,
666 * print that there is a circular dependency on it and abort
668 if (head->in_progress == SET) {
669 warnx("Circular dependency on provision `%s' in file `%s'.",
670 Hash_GetKey(entry), filename);
675 head->in_progress = SET;
678 * while provision_list is not empty
679 * do_file(first_member_of(provision_list));
681 while (head->next != NULL)
682 do_file(head->next->fnode);
692 for (s = skip_list; s; s = s->next)
693 for (k = fnode->keyword_list; k; k = k->next)
694 if (strcmp(k->s, s->s) == 0)
707 for (s = keep_list; s; s = s->next)
708 for (k = fnode->keyword_list; k; k = k->next)
709 if (strcmp(k->s, s->s) == 0)
712 /* an empty keep_list means every one */
717 * given a filenode, we ensure we are not a cyclic graph. if this
718 * is ok, we loop over the filenodes requirements, calling satisfy_req()
719 * for each of them.. once we have done this, remove this filenode
720 * from each provision table, as we are now done.
726 f_reqnode *r, *r_tmp;
727 f_provnode *p, *p_tmp;
731 DPRINTF((stderr, "do_file on %s.\n", fnode->filename));
734 * if fnode is marked as in progress,
735 * print that fnode; is circularly depended upon and abort.
737 if (fnode->in_progress == SET) {
738 warnx("Circular dependency on file `%s'.",
740 was_set = exit_code = 1;
745 fnode->in_progress = SET;
748 * for each requirement of fnode -> r
749 * satisfy_req(r, filename)
754 satisfy_req(r, fnode->filename);
758 fnode->req_list = NULL;
761 * for each provision of fnode -> p
762 * remove fnode from provision list for p in hash table
764 p = fnode->prov_list;
768 if (pnode->next != NULL) {
769 pnode->next->last = pnode->last;
771 if (pnode->last != NULL) {
772 pnode->last->next = pnode->next;
778 fnode->prov_list = NULL;
781 DPRINTF((stderr, "next do: "));
783 /* if we were already in progress, don't print again */
784 if (was_set == 0 && skip_ok(fnode) && keep_ok(fnode))
785 printf("%s\n", fnode->filename);
787 if (fnode->next != NULL) {
788 fnode->next->last = fnode->last;
790 if (fnode->last != NULL) {
791 fnode->last->next = fnode->next;
794 DPRINTF((stderr, "nuking %s\n", fnode->filename));
795 free(fnode->filename);
804 * while there remain undone files{f},
805 * pick an arbitrary f, and do_file(f)
806 * Note that the first file in the file list is perfectly
807 * arbitrary, and easy to find, so we use that.
811 * N.B.: the file nodes "self delete" after they execute, so
812 * after each iteration of the loop, the head will be pointing
813 * to something totally different. The loop ends up being
814 * executed only once for every strongly connected set of
817 while (fn_head->next != NULL) {
818 DPRINTF((stderr, "generate on %s\n", fn_head->next->filename));
819 do_file(fn_head->next);