main.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. /* $Id$ */
  6. /* MAIN PROGRAM */
  7. #include "debug.h"
  8. #include <alloc.h>
  9. #include <assert.h>
  10. #include <system.h>
  11. #include <string.h>
  12. #include "arith.h"
  13. #include "file_info.h"
  14. #include "idfsize.h"
  15. #include "idf.h"
  16. #include "macro.h"
  17. #include "print.h"
  18. #include "LLlex.h"
  19. extern char *getwdir();
  20. extern int err_occurred;
  21. extern int do_dependencies;
  22. extern char *dep_file;
  23. int idfsize = IDFSIZE;
  24. extern char options[];
  25. static File *dep_fd = STDOUT;
  26. arith ifval;
  27. char *prog_name;
  28. extern char **inctable;
  29. extern int inc_max, inc_total;
  30. void compile(int argc, char *argv[]);
  31. void list_dependencies(char *source);
  32. void dependency(char *s, char *source);
  33. int main(int argc, char *argv[])
  34. {
  35. /* parse and interpret the command line options */
  36. prog_name = argv[0];
  37. init_idf();
  38. inctable = (char **) Malloc(10 * sizeof(char *));
  39. inc_max = 10;
  40. inc_total = 3;
  41. inctable[0] = ".";
  42. inctable[1] = 0;
  43. init_pp(); /* initialise the preprocessor macros */
  44. /* Note: source file "-" indicates that the source is supplied
  45. as standard input. This is only allowed if INP_READ_IN_ONE is
  46. not defined!
  47. */
  48. while (argc > 1 && *argv[1] == '-' && argv[1][1] != '\0') {
  49. char *par = &argv[1][1];
  50. if (*par == '-')
  51. par++;
  52. do_option(par);
  53. argc--, argv++;
  54. }
  55. compile(argc - 1, &argv[1]);
  56. sys_stop(err_occurred ? S_EXIT : S_END);
  57. /*NOTREACHED*/
  58. return -1;
  59. }
  60. void compile(int argc, char *argv[])
  61. {
  62. register char *source = 0;
  63. char *dummy;
  64. switch (argc) {
  65. case 1:
  66. source = argv[0];
  67. FileName = source;
  68. break;
  69. case 0:
  70. FileName = "";
  71. WorkingDir = "";
  72. break;
  73. default:
  74. FileName = argv[0];
  75. fatal("use: %s [options] [source]", prog_name);
  76. break;
  77. }
  78. if (!InsertFile(source, (char **) 0, &dummy)) /* read the source file */
  79. fatal("%s: no source file %s\n", prog_name,
  80. source ? source : "stdin");
  81. if (source) WorkingDir = getwdir(dummy);
  82. preprocess(source);
  83. if (do_dependencies) list_dependencies(source);
  84. }
  85. struct idf *file_head;
  86. void list_dependencies(char *source)
  87. {
  88. register struct idf *p = file_head;
  89. if (source) {
  90. register char *s = strrchr(source, '.');
  91. if (s && *(s+1)) {
  92. s++;
  93. *s++ = 'o';
  94. *s = '\0';
  95. /* the source may be in another directory than the
  96. * object generated, so don't include the pathname
  97. * leading to it.
  98. */
  99. if ( (s = strrchr(source, '/')) ) {
  100. source = s + 1;
  101. }
  102. }
  103. else source = 0;
  104. }
  105. if (dep_file && !sys_open(dep_file, OP_WRITE, &dep_fd)) {
  106. fatal("could not open %s", dep_file);
  107. }
  108. while (p) {
  109. assert(p->id_resmac == K_FILE);
  110. dependency(p->id_text, source);
  111. p = p->id_file;
  112. }
  113. }
  114. void add_dependency(char *s)
  115. {
  116. register struct idf *p = str2idf(s, 0);
  117. if (! p->id_resmac) {
  118. p->id_resmac = K_FILE;
  119. p->id_file = file_head;
  120. file_head = p;
  121. }
  122. }
  123. void dependency(char *s, char *source)
  124. {
  125. if (options['i'] && !strncmp(s, "/usr/include/", 13)) {
  126. return;
  127. }
  128. if (options['m'] && source) {
  129. fprint(dep_fd, "%s: %s\n", source, s);
  130. }
  131. else fprint(dep_fd, "%s\n", s);
  132. }
  133. void No_Mem() /* called by alloc package */
  134. {
  135. fatal("out of memory");
  136. }