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