home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / OP2DEV.ZIP / PREDI.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-31  |  3.6 KB  |  172 lines

  1. #define INCL_DOS
  2. #include <os2.h>
  3.  
  4. #include <stdio.h>
  5. #include <process.h>
  6.  
  7. #include "bbsexpan.h"
  8.  
  9.  
  10. #define PIPESIZE   128
  11. #define LENGTH     30
  12. #define FLAGS      1
  13. #define ARGS       0L
  14. #define ENVS       0L
  15. #define STDIN      0
  16. #define STDOUT     1
  17. #define STSZ       4096
  18. #define BUFFSIZE   512
  19.  
  20. USHORT ReadHandle, WriteHandle, NewStdout, childID;
  21. USHORT NewStdin, Read2, Write2;
  22.  
  23.  
  24. unsigned long BuffAdd=0;           // semaphore for adding to buffer
  25. char threadstk[STSZ], theadstk2[STSZ];
  26. void ReadPipe(void far *);
  27. void WritePipe(void far *);
  28.  
  29.  
  30. main(int argc, char *argv[])
  31. {
  32.    char tbuf[BUFFSIZE+2], *p;
  33.    char fbuf[LENGTH];
  34.    char ar[128];
  35.    struct _RESULTCODES ExResult, CwResult;
  36.    USHORT HandState, procID;
  37.    USHORT Strlen, Written;
  38.    int j, c;
  39.    short arg;
  40.    short lcountsave;
  41.    short inputflag=0;
  42.    unsigned rc, rc2;
  43.    FILE *err;
  44.  
  45.    arg = 2;
  46.    while( argv[arg][0] == '-' ) {
  47.       switch(argv[arg][1]) {
  48.          case 'i':
  49.             inputflag = 1;
  50.             break;
  51.       }
  52.       arg++;
  53.    }
  54.  
  55.    rc = UseChildAppInit(argv[1],argv[0],&anchor,&unhand,&semhand,&usernum,&instance);
  56.    if( rc != 0 ) {
  57.       printf("\nCould not initialize application (%d)\n",rc);
  58.       goto END_APP;
  59.    }
  60.  
  61.    rc = DosMakePipe(&ReadHandle, &WriteHandle, PIPESIZE);
  62.    if( rc )
  63.       goto END_APP;
  64.  
  65.    rc = DosMakePipe(&Read2, &Write2, 1);
  66.    if( rc ) 
  67.       goto END_APP;
  68.  
  69.    DosClose(STDIN);
  70.    DosClose(STDOUT);
  71.    NewStdout = STDOUT;
  72.    NewStdin = STDIN;
  73.  
  74.    DosDupHandle(WriteHandle,&NewStdout);
  75.    DosDupHandle(Read2,&NewStdin);
  76.  
  77.    DosQFHandState(Write2,&HandState);
  78.    HandState &= 0x7F88;
  79.    HandState |= 0x0080;
  80.    DosSetFHandState(Write2,HandState);
  81.  
  82.    DosQFHandState(ReadHandle,&HandState);
  83.    HandState &= 0x7F88;
  84.    HandState |= 0x0080;
  85.    DosSetFHandState(ReadHandle,HandState);
  86.  
  87.    COLOR(BRCYAN);
  88.  
  89.    if( CMDWAIT )
  90.       SerGetStr(ar,127,unhand);
  91.    else
  92.       ar[0] = '\0';
  93.    lcountsave = unhand->lcounton;
  94.    if( rc=DosExecPgm(fbuf,LENGTH,FLAGS,ar,ENVS,&ExResult,argv[arg]) ) {
  95.       printf("\nError in DosExecPgm: %u %s\n",rc,fbuf);
  96.       goto END_APP;
  97.    }
  98.  
  99.    rc = _beginthread(ReadPipe,(void far *)threadstk,STSZ, NULL);
  100.    if( rc == -1 ) {
  101.       printf("\nError: Could not begin thread\n");
  102.       goto END_APP;
  103.    }
  104.  
  105.    if( inputflag ) {
  106.       unhand->lcounton = 0;
  107.       rc2 = _beginthread(WritePipe,(void far *)theadstk2,STSZ,NULL);
  108.       if( rc == -1 ) {
  109.          printf("\nError: Could not begin thread\n");
  110.          goto END_APP;
  111.       }
  112.    }
  113.  
  114.    DosSleep(1000L);
  115.  
  116.    childID = ExResult.codeTerminate;
  117.    DosCwait(1,0,&CwResult,&procID,childID);
  118.  
  119.    DosClose(Read2);
  120.    DosClose(Write2);
  121.    DosClose(ReadHandle);
  122.    DosClose(WriteHandle);
  123.  
  124.    ///////////////////////////////////// Need this to clean up
  125. END_APP:
  126.    unhand->lcounton = lcountsave;
  127.    DosExit(1,1);
  128. }
  129.  
  130.  
  131. void WritePipe(void far *arg)
  132. {
  133.    short c, ch;
  134.    USHORT written;
  135.  
  136.    while(1) {
  137.       c = SerReadc(unhand);
  138.       ch = c;
  139.       DosWrite(Write2,&ch,1,&written);
  140.       if( ch == 13 ) {
  141.          ch = 10;
  142.          DosWrite(Write2,&ch,1,&written);
  143.       }
  144.    }
  145.    return;
  146. }
  147.  
  148.  
  149. void ReadPipe(void far *arg)
  150. {
  151.    USHORT Read, done = 0;
  152.    unsigned char   st[PIPESIZE+10];
  153.  
  154.    while(!done) {
  155.       DosRead(ReadHandle,st,PIPESIZE,&Read);
  156.       DosSemRequest(&BuffAdd,-1L);
  157.       if( Read ) {
  158.          st[Read] = '\0';
  159.          if( !SerWrite(st,unhand) ) {
  160.             DosKillProcess(0,childID);
  161.             SerWrite("\n",unhand);
  162.             done = 1;
  163.          }
  164.       }
  165.       DosSemClear(&BuffAdd);
  166.    }
  167.    return;
  168. }
  169.  
  170.  
  171.  
  172.