Index: cccp.c
===================================================================
--- cccp.c	(revision 533)
+++ cccp.c	(revision 590)
@@ -4,6 +4,10 @@
    Written by Paul Rubin, June 1986
    Adapted to ANSI C, Richard Stallman, Jan 1987
 
+Modfied for use as cxref-cpp by Andrew M. Bishop 2004.
+Changes marked with 'AMB' in the code.
+
+
 This program is free software; you can redistribute it and/or modify it
 under the terms of the GNU General Public License as published by the
 Free Software Foundation; either version 2, or (at your option) any
@@ -373,6 +377,9 @@
     char fname[1];
   };
 
+#define GCC_INCLUDE_DIR "." /* Not used - AMB */
+#define INCLUDE_DEFAULTS { { 0, 0, 0, 0, 0 } } /* No built-in include dirs - AMB */
+
 /* #include "file" looks in source file dir, then stack.  */
 /* #include <file> just looks in the stack.  */
 /* -I directories are added to the end, then the defaults are added.  */
@@ -457,6 +464,9 @@
    include file directories.  */
 static char *include_prefix;
 
+/* File to include that contains the gcc definitions for cxref-cpp - AMB */
+static char *cxref_cpp_defines=CXREF_CPP_DEFINES;
+
 /* Maintain and search list of included files.  */
 
 struct include_file {
@@ -1024,7 +1034,8 @@
    retrying if necessary.  If MAX_READ_LEN is defined, read at most
    that bytes at a time.  Return a negative value if an error occurs,
    otherwise return the actual number of bytes read,
-   which must be LEN unless end-of-file was reached.  */
+   which may be < LEN if CRs have been skipped, though we try not to do
+   that.  */
 
 static int
 safe_read (desc, ptr, len)
@@ -1033,6 +1044,7 @@
      int len;
 {
   int left, rcount, nchars;
+  char *rptr;
 
   left = len;
   while (left > 0) {
@@ -1052,8 +1064,20 @@
       }
     if (nchars == 0)
       break;
-    ptr += nchars;
+
+    /* CRLF pairs, found with Unix when processing DOS files,
+       throw off backslash-newline removal.
+       Therefore, CRs are thrown away here. */
     left -= nchars;
+    rptr = ptr;
+    while(nchars--)
+      {
+	if(*rptr == '\r' && *(rptr+1) == '\n')
+	  left++;
+	else
+	  *ptr++ = *rptr;
+	rptr++;
+      }
   }
   return len - left;
 }
@@ -1707,6 +1731,17 @@
 	}
 	break;
 
+/* Option for runtime cxref-cpp configuration file - AMB */
+      case 'c':
+	if (!strcmp (argv[i], "-cxref-cpp-defines")) {
+	  if (i + 1 == argc)
+	    fatal ("Filename missing after `-cxref-cpp-defines' option");
+	  else
+	    cxref_cpp_defines = argv[i+1], i++;
+	}
+	break;
+/* Option for runtime cxref-cpp configuration file - AMB */
+
       case 'n':
 	if (!strcmp (argv[i], "-nostdinc"))
 	  /* -nostdinc causes no default include directories.
@@ -1774,6 +1809,83 @@
      and option processing.  */
   initialize_builtins (fp, &outbuf);
 
+  /* Start of setup for the cxref paths and definitions from runtime config - AMB */
+
+  {
+   int fd;
+   struct stat sbuf;
+   char *buf,*p;
+   struct include_file *inc;
+
+   if(stat(cxref_cpp_defines,&sbuf)<0)
+     {perror_with_name(cxref_cpp_defines); exit(FATAL_EXIT_CODE);}
+
+   if(!S_ISREG(sbuf.st_mode))
+     {perror_with_name(cxref_cpp_defines); exit(FATAL_EXIT_CODE);}
+
+   fd=open_include_file(cxref_cpp_defines, NULL_PTR, NULL_PTR, &inc);
+   if(fd==-1)
+     {perror_with_name(cxref_cpp_defines); exit(FATAL_EXIT_CODE);}
+
+   buf=(char *)xmalloc(sbuf.st_size+2); /* AMB - remove gcc-4.0 warning */
+
+   if(safe_read(fd,buf,sbuf.st_size)<0)
+     {perror_with_name(cxref_cpp_defines); exit(FATAL_EXIT_CODE);}
+
+   p=buf;
+
+   while(!strncmp(p,"//",2))
+     {
+      char *oldp=p;
+
+      while(*p && (*p!='\r' && *p!='\n')) p++;
+      while(*p && (*p=='\r' || *p=='\n')) *p++=0;
+
+      if(!strncmp(oldp,"// -",4) && *(oldp+=4))
+         switch(*oldp)
+           {
+           case 'A':
+            make_assertion("-A",oldp+1);
+            break;
+
+           case 'D':
+            make_definition(oldp+1);
+            break;
+
+           case 'I': /* treat like -isystem */
+            {
+             struct file_name_list *dirtmp;
+
+             dirtmp = new_include_prefix(NULL_PTR,NULL_PTR,"",oldp+1);
+             if(!dirtmp)
+                fatal ("File %s specifies invalid -I path",cxref_cpp_defines);
+
+             dirtmp->c_system_include_path = 1;
+
+             if (before_system == 0)
+                before_system = dirtmp;
+             else
+                last_before_system->next = dirtmp;
+             last_before_system = dirtmp; /* Tail follows the last one */
+            }
+            break;
+
+           default:
+            ;
+           }
+     }
+
+   free(buf);
+
+   lseek(fd,0,SEEK_SET);
+
+   no_output++; no_record_file++;
+   finclude(fd,inc,&outbuf,0,NULL_PTR);
+   no_output--; no_record_file--;
+  }
+
+  /* End of setup for the cxref paths and definitions from runtime config - AMB */
+
   /* Now handle the command line options.  */
 
   /* Do -U's, -D's and -A's in the order they were seen.  */
