[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.43

1.43    ! takayama    1: /* $OpenXM: OpenXM/src/kxx/ox_texmacs.c,v 1.42 2018/09/07 01:21:31 takayama Exp $ */
1.1       takayama    2:
                      3: #include <stdio.h>
1.23      takayama    4: #include <stdlib.h>
                      5: #include <string.h>
1.1       takayama    6: #include <setjmp.h>
                      7: #include <signal.h>
                      8: #include "ox_kan.h"
                      9: #include "serversm.h"
1.33      takayama   10: #include "ox_pathfinder.h"
1.1       takayama   11:
1.37      takayama   12: #if defined(__CYGWIN__) || defined(__MSYS__)
1.1       takayama   13: #define JMP_BUF sigjmp_buf
1.37      takayama   14: #define SETJMP(env)  MYSIGSETJMP(env,1)
                     15: #define LONGJMP(env,p)  MYSIGLONGJMP(env,p)
1.1       takayama   16: #else
                     17: #define JMP_BUF jmp_buf
1.37      takayama   18: #define SETJMP(env)  MYSETJMP(env)
                     19: #define LONGJMP(env,p)  MYLONGJMP(env,p)
1.1       takayama   20: #endif
                     21:
1.3       takayama   22: /*
                     23: #define DEBUG
                     24: */
1.43    ! takayama   25: /*
        !            26: #define DEBUG2
        !            27: */
1.3       takayama   28:
1.23      takayama   29: /* Type of View part (user interface engine) */
                     30: #define  GENERIC      0   /* DEBUG, xml */
                     31: #define  V_TEXMACS    1
                     32: #define  V_CFEP       2
1.40      takayama   33: #define  V_QFEP       3
1.42      takayama   34: #define  V_SAGE       4
1.43    ! takayama   35: #define  V_JUPYTER       5
1.23      takayama   36: int View       = V_TEXMACS ;
                     37:
                     38: char *Data_begin_v[] = {
                     39:   "<S type=verbatim>",
                     40:   "\002verbatim:",
                     41:   "\002",
1.42      takayama   42:   "",
1.43    ! takayama   43:   "",
1.40      takayama   44:   ""
1.23      takayama   45: };
                     46: char *Data_begin_l[] = {
                     47:   "<S type=latex>",
                     48:   "\002latex:",
1.40      takayama   49:   "\002latex:",
1.42      takayama   50:   "",
1.43    ! takayama   51:   "",
1.40      takayama   52:   ""
1.23      takayama   53: };
                     54: char *Data_begin_p[] = {
                     55:   "<S type=prompt>",
                     56:   "\002prompt:",
1.40      takayama   57:   "\002prompt:",
1.42      takayama   58:   "",
1.43    ! takayama   59:   "",
1.40      takayama   60:   ""
1.23      takayama   61: };
                     62: char *Data_begin_ps[] = {
                     63:   "<S type=postscript>",
                     64:   "\002ps:",
1.40      takayama   65:   "\002ps:",
1.42      takayama   66:   "",
1.43    ! takayama   67:   "",
1.40      takayama   68:   ""
1.23      takayama   69: };
                     70: char *Data_end[] = {
                     71:   "</S>",
                     72:   "\005",
1.40      takayama   73:   "\n\005",    /* \n is not a part of the protocol. */
1.42      takayama   74:   "",
1.43    ! takayama   75:   "",          /* SAGE */
        !            76:   "\n"
1.23      takayama   77: };
                     78:
1.25      takayama   79: /* todo:  start_of_input */
1.2       takayama   80:
1.25      takayama   81: char End_of_input[] = {
                     82:   0x5,              /* Use ^E and Return to end the input. */
                     83:   '\n',  /* TEXMACS_END_OF_INPUT. 0xd should be used for multiple lines. */
1.40      takayama   84:   0x5,    /* CFEP_END_OF_INPUT    */
1.42      takayama   85:   0x5,
1.43    ! takayama   86:   '\n',   /* SAGE cf. __init__ */
        !            87:   '\n'
1.42      takayama   88: };
                     89:
1.43    ! takayama   90: /* Note! if you give more than 10 Views, increase the number 10  below.
        !            91:    See the option --prompt which should be given after --view is set.
        !            92: */
        !            93: char *Prompt[10]={
1.42      takayama   94:   "generic>", /* generic */
                     95:   "", /* texmacs */
                     96:   "",    /* cfep */
                     97:   "",   /* qfep */
1.43    ! takayama   98:   "\nasir>",    /* SAGE */
        !            99:   "\nPEXPECT_PROMPT>"
1.25      takayama  100: };
1.1       takayama  101:
1.42      takayama  102:
1.5       takayama  103: /* Table for the engine type. */
                    104: #define ASIR          1
                    105: #define SM1           2
                    106: #define K0            3
                    107:
1.1       takayama  108: extern int Quiet;
                    109: extern JMP_BUF EnvOfStackMachine;
1.19      takayama  110: extern int Calling_ctrlC_hook;
1.20      takayama  111: extern int RestrictedMode, RestrictedMode_saved;
1.33      takayama  112: int Format=1;  /* 1 : latex mode, 2: cfep_png */
1.3       takayama  113: int OutputLimit_for_TeXmacs = (1024*10);
1.1       takayama  114:
1.22      takayama  115:
1.5       takayama  116: int TM_Engine  = ASIR ;
                    117: int TM_asirStarted = 0;
                    118: int TM_sm1Started  = 0;
                    119: int TM_k0Started  = 0;
1.7       takayama  120: int TM_do_not_print = 0;
1.5       takayama  121:
1.15      takayama  122: int Xm_noX = 0;
                    123: int NoCopyright = 0;
1.25      takayama  124: int Cpp = 0;                 /* Use cpp before sending to the engine. */
                    125: int EngineLogToStdout = 0;   /* Do not run the ox engine inside xterm. */
1.15      takayama  126:
1.33      takayama  127: char *AsirInitFile = NULL;
1.32      takayama  128:
1.29      takayama  129: char *LanguageResource = NULL;
1.41      takayama  130: int TM_quiet = 0;
1.29      takayama  131:
1.1       takayama  132: void ctrlC();
                    133: struct object KpoString(char *s);
                    134: char *KSpopString(void);
                    135:
1.2       takayama  136: static char *readString(FILE *fp,char *prolog, char *eplog);
1.1       takayama  137: static void printv(char *s);
                    138: static void printl(char *s);
                    139: static void printp(char *s);
1.17      takayama  140: static void printps(char *s);
1.2       takayama  141: static void printCopyright(char *s);
1.5       takayama  142: static int startEngine(int type,char *msg);
1.17      takayama  143: static int isPS(char *s);
1.22      takayama  144: static int end_of_input(int c);
                    145: static void setDefaultParameterForCfep();
1.42      takayama  146: static void setDefaultParameterForSage();
1.43    ! takayama  147: static void setDefaultParameterForJupyter();
1.2       takayama  148:
1.27      takayama  149: static void myEncoder(int c);
                    150: static void myEncoderS(unsigned char *s);
                    151: static void myEncoderSn(unsigned char *s,int n);
                    152: static void outputStringToTunnel0(int channel, unsigned char *s, int size, int view);
                    153: static void outputStringToTunnel(int channel, unsigned char *s,  int view);
                    154:
1.33      takayama  155: static void pngSendFile(char *path);
                    156: static int pngCheck(void);
                    157: static void pngNotAvailable(void);
                    158: static char *pngGetResult();
                    159:
1.31      takayama  160: static void flushSm1();
                    161:
1.2       takayama  162: /* tail -f /tmp/debug-texmacs.txt
                    163:    Debug output to understand the timing problem of pipe interface.
                    164: */
1.3       takayama  165: FILE *Dfp;
1.1       takayama  166:
1.10      takayama  167: main(int argc,char *argv[]) {
1.1       takayama  168:   char *s;
                    169:   char *r;
                    170:   char *sys;
1.21      takayama  171:   struct object ob = OINIT;
1.3       takayama  172:   int irt=0;
1.4       takayama  173:   int vmode=1;
1.7       takayama  174:   char *openxm_home;
                    175:   char *asir_config;
1.33      takayama  176:   char *path;
1.10      takayama  177:   int i;
1.1       takayama  178:
1.7       takayama  179:
                    180:
1.3       takayama  181: #ifdef DEBUG2
                    182:   Dfp = fopen("/tmp/debug-texmacs.txt","w");
                    183: #endif
1.41      takayama  184:
                    185:   TM_quiet=0;
                    186:   for (i=1; i<argc; i++) {
                    187:     if (strcmp(argv[i],"--quiet")==0) {
                    188:       TM_quiet=1;
                    189:     }
                    190:   }
1.36      takayama  191:   /* Initialize kanlib (gc is also initialized) */
1.41      takayama  192:   if (TM_quiet) {
                    193:     KSstart_quiet();
                    194:   }else{
                    195:     KSstart();
                    196:   }
1.1       takayama  197:   /* Set consts */
                    198:   Quiet = 1;
1.10      takayama  199:   for (i=1; i<argc; i++) {
1.22      takayama  200:     if (strcmp(argv[i],"--view") == 0) {
                    201:       i++;
                    202:       if (strcmp(argv[i],"texmacs") == 0) {
                    203:         View = V_TEXMACS;
                    204:       }else if (strcmp(argv[i],"cfep")==0) {
                    205:         View = V_CFEP; setDefaultParameterForCfep();
1.40      takayama  206:       }else if (strcmp(argv[i],"qfep")==0) {
                    207:        View = V_QFEP; setDefaultParameterForCfep();
1.42      takayama  208:       }else if (strcmp(argv[i],"sage")==0) {
                    209:        View = V_SAGE; setDefaultParameterForSage();
1.43    ! takayama  210:       }else if (strcmp(argv[i],"jupyter")==0) {
        !           211:        View = V_JUPYTER; setDefaultParameterForJupyter();
1.22      takayama  212:       }else{
1.25      takayama  213:         View = GENERIC;
1.22      takayama  214:         /* printv("Unknown view type.\n"); */
                    215:       }
                    216:     } else if (strcmp(argv[i],"--sm1") == 0) {
                    217:       TM_Engine = SM1;
                    218:     }else if (strcmp(argv[i],"--asir") == 0) {
                    219:       TM_Engine = ASIR;
                    220:     }else if (strcmp(argv[i],"--k0") == 0) {
                    221:       TM_Engine = K0;
1.14      takayama  222:     }else if (strcmp(argv[i],"--outputLimit") == 0) {
                    223:       i++;
                    224:       sscanf(argv[i],"%d",&OutputLimit_for_TeXmacs);
1.15      takayama  225:     }else if (strcmp(argv[i],"--noLogWindow") == 0) {
1.22      takayama  226:       Xm_noX = 1;
1.15      takayama  227:     }else if (strcmp(argv[i],"--noCopyright") == 0) {
1.22      takayama  228:       NoCopyright = 1;
1.25      takayama  229:     }else if (strcmp(argv[i],"--cpp") == 0) {
                    230:       Cpp = 1;
                    231:     }else if (strcmp(argv[i],"--engineLogToStdout") == 0) {
                    232:       EngineLogToStdout = 1;
1.29      takayama  233:     }else if (strcmp(argv[i],"--languageResource") == 0) {
                    234:       i++;
                    235:       LanguageResource = (char *)sGC_malloc(strlen(argv[i])+80);
                    236:       sprintf(LanguageResource,
                    237:                          " /localizedString.file (%s) def localizedString.load ",argv[i]);
1.32      takayama  238:     }else if (strcmp(argv[i],"--asirInitFile") == 0) {
                    239:       i++;
1.33      takayama  240:       AsirInitFile = (char *)sGC_malloc(strlen(argv[i])+80);
1.32      takayama  241:       sprintf(AsirInitFile,"%s",argv[i]);
1.41      takayama  242:     }else if (strcmp(argv[i],"--quiet") == 0) {
                    243:       /* it is already set. Ignore. */
1.43    ! takayama  244:     }else if (strcmp(argv[i],"--prompt")==0) {
        !           245:       i++;
        !           246:       Prompt[View] = (char *)sGC_malloc(strlen(argv[i])+2);
        !           247:       strcpy(Prompt[View],argv[i]);
1.22      takayama  248:     }else{
                    249:       /* printv("Unknown option\n"); */
                    250:     }
1.10      takayama  251:   }
1.1       takayama  252:
1.23      takayama  253:   openxm_home = (char *) getenv("OpenXM_HOME");
                    254:   asir_config = (char *) getenv("ASIR_CONFIG");
                    255:   if (openxm_home == NULL)  {
                    256:     printv("Error. The environmental variable OpenXM_HOME is not set.\nStart the texmacs with openxm texmacs or ox_texmacs by openxm ox_texmacs\nBye...");
                    257:     exit(10);
                    258:   }
                    259:
1.1       takayama  260:
1.3       takayama  261:   /* Main loop */
1.23      takayama  262:   printf("%s",Data_begin_v[View]);
                    263:   if ((asir_config == NULL) && (View == V_TEXMACS)) {
                    264:     printf("Warning. The environmental variable ASIR_CONFIG is not set.\nStart the texmacs with openxm texmacs or ox_texmacs by openxm ox_texmacs.\nOtherwise, tex translation will not be performed\n");
                    265:
                    266:   }
                    267:
1.3       takayama  268:   printCopyright("");
                    269:
1.23      takayama  270:
1.1       takayama  271:   /* Load ox engine here */
                    272:   /* engine id should be set to ox.engine */
                    273:   KSexecuteString(" [(parse) (ox.sm1) pushfile] extension ");
1.15      takayama  274:   if (Xm_noX) KSexecuteString(" /Xm_noX 1 def ");
1.26      takayama  275:   if (EngineLogToStdout) KSexecuteString(" /Xm_engineLogToStdout 1 def ");
1.29      takayama  276:   if (LanguageResource != NULL) KSexecuteString(LanguageResource);
1.5       takayama  277:   startEngine(TM_Engine," ");
1.1       takayama  278:
1.39      takayama  279:   if (mysignal(SIGINT,SIG_IGN) != SIG_IGN) {
                    280:     mysignal(SIGINT,ctrlC);
1.1       takayama  281:   }
1.3       takayama  282:
                    283:   irt = 0;
1.1       takayama  284:   while(1) {
1.8       takayama  285:     /* printp(sys);  no prompt */
                    286:     if (SETJMP(EnvOfStackMachine)) {
1.19      takayama  287:       if (!Calling_ctrlC_hook) {
1.20      takayama  288:         Calling_ctrlC_hook = 1; RestrictedMode = 0;
1.19      takayama  289:         KSexecuteString(" ctrlC-hook "); /* Execute User Defined functions. */
1.20      takayama  290:         RestrictedMode = RestrictedMode_saved;
1.19      takayama  291:       }
                    292:       Calling_ctrlC_hook = 0;
1.39      takayama  293:       if (mysignal(SIGINT,SIG_IGN) != SIG_IGN) {
                    294:         mysignal(SIGINT,ctrlC);
1.8       takayama  295:       }
                    296:       irt = 1;
                    297:       continue;
                    298:     } else {  }
                    299:     if (!irt) {
1.42      takayama  300:       if (View != V_SAGE) printf("%s",Data_end[View]); fflush(stdout);
1.8       takayama  301:     }
                    302:     irt = 0;
1.7       takayama  303:
                    304:     /* Reading the input. */
1.43    ! takayama  305:     if ((View == V_SAGE) || (View == V_JUPYTER)) {
        !           306:       if (s != NULL) {printf("%s",Prompt[View]); fflush(stdout);}
        !           307:       else {printf("%s",&(Prompt[View][1])); fflush(stdout);}
        !           308:     }else{
        !           309:       printf("%s",Prompt[View]); fflush(stdout);
        !           310:     }
1.8       takayama  311:     if (TM_Engine == K0) {
                    312:       s=readString(stdin, " ", " "); /* see test data */
                    313:     }else if (TM_Engine == SM1) {
                    314:       s=readString(stdin, " ", " "); /* see test data */
                    315:     }else{
                    316:       s=readString(stdin, "if (1) { ", " ; }else{ }"); /* see test data */
                    317:     }
1.5       takayama  318:
1.8       takayama  319:     if (s == NULL) { irt = 1; continue; }
1.23      takayama  320:     if (!irt) printf("%s",Data_begin_v[View]);
1.7       takayama  321:     /* Evaluate the input on the engine */
1.27      takayama  322:     KSexecuteString(" ox.engine oxclearstack ");
1.1       takayama  323:     KSexecuteString(" ox.engine ");
1.7       takayama  324:     ob = KpoString(s);
1.8       takayama  325:     KSpush(ob);
                    326:     KSexecuteString(" oxsubmit ");
1.31      takayama  327:
                    328:        if (TM_Engine == SM1) flushSm1();
1.8       takayama  329:
1.27      takayama  330:     /* Get the result in string for cfep. */
                    331:     if (View != V_TEXMACS) {
                    332:       KSexecuteString(" ox.engine oxgeterrors ");
                    333:       ob = KSpop();
                    334:       if (ob.tag == Sarray) {
                    335:         if (getoaSize(ob) > 0) {
                    336:           ob = getoa(ob,0);
                    337:           KSpush(ob);
                    338:           KSexecuteString(" translateErrorForCfep ");
                    339:           r = KSpopString();
                    340:           outputStringToTunnel(0,(unsigned char *)r,View);
                    341:         }
                    342:       }
                    343:       if (!TM_do_not_print) {
1.33      takayama  344:         if (Format == 2) { /* png mode */
                    345:           r = pngGetResult();
                    346:         }else{
                    347:           KSexecuteString(" ox.engine oxpopstring ");
                    348:           r = KSpopString();
                    349:         }
1.27      takayama  350:         printv(r);
                    351:       }else{
                    352:         KSexecuteString(" ox.engine 1 oxpops ");
                    353:         KSexecuteString(" ox.engine 0 oxpushcmo ox.engine oxpopcmo ");
                    354:         ob = KSpop();
                    355:         printv("");
                    356:       }
                    357:       continue;
                    358:     }
                    359:     /* Get the result in string for texmacs  */
1.33      takayama  360:     if ((Format == 1) && (! TM_do_not_print)) {
1.8       takayama  361:       /* translate to latex form */
                    362:       KSexecuteString(" ox.engine oxpushcmotag ox.engine oxpopcmo ");
                    363:       ob = KSpop();
                    364:       vmode = 0;
                    365:       /* printf("id=%d\n",ob.tag); bug: matrix return 17 instead of Sinteger
                    366:        or error. */
                    367:       if (ob.tag == Sinteger) {
                    368:         /* printf("cmotag=%d\n",ob.lc.ival);*/
                    369:         if (ob.lc.ival == CMO_ERROR2) {
                    370:           vmode = 1;
                    371:         }
                    372:         if (ob.lc.ival == CMO_STRING) {
                    373:           vmode = 1;
                    374:         }
                    375:       }
                    376:       if (vmode) {
                    377:         KSexecuteString(" ox.engine oxpopstring ");
                    378:         r = KSpopString();
                    379:       }else{
                    380:         KSexecuteString(" ox.engine 1 oxpushcmo ox.engine (print_tex_form) oxexec  ");
                    381:         KSexecuteString(" ox.engine oxpopstring ");
                    382:         r = KSpopString();
                    383:       }
1.17      takayama  384:       if (isPS(r)) {
                    385:         printps(r);
                    386:       }else{
                    387:         if (vmode) printv(r);
                    388:         else{
                    389:           if (strlen(r) < OutputLimit_for_TeXmacs) {
                    390:             printl(r);
                    391:           } else printv("Output is too large.\n");
                    392:         }
                    393:       }
1.8       takayama  394:     }else{
                    395:       if (!TM_do_not_print) {
                    396:         KSexecuteString(" ox.engine oxpopstring ");
                    397:         r = KSpopString();
1.17      takayama  398:         printv(r);
1.8       takayama  399:       }else{
                    400:         KSexecuteString(" ox.engine 1 oxpops "); /* Discard the result. */
1.12      takayama  401:         /* Push and pop dummy data to wait until the computation finishes. */
                    402:         KSexecuteString(" ox.engine 0 oxpushcmo ox.engine oxpopcmo ");
                    403:         ob = KSpop();
1.8       takayama  404:         printv("");
                    405:       }
                    406:     }
1.27      takayama  407:     /* note that there is continue above. */
1.1       takayama  408:   }
                    409: }
                    410:
1.33      takayama  411: static char *pngGetResult(void) {
                    412:   struct object ob;
                    413:   int vmode;
                    414:   char *r;
                    415:   char *path;
                    416:
                    417:   /* translate to latex form */
                    418:   KSexecuteString(" ox.engine oxpushcmotag ox.engine oxpopcmo ");
                    419:   ob = KSpop();
                    420:   vmode = 0;
                    421:   /* printf("id=%d\n",ob.tag); bug: matrix return 17 instead of Sinteger
                    422:      or error. */
                    423:   if (ob.tag == Sinteger) {
                    424:     /* printf("cmotag=%d\n",ob.lc.ival);*/
                    425:     if (ob.lc.ival == CMO_ERROR2) {
                    426:       vmode = 1;
                    427:     }
                    428:     if (ob.lc.ival == CMO_STRING) {
                    429:       vmode = 1;
                    430:     }
                    431:   }
                    432:   if (vmode) {
                    433:     KSexecuteString(" ox.engine oxpopstring ");
                    434:     r = KSpopString();
                    435:   }else{
                    436:     KSexecuteString(" ox.engine 1 oxpushcmo ox.engine (print_png_form2) oxexec  ");
                    437:     KSexecuteString(" ox.engine oxpopcmo /tmp_ox_texmacs set tmp_ox_texmacs 0 get ");
                    438:     r = KSpopString();  /* input form */
                    439:     KSexecuteString(" tmp_ox_texmacs 1 get ");
                    440:     path = KSpopString(); /* path name of the png file. */
                    441:     pngSendFile(path);
                    442:   }
                    443:   return r;
                    444: }
                    445:
1.1       takayama  446: #define SB_SIZE 1024
1.8       takayama  447: #define INC_BUF     if (n >= limit-3) { \
                    448:       tmp = s; \
                    449:       limit *= 2;  \
                    450:       s = (char *) sGC_malloc(limit); \
                    451:       if (s == NULL) { \
                    452:         fprintf(stderr,"No more memory.\n"); \
                    453:         exit(10); \
                    454:       } \
                    455:       strcpy(s,tmp); \
                    456:     }
