/* File: Fall99/Cproject2/maple_cgi.c
   Programmer: Erich Kaltofen (Sep 17, 1998)
                              (Revision Sep 7, 1999)
   Revised for Project 2 - Ginger E. Cutt - 10/8/99
   This program is intended as a cgi for running Maple from a browser
*/
   
/* files used in this script */

/* Maple script */

/***  not authorized to use system directory: "system:authuser rl"
static char maple[] = "/afs/bp.ncsu.edu/dist/maple54/bin/maple";
***/
static char maple[] = "/afs/eos.ncsu.edu/users/k/kaltofen/www/maple/bin/maple";

/* scratch file for Maple input */
/* WARNING: in your project, please use your individualized names;
            otherwise, your classmates might overwrite your files */

static char maple_in_name[] = "/tmp/gecutt_proj2_maple_input";
/****
static char maple_in_name[] = "gecutt_proj2_maple_input";
****/

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

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

#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];

  FILE  *maple_in, *maple_out;

  char a11[20], x1[20], a12[20], x2[20], b1[20], a21[20], a22[20], b2[20];   /* names from html form field  */ 
  int sys; /* return code from system call */
  
  /* field values are received on stdin */
  scanf("a11=%[^&]",&a11[0]);
  scanf("&");
  scanf("x1=%[^&]",&x1[0]);
  scanf("&"); 
  scanf("a12=%[^&]",&a12[0]);
  scanf("&"); 
  scanf("x2=%[^&]",&x2[0]);
  scanf("&"); 
  scanf("b1=%[^&]",&b1[0]);
  scanf("&"); 
  scanf("a21=%[^&]",&a21[0]);
  scanf("&"); 
  scanf("a22=%[^&]",&a22[0]);
  scanf("&"); 
  scanf("b2=%s",&b2[0]);
		
  /* 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);

  /* place Maple command into scratch file */
  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);

  /* show it */
  /***  Maple will show it (if it runs) 
  printf("> 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);

  /* for debugging: just cat what was put into in_file */
  /***********
  sprintf (run_maple, "cat %s", maple_in_name);
  **********/

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

  /* show it */
  printf ("%s\n", run_maple);

  /* 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;
}
