include.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "cpp.h"
  4. Includelist includelist[NINCLUDE];
  5. extern char *objname;
  6. void
  7. doinclude(Tokenrow *trp)
  8. {
  9. char fname[256], iname[256];
  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>");
  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 ( Mflag>1 || !angled&&Mflag==1 ) {
  59. write(1,objname,strlen(objname));
  60. write(1,iname,strlen(iname));
  61. write(1,"\n",1);
  62. }
  63. if (fd >= 0) {
  64. if (++incdepth > 10)
  65. error(FATAL, "#include too deeply nested");
  66. setsource((char*)newstring((uchar*)iname, strlen(iname), 0), fd, NULL);
  67. genline();
  68. } else {
  69. trp->tp = trp->bp+2;
  70. error(ERROR, "Could not find include file %r", trp);
  71. }
  72. return;
  73. syntax:
  74. error(ERROR, "Syntax error in #include");
  75. return;
  76. }
  77. /*
  78. * Generate a line directive for cursource
  79. */
  80. void
  81. genline(void)
  82. {
  83. static Token ta = { UNCLASS };
  84. static Tokenrow tr = { &ta, &ta, &ta+1, 1 };
  85. uchar *p;
  86. ta.t = p = (uchar*)outp;
  87. strcpy((char*)p, "#line ");
  88. p += sizeof("#line ")-1;
  89. p = (uchar*)outnum((char*)p, cursource->line);
  90. *p++ = ' '; *p++ = '"';
  91. if (cursource->filename[0]!='/' && wd[0]) {
  92. strcpy((char*)p, wd);
  93. p += strlen(wd);
  94. *p++ = '/';
  95. }
  96. strcpy((char*)p, cursource->filename);
  97. p += strlen((char*)p);
  98. *p++ = '"'; *p++ = '\n';
  99. ta.len = (char*)p-outp;
  100. outp = (char*)p;
  101. tr.tp = tr.bp;
  102. puttokens(&tr);
  103. }
  104. void
  105. setobjname(char *f)
  106. {
  107. int n = strlen(f);
  108. objname = (char*)domalloc(n+5);
  109. strcpy(objname,f);
  110. if(objname[n-2]=='.'){
  111. strcpy(objname+n-1,"$O: ");
  112. }else{
  113. strcpy(objname+n,"$O: ");
  114. }
  115. }