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

1.13    ! takayama    1: /* $OpenXM: OpenXM/src/kxx/ox_texmacs.c,v 1.12 2004/03/03 12:27:31 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.13    ! takayama  225:   struct object ob;
1.1       takayama  226:   if (limit == 0) {
1.8       takayama  227:     limit = 1024;
                    228:     s = (char *)sGC_malloc(limit);
                    229:     if (s == NULL) {
                    230:       fprintf(stderr,"No more memory.\n");
                    231:       exit(10);
                    232:     }
1.1       takayama  233:   }
1.2       takayama  234:   s[0] = 0; n = 0; m = 0;
                    235:   for (i=0; i < strlen(prolog); i++) {
1.8       takayama  236:     s[n++] = prolog[i];  s[n] = 0;
1.2       takayama  237:     INC_BUF ;
                    238:   }
1.3       takayama  239:   start = n;
1.1       takayama  240:   while ((c = fgetc(fp)) != EOF) {
1.3       takayama  241: #ifdef DEBUG2
1.8       takayama  242:     fprintf(Dfp,"[%x] ",c); fflush(Dfp);
1.3       takayama  243: #endif
1.8       takayama  244:     if (c == END_OF_INPUT) {
                    245:       if (oxSocketSelect0(0,1)) {
                    246:         /* If there remains data in the stream,
                    247:            read the remaining data. */
                    248:         if (c == '\n') c=' ';
                    249:         s[n++] = c; s[n] = 0;  m++;
                    250:         INC_BUF ;
                    251:         continue;
                    252:       }
                    253:       break;
                    254:     }
                    255:     if (c == '\n') c=' ';
                    256:     s[n++] = c; s[n] = 0;  m++;
1.2       takayama  257:     INC_BUF ;
                    258:   }
1.5       takayama  259:   /* Check the escape sequence */
                    260:   if (strcmp(&(s[start]),"!quit;") == 0) {
1.8       takayama  261:     printv("Terminated the process ox_texmacs.\n");
                    262:     exit(0);
1.3       takayama  263:   }
1.11      takayama  264:   /* Check the escape sequence to change the global env. */
1.5       takayama  265:   if (strcmp(&(s[start]),"!verbatim;") == 0) {
1.8       takayama  266:     printv("Output mode is changed to verbatim mode.");
                    267:     Format=0;
                    268:     return NULL;
1.5       takayama  269:   }
                    270:   if (strcmp(&(s[start]),"!latex;") == 0) {
1.8       takayama  271:     printv("Output mode is changed to latex/verbose.");
                    272:     Format = 1;
                    273:     return NULL;
1.5       takayama  274:   }
                    275:   if (strcmp(&(s[start]),"!asir;") == 0) {
1.8       takayama  276:     Format=1;
                    277:     TM_Engine=ASIR; startEngine(TM_Engine,"Asir");
                    278:     return NULL;
1.5       takayama  279:   }
                    280:   if (strcmp(&(s[start]),"!sm1;") == 0) {
1.8       takayama  281:     Format=0;
                    282:     TM_Engine=SM1; startEngine(TM_Engine,"sm1");
                    283:     return NULL;
1.5       takayama  284:   }
                    285:   if (strcmp(&(s[start]),"!k0;") == 0) {
1.8       takayama  286:     Format=0;
                    287:     TM_Engine=K0; startEngine(TM_Engine,"k0");
                    288:     return NULL;
1.5       takayama  289:   }
1.13    ! takayama  290:   if (strcmp(&(s[start]),"!reset;") == 0) {
        !           291:        printf("%s",DATA_BEGIN_V);
        !           292:     KSexecuteString(" ox.engine oxreset ox.engine oxpopcmo ");
        !           293:        ob = KSpop();
        !           294:        printf("%s",DATA_END); fflush(stdout);
        !           295:     return NULL;
        !           296:   }
1.11      takayama  297:
                    298:   /* Set TM_do_no_print */
                    299:   if (s[n-1] == '$' && TM_Engine == ASIR) {
                    300:        TM_do_not_print = 1; s[n-1] = ' ';
                    301:   } else if (s[n-1] == ';' && TM_Engine == SM1) {
                    302:        TM_do_not_print = 1; s[n-1] = ' ';
                    303:   } else TM_do_not_print = 0;
