[BACK]Return to ox_texmacs.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / kxx

Annotation of OpenXM/src/kxx/ox_texmacs.c, Revision 1.20

1.20    ! takayama    1: /* $OpenXM: OpenXM/src/kxx/ox_texmacs.c,v 1.19 2004/09/05 00:51:18 takayama Exp $ */
1.1       takayama    2:
                      3: #include <stdio.h>
                      4: #include <setjmp.h>
                      5: #include <signal.h>
                      6: #include "ox_kan.h"
                      7: #include "serversm.h"
                      8:
                      9: #if defined(__CYGWIN__)
                     10: #define JMP_BUF sigjmp_buf
                     11: #define SETJMP(env)  sigsetjmp(env,1)
                     12: #define LONGJMP(env,p)  siglongjmp(env,p)
                     13: #else
                     14: #define JMP_BUF jmp_buf
                     15: #define SETJMP(env)  setjmp(env)
                     16: #define LONGJMP(env,p)  longjmp(env,p)
                     17: #endif
                     18:
1.3       takayama   19: /*
                     20: #define DEBUG
                     21: */
1.7       takayama   22: /* #define DEBUG2 */
1.3       takayama   23:
1.2       takayama   24: #ifdef DEBUG
                     25: #define DATA_BEGIN_V  "<S type=verbatim>"     /* "\002verbatim:" */
                     26: #define DATA_BEGIN_L  "<S type=latex>"        /* "\002latex:" */
                     27: #define DATA_BEGIN_P  "<S type=prompt>"        /* "\002channel:prompt " */
1.17      takayama   28: #define DATA_BEGIN_PS  "<S type=postscript>"        /* "\002ps: " */
1.2       takayama   29: #define DATA_END      "</S>"    /* "\005" */
                     30: #else
                     31: #define DATA_BEGIN_V  "\002verbatim:"
                     32: #define DATA_BEGIN_L  "\002latex:"
                     33: #define DATA_BEGIN_P  "\002prompt:"
1.17      takayama   34: #define DATA_BEGIN_PS  "\002ps:"
1.2       takayama   35: #define DATA_END      "\005"
                     36: #endif
                     37:
                     38: /*
                     39: #define END_OF_INPUT  '#'
                     40: */
                     41: #define END_OF_INPUT '\n'
1.1       takayama   42:
1.5       takayama   43: /* Table for the engine type. */
                     44: #define ASIR          1
                     45: #define SM1           2
                     46: #define K0            3
                     47:
1.1       takayama   48: extern int Quiet;
                     49: extern JMP_BUF EnvOfStackMachine;
1.19      takayama   50: extern int Calling_ctrlC_hook;
1.20    ! takayama   51: extern int RestrictedMode, RestrictedMode_saved;
1.3       takayama   52: int Format=1;  /* 1 : latex mode */
                     53: int OutputLimit_for_TeXmacs = (1024*10);
1.1       takayama   54:
1.5       takayama   55: int TM_Engine  = ASIR ;
                     56: int TM_asirStarted = 0;
                     57: int TM_sm1Started  = 0;
                     58: int TM_k0Started  = 0;
1.7       takayama   59: int TM_do_not_print = 0;
1.5       takayama   60:
1.15      takayama   61: int Xm_noX = 0;
                     62: int NoCopyright = 0;
                     63:
1.1       takayama   64: void ctrlC();
                     65: struct object KpoString(char *s);
                     66: char *KSpopString(void);
                     67:
1.2       takayama   68: static char *readString(FILE *fp,char *prolog, char *eplog);
1.1       takayama   69: static void printv(char *s);
                     70: static void printl(char *s);
                     71: static void printp(char *s);
1.17      takayama   72: static void printps(char *s);
1.2       takayama   73: static void printCopyright(char *s);
1.5       takayama   74: static int startEngine(int type,char *msg);
1.17      takayama   75: static int isPS(char *s);
1.2       takayama   76:
                     77: /* tail -f /tmp/debug-texmacs.txt
                     78:    Debug output to understand the timing problem of pipe interface.
                     79: */
