shell: added completion support
This commit is contained in:
@@ -1,8 +1,26 @@
|
||||
#include "builtins.h"
|
||||
|
||||
const char* s_builtin_names[] = {"cd", "exit"};
|
||||
const char* s_builtin_names[] = {"cd", "exit", NULL};
|
||||
s_builtin s_builtins[] = {&s_cd, &s_exit};
|
||||
|
||||
char* s_names(const char* text, int state) {
|
||||
static int li, len;
|
||||
const char* name;
|
||||
|
||||
if(!state) {
|
||||
li = 0;
|
||||
len = strlen(text);
|
||||
}
|
||||
|
||||
while ((name = s_builtin_names[li++])) {
|
||||
if (strncmp(name, text, len) == 0) {
|
||||
return strdup(name);
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int s_cd(char** args) {
|
||||
if (!args[1]) {
|
||||
s_error("cd", "expected argument");
|
||||
@@ -17,5 +35,5 @@ int s_exit(char** args) {
|
||||
}
|
||||
|
||||
int s_builtins_count() {
|
||||
return sizeof(s_builtin_names) / sizeof(const char *);
|
||||
return (sizeof(s_builtin_names) / sizeof(const char *)) - 1;
|
||||
}
|
||||
|
@@ -1,9 +1,12 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
typedef int (*s_builtin)(char**);
|
||||
|
||||
char* s_names(const char* text, int state);
|
||||
|
||||
int s_cd(char**);
|
||||
int s_exit(char**);
|
||||
int s_builtins_count();
|
||||
|
@@ -1,5 +1,10 @@
|
||||
#include "shell.h"
|
||||
|
||||
char** s_completion(const char* text, int start, int end) {
|
||||
rl_attempted_completion_over = 1;
|
||||
return rl_completion_matches(text, s_names);
|
||||
}
|
||||
|
||||
void s_die(const char* msg) {
|
||||
fprintf(stderr, "s: %s\n", msg);
|
||||
exit(1);
|
||||
@@ -72,6 +77,8 @@ int s_exec(char** args) {
|
||||
}
|
||||
|
||||
void s_loop() {
|
||||
rl_attempted_completion_function = s_completion;
|
||||
|
||||
char** cmd;
|
||||
int s;
|
||||
|
||||
|
Reference in New Issue
Block a user