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