
/*
 *  MAIN.C
 *
 *  (c)Copyright 1990, Matthew Dillon, All Rights Reserved
 *  This code is freely distributable
 *
 *  DCMD <file>
 *  DCMD SYNC
 */

#include "defs.h"

main(ac, av)
char *av[];
{
    Process *proc = (Process *)FindTask(NULL);
    XMsgPort *port;
    char buf[64];
    long t;

    time(&t);

    sprintf(buf, "DCMD%08lX.PORT", proc);

    fclose(stderr);     /*  MUST close error channel!   */

    port = FindPort(buf);

    if (ac == 1) {
	if (port == NULL) {
	    printf("DCMD (c)Copyright 1990, Matthew Dillon, All Rights Reserved\n");
	    printf("V1.00 %s, Free Distributable\n", __DATE__);
	    puts("DCMD monitors all CLI activity and appends it to the specified file");
	    puts("");
	    puts("DCMD filename     - start cmding");
	    puts("DCMD              - end cmding");
	    puts("DCMD sync         - sync file");
	    puts("\nWARNING: do not end a dcmd unless all programs run after\n"
		 "the initial DCMD have exited!!!!");
	    exit(1);
	}
	if (port->Port.mp_SigTask) {
	    Task *task = port->Port.mp_SigTask;

	    port->Port.mp_SigTask = FindTask(NULL);
	    Signal(task, SIGBREAKF_CTRL_F);         /*  signal end          */
	    while (port->Port.mp_SigTask)           /*  wait for handshake  */
		Wait(SIGBREAKF_CTRL_E);
	    Delay(50);
	    RemPort(port);
	    if (port->FileName);
		FreeMem(port->FileName, strlen(port->FileName) + 1);
	    if (port->Segment);
		UnLoadSeg(port->Segment);
	    FreeMem(port, sizeof(XMsgPort) + 64);

	    puts("DCMD ended");
	} else {
	    puts("PORT error!");
	}
	exit(0);
    }
    if (strcmp(av[1], "sync") == 0 || strcmp(av[1], "SYNC") == 0) {
	if (port = FindPort(buf)) {
	    Signal(port->Port.mp_SigTask, SIGBREAKF_CTRL_D);
	    puts("File Synced");
	} else {
	    puts("Error: DCMD not running");
	}
	exit(0);
    }
    if (port) {
	printf("A DCMD to %s is already running!\n", port->FileName);
	exit(1);
    }

    /*
     *	start new one
     */

    port = AllocMem(sizeof(XMsgPort) + 64, MEMF_CLEAR|MEMF_PUBLIC);
    port->Port.mp_Node.ln_Name = (char *)(port + 1);
    strcpy(port->Port.mp_Node.ln_Name, buf);

    port->FileName = AllocMem(strlen(av[1]) + 1, MEMF_PUBLIC);
    strcpy(port->FileName, av[1]);

    /*
     *	Start up dcmd-handler
     */

    port->Segment = LoadSeg("dcmd-handler");
    if (port->Segment == NULL)
	port->Segment = LoadSeg("l:dcmd-handler");

    if (port->Segment == NULL) {
	FreeMem(port->FileName, strlen(port->FileName) + 1);
	FreeMem(port, sizeof(XMsgPort) + 64);

	puts("Unable to find [l:]dcmd-handler");
	exit(1);
    }

    port->Proc = proc;

    /*
     *	initialize public port and add, then create process and exit
     */

    port->Port.mp_Node.ln_Type = NT_MSGPORT;
    port->Port.mp_Node.ln_Pri  = 0;
    port->Port.mp_Flags = PA_IGNORE;
    AddPort(port);

    port->OldConsoleTask = proc->pr_ConsoleTask;
    proc->pr_ConsoleTask = port;

    {
	FileHandle *fh;
	CLI *cli = BTOC(port->Proc->pr_CLI);

	if ((fh = BTOC(cli->cli_CurrentOutput)) && fh->fh_Type == port->OldConsoleTask)
	    fh->fh_Type = port;
	if ((fh = BTOC(cli->cli_CurrentInput)) && fh->fh_Type == port->OldConsoleTask)
	    fh->fh_Type = port;
    }

    CreateProc(port->Port.mp_Node.ln_Name, 0, port->Segment, 4096);
    printf("BEGIN %s %s", buf, ctime(&t));
    return(0);
}

