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

1.2     ! takayama    1: /* $OpenXM: OpenXM/src/kxx/ox_texmacs.c,v 1.1 2004/02/29 03:00:37 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.2     ! takayama   19: /* #define DEBUG */
        !            20: #ifdef DEBUG
        !            21: #define DATA_BEGIN_V  "<S type=verbatim>"     /* "\002verbatim:" */
        !            22: #define DATA_BEGIN_L  "<S type=latex>"        /* "\002latex:" */
        !            23: #define DATA_BEGIN_P  "<S type=prompt>"        /* "\002channel:prompt " */
        !            24: #define DATA_END      "</S>"    /* "\005" */
        !            25: #else
        !            26: #define DATA_BEGIN_V  "\002verbatim:"
        !            27: #define DATA_BEGIN_L  "\002latex:"
        !            28: #define DATA_BEGIN_P  "\002prompt:"
        !            29: #define DATA_END      "\005"
        !            30: #endif
        !            31:
        !            32: /*
        !            33: #define END_OF_INPUT  '#'
        !            34: */
        !            35: #define END_OF_INPUT '\n'
1.1       takayama   36:
                     37: extern int Quiet;
                     38: extern JMP_BUF EnvOfStackMachine;
                     39: int Format=0;
                     40:
                     41: void ctrlC();
                     42: struct object KpoString(char *s);
                     43: char *KSpopString(void);
                     44:
1.2     ! takayama   45: static char *readString(FILE *fp,char *prolog, char *eplog);
1.1       takayama   46: static void printv(char *s);
                     47: static void printl(char *s);
                     48: static void printp(char *s);
1.2     ! takayama   49: static void printCopyright(char *s);
        !            50:
        !            51: /* tail -f /tmp/debug-texmacs.txt
        !            52:    Debug output to understand the timing problem of pipe interface.
        !            53: */
        !            54: FILE *dfp;
1.1       takayama   55:
                     56: main() {
                     57:   char *s;
                     58:   char *r;
                     59:   char *sys;
                     60:   struct object ob;
                     61:
1.2     ! takayama   62:   dfp = fopen("/tmp/debug-texmacs.txt","w");
        !            63:
1.1       takayama   64:   /* Set consts */
                     65:   sys = "asir> ";
                     66:   Quiet = 1;
                     67:
                     68:   /* Initialize kanlib (gc is also initialized) */
                     69:   KSstart();
                     70:
                     71:   /* Load ox engine here */
                     72:   /* engine id should be set to ox.engine */
                     73:   KSexecuteString(" [(parse) (ox.sm1) pushfile] extension ");
                     74:   KSexecuteString(" asirconnectr /ox.engine oxasir.ccc def ");
                     75:
                     76:   if (signal(SIGINT,SIG_IGN) != SIG_IGN) {
                     77:        signal(SIGINT,ctrlC);
                     78:   }
                     79:   /* Main loop */
1.2     ! takayama   80:   printCopyright("");
1.1       takayama   81:   while(1) {
1.2     ! takayama   82:        /* printp(sys);  no prompt */
1.1       takayama   83:        if (SETJMP(EnvOfStackMachine)) {
                     84:          printv("Syntax error or an interruption\n");
                     85:          KSexecuteString(" ctrlC-hook "); /* Execute User Defined functions. */
                     86:          if (signal(SIGINT,SIG_IGN) != SIG_IGN) {
                     87:                signal(SIGINT,ctrlC);
                     88:          }
                     89:          continue;
                     90:        } else {  }
1.2     ! takayama   91:        s=readString(stdin, "if (1) { ", " ; }else{ };"); /* see test data */
        !            92:        if (s == NULL) break;
1.1       takayama   93:     KSexecuteString(" ox.engine ");
                     94:     ob = KpoString(s);
                     95:        KSpush(ob);
                     96:        KSexecuteString(" oxsubmit ");
                     97:
                     98:     /* Get the result in string. */
                     99:        if (Format == 1) {
                    100:          /* translate to latex form */
                    101:        }else{
                    102:          KSexecuteString(" ox.engine oxpopstring ");
                    103:          r = KSpopString();
                    104:          printv(r);
                    105:        }
                    106:   }
                    107: }
                    108:
                    109: #define SB_SIZE 1024
