/* File: Fall02/Cproject2/maple_cgi_linux.c Programmer: Erich Kaltofen (Sep 17, 1998) (Revision Sep 7, 1999) (Revision May, 2000: get method) (Revision Oct 11 ,2000: linux servers) (Revision Sep 25, 2002: path to maple7) (Revision Feb 6, 2008: path to maple9.5) This program is intended as a cgi for running Maple from a browser */ /* files used in this script */ /* Maple script */ static char maple[] = "/root/maple9.5/bin/maple -a -s -q"; /* 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 #include #include #include int main (int argc, char* argv[]) { char run_maple[128]; char line[MAX_LINE_LENGTH]; char *query_string; 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 in environ var QUERY_STRING */ query_string = getenv("QUERY_STRING"); sscanf(query_string, "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 < %s > %s", maple, maple_in_name, maple_out_name); /* for debugging: just cat what was put into in_file */ /*********** sprintf (run_maple, "cat %s > %s", 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); /* 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; }