home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / tex / texsrc2 / Src / web2c / c / splitup < prev    next >
Text File  |  1993-05-01  |  5KB  |  162 lines

  1. /* splitup -- take TeX or MF in C as a single stream on stdin,
  2.    and it produces several .c and .h files in the current directory
  3.    as its output.
  4.  
  5.   Tim Morgan  September 19, 1987.  */
  6.  
  7. #include "config.h"
  8.  
  9. int filenumber = 0, ifdef_nesting = 0, lines_in_file = 0;
  10. char *output_name = "tex";
  11. int has_ini;
  12.  
  13. #ifdef RISCOS
  14. #define TEMPFILE        "c.temp"
  15. #else
  16. #define TEMPFILE        "temp.c"
  17. #endif
  18. #define MAXLINES        2000
  19.  
  20. #ifdef RISCOS /* don't have unlink() but remove() */
  21. #define unlink remove
  22. #endif
  23. char buffer[1024], filename[PATH_MAX];
  24.  
  25. FILE *out, *ini, *temp;
  26. FILE *in = stdin;
  27.  
  28.  
  29. int
  30. main (argc, argv)
  31.   int argc;
  32.   char *argv[];
  33. {
  34.     if (argc > 1)
  35.         output_name = argv[1];
  36.  
  37. #ifdef RISCOS
  38.     (void) sprintf (filename, "h.%sd", output_name);
  39. #else
  40.     (void) sprintf (filename, "%sd.h", output_name);
  41. #endif
  42.     if (!(out = fopen (filename, "w")))
  43.         FATAL_PERROR (filename);
  44.  
  45.     (void) fprintf(out,
  46.                 "#undef\tTRIP\n#undef\tTRAP\n#define\tSTAT\n#undef\tDEBUG\n");
  47.     for ((void) fgets(buffer, sizeof(buffer), in);
  48. #ifdef RISCOS
  49.           strncmp(&buffer[10], "h.coerce", 8);
  50. #else
  51.           strncmp(&buffer[10], "coerce.h", 8);
  52. #endif
  53.          (void) fgets(buffer, sizeof(buffer), in))
  54.       {
  55.         if (buffer[0] == '#' || buffer[0] == '\n' || buffer[0] == '}'
  56.             || buffer[0] == '/'
  57.             || buffer[0] == ' ' || strncmp(buffer, "typedef", 7) == 0)
  58.           /*nothing*/;
  59.         else
  60.           (void) fputs("EXTERN ", out);
  61.  
  62.         (void) fputs(buffer, out);
  63.       }
  64.  
  65.     (void) fputs(buffer, out);
  66.     fclose (out);
  67.  
  68. #ifdef RISCOS
  69.     (void) sprintf(filename, "c.i%s", output_name);
  70. #else
  71.     (void) sprintf(filename, "i%s.c", output_name);
  72. #endif
  73.     ini = fopen(filename, "w");
  74.     if (!ini)
  75.         FATAL_PERROR (filename);
  76.  
  77.     (void) fputs("#define EXTERN extern\n", ini);
  78.     (void) fprintf(ini, "#include \"%sd.h\"\n\n", output_name);
  79. #ifdef RISCOS
  80.     (void) sprintf(filename, "c.%s0", output_name);
  81. #else
  82.     (void) sprintf(filename, "%s0.c", output_name);
  83. #endif
  84.  
  85.     if (!(out = fopen(filename, "w")))
  86.         FATAL_PERROR (filename);
  87.     (void) fputs("#define EXTERN extern\n", out);
  88.     (void) fprintf(out, "#include \"%sd.h\"\n\n", output_name);
  89.  
  90.     do {
  91.         /* Read one routine into a temp file */
  92.         has_ini = false;
  93.         if (!(temp = fopen(TEMPFILE, "w+")))
  94.             FATAL_PERROR (TEMPFILE);
  95.             
  96.         while (read_line()) {
  97.             (void) fputs(buffer, temp);
  98.             if (buffer[0] == '}') break; /* End of procedure */
  99.         }
  100.         while (ifdef_nesting > 0 && read_line())
  101.             (void) fputs(buffer, temp);
  102.         rewind(temp);
  103.  
  104.         if (has_ini) {  /* Contained "#ifdef INI..." */
  105.             while (fgets(buffer, sizeof(buffer), temp))
  106.                 (void) fputs(buffer, ini);
  107.         }
  108.         else {                  /* Doesn't contain "#ifdef INI..." */
  109.             while (fgets(buffer, sizeof(buffer), temp)) {
  110.                 (void) fputs(buffer, out);
  111.                 lines_in_file++;
  112.             }
  113.         }
  114.         if (fclose (temp))
  115.             FATAL_PERROR ("fclose");
  116.             
  117.         if (lines_in_file > MAXLINES) {
  118.             if (fclose(out))
  119.                 perror("fclose"), uexit (1);
  120. #ifdef RISCOS
  121.             (void) sprintf(filename, "c.%s%d", output_name, ++filenumber);
  122. #else
  123.             (void) sprintf(filename, "%s%d.c", output_name, ++filenumber);
  124. #endif
  125.             if ( !(out = fopen(filename, "w")))
  126.                 perror(filename), uexit (1);
  127.             (void) fputs("#define EXTERN extern\n", out);
  128.             (void) fprintf(out, "#include \"%sd.h\"\n\n", output_name);
  129.             lines_in_file = 0;
  130.         }
  131.     } while (!feof(in));
  132.  
  133.     if (fclose(out))
  134.         perror("fclose"), uexit (1);
  135.     if (lines_in_file == 0)
  136.         (void) unlink(filename);
  137.     if (fclose(ini))
  138.         perror("fclose"), uexit (1);
  139.     if (unlink(TEMPFILE))
  140.         perror(TEMPFILE), uexit (1);
  141.  
  142.     uexit (0);
  143. }
  144.  
  145. /*
  146.  * Read a line of input into the buffer, returning `false' on EOF.
  147.  * If the line is of the form "#ifdef INI...", we set "has_ini"
  148.  * `true' else `false'.  We also keep up with the #ifdef/#endif nesting
  149.  * so we know when it's safe to finish writing the current file.
  150.  */
  151. int
  152. read_line()
  153. {
  154.     if (fgets(buffer, sizeof(buffer), in) == NULL) return false;
  155.     if (strncmp(buffer, "#ifdef", 6) == 0) {
  156.         ++ifdef_nesting;
  157.         if (strncmp(&buffer[7], "INI", 3) == 0) has_ini = true;
  158.     }
  159.     else if (strncmp(buffer, "#endif", 6) == 0) --ifdef_nesting;
  160.     return true;
  161. }
  162.