main.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. /* MAIN PROGRAM */
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <alloc.h>
  10. #include <em_arith.h>
  11. #include <assert.h>
  12. #include <system.h>
  13. #include "LLlex.h"
  14. #include "file_info.h"
  15. #include "idfsize.h"
  16. #include "idf.h"
  17. #include "macro.h"
  18. extern char *symbol2str();
  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. void compile(int argc, char *argv[]);
  28. void list_dependencies(char *source);
  29. void dependency(char *s, char *source);
  30. char *prog_name;
  31. extern char **inctable;
  32. extern int inc_max, inc_total;
  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] = "/usr/include";
  43. inctable[2] = 0;
  44. init_pp(); /* initialise the preprocessor macros */
  45. /* Note: source file "-" indicates that the source is supplied
  46. as standard input. This is only allowed if INP_READ_IN_ONE is
  47. not defined!
  48. */
  49. while (argc > 1 && *argv[1] == '-' && argv[1][1] != '\0') {
  50. char *par = &argv[1][1];
  51. if (*par == '-')
  52. par++;
  53. do_option(par);
  54. argc--, argv++;
  55. }
  56. compile(argc - 1, &argv[1]);
  57. exit(err_occurred);
  58. return 0;
  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", 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. }