Annotation of OpenXM/src/kxx/ox_texmacs.c, Revision 1.6
1.6 ! takayama 1: /* $OpenXM: OpenXM/src/kxx/ox_texmacs.c,v 1.5 2004/03/01 09:37:05 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: */
22: #define DEBUG2
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;
55:
1.1 takayama 56: void ctrlC();
57: struct object KpoString(char *s);
58: char *KSpopString(void);
59:
1.2 takayama 60: static char *readString(FILE *fp,char *prolog, char *eplog);
1.1 takayama 61: static void printv(char *s);
62: static void printl(char *s);
63: static void printp(char *s);
1.2 takayama 64: static void printCopyright(char *s);
1.5 takayama 65: static int startEngine(int type,char *msg);
1.2 takayama 66:
67: /* tail -f /tmp/debug-texmacs.txt
68: Debug output to understand the timing problem of pipe interface.
69: */
1.3 takayama 70: FILE *Dfp;
1.1 takayama 71:
72: main() {
73: char *s;
74: char *r;
75: char *sys;
76: struct object ob;
1.3 takayama 77: int irt=0;
1.4 takayama 78: int vmode=1;
1.1 takayama 79:
1.3 takayama 80: #ifdef DEBUG2
81: Dfp = fopen("/tmp/debug-texmacs.txt","w");
82: #endif
1.2 takayama 83:
1.1 takayama 84: /* Set consts */
85: sys = "asir> ";
86: Quiet = 1;
87:
88: /* Initialize kanlib (gc is also initialized) */
89: KSstart();
90:
1.3 takayama 91: /* Main loop */
92: printf("%s",DATA_BEGIN_V);
93: printCopyright("");
94:
1.1 takayama 95: /* Load ox engine here */
96: /* engine id should be set to ox.engine */
97: KSexecuteString(" [(parse) (ox.sm1) pushfile] extension ");
1.5 takayama 98: startEngine(TM_Engine," ");
1.1 takayama 99:
100: if (signal(SIGINT,SIG_IGN) != SIG_IGN) {
101: signal(SIGINT,ctrlC);
102: }
1.3 takayama 103:
104: irt = 0;
1.1 takayama 105: while(1) {
1.2 takayama 106: /* printp(sys); no prompt */
1.1 takayama 107: if (SETJMP(EnvOfStackMachine)) {
108: KSexecuteString(" ctrlC-hook "); /* Execute User Defined functions. */
109: if (signal(SIGINT,SIG_IGN) != SIG_IGN) {
110: signal(SIGINT,ctrlC);
111: }
1.3 takayama 112: irt = 1;
1.1 takayama 113: continue;
114: } else { }
1.3 takayama 115: if (!irt) {
116: printf("%s",DATA_END); fflush(stdout);
117: }
118: irt = 0;
1.5 takayama 119: if (TM_Engine == K0) {
120: s=readString(stdin, " ", " "); /* see test data */
121: }else if (TM_Engine == SM1) {
122: s=readString(stdin, " ", " "); /* see test data */
123: }else{
124: s=readString(stdin, "if (1) { ", " ; }else{ }"); /* see test data */
125: }
126:
127: if (s == NULL) { irt = 1; continue; }
1.6 ! takayama 128: if (!irt) printf("%s",DATA_BEGIN_V);
1.1 takayama 129: KSexecuteString(" ox.engine ");
130: ob = KpoString(s);
131: KSpush(ob);
132: KSexecuteString(" oxsubmit ");
133:
134: /* Get the result in string. */
135: if (Format == 1) {
136: /* translate to latex form */
1.4 takayama 137: KSexecuteString(" ox.engine oxpushcmotag ox.engine oxpopcmo ");
138: ob = KSpop();
139: vmode = 0;
140: /* printf("id=%d\n",ob.tag); bug: matrix return 17 instead of Sinteger
141: or error. */
142: if (ob.tag == Sinteger) {
143: /* printf("cmotag=%d\n",ob.lc.ival);*/
144: if (ob.lc.ival == CMO_ERROR2) {
145: vmode = 1;
146: }
1.5 takayama 147: if (ob.lc.ival == CMO_STRING) {
148: vmode = 1;
149: }
1.4 takayama 150: }
151: if (vmode) {
152: KSexecuteString(" ox.engine oxpopstring ");
153: r = KSpopString();
154: }else{
155: KSexecuteString(" ox.engine 1 oxpushcmo ox.engine (print_tex_form) oxexec ");
156: KSexecuteString(" ox.engine oxpopstring ");
157: r = KSpopString();
158: }
159: if (strlen(r) < OutputLimit_for_TeXmacs) {
160: if (vmode) printv(r); else printl(r);
161: } else printv("Output is too large.\n");
1.1 takayama 162: }else{
163: KSexecuteString(" ox.engine oxpopstring ");
164: r = KSpopString();
1.3 takayama 165: if (strlen(r) < OutputLimit_for_TeXmacs) printv(r);
166: else printv("Output is too large.\n");
1.1 takayama 167: }
168: }
169: }
170:
171: #define SB_SIZE 1024
1.2 takayama 172: #define INC_BUF if (n >= limit-3) { \
173: tmp = s; \
174: limit *= 2; \
175: s = (char *) sGC_malloc(limit); \
176: if (s == NULL) { \
177: fprintf(stderr,"No more memory.\n"); \
178: exit(10); \
179: } \
180: strcpy(s,tmp); \
181: }
182: /* */
183: static char *readString(FILE *fp, char *prolog, char *epilog) {
1.1 takayama 184: int n = 0;
185: static int limit = 0;
186: static char *s;
187: int c;
188: char *tmp;
1.2 takayama 189: int i;
1.3 takayama 190: int m;
191: int start;
1.1 takayama 192: if (limit == 0) {
193: limit = 1024;
194: s = (char *)sGC_malloc(limit);
195: if (s == NULL) {
196: fprintf(stderr,"No more memory.\n");
197: exit(10);
198: }
199: }
1.2 takayama 200: s[0] = 0; n = 0; m = 0;
201: for (i=0; i < strlen(prolog); i++) {
202: s[n++] = prolog[i]; s[n] = 0;
203: INC_BUF ;
204: }
1.3 takayama 205: start = n;
1.1 takayama 206: while ((c = fgetc(fp)) != EOF) {
1.3 takayama 207: #ifdef DEBUG2
208: fprintf(Dfp,"[%x] ",c); fflush(Dfp);
209: #endif
1.2 takayama 210: if (c == END_OF_INPUT) {
1.3 takayama 211: if (oxSocketSelect0(0,1)) {
212: /* If there remains data in the stream,
213: read the remaining data. */
214: if (c == '\n') c=' ';
215: s[n++] = c; s[n] = 0; m++;
216: INC_BUF ;
217: continue;
218: }
1.2 takayama 219: break;
1.1 takayama 220: }
1.2 takayama 221: if (c == '\n') c=' ';
222: s[n++] = c; s[n] = 0; m++;
223: INC_BUF ;
224: }
1.5 takayama 225: /* Check the escape sequence */
226: if (strcmp(&(s[start]),"!quit;") == 0) {
1.3 takayama 227: printv("Terminated the process ox_texmacs.\n");
228: exit(0);
229: }
1.5 takayama 230: /* Check the escape sequence to change the globa env. */
231: if (strcmp(&(s[start]),"!verbatim;") == 0) {
232: printv("Output mode is changed to verbatim mode.");
233: Format=0;
234: return NULL;
235: }
236: if (strcmp(&(s[start]),"!latex;") == 0) {
237: printv("Output mode is changed to latex/verbose.");
238: Format = 1;
239: return NULL;
240: }
241: if (strcmp(&(s[start]),"!asir;") == 0) {
242: Format=1;
243: TM_Engine=ASIR; startEngine(TM_Engine,"Asir");
244: return NULL;
245: }
246: if (strcmp(&(s[start]),"!sm1;") == 0) {
247: Format=0;
248: TM_Engine=SM1; startEngine(TM_Engine,"sm1");
249: return NULL;
250: }
251: if (strcmp(&(s[start]),"!k0;") == 0) {
252: Format=0;
253: TM_Engine=K0; startEngine(TM_Engine,"k0");
254: return NULL;
255: }
256:
1.2 takayama 257: for (i=0; i < strlen(epilog); i++) {
258: s[n++] = epilog[i]; s[n] = 0;
259: INC_BUF ;
1.1 takayama 260: }
261: return s;
262: }
263:
264: static void printv(char *s) {
265: int i;
266: printf("%s",DATA_BEGIN_V);
267: printf("%s",s);
268: printf("%s",DATA_END);
1.3 takayama 269: #ifdef DEBUG2
270: fprintf(Dfp,"<%s>",s); fflush(Dfp);
271: #endif
1.1 takayama 272: fflush(NULL);
273: }
274: static void printl(char *s) {
275: printf("%s",DATA_BEGIN_L);
1.4 takayama 276: printf(" $ %s $ ",s);
1.1 takayama 277: printf("%s",DATA_END);
278: fflush(NULL);
279: }
280: static void printp(char *s) {
281: printf("%s",DATA_BEGIN_P);
1.2 takayama 282: printf("%s",DATA_END);
283: printf("%s] ",s);
284: fflush(NULL);
285: }
286: static void printCopyright(char *s) {
1.3 takayama 287: printf("%s",DATA_BEGIN_V);
1.5 takayama 288: printf("OpenXM engine (ox engine) interface for TeXmacs\n2004 (C) openxm.org");
289: printf(" under the BSD licence. !asir, !sm1, !k0.");
1.1 takayama 290: printf("%s",s);
1.5 takayama 291: printf("%s",DATA_END);
292: fflush(NULL);
293: }
294:
295: static int startEngine(int type,char *msg) {
296: struct object ob;
297: printf("%s",DATA_BEGIN_V);
298: if (type == SM1) {
299: if (!TM_sm1Started) KSexecuteString(" sm1connectr ");
300: KSexecuteString(" /ox.engine oxsm1.ccc def ");
301: TM_sm1Started = 1;
302: printf("%s\n",msg);
303: }else if (type == K0) {
304: if (!TM_k0Started) KSexecuteString(" k0connectr ");
305: KSexecuteString(" /ox.engine oxk0.ccc def ");
306: TM_k0Started = 1;
307: printf("%s\n",msg);
308: }else{
309: if (!TM_asirStarted) KSexecuteString(" asirconnectr ");
310: KSexecuteString(" /ox.engine oxasir.ccc def ");
311: TM_asirStarted = 1;
312: printf("%s\n",msg);
313: KSexecuteString(" oxasir.ccc (copyright();) oxsubmit oxasir.ccc oxpopstring ");
314: ob = KSpop();
315: if (ob.tag == Sdollar) {
316: printf("%s",ob.lc.str);
317: }
318: }
1.1 takayama 319: printf("%s",DATA_END);
320: fflush(NULL);
321: }
1.2 takayama 322:
323: /* test data
324:
325: 1. print("hello"); print("afo");
326:
327: 1+2;
328:
329: 2. def foo(N) { for (I=0; I<10; I++) { --> error
330:
331: 3+5;
332:
333: 4. print("hello"); shift+return print("afo");
334:
335: */
336:
337:
338:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>