Annotation of OpenXM/src/kxx/ox_texmacs.c, Revision 1.11
1.11 ! takayama 1: /* $OpenXM: OpenXM/src/kxx/ox_texmacs.c,v 1.10 2004/03/03 06:09:29 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.5 takayama 255: /* Check the escape sequence */
256: if (strcmp(&(s[start]),"!quit;") == 0) {
1.8 takayama 257: printv("Terminated the process ox_texmacs.\n");
258: exit(0);
1.3 takayama 259: }
1.11 ! takayama 260: /* Check the escape sequence to change the global env. */
1.5 takayama 261: if (strcmp(&(s[start]),"!verbatim;") == 0) {
1.8 takayama 262: printv("Output mode is changed to verbatim mode.");
263: Format=0;
264: return NULL;
1.5 takayama 265: }
266: if (strcmp(&(s[start]),"!latex;") == 0) {
1.8 takayama 267: printv("Output mode is changed to latex/verbose.");
268: Format = 1;
269: return NULL;
1.5 takayama 270: }
271: if (strcmp(&(s[start]),"!asir;") == 0) {
1.8 takayama 272: Format=1;
273: TM_Engine=ASIR; startEngine(TM_Engine,"Asir");
274: return NULL;
1.5 takayama 275: }
276: if (strcmp(&(s[start]),"!sm1;") == 0) {
1.8 takayama 277: Format=0;
278: TM_Engine=SM1; startEngine(TM_Engine,"sm1");
279: return NULL;
1.5 takayama 280: }
281: if (strcmp(&(s[start]),"!k0;") == 0) {
1.8 takayama 282: Format=0;
283: TM_Engine=K0; startEngine(TM_Engine,"k0");
284: return NULL;
1.5 takayama 285: }
1.11 ! takayama 286:
! 287: /* Set TM_do_no_print */
! 288: if (s[n-1] == '$' && TM_Engine == ASIR) {
! 289: TM_do_not_print = 1; s[n-1] = ' ';
! 290: } else if (s[n-1] == ';' && TM_Engine == SM1) {
! 291: TM_do_not_print = 1; s[n-1] = ' ';
! 292: } else TM_do_not_print = 0;
1.5 takayama 293:
1.2 takayama 294: for (i=0; i < strlen(epilog); i++) {
1.8 takayama 295: s[n++] = epilog[i]; s[n] = 0;
1.2 takayama 296: INC_BUF ;
1.1 takayama 297: }
298: return s;
299: }
300:
301: static void printv(char *s) {
302: int i;
303: printf("%s",DATA_BEGIN_V);
304: printf("%s",s);
305: printf("%s",DATA_END);
1.3 takayama 306: #ifdef DEBUG2
307: fprintf(Dfp,"<%s>",s); fflush(Dfp);
308: #endif
1.1 takayama 309: fflush(NULL);
310: }
311: static void printl(char *s) {
312: printf("%s",DATA_BEGIN_L);
1.4 takayama 313: printf(" $ %s $ ",s);
1.1 takayama 314: printf("%s",DATA_END);
315: fflush(NULL);
316: }
317: static void printp(char *s) {
318: printf("%s",DATA_BEGIN_P);
1.2 takayama 319: printf("%s",DATA_END);
320: printf("%s] ",s);
321: fflush(NULL);
322: }
323: static void printCopyright(char *s) {
1.3 takayama 324: printf("%s",DATA_BEGIN_V);
1.5 takayama 325: printf("OpenXM engine (ox engine) interface for TeXmacs\n2004 (C) openxm.org");
1.7 takayama 326: printf(" under the BSD licence. !asir; !sm1; !k0; !verbatim;");
1.1 takayama 327: printf("%s",s);
1.5 takayama 328: printf("%s",DATA_END);
329: fflush(NULL);
330: }
331:
332: static int startEngine(int type,char *msg) {
333: struct object ob;
334: printf("%s",DATA_BEGIN_V);
335: if (type == SM1) {
336: if (!TM_sm1Started) KSexecuteString(" sm1connectr ");
1.8 takayama 337: KSexecuteString(" /ox.engine oxsm1.ccc def ");
1.10 takayama 338: /* Initialize the setting of sm1. */
339: KSexecuteString(" oxsm1.ccc ( [(cmoLispLike) 0] extension ) oxsubmit ");
340: KSexecuteString(" oxsm1.ccc ( ox_server_mode ) oxsubmit ");
341: KSexecuteString(" oxsm1.ccc ( ( ) message (------------- Message from sm1 ----------------)message ) oxsubmit ");
1.8 takayama 342: TM_sm1Started = 1;
1.10 takayama 343: /* Welcome message. BUG. Copyright should be returned by a function. */
344: printf("Kan/StackMachine1 1991 April --- 2004.\n");
345: printf("This software may be freely distributed as is with no warranty expressed. \n");
346: printf("See OpenXM/Copyright/Copyright.generic\n");
347: printf("Info: http://www.math.kobe-u.ac.jp/KAN, kan@math.kobe-u.ac.jp.\n");
348: printf("0 usages to show a list of functions. \n(keyword) usages to see a short description\n");
1.8 takayama 349: printf("%s\n",msg);
1.5 takayama 350: }else if (type == K0) {
351: if (!TM_k0Started) KSexecuteString(" k0connectr ");
1.8 takayama 352: KSexecuteString(" /ox.engine oxk0.ccc def ");
353: TM_k0Started = 1;
354: printf("%s\n",msg);
1.5 takayama 355: }else{
356: if (!TM_asirStarted) KSexecuteString(" asirconnectr ");
1.8 takayama 357: KSexecuteString(" /ox.engine oxasir.ccc def ");
358: TM_asirStarted = 1;
359: printf("%s\n",msg);
360: KSexecuteString(" oxasir.ccc (copyright()+asir_contrib_copyright();) oxsubmit oxasir.ccc oxpopstring ");
361: ob = KSpop();
362: if (ob.tag == Sdollar) {
363: printf("%s",ob.lc.str);
364: }
1.7 takayama 365: /* Initialize the setting of asir. */
366: KSexecuteString(" oxasir.ccc (if(1) { Xm_server_mode = 1; Xm_helpdir = \"help-eg\"; } else { ; } ;) oxsubmit oxasir.ccc oxpopcmo ");
367: KSexecuteString(" oxasir.ccc (if(1) { ctrl(\"message\",0); } else { ; } ;) oxsubmit oxasir.ccc oxpopcmo ");
368: /* bug; if ctrl is written with Xm_helpdir = ... without oxpopcmo, then it does
369: not work. */
1.9 takayama 370: KSexecuteString(" oxasir.ccc (print(\"----------- Messages from asir ------------------------------\")$ ) oxsubmit oxasir.ccc oxpopcmo ");
1.5 takayama 371: }
1.1 takayama 372: printf("%s",DATA_END);
373: fflush(NULL);
374: }
1.2 takayama 375:
376: /* test data
377:
378: 1. print("hello"); print("afo");
379:
380: 1+2;
381:
382: 2. def foo(N) { for (I=0; I<10; I++) { --> error
383:
384: 3+5;
385:
386: 4. print("hello"); shift+return print("afo");
387:
388: */
389:
390:
391:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>