home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / wfdos.zip / WFDOS-C.C < prev    next >
Text File  |  1994-09-04  |  7KB  |  172 lines

  1. /*┌─ WFDOS-C.C ──────────────────────────────────────────────────────────────────┐*/
  2. /*│ WFDOS - Invoke DOS program in background mode from IBM WorkFrame/2.          │*/
  3. /*│                                                                              │*/
  4. /*│ Copyright:                                                                   │*/
  5. /*│ ▀▀▀▀▀▀▀▀▀▀                                                                   │*/
  6. /*│ (c) Copyright Per Jessen, 1994.                                              │*/
  7. /*│ Permission is granted to any individual or institution to use, copy, or      │*/
  8. /*│ redistribute this software so long as all of the original files are included │*/
  9. /*│ unmodified, that it is not sold for profit, and that this copyright notice   │*/
  10. /*│ is retained.                                                                 │*/
  11. /*│                                                                              │*/
  12. /*│ Disclaimer:                                                                  │*/
  13. /*│ ▀▀▀▀▀▀▀▀▀▀▀                                                                  │*/
  14. /*│ The code is provided on an "AS IS" basis without any warranty of any kind.   │*/
  15. /*│ The author shall not be liable for any damages arising out of your use of    │*/
  16. /*│ this code, even if he has been advised of the possibility of such damages.   │*/
  17. /*└──────────────────────────────────────────────────────────────────────────────┘*/
  18. /*┌──────────────────────────────────────────────────────────────────────────────┐*/
  19. /*│ The purpose of this package is to re-direct the output from a background     │*/
  20. /*│ DOS-session from the screen to the Monitor Window in the IBM Workframe/2.    │*/
  21. /*│ This enables a transparent integration of DOS-tools, such as language pro-   │*/
  22. /*│ cessors etc., in the Workframe/2.                                            │*/
  23. /*│ Briefly, the Server (WFDOS-S.EXE) sets up a named pipe, and passes it on to  │*/
  24. /*│ the Client (WFDOS-C.EXE), which is then started in the DOS-environment.      │*/
  25. /*│ When the pipe is established, the Server provides the Client with a program- │*/
  26. /*│ name and a list of arguments. The Client will then redirect the normal std-  │*/
  27. /*│ out and stderr streams to the pipe, before transferring control to the pro-  │*/
  28. /*│ gram to be executed. Any output from this program is now effectively routed  │*/
  29. /*│ to the Server, which will display it in the Monitor Window.                  │*/
  30. /*│                                                                              │*/
  31. /*│ Please refer to WFDOS-S.C for a description of arguments etc.                │*/
  32. /*│                                                                              │*/
  33. /*│            ───────────────────────  o  ───────────────────────               │*/
  34. /*│                                                                              │*/
  35. /*│ History:                                                                     │*/
  36. /*│ ───────────────────────────────────────────────────────────────────────────  │*/
  37. /*│ 94.05.31 - Version 2.02 released.                                            │*/
  38. /*│ 94.07.11 - Version 2.03 - no modifications.                                  │*/
  39. /*│            Note: This version was not released to the public domain.         │*/
  40. /*│ 94.07.31 - Version 2.04 - only minor modifications. See WFDOS-S.             │*/
  41. /*│ 94.09.03 - Version 2.05 - minor bugfix in WFDOS-S.                           │*/
  42. /*└──────────────────────────────────────────────────────────────────────────────┘*/
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include <stdlib.h>
  46. #include <process.h>
  47. #include <conio.h>
  48. #include <io.h>
  49. #include <dir.h>
  50. #include <dos.h>
  51. #include <fcntl.h>
  52. #include <sys\stat.h>
  53. #include "wfdos.h"
  54.  
  55. static char message[BFR_SIZE];
  56.  
  57. int cdecl main ( int argc, char *argv[] )
  58. {
  59.    char    *pipeName;
  60.    int     hpipe, hstdout, hstderr;
  61.    char    *args[MAX_ARG];
  62.    int     arg, rc, msglevel, i;
  63.    char    *logo = CLIENT_LOGO;
  64.  
  65.    /* Check that we're being called by WFDOS-S */
  66.    if ( argc<2 || strcmp( argv[1], SERVER_IDENT )!=0 )
  67.    {
  68.       printf("%s"
  69.              "The Client-process can only be started by the "
  70.              "Server-process.", logo);
  71.       return -1;
  72.    }
  73.  
  74.    /* Before we start doing anything else, we have to redirect  */
  75.    /* stdout and stderr to the pipe                             */
  76.  
  77.    /* Get the pipename supplied as the last argument            */
  78.    pipeName=argv[argc-1];
  79.  
  80.    /* Open the pipe for reading and writing text - do 5 retries */
  81.    i=0;
  82.    while ( (hpipe=open( pipeName, O_RDWR|O_TEXT, S_IWRITE|S_IREAD ))<0 && ++i<5 ) delay(250);
  83.  
  84.    if ( hpipe<0 )
  85.    {
  86.       DosBeep(500,100); DosBeep(1000,100);
  87.       printf("\n%s"
  88.              "Error opening pipe %s - %s"
  89.              "\n\nPress any key to exit ...",
  90.              logo, pipeName, sys_errlist[errno] );
  91.       getch();
  92.       return -1;
  93.    }
  94.  
  95.    /* Read the message (arguments list) */
  96.    read( hpipe, message, BFR_SIZE );
  97.  
  98.    /* Save a copy of handle for STDOUT */
  99.    hstdout=dup( STDOUT );
  100.    hstderr=dup( STDERR );
  101.  
  102.    /* Redirect stdout to our pipe by copying the pipe-handle to stdout */
  103.    dup2( hpipe, STDOUT );
  104.    dup2( hpipe, STDERR );
  105.  
  106.    /* Close the pipe */
  107.    close( hpipe );
  108.  
  109.    arg=2;
  110.    while ( *argv[arg]=='/' && arg<argc )
  111.    {
  112.       switch ( *(argv[arg]+1) ) {
  113.       case 'm':
  114.       case 'M':
  115.          sscanf( argv[arg]+2, "%d", &msglevel );
  116.          break;
  117.       default:
  118.          printf( "%s"
  119.                  "Client: Invalid argument %s ignored.\n", logo, argv[arg] );
  120.          logo="";
  121.          break;
  122.       } /* endswitch */
  123.       arg++;
  124.    } /* endwhile */
  125.  
  126.    if ( msglevel>0 ) 
  127.    {
  128.       printf("%s", logo );
  129.       logo="";
  130.    }
  131.  
  132.    /* Tokenize the arg-list - blanks separate tokens */
  133.    args[0]=strtok( message, " " );
  134.    arg=1;
  135.    while ( (args[arg]=strtok( NULL, " " ))!=NULL && arg<MAX_ARG ) arg++;
  136.  
  137.    if ( arg==MAX_ARG )
  138.    printf("%s"
  139.           "Client: Number of arguments > %d - extraneous arguments ignored.\n", 
  140.           logo, MAX_ARG);
  141.  
  142.    if ( msglevel>=MLVL_CRUNTIME )
  143.    {
  144.       printf("Client: Calling %s ...\n"
  145.              "Client: with argument(s): ", searchpath(args[0]) );
  146.       for ( i=1; i<arg; i++) printf("%s ", args[i] );
  147.       putchar('\n');
  148.    }
  149.  
  150.    /* NOTE: spawnvp is used instead of execvp to enable the Client to */
  151.    /* retrieve the returncode and pass it on to the Server.           */
  152.    rc=spawnvp( P_WAIT, args[0], args );
  153.  
  154.    /* NOTE: As a negative rc indicates a spawn.. processing error   */
  155.    /* any negative rc's returned by the called program will be      */
  156.    /* turned into positive by overlaying the leftmost byte with 00. */
  157.    if ( rc<0 )
  158.       printf("%s"
  159.              "Client: %s: %s\n", logo, args[0], sys_errlist[errno] );
  160.  
  161.    /* Pipe the return-code to the server */
  162.    printf(CLIENTRC_IDENT"%d", rc);
  163.  
  164.    dup2( hstdout, STDOUT );
  165.    dup2( hstderr, STDERR );
  166.  
  167.    close( hstdout );
  168.    close( hstderr );
  169.  
  170.    return 0;
  171. }
  172.