Annotation of OpenXM/src/kxx/ox_texmacs.c, Revision 1.26
1.26 ! takayama 1: /* $OpenXM: OpenXM/src/kxx/ox_texmacs.c,v 1.25 2006/01/26 07:38:32 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"
10:
11: #if defined(__CYGWIN__)
12: #define JMP_BUF sigjmp_buf
13: #define SETJMP(env) sigsetjmp(env,1)
14: #define LONGJMP(env,p) siglongjmp(env,p)
15: #else
16: #define JMP_BUF jmp_buf
17: #define SETJMP(env) setjmp(env)
18: #define LONGJMP(env,p) longjmp(env,p)
19: #endif
20:
1.3 takayama 21: /*
22: #define DEBUG
23: */
1.7 takayama 24: /* #define DEBUG2 */
1.3 takayama 25:
1.23 takayama 26: /* Type of View part (user interface engine) */
27: #define GENERIC 0 /* DEBUG, xml */
28: #define V_TEXMACS 1
29: #define V_CFEP 2
30: int View = V_TEXMACS ;
31:
32: char *Data_begin_v[] = {
33: "<S type=verbatim>",
34: "\002verbatim:",
35: "\002",
36: };
37: char *Data_begin_l[] = {
38: "<S type=latex>",
39: "\002latex:",
40: "\002latex:"
41: };
42: char *Data_begin_p[] = {
43: "<S type=prompt>",
44: "\002prompt:",
45: "\002prompt:"
46: };
47: char *Data_begin_ps[] = {
48: "<S type=postscript>",
49: "\002ps:",
50: "\002ps:"
51: };
52: char *Data_end[] = {
53: "</S>",
54: "\005",
1.25 takayama 55: "\n\005" /* \n is not a part of the protocol. */
1.23 takayama 56: };
57:
1.25 takayama 58: /* todo: start_of_input */
1.2 takayama 59:
1.25 takayama 60: char End_of_input[] = {
61: 0x5, /* Use ^E and Return to end the input. */
62: '\n', /* TEXMACS_END_OF_INPUT. 0xd should be used for multiple lines. */
63: 0x5 /* CFEP_END_OF_INPUT */
64: };
1.1 takayama 65:
1.5 takayama 66: /* Table for the engine type. */
67: #define ASIR 1
68: #define SM1 2
69: #define K0 3
70:
1.1 takayama 71: extern int Quiet;
72: extern JMP_BUF EnvOfStackMachine;
1.19 takayama 73: extern int Calling_ctrlC_hook;
1.20 takayama 74: extern int RestrictedMode, RestrictedMode_saved;
1.3 takayama 75: int Format=1; /* 1 : latex mode */
76: int OutputLimit_for_TeXmacs = (1024*10);
1.1 takayama 77:
1.22 takayama 78:
1.5 takayama 79: int TM_Engine = ASIR ;
80: int TM_asirStarted = 0;
81: int TM_sm1Started = 0;
82: int TM_k0Started = 0;
1.7 takayama 83: int TM_do_not_print = 0;
1.5 takayama 84:
1.15 takayama 85: int Xm_noX = 0;
86: int NoCopyright = 0;
1.25 takayama 87: int Cpp = 0; /* Use cpp before sending to the engine. */
88: int EngineLogToStdout = 0; /* Do not run the ox engine inside xterm. */
1.15 takayama 89:
1.1 takayama 90: void ctrlC();
91: struct object KpoString(char *s);
92: char *KSpopString(void);
93:
1.2 takayama 94: static char *readString(FILE *fp,char *prolog, char *eplog);
1.1 takayama 95: static void printv(char *s);
96: static void printl(char *s);
97: static void printp(char *s);
1.17 takayama 98: static void printps(char *s);
1.2 takayama 99: static void printCopyright(char *s);
1.5 takayama 100: static int startEngine(int type,char *msg);
1.17 takayama 101: static int isPS(char *s);
1.22 takayama 102: static int end_of_input(int c);
103: static void setDefaultParameterForCfep();
1.2 takayama 104:
105: /* tail -f /tmp/debug-texmacs.txt
106: Debug output to understand the timing problem of pipe interface.
107: */
1.3 takayama 108: FILE *Dfp;
1.1 takayama 109:
1.10 takayama 110: main(int argc,char *argv[]) {
1.1 takayama 111: char *s;
112: char *r;
113: char *sys;
1.21 takayama 114: struct object ob = OINIT;
1.3 takayama 115: int irt=0;
1.4 takayama 116: int vmode=1;
1.7 takayama 117: char *openxm_home;
118: char *asir_config;
1.10 takayama 119: int i;
1.1 takayama 120:
1.7 takayama 121:
122:
1.3 takayama 123: #ifdef DEBUG2
124: Dfp = fopen("/tmp/debug-texmacs.txt","w");
125: #endif
1.2 takayama 126:
1.1 takayama 127: /* Set consts */
128: Quiet = 1;
1.10 takayama 129: for (i=1; i<argc; i++) {
1.22 takayama 130: if (strcmp(argv[i],"--view") == 0) {
131: i++;
132: if (strcmp(argv[i],"texmacs") == 0) {
133: View = V_TEXMACS;
134: }else if (strcmp(argv[i],"cfep")==0) {
135: View = V_CFEP; setDefaultParameterForCfep();
136: }else{
1.25 takayama 137: View = GENERIC;
1.22 takayama 138: /* printv("Unknown view type.\n"); */
139: }
140: } else if (strcmp(argv[i],"--sm1") == 0) {
141: TM_Engine = SM1;
142: }else if (strcmp(argv[i],"--asir") == 0) {
143: TM_Engine = ASIR;
144: }else if (strcmp(argv[i],"--k0") == 0) {
145: TM_Engine = K0;
1.14 takayama 146: }else if (strcmp(argv[i],"--outputLimit") == 0) {
147: i++;
148: sscanf(argv[i],"%d",&OutputLimit_for_TeXmacs);
1.15 takayama 149: }else if (strcmp(argv[i],"--noLogWindow") == 0) {
1.22 takayama 150: Xm_noX = 1;
1.15 takayama 151: }else if (strcmp(argv[i],"--noCopyright") == 0) {
1.22 takayama 152: NoCopyright = 1;
1.25 takayama 153: }else if (strcmp(argv[i],"--cpp") == 0) {
154: Cpp = 1;
155: }else if (strcmp(argv[i],"--engineLogToStdout") == 0) {
156: EngineLogToStdout = 1;
1.22 takayama 157: }else{
158: /* printv("Unknown option\n"); */
159: }
1.10 takayama 160: }
1.1 takayama 161:
1.23 takayama 162: openxm_home = (char *) getenv("OpenXM_HOME");
163: asir_config = (char *) getenv("ASIR_CONFIG");
164: if (openxm_home == NULL) {
165: printv("Error. The environmental variable OpenXM_HOME is not set.\nStart the texmacs with openxm texmacs or ox_texmacs by openxm ox_texmacs\nBye...");
166: exit(10);
167: }
168:
1.1 takayama 169: /* Initialize kanlib (gc is also initialized) */
170: KSstart();
171:
1.3 takayama 172: /* Main loop */
1.23 takayama 173: printf("%s",Data_begin_v[View]);
174: if ((asir_config == NULL) && (View == V_TEXMACS)) {
175: 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");
176:
177: }
178:
1.3 takayama 179: printCopyright("");
180:
1.23 takayama 181:
1.1 takayama 182: /* Load ox engine here */
183: /* engine id should be set to ox.engine */
184: KSexecuteString(" [(parse) (ox.sm1) pushfile] extension ");
1.15 takayama 185: if (Xm_noX) KSexecuteString(" /Xm_noX 1 def ");
1.26 ! takayama 186: if (EngineLogToStdout) KSexecuteString(" /Xm_engineLogToStdout 1 def ");
1.5 takayama 187: startEngine(TM_Engine," ");
1.1 takayama 188:
189: if (signal(SIGINT,SIG_IGN) != SIG_IGN) {
1.8 takayama 190: signal(SIGINT,ctrlC);
1.1 takayama 191: }
1.3 takayama 192:
193: irt = 0;
1.1 takayama 194: while(1) {
1.8 takayama 195: /* printp(sys); no prompt */
196: if (SETJMP(EnvOfStackMachine)) {
1.19 takayama 197: if (!Calling_ctrlC_hook) {
1.20 takayama 198: Calling_ctrlC_hook = 1; RestrictedMode = 0;
1.19 takayama 199: KSexecuteString(" ctrlC-hook "); /* Execute User Defined functions. */
1.20 takayama 200: RestrictedMode = RestrictedMode_saved;
1.19 takayama 201: }
202: Calling_ctrlC_hook = 0;
1.8 takayama 203: if (signal(SIGINT,SIG_IGN) != SIG_IGN) {
204: signal(SIGINT,ctrlC);
205: }
206: irt = 1;
207: continue;
208: } else { }
209: if (!irt) {
1.23 takayama 210: printf("%s",Data_end[View]); fflush(stdout);
1.8 takayama 211: }
212: irt = 0;
1.7 takayama 213:
214: /* Reading the input. */
1.8 takayama 215: if (TM_Engine == K0) {
216: s=readString(stdin, " ", " "); /* see test data */
217: }else if (TM_Engine == SM1) {
218: s=readString(stdin, " ", " "); /* see test data */
219: }else{
220: s=readString(stdin, "if (1) { ", " ; }else{ }"); /* see test data */
221: }
1.5 takayama 222:
1.8 takayama 223: if (s == NULL) { irt = 1; continue; }
1.23 takayama 224: if (!irt) printf("%s",Data_begin_v[View]);
1.7 takayama 225: /* Evaluate the input on the engine */
1.1 takayama 226: KSexecuteString(" ox.engine ");
1.7 takayama 227: ob = KpoString(s);
1.8 takayama 228: KSpush(ob);
229: KSexecuteString(" oxsubmit ");
230:
1.1 takayama 231: /* Get the result in string. */
1.8 takayama 232: if (Format == 1 && (! TM_do_not_print)) {
233: /* translate to latex form */
234: KSexecuteString(" ox.engine oxpushcmotag ox.engine oxpopcmo ");
235: ob = KSpop();
236: vmode = 0;
237: /* printf("id=%d\n",ob.tag); bug: matrix return 17 instead of Sinteger
238: or error. */
239: if (ob.tag == Sinteger) {
240: /* printf("cmotag=%d\n",ob.lc.ival);*/
241: if (ob.lc.ival == CMO_ERROR2) {
242: vmode = 1;
243: }
244: if (ob.lc.ival == CMO_STRING) {
245: vmode = 1;
246: }
247: }
248: if (vmode) {
249: KSexecuteString(" ox.engine oxpopstring ");
250: r = KSpopString();
251: }else{
252: KSexecuteString(" ox.engine 1 oxpushcmo ox.engine (print_tex_form) oxexec ");
253: KSexecuteString(" ox.engine oxpopstring ");
254: r = KSpopString();
255: }
1.17 takayama 256: if (isPS(r)) {
257: printps(r);
258: }else{
259: if (vmode) printv(r);
260: else{
261: if (strlen(r) < OutputLimit_for_TeXmacs) {
262: printl(r);
263: } else printv("Output is too large.\n");
264: }
265: }
1.8 takayama 266: }else{
267: if (!TM_do_not_print) {
268: KSexecuteString(" ox.engine oxpopstring ");
269: r = KSpopString();
1.17 takayama 270: printv(r);
1.8 takayama 271: }else{
272: KSexecuteString(" ox.engine 1 oxpops "); /* Discard the result. */
1.12 takayama 273: /* Push and pop dummy data to wait until the computation finishes. */
274: KSexecuteString(" ox.engine 0 oxpushcmo ox.engine oxpopcmo ");
275: ob = KSpop();
1.8 takayama 276: printv("");
277: }
278: }
1.1 takayama 279: }
280: }
281:
282: #define SB_SIZE 1024
1.8 takayama 283: #define INC_BUF if (n >= limit-3) { \
284: tmp = s; \
285: limit *= 2; \
286: s = (char *) sGC_malloc(limit); \
287: if (s == NULL) { \
288: fprintf(stderr,"No more memory.\n"); \
289: exit(10); \
290: } \
291: strcpy(s,tmp); \
292: }
1.2 takayama 293: /* */
294: static char *readString(FILE *fp, char *prolog, char *epilog) {
1.1 takayama 295: int n = 0;
296: static int limit = 0;
297: static char *s;
298: int c;
299: char *tmp;
1.2 takayama 300: int i;
1.3 takayama 301: int m;
302: int start;
1.21 takayama 303: struct object ob = OINIT;
1.1 takayama 304: if (limit == 0) {
1.8 takayama 305: limit = 1024;
306: s = (char *)sGC_malloc(limit);
307: if (s == NULL) {
308: fprintf(stderr,"No more memory.\n");
309: exit(10);
310: }
1.1 takayama 311: }
1.2 takayama 312: s[0] = 0; n = 0; m = 0;
313: for (i=0; i < strlen(prolog); i++) {
1.8 takayama 314: s[n++] = prolog[i]; s[n] = 0;
1.2 takayama 315: INC_BUF ;
316: }
1.3 takayama 317: start = n;
1.1 takayama 318: while ((c = fgetc(fp)) != EOF) {
1.3 takayama 319: #ifdef DEBUG2
1.8 takayama 320: fprintf(Dfp,"[%x] ",c); fflush(Dfp);
1.3 takayama 321: #endif
1.22 takayama 322: if (end_of_input(c)) {
1.18 takayama 323: /* If there remains data in the stream,
1.25 takayama 324: read the remaining data. (for debug) */
1.18 takayama 325: /*
1.8 takayama 326: if (oxSocketSelect0(0,1)) {
327: if (c == '\n') c=' ';
328: s[n++] = c; s[n] = 0; m++;
329: INC_BUF ;
330: continue;
331: }
1.18 takayama 332: */
1.8 takayama 333: break;
334: }
1.18 takayama 335: if ( c == '\v') c=' ';
1.8 takayama 336: s[n++] = c; s[n] = 0; m++;
1.2 takayama 337: INC_BUF ;
338: }
1.5 takayama 339: /* Check the escape sequence */
340: if (strcmp(&(s[start]),"!quit;") == 0) {
1.8 takayama 341: printv("Terminated the process ox_texmacs.\n");
342: exit(0);
1.3 takayama 343: }
1.11 takayama 344: /* Check the escape sequence to change the global env. */
1.5 takayama 345: if (strcmp(&(s[start]),"!verbatim;") == 0) {
1.8 takayama 346: printv("Output mode is changed to verbatim mode.");
347: Format=0;
348: return NULL;
1.5 takayama 349: }
350: if (strcmp(&(s[start]),"!latex;") == 0) {
1.8 takayama 351: printv("Output mode is changed to latex/verbose.");
352: Format = 1;
353: return NULL;
1.5 takayama 354: }
355: if (strcmp(&(s[start]),"!asir;") == 0) {
1.8 takayama 356: Format=1;
357: TM_Engine=ASIR; startEngine(TM_Engine,"Asir");
358: return NULL;
1.5 takayama 359: }
360: if (strcmp(&(s[start]),"!sm1;") == 0) {
1.8 takayama 361: Format=0;
362: TM_Engine=SM1; startEngine(TM_Engine,"sm1");
363: return NULL;
1.5 takayama 364: }
365: if (strcmp(&(s[start]),"!k0;") == 0) {
1.8 takayama 366: Format=0;
367: TM_Engine=K0; startEngine(TM_Engine,"k0");
368: return NULL;
1.5 takayama 369: }
1.13 takayama 370: if (strcmp(&(s[start]),"!reset;") == 0) {
1.23 takayama 371: printf("%s",Data_begin_v[View]);
1.13 takayama 372: KSexecuteString(" ox.engine oxreset ox.engine oxpopcmo ");
373: ob = KSpop();
1.23 takayama 374: printf("%s",Data_end[View]); fflush(stdout);
1.13 takayama 375: return NULL;
376: }
1.11 takayama 377:
378: /* Set TM_do_no_print */
379: if (s[n-1] == '$' && TM_Engine == ASIR) {
380: TM_do_not_print = 1; s[n-1] = ' ';
381: } else if (s[n-1] == ';' && TM_Engine == SM1) {
382: TM_do_not_print = 1; s[n-1] = ' ';
383: } else TM_do_not_print = 0;
1.5 takayama 384:
1.2 takayama 385: for (i=0; i < strlen(epilog); i++) {
1.8 takayama 386: s[n++] = epilog[i]; s[n] = 0;
1.2 takayama 387: INC_BUF ;
1.1 takayama 388: }
389: return s;
1.22 takayama 390: }
391:
392: static int end_of_input(int c) {
1.25 takayama 393: if (c == End_of_input[View]) return 1;
394: else return 0;
1.22 takayama 395: }
1.25 takayama 396:
1.22 takayama 397: static void setDefaultParameterForCfep() {
398: Format = 0;
1.1 takayama 399: }
400:
401: static void printv(char *s) {
402: int i;
1.23 takayama 403: printf("%s",Data_begin_v[View]);
1.1 takayama 404: printf("%s",s);
1.23 takayama 405: printf("%s",Data_end[View]);
1.3 takayama 406: #ifdef DEBUG2
407: fprintf(Dfp,"<%s>",s); fflush(Dfp);
408: #endif
1.1 takayama 409: fflush(NULL);
410: }
411: static void printl(char *s) {
1.23 takayama 412: printf("%s",Data_begin_l[View]);
1.4 takayama 413: printf(" $ %s $ ",s);
1.23 takayama 414: printf("%s",Data_end[View]);
1.17 takayama 415: fflush(NULL);
416: }
417: static int isPS(char *s) {
418: if (s[0] != '%') return 0;
419: if (s[1] != '%') return 0;
420: if (s[2] != '!') return 0;
421: if (s[3] != 'P') return 0;
422: if (s[4] != 'S') return 0;
423: return 1;
424: }
425: static void printps(char *s) {
1.23 takayama 426: printf("%s",Data_begin_ps[View]);
1.17 takayama 427: printf("%s",s);
1.23 takayama 428: printf("%s",Data_end[View]);
1.1 takayama 429: fflush(NULL);
430: }
431: static void printp(char *s) {
1.23 takayama 432: printf("%s",Data_begin_p[View]);
433: printf("%s",Data_end[View]);
1.2 takayama 434: printf("%s] ",s);
435: fflush(NULL);
436: }
437: static void printCopyright(char *s) {
1.23 takayama 438: printf("%s",Data_begin_v[View]);
1.15 takayama 439: if (! NoCopyright) {
1.24 takayama 440: printf("OpenXM engine (ox engine) interface with TeXmacs protocol.\n2004 (C) openxm.org");
441: printf(" under the BSD license. !asir; !sm1; !k0; !verbatim; !quit;\n");
1.15 takayama 442: printf("Type in !reset; when the engine gets confused. ");
443: printf("%s",s);
444: }
1.23 takayama 445: printf("%s",Data_end[View]);
1.5 takayama 446: fflush(NULL);
447: }
448:
449: static int startEngine(int type,char *msg) {
1.21 takayama 450: struct object ob = OINIT;
1.23 takayama 451: printf("%s",Data_begin_v[View]);
1.5 takayama 452: if (type == SM1) {
453: if (!TM_sm1Started) KSexecuteString(" sm1connectr ");
1.8 takayama 454: KSexecuteString(" /ox.engine oxsm1.ccc def ");
1.10 takayama 455: /* Initialize the setting of sm1. */
456: KSexecuteString(" oxsm1.ccc ( [(cmoLispLike) 0] extension ) oxsubmit ");
457: KSexecuteString(" oxsm1.ccc ( ox_server_mode ) oxsubmit ");
458: KSexecuteString(" oxsm1.ccc ( ( ) message (------------- Message from sm1 ----------------)message ) oxsubmit ");
1.8 takayama 459: TM_sm1Started = 1;
1.10 takayama 460: /* Welcome message. BUG. Copyright should be returned by a function. */
1.15 takayama 461: if (! NoCopyright) {
462: printf("Kan/StackMachine1 1991 April --- 2004.\n");
463: printf("This software may be freely distributed as is with no warranty expressed. \n");
464: printf("See OpenXM/Copyright/Copyright.generic\n");
465: printf("Info: http://www.math.kobe-u.ac.jp/KAN, kan@math.kobe-u.ac.jp.\n");
466: printf("0 usages to show a list of functions. \n(keyword) usages to see a short description\n");
467: }
1.8 takayama 468: printf("%s\n",msg);
1.5 takayama 469: }else if (type == K0) {
470: if (!TM_k0Started) KSexecuteString(" k0connectr ");
1.8 takayama 471: KSexecuteString(" /ox.engine oxk0.ccc def ");
472: TM_k0Started = 1;
473: printf("%s\n",msg);
1.5 takayama 474: }else{
475: if (!TM_asirStarted) KSexecuteString(" asirconnectr ");
1.8 takayama 476: KSexecuteString(" /ox.engine oxasir.ccc def ");
477: TM_asirStarted = 1;
478: printf("%s\n",msg);
1.24 takayama 479: if ((!NoCopyright) && ((char *)getenv("ASIR_CONFIG") != NULL)) {
1.15 takayama 480: KSexecuteString(" oxasir.ccc (copyright()+asir_contrib_copyright();) oxsubmit oxasir.ccc oxpopstring ");
1.24 takayama 481: ob = KSpop();
482: if (ob.tag == Sdollar) {
483: printf("%s",ob.lc.str);
484: }
485: }else if ((!NoCopyright) && ((char *)getenv("ASIR_CONFIG") == NULL)) {
486: KSexecuteString(" oxasir.ccc (copyright();) oxsubmit oxasir.ccc oxpopstring ");
1.15 takayama 487: ob = KSpop();
488: if (ob.tag == Sdollar) {
489: printf("%s",ob.lc.str);
490: }
1.8 takayama 491: }
1.7 takayama 492: /* Initialize the setting of asir. */
493: KSexecuteString(" oxasir.ccc (if(1) { Xm_server_mode = 1; Xm_helpdir = \"help-eg\"; } else { ; } ;) oxsubmit oxasir.ccc oxpopcmo ");
494: KSexecuteString(" oxasir.ccc (if(1) { ctrl(\"message\",0); } else { ; } ;) oxsubmit oxasir.ccc oxpopcmo ");
495: /* bug; if ctrl is written with Xm_helpdir = ... without oxpopcmo, then it does
496: not work. */
1.9 takayama 497: KSexecuteString(" oxasir.ccc (print(\"----------- Messages from asir ------------------------------\")$ ) oxsubmit oxasir.ccc oxpopcmo ");
1.5 takayama 498: }
1.23 takayama 499: printf("%s",Data_end[View]);
1.1 takayama 500: fflush(NULL);
501: }
1.2 takayama 502:
503: /* test data
504:
505: 1. print("hello"); print("afo");
506:
507: 1+2;
508:
509: 2. def foo(N) { for (I=0; I<10; I++) { --> error
510:
511: 3+5;
512:
513: 4. print("hello"); shift+return print("afo");
514:
515: */
516:
517:
518:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>