home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / GUSI / GUSIRemoteConsole.cp < prev    next >
Encoding:
Text File  |  1993-11-08  |  2.7 KB  |  111 lines  |  [TEXT/MPS ]

  1. /*********************************************************************
  2. Project    :    GUSI                        -    Grand Unified Socket Interface
  3. File        :    GUSIRemoteConsole.cp    -    Divert standard I/O to other 
  4.                                                 application.
  5. Author    :    Matthias Neeracher
  6. Started    :    07Nov93                                Language    :    MPW C++
  7. Last        :    07Nov93
  8. *********************************************************************/
  9.  
  10. #pragma force_active on
  11.  
  12. #include "GUSIPPC_P.h"
  13.  
  14. #include <SysEqu.h>
  15. #include <Script.h>
  16. #include <TextUtils.h>
  17. #include <PLStringFuncs.h>
  18.  
  19. class GUSIRemoteConsoleDomain : public DeviceSocketDomain {
  20.     static Socket *    console;
  21. public:
  22.     GUSIRemoteConsoleDomain()    :    DeviceSocketDomain(AF_REMOTECON)    {    }
  23.     
  24.     virtual     Socket * open(const char * filename, int oflag);
  25. };
  26.  
  27. GUSIRemoteConsoleDomain    GUSIRemoteConsoleSockets;
  28.  
  29. Socket * GUSIRemoteConsoleDomain::console    =    (Socket *) nil;
  30.  
  31. static sa_constr_ppc    RemoteConConstr    =    {
  32.     PPC_CON_NEWSTYLE+PPC_CON_MATCH_TYPE,
  33.     "\p",
  34.     {
  35.         smRoman,
  36.         "\p",
  37.         ppcByString,
  38.         "\pRemote Console"
  39.     }
  40. };
  41.  
  42. Socket * GUSIRemoteConsoleDomain::open(const char * filename, int flags)
  43. {
  44.     char                 title[256];
  45.     
  46.     strncpy(title, filename, 7);
  47.     title[7] = 0;
  48.     
  49.     if (equalstring(title, (char *) "stdin", false, true)) {
  50.         flags = O_RDONLY;
  51.         
  52.         if (filename[5])
  53.             return (Socket *) TryNextDevice;
  54.         else
  55.             filename += 5;
  56.     } else if (equalstring(title, (char *) "stdout", false, true)) {
  57.         flags = O_WRONLY;
  58.         
  59.         if (filename[6])
  60.             return (Socket *) TryNextDevice;
  61.         else
  62.             filename += 6;
  63.     } else if (equalstring(title, (char *) "console", false, true)) {
  64.         if (filename[7])
  65.             return (Socket *) TryNextDevice;
  66.         else
  67.             filename += 7;
  68.     } else
  69.         return (Socket *) TryNextDevice;
  70.  
  71.     if (!console) {
  72.         if (!(console = PPCSockets.socket(SOCK_STREAM, 0)))
  73.             goto lose;
  74.         
  75.         sockaddr_ppc    addr;
  76.         int                len = sizeof(sockaddr_ppc);
  77.     
  78.         addr.family                                    =    AF_PPC;
  79.         addr.port.nameScript                        =    smRoman;
  80.         addr.port.portKindSelector                =    ppcByString;
  81.         addr.location.locationKindSelector    =    ppcNBPTypeLocation;
  82.         
  83.         PLstrcpy(addr.port.name, (StringPtr) CurApName);
  84.         PLstrcpy(addr.location.u.nbpType, "\pPPCToolBox");
  85.         PLstrcpy(addr.port.u.portTypeStr, "\pRemote Console Client");
  86.         
  87.         if (console->bind((struct sockaddr *) &addr, sizeof(sockaddr_ppc))) {
  88.             if (errno != EADDRINUSE)
  89.                 goto lose;
  90.                 
  91.             strcpy((char *) addr.port.name+addr.port.name[0]+1, " - 2");
  92.             addr.port.name[0] += 4;
  93.             
  94.             while (console->bind((struct sockaddr *) &addr, sizeof(sockaddr_ppc))) {
  95.                 if (errno != EADDRINUSE || addr.port.name[addr.port.name[0]] == '9')
  96.                     goto lose;
  97.                 ++addr.port.name[addr.port.name[0]];
  98.             }
  99.         }
  100.         if (PPCSockets.choose(0, "Please choose a console to connect to", &RemoteConConstr, 0, &addr, &len))
  101.             goto lose;
  102.         if (console->connect(&addr, len))
  103.             goto lose;
  104.     }
  105.     
  106.     return console;
  107.  
  108. lose:
  109.     exit(1);
  110. }
  111.