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

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

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