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 $
34 * $DragonFly: src/sbin/rcorder/rcorder.c,v 1.2 2003/09/28 14:39:21 hmp Exp $
37 #include <sys/types.h>
53 # define DPRINTF(args) if (debug) { fflush(stdout); fprintf args; }
55 # define DPRINTF(args)
58 #define REQUIRE_STR "# REQUIRE:"
59 #define REQUIRE_LEN (sizeof(REQUIRE_STR) - 1)
60 #define REQUIRES_STR "# REQUIRES:"
61 #define REQUIRES_LEN (sizeof(REQUIRES_STR) - 1)
62 #define PROVIDE_STR "# PROVIDE:"
63 #define PROVIDE_LEN (sizeof(PROVIDE_STR) - 1)
64 #define PROVIDES_STR "# PROVIDES:"
65 #define PROVIDES_LEN (sizeof(PROVIDES_STR) - 1)
66 #define BEFORE_STR "# BEFORE:"
67 #define BEFORE_LEN (sizeof(BEFORE_STR) - 1)
68 #define KEYWORD_STR "# KEYWORD:"
69 #define KEYWORD_LEN (sizeof(KEYWORD_STR) - 1)
70 #define KEYWORDS_STR "# KEYWORDS:"
71 #define KEYWORDS_LEN (sizeof(KEYWORDS_STR) - 1)
84 Hash_Table provide_hash_s, *provide_hash;
86 typedef struct provnode provnode;
87 typedef struct filenode filenode;
88 typedef struct f_provnode f_provnode;
89 typedef struct f_reqnode f_reqnode;
90 typedef struct strnodelist strnodelist;
96 provnode *next, *last;
118 filenode *next, *last;
120 f_provnode *prov_list;
121 strnodelist *keyword_list;
124 filenode fn_head_s, *fn_head;
126 strnodelist *bl_list;
127 strnodelist *keep_list;
128 strnodelist *skip_list;
130 void do_file __P((filenode *fnode));
131 void strnode_add __P((strnodelist **, char *, filenode *));
132 int skip_ok __P((filenode *fnode));
133 int keep_ok __P((filenode *fnode));
134 void satisfy_req __P((f_reqnode *rnode, char *filename));
135 void crunch_file __P((char *));
136 void parse_require __P((filenode *, char *));
137 void parse_provide __P((filenode *, char *));
138 void parse_before __P((filenode *, char *));
139 void parse_keywords __P((filenode *, char *));
140 filenode *filenode_new __P((char *));
141 void add_require __P((filenode *, char *));
142 void add_provide __P((filenode *, char *));
143 void add_before __P((filenode *, char *));
144 void add_keyword __P((filenode *, char *));
145 void insert_before __P((void));
146 Hash_Entry *make_fake_provision __P((filenode *));
147 void crunch_all_files __P((void));
148 void initialize __P((void));
149 void generate_ordering __P((void));
150 int main __P((int, char *[]));
153 main(int argc, char **argv)
157 while ((ch = getopt(argc, argv, "dk:s:")) != -1)
163 warnx("debugging not compiled in, -d ignored");
167 strnode_add(&keep_list, optarg, 0);
170 strnode_add(&skip_list, optarg, 0);
173 /* XXX should crunch it? */
182 DPRINTF((stderr, "parse_args\n"));
184 DPRINTF((stderr, "initialize\n"));
186 DPRINTF((stderr, "crunch_all_files\n"));
188 DPRINTF((stderr, "generate_ordering\n"));
194 * initialise various variables.
200 fn_head = &fn_head_s;
202 provide_hash = &provide_hash_s;
203 Hash_InitTable(provide_hash, file_count);
206 /* generic function to insert a new strnodelist element */
208 strnode_add(strnodelist **listp, char *s, filenode *fnode)
212 ent = emalloc(sizeof *ent + strlen(s));
220 * below are the functions that deal with creating the lists
221 * from the filename's given and the dependancies and provisions
222 * in each of these files. no ordering or checking is done here.
226 * we have a new filename, create a new filenode structure.
227 * fill in the bits, and put it in the filenode linked list
230 filenode_new(char *filename)
234 temp = emalloc(sizeof(*temp));
235 memset(temp, 0, sizeof(*temp));
236 temp->filename = estrdup(filename);
237 temp->req_list = NULL;
238 temp->prov_list = NULL;
239 temp->keyword_list = NULL;
240 temp->in_progress = RESET;
242 * link the filenode into the list of filenodes.
243 * note that the double linking means we can delete a
244 * filenode without searching for where it belongs.
246 temp->next = fn_head->next;
247 if (temp->next != NULL)
248 temp->next->last = temp;
249 temp->last = fn_head;
250 fn_head->next = temp;
255 * add a requirement to a filenode.
258 add_require(filenode *fnode, char *s)
264 entry = Hash_CreateEntry(provide_hash, s, &new);
266 Hash_SetValue(entry, NULL);
267 rnode = emalloc(sizeof(*rnode));
268 rnode->entry = entry;
269 rnode->next = fnode->req_list;
270 fnode->req_list = rnode;
274 * add a provision to a filenode. if this provision doesn't
275 * have a head node, create one here.
278 add_provide(filenode *fnode, char *s)
282 provnode *pnode, *head;
285 entry = Hash_CreateEntry(provide_hash, s, &new);
286 head = Hash_GetValue(entry);
288 /* create a head node if necessary. */
290 head = emalloc(sizeof(*head));
292 head->in_progress = RESET;
294 head->last = head->next = NULL;
295 Hash_SetValue(entry, head);
299 * Don't warn about this. We want to be able to support
300 * scripts that do two complex things:
302 * - Two independent scripts which both provide the
303 * same thing. Both scripts must be executed in
304 * any order to meet the barrier. An example:
316 * - Two interdependent scripts which both provide the
317 * same thing. Both scripts must be executed in
318 * graph order to meet the barrier. An example:
322 * PROVIDE: nameservice dnscache
327 * PROVIDE: nameservice nscd
331 warnx("file `%s' provides `%s'.", fnode->filename, s);
332 warnx("\tpreviously seen in `%s'.",
333 head->next->fnode->filename);
337 pnode = emalloc(sizeof(*pnode));
339 pnode->in_progress = RESET;
340 pnode->fnode = fnode;
341 pnode->next = head->next;
344 if (pnode->next != NULL)
345 pnode->next->last = pnode;
347 f_pnode = emalloc(sizeof(*f_pnode));
348 f_pnode->pnode = pnode;
349 f_pnode->next = fnode->prov_list;
350 fnode->prov_list = f_pnode;
354 * put the BEFORE: lines to a list and handle them later.
357 add_before(filenode *fnode, char *s)
361 bf_ent = emalloc(sizeof *bf_ent + strlen(s));
362 bf_ent->node = fnode;
363 strcpy(bf_ent->s, s);
364 bf_ent->next = bl_list;
369 * add a key to a filenode.
372 add_keyword(filenode *fnode, char *s)
375 strnode_add(&fnode->keyword_list, s, fnode);
379 * loop over the rest of a REQUIRE line, giving each word to
380 * add_require() to do the real work.
383 parse_require(filenode *node, char *buffer)
387 while ((s = strsep(&buffer, " \t\n")) != NULL)
389 add_require(node, s);
393 * loop over the rest of a PROVIDE line, giving each word to
394 * add_provide() to do the real work.
397 parse_provide(filenode *node, char *buffer)
401 while ((s = strsep(&buffer, " \t\n")) != NULL)
403 add_provide(node, s);
407 * loop over the rest of a BEFORE line, giving each word to
408 * add_before() to do the real work.
411 parse_before(filenode *node, char *buffer)
415 while ((s = strsep(&buffer, " \t\n")) != NULL)
421 * loop over the rest of a KEYWORD line, giving each word to
422 * add_keyword() to do the real work.
425 parse_keywords(filenode *node, char *buffer)
429 while ((s = strsep(&buffer, " \t\n")) != NULL)
431 add_keyword(node, s);
435 * given a file name, create a filenode for it, read in lines looking
436 * for provision and requirement lines, building the graphs as needed.
439 crunch_file(char *filename)
443 int require_flag, provide_flag, before_flag, keywords_flag;
444 enum { BEFORE_PARSING, PARSING, PARSING_DONE } state;
446 char delims[3] = { '\\', '\\', '\0' };
449 if ((fp = fopen(filename, "r")) == NULL) {
450 warn("could not open %s", filename);
454 if (fstat(fileno(fp), &st) == -1) {
455 warn("could not stat %s", filename);
460 if (!S_ISREG(st.st_mode)) {
462 warnx("%s is not a file", filename);
468 node = filenode_new(filename);
471 * we don't care about length, line number, don't want # for comments,
474 for (state = BEFORE_PARSING; state != PARSING_DONE &&
475 (buf = fparseln(fp, NULL, NULL, delims, 0)) != NULL; free(buf)) {
476 require_flag = provide_flag = before_flag = keywords_flag = 0;
477 if (strncmp(REQUIRE_STR, buf, REQUIRE_LEN) == 0)
478 require_flag = REQUIRE_LEN;
479 else if (strncmp(REQUIRES_STR, buf, REQUIRES_LEN) == 0)
480 require_flag = REQUIRES_LEN;
481 else if (strncmp(PROVIDE_STR, buf, PROVIDE_LEN) == 0)
482 provide_flag = PROVIDE_LEN;
483 else if (strncmp(PROVIDES_STR, buf, PROVIDES_LEN) == 0)
484 provide_flag = PROVIDES_LEN;
485 else if (strncmp(BEFORE_STR, buf, BEFORE_LEN) == 0)
486 before_flag = BEFORE_LEN;
487 else if (strncmp(KEYWORD_STR, buf, KEYWORD_LEN) == 0)
488 keywords_flag = KEYWORD_LEN;
489 else if (strncmp(KEYWORDS_STR, buf, KEYWORDS_LEN) == 0)
490 keywords_flag = KEYWORDS_LEN;
492 if (state == PARSING)
493 state = PARSING_DONE;
499 parse_require(node, buf + require_flag);
500 else if (provide_flag)
501 parse_provide(node, buf + provide_flag);
502 else if (before_flag)
503 parse_before(node, buf + before_flag);
504 else if (keywords_flag)
505 parse_keywords(node, buf + keywords_flag);
511 make_fake_provision(filenode *node)
515 provnode *head, *pnode;
521 snprintf(buffer, sizeof buffer, "fake_prov_%08d", i++);
522 entry = Hash_CreateEntry(provide_hash, buffer, &new);
524 head = emalloc(sizeof(*head));
526 head->in_progress = RESET;
528 head->last = head->next = NULL;
529 Hash_SetValue(entry, head);
531 pnode = emalloc(sizeof(*pnode));
533 pnode->in_progress = RESET;
535 pnode->next = head->next;
538 if (pnode->next != NULL)
539 pnode->next->last = pnode;
541 f_pnode = emalloc(sizeof(*f_pnode));
542 f_pnode->pnode = pnode;
543 f_pnode->next = node->prov_list;
544 node->prov_list = f_pnode;
550 * go through the BEFORE list, inserting requirements into the graph(s)
551 * as required. in the before list, for each entry B, we have a file F
552 * and a string S. we create a "fake" provision (P) that F provides.
553 * for each entry in the provision list for S, add a requirement to
554 * that provisions filenode for P.
559 Hash_Entry *entry, *fake_prov_entry;
565 while (bl_list != NULL) {
568 fake_prov_entry = make_fake_provision(bl_list->node);
570 entry = Hash_CreateEntry(provide_hash, bl_list->s, &new);
572 warnx("file `%s' is before unknown provision `%s'", bl_list->node->filename, bl_list->s);
574 for (pnode = Hash_GetValue(entry); pnode; pnode = pnode->next) {
578 rnode = emalloc(sizeof(*rnode));
579 rnode->entry = fake_prov_entry;
580 rnode->next = pnode->fnode->req_list;
581 pnode->fnode->req_list = rnode;
590 * loop over all the files calling crunch_file() on them to do the
591 * real work. after we have built all the nodes, insert the BEFORE:
592 * lines into graph(s).
595 crunch_all_files(void)
599 for (i = 0; i < file_count; i++)
600 crunch_file(file_list[i]);
605 * below are the functions that traverse the graphs we have built
606 * finding out the desired ordering, printing each file in turn.
607 * if missing requirements, or cyclic graphs are detected, a
608 * warning will be issued, and we will continue on..
612 * given a requirement node (in a filename) we attempt to satisfy it.
613 * we do some sanity checking first, to ensure that we have providers,
614 * aren't already satisfied and aren't already being satisfied (ie,
615 * cyclic). if we pass all this, we loop over the provision list
616 * calling do_file() (enter recursion) for each filenode in this
620 satisfy_req(f_reqnode *rnode, char *filename)
625 entry = rnode->entry;
626 head = Hash_GetValue(entry);
629 warnx("requirement `%s' in file `%s' has no providers.",
630 Hash_GetKey(entry), filename);
635 /* return if the requirement is already satisfied. */
636 if (head->next == NULL)
640 * if list is marked as in progress,
641 * print that there is a circular dependency on it and abort
643 if (head->in_progress == SET) {
644 warnx("Circular dependency on provision `%s' in file `%s'.",
645 Hash_GetKey(entry), filename);
650 head->in_progress = SET;
653 * while provision_list is not empty
654 * do_file(first_member_of(provision_list));
656 while (head->next != NULL)
657 do_file(head->next->fnode);
661 skip_ok(filenode *fnode)
666 for (s = skip_list; s; s = s->next)
667 for (k = fnode->keyword_list; k; k = k->next)
668 if (strcmp(k->s, s->s) == 0)
675 keep_ok(filenode *fnode)
680 for (s = keep_list; s; s = s->next)
681 for (k = fnode->keyword_list; k; k = k->next)
682 if (strcmp(k->s, s->s) == 0)
685 /* an empty keep_list means every one */
690 * given a filenode, we ensure we are not a cyclic graph. if this
691 * is ok, we loop over the filenodes requirements, calling satisfy_req()
692 * for each of them.. once we have done this, remove this filenode
693 * from each provision table, as we are now done.
696 do_file(filenode *fnode)
698 f_reqnode *r, *r_tmp;
699 f_provnode *p, *p_tmp;
703 DPRINTF((stderr, "do_file on %s.\n", fnode->filename));
706 * if fnode is marked as in progress,
707 * print that fnode; is circularly depended upon and abort.
709 if (fnode->in_progress == SET) {
710 warnx("Circular dependency on file `%s'.",
712 was_set = exit_code = 1;
717 fnode->in_progress = SET;
720 * for each requirement of fnode -> r
721 * satisfy_req(r, filename)
726 satisfy_req(r, fnode->filename);
730 fnode->req_list = NULL;
733 * for each provision of fnode -> p
734 * remove fnode from provision list for p in hash table
736 p = fnode->prov_list;
740 if (pnode->next != NULL) {
741 pnode->next->last = pnode->last;
743 if (pnode->last != NULL) {
744 pnode->last->next = pnode->next;
750 fnode->prov_list = NULL;
753 DPRINTF((stderr, "next do: "));
755 /* if we were already in progress, don't print again */
756 if (was_set == 0 && skip_ok(fnode) && keep_ok(fnode))
757 printf("%s\n", fnode->filename);
759 if (fnode->next != NULL) {
760 fnode->next->last = fnode->last;
762 if (fnode->last != NULL) {
763 fnode->last->next = fnode->next;
766 DPRINTF((stderr, "nuking %s\n", fnode->filename));
767 free(fnode->filename);
772 generate_ordering(void)
776 * while there remain undone files{f},
777 * pick an arbitrary f, and do_file(f)
778 * Note that the first file in the file list is perfectly
779 * arbitrary, and easy to find, so we use that.
783 * N.B.: the file nodes "self delete" after they execute, so
784 * after each iteration of the loop, the head will be pointing
785 * to something totally different. The loop ends up being
786 * executed only once for every strongly connected set of
789 while (fn_head->next != NULL) {
790 DPRINTF((stderr, "generate on %s\n", fn_head->next->filename));
791 do_file(fn_head->next);