include.c 3.2 KB

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