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

1.31    ! takayama    1: /* $OpenXM: OpenXM/src/kxx/ox_texmacs.c,v 1.30 2006/03/03 02:30:09 takayama Exp $ */
1.1       takayama    2:
                      3: #include <stdio.h>
1.23      takayama    4: #include <stdlib.h>
                      5: #include <string.h>
1.1       takayama    6: #include <setjmp.h>
                      7: #include <signal.h>
                      8: #include "ox_kan.h"
                      9: #include "serversm.h"
                     10:
                     11: #if defined(__CYGWIN__)
                     12: #define JMP_BUF sigjmp_buf
                     13: #define SETJMP(env)  sigsetjmp(env,1)
                     14: #define LONGJMP(env,p)  siglongjmp(env,p)
                     15: #else
                     16: #define JMP_BUF jmp_buf
                     17: #define SETJMP(env)  setjmp(env)
                     18: #define LONGJMP(env,p)  longjmp(env,p)
                     19: #endif
                     20:
1.3       takayama   21: /*
                     22: #define DEBUG
                     23: */
1.7       takayama   24: /* #define DEBUG2 */
1.3       takayama   25:
1.23      takayama   26: /* Type of View part (user interface engine) */
                     27: #define  GENERIC      0   /* DEBUG, xml */
                     28: #define  V_TEXMACS    1
                     29: #define  V_CFEP       2
                     30: int View       = V_TEXMACS ;
                     31:
                     32: char *Data_begin_v[] = {
                     33:   "<S type=verbatim>",
                     34:   "\002verbatim:",
                     35:   "\002",
                     36: };
                     37: char *Data_begin_l[] = {
                     38:   "<S type=latex>",
                     39:   "\002latex:",
                     40:   "\002latex:"
                     41: };
                     42: char *Data_begin_p[] = {
                     43:   "<S type=prompt>",
                     44:   "\002prompt:",
                     45:   "\002prompt:"
                     46: };
                     47: char *Data_begin_ps[] = {
                     48:   "<S type=postscript>",
                     49:   "\002ps:",
                     50:   "\002ps:"
                     51: };
                     52: char *Data_end[] = {
                     53:   "</S>",
                     54:   "\005",
1.25      takayama   55:   "\n\005"    /* \n is not a part of the protocol. */
1.23      takayama   56: };
                     57:
1.25      takayama   58: /* todo:  start_of_input */
1.2       takayama   59:
1.25      takayama   60: char End_of_input[] = {
                     61:   0x5,              /* Use ^E and Return to end the input. */
                     62:   '\n',  /* TEXMACS_END_OF_INPUT. 0xd should be used for multiple lines. */
                     63:   0x5    /* CFEP_END_OF_INPUT    */
                     64: };
1.1       takayama   65:
1.5       takayama   66: /* Table for the engine type. */
                     67: #define ASIR          1
                     68: #define SM1           2
                     69: #define K0            3
                     70:
1.1       takayama   71: extern int Quiet;
                     72: extern JMP_BUF EnvOfStackMachine;
1.19      takayama   73: extern int Calling_ctrlC_hook;
1.20      takayama   74: extern int RestrictedMode, RestrictedMode_saved;
1.3       takayama   75: int Format=1;  /* 1 : latex mode */
                     76: int OutputLimit_for_TeXmacs = (1024*10);
1.1       takayama   77:
1.22      takayama   78:
1.5       takayama   79: int TM_Engine  = ASIR ;
                     80: int TM_asirStarted = 0;
                     81: int TM_sm1Started  = 0;
                     82: int TM_k0Started  = 0;
1.7       takayama   83: int TM_do_not_print = 0;
1.5       takayama   84:
1.15      takayama   85: int Xm_noX = 0;
                     86: int NoCopyright = 0;
1.25      takayama   87: int Cpp = 0;                 /* Use cpp before sending to the engine. */
                     88: int EngineLogToStdout = 0;   /* Do not run the ox engine inside xterm. */
