Page 1 of 1

Keyboard problem (so I thought)

Posted: Mon Jun 14, 2010 2:10 pm
by BASICFreak
ok, lets get straight to the point, I am trying to do a very simple command prompt right now, here is my Key-Board driver:

Code: Select all

#include <system.h>
unsigned char* input = "";
unsigned char kbdus[128] =
{
    0,  27, '1', '2', '3', '4', '5', '6', '7', '8',	/* 9 */
  '9', '0', '-', '=', '\b',	/* Backspace */
  '\t',			/* Tab */
  'q', 'w', 'e', 'r',	/* 19 */
  't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',		/* Enter key */
    0,			/* 29   - Control */
  'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',	/* 39 */
 '\'', '`',   0,		/* Left shift */
 '\\', 'z', 'x', 'c', 'v', 'b', 'n',			/* 49 */
  'm', ',', '.', '/',   0,					/* Right shift */
  '*',
    0,	/* Alt */
  ' ',	/* Space bar */
    0,	/* Caps lock */
    0,	/* 59 - F1 key ... > */
    0,   0,   0,   0,   0,   0,   0,   0,
    0,	/* < ... F10 */
    0,	/* 69 - Num lock*/
    0,	/* Scroll Lock */
    0,	/* Home key */
    0,	/* Up Arrow */
    0,	/* Page Up */
  '-',
    0,	/* Left Arrow */
    0,
    0,	/* Right Arrow */
  '+',
    0,	/* 79 - End key*/
    0,	/* Down Arrow */
    0,	/* Page Down */
    0,	/* Insert Key */
    0,	/* Delete Key */
    0,   0,   0,
    0,	/* F11 Key */
    0,	/* F12 Key */
    0,	/* All other keys are undefined */
};

void keyboard_handler(struct regs *r)
{
    unsigned char scancode;

    scancode = inportb(0x60);

    if (scancode & 0x80)
    {
    }
    else
    {
	char temp = kbdus[scancode];
	if(scancode != 0)
	{
		if(temp == '\n')
		{
			runCommand(input);
			input = "";
		}
		else
		{
			input = input + temp;
		}
	}
        putch(temp);
    }
}
void keyboard_install()
{
    irq_install_handler(1, keyboard_handler);
}
and here is runCommand:

Code: Select all

void runCommand(unsigned char * command)
{
	if(command == "cls")
	{
		cls();
	}
	else if(command == "help")
	{
		puts("\nBrian Hoover's OS v. 0.01\n");
		puts("\ncls - Clears Screen\n");
		puts("help - Displays This Help Screen\n");
		puts("exit - Exit The Shell\n");
	}
	else
	{
		puts("\nInvalid Command");
	}
	puts("\n");
	puts(command);
	puts("\n>");
	return;
}
Every time I press enter it puts out Invalid Command and a "random" string or chars, but while typing it I see it perfectly... I have googled this and so far I heard the linker script may be the problem, I am using a standard link.ld which is found on almost every tutorial on osdever.net

Something strange seems to be happening with the variable input.

I just need to be pointed in the right direction.

snapshot of bochs:

Code: Select all

 - Clears Screen

>
1234
Invalid Command

>
123456789
Invalid Command

>
123
Invalid Command
 - Clears Screen

