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

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

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