1.15      takayama   89:
1.29      takayama   90: char *LanguageResource = NULL;
                     91:
1.1       takayama   92: void ctrlC();
                     93: struct object KpoString(char *s);
                     94: char *KSpopString(void);
                     95:
1.2       takayama   96: static char *readString(FILE *fp,char *prolog, char *eplog);
1.1       takayama   97: static void printv(char *s);
                     98: static void printl(char *s);
                     99: static void printp(char *s);
1.17      takayama  100: static void printps(char *s);
1.2       takayama  101: static void printCopyright(char *s);
1.5       takayama  102: static int startEngine(int type,char *msg);
1.17      takayama  103: static int isPS(char *s);
1.22      takayama  104: static int end_of_input(int c);
                    105: static void setDefaultParameterForCfep();
1.2       takayama  106:
1.27      takayama  107: static void myEncoder(int c);
                    108: static void myEncoderS(unsigned char *s);
                    109: static void myEncoderSn(unsigned char *s,int n);
                    110: static void outputStringToTunnel0(int channel, unsigned char *s, int size, int view);
                    111: static void outputStringToTunnel(int channel, unsigned char *s,  int view);
                    112:
1.31    ! takayama  113: static void flushSm1();
        !           114:
1.2       takayama  115: /* tail -f /tmp/debug-texmacs.txt
                    116:    Debug output to understand the timing problem of pipe interface.
                    117: */
