home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2 / Openstep-4.2-Intel-User.iso / NextLibrary / Documentation / NextAdmin / ReleaseNotes / Terminal.rtfd / terminal.m < prev    next >
Encoding:
Text File  |  1994-07-25  |  3.3 KB  |  165 lines

  1. // terminal.m
  2. // this program opens a terminal window in the current directory
  3. // author: sam streeper
  4.  
  5. /*
  6. **    terminal [-shell] [-dir <dir>] [-noclose] [-env] [command]
  7. */
  8.  
  9. #import <appkit/appkit.h>
  10. #import <mach/mach.h>
  11. #import <mach/mach_error.h>
  12. #import <mach/task_special_ports.h>
  13. #import <servers/bootstrap.h>
  14. #import <machkit/NXPort.h>
  15. // #import "/NextDeveloper/Headers/apps/Terminal.h"
  16. #import "TerminalDOProtocol.h"
  17. #import <sys/param.h>
  18. #import <string.h>
  19.  
  20. #define BLOCKSIZE (2048)
  21. #define streq !strcmp
  22.  
  23. char pathArray[MAXPATHLEN];
  24. char command[2048];
  25.  
  26. usage()
  27. {
  28.     fprintf(stderr,"terminal: open a new Terminal.app window\n");
  29.     fprintf(stderr,"usage: terminal [-shell] [-dir <dir>] [-noclose] [-env] [command]\n");
  30.     fprintf(stderr,"    -shell   - run command from default shell\n");
  31.     fprintf(stderr,"    -dir     - specify working directory\n");
  32.     fprintf(stderr,"    -noclose - retain window upon exit\n");
  33.     fprintf(stderr,"    -env     - export current environment\n");
  34.     exit(-1);
  35. }
  36.  
  37. main(int argc, char *argv[])
  38. {
  39.     int i, ret, firstCommand = 1;
  40.     NXData *myEnv=nil;
  41.     id <TSTerminalDOServices> terminal;
  42.     port_t    sport;
  43.     int err = bootstrap_look_up(bootstrap_port, "TerminalDO", &sport);
  44.     NXPort *terminalPort;
  45.     char *cp;
  46.     char *path = NULL;
  47.     BOOL useShell = NO;
  48.     BOOL close = YES;
  49.     BOOL exportEnv = NO;
  50.  
  51.     if (err != KERN_SUCCESS)
  52.     {
  53.         // should really ask workspace to open terminal
  54.         // and try again...
  55.         fprintf(stderr,"Can't connect to Terminal\n");
  56.         exit(-1);
  57.     }
  58.  
  59.     terminalPort = [NXPort newFromMachPort:sport];
  60.     terminal = [NXConnection connectToPort:terminalPort];
  61.     if (!terminal)
  62.     {
  63.         fprintf(stderr,"Can't connect to Terminal\n");
  64.         exit(-2);
  65.     }
  66.  
  67.     if ([terminal protocolVersion] < 0x10002)
  68.     {
  69.         fprintf(stderr,"Incompatible terminal protocol version\n");
  70.         exit(-4);
  71.     }
  72.  
  73.     if (argc == 1)
  74.     {
  75.         useShell = YES;
  76.     }
  77.  
  78.     while ((firstCommand < argc) && (*argv[firstCommand]=='-'))
  79.     {
  80.         if (streq("-shell", argv[firstCommand]))
  81.         {
  82.             useShell = YES;
  83.         }
  84.         else if (streq("-dir", argv[firstCommand]))
  85.         {
  86.             if (++firstCommand >= argc) usage();
  87.             path = argv[firstCommand];
  88.         }
  89.         else if ((streq("-noclose", argv[firstCommand])))
  90.         {
  91.             close = NO;
  92.         }
  93.         else if ((streq("-env", argv[firstCommand])))
  94.         {
  95.             exportEnv = YES;
  96.         }
  97.         else
  98.         {
  99.             usage();
  100.         }
  101.         firstCommand++;
  102.     }
  103.  
  104.     if (!path)
  105.     {
  106.         path = pathArray;
  107.         if(!getwd(path))
  108.         {
  109.             fprintf(stderr,"Can't get current directory\n");
  110.             exit(-3);
  111.         }
  112.     }
  113.  
  114.     // build command from args
  115.     command[0] = '\0';
  116.     for (i=firstCommand; i<argc; i++)
  117.     {
  118.         strcat(command, argv[i]);
  119.         strcat(command, " ");
  120.     }
  121.  
  122.     if (exportEnv)
  123.     {
  124.         //build passed env
  125.         ret = 1;
  126.         for(i=0; environ[i] != NULL; i++)
  127.         {
  128.             ret = ret + strlen(environ[i]) + 1;
  129.         }
  130.  
  131.         ret = ((ret / BLOCKSIZE) + 1) * BLOCKSIZE;
  132.  
  133.         myEnv = [[NXData alloc] initWithSize:ret];
  134.  
  135.         if (myEnv)
  136.         {
  137.             cp = [myEnv data];
  138.  
  139.             for(i=0; environ[i] != NULL; i++)
  140.             {
  141.                 strcpy(cp,environ[i]);
  142.                 while (*cp++);
  143.             }
  144.             strcpy(cp,"");
  145.         }
  146.         else exportEnv = NO;
  147.     }
  148.  
  149.     [terminal runCommand: command[0] ? command : NULL
  150.         inputData: NULL
  151.         outputData: NULL
  152.         errorData: NULL
  153.         waitForReturn: NO
  154.         windowType: TSWindowNew
  155.         windowHandle: NULL
  156.         exitAction: close ? TSCloseUnlessError : TSDontCloseOnExit
  157.         shellType: useShell ? TSShellDefault : TSShellNone
  158.         windowTitle: command[0] ? command : NULL
  159.         directory: path
  160.         environment: exportEnv ? myEnv : NULL
  161.         returnCode: &ret];
  162.  
  163.     return ret;
  164. }
  165.