Back to index

texmacs  1.0.7.15
tm_maple_9.c
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : tm_maple_9.c
00004 * DESCRIPTION: Interface with Maple
00005 * COPYRIGHT  : (C) 2005 Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 #include <signal.h>
00015 #include <sys/types.h>
00016 #include <time.h>
00017 #include "maplec.h"
00018 
00019 #define DATA_BEGIN   ((char) 2)
00020 #define DATA_END     ((char) 5)
00021 #define DATA_ESCAPE  ((char) 27)
00022 //#define DATA_BEGIN   "[BEGIN]"
00023 //#define DATA_END     "[END]"
00024 //#define DATA_ESCAPE  "[ESCAPE]"
00025 
00026 static int counter= 0;
00027 static char in [2148];
00028 static char err[2048];
00029 
00030 /******************************************************************************
00031 * Handling maple output
00032 ******************************************************************************/
00033 
00034 void
00035 next_input () {
00036   counter++;
00037   printf ("\2channel:prompt\5");
00038   printf ("\2scheme:(with \"color\" \"brown\" \"");
00039   printf ("%s%i%s", "Maple ", counter, "] ");
00040   printf ("\")\5");
00041 }
00042 
00043 /******************************************************************************
00044 * Maple callbacks
00045 ******************************************************************************/
00046 
00047 #ifdef _MSC_VER
00048 #  define CDECL __cdecl
00049 #else
00050 #  define CDECL
00051 #endif
00052 
00053 /* global variable used by queryInterrupt() */
00054 static int Interrupted = 0;
00055 
00056 /* interrupt signal handler: sets global variable when user hits Ctrl-C */
00057 void CDECL catch_intr( int signo )
00058 {
00059   Interrupted = TRUE;
00060   signal(SIGINT,catch_intr);
00061 #ifdef _MSC_VER
00062   signal(SIGBREAK,catch_intr);
00063 #endif
00064 }
00065 
00066 /* interrupt callback: stops computation when `Interrupted' is true */
00067 static M_BOOL M_DECL queryInterrupt( void *data )
00068 {
00069   if (Interrupted) {
00070     Interrupted = 0;
00071     return (TRUE);
00072   }
00073   return (FALSE);
00074 }
00075 
00076 /* callback used for directing help output */
00077 static M_BOOL M_DECL writeHelpChar( void *data, int c )
00078 {
00079     putchar (c);
00080     return (FALSE);
00081 }
00082 
00083 /* callback used for directing result output */
00084 static void M_DECL textCallBack( void *data, int tag, char *output )
00085 {
00086   if (tag != MAPLE_TEXT_STATUS)
00087     printf ("%s\n", output);
00088 }
00089 
00090 static void M_DECL errorCallBack( void *data, M_INT offset, char *msg )
00091 {
00092   M_INT i;
00093   /* TODO: too wide (>= 80 characters) user input */
00094 
00095   if (offset < 0)
00096     fprintf (stderr, "%s\n", msg);
00097   else {
00098     /* put ^^^ under the original input to indicate where 
00099        the syntax error probably was
00100     */
00101     fprintf (stderr, "Syntax Error, %s\n> %s\n", msg, in);
00102     for (i=0; i<offset; ++i)
00103       fputc (' ', stderr);
00104     fprintf (stderr, "^\n");
00105   }
00106 }
00107 
00108 /******************************************************************************
00109 * Launching maple
00110 ******************************************************************************/
00111 
00112 int
00113 main (int argc, char *argv[]) {
00114   MKernelVector kv;  /* Maple kernel handle */
00115   MCallBackVectorDesc cb = {  textCallBack, 
00116                            errorCallBack,
00117                            0,   /* statusCallBack not used */
00118                            0,   /* readLineCallBack not used */
00119                            0,   /* redirectCallBack not used */
00120                            0,   /* streamCallBack not used */
00121                            queryInterrupt, 
00122                            0    /* callBackCallBack not used */
00123                            };
00124   ALGEB r, l;  /* Maple data-structures */
00125 
00126   signal(SIGINT,catch_intr);
00127   printf("\2verbatim:");
00128   printf("    |\\^/|     Maple\n");
00129   printf("._|\\|   |/|_. Copyright (c) Maplesoft, a division of Waterloo Maple Inc. 2004\n");
00130   printf(" \\OPENMAPLE/  All rights reserved. Maple and OpenMaple are trademarks of\n");
00131   printf(" <____ ____>  Waterloo Maple Inc.\n");
00132   printf("      |       Type ? for help.\n");
00133   printf("\nTeXmacs interface by Joris van der Hoeven\n");
00134   
00135   /* initialize Maple */
00136   if( (kv=StartMaple(argc,argv,&cb,NULL,NULL,err)) == NULL ) {
00137     printf("Fatal error, %s\n",err);
00138     return( 1 );
00139   }
00140 
00141   r= EvalMapleStatement (kv, "tmmaple:=9:");
00142   char* tm_path= getenv ("TEXMACS_PATH");
00143   char init[1000];
00144   strcpy (init, "read (`");
00145   strcat (init, tm_path);
00146   strcat (init, "/plugins/maple/maple/init-maple.mpl`);");
00147   r= EvalMapleStatement (kv, init);
00148 
00149   while (1) {
00150     next_input ();
00151     printf("\5");
00152     fflush (stdout);
00153     int i= 0;
00154     for (i=0; i<2047; i++) {
00155       char c= getchar ();
00156       if (c == '\n') break;
00157       in[i]= c;
00158     }
00159     in[i]= '\0';
00160     while (in[strlen(in)-1] == ';') in[strlen(in)-1]= '\0';
00161     if (strncmp (in, "quit", 4) == 0) break;
00162     printf("%c%s", DATA_BEGIN, "verbatim:");
00163     if (in[0] == '?')
00164       MapleHelp (kv, in+1, NULL, writeHelpChar, NULL, 80, NULL);
00165     else {
00166       if (in[strlen(in)-1] != ':')
00167        strcat (in, ":tmresult:=\%:tmprint(tmresult):tmresult:");
00168       r = EvalMapleStatement (kv, in);
00169     }
00170   }
00171 
00172   StopMaple(kv);
00173 
00174   return (0);
00175 }