1.5       takayama  304:
1.2       takayama  305:   for (i=0; i < strlen(epilog); i++) {
1.8       takayama  306:     s[n++] = epilog[i];  s[n] = 0;
1.2       takayama  307:     INC_BUF ;
1.1       takayama  308:   }
                    309:   return s;
                    310: }
                    311:
                    312: static void printv(char *s) {
                    313:   int i;
                    314:   printf("%s",DATA_BEGIN_V);
                    315:   printf("%s",s);
                    316:   printf("%s",DATA_END);
1.3       takayama  317: #ifdef DEBUG2
                    318:   fprintf(Dfp,"<%s>",s); fflush(Dfp);
                    319: #endif
1.1       takayama  320:   fflush(NULL);
                    321: }
                    322: static void printl(char *s) {
                    323:   printf("%s",DATA_BEGIN_L);
1.4       takayama  324:   printf(" $ %s $ ",s);
1.1       takayama  325:   printf("%s",DATA_END);
                    326:   fflush(NULL);
                    327: }
                    328: static void printp(char *s) {
                    329:   printf("%s",DATA_BEGIN_P);
1.2       takayama  330:   printf("%s",DATA_END);
                    331:   printf("%s] ",s);
                    332:   fflush(NULL);
                    333: }
                    334: static void printCopyright(char *s) {
1.3       takayama  335:   printf("%s",DATA_BEGIN_V);
1.5       takayama  336:   printf("OpenXM engine (ox engine) interface for TeXmacs\n2004 (C) openxm.org");
1.13    ! takayama  337:   printf(" under the BSD licence.  !asir; !sm1; !k0; !verbatim;\n");
        !           338:   printf("Type in      !reset;     when the engine gets confused. ");
1.1       takayama  339:   printf("%s",s);
1.5       takayama  340:   printf("%s",DATA_END);
                    341:   fflush(NULL);
                    342: }
                    343:
                    344: static int startEngine(int type,char *msg) {
                    345:   struct object ob;
                    346:   printf("%s",DATA_BEGIN_V);
                    347:   if (type == SM1) {
                    348:     if (!TM_sm1Started) KSexecuteString(" sm1connectr ");
1.8       takayama  349:     KSexecuteString(" /ox.engine oxsm1.ccc def ");
1.10      takayama  350:     /* Initialize the setting of sm1. */
                    351:     KSexecuteString("  oxsm1.ccc ( [(cmoLispLike) 0] extension ) oxsubmit ");
                    352:     KSexecuteString("  oxsm1.ccc ( ox_server_mode ) oxsubmit ");
                    353:     KSexecuteString("  oxsm1.ccc ( ( ) message (------------- Message from sm1 ----------------)message ) oxsubmit ");
1.8       takayama  354:     TM_sm1Started = 1;
1.10      takayama  355:        /* Welcome message.  BUG. Copyright should be returned by a function. */
                    356:     printf("Kan/StackMachine1                         1991 April --- 2004.\n");
                    357:     printf("This software may be freely distributed as is with no warranty expressed. \n");
                    358:        printf("See OpenXM/Copyright/Copyright.generic\n");
                    359:        printf("Info: http://www.math.kobe-u.ac.jp/KAN, kan@math.kobe-u.ac.jp.\n");
                    360:        printf("0 usages to show a list of functions. \n(keyword) usages to see a short description\n");
1.8       takayama  361:     printf("%s\n",msg);
1.5       takayama  362:   }else if (type == K0) {
                    363:     if (!TM_k0Started) KSexecuteString(" k0connectr ");
1.8       takayama  364:     KSexecuteString(" /ox.engine oxk0.ccc def ");
                    365:     TM_k0Started = 1;
                    366:     printf("%s\n",msg);
1.5       takayama  367:   }else{
                    368:     if (!TM_asirStarted) KSexecuteString(" asirconnectr ");
1.8       takayama  369:     KSexecuteString(" /ox.engine oxasir.ccc def ");
                    370:     TM_asirStarted = 1;
                    371:     printf("%s\n",msg);
                    372:     KSexecuteString(" oxasir.ccc (copyright()+asir_contrib_copyright();) oxsubmit oxasir.ccc oxpopstring ");
                    373:     ob = KSpop();
                    374:     if (ob.tag == Sdollar) {
                    375:       printf("%s",ob.lc.str);
                    376:     }
1.7       takayama  377:     /* Initialize the setting of asir. */
                    378:     KSexecuteString(" oxasir.ccc (if(1) {  Xm_server_mode = 1; Xm_helpdir = \"help-eg\";  } else { ; } ;) oxsubmit oxasir.ccc oxpopcmo ");
                    379:     KSexecuteString(" oxasir.ccc (if(1) {  ctrl(\"message\",0);  } else { ; } ;) oxsubmit oxasir.ccc oxpopcmo ");
                    380:     /* bug; if ctrl is written with Xm_helpdir = ... without oxpopcmo, then it does
                    381:        not work. */
1.9       takayama  382:     KSexecuteString(" oxasir.ccc (print(\"----------- Messages from asir ------------------------------\")$ ) oxsubmit oxasir.ccc oxpopcmo ");
1.5       takayama  383:   }
1.1       takayama  384:   printf("%s",DATA_END);
                    385:   fflush(NULL);
                    386: }
1.2       takayama  387:
                    388: /* test data
                    389:
                    390: 1.  print("hello"); print("afo");
                    391:
                    392:     1+2;
                    393:
                    394: 2. def foo(N) { for (I=0; I<10; I++) {   --> error
                    395:
                    396:    3+5;
                    397:
                    398: 4.  print("hello"); shift+return print("afo");
                    399:
                    400: */
                    401:
                    402:
                    403:

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