1.3       takayama   80: FILE *Dfp;
1.1       takayama   81:
1.10      takayama   82: main(int argc,char *argv[]) {
1.1       takayama   83:   char *s;
                     84:   char *r;
                     85:   char *sys;
                     86:   struct object ob;
1.3       takayama   87:   int irt=0;
1.4       takayama   88:   int vmode=1;
1.7       takayama   89:   char *openxm_home;
                     90:   char *asir_config;
1.10      takayama   91:   int i;
1.1       takayama   92:
1.7       takayama   93:   openxm_home = (char *) getenv("OpenXM_HOME");
                     94:   asir_config = (char *) getenv("ASIR_CONFIG");
                     95:   if (openxm_home == NULL || asir_config == NULL) {
1.8       takayama   96:     printv("The environmental variables OpenXM_HOME/ASIR_CONFIG are not set.\nStart the texmacs with openxm texmacs or ox_texmacs by openxm ox_texmacs\nBye...");
                     97:     exit(10);
1.7       takayama   98:   }
                     99:
                    100:
1.3       takayama  101: #ifdef DEBUG2
                    102:   Dfp = fopen("/tmp/debug-texmacs.txt","w");
                    103: #endif
1.2       takayama  104:
1.1       takayama  105:   /* Set consts */
                    106:   Quiet = 1;
1.10      takayama  107:   for (i=1; i<argc; i++) {
                    108:        if (strcmp(argv[i],"--sm1") == 0) {
                    109:          TM_Engine = SM1;
                    110:        }else if (strcmp(argv[i],"--asir") == 0) {
                    111:          TM_Engine = ASIR;
                    112:        }else if (strcmp(argv[i],"--k0") == 0) {
                    113:          TM_Engine = K0;
1.14      takayama  114:     }else if (strcmp(argv[i],"--outputLimit") == 0) {
                    115:       i++;
                    116:       sscanf(argv[i],"%d",&OutputLimit_for_TeXmacs);
1.15      takayama  117:     }else if (strcmp(argv[i],"--noLogWindow") == 0) {
                    118:          Xm_noX = 1;
                    119:     }else if (strcmp(argv[i],"--noCopyright") == 0) {
                    120:          NoCopyright = 1;
1.10      takayama  121:        }else{
1.12      takayama  122:          /* printv("Unknown option\n"); */
1.10      takayama  123:        }
                    124:   }
1.1       takayama  125:
                    126:   /* Initialize kanlib (gc is also initialized) */
                    127:   KSstart();
                    128:
1.3       takayama  129:   /* Main loop */
                    130:   printf("%s",DATA_BEGIN_V);
                    131:   printCopyright("");
                    132:
1.1       takayama  133:   /* Load ox engine here */
                    134:   /* engine id should be set to ox.engine */
                    135:   KSexecuteString(" [(parse) (ox.sm1) pushfile] extension ");
1.15      takayama  136:   if (Xm_noX) KSexecuteString(" /Xm_noX 1 def ");
1.5       takayama  137:   startEngine(TM_Engine," ");
1.1       takayama  138:
                    139:   if (signal(SIGINT,SIG_IGN) != SIG_IGN) {
1.8       takayama  140:     signal(SIGINT,ctrlC);
1.1       takayama  141:   }
1.3       takayama  142:
                    143:   irt = 0;
1.1       takayama  144:   while(1) {
1.8       takayama  145:     /* printp(sys);  no prompt */
                    146:     if (SETJMP(EnvOfStackMachine)) {
1.19      takayama  147:       if (!Calling_ctrlC_hook) {
1.20    ! takayama  148:         Calling_ctrlC_hook = 1; RestrictedMode = 0;
1.19      takayama  149:         KSexecuteString(" ctrlC-hook "); /* Execute User Defined functions. */
1.20    ! takayama  150:         RestrictedMode = RestrictedMode_saved;
1.19      takayama  151:       }
                    152:       Calling_ctrlC_hook = 0;
1.8       takayama  153:       if (signal(SIGINT,SIG_IGN) != SIG_IGN) {
                    154:         signal(SIGINT,ctrlC);
                    155:       }
                    156:       irt = 1;
                    157:       continue;
                    158:     } else {  }
                    159:     if (!irt) {
                    160:       printf("%s",DATA_END); fflush(stdout);
                    161:     }
                    162:     irt = 0;
1.7       takayama  163:
                    164:     /* Reading the input. */
1.8       takayama  165:     if (TM_Engine == K0) {
                    166:       s=readString(stdin, " ", " "); /* see test data */
                    167:     }else if (TM_Engine == SM1) {
                    168:       s=readString(stdin, " ", " "); /* see test data */
                    169:     }else{
                    170:       s=readString(stdin, "if (1) { ", " ; }else{ }"); /* see test data */
                    171:     }
1.5       takayama  172:
1.8       takayama  173:     if (s == NULL) { irt = 1; continue; }
                    174:     if (!irt) printf("%s",DATA_BEGIN_V);
1.7       takayama  175:     /* Evaluate the input on the engine */
1.1       takayama  176:     KSexecuteString(" ox.engine ");
1.7       takayama  177:     ob = KpoString(s);
1.8       takayama  178:     KSpush(ob);
                    179:     KSexecuteString(" oxsubmit ");
                    180:
1.1       takayama  181:     /* Get the result in string. */
1.8       takayama  182:     if (Format == 1 && (! TM_do_not_print)) {
                    183:       /* translate to latex form */
                    184:       KSexecuteString(" ox.engine oxpushcmotag ox.engine oxpopcmo ");
                    185:       ob = KSpop();
                    186:       vmode = 0;
                    187:       /* printf("id=%d\n",ob.tag); bug: matrix return 17 instead of Sinteger
                    188:        or error. */
                    189:       if (ob.tag == Sinteger) {
                    190:         /* printf("cmotag=%d\n",ob.lc.ival);*/
                    191:         if (ob.lc.ival == CMO_ERROR2) {
                    192:           vmode = 1;
                    193:         }
                    194:         if (ob.lc.ival == CMO_STRING) {
                    195:           vmode = 1;
                    196:         }
                    197:       }
                    198:       if (vmode) {
                    199:         KSexecuteString(" ox.engine oxpopstring ");
                    200:         r = KSpopString();
                    201:       }else{
                    202:         KSexecuteString(" ox.engine 1 oxpushcmo ox.engine (print_tex_form) oxexec  ");
                    203:         KSexecuteString(" ox.engine oxpopstring ");
                    204:         r = KSpopString();
                    205:       }
1.17      takayama  206:       if (isPS(r)) {
                    207:         printps(r);
                    208:       }else{
                    209:         if (vmode) printv(r);
                    210:         else{
                    211:           if (strlen(r) < OutputLimit_for_TeXmacs) {
                    212:             printl(r);
                    213:           } else printv("Output is too large.\n");
                    214:         }
                    215:       }
1.8       takayama  216:     }else{
                    217:       if (!TM_do_not_print) {
                    218:         KSexecuteString(" ox.engine oxpopstring ");
                    219:         r = KSpopString();
1.17      takayama  220:         printv(r);
1.8       takayama  221:       }else{
                    222:         KSexecuteString(" ox.engine 1 oxpops "); /* Discard the result. */
1.12      takayama  223:         /* Push and pop dummy data to wait until the computation finishes. */
                    224:         KSexecuteString(" ox.engine 0 oxpushcmo ox.engine oxpopcmo ");
                    225:         ob = KSpop();
1.8       takayama  226:         printv("");
                    227:       }
                    228:     }
1.1       takayama  229:   }
                    230: }
                    231:
                    232: #define SB_SIZE 1024
