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