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