1.2     ! takayama  110: #define INC_BUF        if (n >= limit-3) { \
        !           111:          tmp = s; \
        !           112:          limit *= 2;  \
        !           113:          s = (char *) sGC_malloc(limit); \
        !           114:          if (s == NULL) { \
        !           115:                fprintf(stderr,"No more memory.\n"); \
        !           116:                exit(10); \
        !           117:          } \
        !           118:          strcpy(s,tmp); \
        !           119:        }
        !           120: /*   */
        !           121: static char *readString(FILE *fp, char *prolog, char *epilog) {
1.1       takayama  122:   int n = 0;
                    123:   static int limit = 0;
                    124:   static char *s;
                    125:   int c;
                    126:   char *tmp;
1.2     ! takayama  127:   int i;
        !           128:   int m;
1.1       takayama  129:   if (limit == 0) {
                    130:        limit = 1024;
                    131:        s = (char *)sGC_malloc(limit);
                    132:        if (s == NULL) {
                    133:          fprintf(stderr,"No more memory.\n");
                    134:          exit(10);
                    135:        }
                    136:   }
1.2     ! takayama  137:   s[0] = 0; n = 0; m = 0;
        !           138:   for (i=0; i < strlen(prolog); i++) {
        !           139:        s[n++] = prolog[i];  s[n] = 0;
        !           140:     INC_BUF ;
        !           141:   }
1.1       takayama  142:   while ((c = fgetc(fp)) != EOF) {
1.2     ! takayama  143:        fprintf(dfp,"[%x] ",c); fflush(dfp);
        !           144:        if (c == END_OF_INPUT) {
        !           145:          break;
1.1       takayama  146:        }
1.2     ! takayama  147:        if (c == '\n') c=' ';
        !           148:        s[n++] = c; s[n] = 0;  m++;
        !           149:     INC_BUF ;
        !           150:   }
        !           151:   if (m == 0) return (char *) NULL;
        !           152:   for (i=0; i < strlen(epilog); i++) {
        !           153:        s[n++] = epilog[i];  s[n] = 0;
        !           154:     INC_BUF ;
1.1       takayama  155:   }
                    156:   return s;
                    157: }
                    158:
                    159: static void printv(char *s) {
                    160:   int i;
                    161:   printf("%s",DATA_BEGIN_V);
                    162:   printf("%s",s);
                    163:   printf("%s",DATA_END);
1.2     ! takayama  164:   fprintf(dfp,"<%s>",s); fflush(dfp);
1.1       takayama  165:   /* for debug "hello;
                    166:   for (i=0; i<strlen(s); i++) {
                    167:        printf("%x ",s[i]);
                    168:   }
                    169:   printf("\n");
                    170:   */
                    171:
                    172:   fflush(NULL);
                    173: }
                    174: static void printl(char *s) {
                    175:   printf("%s",DATA_BEGIN_L);
                    176:   printf("%s",s);
                    177:   printf("%s",DATA_END);
                    178:   fflush(NULL);
                    179: }
                    180: static void printp(char *s) {
                    181:   printf("%s",DATA_BEGIN_P);
1.2     ! takayama  182:   printf("%s",DATA_END);
        !           183:   printf("%s] ",s);
        !           184:   fflush(NULL);
        !           185: }
        !           186: static void printCopyright(char *s) {
        !           187:   printf("%s",DATA_BEGIN_P);
        !           188:   printf("OpenXM engine (ox engine) interface for TeXmacs\n2004 (C) openxm.org\n");
1.1       takayama  189:   printf("%s",s);
                    190:   printf("%s",DATA_END);
                    191:   fflush(NULL);
                    192: }
1.2     ! takayama  193:
        !           194: /* test data
        !           195:
        !           196: 1.  print("hello"); print("afo");
        !           197:
        !           198:     1+2;
        !           199:
        !           200: 2. def foo(N) { for (I=0; I<10; I++) {   --> error
        !           201:
        !           202:    3+5;
        !           203:
        !           204: 4.  print("hello"); shift+return print("afo");
        !           205:
        !           206: */
        !           207:
        !           208:
        !           209:

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