/*
 * ypp.c
 *
 *    Simple pre-processor for yacc files.  Any line of the matching
 *  the form #<filename (where # is the 1st char on a line and filename
 *  is any sequence of ASCII char's up to the 1st white space) will be
 *  replaced with the contents of the file "filename."  Characters after
 *  the file name are discarded, so comments may be put there.
 *
 *  Created September 14, 1990 by Raymie Stata
 *  (c) 1990 Raymie Stata, All Rights Reserved
 */


#include <stdio.h>
#include <string.h>
#include <ctype.h>

extern int errno;

main()
{
  static char linebuf[512];
  char *p;
  int skiptest = 0;
  FILE *fp;

  /* WARNING: will mess up if a #! sequence starts in column 512... */
  for(;;) {
    if (! fgets(linebuf, 512, stdin))
      if (errno) { perror("ypp1");  exit(1); }
      else break;

    if (linebuf[0] == '#' && linebuf[1] == '<') {
      /* Have an input file.  Open it and copy out contents. */
      for(p = &linebuf[2]; ; p++) { /* Find end of file name. */
        if (! *p) break;
        if (isspace(*p)) { *p = '\0'; break; }
      }
      if (! (fp = fopen(&linebuf[2], "r"))) {  perror("ypp2");  exit(1); }
      for(;;) {
        if (! fgets(linebuf, 512, fp))
          if (errno) { perror("ypp3");  exit(1); }
          else break;
        fputs(linebuf, stdout);
      }
    } else fputs(linebuf, stdout);
  }
}
