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