1.2       takayama  457: /*   */
1.43    ! takayama  458: static char *Sss=NULL;
        !           459: static int Sss_size=10240;
        !           460:
1.2       takayama  461: static char *readString(FILE *fp, char *prolog, char *epilog) {
1.1       takayama  462:   int n = 0;
                    463:   static int limit = 0;
                    464:   static char *s;
                    465:   int c;
                    466:   char *tmp;
1.2       takayama  467:   int i;
1.3       takayama  468:   int m;
                    469:   int start;
1.21      takayama  470:   struct object ob = OINIT;
1.1       takayama  471:   if (limit == 0) {
1.8       takayama  472:     limit = 1024;
                    473:     s = (char *)sGC_malloc(limit);
                    474:     if (s == NULL) {
                    475:       fprintf(stderr,"No more memory.\n");
                    476:       exit(10);
                    477:     }
1.1       takayama  478:   }
1.2       takayama  479:   s[0] = 0; n = 0; m = 0;
                    480:   for (i=0; i < strlen(prolog); i++) {
1.8       takayama  481:     s[n++] = prolog[i];  s[n] = 0;
1.2       takayama  482:     INC_BUF ;
                    483:   }
1.3       takayama  484:   start = n;
1.1       takayama  485:   while ((c = fgetc(fp)) != EOF) {
1.3       takayama  486: #ifdef DEBUG2
1.43    ! takayama  487:     fprintf(Dfp,"[%x]%c ",c,c); fflush(Dfp);
1.3       takayama  488: #endif
1.22      takayama  489:     if (end_of_input(c)) {
1.18      takayama  490:       /* If there remains data in the stream,
1.25      takayama  491:          read the remaining data. (for debug) */
1.43    ! takayama  492:       /*
        !           493:       if ((View == V_JUPYTER) || (View == V_SAGE)) {
        !           494:         if (oxSocketSelect0(0,1)) {
        !           495: #ifdef DEBUG2
        !           496:           fprintf(Dfp,"<%x>%c ",c,c); fflush(Dfp);
        !           497: #endif
        !           498:           if (c == '\n') c=' ';
        !           499:           s[n++] = c; s[n] = 0;  m++;
        !           500:           INC_BUF ;
        !           501:           continue;
        !           502:         }
1.8       takayama  503:       }
1.18      takayama  504:       */
1.8       takayama  505:       break;
                    506:     }
1.18      takayama  507:     if ( c == '\v') c=' ';
1.8       takayama  508:     s[n++] = c; s[n] = 0;  m++;
1.2       takayama  509:     INC_BUF ;
                    510:   }
1.43    ! takayama  511:   if ((c == EOF) && (start == n)) exit(0);
1.5       takayama  512:   /* Check the escape sequence */
                    513:   if (strcmp(&(s[start]),"!quit;") == 0) {
1.8       takayama  514:     printv("Terminated the process ox_texmacs.\n");
                    515:     exit(0);
1.3       takayama  516:   }
1.43    ! takayama  517:   if (((View == V_JUPYTER) || (View == V_SAGE)) &&
        !           518:       ((strcmp(&(s[start]),"quit")==0) || (strcmp(&(s[start]),"quit()")==0))) {
        !           519:     printv("Terminated the process ox_texmacs.\n");
        !           520:     exit(0);
        !           521:   }
        !           522:   if ((View == V_JUPYTER) &&
        !           523:       (strcmp(&(s[start]),"version")==0)) {
        !           524:     s[n=strlen(s)]='('; s[++n]=0; INC_BUF;
        !           525:     s[n++]=')'; s[n]=0; INC_BUF;
        !           526:   }
1.11      takayama  527:   /* Check the escape sequence to change the global env. */
1.5       takayama  528:   if (strcmp(&(s[start]),"!verbatim;") == 0) {
1.33      takayama  529:     if (View != V_CFEP) printv("Output mode is changed to verbatim mode.");
1.8       takayama  530:     Format=0;
                    531:     return NULL;
1.5       takayama  532:   }
                    533:   if (strcmp(&(s[start]),"!latex;") == 0) {
1.8       takayama  534:     printv("Output mode is changed to latex/verbose.");
                    535:     Format = 1;
                    536:     return NULL;
1.5       takayama  537:   }
1.33      takayama  538:   if (strcmp(&(s[start]),"!cfep_png;") == 0) {
                    539:        if (pngCheck()) {
                    540:          if (View != V_CFEP) printv("Output mode is changed to cfep_png/verbose.");
                    541:       Format = 2;
                    542:     } else pngNotAvailable();
                    543:     return NULL;
                    544:   }
1.5       takayama  545:   if (strcmp(&(s[start]),"!asir;") == 0) {
1.8       takayama  546:     Format=1;
                    547:     TM_Engine=ASIR; startEngine(TM_Engine,"Asir");
                    548:     return NULL;
1.5       takayama  549:   }
                    550:   if (strcmp(&(s[start]),"!sm1;") == 0) {
1.8       takayama  551:     Format=0;
                    552:     TM_Engine=SM1; startEngine(TM_Engine,"sm1");
                    553:     return NULL;
1.5       takayama  554:   }
                    555:   if (strcmp(&(s[start]),"!k0;") == 0) {
1.8       takayama  556:     Format=0;
                    557:     TM_Engine=K0; startEngine(TM_Engine,"k0");
                    558:     return NULL;
1.5       takayama  559:   }
1.13      takayama  560:   if (strcmp(&(s[start]),"!reset;") == 0) {
1.23      takayama  561:        printf("%s",Data_begin_v[View]);
1.13      takayama  562:     KSexecuteString(" ox.engine oxreset ox.engine oxpopcmo ");
                    563:        ob = KSpop();
1.23      takayama  564:        printf("%s",Data_end[View]); fflush(stdout);
1.13      takayama  565:     return NULL;
                    566:   }
1.11      takayama  567:
1.32      takayama  568:   /* remove end$ or end; */
                    569:   if (TM_Engine == ASIR) {
                    570:     for (i=n-1; i>=4; i--) {
                    571:          if ((s[i-4]=='\n') && (s[i-3]=='e') && (s[i-2]=='n') && (s[i-1]=='d') && (s[i]=='$')) {
                    572:         s[i-3] = s[i-2] = s[i-1] = s[i] = ' '; break;
                    573:       }
                    574:          if ((s[i-4]=='\n') && (s[i-3]=='e') && (s[i-2]=='n') && (s[i-1]=='d') && (s[i]==';')) {
                    575:         s[i-3] = s[i-2] = s[i-1] = s[i] = ' '; break;
                    576:       }
                    577:     }
1.43    ! takayama  578:
        !           579:     /* 2019.03.28 for jupyter */
        !           580:     if ((strncmp(&(s[start]),"base_prompt",strlen("base_prompt")) != 0)
        !           581:         &&(strncmp(&(s[start]),"version()",strlen("version()")) != 0)) {
        !           582:       if (Sss == NULL) {
        !           583:           Sss = (char *)sGC_malloc(Sss_size);
        !           584:           Sss[0] = 0;
        !           585:       }
        !           586:       if (Sss_size < strlen(Sss)+strlen(s)+strlen(epilog)-10) {
        !           587:         Sss_size = 2*Sss_size;
        !           588:         if (Sss_size < 0) { fprintf(stderr,"Fatal error: too big string\n"); exit(-1); }
        !           589:         char *sss_tmp = (char *)sGC_malloc(Sss_size);
        !           590:        strcpy(sss_tmp,Sss);
        !           591:         Sss=sss_tmp;
        !           592:       }
        !           593:       strcpy(&(Sss[strlen(Sss)]),&(s[start]));
        !           594:       for (i=strlen(Sss); i>=0; i--) {
        !           595:        if (Sss[i] < 0x20) continue;
        !           596:         if (Sss[i] == '#') {
        !           597:           /* construct s and return s */
        !           598:           Sss[i] = ' ';
        !           599:           s = (char *) sGC_malloc(strlen(Sss)+strlen(prolog)+strlen(epilog)+10);
        !           600:           sprintf(s,"%s%s%s",prolog,Sss,epilog);
        !           601:           Sss[0]=0;
        !           602: #ifdef DEBUG2
        !           603:           fprintf(Dfp,"#By Sss: %s#\n",s); fflush(Dfp);
        !           604: #endif
        !           605:           return s;
        !           606:         }else break;
        !           607:       }
        !           608:       return NULL;
        !           609:     }
        !           610:   }else{
        !           611: #ifdef DEBUG2
        !           612:     fprintf(Dfp,"#base_prompt or version()#\n",s); fflush(Dfp);
        !           613: #endif
1.32      takayama  614:   }
1.43    ! takayama  615:   /* end 2019.03.28 for jupyter. */
1.32      takayama  616:
1.11      takayama  617:   /* Set TM_do_no_print */
                    618:   if (s[n-1] == '$' && TM_Engine == ASIR) {
                    619:        TM_do_not_print = 1; s[n-1] = ' ';
                    620:   } else if (s[n-1] == ';' && TM_Engine == SM1) {
                    621:        TM_do_not_print = 1; s[n-1] = ' ';
                    622:   } else TM_do_not_print = 0;
1.5       takayama  623:
1.2       takayama  624:   for (i=0; i < strlen(epilog); i++) {
1.8       takayama  625:     s[n++] = epilog[i];  s[n] = 0;
1.2       takayama  626:     INC_BUF ;
1.1       takayama  627:   }
                    628:   return s;
1.22      takayama  629: }
                    630:
                    631: static int end_of_input(int c) {
1.25      takayama  632:   if (c == End_of_input[View]) return 1;
                    633:   else return 0;
1.22      takayama  634: }
1.25      takayama  635:
1.22      takayama  636: static void setDefaultParameterForCfep() {
                    637:   Format = 0;
1.1       takayama  638: }
1.42      takayama  639: static void setDefaultParameterForSage() {
                    640:   Format = 0;
                    641: }
