home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / brklyprl.lha / Emulator / compile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-14  |  1.2 KB  |  58 lines

  1.  
  2. /* Copyright (C) 1988, 1989 Herve' Touati, Aquarius Project, UC Berkeley */
  3.  
  4. /* Copyright Herve' Touati, Aquarius Project, UC Berkeley */
  5.  
  6. #include <stream.h>
  7. #include <sys/types.h>
  8. #include <sys/wait.h>
  9. #include <strings.h>
  10. #include "compile.h"
  11.  
  12. char* concat_strings(char* s1, char* s2)
  13. {
  14.   int strlen(...);
  15.   char* strcat(...);
  16.   char* buf = new char[strlen(s1) + strlen(s2) + 2];
  17.   buf[0] = '\0';
  18.   strcat(buf, s1);
  19.   strcat(buf, s2);
  20.   return buf;
  21. }
  22.  
  23. istream* open_input_file(char* filename)
  24. {
  25.   filebuf* f1 = new filebuf;
  26.   return (f1->open(filename, input) == 0) ? 0 : new istream(f1);
  27. }
  28.  
  29. void compile(char* name)
  30. {
  31.   int vfork();
  32.   int execl(...);
  33.   int wait(...);
  34.   int pid;
  35.   switch (pid = vfork()) {
  36.   case 0:
  37.     execl("compiler", "compiler", name, 0);
  38.     break;
  39.   default:
  40.     if (pid == -1) {
  41.       cout << "can't fork another process\n";
  42.     }
  43.     wait(0);
  44.     break;
  45.   }
  46. }
  47.  
  48. istream* get_loadable_file(char* name, int mode)
  49. {
  50.   istream* new_cinp;
  51.   char* full_name = concat_strings(name, ".w");
  52.   new_cinp = open_input_file(full_name);
  53.   if (new_cinp == 0 || mode == COMPILE_MODE)
  54.     compile(name);
  55.   new_cinp = open_input_file(full_name);
  56.   return new_cinp;
  57. }  
  58.