[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.4

1.4     ! takayama    1: /* $OpenXM: OpenXM/src/kxx/ox_texmacs.c,v 1.3 2004/02/29 13:02:44 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:
                     41: extern int Quiet;
                     42: extern JMP_BUF EnvOfStackMachine;
1.3       takayama   43: int Format=1;  /* 1 : latex mode */
                     44: int OutputLimit_for_TeXmacs = (1024*10);
1.1       takayama   45:
                     46: void ctrlC();
                     47: struct object KpoString(char *s);
                     48: char *KSpopString(void);
                     49:
1.2       takayama   50: static char *readString(FILE *fp,char *prolog, char *eplog);
1.1       takayama   51: static void printv(char *s);
                     52: static void printl(char *s);
                     53: static void printp(char *s);
1.2       takayama   54: static void printCopyright(char *s);
                     55:
                     56: /* tail -f /tmp/debug-texmacs.txt
                     57:    Debug output to understand the timing problem of pipe interface.
                     58: */
1.3       takayama   59: FILE *Dfp;
1.1       takayama   60:
                     61: main() {
                     62:   char *s;
                     63:   char *r;
                     64:   char *sys;
                     65:   struct object ob;
1.3       takayama   66:   int irt=0;
1.4     ! takayama   67:   int vmode=1;
1.1       takayama   68:
1.3       takayama   69: #ifdef DEBUG2
                     70:   Dfp = fopen("/tmp/debug-texmacs.txt","w");
                     71: #endif
1.2       takayama   72:
1.1       takayama   73:   /* Set consts */
                     74:   sys = "asir> ";
                     75:   Quiet = 1;
                     76:
                     77:   /* Initialize kanlib (gc is also initialized) */
                     78:   KSstart();
                     79:
1.3       takayama   80:   /* Main loop */
                     81:   printf("%s",DATA_BEGIN_V);
                     82:   printCopyright("");
                     83:
1.1       takayama   84:   /* Load ox engine here */
                     85:   /* engine id should be set to ox.engine */
                     86:   KSexecuteString(" [(parse) (ox.sm1) pushfile] extension ");
                     87:   KSexecuteString(" asirconnectr /ox.engine oxasir.ccc def ");
                     88:
                     89:   if (signal(SIGINT,SIG_IGN) != SIG_IGN) {
                     90:        signal(SIGINT,ctrlC);
                     91:   }
1.3       takayama   92:
                     93:   irt = 0;
1.1       takayama   94:   while(1) {
1.2       takayama   95:        /* printp(sys);  no prompt */
1.1       takayama   96:        if (SETJMP(EnvOfStackMachine)) {
                     97:          printv("Syntax error or an interruption\n");
                     98:          KSexecuteString(" ctrlC-hook "); /* Execute User Defined functions. */
                     99:          if (signal(SIGINT,SIG_IGN) != SIG_IGN) {
                    100:                signal(SIGINT,ctrlC);
                    101:          }
1.3       takayama  102:          irt = 1;
1.1       takayama  103:          continue;
                    104:        } else {  }
1.3       takayama  105:        if (!irt) {
                    106:          printf("%s",DATA_END); fflush(stdout);
                    107:        }
                    108:        irt = 0;
1.4     ! takayama  109:        s=readString(stdin, "if (1) { ", " ; }else{ }"); /* see test data */
1.2       takayama  110:        if (s == NULL) break;
1.3       takayama  111:        printf("%s",DATA_BEGIN_V);
1.1       takayama  112:     KSexecuteString(" ox.engine ");
                    113:     ob = KpoString(s);
                    114:        KSpush(ob);
                    115:        KSexecuteString(" oxsubmit ");
                    116:
                    117:     /* Get the result in string. */
                    118:        if (Format == 1) {
                    119:          /* translate to latex form */
1.4     ! takayama  120:          KSexecuteString(" ox.engine oxpushcmotag ox.engine oxpopcmo ");
        !           121:          ob = KSpop();
        !           122:          vmode = 0;
        !           123:          /* printf("id=%d\n",ob.tag); bug: matrix return 17 instead of Sinteger
        !           124:           or error. */
        !           125:          if (ob.tag == Sinteger) {
        !           126:                /* printf("cmotag=%d\n",ob.lc.ival);*/
        !           127:                if (ob.lc.ival == CMO_ERROR2) {
        !           128:                  vmode = 1;
        !           129:                }
        !           130:          }
        !           131:          if (vmode) {
        !           132:                KSexecuteString(" ox.engine oxpopstring ");
        !           133:                r = KSpopString();
        !           134:          }else{
        !           135:                KSexecuteString(" ox.engine 1 oxpushcmo ox.engine (print_tex_form) oxexec  ");
        !           136:                KSexecuteString(" ox.engine oxpopstring ");
        !           137:                r = KSpopString();
        !           138:          }
        !           139:          if (strlen(r) < OutputLimit_for_TeXmacs) {
        !           140:                if (vmode) printv(r); else printl(r);
        !           141:          } else printv("Output is too large.\n");
1.1       takayama  142:        }else{
                    143:          KSexecuteString(" ox.engine oxpopstring ");
                    144:          r = KSpopString();
1.3       takayama  145:          if (strlen(r) < OutputLimit_for_TeXmacs) printv(r);
                    146:          else printv("Output is too large.\n");
1.1       takayama  147:        }
                    148:   }
                    149: }
                    150:
                    151: #define SB_SIZE 1024
1.2       takayama  152: #define INC_BUF        if (n >= limit-3) { \
                    153:          tmp = s; \
                    154:          limit *= 2;  \
                    155:          s = (char *) sGC_malloc(limit); \
                    156:          if (s == NULL) { \
                    157:                fprintf(stderr,"No more memory.\n"); \
                    158:                exit(10); \
                    159:          } \
                    160:          strcpy(s,tmp); \
                    161:        }
                    162: /*   */
                    163: static char *readString(FILE *fp, char *prolog, char *epilog) {
1.1       takayama  164:   int n = 0;
                    165:   static int limit = 0;
                    166:   static char *s;
                    167:   int c;
                    168:   char *tmp;
1.2       takayama  169:   int i;
1.3       takayama  170:   int m;
                    171:   int start;
1.1       takayama  172:   if (limit == 0) {
                    173:        limit = 1024;
                    174:        s = (char *)sGC_malloc(limit);
                    175:        if (s == NULL) {
                    176:          fprintf(stderr,"No more memory.\n");
                    177:          exit(10);
                    178:        }
                    179:   }
1.2       takayama  180:   s[0] = 0; n = 0; m = 0;
                    181:   for (i=0; i < strlen(prolog); i++) {
                    182:        s[n++] = prolog[i];  s[n] = 0;
                    183:     INC_BUF ;
                    184:   }
1.3       takayama  185:   start = n;
1.1       takayama  186:   while ((c = fgetc(fp)) != EOF) {
1.3       takayama  187: #ifdef DEBUG2
                    188:        fprintf(Dfp,"[%x] ",c); fflush(Dfp);
                    189: #endif
1.2       takayama  190:        if (c == END_OF_INPUT) {
1.3       takayama  191:          if (oxSocketSelect0(0,1)) {
                    192:                /* If there remains data in the stream,
                    193:                   read the remaining data. */
                    194:                if (c == '\n') c=' ';
                    195:                s[n++] = c; s[n] = 0;  m++;
                    196:                INC_BUF ;
                    197:                continue;
                    198:          }
1.2       takayama  199:          break;
1.1       takayama  200:        }
1.2       takayama  201:        if (c == '\n') c=' ';
                    202:        s[n++] = c; s[n] = 0;  m++;
                    203:     INC_BUF ;
                    204:   }
1.3       takayama  205:   if (strcmp(&(s[start]),"quit;") == 0) {
                    206:        printv("Terminated the process ox_texmacs.\n");
                    207:        exit(0);
                    208:   }
1.2       takayama  209:   for (i=0; i < strlen(epilog); i++) {
                    210:        s[n++] = epilog[i];  s[n] = 0;
                    211:     INC_BUF ;
1.1       takayama  212:   }
                    213:   return s;
                    214: }
                    215:
                    216: static void printv(char *s) {
                    217:   int i;
                    218:   printf("%s",DATA_BEGIN_V);
                    219:   printf("%s",s);
                    220:   printf("%s",DATA_END);
1.3       takayama  221: #ifdef DEBUG2
                    222:   fprintf(Dfp,"<%s>",s); fflush(Dfp);
                    223: #endif
1.1       takayama  224:   fflush(NULL);
                    225: }
                    226: static void printl(char *s) {
                    227:   printf("%s",DATA_BEGIN_L);
1.4     ! takayama  228:   printf(" $ %s $ ",s);
1.1       takayama  229:   printf("%s",DATA_END);
                    230:   fflush(NULL);
                    231: }
                    232: static void printp(char *s) {
                    233:   printf("%s",DATA_BEGIN_P);
1.2       takayama  234:   printf("%s",DATA_END);
                    235:   printf("%s] ",s);
                    236:   fflush(NULL);
                    237: }
                    238: static void printCopyright(char *s) {
1.3       takayama  239:   printf("%s",DATA_BEGIN_V);
1.2       takayama  240:   printf("OpenXM engine (ox engine) interface for TeXmacs\n2004 (C) openxm.org\n");
1.1       takayama  241:   printf("%s",s);
                    242:   printf("%s",DATA_END);
                    243:   fflush(NULL);
                    244: }
1.2       takayama  245:
                    246: /* test data
                    247:
                    248: 1.  print("hello"); print("afo");
                    249:
                    250:     1+2;
                    251:
                    252: 2. def foo(N) { for (I=0; I<10; I++) {   --> error
                    253:
                    254:    3+5;
                    255:
                    256: 4.  print("hello"); shift+return print("afo");
                    257:
                    258: */
                    259:
                    260:
                    261:

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