mkdep.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* $Id$ */
  2. /*
  3. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  4. * See the copyright notice in the ACK home directory, in the file "Copyright".
  5. */
  6. /* make dependencies; Date: jan 07, 1986; Author: Erik Baalbergen */
  7. /* Log:
  8. [Thu Oct 6 09:56:30 MET 1988; erikb]
  9. Added option '-d' which suppresses "file.c :" be printed
  10. */
  11. #include <stdio.h>
  12. #define BSIZ 1024
  13. char *prog;
  14. int dflag = 0; /* suppress "file.c :" */
  15. struct namelist {
  16. struct namelist *next;
  17. char *name;
  18. };
  19. struct namelist *freelist;
  20. struct namelist *new_namelist();
  21. struct namelist *nl = 0;
  22. char *Malloc(u)
  23. unsigned u;
  24. {
  25. char *sp, *malloc();
  26. if ((sp = malloc(u)) == 0) {
  27. fprintf(stderr, "%s: out of space\n");
  28. exit(1);
  29. }
  30. return sp;
  31. }
  32. struct namelist *
  33. new_namelist()
  34. {
  35. register struct namelist *nlp = freelist;
  36. if (nlp) {
  37. freelist = nlp->next;
  38. return nlp;
  39. }
  40. return (struct namelist *) Malloc(sizeof(struct namelist));
  41. }
  42. free_namelist(nlp)
  43. struct namelist *nlp;
  44. {
  45. if (nlp) {
  46. free_namelist(nlp->next);
  47. nlp->next = freelist;
  48. freelist = nlp;
  49. }
  50. }
  51. add_name(nm)
  52. char *nm;
  53. {
  54. struct namelist *nlp = nl, *lnlp = 0, *nnlp;
  55. char *strcpy();
  56. while (nlp) {
  57. register i = strcmp(nm, nlp->name);
  58. if (i < 0)
  59. break;
  60. if (i == 0) /* already present */
  61. return;
  62. lnlp = nlp;
  63. nlp = nlp->next;
  64. }
  65. (nnlp = new_namelist())->name = strcpy(Malloc((unsigned)strlen(nm) + 1), nm);
  66. if (lnlp) {
  67. nnlp->next = lnlp->next;
  68. lnlp->next = nnlp;
  69. }
  70. else {
  71. nnlp->next = nl;
  72. nl = nnlp;
  73. }
  74. }
  75. print_namelist(nm, nlp)
  76. char *nm;
  77. struct namelist *nlp;
  78. {
  79. while (nlp) {
  80. if (!dflag)
  81. printf("%s: ", nm);
  82. printf("%s\n", nlp->name);
  83. nlp = nlp->next;
  84. }
  85. }
  86. /*ARGSUSED*/
  87. main(argc, argv)
  88. char *argv[];
  89. {
  90. int err = 0;
  91. prog = *argv++;
  92. if (*argv && **argv == '-') {
  93. char *opt = &(*argv++)[1];
  94. if (*opt++ != 'd' || *opt) {
  95. fprintf(stderr, "use: %s [-d] [file ...]\n", prog);
  96. exit(1);
  97. }
  98. dflag = 1;
  99. }
  100. while (*argv) {
  101. free_namelist(nl);
  102. nl = 0;
  103. if (dofile(*argv) == 0)
  104. ++err;
  105. print_namelist(*argv++, nl);
  106. }
  107. exit(err ? 1 : 0);
  108. }
  109. int
  110. contains_slash(s)
  111. register char *s;
  112. {
  113. while (*s)
  114. if (*s++ == '/') return 1;
  115. return 0;
  116. }
  117. extern char *fgets();
  118. dofile(fn)
  119. char *fn;
  120. {
  121. char buf[BSIZ];
  122. FILE *fp;
  123. char *nm, *include_line();
  124. if ((fp = fopen(fn, "r")) == 0) {
  125. fprintf(stderr, "%s: cannot read %s\n", prog, fn);
  126. return 0;
  127. }
  128. if (contains_slash(fn)) {
  129. fprintf(stderr, "%s: (warning) %s not in current directory; not checked\n", prog, fn);
  130. fclose(fp);
  131. return 1;
  132. }
  133. while (fgets(buf, BSIZ, fp) != NULL)
  134. if (nm = include_line(buf)) {
  135. add_name(nm);
  136. if (dofile(nm)) ;
  137. }
  138. fclose(fp);
  139. return 1;
  140. }
  141. char *
  142. include_line(s)
  143. char *s;
  144. {
  145. while ((*s == '\t') || (*s == ' '))
  146. s++;
  147. if (*s++ == '#') {
  148. while ((*s == '\t') || (*s == ' '))
  149. s++;
  150. if (
  151. (*s++ == 'i') &&
  152. (*s++ == 'n') &&
  153. (*s++ == 'c') &&
  154. (*s++ == 'l') &&
  155. (*s++ == 'u') &&
  156. (*s++ == 'd') &&
  157. (*s++ == 'e')
  158. ) {
  159. while ((*s == '\t') || (*s == ' '))
  160. s++;
  161. if (*s++ == '"') {
  162. char *nm = s;
  163. while (*s != 0 && *s != '"')
  164. s++;
  165. *s = '\0';
  166. return nm;
  167. }
  168. }
  169. }
  170. return (char *) 0;
  171. }