@@ -2086,8 +2198,8 @@
     for (;;) {
       cnt = safe_read (f, (char *) fp->buf + size, bsize - size);
       if (cnt < 0) goto perror;	/* error! */
+      if (cnt == 0) break; /* End of file */
       size += cnt;
-      if (size != bsize) break;	/* End of file */
       bsize *= 2;
       fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
     }
@@ -2421,6 +2533,7 @@
   register U_CHAR *numptr;	/* temp for arg parsing */
 
   *arglen = 0;
+  *argstart=NULL; /* AMB - remove gcc-4.0 warning */
 
   SKIP_WHITE_SPACE (ibp);
 
@@ -5027,6 +5140,8 @@
 	  map_list_ptr->map_list_map = ptr;
 
 	  while ((ch = getc (f)) != '\n')
+	    if (ch == '\r')
+		continue;
 	    if (ch == EOF)
 	      break;
 	}
@@ -5259,9 +5374,9 @@
       i = safe_read (f, (char *) fp->buf + st_size, bsize - st_size);
       if (i < 0)
 	goto nope;      /* error! */
+      if (i == 0)
+	break;	/* End of file */
       st_size += i;
-      if (st_size != bsize)
-	break;	/* End of file */
       bsize *= 2;
       fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
     }
@@ -5881,6 +5996,8 @@
   return mdef;
 
  nope:
+  mdef.symnam = NULL; /* AMB - remove gcc-4.0 warning */
+  mdef.symlen = 0;    /* AMB - remove gcc-4.0 warning */
   mdef.defn = 0;
   return mdef;
 }
@@ -10102,6 +10219,7 @@
   install ((U_CHAR *) "__FILE__", -1, T_FILE, NULL_PTR, -1);
   install ((U_CHAR *) "__BASE_FILE__", -1, T_BASE_FILE, NULL_PTR, -1);
   install ((U_CHAR *) "__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, NULL_PTR, -1);
+  /* Don't setup the standard #defines - AMB
   install ((U_CHAR *) "__VERSION__", -1, T_VERSION, NULL_PTR, -1);
 #ifndef NO_BUILTIN_SIZE_TYPE
   install ((U_CHAR *) "__SIZE_TYPE__", -1, T_SIZE_TYPE, NULL_PTR, -1);
@@ -10116,6 +10234,7 @@
 	   NULL_PTR, -1);
   install ((U_CHAR *) "__IMMEDIATE_PREFIX__", -1, T_IMMEDIATE_PREFIX_TYPE,
 	   NULL_PTR, -1);
+  */
   install ((U_CHAR *) "__TIME__", -1, T_TIME, NULL_PTR, -1);
   if (!traditional) {
     install ((U_CHAR *) "__STDC__", -1, T_CONST, "1", -1);
@@ -10124,7 +10243,9 @@
 /*  This is supplied using a -D by the compiler driver
     so that it is present only when truly compiling with GNU C.  */
 /*  install ((U_CHAR *) "__GNUC__", -1, T_CONST, "2", -1);  */
+  /* Don't setup the standard #defines - AMB
   install ((U_CHAR *) "__HAVE_BUILTIN_SETJMP__", -1, T_CONST, "1", -1);
+  */
 
   if (debug_output)
     {
@@ -10139,6 +10260,7 @@
       pass_thru_directive (udirective, &udirective[strlen (directive)],
 			   outp, dp);
 
+      /* Don't setup the standard #defines - AMB
       sprintf (directive, " __VERSION__ \"%s\"\n", version_string);
       output_line_directive (inp, outp, 0, same_file);
       pass_thru_directive (udirective, &udirective[strlen (directive)],
@@ -10162,6 +10284,7 @@
       output_line_directive (inp, outp, 0, same_file);
       pass_thru_directive (udirective, &udirective[strlen (directive)],
 			   outp, dp);
+      */
 
       sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
 	       monthnames[timebuf->tm_mon],
@@ -10324,7 +10447,7 @@
   U_CHAR *buf, *p, *q;
 
   /* Copy the entire option so we can modify it.  */
-  buf = (U_CHAR *) alloca (strlen (str) + 1);
+  buf = (U_CHAR *) alloca (strlen (str) + 2); /* Change to handle gcc 3.x "-Afoo=bar" - AMB */
   strcpy ((char *) buf, str);
   /* Scan for any backslash-newline and remove it.  */
   p = q = buf;
@@ -10344,10 +10467,18 @@
   while (is_idchar[*++p])
     ;
   SKIP_WHITE_SPACE (p);
-  if (! (*p == 0 || *p == '(')) {
+  if (! (*p == 0 || *p == '(' || *p == '=')) { /* Change to handle gcc 3.x "-Afoo=bar" - AMB */
     error ("malformed option `%s %s'", option, str);
     return;
   }
+
+/* Start change to handle gcc 3.x "-Afoo=bar" - AMB */
+  if(*p=='=')
+    {
+     *p='(';
+     strcat((char*)p,")");
+    }
+/* End change to handle gcc 3.x "-Afoo=bar" - AMB */
   
   ip = &instack[++indepth];
   ip->nominal_fname = ip->fname = "*Initialization*";