1.3       takayama  118: FILE *Dfp;
1.1       takayama  119:
1.10      takayama  120: main(int argc,char *argv[]) {
1.1       takayama  121:   char *s;
                    122:   char *r;
                    123:   char *sys;
1.21      takayama  124:   struct object ob = OINIT;
1.3       takayama  125:   int irt=0;
1.4       takayama  126:   int vmode=1;
1.7       takayama  127:   char *openxm_home;
                    128:   char *asir_config;
1.10      takayama  129:   int i;
1.1       takayama  130:
1.7       takayama  131:
                    132:
1.3       takayama  133: #ifdef DEBUG2
                    134:   Dfp = fopen("/tmp/debug-texmacs.txt","w");
                    135: #endif
1.2       takayama  136:
1.1       takayama  137:   /* Set consts */
                    138:   Quiet = 1;
1.10      takayama  139:   for (i=1; i<argc; i++) {
1.22      takayama  140:     if (strcmp(argv[i],"--view") == 0) {
                    141:       i++;
                    142:       if (strcmp(argv[i],"texmacs") == 0) {
                    143:         View = V_TEXMACS;
                    144:       }else if (strcmp(argv[i],"cfep")==0) {
                    145:         View = V_CFEP; setDefaultParameterForCfep();
                    146:       }else{
1.25      takayama  147:         View = GENERIC;
1.22      takayama  148:         /* printv("Unknown view type.\n"); */
                    149:       }
                    150:     } else if (strcmp(argv[i],"--sm1") == 0) {
                    151:       TM_Engine = SM1;
                    152:     }else if (strcmp(argv[i],"--asir") == 0) {
                    153:       TM_Engine = ASIR;
                    154:     }else if (strcmp(argv[i],"--k0") == 0) {
                    155:       TM_Engine = K0;
1.14      takayama  156:     }else if (strcmp(argv[i],"--outputLimit") == 0) {
                    157:       i++;
                    158:       sscanf(argv[i],"%d",&OutputLimit_for_TeXmacs);
1.15      takayama  159:     }else if (strcmp(argv[i],"--noLogWindow") == 0) {
1.22      takayama  160:       Xm_noX = 1;
1.15      takayama  161:     }else if (strcmp(argv[i],"--noCopyright") == 0) {
1.22      takayama  162:       NoCopyright = 1;
1.25      takayama  163:     }else if (strcmp(argv[i],"--cpp") == 0) {
                    164:       Cpp = 1;
                    165:     }else if (strcmp(argv[i],"--engineLogToStdout") == 0) {
                    166:       EngineLogToStdout = 1;
1.29      takayama  167:     }else if (strcmp(argv[i],"--languageResource") == 0) {
                    168:       i++;
                    169:       LanguageResource = (char *)sGC_malloc(strlen(argv[i])+80);
                    170:       sprintf(LanguageResource,
                    171:                          " /localizedString.file (%s) def localizedString.load ",argv[i]);
1.22      takayama  172:     }else{
                    173:       /* printv("Unknown option\n"); */
                    174:     }
1.10      takayama  175:   }
1.1       takayama  176:
1.23      takayama  177:   openxm_home = (char *) getenv("OpenXM_HOME");
                    178:   asir_config = (char *) getenv("ASIR_CONFIG");
                    179:   if (openxm_home == NULL)  {
                    180:     printv("Error. The environmental variable OpenXM_HOME is not set.\nStart the texmacs with openxm texmacs or ox_texmacs by openxm ox_texmacs\nBye...");
                    181:     exit(10);
                    182:   }
                    183:
1.1       takayama  184:   /* Initialize kanlib (gc is also initialized) */
                    185:   KSstart();
                    186:
1.3       takayama  187:   /* Main loop */
1.23      takayama  188:   printf("%s",Data_begin_v[View]);
                    189:   if ((asir_config == NULL) && (View == V_TEXMACS)) {
                    190:     printf("Warning. The environmental variable ASIR_CONFIG is not set.\nStart the texmacs with openxm texmacs or ox_texmacs by openxm ox_texmacs.\nOtherwise, tex translation will not be performed\n");
                    191:
                    192:   }
                    193:
1.3       takayama  194:   printCopyright("");
                    195:
1.23      takayama  196:
1.1       takayama  197:   /* Load ox engine here */
                    198:   /* engine id should be set to ox.engine */
                    199:   KSexecuteString(" [(parse) (ox.sm1) pushfile] extension ");
1.15      takayama  200:   if (Xm_noX) KSexecuteString(" /Xm_noX 1 def ");
1.26      takayama  201:   if (EngineLogToStdout) KSexecuteString(" /Xm_engineLogToStdout 1 def ");
1.29      takayama  202:   if (LanguageResource != NULL) KSexecuteString(LanguageResource);
1.5       takayama  203:   startEngine(TM_Engine," ");
1.1       takayama  204:
                    205:   if (signal(SIGINT,SIG_IGN) != SIG_IGN) {
1.8       takayama  206:     signal(SIGINT,ctrlC);
1.1       takayama  207:   }
1.3       takayama  208:
                    209:   irt = 0;
1.1       takayama  210:   while(1) {
1.8       takayama  211:     /* printp(sys);  no prompt */
                    212:     if (SETJMP(EnvOfStackMachine)) {
1.19      takayama  213:       if (!Calling_ctrlC_hook) {
1.20      takayama  214:         Calling_ctrlC_hook = 1; RestrictedMode = 0;
1.19      takayama  215:         KSexecuteString(" ctrlC-hook "); /* Execute User Defined functions. */
1.20      takayama  216:         RestrictedMode = RestrictedMode_saved;
1.19      takayama  217:       }
                    218:       Calling_ctrlC_hook = 0;
1.8       takayama  219:       if (signal(SIGINT,SIG_IGN) != SIG_IGN) {
                    220:         signal(SIGINT,ctrlC);
                    221:       }
                    222:       irt = 1;
                    223:       continue;
                    224:     } else {  }
                    225:     if (!irt) {
1.23      takayama  226:       printf("%s",Data_end[View]); fflush(stdout);
1.8       takayama  227:     }
                    228:     irt = 0;
1.7       takayama  229:
                    230:     /* Reading the input. */
1.8       takayama  231:     if (TM_Engine == K0) {
                    232:       s=readString(stdin, " ", " "); /* see test data */
                    233:     }else if (TM_Engine == SM1) {
                    234:       s=readString(stdin, " ", " "); /* see test data */
                    235:     }else{
                    236:       s=readString(stdin, "if (1) { ", " ; }else{ }"); /* see test data */
                    237:     }
1.5       takayama  238:
1.8       takayama  239:     if (s == NULL) { irt = 1; continue; }
1.23      takayama  240:     if (!irt) printf("%s",Data_begin_v[View]);
1.7       takayama  241:     /* Evaluate the input on the engine */
1.27      takayama  242:     KSexecuteString(" ox.engine oxclearstack ");
1.1       takayama  243:     KSexecuteString(" ox.engine ");
1.7       takayama  244:     ob = KpoString(s);
1.8       takayama  245:     KSpush(ob);
                    246:     KSexecuteString(" oxsubmit ");
1.31    ! takayama  247:
        !           248:        if (TM_Engine == SM1) flushSm1();
1.8       takayama  249:
1.27      takayama  250:     /* Get the result in string for cfep. */
                    251:     if (View != V_TEXMACS) {
                    252:       KSexecuteString(" ox.engine oxgeterrors ");
                    253:       ob = KSpop();
                    254:       if (ob.tag == Sarray) {
                    255:         if (getoaSize(ob) > 0) {
                    256:           ob = getoa(ob,0);
                    257:           KSpush(ob);
                    258:           KSexecuteString(" translateErrorForCfep ");
                    259:           r = KSpopString();
                    260:           outputStringToTunnel(0,(unsigned char *)r,View);
                    261:         }
                    262:       }
                    263:       if (!TM_do_not_print) {
                    264:         KSexecuteString(" ox.engine oxpopstring ");
                    265:         r = KSpopString();
                    266:         printv(r);
                    267:       }else{
                    268:         KSexecuteString(" ox.engine 1 oxpops ");
                    269:         KSexecuteString(" ox.engine 0 oxpushcmo ox.engine oxpopcmo ");
                    270:         ob = KSpop();
                    271:         printv("");
                    272:       }
                    273:       continue;
                    274:     }
                    275:     /* Get the result in string for texmacs  */
1.8       takayama  276:     if (Format == 1 && (! TM_do_not_print)) {
                    277:       /* translate to latex form */
                    278:       KSexecuteString(" ox.engine oxpushcmotag ox.engine oxpopcmo ");
                    279:       ob = KSpop();
                    280:       vmode = 0;
                    281:       /* printf("id=%d\n",ob.tag); bug: matrix return 17 instead of Sinteger
                    282:        or error. */
                    283:       if (ob.tag == Sinteger) {
                    284:         /* printf("cmotag=%d\n",ob.lc.ival);*/
                    285:         if (ob.lc.ival == CMO_ERROR2) {
                    286:           vmode = 1;
                    287:         }
                    288:         if (ob.lc.ival == CMO_STRING) {
                    289:           vmode = 1;
                    290:         }
                    291:       }
                    292:       if (vmode) {
                    293:         KSexecuteString(" ox.engine oxpopstring ");
                    294:         r = KSpopString();
                    295:       }else{
                    296:         KSexecuteString(" ox.engine 1 oxpushcmo ox.engine (print_tex_form) oxexec  ");
                    297:         KSexecuteString(" ox.engine oxpopstring ");
                    298:         r = KSpopString();
                    299:       }
1.17      takayama  300:       if (isPS(r)) {
                    301:         printps(r);
                    302:       }else{
                    303:         if (vmode) printv(r);
                    304:         else{
                    305:           if (strlen(r) < OutputLimit_for_TeXmacs) {
                    306:             printl(r);
                    307:           } else printv("Output is too large.\n");
                    308:         }
                    309:       }
1.8       takayama  310:     }else{
                    311:       if (!TM_do_not_print) {
                    312:         KSexecuteString(" ox.engine oxpopstring ");
                    313:         r = KSpopString();
1.17      takayama  314:         printv(r);
1.8       takayama  315:       }else{
                    316:         KSexecuteString(" ox.engine 1 oxpops "); /* Discard the result. */
1.12      takayama  317:         /* Push and pop dummy data to wait until the computation finishes. */
                    318:         KSexecuteString(" ox.engine 0 oxpushcmo ox.engine oxpopcmo ");
                    319:         ob = KSpop();
1.8       takayama  320:         printv("");
                    321:       }
                    322:     }
1.27      takayama  323:     /* note that there is continue above. */
1.1       takayama  324:   }
                    325: }
                    326:
                    327: #define SB_SIZE 1024
