Example #1
0
static void traiteLigne(char *b, int Ctx)
{
char *mot, *d, *f, *w;
   /* case of sh command : ! */
   if (*b=='!') {
     runCommandT(b+1);
     return;
   }
   d=b; f=b+strlen(d);
#ifdef DEBUG
     printf("traiteLigne : <%s>\n",d);
#endif
   switch(Ctx) {
     case 1 : /* compileFile */
       D_Trace(" #");
       break;
     case 2 : /* IF_ExecCS */
       D_Trace("# ExecCS:");
       break;
     case 3 : /* makeFunction */
       D_Trace("# makeFunction:");
       break;
     default : /* 0 */
       if (getiFD()) D_Trace(" #");
   }
   D_Tracenl(b);
   while (d<f) {
       if (noErr()) break;
       /* recherche du 1er mot */
       if (stringEnCours) {
         mot = d;
         while (1) {
            if((d = strchr(d,'"')) == NULL) {
               d=mot+strlen(mot);
               break;
            }
            if (*(d-1) == '\\') {
               w = d-1;
               while (*w != '\0') {
                   *w = *(w+1);
                   w++;
               }
               continue;
            }
            d++;
            if (!isSepa(*d,1)) continue;
            break;
         }
       }  else {
         /* on ignore les commentaires */
         if ((mot = strchr(d, (int)'#')) != NULL) {
                *mot = '\0';
                f = mot;
         }
         while (isSepa(*d,0)) d++; /* on avance tant que separateurs */
         mot = d;
         while (!isSepa(*d,1)) d++; /* on avance si nonSepa ET non \0 */
       }
       *d++ = '\0';  /* fin de la commande */
       if (strlen(mot)>0)
          if (traiteMot(mot)) break; /* abort if error */
   }
}
Example #2
0
int lireLigne(int fd, char *b, char *s, int nc)
/* fd = file descriptor
   b  = adresse du buffer
   nc = nb de caracteres possible (longueur du buffer */
{
char *d, *f, c, c2, c3, *h, *w, *Wl, *rac;
int n, i, l, ls=0, ins=0, ignTild=0, nbT=0, Nc;
unsigned int j;
char bufd[50];
   /* printf("lireLigne ... \n"); */
   d = b;
   f = b+nc;
   while(d<f-ls) {
     if (noErr()) {
         n = -1;
         break;
     }
     /* fprintf(stderr,"d-b=%d ins=%d s=<%s> b=<%s>\n",d-b,ins,s,b); */
     if ((n=read(fd,d,1)) != 1) break;
     c=*d;
     if (ignTild && (c == '~')) {
        ignTild=0; continue;
     }
     if ((c > 31) && (c < 127)) { /* de SPACE a TILDE */
         if (!((FD_IN || iTERM) && ECHOOFF)) {
               printf("%c",c);
               if (ins) {
                  if (ins==2)  /* rewrite */
                     enleve1(s);
                  if(*s =='\0') ins=0;
                  else {
                     printf("%s",s);
                     for (j=0; j<strlen(s); j++) printf("\b");  
                  }
               }
         }
         fflush(stdout);
         d++;
     } else {
         switch (c) {
            /* Ctrl C
            case '\003' : 
              kill(getpid(), SIGINT);
              break; */
            case '\t': /* tab */
              if (d>b) {
                 *d='\0';
                 w=d-1;
                 while ((w>b) && (!(isSepa(*w,1)))) w--;
                 if (isSepa(*w,0)) w++;
                 /* fprintf(stderr,"d-b=%d  w=<%s>\n",d-b,w); */
                 if (strlen(w) > 0) {
                    j=nbLibBegin(w, &rac);
                    /* fprintf(stderr,"j=%d  w=<%s>\n",j,w); */
                    if (j==0) printf("\a");
                    else {
                       if (j==1) {
                          Wl=getLibBegin(w);
                          i=strlen(Wl)-strlen(w);
                          /* fprintf(stderr,"i=%d  Wl=<%s>\n",i,Wl); */
                          if (i>0) {
                             strcpy(w,Wl);
                             printf("%s ",d);
                             d+=i;
                             *d++ = ' ';
                             if (ins) {
                              printf("%s ",s);
                              for (j=0; j<(strlen(s)+1); j++) printf("\b");  
                             }
                          } else { /* XXXX */
                             if (i==0) {
                               printf (" ");
                               *d++ = ' ';
                             }
                          }
                       } else {
                          if (rac != NULL) {
                             i=strlen(rac)-strlen(w);
                             strcpy(w,rac);
                             printf("%s",d);
                             d+=i;
                             if (ins) {
                              printf("%s ",s);
                              for (j=0; j<(strlen(s)+1); j++) printf("\b");  
                             }
                          } else {
                            nbT++;
                            if (nbT>1) {
                               nbT=0;
                               printf("\n");
                               listLibBegin(w);
                               *d='\0';
                               printf("> %s",b);
                               if (ins) {
                                printf("%s ",s);
                                for (j=0; j<(strlen(s)+1); j++) printf("\b");  
                               }
                               fflush(stdout);
                            }
                          }
                       }
                    }
                    fflush(stdout);
                 }
              }
              break;
            case '\177':
              if (d>b) {
                 printf("\b \b");
                 if (ins) {
                  printf("%s ",s);
                  for (j=0; j<(strlen(s)+1); j++) printf("\b");  
                 }
                 fflush(stdout);
                 d--;
              }
              break;
            case '\n':
            case '\r':
              if ((FD_IN || iTERM) && ECHOOFF) printf(".");
              else {
                 printf("\n");
                 if (ins) {
                     if (d+strlen(s) < f) {
                        sprintf(d,"%s",s);
                        d+=strlen(s);
                     } else return(-1);
                 }
              }
              goto finBoucle;
         /* gestion des caracteres speciaux */
            case '\033':   /* ESCAPE */
              ignTild=1;
              Nc=read(fd,&c2,1);
              Nc=read(fd,&c3,1);
              if (c2 == '[') { /* CSI - see XTerm Control Sequences */
                  switch(c3) {
                      case '2' : /* Insert */
                        if (ins) {
                           ins++;
                           if (ins==3) ins=1;
                        }
                        break; 
                      case '3' : /* Suppr */
                        if (ins) {
                           enleve1(s);
                           if(*s =='\0') ins=0;
                           printf("%s \b",s);
                           for (j=0; j<strlen(s); j++) printf("\b");  
                           fflush(stdout);
                        }
                        break; 
                      case 'A' : /* UpArrow */
                      case 'B' : /* DownArrow */
                        ins = 0;
                        /* efface la ligne en cours */
                        l=d-b;
                        for(i=0;i<l;i++) printf("\b \b");
                        fflush(stdout);
                        d=b;
                        *d = '\0';
                        if ((h=getHisto(c3)) != NULL) {
                           strcpy(b,h);
                           d=b+strlen(h);
/*
                           printf("\n%s (iH=%d iHt=%d)\n",h,iH,iHt);
                           *d='\0';
*/
                           printf("%s",b);
                           fflush(stdout);
                        }
                        break; 
                      case 'C' : /* -> */
                        if (ins) {
                           *d = *s;
                           printf("%c",*d++);
                           enleve1(s);
                           if(*s =='\0') ins=0;
                           else {
                              printf("%s",s);
                              for (j=0; j<strlen(s); j++) printf("\b");  
                           }
                           fflush(stdout);
                        }
                        break; 
                      case 'D' : /* <- */
                        if (d>b) {
                           if (ins==0) {
                              ins=1;
                              *d='\0';
                              strcpy(s,d-1);
                           } else insertC(*(d-1),s);
                           d--;
                           printf("\b");
                           fflush(stdout);
                        }
                        break; 
                      case 'H' : /* home : debug terms ON */
                      case '1' : /* home numeric */
                        IFD_DebugTOn();
                        break; 
                      case 'F' : /* end : debug terms OFF */
                      case '4' : /* end numeric */
                        IFD_DebugTOff();
                        break; 
                      default:
                        sprintf(bufd,"# ignore : ESC [ %d (%c) !",(int)c3, c3);
                        D_Tracenl(bufd);
                        *d='\0';
                        /* **************
                        printf("> %s",b);
                        fflush(stdout); */
                  }
              } else {
                 if (c2 == 'O') { /* SS3 - see XTerm Control Sequences */
                    switch(c3) {
                      case 'P' : /* F1 */
                      break; 
                      case 'Q' : /* F2 */
                      break; 
                      case 'R' : /* F3 */
                      break; 
                      case 'S' : /* F4 */
                      break; 
                      case 'H' : /* home : debug terms ON */
                        IFD_DebugTOn();
                        break; 
                      case 'F' : /* end : debug terms OFF */
                        IFD_DebugTOff();
                        break; 
                    }
                 } else {
                  sprintf(bufd,"# ignore : ESC %d %d (%c) !",(int)c2,(int)c3, c3);
                  D_Tracenl(bufd);
                  *d='\0';
                  /* **************
                  printf("> %s",b);
                  fflush(stdout); */
                 }
              }
              break; 
            default :
/*
              printf("\nCar = %d !\n",(int)c);
              *d='\0';
              printf("> %s",b);
              fflush(stdout);
*/
              break; 
         }
     }
     if (ins) ls = strlen(s);
   }
finBoucle:
   /* printf("fin lireLigne!\n"); */
   if ((n<1) && (FD_IN !=0)) {
      closeFD();  /* fichier loader */
      if (ECHOOFF) printf("\n");
      return 0;
   }
   if ((n<1) && iTERM) {
      close(FD_IN);  /* pipe ou autre */
      Nc=dup(iTERM); /* stdin on term */
      iTERM = 0;
      if (ECHOOFF) printf("\n");
      return 0;
   }
   if (d == f) { /* cas du buffer trop petit */
/*   d=b;
     while (*d != '\n') read(fd,d,1);
**** not in raw mode */
#ifdef DEBUG
     printf("lireLigne : erreur !\n");
#endif
     return(-1);
   } 
   *d = '\0';
#ifdef DEBUG
     printf("lireLigne : retour <%s> !\n",b);
#endif
   if (!FD_IN) putHisto(b);
   incFDlig();
   return(strlen(b));
}