main.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 <alloc.h>
  8. #include <em_arith.h>
  9. #include <assert.h>
  10. #include <system.h>
  11. #include "file_info.h"
  12. #include "idfsize.h"
  13. #include "idf.h"
  14. #include "macro.h"
  15. extern char *symbol2str();
  16. extern char *getwdir();
  17. extern int err_occurred;
  18. extern int do_dependencies;
  19. extern char *dep_file;
  20. int idfsize = IDFSIZE;
  21. extern char options[];
  22. static File *dep_fd = STDOUT;
  23. arith ifval;
  24. char *prog_name;
  25. extern char **inctable;
  26. extern int inc_max, inc_total;
  27. main(argc, argv)
  28. char *argv[];
  29. {
  30. /* parse and interpret the command line options */
  31. prog_name = argv[0];
  32. init_idf();
  33. inctable = (char **) Malloc(10 * sizeof(char *));
  34. inc_max = 10;
  35. inc_total = 3;
  36. inctable[0] = "";
  37. inctable[1] = "/usr/include";
  38. inctable[2] = 0;
  39. init_pp(); /* initialise the preprocessor macros */
  40. /* Note: source file "-" indicates that the source is supplied
  41. as standard input. This is only allowed if INP_READ_IN_ONE is
  42. not defined!
  43. */
  44. while (argc > 1 && *argv[1] == '-' && argv[1][1] != '\0') {
  45. char *par = &argv[1][1];
  46. if (*par == '-')
  47. par++;
  48. do_option(par);
  49. argc--, argv++;
  50. }
  51. compile(argc - 1, &argv[1]);
  52. exit(err_occurred);
  53. }
  54. compile(argc, argv)
  55. char *argv[];
  56. {
  57. register char *source = 0;
  58. char *dummy;
  59. switch (argc) {
  60. case 1:
  61. source = argv[0];
  62. FileName = source;
  63. break;
  64. case 0:
  65. FileName = "";
  66. WorkingDir = "";
  67. break;
  68. default:
  69. FileName = argv[0];
  70. fatal("use: %s [options] [source]", prog_name);
  71. break;
  72. }
  73. if (!InsertFile(source, (char **) 0, &dummy)) /* read the source file */
  74. fatal("%s: no source file %s", prog_name,
  75. source ? source : "stdin");
  76. if (source) WorkingDir = getwdir(dummy);
  77. preprocess(source);
  78. if (do_dependencies) list_dependencies(source);
  79. }
  80. struct idf *file_head;
  81. extern char *strrindex();
  82. list_dependencies(source)
  83. char *source;
  84. {
  85. register struct idf *p = file_head;
  86. if (source) {
  87. register char *s = strrindex(source, '.');
  88. if (s && *(s+1)) {
  89. s++;
  90. *s++ = 'o';
  91. *s = '\0';
  92. /* the source may be in another directory than the
  93. * object generated, so don't include the pathname
  94. * leading to it.
  95. */
  96. if (s = strrindex(source, '/')) {
  97. source = s + 1;
  98. }
  99. }
  100. else source = 0;
  101. }
  102. if (dep_file && !sys_open(dep_file, OP_WRITE, &dep_fd)) {
  103. fatal("could not open %s", dep_file);
  104. }
  105. while (p) {
  106. assert(p->id_resmac == K_FILE);
  107. dependency(p->id_text, source);
  108. p = p->id_file;
  109. }
  110. }
  111. add_dependency(s)
  112. char *s;
  113. {
  114. register struct idf *p = str2idf(s, 0);
  115. if (! p->id_resmac) {
  116. p->id_resmac = K_FILE;
  117. p->id_file = file_head;
  118. file_head = p;
  119. }
  120. }
  121. dependency(s, source)
  122. char *s, *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. }