1.8       takayama  328: #define INC_BUF     if (n >= limit-3) { \
                    329:       tmp = s; \
                    330:       limit *= 2;  \
                    331:       s = (char *) sGC_malloc(limit); \
                    332:       if (s == NULL) { \
                    333:         fprintf(stderr,"No more memory.\n"); \
                    334:         exit(10); \
                    335:       } \
                    336:       strcpy(s,tmp); \
                    337:     }
1.2       takayama  338: /*   */
                    339: static char *readString(FILE *fp, char *prolog, char *epilog) {
1.1       takayama  340:   int n = 0;
                    341:   static int limit = 0;
                    342:   static char *s;
                    343:   int c;
                    344:   char *tmp;
1.2       takayama  345:   int i;
1.3       takayama  346:   int m;
                    347:   int start;
1.21      takayama  348:   struct object ob = OINIT;
1.1       takayama  349:   if (limit == 0) {
1.8       takayama  350:     limit = 1024;
                    351:     s = (char *)sGC_malloc(limit);
                    352:     if (s == NULL) {
                    353:       fprintf(stderr,"No more memory.\n");
                    354:       exit(10);
                    355:     }
1.1       takayama  356:   }
1.2       takayama  357:   s[0] = 0; n = 0; m = 0;
                    358:   for (i=0; i < strlen(prolog); i++) {
1.8       takayama  359:     s[n++] = prolog[i];  s[n] = 0;
1.2       takayama  360:     INC_BUF ;
                    361:   }
1.3       takayama  362:   start = n;
1.1       takayama  363:   while ((c = fgetc(fp)) != EOF) {
1.3       takayama  364: #ifdef DEBUG2
1.8       takayama  365:     fprintf(Dfp,"[%x] ",c); fflush(Dfp);
1.3       takayama  366: #endif
1.22      takayama  367:     if (end_of_input(c)) {
1.18      takayama  368:       /* If there remains data in the stream,
1.25      takayama  369:          read the remaining data. (for debug) */
1.18      takayama  370:          /*
1.8       takayama  371:       if (oxSocketSelect0(0,1)) {
                    372:         if (c == '\n') c=' ';
                    373:         s[n++] = c; s[n] = 0;  m++;
                    374:         INC_BUF ;
                    375:         continue;
                    376:       }
1.18      takayama  377:       */
1.8       takayama  378:       break;
                    379:     }
1.18      takayama  380:     if ( c == '\v') c=' ';
1.8       takayama  381:     s[n++] = c; s[n] = 0;  m++;
1.2       takayama  382:     INC_BUF ;
                    383:   }
1.5       takayama  384:   /* Check the escape sequence */
                    385:   if (strcmp(&(s[start]),"!quit;") == 0) {
1.8       takayama  386:     printv("Terminated the process ox_texmacs.\n");
                    387:     exit(0);
1.3       takayama  388:   }
1.11      takayama  389:   /* Check the escape sequence to change the global env. */
1.5       takayama  390:   if (strcmp(&(s[start]),"!verbatim;") == 0) {
1.8       takayama  391:     printv("Output mode is changed to verbatim mode.");
                    392:     Format=0;
                    393:     return NULL;
1.5       takayama  394:   }
                    395:   if (strcmp(&(s[start]),"!latex;") == 0) {
1.8       takayama  396:     printv("Output mode is changed to latex/verbose.");
                    397:     Format = 1;
                    398:     return NULL;
1.5       takayama  399:   }
                    400:   if (strcmp(&(s[start]),"!asir;") == 0) {
1.8       takayama  401:     Format=1;
                    402:     TM_Engine=ASIR; startEngine(TM_Engine,"Asir");
                    403:     return NULL;
1.5       takayama  404:   }
                    405:   if (strcmp(&(s[start]),"!sm1;") == 0) {
1.8       takayama  406:     Format=0;
                    407:     TM_Engine=SM1; startEngine(TM_Engine,"sm1");
                    408:     return NULL;
1.5       takayama  409:   }
                    410:   if (strcmp(&(s[start]),"!k0;") == 0) {
1.8       takayama  411:     Format=0;
                    412:     TM_Engine=K0; startEngine(TM_Engine,"k0");
                    413:     return NULL;
1.5       takayama  414:   }
1.13      takayama  415:   if (strcmp(&(s[start]),"!reset;") == 0) {
1.23      takayama  416:        printf("%s",Data_begin_v[View]);
1.13      takayama  417:     KSexecuteString(" ox.engine oxreset ox.engine oxpopcmo ");
                    418:        ob = KSpop();
1.23      takayama  419:        printf("%s",Data_end[View]); fflush(stdout);
1.13      takayama  420:     return NULL;
                    421:   }
1.11      takayama  422:
                    423:   /* Set TM_do_no_print */
                    424:   if (s[n-1] == '$' && TM_Engine == ASIR) {
                    425:        TM_do_not_print = 1; s[n-1] = ' ';
                    426:   } else if (s[n-1] == ';' && TM_Engine == SM1) {
                    427:        TM_do_not_print = 1; s[n-1] = ' ';
                    428:   } else TM_do_not_print = 0;
1.5       takayama  429:
1.2       takayama  430:   for (i=0; i < strlen(epilog); i++) {
1.8       takayama  431:     s[n++] = epilog[i];  s[n] = 0;
1.2       takayama  432:     INC_BUF ;
1.1       takayama  433:   }
                    434:   return s;
1.22      takayama  435: }
                    436:
                    437: static int end_of_input(int c) {
1.25      takayama  438:   if (c == End_of_input[View]) return 1;
                    439:   else return 0;
1.22      takayama  440: }
1.25      takayama  441:
1.22      takayama  442: static void setDefaultParameterForCfep() {
                    443:   Format = 0;
1.1       takayama  444: }
                    445:
                    446: static void printv(char *s) {
                    447:   int i;
1.23      takayama  448:   printf("%s",Data_begin_v[View]);
1.1       takayama  449:   printf("%s",s);
1.23      takayama  450:   printf("%s",Data_end[View]);
1.3       takayama  451: #ifdef DEBUG2
                    452:   fprintf(Dfp,"<%s>",s); fflush(Dfp);
                    453: #endif
1.1       takayama  454:   fflush(NULL);
                    455: }
                    456: static void printl(char *s) {
1.23      takayama  457:   printf("%s",Data_begin_l[View]);
1.4       takayama  458:   printf(" $ %s $ ",s);
1.23      takayama  459:   printf("%s",Data_end[View]);
1.17      takayama  460:   fflush(NULL);
                    461: }
                    462: static int isPS(char *s) {
                    463:   if (s[0] != '%') return 0;
                    464:   if (s[1] != '%') return 0;
                    465:   if (s[2] != '!') return 0;
                    466:   if (s[3] != 'P') return 0;
                    467:   if (s[4] != 'S') return 0;
                    468:   return 1;
                    469: }
                    470: static void printps(char *s) {
1.23      takayama  471:   printf("%s",Data_begin_ps[View]);
1.17      takayama  472:   printf("%s",s);
1.23      takayama  473:   printf("%s",Data_end[View]);
1.1       takayama  474:   fflush(NULL);
                    475: }
                    476: static void printp(char *s) {
1.23      takayama  477:   printf("%s",Data_begin_p[View]);
                    478:   printf("%s",Data_end[View]);
1.2       takayama  479:   printf("%s] ",s);
                    480:   fflush(NULL);
                    481: }
                    482: static void printCopyright(char *s) {
1.23      takayama  483:   printf("%s",Data_begin_v[View]);
1.15      takayama  484:   if (! NoCopyright) {
1.24      takayama  485:     printf("OpenXM engine (ox engine) interface with TeXmacs protocol.\n2004 (C) openxm.org");
                    486:     printf(" under the BSD license.  !asir; !sm1; !k0; !verbatim; !quit;\n");
1.15      takayama  487:     printf("Type in      !reset;     when the engine gets confused. ");
                    488:     printf("%s",s);
                    489:   }
1.23      takayama  490:   printf("%s",Data_end[View]);
1.5       takayama  491:   fflush(NULL);
                    492: }
                    493:
