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