1.43    ! takayama  642: static void setDefaultParameterForJupyter() {
        !           643:   Format = 0;
        !           644: }
1.1       takayama  645:
                    646: static void printv(char *s) {
                    647:   int i;
1.23      takayama  648:   printf("%s",Data_begin_v[View]);
1.1       takayama  649:   printf("%s",s);
1.23      takayama  650:   printf("%s",Data_end[View]);
1.3       takayama  651: #ifdef DEBUG2
                    652:   fprintf(Dfp,"<%s>",s); fflush(Dfp);
                    653: #endif
1.1       takayama  654:   fflush(NULL);
                    655: }
                    656: static void printl(char *s) {
1.23      takayama  657:   printf("%s",Data_begin_l[View]);
1.4       takayama  658:   printf(" $ %s $ ",s);
1.23      takayama  659:   printf("%s",Data_end[View]);
1.17      takayama  660:   fflush(NULL);
                    661: }
                    662: static int isPS(char *s) {
                    663:   if (s[0] != '%') return 0;
                    664:   if (s[1] != '%') return 0;
                    665:   if (s[2] != '!') return 0;
                    666:   if (s[3] != 'P') return 0;
                    667:   if (s[4] != 'S') return 0;
                    668:   return 1;
                    669: }
                    670: static void printps(char *s) {
1.23      takayama  671:   printf("%s",Data_begin_ps[View]);
1.17      takayama  672:   printf("%s",s);
1.23      takayama  673:   printf("%s",Data_end[View]);
1.1       takayama  674:   fflush(NULL);
                    675: }
                    676: static void printp(char *s) {
1.23      takayama  677:   printf("%s",Data_begin_p[View]);
                    678:   printf("%s",Data_end[View]);
1.2       takayama  679:   printf("%s] ",s);
                    680:   fflush(NULL);
                    681: }
                    682: static void printCopyright(char *s) {
1.15      takayama  683:   if (! NoCopyright) {
1.42      takayama  684:     printf("%s",Data_begin_v[View]);
1.24      takayama  685:     printf("OpenXM engine (ox engine) interface with TeXmacs protocol.\n2004 (C) openxm.org");
                    686:     printf(" under the BSD license.  !asir; !sm1; !k0; !verbatim; !quit;\n");
1.15      takayama  687:     printf("Type in      !reset;     when the engine gets confused. ");
                    688:     printf("%s",s);
1.42      takayama  689:     printf("%s",Data_end[View]);
1.15      takayama  690:   }
1.5       takayama  691:   fflush(NULL);
                    692: }
                    693:
