aboutsummaryrefslogtreecommitdiffstats
path: root/code/tools/lcc/cpp
diff options
context:
space:
mode:
Diffstat (limited to 'code/tools/lcc/cpp')
-rw-r--r--code/tools/lcc/cpp/cpp.h3
-rw-r--r--code/tools/lcc/cpp/include.c27
-rw-r--r--code/tools/lcc/cpp/unix.c28
3 files changed, 44 insertions, 14 deletions
diff --git a/code/tools/lcc/cpp/cpp.h b/code/tools/lcc/cpp/cpp.h
index 203ab74..ae855c9 100644
--- a/code/tools/lcc/cpp/cpp.h
+++ b/code/tools/lcc/cpp/cpp.h
@@ -108,6 +108,7 @@ void control(Tokenrow *);
void dodefine(Tokenrow *);
void doadefine(Tokenrow *, int);
void doinclude(Tokenrow *);
+void appendDirToIncludeList( char *dir );
void doif(Tokenrow *, enum kwtype);
void expand(Tokenrow *, Nlist *);
void builtin(Tokenrow *, int);
@@ -140,6 +141,8 @@ void iniths(void);
void setobjname(char *);
#define rowlen(tokrow) ((tokrow)->lp - (tokrow)->bp)
+char *basepath( char *fname );
+
extern char *outp;
extern Token nltoken;
extern Source *cursource;
diff --git a/code/tools/lcc/cpp/include.c b/code/tools/lcc/cpp/include.c
index b3757e8..71bd90c 100644
--- a/code/tools/lcc/cpp/include.c
+++ b/code/tools/lcc/cpp/include.c
@@ -1,3 +1,4 @@
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cpp.h"
@@ -6,6 +7,29 @@ Includelist includelist[NINCLUDE];
extern char *objname;
+void appendDirToIncludeList( char *dir )
+{
+ int i;
+
+ //avoid adding it more than once
+ for (i=NINCLUDE-2; i>=0; i--) {
+ if (includelist[i].file &&
+ !strcmp (includelist[i].file, dir)) {
+ return;
+ }
+ }
+
+ for (i=NINCLUDE-2; i>=0; i--) {
+ if (includelist[i].file==NULL) {
+ includelist[i].always = 1;
+ includelist[i].file = dir;
+ break;
+ }
+ }
+ if (i<0)
+ error(FATAL, "Too many -I directives");
+}
+
void
doinclude(Tokenrow *trp)
{
@@ -44,6 +68,9 @@ doinclude(Tokenrow *trp)
if (trp->tp < trp->lp || len==0)
goto syntax;
fname[len] = '\0';
+
+ appendDirToIncludeList( basepath( fname ) );
+
if (fname[0]=='/') {
fd = open(fname, 0);
strcpy(iname, fname);
diff --git a/code/tools/lcc/cpp/unix.c b/code/tools/lcc/cpp/unix.c
index 749736c..f58b51e 100644
--- a/code/tools/lcc/cpp/unix.c
+++ b/code/tools/lcc/cpp/unix.c
@@ -29,15 +29,7 @@ setup(int argc, char **argv)
includelist[i].deleted = 1;
break;
case 'I':
- for (i=NINCLUDE-2; i>=0; i--) {
- if (includelist[i].file==NULL) {
- includelist[i].always = 1;
- includelist[i].file = optarg;
- break;
- }
- }
- if (i<0)
- error(FATAL, "Too many -I directives");
+ appendDirToIncludeList( optarg );
break;
case 'D':
case 'U':
@@ -66,11 +58,7 @@ setup(int argc, char **argv)
fp = "<stdin>";
fd = 0;
if (optind<argc) {
- if ((fp = strrchr(argv[optind], '/')) != NULL) {
- int len = fp - argv[optind];
- dp = (char*)newstring((uchar*)argv[optind], len+1, 0);
- dp[len] = '\0';
- }
+ dp = basepath( argv[optind] );
fp = (char*)newstring((uchar*)argv[optind], strlen(argv[optind]), 0);
if ((fd = open(fp, 0)) <= 0)
error(FATAL, "Can't open input file %s", fp);
@@ -89,6 +77,18 @@ setup(int argc, char **argv)
}
+char *basepath( char *fname )
+{
+ char *dp = ".";
+ char *p;
+ if ((p = strrchr(fname, '/')) != NULL) {
+ int dlen = p - fname;
+ dp = (char*)newstring((uchar*)fname, dlen+1, 0);
+ dp[dlen] = '\0';
+ }
+
+ return dp;
+}
/* memmove is defined here because some vendors don't provide it at
all and others do a terrible job (like calling malloc) */