wm: teppich

ref: e97a0dc45f6266ac067b23dabba72e571bde120b
dir: /cmd/rc.c/

View raw version
#include <u.h>
#include <libc.h>

#include <ps2.h>

#include <mem.h>
#include <err.h>
#include "rc.h"

static char *cwd;

int
run(char *cmd)
{
	int argc;
	char *arg0, **argv;
	
	/* always always always
	check for nil pointers */
	
	arg0 = strcntok(cmd, ' ', 0);
	
	if(!arg0)
	{
		arg0 = cmd;
		argc = 0;
	}
	else
	{
		for(argc = 0 ; argc < (strccnt(cmd, ' ') + 1) ; argc++)
		{
			argv[argc] = strcntok(cmd, ' ', argc);
		}
	}
	for(int i = 0 ; i < sizeof(cmdtab) ; i++)
	{
		if(!strcmp(arg0, cmdtab[i].name))
			return cmdtab[i].main(argc, argv, cwd);
	}
	printf("No such command\n");
	
	return NO_SUCH_CMD;
}

void
rc_main(void)
{
	char c;
	char *cmd;

	cwd = "/";
	int i = 0;
	memset(cmd, 0, 25);
	
	printf("> ");
	while(1)
	{
		c = ps2_getc();
		printf("%c", c);
		
		if(c == '\r' || c == '\n')
		{
			cmd[i] = '\0';
			run(cmd);
			
			/* clear the mess */
			cmd = malloc(25);
			i = 0;
			
			printf("> ");
		}
		/* ignore spaces as first character and ignore more than one space */
		else if((c == ' ') && (i == 0 || cmd[i - 1] == ' '))
			continue;
		else if(c != '\b')
		{
			cmd[i] = c;
			i++;
		}
		else
		{
			/* we don't want a negative i */
			if(i)
				i--;
			cmd[i] = 0;
		}
	}
}