Page 1 of 1

Console developement question

Posted: Mon Sep 17, 2007 6:38 pm
by raevin
What I'm trying to do is create a console (think DOS, for simplicity sake). I have a kernel set up, monitor code to print code and such, really basic filesystem set up (using the Skelix OS tutorials), and some other stuff...I don't have much memory management created yet (yet, I kind of think I should do this before working on console stuff). I don't have paging set up (important?...never saw the point in this for a mono-kernel).

Anywho...enough with the rundown, the issue I'm posting about.

If I do a comparison of what was entered character-by-character, I get the "command" to run. However, if I do anything else, such as storing the valid commands in an char array (like commands[] or commands[COMMANDS+1] (where COMMANDS is how many valid commands there are)), it either doesn't work (at all), or compiles with errors. Below is the current source for this information.

tty.h

Code: Select all

#ifndef __TTY_H
#define __TTY_H

#include "common.h"

#define COMMANDS 1 // How many commands are available to be called?

typedef struct tty{
	int index; // Current position in in[] array
	char in[];    // Input buffer
} tty_t;

tty_t teletype;

void handle_key(char key);
void do_command();

#endif
tty.c

Code: Select all

#include "include/tty.h"
#include "include/strings.h"

static char commands[] = {"hi"};

void handle_key(char key){
	if(key == '\n'){
		do_command(); // enter-key was pressed

		int i = 0;

		while(i < teletype.index){
			teletype.in[i] = 0x00;

			i++;
		}

		teletype.index = 0;
	} else{
		teletype.in[teletype.index] = key;
		teletype.index += 1;

		printf("%c", key);
	}
}

void do_command(){
	teletype.in[teletype.index] = 0x00;

	int i = 0;

	while(i < COMMANDS){
		if(teletype.in == commands[i])
			printf("\nWe hit a command!\n");

		i++;
	}

	printf("\nTeletype buffer: %s\nIndex: %d\n\n", teletype.in, teletype.index);
}
What happens is I pass a converted scancode (that is a char by the time the function is called) in my keyboard setup...and if the user hit enter I do the command, and reset the buffer and index...otherwise I store the character in the buffer, increment the index, and print the character. I know "index += 1;" isn't the best way to do it, and I could do "index++;", but ++ wouldn't work for me either...

(I know the files are named wrong, since I doubt this is a TTY setup by any means...but, I'm more focused on the issue right now.)

The code isn't commented for now because I'm just debugging the stuff, and it's pretty self explanatory with what's there...(not to mention I was in a rush while typing it up).

Most of the time, when I compile this portion of the system, I get an error saying that the commands array has an excess of entries...no matter how I declare it.

Example:

Code: Select all

#define COMMANDS 3

static char commands[] = {"hi", "bob", "senior"};
And, the system specs if that helps:

GCC v4.1, latest version of NASM
Using Linux (Ubuntu)

Posted: Mon Sep 17, 2007 7:59 pm
by proxy
you are getting an error because you are putting things of type "char *" into an array of chars..

if you want an array of strings you need to do something like this:

Code: Select all

char *commands[] = { "this", "is", "a", "test" };

Posted: Mon Sep 17, 2007 8:37 pm
by raevin
proxy wrote:you are getting an error because you are putting things of type "char *" into an array of chars..

if you want an array of strings you need to do something like this:

Code: Select all

char *commands[] = { "this", "is", "a", "test" };
Actually, I think I tried that too...but, I'll give it a whack and see what happens; thanks much!

Posted: Mon Sep 17, 2007 10:49 pm
by raevin
Okay, somewhat of a new problem...

It's still not working the way it should...well, not at all really.

Below is my new code...

tty.h

Code: Select all

#ifndef __TTY_H
#define __TTY_H

#include "common.h"

// TTY struct (change to console)
typedef struct tty{
	u32int index; // Current position in in[] array
	char in[];    // Input buffer
} tty_t;

tty_t teletype; // Define it here

void handle_key(char key); // Function to handle input from keyboard
void do_command(char *cmd);         // The command to do

#endif
And now tty.c

Code: Select all

#include "include/tty.h"
#include "include/strings.h"

char *commands[] = {"hi", "bob"};

void handle_key(char key){
	if(key == '\n'){
		do_command(teletype.in); // enter-key was pressed

		int i = 0;

		while(i < teletype.index){
			teletype.in[i] = 0x00;

			i++;
		}

		teletype.index = 0;
	} else{
		teletype.in[teletype.index] = key;
		teletype.index += 1;

		printf("%c", key);
	}
}

void do_command(char *cmd){
	int i = 0;

	while(commands[i] != NULL){
		printf("Checking %s (len: %d) against %s (len: %d)\n", cmd, strlen(cmd), commands[i], strlen(commands[i]));

		if(cmd == commands[i])
			printf("\nWe hit a command!\n");

		i++;
	}
	
	printf("\nTeletype buffer: %s\nIndex: %d\n\n", teletype.in, teletype.index);
}
If you type "hi" in, the "Checking..." message displays the same for both the command entered, and the "hi" command in the array, but there's still no match.

The only thing I can think of doing is doing a check character-by-character, but I don't see how that's possible with the "char *" array for the commands. I've already tried it too (for the sake of seeing), by doing like:

Code: Select all

char cmds = commands[i];

int z = 0;

while(cmd[z] == cmds[z]){
// blahblahblah
i++;
}
(Typecasting the commands to "char" doesn't work either.)

I'm just really...baffled, as to why the comparison of "hi == hi" doesn't work.

I don't have the handle_key() function do anything with the \n's...perhaps that could be a problem?

Posted: Mon Sep 17, 2007 11:46 pm
by jnc100
When you do
raevin wrote:

Code: Select all

if(cmd == commands[i])
         printf("\nWe hit a command!\n");
you're actually comparing the pointer to the 'cmd' string with that for the 'commands' string, which are different, despite the fact that the actual data stored at these addresses ('hi') are the same. To compare strings, use strcmp() or equivalent. You'll probably have to write these yourself, but they're not too hard (just look up the man page for a description).

Regards,
John.

Posted: Mon Sep 17, 2007 11:47 pm
by distantvoices
You want to compare two strings, eh?

In C, as in other related languages (not Visual Basic, not Power Builder nor any scripting struff where this is hidden from us programmers) you have to invoke a library function/method to compare strings. :-)

say in c this 'd be strcmp(s1,s2), which returns 0 if the both of them are equal or summat !=0 (-1,1) in case they are different.

man strcmp will reveal more info.

You may read up on string comparison in the internet. google spits hell a lot of useful stuff at you. :-)

After that, provide your os with some lib functions for dealing with strings.

Stay safe

Posted: Mon Sep 17, 2007 11:52 pm
by raevin
distantvoices wrote:You want to compare two strings, eh?

In C, as in other related languages (not Visual Basic, not Power Builder nor any scripting struff where this is hidden from us programmers) you have to invoke a library function/method to compare strings. :-)

say in c this 'd be strcmp(s1,s2), which returns 0 if the both of them are equal or summat !=0 (-1,1) in case they are different.

man strcmp will reveal more info.

You may read up on string comparison in the internet. google spits hell a lot of useful stuff at you. :-)

After that, provide your os with some lib functions for dealing with strings.

Stay safe
Actually...I didn't think about that...T_T

I have those functions already created (have a math and string library at least partially created right now).

Hmm...thanks much! I'll see how that goes.

Edit

Works better than I was doing before...^_^; Great start to this...thanks again to you both for the help!