home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / C80TCOG.ZIP / INCLUDE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-08  |  3.0 KB  |  124 lines

  1. /* include - replace include file by contents of file */
  2. /* Version 1.2 1982/12/01 22:05        */
  3. /*                                                      1982/10/09 22:57
  4. Original version from "Software Tools" by
  5. Kernighan & Plauger.
  6.  
  7. This C version:
  8.  
  9.         Copyright 1982  William G. Hutchison, Jr.
  10.                         P.O. Box 278
  11.                         Exton, PA 19341-0278
  12.                         U.S.A.
  13.  
  14.                         CompuServe 70665,1307
  15.  
  16.  
  17.     This program may be used freely for any non-commercial
  18. purpose, provided that the user does  not  remove  or  alter
  19. this notice or the copyright statement.
  20.     Those who wish to  sell  or lease this program, or to
  21. incorporate this into a product for  sale or  lease,  should
  22. apply to the author (above) for licensing information.
  23.     This program is not  covered by  a  warranty, either
  24. express or implied. The author shall not be responsible  for
  25. any  damages (including consequential) caused by reliance on
  26. the  materials  presented,  including  but  not  limited  to
  27. typographical errors or arithmetic errors.
  28.  
  29.  */
  30.  
  31. /* max depth of include files:        */
  32. #define MAINLY
  33. #define NFILES 5
  34. #include "a:c80def.h"
  35. #include "gets.c"
  36. #include "puts.c" 
  37.  
  38. #ifdef CP_M
  39. extern FILE *STDIN, *STDOUT;
  40. static FILE *STDERR;
  41. #endif
  42.  
  43. main(argc, argv)
  44. int argc;
  45. char *argv[];
  46. {
  47. char line[MAXLINE], str[MAXLINE];
  48. int infile[NFILES];
  49. int len, level, loc;
  50. #define incl "#include"
  51.  
  52. #ifdef CP_M
  53. STDERR= open("CON:", WRITE)    /* delete for UNIX */;
  54. #endif
  55. infile[0]= STDIN;
  56. for (level= 0; level >= 0; level--) {
  57.     while (fgets(line, MAXLINE, infile[level]) != NULL) {
  58.         loc= 0;
  59.         len= getwrd(line, &loc, str);
  60.         if    (equal(str, incl) == NO)
  61.             call fputs(line, STDOUT);
  62.         else {
  63.             if    (++level >= NFILES)
  64.                 call error("Includes nested too deeply.");
  65.             len= getwrd(line, &loc, str);
  66.             if    ((infile[level]= open(str, READ)) == ERR)
  67.                 call cant(str);
  68.             }
  69.         }
  70.     if    (level > 0)
  71.         call close(infile[level]);
  72.     }
  73. }                /* end of main */
  74.  
  75. cant(s)                /* can't open file message */
  76. char *s;
  77. {
  78. call fputs(s, STDERR);
  79. call error(": can't open.");
  80. }                /* end of cant */
  81.  
  82. equal(x, y)            /* test two strings for equality */
  83. register char *x, *y;
  84. {
  85. for    (; *x == *y; x++, y++)
  86.     if    (*x == EOS)
  87.         return(YES);
  88. return    (NO);
  89. }                /* end of equal */
  90.  
  91. error(s)            /* print error message, then stop */
  92. char *s;
  93. {
  94. remark(s);
  95. exit();
  96. }                /* end of error */
  97.  
  98. /* getwrd gets the next word from the input line, advances the line
  99.    index, and returns the word and the length of the word found */
  100. getwrd(in, i, out)
  101. char in[], out[];
  102. register int *i;
  103. {
  104. register int j;
  105.  
  106. while    (in[*i] == BLANK || in[*i] == TAB)
  107.     (*i)++;
  108. j= 0;
  109. while    (in[*i] != EOS &&
  110.      in[*i] != BLANK &&
  111.      in[*i] != TAB &&
  112.      in[*i] != NEWLINE) 
  113.     out[j++]= in[(*i)++];
  114. out[j]= EOS;
  115. return(j);
  116. }                /* end of getwrd */
  117.  
  118. remark(s)            /* print error message, then continue */
  119. char *s;
  120. {
  121. call fputs(s, STDERR);
  122. putc(NEWLINE, STDERR);
  123. }                /* end of remark */
  124.