[BACK]Return to ox_texmacs.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / kxx

Annotation of OpenXM/src/kxx/ox_texmacs.c, Revision 1.5

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>