Reputation: 953
I'm trying to implement auto completion and history in my shell using the GNU Readline library. I was using fgets()
to retrieve user and after reading on how the readline
function works, I decide on using it so as to support auto completion etc. But when I execute my program, the readline
function outputs weird characters on the shell before I even type any input. Weird results like P�6
, PJ�
`,
P�#,
P�s`. For some reason it always start with P. Here's my code:
int main(int argc, char* argv[]) {
char *historic, userInput[1000];
static char **cmdArgv;/**The argv of the main*/
sa.sa_handler = handle_signal;
sigaction(SIGINT, &sa, NULL);
sa.sa_flags = SA_RESTART; /** Restart function incase it's interrupted by handler */
cmdArgv = malloc(sizeof (*cmdArgv));
welcomeScreen();//just printf's nothing special
while(TRUE)
{
shellPrompt();// getcwd function
historic = readline(userInput);
if (!historic)
break;
//path autocompletion when tabulation hit
rl_bind_key('\t', rl_complete);
//adding the previous input into history
add_history(historic);
if( check_syntax(userInput) == 0 ){
createVariable(userInput);
}
else{
tokenize(cmdArgv, userInput);
special_chars(cmdArgv);
executeCommands(cmdArgv, userInput);
}
}
Any ideas as to what the problem is? Thanks.
Upvotes: 2
Views: 978
Reputation: 121961
Initialise userInput
before passing it to readLine()
:
memset(userInput, 0, sizeof(userInput));
This is the description for argument passed to the readLine()
function (which I found here man readline):
If the argument is NULL or the empty string, no prompt is issued.
As you had not initialised userInput
it was displaying whatever happened to be in there.
Upvotes: 3