home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume12 / cake / part04 / Aux / ccincl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-10-14  |  4.9 KB  |  314 lines

  1. /*
  2. **    Ccincl main file.
  3. */
  4.  
  5. static    char
  6. rcs_id[] = "$Header$";
  7.  
  8. #include    <ctype.h>
  9. #include    <stdio.h>
  10. #include    <sys/types.h>
  11. #include    <sys/stat.h>
  12. #include    "std.h"
  13.  
  14. typedef    struct    stat    Stat;
  15.  
  16. #define    MAXDIRS        10
  17. #define    MAXSTOPS    10
  18. #define    MAXIGNORES    10
  19. #define    MAXFILES    64
  20. #define    MAXBUF        512
  21.  
  22. char    *dir_list[MAXDIRS];
  23. char    *stop_list[MAXSTOPS];
  24. char    *ign_list[MAXIGNORES];
  25. char    *file_list[MAXFILES];
  26. bool    search_list[MAXFILES];
  27. ino_t    ino_list[MAXFILES];
  28. int    dircount = 0;
  29. int    stopcount = 0;
  30. int    igncount = 0;
  31. int    filecount = 0;
  32.  
  33. char    *currdir = ".";
  34. char    *filename;
  35. Stat    statbuf;
  36. int    rflag = FALSE;
  37. int    fflag = FALSE;
  38. int    errorcount = 0;
  39.  
  40. main(argc, argv)
  41. int    argc;
  42. char    **argv;
  43. {
  44.     extern    FILE    *yyin;
  45.     reg    int    i;
  46.  
  47.     dir_list[dircount++] = ".";
  48.     while (argc > 1 && argv[1][0] == '-')
  49.     {
  50.         for (i = 1; argv[1][i] != '\0'; i++)
  51.         {
  52.             switch (argv[1][i])
  53.             {
  54.  
  55.         when 'f':
  56.                 fflag = TRUE;
  57.         when 'r':
  58.                 rflag = TRUE;
  59.         when 'i':
  60.                 if (i != 1)
  61.                     usage();
  62.  
  63.                 ign_list[igncount++] = &argv[1][2];
  64.                 file_list[filecount] = &argv[1][2];
  65.  
  66.                 if (stat(&argv[1][2], &statbuf) != 0)
  67.                 {
  68.                     printf("ccincl: cannot find %s\n", &argv[1][2]);
  69.                     exit(1);
  70.                 }
  71.  
  72.                 search_list[filecount] = FALSE;
  73.                 ino_list[filecount] = statbuf.st_ino;
  74.                 filecount++;
  75.                 goto nextword;
  76.         when 's':
  77.                 if (i != 1)
  78.                     usage();
  79.  
  80.                 stop_list[stopcount++] = &argv[1][2];
  81.                 file_list[filecount] = &argv[1][2];
  82.  
  83.                 if (stat(&argv[1][2], &statbuf) != 0)
  84.                 {
  85.                     printf("ccincl: cannot find %s\n", &argv[1][2]);
  86.                     exit(1);
  87.                 }
  88.  
  89.                 search_list[filecount] = FALSE;
  90.                 ino_list[filecount] = statbuf.st_ino;
  91.                 filecount++;
  92.                 goto nextword;
  93.         when 'C':
  94.                 if (i != 1)
  95.                     usage();
  96.  
  97.                 currdir = &argv[1][2];
  98.                 goto nextword;
  99.         when 'I':
  100.                 if (i != 1)
  101.                     usage();
  102.  
  103.                 dir_list[dircount++] = &argv[1][2];
  104.                 goto nextword;
  105.         otherwise:
  106.                 usage();
  107.             }
  108.         }
  109.  
  110. nextword:
  111.         argc--;
  112.         argv++;
  113.     }
  114.  
  115.     dir_list[dircount++] = "/usr/include";
  116.     if (dircount > MAXDIRS)
  117.     {
  118.         printf("ccincl: too many dir_list.\n");
  119.         exit(1);
  120.     }
  121.  
  122.     if (argc < 2)
  123.         usage();
  124.     
  125.     while (argc > 1)
  126.     {
  127.         file_list[filecount] = argv[1];
  128.         if (stat(argv[1], &statbuf) != 0)
  129.         {
  130.             printf("ccincl: cannot find %s\n", argv[1]);
  131.             exit(1);
  132.         }
  133.  
  134.         search_list[filecount] = TRUE;
  135.         ino_list[filecount] = statbuf.st_ino;
  136.         filecount++;
  137.         argc--;
  138.         argv++;
  139.     }
  140.     
  141.     for (i = 0; i < filecount; i++)
  142.     {
  143.         if (! search_list[i])
  144.             continue;
  145.  
  146.         filename = file_list[i];
  147.         if ((yyin = fopen(filename, "r")) == NULL)
  148.         {
  149.             fflush(stdout);
  150.             perror("ccincl");
  151.             printf("ccincl: cannot open %s\n", filename);
  152.             exit(1);
  153.         }
  154.  
  155.         yylex();
  156.         fclose(yyin);
  157.     }
  158.  
  159.     exit(errorcount);
  160. }
  161.  
  162. process(line)
  163. reg    char    *line;
  164. {
  165.     extern    char    *malloc();
  166.     extern    int    yylineno;
  167.     char        buf[MAXBUF];
  168.     reg    char    *s, *start;
  169.     reg    int    i, startdir;
  170.     reg    bool    index;
  171.     reg    char    endchar;
  172.     reg    bool    found;
  173.  
  174.     for (s = line+1; *s != '\0' && isspace(*s); s++)
  175.         ;
  176.     
  177.     if (strndiff(s, "include", 7))
  178.         return;
  179.     
  180.     for (s += 7; *s != '\0' && isspace(*s); s++)
  181.         ;
  182.     
  183.     if (*s == '<')
  184.         endchar = '>', startdir = 1;
  185.     else
  186.         endchar = '"', startdir = 0;
  187.  
  188.     start = s+1;
  189.     for (s = start; *s != '\0' && *s != endchar; s++)
  190.         ;
  191.  
  192.     if (*s != endchar)
  193.     {
  194.         printf("ccincl: %s(%d) bad include syntax\n", filename, yylineno);
  195.         errorcount++;
  196.         return;
  197.     }
  198.  
  199.     /* terminate arg (now pointed to by start) */
  200.     *s = '\0';
  201.  
  202.     /* handle absolute pathnames */
  203.     if (*start == '/')
  204.     {
  205.         sprintf(buf, "%s", start);
  206.         goto end;
  207.     }
  208.  
  209.     /* handle relative pathnames */
  210.     if (*start == '.')
  211.     {
  212.         sprintf(buf, "%s/%s", currdir, start);
  213.         goto end;
  214.     }
  215.  
  216.     /* handle implicit pathnames */
  217.     found = FALSE;
  218.     for (i = startdir; i < dircount; i++)
  219.     {
  220.         if (i == 0)
  221.         {
  222.             if (streq(currdir, "."))
  223.                 strcpy(buf, "");
  224.             else
  225.             {
  226.                 strcpy(buf, dir_list[i]);
  227.                 strcat(buf, "/");
  228.             }
  229.  
  230.             strcat(buf, start);
  231.         }
  232.         else
  233.         {
  234.             strcpy(buf, dir_list[i]);
  235.             strcat(buf, "/");
  236.             strcat(buf, start);
  237.         }
  238.  
  239.         if (strlen(buf) > MAXBUF)
  240.         {
  241.             printf("ccincl: buffer length exceeded\n");
  242.             exit(1);
  243.         }
  244.  
  245.         if (stat(buf, &statbuf) == 0)
  246.         {
  247.             found = TRUE;
  248.             break;
  249.         }
  250.     }
  251.  
  252.     if (! found)
  253.     {
  254.         printf("ccincl: cannot find %s\n", start);
  255.         errorcount++;
  256.         return;
  257.     }
  258.  
  259. end:
  260.     if (stat(buf, &statbuf) != 0)
  261.     {
  262.         printf("ccincl: cannot stat %s\n", buf);
  263.         errorcount++;
  264.         return;
  265.     }
  266.  
  267.     found = FALSE;
  268.     for (i = 0; i < filecount; i++)
  269.         if (ino_list[i] == statbuf.st_ino)
  270.         {
  271.             found = TRUE;
  272.             index = i;
  273.             break;
  274.         }
  275.  
  276.     if (! found)
  277.     {
  278.         index = filecount;
  279.         ino_list[filecount]    = statbuf.st_ino;
  280.         search_list[filecount] = FALSE;
  281.         file_list[filecount]   = malloc(strlen(buf)+1);
  282.         strcpy(file_list[filecount], buf);
  283.         filecount++;
  284.     }
  285.  
  286.     for (i = 0; i < igncount; i++)
  287.         if (streq(file_list[index], ign_list[i]))
  288.             return;
  289.  
  290.     if (fflag)
  291.         printf("%s: ", filename);
  292.  
  293.     printf("%s\n", file_list[index]);
  294.  
  295.     if (rflag)
  296.     {
  297.         for (i = 0; i < stopcount; i++)
  298.             if (streq(buf, stop_list[i]))
  299.                 return;
  300.  
  301.         search_list[filecount] = TRUE;
  302.     }
  303. }
  304.  
  305. /*
  306. **    Tell the unfortunate user how to use ccincl.
  307. */
  308.  
  309. usage()
  310. {
  311.     printf("Usage: ccincl [-rf] [-ifile] [-sfile] [-Cdir] [-Idir] ... file ...\n");
  312.     exit(1);
  313. }
  314.