CLI history

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
vorg147
Posts: 1
Joined: Wed Sep 17, 2014 7:59 am

CLI history

Post 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!
User avatar
iansjack
Member
Member
Posts: 4709
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: CLI history

Post by iansjack »

You need to create a linked list, or a stack, of the commands enetered.
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: CLI history

Post 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.
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
Post Reply