1.31    ! takayama  494: static void flushSm1() {
        !           495:   KSexecuteString(" oxsm1.ccc ( [(flush)] extension pop ) oxsubmit ");
        !           496: }
1.5       takayama  497: static int startEngine(int type,char *msg) {
1.21      takayama  498:   struct object ob = OINIT;
1.23      takayama  499:   printf("%s",Data_begin_v[View]);
1.5       takayama  500:   if (type == SM1) {
                    501:     if (!TM_sm1Started) KSexecuteString(" sm1connectr ");
1.8       takayama  502:     KSexecuteString(" /ox.engine oxsm1.ccc def ");
1.10      takayama  503:     /* Initialize the setting of sm1. */
                    504:     KSexecuteString("  oxsm1.ccc ( [(cmoLispLike) 0] extension ) oxsubmit ");
                    505:     KSexecuteString("  oxsm1.ccc ( ox_server_mode ) oxsubmit ");
1.31    ! takayama  506:     KSexecuteString("  oxsm1.ccc ( ( ) message (------------- Message from sm1 ----------------)message ) oxsubmit ");
        !           507:        flushSm1();
1.8       takayama  508:     TM_sm1Started = 1;
1.10      takayama  509:        /* Welcome message.  BUG. Copyright should be returned by a function. */
1.15      takayama  510:     if (! NoCopyright) {
                    511:       printf("Kan/StackMachine1                         1991 April --- 2004.\n");
                    512:       printf("This software may be freely distributed as is with no warranty expressed. \n");
                    513:       printf("See OpenXM/Copyright/Copyright.generic\n");
                    514:       printf("Info: http://www.math.kobe-u.ac.jp/KAN, kan@math.kobe-u.ac.jp.\n");
                    515:       printf("0 usages to show a list of functions. \n(keyword) usages to see a short description\n");
                    516:     }
1.30      takayama  517:     printf("%s\n",msg); fflush(NULL);
1.5       takayama  518:   }else if (type == K0) {
                    519:     if (!TM_k0Started) KSexecuteString(" k0connectr ");
1.8       takayama  520:     KSexecuteString(" /ox.engine oxk0.ccc def ");
                    521:     TM_k0Started = 1;
                    522:     printf("%s\n",msg);
1.5       takayama  523:   }else{
                    524:     if (!TM_asirStarted) KSexecuteString(" asirconnectr ");
1.8       takayama  525:     KSexecuteString(" /ox.engine oxasir.ccc def ");
                    526:     TM_asirStarted = 1;
                    527:     printf("%s\n",msg);
1.24      takayama  528:     if ((!NoCopyright) && ((char *)getenv("ASIR_CONFIG") != NULL)) {
1.15      takayama  529:       KSexecuteString(" oxasir.ccc (copyright()+asir_contrib_copyright();) oxsubmit oxasir.ccc oxpopstring ");
1.24      takayama  530:       ob = KSpop();
                    531:       if (ob.tag == Sdollar) {
                    532:         printf("%s",ob.lc.str);
                    533:       }
                    534:     }else if ((!NoCopyright) && ((char *)getenv("ASIR_CONFIG") == NULL)) {
                    535:       KSexecuteString(" oxasir.ccc (copyright();) oxsubmit oxasir.ccc oxpopstring ");
1.15      takayama  536:       ob = KSpop();
                    537:       if (ob.tag == Sdollar) {
                    538:         printf("%s",ob.lc.str);
                    539:       }
1.8       takayama  540:     }
1.7       takayama  541:     /* Initialize the setting of asir. */
                    542:     KSexecuteString(" oxasir.ccc (if(1) {  Xm_server_mode = 1; Xm_helpdir = \"help-eg\";  } else { ; } ;) oxsubmit oxasir.ccc oxpopcmo ");
                    543:     KSexecuteString(" oxasir.ccc (if(1) {  ctrl(\"message\",0);  } else { ; } ;) oxsubmit oxasir.ccc oxpopcmo ");
                    544:     /* bug; if ctrl is written with Xm_helpdir = ... without oxpopcmo, then it does
                    545:        not work. */
1.9       takayama  546:     KSexecuteString(" oxasir.ccc (print(\"----------- Messages from asir ------------------------------\")$ ) oxsubmit oxasir.ccc oxpopcmo ");
1.5       takayama  547:   }
1.23      takayama  548:   printf("%s",Data_end[View]);
1.1       takayama  549:   fflush(NULL);
                    550: }
