include.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include <u.h>
  2. #include <libc.h>
  3. #include "cpp.h"
  4. Includelist includelist[NINCLUDE];
  5. char *objname;
  6. void
  7. doinclude(Tokenrow *trp)
  8. {
  9. char fname[256], iname[256], *p;
  10. Includelist *ip;
  11. int angled, len, fd, i;
  12. trp->tp += 1;
  13. if (trp->tp>=trp->lp)
  14. goto syntax;
  15. if (trp->tp->type!=STRING && trp->tp->type!=LT) {
  16. len = trp->tp - trp->bp;
  17. expandrow(trp, "<include>", Notinmacro);
  18. trp->tp = trp->bp+len;
  19. }
  20. if (trp->tp->type==STRING) {
  21. len = trp->tp->len-2;
  22. if (len > sizeof(fname) - 1)
  23. len = sizeof(fname) - 1;
  24. strncpy(fname, (char*)trp->tp->t+1, len);
  25. angled = 0;
  26. } else if (trp->tp->type==LT) {
  27. len = 0;
  28. trp->tp++;
  29. while (trp->tp->type!=GT) {
  30. if (trp->tp>trp->lp || len+trp->tp->len+2 >= sizeof(fname))
  31. goto syntax;
  32. strncpy(fname+len, (char*)trp->tp->t, trp->tp->len);
  33. len += trp->tp->len;
  34. trp->tp++;
  35. }
  36. angled = 1;
  37. } else
  38. goto syntax;
  39. trp->tp += 2;
  40. if (trp->tp < trp->lp || len==0)
  41. goto syntax;
  42. fname[len] = '\0';
  43. if (fname[0]=='/') {
  44. fd = open(fname, 0);
  45. strcpy(iname, fname);
  46. } else for (fd=-1,i=NINCLUDE-1; i>=0; i--) {
  47. ip = &includelist[i];
  48. if (ip->file==NULL || ip->deleted || (angled && ip->always==0))
  49. continue;
  50. if (strlen(fname)+strlen(ip->file)+2 > sizeof(iname))
  51. continue;
  52. strcpy(iname, ip->file);
  53. strcat(iname, "/");
  54. strcat(iname, fname);
  55. if ((fd = open(iname, 0)) >= 0)
  56. break;
  57. }
  58. if(fd < 0) {
  59. strcpy(iname, cursource->filename);
  60. p = strrchr(iname, '/');
  61. if(p != NULL) {
  62. *p = '\0';
  63. strcat(iname, "/");
  64. strcat(iname, fname);
  65. fd = open(iname, 0);
  66. }
  67. }
  68. if ( Mflag>1 || !angled&&Mflag==1 ) {
  69. write(1,objname,strlen(objname));
  70. write(1,iname,strlen(iname));
  71. write(1,"\n",1);
  72. }
  73. if (fd >= 0) {
  74. if (++incdepth > 20)
  75. error(FATAL, "#include too deeply nested");
  76. setsource((char*)newstring((uchar*)iname, strlen(iname), 0), fd, NULL);
  77. genline();
  78. } else {
  79. trp->tp = trp->bp+2;
  80. error(ERROR, "Could not find include file %r", trp);
  81. }
  82. return;
  83. syntax:
  84. error(ERROR, "Syntax error in #include");
  85. return;
  86. }
  87. /*
  88. * Generate a line directive for cursource
  89. */
  90. void
  91. genline(void)
  92. {
  93. static Token ta = { UNCLASS, NULL, 0, 0 };
  94. static Tokenrow tr = { &ta, &ta, &ta+1, 1 };
  95. uchar *p;
  96. if(nolineinfo)
  97. return;
  98. ta.t = p = (uchar*)outp;
  99. strcpy((char*)p, "#line ");
  100. p += sizeof("#line ")-1;
  101. p = (uchar*)outnum((char*)p, cursource->line);
  102. *p++ = ' '; *p++ = '"';
  103. if (cursource->filename[0]!='/' && wd[0]) {
  104. strcpy((char*)p, wd);
  105. p += strlen(wd);
  106. *p++ = '/';
  107. }
  108. strcpy((char*)p, cursource->filename);
  109. p += strlen((char*)p);
  110. *p++ = '"'; *p++ = '\n';
  111. ta.len = (char*)p-outp;
  112. outp = (char*)p;
  113. tr.tp = tr.bp;
  114. puttokens(&tr);
  115. }
  116. void
  117. setobjname(char *f)
  118. {
  119. int n = strlen(f);
  120. objname = (char*)domalloc(n+5);
  121. strcpy(objname,f);
  122. if(objname[n-2]=='.'){
  123. strcpy(objname+n-1,"$O: ");
  124. }else{
  125. strcpy(objname+n,"$O: ");
  126. }
  127. }