Back to index

texmacs  1.0.7.15
tm_graphviz.c
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : tm_graphviz.c
00004 * DESCRIPTION: A simple computer algebra system with a link to TeXmacs
00005 * COPYRIGHT  : (C) 2001  Joris van der Hoeven and
00006 *              Jorik Blaas (jrk at Spanic.Pet.Atudelft.Mnl (remove SPAM))
00007 *******************************************************************************
00008 * It now acts as a simple frontend to dot from the graphviz package.
00009 * At the moment it uses temporary files, which is not very elegant,
00010 * I will try to change this to pipes later on.
00011 *******************************************************************************
00012 * This software falls under the GNU general public license version 3 or later.
00013 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00014 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00015 ******************************************************************************/
00016 
00017 #include <stdio.h>
00018 #include <stdlib.h>
00019 #include <string.h>
00020 
00021 #define DATA_BEGIN   ((char) 2)
00022 #define DATA_END     ((char) 5)
00023 #define DATA_ESCAPE  ((char) 27)
00024 
00025 static int counter= 0;
00026 static int buffer_len= 4096; // FIXME yes i am aware of the error here
00027 static char buffer [4096];
00028 
00029 void
00030 next_input () {
00031   counter++;
00032   printf ("%cchannel:prompt%c", DATA_BEGIN, DATA_END);
00033   printf ("Graphviz %d] ", counter);
00034 }
00035 
00036 void
00037 read_input () {
00038   int i;
00039   for (i=0; i<buffer_len-1; i++) {
00040     buffer[i]= fgetc (stdin);
00041     if (buffer[i] == '\n') break;
00042   }
00043   buffer[i]='\0';
00044 }
00045 
00046 /******************************************************************************
00047 * Main process       Dotty
00048 * (stdinpipe0)  ---> (stdin, stdinpipe1)
00049 * (stdoutpipe1) <--- (stdout, stdoutpipe0)
00050 * (errpipe1)    <--- (stderr, errpipe0)
00051 *
00052 * int stdinpipe[2];
00053 * int stdoutpipe[2];
00054 * int stderrpipe[2];
00055 * pipe( stdinpipe );  
00056 *
00057 * FIXME (it should be implemented as above, but I really cannot
00058 * be bothered to do this atm..pipes  can be such a kludge)
00059 ******************************************************************************/
00060 
00061 char *
00062 readfile( FILE * fp ) {
00063   char * buf = (char*)malloc(1025);
00064   char *wbuf = buf;
00065   int buflen=1025;
00066 
00067   while(!feof(fp))
00068   {
00069     int br = fread( wbuf, 1, 1024, fp );
00070     if(br==-1 || br==0)
00071        break;
00072     wbuf += br;
00073     buflen+=1024;
00074     buf = (char*)realloc( buf, buflen );
00075   }
00076   *wbuf='\0';
00077   return buf;
00078 }
00079 
00080 // output and errors are allocated by this routine
00081 void
00082 run_dotty( char * input, char ** output, char ** errors ) {
00083   FILE *wfp, *fp, *efp;
00084 
00085   wfp = fopen("dotty.tmp.dot", "w");
00086   fprintf(wfp, "%s", input );
00087   fclose(wfp);
00088 
00089   fp = popen( "dot -Tps dotty.tmp.dot 2>dotty.tmp.err", "r" );
00090   *output = readfile(fp);
00091   fclose(fp);
00092 
00093   efp = fopen("dotty.tmp.err","r");
00094   *errors = readfile(efp);
00095   fclose(efp); 
00096 }
00097 
00098 void
00099 build_input () {
00100   FILE *foutp = fopen ("dotty.tmp.in", "a");
00101   fprintf (foutp, "%s", buffer);
00102   fclose (foutp);
00103 }
00104 
00105 int
00106 main () {
00107   printf ("%cverbatim:", DATA_BEGIN);
00108   printf ("Welcome to a simple TeXmacs interface to Graphviz/dot\n");
00109   printf ("(C) 2002 Jorik Blaas and Joris van der Hoeven\n");
00110   next_input ();
00111   printf ("%c", DATA_END);
00112   fflush (stdout);
00113 
00114   while (!feof (stdin)) {
00115     char * out;
00116     char * err;
00117 
00118     read_input ();
00119     if (strcmp (buffer, "quit") == 0) break;
00120 
00121     printf ("%cps:", DATA_BEGIN);
00122     run_dotty (buffer, &out, &err);
00123     build_input ();
00124     printf ("%s", out);
00125     free (out);
00126     if (strlen(err)>0)
00127     {
00128       printf ("%cverbatim:", DATA_BEGIN);
00129       printf ("\nErrors: %s\n", err);
00130     }
00131     free(err);
00132     printf ("%c\n", DATA_END);
00133 
00134     next_input ();
00135     printf ("%c", DATA_END);
00136     fflush (stdout);
00137   }
00138   return 0;
00139 }