1.2       takayama  551:
                    552: /* test data
                    553:
                    554: 1.  print("hello"); print("afo");
                    555:
                    556:     1+2;
                    557:
                    558: 2. def foo(N) { for (I=0; I<10; I++) {   --> error
                    559:
                    560:    3+5;
                    561:
                    562: 4.  print("hello"); shift+return print("afo");
                    563:
                    564: */
1.27      takayama  565:
                    566: static void myEncoder(int c) {
                    567:   putchar(0xf8 | (0x3 & (c >> 6)));
                    568:   putchar(0xf0 | (0x7 & (c >> 3)));
                    569:   putchar(0xf0 | (0x7 & c));
                    570: }
                    571: static void myEncoderS(unsigned char *s) {
                    572:   int i,n;
                    573:   n = strlen(s);
                    574:   for (i = 0; i<n ; i++) myEncoder(s[i]);
                    575: }
                    576: static void myEncoderSn(unsigned char *s,int n) {
                    577:   int i;
                    578:   for (i = 0; i<n ; i++) myEncoder(s[i]);
                    579: }
                    580: static void outputStringToTunnel0(int channel, unsigned char *s, int n,int view) {
                    581:   unsigned char ts[128];
                    582:   int i;
                    583:   if (view == GENERIC) {
                    584:     printf("{%d<%d ",channel,n+1);
                    585:     for (i=0; i<n; i++) putchar(s[i]);
                    586:     printf("%c>}",0);
                    587:   }else if (view == V_CFEP) {
                    588:     sprintf(ts,"{%d<%d ",channel,n+1);
                    589:     myEncoderS(ts);
                    590:     myEncoderSn(s,n); myEncoder(0);
                    591:     myEncoderS(">}");
                    592:   }
                    593:   fflush(stdout);
                    594: }
1.28      takayama  595: #define LINE_FEED 0xc   /* ^L */
1.27      takayama  596: static void outputStringToTunnel(int channel, unsigned char *s, int view) {
                    597:   int start, i;
                    598:   start = i = 0;
                    599:   while (s[i] != 0) {
1.28      takayama  600:        if (s[i] == LINE_FEED) {
                    601:          outputStringToTunnel0(channel,&(s[start]),i-start,view);
1.27      takayama  602:          start = i+1;
                    603:        }
                    604:        i++;
                    605:   }
                    606: }
                    607:
1.2       takayama  608:
                    609:
                    610:

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