Page 1 of 1

CLI history

Posted: Wed Sep 17, 2014 8:10 am
by vorg147
Good day!

I'm currently new in making OS and i want to save all commands entered in the CLI.
here is the code given to me to get the input in the CLI

Code: Select all

void get_input (char* buf, int n) {
	
	KEYCODE key = KEY_UNKNOWN;
	bool	BufChar;

	//! get command string
	int i=0;
	int x= 0;
	while ( i < n ) {

		//! buffer the next char
		BufChar = true;

		//! grab next char
		key = getch ();

		//! end of command if enter is pressed
		if (key==KEY_RETURN){
			
			
			break;
		}
			

		//! backspace
		if (key==KEY_BACKSPACE) {

			//! dont buffer this char
			BufChar = false;

			if (i > 0) {

				//! go back one char
				unsigned y, x;
				DebugGetXY (&x, &y);
				if (x>0)
					DebugGotoXY (--x, y);
				else {
					//! x is already 0, so go back one line
					y--;
					x = DebugGetHorz ();
				}

				//! erase the character from display
				DebugPutc (' ');
				DebugGotoXY (x, y);

				//! go back one char in cmd buf
				i--;
			}
		}

		//! only add the char if it is to be buffered
		if (BufChar) {

			//! convert key to an ascii char and put it in buffer
			char c = kkybrd_key_to_ascii (key);
			
			if (c != 0) { //insure its an ascii char

				DebugPutc (c);
				buf [i++] = c;
				
			}

			
		}

		//! wait for next key. You may need to adjust this to suite your needs
		sleep (10);
	}

	//! null terminate the string
	buf [i] = '\0';
}






i tried creating a char* history above it then equate it with buf when enter key is pressed

Code: Select all

if (key==KEY_RETURN){
			
		history = buf;	
			break;
		}
this code works only for the previous input.

For example i typed history to print the CLI command history but the only output i get is "history" which is the last input before printing the CLI command history


thank you for your help!

Re: CLI history

Posted: Wed Sep 17, 2014 8:15 am
by iansjack
You need to create a linked list, or a stack, of the commands enetered.

Re: CLI history

Posted: Wed Sep 17, 2014 9:45 am
by SpyderTL
Change history to an array of pointers, and add a counter variable to count the number of items in the array. Then when you hit enter, set that history array entry at the current count to the buffer address, and increment the counter variable.