
/*
 * CMD3.C
 *
 *	(C)Copyright 1988 by Matthew Dillon, All Rights Reserved
 *
 *  SETFONT
 *  IGNORECASE
 *  SET
 *  SETENV
 *  UNSET
 *  UNSETENV
 *  CD
 */

#include "defs.h"
#include <local/xmisc.h>
#include <stdio.h>


#define nomemory()  { memoryfail = 1; }

/*
 *  SETFONT font size
 */

void
do_setfont()
{
    register FONT *font = GetFont(av[1], (short)atoi(av[2]));
    register ED *ep = Ep;
    if (font) {
	if (ep->Font)
	    CloseFont(ep->Font);
	ep->Font = font;
	SetFont(ep->Win->RPort, font);
	SetRast(ep->Win->RPort, 0);
	RefreshWindowFrame(ep->Win);
	set_window_params();
	text_redisplay();
    } else {
	title("Unable to find font");
    }
}

void
do_ignorecase()
{
    register ED *ep = Ep;

    if (av[1][0]) {
	switch(av[1][1] & 0x1F) {
	case 'n'&0x1F:
	    ep->IgnoreCase = 1;
	    break;
	case 'f'&0x1F:
	    ep->IgnoreCase = 0;
	    break;
	case 'o'&0x1F:
	    ep->IgnoreCase = 1 - ep->IgnoreCase;
	    break;
	}
	if (ep->IgnoreCase)
	    title("Case InSensitive");
	else
	    title("Case Sensitive");
    }
}

/*
 *  av[1]
 */

void
do_cd()
{
    long oldlock;
    long lock;

    oldlock = CurrentDir(Ep->dirlock);
    if (lock = Lock(av[1], SHARED_LOCK)) {
	UnLock(CurrentDir(oldlock));
	Ep->dirlock = lock;
    } else {
	CurrentDir(oldlock);
	Abortcommand = 1;
	title("Unable to CD");
    }
}

/*
 *  VARIABLE SUPPORT!
 */

#define VARS	struct _VARS
VARS {
    MNODE   Node;
    char    *Name;
    char    *Str;
};

static MLIST SList = { (MNODE *)&SList.mlh_Tail, NULL, (MNODE *)&SList.mlh_Head };

void
do_set()
{
    register VARS *v;
    void do_unset();

    do_unset();
    if (v = malloc(sizeof(VARS))) {
	if (v->Name = malloc(strlen(av[1])+1)) {
	    if (v->Str = malloc(strlen(av[2])+1)) {
		AddHead((LIST *)&SList, (NODE *)v);
		strcpy(v->Name, av[1]);
		strcpy(v->Str , av[2]);
		return;
	    }
	    free(v->Name);
	}
	free(v);
    }
    nomemory();
}

void
do_setenv()
{
    SetDEnv(av[1], av[2]);
}

void
do_unset()
{
    register VARS *v;

    for (v = (VARS *)SList.mlh_Head; v->Node.mln_Succ; v = (VARS *)v->Node.mln_Succ) {
	if (strcmp(v->Name, av[1]) == 0) {
	    Remove((NODE *)v);
	    free(v);
	    free(v->Name);
	    free(v->Str);
	    break;
	}
    }
}

void
do_unsetenv()
{
    register char *ptr = (char *)av[1];
    register char *tmp = malloc(4+strlen(ptr)+1);

    if (tmp) {
	strcpy(tmp, "ENV:");
	strcat(tmp, ptr);
	mountrequest(0);
	DeleteFile(tmp);
	mountrequest(1);
	free(tmp);
    }
}

/*
 *  Search (1) internal list, (2) enviroment, (3) macros.  The variable
 *  is allocated with malloc().  NULL if not found.  ENV: need not exist.
 */

char *
getvar(find)
char *find;
{
    register char *str = NULL;
    {
	register VARS *v;

	for (v = (VARS *)SList.mlh_Head; v->Node.mln_Succ; v = (VARS *)v->Node.mln_Succ) {
	    if (strcmp(v->Name, find) == 0) {
		if (str = malloc(strlen(v->Str)+1)) {
		    strcpy(str, v->Str);
		    return(str);
		}
	    }
	}
    }

    mountrequest(0);
    str = GetDEnv(find);
    mountrequest(1);
    if (str)
	return(str);

    if ((str = keyspectomacro(find)) || (str = menutomacro(find))) {
	register char *ptr = malloc(strlen(str)+1);
	if (ptr) {
	    strcpy(ptr, str);
	    return(ptr);
	}
    }
    return(NULL);
}

void
do_col()
{
    int col = atoi(av[1]) - 1;
    short i;

    if (col > 254 || col < 0) {
	Abortcommand = 1;
	return;
    }
    while (Clen < col)
	Current[Clen++] = ' ';
    Ep->Column = col;
    if (Ep->Column - Ep->Topcolumn >= Columns || Ep->Column < Ep->Topcolumn)
	text_sync();
}

