main.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 "file_info.h"
  14. #include "idfsize.h"
  15. #include "idf.h"
  16. #include "macro.h"
  17. extern char *symbol2str();
  18. extern char *getwdir();
  19. extern int err_occurred;
  20. extern int do_dependencies;
  21. extern char *dep_file;
  22. int idfsize = IDFSIZE;
  23. extern char options[];
  24. static File *dep_fd = STDOUT;
  25. arith ifval;
  26. void compile(int argc, char *argv[]);
  27. void list_dependencies(char *source);
  28. void dependency(char *s, char *source);
  29. char *prog_name;
  30. extern char **inctable;
  31. extern int inc_max, inc_total;
  32. int main(int argc, char *argv[])
  33. {
  34. /* parse and interpret the command line options */
  35. prog_name = argv[0];
  36. init_idf();
  37. inctable = (char **) Malloc(10 * sizeof(char *));
  38. inc_max = 10;
  39. inc_total = 3;
  40. inctable[0] = "";
  41. inctable[1] = "/usr/include";
  42. inctable[2] = 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. exit(err_occurred);
  57. return 0;
  58. }
  59. void compile(int argc, char *argv[])
  60. {
  61. register char *source = 0;
  62. char *dummy;
  63. switch (argc) {
  64. case 1:
  65. source = argv[0];
  66. FileName = source;
  67. break;
  68. case 0:
  69. FileName = "";
  70. WorkingDir = "";
  71. break;
  72. default:
  73. FileName = argv[0];
  74. fatal("use: %s [options] [source]", prog_name);
  75. break;
  76. }
  77. if (!InsertFile(source, (char **) 0, &dummy)) /* read the source file */
  78. fatal("%s: no source file %s", prog_name,
  79. source ? source : "stdin");
  80. if (source) WorkingDir = getwdir(dummy);
  81. preprocess(source);
  82. if (do_dependencies) list_dependencies(source);
  83. }
  84. struct idf *file_head;
  85. void list_dependencies(char *source)
  86. {
  87. register struct idf *p = file_head;
  88. if (source) {
  89. register char *s = strrchr(source, '.');
  90. if (s && *(s+1)) {
  91. s++;
  92. *s++ = 'o';
  93. *s = '\0';
  94. /* the source may be in another directory than the
  95. * object generated, so don't include the pathname
  96. * leading to it.
  97. */
  98. if ( (s = strrchr(source, '/')) ) {
  99. source = s + 1;
  100. }
  101. }
  102. else source = 0;
  103. }
  104. if (dep_file && !sys_open(dep_file, OP_WRITE, &dep_fd)) {
  105. fatal("could not open %s", dep_file);
  106. }
  107. while (p) {
  108. assert(p->id_resmac == K_FILE);
  109. dependency(p->id_text, source);
  110. p = p->id_file;
  111. }
  112. }
  113. void add_dependency(char *s)
  114. {
  115. register struct idf *p = str2idf(s, 0);
  116. if (! p->id_resmac) {
  117. p->id_resmac = K_FILE;
  118. p->id_file = file_head;
  119. file_head = p;
  120. }
  121. }
  122. void dependency(char *s, char *source)
  123. {
  124. if (options['i'] && !strncmp(s, "/usr/include/", 13)) {
  125. return;
  126. }
  127. if (options['m'] && source) {
  128. fprint(dep_fd, "%s: %s\n", source, s);
  129. }
  130. else fprint(dep_fd, "%s\n", s);
  131. }