OSDev.org

The Place to Start for Operating System Developers
It is currently Sun Apr 05, 2020 7:48 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: Keyboard problem (so I thought)
PostPosted: Mon Jun 14, 2010 2:10 pm 
Offline
Member
Member
User avatar

Joined: Fri Jan 16, 2009 8:34 pm
Posts: 284
Location: Louisiana, USA
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:
#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:
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:
- 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.


Top
 Profile  
 
 Post subject: Re: Keyboard problem (so I thought)
PostPosted: Mon Jun 14, 2010 3:20 pm 
Offline
Member
Member

Joined: Sun Jan 14, 2007 9:15 pm
Posts: 2566
Location: Sydney, Australia (I come from a land down under!)
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.

_________________
Pedigree | GitHub | Twitter | LinkedIn


Top
 Profile  
 
 Post subject: Re: Keyboard problem (so I thought)
PostPosted: Tue Jun 15, 2010 4:51 am 
Offline
Member
Member
User avatar

Joined: Thu Feb 21, 2008 5:59 am
Posts: 120
Location: Ottawa, Canada
Yeah, that's not Java or C#, so

Code:
if(command == "cls")


isn't going to work! ;)


Top
 Profile  
 
 Post subject: Re: Keyboard problem (so I thought)
PostPosted: Tue Jun 15, 2010 10:19 am 
Offline
Member
Member
User avatar

Joined: Thu Mar 06, 2008 1:26 pm
Posts: 81
Location: Roseville, California (USA)
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


Top
 Profile  
 
 Post subject: Re: Keyboard problem (so I thought)
PostPosted: Tue Jun 15, 2010 10:45 am 
Offline
Member
Member
User avatar

Joined: Tue Aug 08, 2006 7:43 am
Posts: 271
Location: Sydney, Australia
-m32 wrote:
Yeah, that's not Java or C#, so

Code:
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


Top
 Profile  
 
 Post subject: Re: Keyboard problem (so I thought)
PostPosted: Tue Jun 15, 2010 1:09 pm 
Offline
Member
Member
User avatar

Joined: Thu Feb 21, 2008 5:59 am
Posts: 120
Location: Ottawa, Canada
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


Top
 Profile  
 
 Post subject: Re: Keyboard problem (so I thought)
PostPosted: Wed Jun 16, 2010 9:50 am 
Offline
Member
Member
User avatar

Joined: Fri Jan 16, 2009 8:34 pm
Posts: 284
Location: Louisiana, USA
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:
#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:
#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:
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.


Top
 Profile  
 
 Post subject: Re: Keyboard problem (so I thought)
PostPosted: Wed Jun 16, 2010 10:02 am 
Offline
Member
Member
User avatar

Joined: Sun Oct 22, 2006 7:01 am
Posts: 2637
Location: Devon, UK
Hi,

BASICFreak wrote:
wondering if there is an easier way than:
Code:
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:
strcmp("cls", command) == 0

Cheers,
Adam


Top
 Profile  
 
 Post subject: Re: Keyboard problem (so I thought)
PostPosted: Wed Jun 16, 2010 2:27 pm 
Offline
Member
Member

Joined: Sat Nov 21, 2009 5:11 pm
Posts: 668
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.


Top
 Profile  
 
 Post subject: Re: Keyboard problem (so I thought)
PostPosted: Wed Jun 16, 2010 2:33 pm 
Offline
Member
Member
User avatar

Joined: Fri Jun 13, 2008 3:21 pm
Posts: 1700
Location: Cambridge, United Kingdom
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)


Top
 Profile  
 
 Post subject: Re: Keyboard problem (so I thought)
PostPosted: Wed Jun 16, 2010 4:56 pm 
Offline
Member
Member
User avatar

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


Top
 Profile  
 
 Post subject: Re: Keyboard problem (so I thought)
PostPosted: Thu Jun 17, 2010 8:32 am 
Offline
Member
Member
User avatar

Joined: Thu Nov 16, 2006 12:01 pm
Posts: 7461
Location: Germany
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.


Top
 Profile  
 
 Post subject: Re: Keyboard problem (so I thought)
PostPosted: Thu Jun 17, 2010 10:40 am 
Offline
Member
Member
User avatar

Joined: Thu Mar 06, 2008 1:26 pm
Posts: 81
Location: Roseville, California (USA)
I second that. Try writing software that you might like to run in your OS. :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot], MSN [Bot] and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group