main.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * make [-f makefile] [-ins] [target(s) ...]
  3. *
  4. * (Better than EON mk but not quite as good as UNIX make)
  5. *
  6. * -f makefile name
  7. * -i ignore exit status
  8. * -n Pretend to make
  9. * -p Print all macros & targets
  10. * -q Question up-to-dateness of target. Return exit status 1 if not
  11. * -r Don't not use inbuilt rules
  12. * -s Make silently
  13. * -t Touch files instead of making them
  14. * -m Change memory requirements (EON only)
  15. * -k For the time being: accept but ignore
  16. *
  17. * $Header$
  18. */
  19. #include <stdio.h>
  20. #include "h.h"
  21. #ifdef unix
  22. #include <errno.h>
  23. #endif
  24. #ifdef eon
  25. #include <sys/err.h>
  26. #endif
  27. #ifdef os9
  28. #include <errno.h>
  29. #endif
  30. #ifdef eon
  31. #define MEMSPACE (16384)
  32. #endif
  33. char * myname;
  34. char * makefile; /* The make file */
  35. #ifdef eon
  36. unsigned memspace = MEMSPACE;
  37. #endif
  38. FILE * ifd; /* Input file desciptor */
  39. bool domake = TRUE; /* Go through the motions option */
  40. bool ignore = FALSE; /* Ignore exit status option */
  41. bool silent = FALSE; /* Silent option */
  42. bool print = FALSE; /* Print debuging information */
  43. bool rules = TRUE; /* Use inbuilt rules */
  44. bool dotouch = FALSE;/* Touch files instead of making */
  45. bool quest = FALSE; /* Question up-to-dateness of file */
  46. void
  47. main(argc, argv)
  48. int argc;
  49. char ** argv;
  50. {
  51. register char * p; /* For argument processing */
  52. int estat = 0; /* For question */
  53. register struct name * np;
  54. int nargc = 0;
  55. char **nargv;
  56. int fflag = 0;
  57. myname = (argc-- < 1) ? "make" : *argv++;
  58. nargv = argv;
  59. while (argc > 0)
  60. {
  61. argc--; /* One less to process */
  62. p = *argv++; /* Now processing this one */
  63. if (*p == '-') while (*++p != '\0')
  64. {
  65. switch(*p)
  66. {
  67. case 'f': /* Alternate file name */
  68. fflag = 1;
  69. break;
  70. #ifdef eon
  71. case 'm': /* Change space requirements */
  72. if (*++p == '\0')
  73. {
  74. if (argc-- <= 0)
  75. usage();
  76. p = *argv++;
  77. }
  78. memspace = atoi(p);
  79. goto end_of_args;
  80. #endif
  81. case 'n': /* Pretend mode */
  82. domake = FALSE;
  83. break;
  84. case 'i': /* Ignore fault mode */
  85. ignore = TRUE;
  86. break;
  87. case 's': /* Silent about commands */
  88. silent = TRUE;
  89. break;
  90. case 'p':
  91. print = TRUE;
  92. break;
  93. case 'r':
  94. rules = FALSE;
  95. break;
  96. case 't':
  97. dotouch = TRUE;
  98. break;
  99. case 'q':
  100. quest = TRUE;
  101. break;
  102. case 'k':
  103. break;
  104. default: /* Wrong option */
  105. usage();
  106. }
  107. }
  108. else {
  109. if (fflag) {
  110. if (argc <= 0) usage();
  111. makefile = p;
  112. fflag = 0;
  113. }
  114. else {
  115. nargc++;
  116. *nargv++ = p;
  117. }
  118. }
  119. end_of_args:;
  120. }
  121. argv = nargv - nargc;
  122. argc = nargc;
  123. #ifdef eon
  124. if (initalloc(memspace) == 0xffff) /* Must get memory for alloc */
  125. fatal("Cannot initalloc memory");
  126. #endif
  127. if (makefile && strcmp(makefile, "-") == 0) /* Can use stdin as makefile */
  128. ifd = stdin;
  129. else
  130. if (!makefile) /* If no file, then use default */
  131. {
  132. if ((ifd = fopen(DEFN1, "r")) == (FILE *)0)
  133. #ifdef eon
  134. if (errno != ER_NOTF)
  135. fatal("Can't open %s; error %02x", DEFN1, errno);
  136. #endif
  137. #ifdef unix
  138. if (errno != ENOENT)
  139. fatal("Can't open %s; error %02x", DEFN1, errno);
  140. #endif
  141. #ifndef os9
  142. if ((ifd == (FILE *)0)
  143. && ((ifd = fopen(DEFN2, "r")) == (FILE *)0))
  144. fatal("Can't open %s", DEFN2);
  145. #else
  146. fatal("Can't open %s", DEFN1);
  147. #endif
  148. }
  149. else
  150. if ((ifd = fopen(makefile, "r")) == (FILE *)0)
  151. fatal("Can't open %s", makefile);
  152. makerules();
  153. setmacro("$", "$", 4);
  154. while (argc && (p = index(*argv, '=')))
  155. {
  156. char c;
  157. c = *p;
  158. *p = '\0';
  159. setmacro(*argv, p+1, 3);
  160. *p = c;
  161. argv++;
  162. argc--;
  163. }
  164. input(ifd); /* Input all the gunga */
  165. fclose(ifd); /* Finished with makefile */
  166. lineno = 0; /* Any calls to error now print no line number */
  167. if (print)
  168. prt(); /* Print out structures */
  169. np = newname(".SILENT");
  170. if (np->n_flag & N_TARG)
  171. silent = TRUE;
  172. np = newname(".IGNORE");
  173. if (np->n_flag & N_TARG)
  174. ignore = TRUE;
  175. precious();
  176. if (!firstname)
  177. fatal("No targets defined");
  178. circh(); /* Check circles in target definitions */
  179. if (!argc)
  180. estat = make(firstname, 0);
  181. else while (argc--)
  182. {
  183. if (!print && !silent && strcmp(*argv, "love") == 0)
  184. printf("Not war!\n");
  185. estat |= make(newname(*argv++), 0);
  186. }
  187. if (quest)
  188. exit(estat);
  189. else
  190. exit(0);
  191. }
  192. usage()
  193. {
  194. fprintf(stderr, "Usage: %s [-f makefile] [-inpqrst] [macro=val ...] [target(s) ...]\n", myname);
  195. exit(1);
  196. }
  197. /*VARARGS1*/
  198. void
  199. fatal(msg, a1, a2, a3, a4, a5, a6)
  200. char *msg;
  201. {
  202. fprintf(stderr, "%s: ", myname);
  203. fprintf(stderr, msg, a1, a2, a3, a4, a5, a6);
  204. fputc('\n', stderr);
  205. exit(1);
  206. }
  207. char *
  208. index(s, c)
  209. register char *s, c;
  210. {
  211. while (*s)
  212. if (*s++ == c)
  213. return --s;
  214. return (char *)0;
  215. }
  216. char *
  217. rindex(str, chr)
  218. register char *str, chr;
  219. {
  220. register char *retptr = 0;
  221. while (*str)
  222. if (*str++ == chr)
  223. retptr = &str[-1];
  224. return retptr;
  225. }