Help with a simple Terminal Project
Hello all. I've used the threads in this board before for a lot of help, but just now posting.
I'm trying to code a simple Terminal as part of an OS course. I feel like I have a good start on it, and I'm trying to make sure my input function works as I want it to.
The only problem, for some reason I'm getting nothing. I'm guessing there's an error early on in main() but I can't find it for the life of me.
reason I believe it's here, is that I'm not even getting my prompt, which should come up before any input.
prompt is this part:
char cwd[512];
getcwd(cwd, 512);
printf("%s>", cwd);
just after the start of the while loop in main.
It's been a long time since I've had to work in C, so I know my pointer-fu is off. Please be gentle. Any help greatly appreciated.
Code:
int getInput(char *outputBuffer, int currentPos, char *breakCharacter){
//Declare:
char *output = outputBuffer + currentPos; //outputBuffer
int position = currentPos; //outputPosition = 0
char innerBuffer[256]; //innerBuffer
char *broken = breakCharacter; //brokenCharacter
//while not broken
while(1){
//read to innerBuffer
int bytesRead;
bytesRead = read(0, innerBuffer, 256);
//for each character in innerBuffer
int i;
for(i = 0; i < bytesRead; i++){
//if current character is an ignore character
//- do nothing
if(bytesRead - i >= 3 && ((innerBuffer[i] == 27 && innerBuffer[i+1] == 91 && innerBuffer[i+2] ==68)
|| (innerBuffer[i] == 27 && innerBuffer[i+1] == 91 && innerBuffer[i+2] ==67)))
continue;
//else if current character is a break character
//- set brokenCharacter
//- break out of loops
else if(innerBuffer[i] == '\n'){
*broken = '\n';
break;
}
else if(bytesRead - i >= 3 && (innerBuffer[i] == 27 && innerBuffer[i+1] == 91 && innerBuffer[i+2] ==65)){
*broken = 'u'; //for up, saves 2 characters and allows for more uniform code.
break;
}
else if(bytesRead - i >= 3 && (innerBuffer[i] == 27 && innerBuffer[i+1] == 91 && innerBuffer[i+2] ==66)){
*broken = 'd'; //for down
break;
}
//else if current character is backspace or delete
//- print \b
//- decrement outputPosition
if(innerBuffer[i] == 8 || innerBuffer[i] == 127){
if(position > 0){
putchar('\b');
position--;
}
}
//else
//- print current character
//- copy the current character to outBuffer at
//outputPosition
//- increment the outputPosition
else{
putchar(innerBuffer[i]);
output[position] = innerBuffer[i];
position++;
}
}
}
//Return:
//outputBuffer
//size of outputBuffer
//brokenCharacter
outputBuffer = output;
return position;
}
int main()
{
/*
//configure the input (termios)
// get the original configuration
struct termios origConfig;
tcgetattr(0, &origConfig);
// create a copy of the original configuration
struct termios newConfig = origConfig;
// adjust the new configuration
newConfig.c_lflag &= ~(ICANON|ECHO);
newConfig.c_cc[VMIN] = 10;
newConfig.c_cc[VTIME] = 2;
// set the new configuration
tcsetattr(0, TCSANOW, &newConfig);
*/
char command[256];
char breakCharacter;
char *brokenCharacter = &breakCharacter;
int commandSize = 0;
//while not exited
while(1){
//print prompt
char cwd[512];
getcwd(cwd, 512);
printf("%s>", cwd);
//read input (break on up, down and \n)
commandSize = getInput(command, commandSize, brokenCharacter);
//if broken on up or down
if(breakCharacter == 'd' || breakCharacter == 'u'){
//clear the current input
int i;
for(i = commandSize; i > 0; i++){
putchar('\b');
putchar(' ');
putchar('\b');
}
//execute appropriate command to generate corresponding message
if(breakCharacter == 'u'){
strcpy(command, getenv("UserName"));
commandSize = strlen(command);
}
else{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
strcpy(command, asctime(timeinfo));
commandSize = strlen(command);
}
continue;
}
else{
//parse input into a command
//if cd command
//change the current working directory
//else if exit command
//break
break;
//else
//execute the command
}
}
//restore the input configuration (termios)
//tcsetattr(0, TCSANOW, &origConfig);
return 0;
}
largely commented area at the beginning of main is to set terminal io, but commented in the off chance it was the issue.