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