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

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

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