1.31      takayama  694: static void flushSm1() {
                    695:   KSexecuteString(" oxsm1.ccc ( [(flush)] extension pop ) oxsubmit ");
                    696: }
1.5       takayama  697: static int startEngine(int type,char *msg) {
1.21      takayama  698:   struct object ob = OINIT;
1.42      takayama  699:   if (!TM_quiet) printf("%s",Data_begin_v[View]);
1.5       takayama  700:   if (type == SM1) {
                    701:     if (!TM_sm1Started) KSexecuteString(" sm1connectr ");
1.8       takayama  702:     KSexecuteString(" /ox.engine oxsm1.ccc def ");
1.10      takayama  703:     /* Initialize the setting of sm1. */
                    704:     KSexecuteString("  oxsm1.ccc ( [(cmoLispLike) 0] extension ) oxsubmit ");
                    705:     KSexecuteString("  oxsm1.ccc ( ox_server_mode ) oxsubmit ");
1.31      takayama  706:     KSexecuteString("  oxsm1.ccc ( ( ) message (------------- Message from sm1 ----------------)message ) oxsubmit ");
                    707:        flushSm1();
1.8       takayama  708:     TM_sm1Started = 1;
1.10      takayama  709:        /* Welcome message.  BUG. Copyright should be returned by a function. */
1.15      takayama  710:     if (! NoCopyright) {
                    711:       printf("Kan/StackMachine1                         1991 April --- 2004.\n");
                    712:       printf("This software may be freely distributed as is with no warranty expressed. \n");
                    713:       printf("See OpenXM/Copyright/Copyright.generic\n");
                    714:       printf("Info: http://www.math.kobe-u.ac.jp/KAN, kan@math.kobe-u.ac.jp.\n");
                    715:       printf("0 usages to show a list of functions. \n(keyword) usages to see a short description\n");
                    716:     }
1.30      takayama  717:     printf("%s\n",msg); fflush(NULL);
1.5       takayama  718:   }else if (type == K0) {
                    719:     if (!TM_k0Started) KSexecuteString(" k0connectr ");
1.8       takayama  720:     KSexecuteString(" /ox.engine oxk0.ccc def ");
                    721:     TM_k0Started = 1;
                    722:     printf("%s\n",msg);
1.5       takayama  723:   }else{
                    724:     if (!TM_asirStarted) KSexecuteString(" asirconnectr ");
1.8       takayama  725:     KSexecuteString(" /ox.engine oxasir.ccc def ");
                    726:     TM_asirStarted = 1;
                    727:     printf("%s\n",msg);
1.24      takayama  728:     if ((!NoCopyright) && ((char *)getenv("ASIR_CONFIG") != NULL)) {
1.15      takayama  729:       KSexecuteString(" oxasir.ccc (copyright()+asir_contrib_copyright();) oxsubmit oxasir.ccc oxpopstring ");
1.24      takayama  730:       ob = KSpop();
                    731:       if (ob.tag == Sdollar) {
                    732:         printf("%s",ob.lc.str);
                    733:       }
                    734:     }else if ((!NoCopyright) && ((char *)getenv("ASIR_CONFIG") == NULL)) {
                    735:       KSexecuteString(" oxasir.ccc (copyright();) oxsubmit oxasir.ccc oxpopstring ");
1.15      takayama  736:       ob = KSpop();
                    737:       if (ob.tag == Sdollar) {
                    738:         printf("%s",ob.lc.str);
                    739:       }
1.8       takayama  740:     }
1.7       takayama  741:     /* Initialize the setting of asir. */
1.32      takayama  742:     if (AsirInitFile) {  /* cf. asir-contrib/packages/src/cfep-init.rr */
                    743:          unsigned char *ss;
1.36      takayama  744:          ss = (unsigned char *)sGC_malloc(strlen(AsirInitFile)+256);
1.33      takayama  745:          sprintf((char *)ss," oxasir.ccc (load(\"%s\");) oxsubmit oxasir.ccc oxgeterrors length 0 gt { (Error in loading asirInitFile) message} { } ifelse ",AsirInitFile);
1.32      takayama  746:          /* printf("Loading --asirInitFile %s\n",AsirInitFile); */
                    747:          KSexecuteString(ss);
                    748:     }
1.35      ohara     749:     KSexecuteString(" oxasir.ccc (if(1) {  Xm_server_mode = 1; Xm_helpdir = \"help-en\";  } else { ; } ;) oxsubmit oxasir.ccc oxpopcmo ");
1.7       takayama  750:     KSexecuteString(" oxasir.ccc (if(1) {  ctrl(\"message\",0);  } else { ; } ;) oxsubmit oxasir.ccc oxpopcmo ");
                    751:     /* bug; if ctrl is written with Xm_helpdir = ... without oxpopcmo, then it does
                    752:        not work. */
1.9       takayama  753:     KSexecuteString(" oxasir.ccc (print(\"----------- Messages from asir ------------------------------\")$ ) oxsubmit oxasir.ccc oxpopcmo ");
1.5       takayama  754:   }
1.42      takayama  755:   if (!TM_quiet) printf("%s",Data_end[View]);
1.1       takayama  756:   fflush(NULL);
                    757: }