>
12
Invalid Command
[Аcls
>
cls
Invalid Command

>


Re: Keyboard problem (so I thought)

Posted: Mon Jun 14, 2010 3:20 pm
by pcmattman
There are two things you need to research which will make the faults in your code obvious:
  • strcmp (C library call)
  • Pointers - more specifically, why adding a "char" to a "char*" is incorrect, and then based on that knowledge, how to append a character to a string correctly.
Shouldn't be more than a couple hours of work.

Re: Keyboard problem (so I thought)

Posted: Tue Jun 15, 2010 4:51 am
by -m32
Yeah, that's not Java or C#, so

Code: Select all

if(command == "cls")
isn't going to work! ;)

Re: Keyboard problem (so I thought)

Posted: Tue Jun 15, 2010 10:19 am
by AaronMiller
When you compare a pointer to a constant, chances are the pointer will not equal that constant. For example your pointer, command, might be 0x11223344, which might point to "cls." However, when you compare to "cls," which might be a constant at 0x0010234 in your rodata section, it will NOT equal that value. You must compare each character, stopping at the null character.

As just illustrated, "input = input + temp" will not work either. You're basically add the address of the temporary buffer to the address of the input buffer. This will not store any information. Implementing a command like strcat will work, but your buffer(s) must be preallocated. That is, char input[256], or similar.

Cheers,
-Aaron

Re: Keyboard problem (so I thought)

Posted: Tue Jun 15, 2010 10:45 am
by ucosty
-m32 wrote:Yeah, that's not Java or C#, so

Code: Select all

if(command == "cls")
isn't going to work! ;)
Off topic, I know, but that doesn't work in Java either :wink:

Re: Keyboard problem (so I thought)

Posted: Tue Jun 15, 2010 1:09 pm
by -m32
ucosty wrote:Off topic, I know, but that doesn't work in Java either :wink:
Really? Haven't done any Java in 7+ years. Guess I'm a bit rusty now then :D

Re: Keyboard problem (so I thought)

Posted: Wed Jun 16, 2010 9:50 am
by BASICFreak
Well I fixed it and got it working today, (took a break only work on this in my free time at work)
here is my code:
KeyBoard:

Code: Select all

#include <system.h>
char input[256] = {'\0',};

unsigned char kbdus[128] =
{
    0,  27, '1', '2', '3', '4', '5', '6', '7', '8',	/* 9 */
  '9', '0', '-', '=', '\b',	/* Backspace */
  '\t',			/* Tab */
  'q', 'w', 'e', 'r',	/* 19 */
  't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',		/* Enter key */
    0,			/* 29   - Control */
  'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',	/* 39 */
 '\'', '`',   0,		/* Left shift */
 '\\', 'z', 'x', 'c', 'v', 'b', 'n',			/* 49 */
  'm', ',', '.', '/',   0,					/* Right shift */
  '*',
    0,	/* Alt */
  ' ',	/* Space bar */
    0,	/* Caps lock */
    0,	/* 59 - F1 key ... > */
    0,   0,   0,   0,   0,   0,   0,   0,
    0,	/* < ... F10 */
    0,	/* 69 - Num lock*/
    0,	/* Scroll Lock */
    0,	/* Home key */
    0,	/* Up Arrow */
    0,	/* Page Up */
  '-',
    0,	/* Left Arrow */
    0,
    0,	/* Right Arrow */
  '+',
    0,	/* 79 - End key*/
    0,	/* Down Arrow */
    0,	/* Page Down */
    0,	/* Insert Key */
    0,	/* Delete Key */
    0,   0,   0,
    0,	/* F11 Key */
    0,	/* F12 Key */
    0,	/* All other keys are undefined */
};

void keyboard_handler(struct regs *r)
{
    unsigned char scancode;

    scancode = inportb(0x60);

    if (scancode & 0x80)
    {
    }
    else
    {
	char temp = kbdus[scancode];
	if(scancode != 0)
	{
		if(temp == '\n')
		{
			runCommand(input);
			int done = 0;
			input[0] = '\0';
		}
		else if(temp == '\b')
		{
			int done = 0;
			int intot = 0;
			while(done!=1)
			{
				if(input[intot] == '\0')
				{
					input[intot - 1] = '\0';
					done = 1;
					puts("\b ");
				}
				intot++;
			}
		}
		else
		{
			int done = 0;
			int intot = 0;
			while(done!=1)
			{
				if(input[intot] == '\0')
				{
					input[intot] = temp;
					done = 1;
				}
				intot++;
			}
			input[intot] = '\0';
		}
	}
        if(temp != '\n') putch(temp);
    }
}
void keyboard_install()
{
    irq_install_handler(1, keyboard_handler);
}
runCommand:

Code: Select all

#include <system.h>

void runCommand(char command[])
{
	if(command[0] == 'c' && command[1] == 'l' && command[2] == 's' && command[3] == '\0')
	{
		cls();
	}
	else if(command[0] == 'h' && command[1] == 'e' && command[2] == 'l' && command[3] == 'p' && command[4] == '\0')
	{
		puts("\nBrian Hoover's OS v. 0.01\n");
		puts("\ncls - Clears Screen\n");
		puts("help - Displays This Help Screen\n");
	}
	else
	{
		puts("\nInvalid Command");
	}
	puts("\n>");
	return;
}
wondering if there is an easier way than:

Code: Select all

if(command[0] == 'c' && command[1] == 'l' && command[2] == 's' && command[3] == '\0')
that I could use, it has been about 5 years since I used C, I wasn't thinking about doing this becouse most my coding lately was in java and php.

Back to the off topic discussion:
if(command == "cls") - will sometimes work in java but in java it should be if(command.equals("cls"))

Re: Keyboard problem (so I thought)

Posted: Wed Jun 16, 2010 10:02 am
by AJ
Hi,
BASICFreak wrote:wondering if there is an easier way than:

Code: Select all

if(command[0] == 'c' && command[1] == 'l' && command[2] == 's' && command[3] == '\0')
that I could use, it has been about 5 years since I used C, I wasn't thinking about doing this becouse most my coding lately was in java and php.
Once you have a strcmp function, just use

Code: Select all

strcmp("cls", command) == 0
Cheers,
Adam

Re: Keyboard problem (so I thought)

Posted: Wed Jun 16, 2010 2:27 pm
by Gigasoft
If you just want to test for the specific string "cls", the easiest way is if(*(int*)command=='slc'), but when there's more than a few possible commands it's better to use an array which you search through, followed by a switch.

Re: Keyboard problem (so I thought)

Posted: Wed Jun 16, 2010 2:33 pm
by Owen
Gigasoft wrote:If you just want to test for the specific string "cls", the easiest way is if(*(int*)command=='slc'), but when there's more than a few possible commands it's better to use an array which you search through, followed by a switch.
The above has completely undefined behaviour, and the compiler is legally allowed to do whatever it wants (including insult your mum ;)) if you do it.

Also, its invalid code anyway (but for different reasons)

Re: Keyboard problem (so I thought)

Posted: Wed Jun 16, 2010 4:56 pm
by -m32
pcmattman didn't suggest looking up strcmp() for no good reason.

You have to at least know how to use basic C functions.

http://lmgtfy.com/?q=man+strcmp, youll get the gist of the function here but of course you'll have to write your own version.

Re: Keyboard problem (so I thought)

Posted: Thu Jun 17, 2010 8:32 am
by Solar
Or use the one from PDCLib.

However, on a whole, it seems that you are not really familiar with C programming. As such, I strongly recommend you put your OS project on hold, and pick up some coding project that happens in user space, where standard libraries and lots of helpful tools are available to you. Become proficient with your language of choice before setting out to write an operating system.

Re: Keyboard problem (so I thought)

Posted: Thu Jun 17, 2010 10:40 am
by AaronMiller
I second that. Try writing software that you might like to run in your OS. :)