home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / AZTEC-C / AZPIPE11.ARK / AZPIPE11.C next >
Text File  |  1989-11-12  |  4KB  |  168 lines

  1. #include <stdio.h>
  2. #include <io.h>
  3. #include <sgtty.h>
  4. #include <ctype.h>
  5.  
  6. #define NUM_MSG 20
  7. #define MSG_SIZ 20
  8. #define TRUE 1
  9. #define FALSE 0
  10.  
  11. char msgarray[NUM_MSG][MSG_SIZ];    /* local array of args to pass on */
  12. int pipeflag;        /* local flag for pipe_set and pipe_exec: TRUE == piping */
  13. char pipein[] = "pipe.in";
  14. char pipeout[] = "pipe.out";
  15.  
  16. /*
  17.     Version 1.1        12 Nov 1989        Bob Presswood
  18. */
  19.  
  20.  
  21. pipe_set(argc,argv)
  22. int *argc; char *argv[];
  23. {
  24. /* Structures channel and fcbtab are defined in io.h */
  25.     struct channel *chanptr;
  26.     struct fcbtab *fcbptr;
  27.     char drive[2], name[9], type[4], user[3];
  28.     char *p, *t;
  29.     int i, j, n;
  30.  
  31.     pipeflag = FALSE;
  32.     for (i = 1; i < *argc; i++){
  33.         /* If pipein exists in arg list, redirect stdin. */
  34.         if (!strcmp(argv[i],pipein)){
  35.             freopen(pipein,"r",stdin);
  36.             for (j = i; j < (*argc - 1); j++)
  37.                 argv[j] = argv[j+1];
  38.             (*argc)--;
  39.             break;
  40.         }
  41.     }
  42.     /* If < 2 args, there can't be a pipe. */
  43.     if (*argc < 2){
  44.         return();
  45.     }
  46.     for (i = 1; i < *argc; i++){
  47.         if (!strcmp(argv[i],"|")){
  48.             for (j = 0; j < NUM_MSG; j++)
  49.                 msgarray[j][0] = 0;                /* Null out msgarray */
  50.             if ((p = index(argv[i+1],':')) && !index(argv[i+1],'/') && (p > (argv[i+1] + 1))){
  51.                 drive[0] = *argv[i+1];
  52.                 drive[1] = user[0] = user[1] = user[2] = '\0';
  53.                 for (j = 1; j < 3; j++){
  54.                     t = argv[i+1] + j;
  55.                     if (isdigit(*t))
  56.                         user[j-1] = *t;
  57.                 }
  58.                 strcpy(msgarray[0],user);
  59.                 strcat(msgarray[0],"/");
  60.                 strcat(msgarray[0],drive);
  61.                 strcat(msgarray[0],p);
  62.             } else
  63.                 strcpy(msgarray[0], argv[i+1]);        /* Prog to run next */
  64.             strcpy(msgarray[1], "?");                /* not passed by execv */
  65.             strcpy(msgarray[2], pipein);            /* stdin from pipein */
  66.             for (j = i + 2, n = 3; j < *argc; j++, n++)
  67.                 strcpy(msgarray[n], argv[j]);    /* Any other args */
  68.  
  69.             /* Is stdout redirected to a file? */
  70.             if (!isatty(1)){
  71.                 drive[1] = name[8] = type[3] = user[2] = '\0';
  72.  
  73.                 /* chantab from io.h, 1 is stdout */
  74.                 chanptr = &chantab[1];
  75.  
  76.                 /* c_arg of chantab[1] is a pointer to stdout fcbtab */
  77.                 fcbptr = chanptr->c_arg;
  78.  
  79.                 /* recover file, etc. for later stdout redirection */
  80.                 if (fcbptr->fcb.f_driv)
  81.                     drive[0] = fcbptr->fcb.f_driv + '@';
  82.                 else
  83.                     drive[0] = '\0';
  84.                 user[0] = (fcbptr->user > 9 ? '1' : '0');
  85.                 user[1] = (fcbptr->user > 9 ? fcbptr->user - 10 + '0' : fcbptr->user + '0');
  86.                 strncpy(name,fcbptr->fcb.f_name,8);
  87.                 if (p = index(name,' '))
  88.                     *p = '\0';
  89.                 strncpy(type,fcbptr->fcb.f_type,3);
  90.  
  91.                 /* add redirected stdout file to msgarray */
  92.                 if (fcbptr->fcb.f_driv)
  93.                     sprintf(msgarray[n],"^%s/%s:%s.%s",user,drive,name,type);
  94.                 else
  95.                     sprintf(msgarray[n],"^%s/%s.%s",user,name,type);
  96.             }
  97.             freopen(pipeout,"w",stdout);
  98.             *argc = i;        /* Extra args not needed for this prog. */
  99.             pipeflag = TRUE;
  100.             break;
  101.         } else if (*argv[i] == '^'){
  102.         /* '^' used to flag for output redirection at end of piping */
  103.             freopen(argv[i]+1,"w",stdout);
  104.             (*argc)--;
  105.             break;
  106.         }
  107.     }
  108. }
  109.  
  110.  
  111. #define NUMPATH 5
  112.  
  113. char path[NUMPATH][6] = {
  114.     "00/A:",
  115.     "00/B:",
  116.     "00/C:",
  117.     "00/D:",
  118.     "00/E:"
  119. };
  120.  
  121. pipe_exec()
  122. {
  123.     char *msgv[NUM_MSG-1];
  124.     char user, current[6], *p, fname[13];
  125.     int i;
  126.  
  127.     /* Erase pipein, ignoring error if it does not exist. */
  128.     unlink(pipein);
  129.     if (pipeflag == TRUE){
  130.         /* Close stdin and stdout, whether redirected or not. */
  131.         fclose(stdin);
  132.         fclose(stdout);
  133.  
  134.         /* If pipeout was created, it must be changed to pipein now. */
  135.         rename(pipeout,pipein);
  136.  
  137.         /* Set array of pointers to char. */
  138.         for (i = 0; i < NUM_MSG - 1; i++)
  139.             if (strlen(msgarray[i+1]))
  140.                 msgv[i] = &msgarray[i+1];
  141.             else
  142.                 msgv[i] = NULL;
  143.  
  144.         /* Chain and pass args. */
  145.         execv(msgarray[0],msgv);
  146.         strcpy(current,"  / :");
  147.         user = bdos(0x20,0xff);
  148.         current[3] = bdos(0x19,0) + 'A' ;
  149.         current[0] = (user > 9 ? '1' : '0');
  150.         current[1] = (user > 9 ? user - 10 + '0' : user + '0');
  151.         if (p = index(msgarray[0],':'))
  152.             strcpy(fname,p+1);
  153.         else if (p = index(msgarray[0],'/'))
  154.             strcpy(fname,p+1);
  155.         else
  156.             strcpy(fname,msgarray[0]);
  157.         for (i = 0; i < NUMPATH; i++){
  158.             if (strcmp(current,path[i])){
  159.                 strcpy(msgarray[0],path[i]);
  160.                 strcat(msgarray[0],fname);
  161.                 execv(msgarray[0],msgv);
  162.             }
  163.         }
  164.         fprintf(stderr,"\nFile %s not found for piping.\n", fname);
  165.         exit(1);
  166.     }
  167. }
  168.