1.2       takayama  758:
                    759: /* test data
                    760:
                    761: 1.  print("hello"); print("afo");
                    762:
                    763:     1+2;
                    764:
                    765: 2. def foo(N) { for (I=0; I<10; I++) {   --> error
                    766:
                    767:    3+5;
                    768:
                    769: 4.  print("hello"); shift+return print("afo");
                    770:
                    771: */
1.27      takayama  772:
                    773: static void myEncoder(int c) {
                    774:   putchar(0xf8 | (0x3 & (c >> 6)));
                    775:   putchar(0xf0 | (0x7 & (c >> 3)));
                    776:   putchar(0xf0 | (0x7 & c));
                    777: }
                    778: static void myEncoderS(unsigned char *s) {
                    779:   int i,n;
1.33      takayama  780:   n = strlen((char *)s);
1.27      takayama  781:   for (i = 0; i<n ; i++) myEncoder(s[i]);
                    782: }
                    783: static void myEncoderSn(unsigned char *s,int n) {
                    784:   int i;
                    785:   for (i = 0; i<n ; i++) myEncoder(s[i]);
                    786: }
                    787: static void outputStringToTunnel0(int channel, unsigned char *s, int n,int view) {
                    788:   unsigned char ts[128];
                    789:   int i;
                    790:   if (view == GENERIC) {
                    791:     printf("{%d<%d ",channel,n+1);
                    792:     for (i=0; i<n; i++) putchar(s[i]);
                    793:     printf("%c>}",0);
                    794:   }else if (view == V_CFEP) {
1.33      takayama  795:     sprintf((char *)ts,"{%d<%d ",channel,n+1);
1.27      takayama  796:     myEncoderS(ts);
                    797:     myEncoderSn(s,n); myEncoder(0);
1.33      takayama  798:     myEncoderS((unsigned char *)">}");
1.27      takayama  799:   }
                    800:   fflush(stdout);
                    801: }
