input.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Parse a makefile
  3. *
  4. * $Header$
  5. */
  6. #include <stdio.h>
  7. #include <ctype.h>
  8. #include "h.h"
  9. struct name namehead;
  10. struct name * firstname;
  11. char str1[LZ]; /* General store */
  12. char str2[LZ];
  13. /*
  14. * Intern a name. Return a pointer to the name struct
  15. */
  16. struct name *
  17. newname(name)
  18. char * name;
  19. {
  20. register struct name * rp;
  21. register struct name * rrp;
  22. register char * cp;
  23. for
  24. (
  25. rp = namehead.n_next, rrp = &namehead;
  26. rp;
  27. rp = rp->n_next, rrp = rrp->n_next
  28. )
  29. if (strcmp(name, rp->n_name) == 0)
  30. return rp;
  31. if ((rp = (struct name *)malloc(sizeof (struct name)))
  32. == (struct name *)0)
  33. fatal("No memory for name");
  34. rrp->n_next = rp;
  35. rp->n_next = (struct name *)0;
  36. if ((cp = malloc((unsigned)(strlen(name)+1))) == (char *)0)
  37. fatal("No memory for name");
  38. strcpy(cp, name);
  39. rp->n_name = cp;
  40. rp->n_line = (struct line *)0;
  41. rp->n_time = (time_t)0;
  42. rp->n_flag = 0;
  43. return rp;
  44. }
  45. /*
  46. * Add a dependant to the end of the supplied list of dependants.
  47. * Return the new head pointer for that list.
  48. */
  49. struct depend *
  50. newdep(np, dp)
  51. struct name * np;
  52. struct depend * dp;
  53. {
  54. register struct depend * rp;
  55. register struct depend * rrp;
  56. if ((rp = (struct depend *)malloc(sizeof (struct depend)))
  57. == (struct depend *)0)
  58. fatal("No memory for dependant");
  59. rp->d_next = (struct depend *)0;
  60. rp->d_name = np;
  61. if (dp == (struct depend *)0)
  62. return rp;
  63. for (rrp = dp; rrp->d_next; rrp = rrp->d_next)
  64. ;
  65. rrp->d_next = rp;
  66. return dp;
  67. }
  68. /*
  69. * Add a command to the end of the supplied list of commands.
  70. * Return the new head pointer for that list.
  71. */
  72. struct cmd *
  73. newcmd(str, cp)
  74. char * str;
  75. struct cmd * cp;
  76. {
  77. register struct cmd * rp;
  78. register struct cmd * rrp;
  79. register char * rcp;
  80. if (rcp = rindex(str, '\n'))
  81. *rcp = '\0'; /* Loose newline */
  82. while (isspace(*str))
  83. str++;
  84. if (*str == '\0') /* If nothing left, the exit */
  85. return 0;
  86. if ((rp = (struct cmd *)malloc(sizeof (struct cmd)))
  87. == (struct cmd *)0)
  88. fatal("No memory for command");
  89. rp->c_next = (struct cmd *)0;
  90. if ((rcp = malloc((unsigned)(strlen(str)+1))) == (char *)0)
  91. fatal("No memory for command");
  92. strcpy(rcp, str);
  93. rp->c_cmd = rcp;
  94. if (cp == (struct cmd *)0)
  95. return rp;
  96. for (rrp = cp; rrp->c_next; rrp = rrp->c_next)
  97. ;
  98. rrp->c_next = rp;
  99. return cp;
  100. }
  101. /*
  102. * Add a new 'line' of stuff to a target. This check to see
  103. * if commands already exist for the target. If flag is set,
  104. * the line is a double colon target.
  105. *
  106. * Kludges:
  107. * i) If the new name begins with a '.', and there are no dependents,
  108. * then the target must cease to be a target. This is for .SUFFIXES.
  109. * ii) If the new name begins with a '.', with no dependents and has
  110. * commands, then replace the current commands. This is for
  111. * redefining commands for a default rule.
  112. * Neither of these free the space used by dependents or commands,
  113. * since they could be used by another target.
  114. */
  115. void
  116. newline(np, dp, cp, flag)
  117. struct name * np;
  118. struct depend * dp;
  119. struct cmd * cp;
  120. {
  121. bool hascmds = FALSE; /* Target has commands */
  122. register struct line * rp;
  123. register struct line * rrp;
  124. /* Handle the .SUFFIXES case */
  125. if (! strcmp(np->n_name, ".SUFFIXES") && !dp && !cp)
  126. {
  127. for (rp = np->n_line; rp; rp = rrp)
  128. {
  129. rrp = rp->l_next;
  130. free((char *)rp);
  131. }
  132. np->n_line = (struct line *)0;
  133. np->n_flag &= ~N_TARG;
  134. return;
  135. }
  136. /* This loop must happen since rrp is used later. */
  137. for
  138. (
  139. rp = np->n_line, rrp = (struct line *)0;
  140. rp;
  141. rrp = rp, rp = rp->l_next
  142. )
  143. if (rp->l_cmd)
  144. hascmds = TRUE;
  145. if (hascmds && cp && !(np->n_flag & N_DOUBLE))
  146. /* Handle the implicit rules redefinition case */
  147. if (np->n_name[0] == '.' && dp == (struct depend *)0)
  148. {
  149. np->n_line->l_cmd = cp;
  150. return;
  151. }
  152. else
  153. error("Commands defined twice for target %s", np->n_name);
  154. if (np->n_flag & N_TARG)
  155. if (!(np->n_flag & N_DOUBLE) != !flag) /* like xor */
  156. error("Inconsistent rules for target %s", np->n_name);
  157. if ((rp = (struct line *)malloc(sizeof (struct line)))
  158. == (struct line *)0)
  159. fatal("No memory for line");
  160. rp->l_next = (struct line *)0;
  161. rp->l_dep = dp;
  162. rp->l_cmd = cp;
  163. if (rrp)
  164. rrp->l_next = rp;
  165. else
  166. np->n_line = rp;
  167. np->n_flag |= N_TARG;
  168. if (flag)
  169. np->n_flag |= N_DOUBLE;
  170. }
  171. /*
  172. * Parse input from the makefile, and construct a tree structure
  173. * of it.
  174. */
  175. void
  176. input(fd)
  177. FILE * fd;
  178. {
  179. char * p; /* General */
  180. char * q;
  181. struct name * np;
  182. struct depend * dp;
  183. struct cmd * cp;
  184. bool dbl;
  185. if (getline(str1, fd)) /* Read the first line */
  186. return;
  187. for(;;)
  188. {
  189. #ifdef os9
  190. if (*str1 == ' ') /* Rules without targets */
  191. #else
  192. if (*str1 == '\t') /* Rules without targets */
  193. #endif
  194. error("Rules not allowed here");
  195. p = str1;
  196. while (isspace(*p)) /* Find first target */
  197. p++;
  198. while (((q = index(p, '=')) != (char *)0) &&
  199. (p != q) && (q[-1] == '\\')) /* Find value */
  200. {
  201. register char * a;
  202. a = q - 1; /* Del \ chr; move rest back */
  203. p = q;
  204. while(*a++ = *q++)
  205. ;
  206. }
  207. if (q != (char *)0)
  208. {
  209. register char * a;
  210. *q++ = '\0'; /* Separate name and val */
  211. while (isspace(*q))
  212. q++;
  213. if (p = rindex(q, '\n'))
  214. *p = '\0';
  215. p = str1;
  216. if ((a = gettok(&p)) == (char *)0)
  217. error("No macro name");
  218. setmacro(a, q, 2);
  219. if (getline(str1, fd))
  220. return;
  221. continue;
  222. }
  223. expand(str1);
  224. p = str1;
  225. while (((q = index(p, ':')) != (char *)0) &&
  226. (p != q) && (q[-1] == '\\')) /* Find dependents */
  227. {
  228. register char * a;
  229. a = q - 1; /* Del \ chr; move rest back */
  230. p = q;
  231. while(*a++ = *q++)
  232. ;
  233. }
  234. if (q == (char *)0)
  235. error("No targets provided");
  236. *q++ = '\0'; /* Separate targets and dependents */
  237. if (*q == ':') /* Double colon */
  238. {
  239. dbl = 1;
  240. q++;
  241. }
  242. else
  243. dbl = 0;
  244. for (dp = (struct depend *)0; ((p = gettok(&q)) != (char *)0);)
  245. /* get list of dep's */
  246. {
  247. np = newname(p); /* Intern name */
  248. dp = newdep(np, dp); /* Add to dep list */
  249. }
  250. *((q = str1) + strlen(str1) + 1) = '\0';
  251. /* Need two nulls for gettok (Remember separation) */
  252. cp = (struct cmd *)0;
  253. if (getline(str2, fd) == FALSE) /* Get commands */
  254. {
  255. #ifdef os9
  256. while (*str2 == ' ')
  257. #else
  258. while (*str2 == '\t')
  259. #endif
  260. {
  261. cp = newcmd(&str2[0], cp);
  262. if (getline(str2, fd))
  263. break;
  264. }
  265. }
  266. while ((p = gettok(&q)) != (char *)0) /* Get list of targ's */
  267. {
  268. np = newname(p); /* Intern name */
  269. newline(np, dp, cp, dbl);
  270. if (!firstname && p[0] != '.')
  271. firstname = np;
  272. }
  273. if (feof(fd)) /* EOF? */
  274. return;
  275. strcpy(str1, str2);
  276. }
  277. }