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