1.28      takayama  802: #define LINE_FEED 0xc   /* ^L */
1.27      takayama  803: static void outputStringToTunnel(int channel, unsigned char *s, int view) {
                    804:   int start, i;
                    805:   start = i = 0;
                    806:   while (s[i] != 0) {
1.28      takayama  807:        if (s[i] == LINE_FEED) {
                    808:          outputStringToTunnel0(channel,&(s[start]),i-start,view);
1.27      takayama  809:          start = i+1;
                    810:        }
                    811:        i++;
                    812:   }
                    813: }
                    814:
1.2       takayama  815:
1.33      takayama  816: static void pngSendFile(char *path) {
                    817:   char s[1024];
                    818:   if (strlen(path) > 1000) return;
                    819:   sprintf(s,"showFile,%s",path);
                    820:   outputStringToTunnel0(10,(unsigned char *)s,strlen(s),View);
                    821:   printf("\n");
                    822:   fflush(NULL);
                    823: }
                    824: static int pngCheck(void) {
                    825:   static int checked = 0;
                    826:   static int ans = 0;
                    827:   if (checked) return ans;
                    828:   checked = 1;
                    829:   if (!getCommandPath("latex")) return 0;
1.34      takayama  830:   if (!getCommandPath("dvipng")) return 0;
1.33      takayama  831:   ans = 1;  return 1;
                    832: }
                    833: static void pngNotAvailable(void) {
                    834:   char *s = "notAvailable";
                    835:   outputStringToTunnel0(10,(unsigned char *)s,strlen(s),View);
                    836:   fflush(NULL);
                    837: }
1.2       takayama  838:

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