/* File: maple_cgi.c
   Programmer: Koren Atwater
   Date: 12 October 99
   This program is intended as a cgi for running Maple from a browser
   Some of this code is taken from Dr. Erich Kaltofen's maple_cgi.c
*/

/*Runs command line maple */   
static char maple[] = "/afs/eos.ncsu.edu/users/k/kaltofen/www/maple/bin/maple";

/* scratch file for Maple input */
static char maple_in_name[] = "/tmp/kdatwate_proj2_maple_input";

/* scratch file for Maple output */
static char maple_out_name[] = "/tmp/kdatwate_proj2_maple_output";

/* max length copied from Maple out to stdout */
#define MAX_LINE_LENGTH 80 

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
int main (int argc, char* argv[])
{
  char run_maple[128];  
  char line[MAX_LINE_LENGTH];  /*maximum number of maple output characters printed*/

  FILE  *maple_in, *maple_out;  /*pointers to files to hold maple input and maple output */

  int sys; /* return code from system call */
  char a11[10],a12[10],b1[10],a21[10],a22[10],b2[10];   /*coefficient values from webpage */
  char x1[5],x2[5]; /*variables names*/

 /* field values are received on stdin */
  scanf("a11=%[^&]", a11);
  getchar();
  scanf("x1=%[^&]",  x1);
  getchar();
  scanf("a12=%[^&]", a12);
  getchar();
  scanf("x2=%[^&]",  x2);
  getchar();
  scanf("b1=%[^&]",  b1);
  getchar();
  scanf("a21=%[^&]", a21);
  getchar();
  scanf("a22=%[^&]", a22);
  getchar();
  scanf("b2=%[^&]",  b2);
    
  
  /* must give proper header information to browser for returned text */
  printf ("Content-type: text/plain\n\n");

  
  /* try to open scratch file for Maple input */
  maple_in = fopen (maple_in_name, "w");
  if (maple_in == NULL)
     { printf("fopen(\"%s\") failed: %s\n",
              maple_in_name, strerror(errno));
       return errno;
     }
                          
  /* prepare Unix Maple command line */
  sprintf (run_maple,
           "%s -f -a -s -q < %s > %s",
           maple, maple_in_name, maple_out_name);

  /* show it */
  printf ("%s\n", run_maple);
   
  /* place Maple command into scratch file */
  fprintf(maple_in, "a11:=%s;", a11);
  fprintf(maple_in, " ");
  fprintf(maple_in, "a12:=%s;", a12);
  fprintf(maple_in," ");
  fprintf(maple_in, "b1:=%s;",  b1);
  fprintf(maple_in,";\n");
  fprintf(maple_in, "a21:=%s;", a21);
  fprintf(maple_in," ");
  fprintf(maple_in, "a22:=%s;", a22);
  fprintf(maple_in," ");
  fprintf(maple_in, "b2:=%s;",b2);
  fprintf(maple_in,";\n");
  
 
  fprintf(maple_in,"solve({(%s)*%s+(%s)*%s=(%s),(%s)*%s+(%s)*%s=(%s)},{%s,%s});\n", a11,x1,a12,x2,b1,a21,x1,a22,x2,b2,x1,x2); 

 
  fflush(stdout);
  fclose(maple_in);

  /* check if shell available */
  sys = system(NULL);
  if(sys == 0)
    { printf("system(NULL) failed: no shell available");
      remove(maple_in_name);
      return -1;
    }

  /* execute Maple command */
  sys = system (run_maple);
  if(sys == -1)
    { printf("system(\"%s\") failed: %s\n",
              run_maple, strerror(errno));
      remove(maple_in_name);
      return errno;
    }  

  maple_out = fopen (maple_out_name, "r");
  if (maple_out == NULL)
     { printf("fopen(\"%s\") failed: %s\n",
              maple_out_name, strerror(errno));
       remove(maple_in_name);
       return errno;
     }

  /* copy Maple output to stdout */
  while(fgets(line,MAX_LINE_LENGTH,maple_out))
       printf("%s",line);

  /* clean up scratch files */
  fclose(maple_out);
  remove(maple_in_name);
  remove(maple_out_name);
  printf("THE END\n");

  return 0;
}

