home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / c / csh4.zip / GETCMD.C < prev    next >
C/C++ Source or Header  |  1985-09-03  |  1KB  |  78 lines

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3.  
  4. char *gets();
  5. char rest[256];
  6. char *rptr = NULL;
  7. char pipeactive = 0;
  8. currname = 0;
  9. char *pipename[2] = {
  10.     "shtmp1",
  11.     "shtmp2"};
  12. char *
  13. getnextcmd(buf)
  14.     char *buf;
  15. {
  16.     char *fgets(),*index(),*pipe,*semi;
  17.     if (!rptr)
  18.     {
  19.         register char *c;
  20.         if (0 == read(0,rest,sizeof(rest)))
  21.             return NULL;
  22.         c = rptr = rest;
  23.         while (*c)
  24.         {
  25.             if (*c == '\r' || *c == '\n')
  26.             {
  27.                 *c = '\0';
  28.                 break;
  29.             }
  30.             ++c;
  31.         }
  32.  
  33.     }
  34.     pipe = index(rptr,'|');
  35.     semi = index(rptr,';');
  36.     if (pipe == NULL && semi == NULL)
  37.     {
  38.         strcpy(buf,rptr);
  39.         if (pipeactive)
  40.         {
  41.             pipeactive = 0;
  42.             strcat(buf," < ");
  43.             strcat(buf,pipename[currname]);
  44.         }
  45.         rptr=NULL;
  46.     }
  47.     /* one or the other, or both are not NULL, so comparison is in order */
  48.     else if (pipe && (!semi || (pipe < semi)))
  49.     {
  50.         *pipe = '\0';    /* terminate string */
  51.         strcpy(buf,rptr); /* copy to buf */
  52.         rptr = pipe+1;    /* set up rest */
  53.         if (pipeactive++)
  54.         {
  55.             pipeactive = 1;
  56.             strcat(buf," < ");
  57.             strcat(buf,pipename[currname]);
  58.         }
  59.         strcat(buf," > ");
  60.         currname ^= 1;    /* flip flop pipe names */
  61.         strcat(buf,pipename[currname]);
  62.     }
  63.     else if (semi && (!pipe || (semi < pipe)))
  64.     /* we have a semicolon to deal with */
  65.     {
  66.         *semi = '\0';
  67.         strcpy(buf,rptr);
  68.         rptr = semi+1;
  69.         if (pipeactive)
  70.         {
  71.             pipeactive = 0;
  72.             strcat(buf," < ");
  73.             strcat(buf,pipename[currname]);
  74.         }
  75.     }
  76.     return buf;
  77. }
  78.