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

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

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