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