home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / dirs / dcmd_408.lzh / DCmd / dcmd.c < prev    next >
C/C++ Source or Header  |  1990-11-21  |  3KB  |  134 lines

  1.  
  2. /*
  3.  *  MAIN.C
  4.  *
  5.  *  (c)Copyright 1990, Matthew Dillon, All Rights Reserved
  6.  *  This code is freely distributable
  7.  *
  8.  *  DCMD <file>
  9.  *  DCMD SYNC
  10.  */
  11.  
  12. #include "defs.h"
  13.  
  14. main(ac, av)
  15. char *av[];
  16. {
  17.     Process *proc = (Process *)FindTask(NULL);
  18.     XMsgPort *port;
  19.     char buf[64];
  20.     long t;
  21.  
  22.     time(&t);
  23.  
  24.     sprintf(buf, "DCMD%08lX.PORT", proc);
  25.  
  26.     fclose(stderr);     /*  MUST close error channel!   */
  27.  
  28.     port = FindPort(buf);
  29.  
  30.     if (ac == 1) {
  31.     if (port == NULL) {
  32.         printf("DCMD (c)Copyright 1990, Matthew Dillon, All Rights Reserved\n");
  33.         printf("V1.00 %s, Free Distributable\n", __DATE__);
  34.         puts("DCMD monitors all CLI activity and appends it to the specified file");
  35.         puts("");
  36.         puts("DCMD filename     - start cmding");
  37.         puts("DCMD              - end cmding");
  38.         puts("DCMD sync         - sync file");
  39.         puts("\nWARNING: do not end a dcmd unless all programs run after\n"
  40.          "the initial DCMD have exited!!!!");
  41.         exit(1);
  42.     }
  43.     if (port->Port.mp_SigTask) {
  44.         Task *task = port->Port.mp_SigTask;
  45.  
  46.         port->Port.mp_SigTask = FindTask(NULL);
  47.         Signal(task, SIGBREAKF_CTRL_F);         /*  signal end          */
  48.         while (port->Port.mp_SigTask)           /*  wait for handshake  */
  49.         Wait(SIGBREAKF_CTRL_E);
  50.         Delay(50);
  51.         RemPort(port);
  52.         if (port->FileName);
  53.         FreeMem(port->FileName, strlen(port->FileName) + 1);
  54.         if (port->Segment);
  55.         UnLoadSeg(port->Segment);
  56.         FreeMem(port, sizeof(XMsgPort) + 64);
  57.  
  58.         puts("DCMD ended");
  59.     } else {
  60.         puts("PORT error!");
  61.     }
  62.     exit(0);
  63.     }
  64.     if (strcmp(av[1], "sync") == 0 || strcmp(av[1], "SYNC") == 0) {
  65.     if (port = FindPort(buf)) {
  66.         Signal(port->Port.mp_SigTask, SIGBREAKF_CTRL_D);
  67.         puts("File Synced");
  68.     } else {
  69.         puts("Error: DCMD not running");
  70.     }
  71.     exit(0);
  72.     }
  73.     if (port) {
  74.     printf("A DCMD to %s is already running!\n", port->FileName);
  75.     exit(1);
  76.     }
  77.  
  78.     /*
  79.      *    start new one
  80.      */
  81.  
  82.     port = AllocMem(sizeof(XMsgPort) + 64, MEMF_CLEAR|MEMF_PUBLIC);
  83.     port->Port.mp_Node.ln_Name = (char *)(port + 1);
  84.     strcpy(port->Port.mp_Node.ln_Name, buf);
  85.  
  86.     port->FileName = AllocMem(strlen(av[1]) + 1, MEMF_PUBLIC);
  87.     strcpy(port->FileName, av[1]);
  88.  
  89.     /*
  90.      *    Start up dcmd-handler
  91.      */
  92.  
  93.     port->Segment = LoadSeg("dcmd-handler");
  94.     if (port->Segment == NULL)
  95.     port->Segment = LoadSeg("l:dcmd-handler");
  96.  
  97.     if (port->Segment == NULL) {
  98.     FreeMem(port->FileName, strlen(port->FileName) + 1);
  99.     FreeMem(port, sizeof(XMsgPort) + 64);
  100.  
  101.     puts("Unable to find [l:]dcmd-handler");
  102.     exit(1);
  103.     }
  104.  
  105.     port->Proc = proc;
  106.  
  107.     /*
  108.      *    initialize public port and add, then create process and exit
  109.      */
  110.  
  111.     port->Port.mp_Node.ln_Type = NT_MSGPORT;
  112.     port->Port.mp_Node.ln_Pri  = 0;
  113.     port->Port.mp_Flags = PA_IGNORE;
  114.     AddPort(port);
  115.  
  116.     port->OldConsoleTask = proc->pr_ConsoleTask;
  117.     proc->pr_ConsoleTask = port;
  118.  
  119.     {
  120.     FileHandle *fh;
  121.     CLI *cli = BTOC(port->Proc->pr_CLI);
  122.  
  123.     if ((fh = BTOC(cli->cli_CurrentOutput)) && fh->fh_Type == port->OldConsoleTask)
  124.         fh->fh_Type = port;
  125.     if ((fh = BTOC(cli->cli_CurrentInput)) && fh->fh_Type == port->OldConsoleTask)
  126.         fh->fh_Type = port;
  127.     }
  128.  
  129.     CreateProc(port->Port.mp_Node.ln_Name, 0, port->Segment, 4096);
  130.     printf("BEGIN %s %s", buf, ctime(&t));
  131.     return(0);
  132. }
  133.  
  134.