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

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

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