main.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 "arith.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] = (char *) 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. sys_stop(err_occurred ? S_EXIT : S_END);
  54. /*NOTREACHED*/
  55. }
  56. compile(argc, argv)
  57. char *argv[];
  58. {
  59. register char *source = 0;
  60. char *dummy;
  61. switch (argc) {
  62. case 1:
  63. source = argv[0];
  64. FileName = source;
  65. break;
  66. case 0:
  67. FileName = "";
  68. WorkingDir = "";
  69. break;
  70. default:
  71. FileName = argv[0];
  72. fatal("use: %s [options] [source]", prog_name);
  73. break;
  74. }
  75. if (!InsertFile(source, (char **) 0, &dummy)) /* read the source file */
  76. fatal("%s: no source file %s\n", prog_name,
  77. source ? source : "stdin");
  78. if (source) WorkingDir = getwdir(dummy);
  79. preprocess(source);
  80. if (do_dependencies) list_dependencies(source);
  81. }
  82. struct idf *file_head;
  83. extern char *strrchr();
  84. list_dependencies(source)
  85. 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. add_dependency(s)
  114. 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. dependency(s, source)
  124. char *s, *source;
  125. {
  126. if (options['i'] && !strncmp(s, "/usr/include/", 13)) {
  127. return;
  128. }
  129. if (options['m'] && source) {
  130. fprint(dep_fd, "%s: %s\n", source, s);
  131. }
  132. else fprint(dep_fd, "%s\n", s);
  133. }
  134. void
  135. No_Mem() /* called by alloc package */
  136. {
  137. fatal("out of memory");
  138. }