home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 154_01 / include.c < prev    next >
Text File  |  1979-12-31  |  2KB  |  97 lines

  1. /* include.c:    Do-it-yourself file inclusion (standard input) */
  2.  
  3. /* guards against recursive inclusion; allows 7 levels of nesting..
  4.  
  5.     Adapted from Software Tools by Chuck Allison -
  6.  
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <ctype.h>
  11. #define MAXLINE 81
  12. #define MAXWORD 20
  13. #define NDEEP 8
  14.  
  15. struct
  16. {
  17.     char fname[MAXWORD];        /* ..file name.. */
  18.     FILE *fp;                    /* ..file pointer.. */
  19. } infile[NDEEP];
  20.  
  21. int level;                        /* ..include depth.. */
  22.  
  23. main()
  24. {
  25.     char line[MAXLINE], s[MAXWORD], *zap();
  26.  
  27.     /* ..start with standard input.. */
  28.     infile[0].fp = stdin; strcpy(infile[0].fname,"");
  29.  
  30.     for (level = 0; level >= 0; --level)
  31.     {
  32.         while (fgets(line,MAXLINE-1,infile[level].fp) != NULL)
  33.         {
  34.             sscanf(line,"%9s",s);
  35.             if (strcmp(s,"#include") != 0)
  36.                 /* ..output text line.. */
  37.                 fputs(line,stdout);
  38.             else
  39.             {
  40.                 /* ..attempt to include.. */
  41.                 if (level == NDEEP)
  42.                 {
  43.                     fprintf(stderr,"nest overflow.\n");      
  44.                     exit(1);
  45.                 }
  46.  
  47.                 if (sscanf(line+strlen(s),"%19s",s) == 0)
  48.                     /* ..filename missing - just go on.. */
  49.                     continue;
  50.  
  51.                 strcpy(s,zap(s));
  52.                 if (search(s))
  53.                 {
  54.                     /* ..self-inclusion.. */
  55.                     fprintf(stderr,"recursion on %s\n",s);
  56.                     continue;
  57.                 } 
  58.  
  59.                 infile[level].fp = fopen(s,"r");
  60.                 if (infile[level].fp == NULL)
  61.                 {
  62.                     /* ..open error.. */
  63.                     fprintf(stderr,"can't open: %s\n",s);
  64.                     --level;
  65.                 }
  66.             }
  67.         }
  68.         if (level > 0)
  69.             fclose(infile[level].fp);
  70.     }
  71. }
  72.  
  73. char *zap(s)                /* ..strip "" or <> from filename.. */
  74. char *s;
  75. {
  76.     int l;
  77.  
  78.     if (s[l=strlen(s)-1] == '\"' || s[l] == '>')
  79.         s[l] = '\0';
  80.     return (s[0]=='\"' || s[0]=='<') ? s+1 : s;
  81. }
  82.  
  83. int search(s)                /* ..check for a recursive include.. */
  84. char *s;
  85. {
  86.     int i;
  87.  
  88.     /* ..(not-so) quick and dirty search.. */
  89.     for (i=0; i <= level; ++i) 
  90.         if (strcmp(s,infile[i].fname) == 0)
  91.             /* ..found it - recursive include request.. */
  92.             return 1;
  93.     /* ..add filename to list of open files.. */
  94.     strcpy(infile[++level].fname,s);
  95.      return 0;
  96. }
  97.