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

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

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