1.8       takayama  233: #define INC_BUF     if (n >= limit-3) { \
                    234:       tmp = s; \
                    235:       limit *= 2;  \
                    236:       s = (char *) sGC_malloc(limit); \
                    237:       if (s == NULL) { \
                    238:         fprintf(stderr,"No more memory.\n"); \
                    239:         exit(10); \
                    240:       } \
                    241:       strcpy(s,tmp); \
                    242:     }
1.2       takayama  243: /*   */
                    244: static char *readString(FILE *fp, char *prolog, char *epilog) {
1.1       takayama  245:   int n = 0;
                    246:   static int limit = 0;
                    247:   static char *s;
                    248:   int c;
                    249:   char *tmp;
1.2       takayama  250:   int i;
1.3       takayama  251:   int m;
                    252:   int start;
1.13      takayama  253:   struct object ob;
1.1       takayama  254:   if (limit == 0) {
1.8       takayama  255:     limit = 1024;
                    256:     s = (char *)sGC_malloc(limit);
                    257:     if (s == NULL) {
                    258:       fprintf(stderr,"No more memory.\n");
                    259:       exit(10);
                    260:     }
1.1       takayama  261:   }
1.2       takayama  262:   s[0] = 0; n = 0; m = 0;
                    263:   for (i=0; i < strlen(prolog); i++) {
1.8       takayama  264:     s[n++] = prolog[i];  s[n] = 0;
1.2       takayama  265:     INC_BUF ;
                    266:   }
1.3       takayama  267:   start = n;
1.1       takayama  268:   while ((c = fgetc(fp)) != EOF) {
1.3       takayama  269: #ifdef DEBUG2
1.8       takayama  270:     fprintf(Dfp,"[%x] ",c); fflush(Dfp);
1.3       takayama  271: #endif
1.8       takayama  272:     if (c == END_OF_INPUT) {
1.18      takayama  273:       /* If there remains data in the stream,
                    274:          read the remaining data. */
                    275:          /*
1.8       takayama  276:       if (oxSocketSelect0(0,1)) {
                    277:         if (c == '\n') c=' ';
                    278:         s[n++] = c; s[n] = 0;  m++;
                    279:         INC_BUF ;
                    280:         continue;
                    281:       }
1.18      takayama  282:       */
1.8       takayama  283:       break;
                    284:     }
1.18      takayama  285:     if ( c == '\v') c=' ';
1.8       takayama  286:     s[n++] = c; s[n] = 0;  m++;
1.2       takayama  287:     INC_BUF ;
                    288:   }
1.5       takayama  289:   /* Check the escape sequence */
                    290:   if (strcmp(&(s[start]),"!quit;") == 0) {
1.8       takayama  291:     printv("Terminated the process ox_texmacs.\n");
                    292:     exit(0);
1.3       takayama  293:   }
1.11      takayama  294:   /* Check the escape sequence to change the global env. */
1.5       takayama  295:   if (strcmp(&(s[start]),"!verbatim;") == 0) {
1.8       takayama  296:     printv("Output mode is changed to verbatim mode.");
                    297:     Format=0;
                    298:     return NULL;
1.5       takayama  299:   }
                    300:   if (strcmp(&(s[start]),"!latex;") == 0) {
1.8       takayama  301:     printv("Output mode is changed to latex/verbose.");
                    302:     Format = 1;
                    303:     return NULL;
1.5       takayama  304:   }
                    305:   if (strcmp(&(s[start]),"!asir;") == 0) {
1.8       takayama  306:     Format=1;
                    307:     TM_Engine=ASIR; startEngine(TM_Engine,"Asir");
                    308:     return NULL;
1.5       takayama  309:   }
                    310:   if (strcmp(&(s[start]),"!sm1;") == 0) {
1.8       takayama  311:     Format=0;
                    312:     TM_Engine=SM1; startEngine(TM_Engine,"sm1");
                    313:     return NULL;
1.5       takayama  314:   }
                    315:   if (strcmp(&(s[start]),"!k0;") == 0) {
1.8       takayama  316:     Format=0;
                    317:     TM_Engine=K0; startEngine(TM_Engine,"k0");
                    318:     return NULL;
1.5       takayama  319:   }
1.13      takayama  320:   if (strcmp(&(s[start]),"!reset;") == 0) {
                    321:        printf("%s",DATA_BEGIN_V);
                    322:     KSexecuteString(" ox.engine oxreset ox.engine oxpopcmo ");
                    323:        ob = KSpop();
                    324:        printf("%s",DATA_END); fflush(stdout);
                    325:     return NULL;
                    326:   }
1.11      takayama  327:
                    328:   /* Set TM_do_no_print */
                    329:   if (s[n-1] == '$' && TM_Engine == ASIR) {
                    330:        TM_do_not_print = 1; s[n-1] = ' ';
                    331:   } else if (s[n-1] == ';' && TM_Engine == SM1) {
                    332:        TM_do_not_print = 1; s[n-1] = ' ';
                    333:   } else TM_do_not_print = 0;
1.5       takayama  334:
1.2       takayama  335:   for (i=0; i < strlen(epilog); i++) {
1.8       takayama  336:     s[n++] = epilog[i];  s[n] = 0;
1.2       takayama  337:     INC_BUF ;
1.1       takayama  338:   }
                    339:   return s;
                    340: }
                    341:
                    342: static void printv(char *s) {
                    343:   int i;
                    344:   printf("%s",DATA_BEGIN_V);
                    345:   printf("%s",s);
                    346:   printf("%s",DATA_END);
1.3       takayama  347: #ifdef DEBUG2
                    348:   fprintf(Dfp,"<%s>",s); fflush(Dfp);
                    349: #endif
1.1       takayama  350:   fflush(NULL);
                    351: }
                    352: static void printl(char *s) {
                    353:   printf("%s",DATA_BEGIN_L);
1.4       takayama  354:   printf(" $ %s $ ",s);
1.17      takayama  355:   printf("%s",DATA_END);
                    356:   fflush(NULL);
                    357: }
                    358: static int isPS(char *s) {
                    359:   if (s[0] != '%') return 0;
                    360:   if (s[1] != '%') return 0;
                    361:   if (s[2] != '!') return 0;
                    362:   if (s[3] != 'P') return 0;
                    363:   if (s[4] != 'S') return 0;
                    364:   return 1;
                    365: }
                    366: static void printps(char *s) {
                    367:   printf("%s",DATA_BEGIN_PS);
                    368:   printf("%s",s);
1.1       takayama  369:   printf("%s",DATA_END);
                    370:   fflush(NULL);
                    371: }
                    372: static void printp(char *s) {
                    373:   printf("%s",DATA_BEGIN_P);
1.2       takayama  374:   printf("%s",DATA_END);
                    375:   printf("%s] ",s);
                    376:   fflush(NULL);
                    377: }
                    378: static void printCopyright(char *s) {
1.3       takayama  379:   printf("%s",DATA_BEGIN_V);
1.15      takayama  380:   if (! NoCopyright) {
                    381:     printf("OpenXM engine (ox engine) interface for TeXmacs\n2004 (C) openxm.org");
1.16      takayama  382:     printf(" under the BSD license.  !asir; !sm1; !k0; !verbatim;\n");
1.15      takayama  383:     printf("Type in      !reset;     when the engine gets confused. ");
                    384:     printf("%s",s);
                    385:   }
1.5       takayama  386:   printf("%s",DATA_END);
                    387:   fflush(NULL);
                    388: }
                    389:
                    390: static int startEngine(int type,char *msg) {
                    391:   struct object ob;
                    392:   printf("%s",DATA_BEGIN_V);
                    393:   if (type == SM1) {
                    394:     if (!TM_sm1Started) KSexecuteString(" sm1connectr ");
1.8       takayama  395:     KSexecuteString(" /ox.engine oxsm1.ccc def ");
1.10      takayama  396:     /* Initialize the setting of sm1. */
                    397:     KSexecuteString("  oxsm1.ccc ( [(cmoLispLike) 0] extension ) oxsubmit ");
                    398:     KSexecuteString("  oxsm1.ccc ( ox_server_mode ) oxsubmit ");
                    399:     KSexecuteString("  oxsm1.ccc ( ( ) message (------------- Message from sm1 ----------------)message ) oxsubmit ");
1.8       takayama  400:     TM_sm1Started = 1;
1.10      takayama  401:        /* Welcome message.  BUG. Copyright should be returned by a function. */
1.15      takayama  402:     if (! NoCopyright) {
                    403:       printf("Kan/StackMachine1                         1991 April --- 2004.\n");
                    404:       printf("This software may be freely distributed as is with no warranty expressed. \n");
                    405:       printf("See OpenXM/Copyright/Copyright.generic\n");
                    406:       printf("Info: http://www.math.kobe-u.ac.jp/KAN, kan@math.kobe-u.ac.jp.\n");
                    407:       printf("0 usages to show a list of functions. \n(keyword) usages to see a short description\n");
                    408:     }
1.8       takayama  409:     printf("%s\n",msg);
1.5       takayama  410:   }else if (type == K0) {
                    411:     if (!TM_k0Started) KSexecuteString(" k0connectr ");
1.8       takayama  412:     KSexecuteString(" /ox.engine oxk0.ccc def ");
                    413:     TM_k0Started = 1;
                    414:     printf("%s\n",msg);
1.5       takayama  415:   }else{
                    416:     if (!TM_asirStarted) KSexecuteString(" asirconnectr ");
1.8       takayama  417:     KSexecuteString(" /ox.engine oxasir.ccc def ");
                    418:     TM_asirStarted = 1;
                    419:     printf("%s\n",msg);
1.15      takayama  420:     if ( ! NoCopyright) {
                    421:       KSexecuteString(" oxasir.ccc (copyright()+asir_contrib_copyright();) oxsubmit oxasir.ccc oxpopstring ");
                    422:       ob = KSpop();
                    423:       if (ob.tag == Sdollar) {
                    424:         printf("%s",ob.lc.str);
                    425:       }
1.8       takayama  426:     }
1.7       takayama  427:     /* Initialize the setting of asir. */
                    428:     KSexecuteString(" oxasir.ccc (if(1) {  Xm_server_mode = 1; Xm_helpdir = \"help-eg\";  } else { ; } ;) oxsubmit oxasir.ccc oxpopcmo ");
                    429:     KSexecuteString(" oxasir.ccc (if(1) {  ctrl(\"message\",0);  } else { ; } ;) oxsubmit oxasir.ccc oxpopcmo ");
                    430:     /* bug; if ctrl is written with Xm_helpdir = ... without oxpopcmo, then it does
                    431:        not work. */
1.9       takayama  432:     KSexecuteString(" oxasir.ccc (print(\"----------- Messages from asir ------------------------------\")$ ) oxsubmit oxasir.ccc oxpopcmo ");
1.5       takayama  433:   }
1.1       takayama  434:   printf("%s",DATA_END);
                    435:   fflush(NULL);
                    436: }
1.2       takayama  437:
                    438: /* test data
                    439:
                    440: 1.  print("hello"); print("afo");
                    441:
                    442:     1+2;
                    443:
                    444: 2. def foo(N) { for (I=0; I<10; I++) {   --> error
                    445:
                    446:    3+5;
                    447:
                    448: 4.  print("hello"); shift+return print("afo");
                    449:
                    450: */
                    451:
                    452:
                    453:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>