home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume15 / inc-elim / inelim.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-05-30  |  6.4 KB  |  280 lines

  1. #ifndef lint
  2. static char *RCSid = "$Header: inelim.c,v 1.5 1988-02-29 00:39:38 sten Exp $";
  3. #endif
  4.  
  5. /*****************************************************************************\
  6. *                                           *
  7. *  NAME                                          *
  8. *    inelim - eliminate include statements                      *
  9. *                                           *
  10. *  SYNOPSIS                                      *
  11. *    inelim [-v] [-p pattern] [files]                          *
  12. *                                           *
  13. *  DESCRIPTION                                      *
  14. *    inelim eliminate include statements by substituting the line with the    *
  15. *    include statement with the contents of then file to be included. The     *
  16. *    result is written to stdout. The -v option prints file namn and line     *
  17. *    number at the beginning of each line. The -p option allows you to          *
  18. *    specify an alternative pattern instead of the standard 'include'. If no  *
  19. *    files are given, stdin is taken. Tilde and environment substitution is   *
  20. *    performed on each include file name. If the expanded file name doesn't   *
  21. *    begin with a '/', the directory in which we are reading from at the      *
  22. *    moment is prefixed. If the first character in the file name is '|', the  *
  23. *    rest is fed to "/bin/sh" and data from stdout replaces the include          *
  24. *    line.                                      *
  25. *                                           *
  26. *  AUTHOR                                      *
  27. *    sten@enea                                      *
  28. *                                           *
  29. \*****************************************************************************/
  30.  
  31. /*
  32.  * $Log:    inelim.c,v $
  33.  * 
  34.  * Revision 1.5  1988-02-29  00:39:38  sten
  35.  * before public release
  36.  * 
  37.  * Revision 1.4  1988-02-23  14:54:58  sten
  38.  * incelim => inelim
  39.  * 
  40.  * Revision 1.3  1987-03-02  00:41:42  sten
  41.  * inclusion of command stdout.
  42.  * 
  43.  * Revision 1.2  1986-10-20  16:40:57  sten
  44.  * released to the Inner Circle
  45.  * 
  46.  * Revision 1.1  1986-10-05  23:09:39  sten
  47.  * Initial revision
  48.  */
  49.  
  50. #include <stdio.h>
  51. #include <ctype.h>
  52. #include <assert.h>
  53.  
  54. #ifdef BSD
  55. #include <sys/param.h>
  56. #endif
  57. #ifdef V7
  58. #define MAXPATHLEN 256
  59. #endif
  60. #ifdef SYSV
  61. #include <limits.h>
  62. #define MAXPATHLEN PATH_MAX
  63. #endif
  64.  
  65. #ifdef SYSV
  66. #define rindex strrchr
  67. #endif
  68.  
  69. #define LLEN 256
  70.  
  71. extern char *strcat();
  72. extern char *strcpy();
  73. extern FILE *popen();
  74. extern int rindex();
  75. extern void exit();
  76.  
  77. #define Strcpy (void) strcpy
  78. #define Strcat (void) strcat
  79.  
  80. #define INCLUDE "include"
  81.  
  82. char *progname;
  83. int verbose = 0;
  84. char *pattern = INCLUDE;
  85.  
  86. /******************************************************************\
  87. *                                    *
  88. *  pipefile: read the file given as parameter and invoke pipefile  *
  89. *    recursively on detected include files.               *
  90. *                                    *
  91. \******************************************************************/
  92. static void
  93. pipefile(cfd, cwd, cfn)
  94. FILE *cfd;
  95. char *cwd, *cfn;
  96. {
  97.     int             lcnt = 0;
  98.     char            iwd[MAXPATHLEN];
  99.     char            line[LLEN];
  100.  
  101.     while (fgets(line, LLEN - 1, cfd) != 0)
  102.     {
  103.     lcnt++;
  104.     if (strncmp(line, pattern, strlen(pattern)) != 0)
  105.     {
  106.         if (verbose)
  107.         fprintf(stdout, "%s/%s, line %d: ", cwd, cfn, lcnt);
  108.         fputs(line, stdout);
  109.     } else
  110.     {
  111.         FILE           *ifd;
  112.         char           *ifn, *p;
  113.         char            path[MAXPATHLEN];
  114.         char            ppath[MAXPATHLEN];
  115.  
  116.         for (p = line + strlen(pattern); isspace(*p); p++);
  117.         if (untilde(ppath, p) != 0)
  118.         {
  119.         fprintf(stderr, "%s/%s, line %d: %s: %s\n",
  120.             cwd, cfn, lcnt, progname, "Cannot untilde");
  121.         exit(1);
  122.         }
  123.         if (ppath[0] == '|')
  124.         {
  125.         ppath[strlen(ppath) - 1] = '\0';
  126.         for (p = ppath + 1; isspace(*p); p++);
  127.         if ((ifd = popen(p, "r")) == (FILE *) NULL)
  128.         {
  129.             fprintf(stderr, "%s: Cannot popen %s\n", progname, ppath);
  130.             exit(1);
  131.         }
  132.         pipefile(ifd, cwd, ppath);
  133.         (void) pclose(ifd);
  134.         } else
  135.         {
  136.         if ((unenv(path, ppath)) != 0)
  137.         {
  138.             fprintf(stderr, "%s/%s, line %d: %s: %s\n",
  139.                 cwd, cfn, lcnt, progname, "Cannot unenv");
  140.             exit(1);
  141.         }
  142.         if (path[0] == '/')
  143.             Strcpy(iwd, path);
  144.         else
  145.         {
  146.             Strcpy(iwd, cwd);
  147.             Strcat(iwd, "/");
  148.             Strcat(iwd, path);
  149.         }
  150.         iwd[strlen(iwd) - 1] = '\0';
  151.         if ((ifd = fopen(iwd, "r")) == (FILE *) NULL)
  152.         {
  153.             fprintf(stderr, "%s: Cannot open %s\n", progname, iwd);
  154.             exit(1);
  155.         }
  156.         ifn = (char *) rindex(iwd, '/');
  157.         *ifn++ = '\0';
  158.         pipefile(ifd, iwd, ifn);
  159.         (void) fclose(ifd);
  160.         }
  161.     }
  162.     }
  163. }
  164.  
  165. #ifdef V7
  166. /**********************************************\
  167. *                            *
  168. * mygetwd retrieves current working directory  *
  169. *                            *
  170. \**********************************************/
  171. static void
  172. mygetwd(to)
  173. char *to;
  174. {
  175.     FILE *pwdfd;
  176.     char *i;
  177.  
  178.     pwdfd = popen ("/bin/pwd\n");
  179.     assert (pwdfd != NULL);
  180.     i = fgets (to, MAXPATHLEN, pwdfd);
  181.     assert (i != NULL);
  182.     pclose (pwdfd);
  183.     to[strlen (to) - 1] = '\0';
  184. }
  185. #endif
  186. /*****************************************\
  187. *                       *
  188. *  usage: display correct usage and exit  *
  189. *                       *
  190. \*****************************************/
  191. static void
  192. usage()
  193. {
  194.     fprintf(stderr, "Usage: %s [-v] [-p pattern] [files]\n", progname);
  195.     exit(1);
  196. }
  197.  
  198. void
  199. main (argc, argv)
  200. int    argc;
  201. char    *argv[];
  202. {
  203.     char            sw;
  204.     extern int      optind;
  205.     extern char    *optarg;
  206.     char            dir[MAXPATHLEN];
  207.  
  208.     if ((progname = (char *)rindex(argv[0], '/')) == 0)
  209.     progname = argv[0];
  210.     while ((sw = getopt(argc, argv, "vp:")) != (char) EOF)
  211.     switch (sw)
  212.     {
  213.     case 'v':
  214.         verbose++;
  215.         break;
  216.     case 'p':
  217.         pattern = optarg;
  218.         break;
  219.     case '?':
  220.         usage();
  221.         break;
  222.     }
  223. #ifdef BSD
  224.     (void) getwd(dir);
  225. #endif
  226. #ifdef V7
  227.     mygetwd(dir);
  228. #endif
  229. #ifdef SYSV
  230.     (void) getcwd(dir, MAXPATHLEN);
  231. #endif
  232.     if (optind == argc)
  233.     pipefile(stdin, dir, "");
  234.     else
  235.     for (; optind < argc; optind++)
  236.     {
  237.         FILE           *fd;
  238.         char           *file;
  239.         char            path[MAXPATHLEN];
  240.         char            ppath[MAXPATHLEN];
  241.  
  242.         if (strcmp(argv[optind], "-") == 0)
  243.         pipefile(stdin, dir, "");
  244.         else
  245.         {
  246.         if (untilde(ppath, argv[optind]) != 0)
  247.         {
  248.             fprintf(stderr, "%s: %s: %s\n",
  249.                 progname, "Cannot untilde", argv[optind]);
  250.             exit(1);
  251.         }
  252.         if ((unenv(path, ppath)) != 0)
  253.         {
  254.             fprintf(stderr, "%s: %s: %s\n",
  255.                 progname, "Cannot unenv", argv[optind]);
  256.             exit(1);
  257.         }
  258.         if (path[0] == '/')
  259.             Strcpy(dir, path);
  260.         else
  261.         {
  262.             if (dir[strlen(dir) - 1] != '/')
  263.             Strcat(dir, "/");
  264.             Strcat(dir, path);
  265.         }
  266.         if ((fd = fopen(dir, "r")) == (FILE *) NULL)
  267.         {
  268.             fprintf(stderr, "%s: Cannot open %s\n", progname, dir);
  269.             exit(1);
  270.         }
  271.         file = (char *) rindex(dir, '/');
  272.         *file++ = '\0';
  273.         pipefile(fd, dir, file);
  274.         (void) fclose(fd);
  275.         }
  276.     }
  277.     exit(0);
  278.     /* NOTREACHED */
  279. }
  280.