Keyboard problem (so I thought)

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
BASICFreak
Member
Member
Posts: 284
Joined: Fri Jan 16, 2009 8:34 pm
Location: Louisiana, USA

Keyboard problem (so I thought)

Post 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

>

BOS Source Thanks to GitHub
BOS Expanded Commentary
Both under active development!
Sortie wrote:
  • Don't play the role of an operating systems developer, be one.
  • Be truly afraid of undefined [behavior].
  • Your operating system should be itself, not fight what it is.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: Keyboard problem (so I thought)

Post 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.
User avatar
-m32
Member
Member
Posts: 120
Joined: Thu Feb 21, 2008 5:59 am
Location: Ottawa, Canada

Re: Keyboard problem (so I thought)

Post by -m32 »

Yeah, that's not Java or C#, so

Code: Select all

if(command == "cls")
isn't going to work! ;)
User avatar
AaronMiller
Member
Member
Posts: 81
Joined: Thu Mar 06, 2008 1:26 pm
Location: Roseville, California (USA)
Contact:

Re: Keyboard problem (so I thought)

Post 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
User avatar
ucosty
Member
Member
Posts: 271
Joined: Tue Aug 08, 2006 7:43 am
Location: Sydney, Australia

Re: Keyboard problem (so I thought)

Post 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:
The cake is a lie | rackbits.com
User avatar
-m32
Member
Member
Posts: 120
Joined: Thu Feb 21, 2008 5:59 am
Location: Ottawa, Canada

Re: Keyboard problem (so I thought)

Post 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
User avatar
BASICFreak
Member
Member
Posts: 284
Joined: Fri Jan 16, 2009 8:34 pm
Location: Louisiana, USA

Re: Keyboard problem (so I thought)

Post 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"))
BOS Source Thanks to GitHub
BOS Expanded Commentary
Both under active development!
Sortie wrote:
  • Don't play the role of an operating systems developer, be one.
  • Be truly afraid of undefined [behavior].
  • Your operating system should be itself, not fight what it is.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Keyboard problem (so I thought)

Post 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
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: Keyboard problem (so I thought)

Post 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.
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: Keyboard problem (so I thought)

Post 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)
User avatar
-m32
Member
Member
Posts: 120
Joined: Thu Feb 21, 2008 5:59 am
Location: Ottawa, Canada

Re: Keyboard problem (so I thought)

Post 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.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Keyboard problem (so I thought)

Post 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.
Every good solution is obvious once you've found it.
User avatar
AaronMiller
Member
Member
Posts: 81
Joined: Thu Mar 06, 2008 1:26 pm
Location: Roseville, California (USA)
Contact:

Re: Keyboard problem (so I thought)

Post by AaronMiller »

I second that. Try writing software that you might like to run in your OS. :)
Post Reply