home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.3 Development Libraries / SGI IRIX 6.3 Development Libraries.iso / dist6.3 / ViewKit_dev.idb / usr / include / Vk / VkSubProcess.h.z / VkSubProcess.h
Encoding:
C/C++ Source or Header  |  1996-09-20  |  5.4 KB  |  189 lines

  1. ////////////////////////////////////////////////////////////////////////////////
  2. ///////   Copyright 1992, Silicon Graphics, Inc.  All Rights Reserved.   ///////
  3. //                                                                            //
  4. // This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;     //
  5. // the contents of this file may not be disclosed to third parties, copied    //
  6. // or duplicated in any form, in whole or in part, without the prior written  //
  7. // permission of Silicon Graphics, Inc.                                       //
  8. //                                                                            //
  9. // RESTRICTED RIGHTS LEGEND:                                                  //
  10. // Use,duplication or disclosure by the Government is subject to restrictions //
  11. // as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data     //
  12. // and Computer Software clause at DFARS 252.227-7013, and/or in similar or   //
  13. // successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -    //
  14. // rights reserved under the Copyright Laws of the United States.             //
  15. //                                                                            //
  16. ////////////////////////////////////////////////////////////////////////////////
  17. #ifndef _VKPROCESS_H
  18. #define _VKPROCESS_H
  19.  
  20. #include <Xm/Xm.h>
  21. #include <signal.h>
  22. #include <Vk/VkCallbackObject.h>
  23.  
  24. // These classes implement the necessary logic for building
  25. // reference counted objects and the associated pointers.
  26.  
  27. class VkSPCounted : public VkCallbackObject {
  28.     friend class VkSPRPtr_base;
  29.     int nreferences;    // number of references to this object
  30.     void addRef()
  31.         { nreferences++; }
  32.     void delRef()
  33.         {
  34.             if (--nreferences <= 0)
  35.               delete this;
  36.         }
  37.   public:
  38.     VkSPCounted() : VkCallbackObject()
  39.         { nreferences = 0; }
  40.     virtual ~VkSPCounted();
  41. } ;
  42.  
  43. class VkSPRPtr_base {
  44.   public:
  45.     operator void*()
  46.     { return (void *)ptr; }
  47.     VkSPRPtr_base& operator=(VkSPCounted *tp)
  48.         { if (ptr) ptr->delRef();
  49.           ptr = tp;
  50.           if (ptr) ptr->addRef();
  51.           return *this; 
  52.         }
  53.     VkSPRPtr_base& operator=(const VkSPRPtr_base& r)
  54.         { if (ptr) ptr->delRef();
  55.           ptr = r.ptr;
  56.           if (ptr) ptr->addRef();
  57.           return *this; 
  58.         }
  59.  
  60.  
  61.   protected:
  62.     VkSPCounted *ptr;
  63.     VkSPRPtr_base() { ptr = 0; }
  64.     VkSPRPtr_base(const VkSPRPtr_base& r)
  65.     {
  66.     ptr = r.ptr;
  67.     if (ptr) ptr->addRef();
  68.     }
  69.     VkSPRPtr_base(VkSPCounted *tp)
  70.         { ptr = tp;
  71.           if (ptr) ptr->addRef();
  72.         }
  73.     ~VkSPRPtr_base()
  74.         { if (ptr)  ptr->delRef(); }
  75. } ;
  76.  
  77. class    VkSubProcessRep;
  78.  
  79. class RPtr_VkSubProcessRep : public VkSPRPtr_base {    
  80.   public:    
  81.     RPtr_VkSubProcessRep();    
  82.     RPtr_VkSubProcessRep(VkSubProcessRep *tp);    
  83.     RPtr_VkSubProcessRep(const RPtr_VkSubProcessRep& that) : VkSPRPtr_base(that) {} 
  84.     ~RPtr_VkSubProcessRep();    
  85.     RPtr_VkSubProcessRep& operator=(VkSubProcessRep *tp)    
  86.         { *((VkSPRPtr_base *) this) = (VkSPCounted *) tp;     
  87.           return *this; }       
  88.     
  89.     VkSubProcessRep& operator *();    
  90.     VkSubProcessRep *operator->();    
  91.     int operator !()    
  92.         { return !ptr; }    
  93. };
  94.  
  95.  
  96.  
  97. typedef RPtr_VkSubProcessRep VkSubProcess;
  98.  
  99. class VkSubProcessPtrQueue;
  100.  
  101. class    VkProgram;
  102. class    VkPipe;
  103.  
  104.  
  105. class VkSubProcessRep : public VkSPCounted {
  106.     public:
  107. static    VkSubProcess    create( char*        cmd,
  108.                 int        killChildOnExit,
  109.                 int redirectIn);
  110.  
  111.         static const char *const  stdoutCallback;
  112.         static const char *const  stderrCallback;
  113.         static const char *const  exitCallback;
  114.  
  115.     /* Useful info about running process */
  116.     pid_t        pid()     const        { return _pid; }
  117.     int        exitStatus() const    { return _exitStatus; }
  118.  
  119.     /* send a signal to the process */
  120.     void        signal(int sig);
  121.  
  122.     /* kill off the process */
  123.     void        kill(int sig = SIGTERM);
  124.  
  125.     int        stdinfd() const { return(_stdinfd); } 
  126.  
  127.     /* Let us use VkSubProcess without going to the Xt event loop */
  128.         VkPipe*         getStdoutPipe() { return stdoutPipe; }
  129.         VkPipe*         getStderrPipe() { return stderrPipe; }
  130.  
  131.     /* Actually create VkSubProcess */
  132.     void        run();
  133.  
  134.     protected:
  135.  
  136.         const char **_argv;
  137.  
  138.        void handleExitCallback(VkCallbackObject *, void *, void * callData);
  139.  
  140.     /* Constructor and  destructor */
  141.             VkSubProcessRep(const char*    prog,
  142.                     char **argv,
  143.                     int        killChildOnExit,
  144.                     int         redirectIn);
  145.  
  146.             ~VkSubProcessRep();
  147.  
  148.  
  149.     /* exec() utility function */
  150.     void        exec(const char*    argv[]);
  151.  
  152.     /* I/O file descriptors and pipes */
  153.     int        in, out, err;
  154.     VkPipe*        stdoutPipe;
  155.     VkPipe*        stderrPipe;
  156.     int        _stdinfd;
  157.  
  158.     /* Keep track of running children */
  159. static    int        childCount;
  160.     void        addChild();
  161. static    void        deleteChild(XtPointer, int*, XtInputId*);
  162.     void        processExited(int status);
  163.  
  164.     /* Ignore interrupt/quit while child is running */
  165. static    SIG_PF        interruptHandler, quitHandler;
  166. static    void        ignoreSignals();
  167. static    void        restoreSignals();
  168.  
  169.     /* Handler for asynchronous termination of child */
  170. static    void        childHandler(int, ...);
  171. static    int        signalPipe[2];        // To signal termination
  172.  
  173.     /* Queue to hold list of active Child VkSubProcesses */
  174. static    VkSubProcessPtrQueue    *children;
  175.  
  176.     /* Name of VkProgram being run */
  177.     const char*    progname;
  178.     const int    killChildOnExit;
  179.  
  180.     /* Variables generated internally */
  181. volatile    pid_t    _pid;        // Process ID of child
  182. volatile    int    _exitStatus;    // Exit status of child
  183.  
  184.     /* Static Initialization flag */
  185. static    int        doneStaticInit;
  186. };
  187.  
  188. #endif
  189.