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

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

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