home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume44 / a32src / part03 / cpp.c
Encoding:
C/C++ Source or Header  |  1994-09-23  |  2.1 KB  |  123 lines

  1. /*    cpp.c - C Preprocessor    */
  2. /*
  3.     USAGE:
  4.         cpp [-C] [-oout] file(s)
  5.         
  6.     Expands file(s) (- = stdin) and writes to stdout.
  7.     If -oout is present, output is written to file out.
  8.     If -C is present, comments are preserved.
  9.     Puts "#line 123 file" lines out as appropriate.
  10. */
  11. /**************
  12.  
  13. THIS SOFTWARE IS RELEASED "AS IS", WITH NO WARRANTY EXPRESSED OR IMPLIED.
  14. This software is copyright 1984, 1991, 1992, 1993, 1994 by Tom Roberts.
  15. License is granted for unlimited distribution, as long as the following
  16. conditions are met:
  17.   A. This notice is preserved intact.
  18.   B. No charge is made for the software (other than a reasonable
  19.      distribution/duplication fee).
  20.  
  21. ***************/
  22.  
  23. #include "port.h"
  24.  
  25. #undef MX_LINE
  26. #define MX_LINE 256
  27.  
  28. extern int Keep_comments;
  29.  
  30. char line[MX_LINE+1];
  31. char rawline[MX_LINE+1];
  32. char errbuf[60];
  33. char *filename;
  34. int lineno;
  35. long value;
  36. int outlineno;
  37. char *outfile = "";
  38. FILE *out;
  39.  
  40. main(argc,argv)
  41. int argc;
  42. char *argv[];
  43. {
  44.     int i,j;
  45.     char *p;
  46.     
  47.     outlineno = 0;
  48.     out = stdout;
  49.  
  50.     while(argc > 2 && argv[1][0] == '-') {
  51.         switch(argv[1][1]) {
  52.         case 'o':
  53.             if(argv[1][2] != '\0') {
  54.                 p = argv[1]+2;
  55.             } else {
  56.                 p = argv[2];
  57.                 --argc, ++argv;
  58.             }
  59.             out = fopen(p,"w");
  60.             if(!out) {
  61.                 fprintf(stderr,"cpp: Cannot write '%s'\n",p);
  62.                 exit(1);
  63.             }
  64.             break;
  65.         case 'C':
  66.             Keep_comments = 1;
  67.             break;
  68.         }
  69.         --argc, ++argv;
  70.     }
  71.     
  72.     for(i=1; i<argc; ++i) {
  73.         ppinit(argv[i]);
  74.         while(!ppgetline(line,MX_LINE)) {
  75.             if(lineno!=outlineno || strcmp(outfile,filename)!=0)
  76.             prtlineno();
  77.             fprintf(out,"%s",line);
  78.             for(j=0; line[j]; ++j) {
  79.             if(line[j] == '\n')
  80.                 ++outlineno;
  81.             }
  82.         }
  83.     }
  84.     
  85.     fclose(out);
  86. }
  87.  
  88. prtlineno()
  89. {
  90.     fprintf(out,"#line %d %s\n",lineno,filename);
  91.     outfile = filename;
  92.     outlineno = lineno;
  93. }
  94.  
  95. error(string,exit_code)
  96. char *string;
  97. int exit_code;
  98. {
  99.  
  100.     fprintf(stderr,"cpp: '%s' line %d: %s\n",filename,lineno,string);
  101.     if(exit_code > 0) {
  102.         fclose(stdout);
  103.         exit(exit_code);
  104.     }
  105. }
  106.  
  107. listit(inlevel,inskip)
  108. int inlevel,inskip;
  109. {
  110. }
  111.  
  112. isident(ch)
  113. int ch;
  114. {
  115.     return(isalnum(ch) || ch == '_');
  116. }
  117.  
  118. sym_val(ptr,pptr)
  119. char *ptr,**pptr;
  120. {
  121.     return(-1);
  122. }
  123.