/* File: Fall99/Cproject2/maple_cgi.c
   Programmer: Erich Kaltofen (Sep 17, 1998)
                              (Revision Sep 7, 1999)
   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/proj2_maple_input";
/* scratch file for Maple output */
static char maple_out_name[] = "/tmp/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;

  int c; /* name of html form field is "c" */
  int sys; /* return code from system call */

  /* field values are received on stdin */
  scanf("c=%d", &c);
 
  /* 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, "ifactor(%d!);\n", c);

  /* show it */
  /***  Maple will show it (if it runs)
  printf("> ifactor(%d!);\n", c);
  ***/
  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;
    }

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