couple things missing but meh its there mostly
This commit is contained in:
parent
12ec8dbe7c
commit
61cd0af471
@ -1,24 +1,47 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#define PROMPT "msh> "
|
||||
#define EXIT_CMD "exit"
|
||||
#define MAX_BUF 121
|
||||
|
||||
// Statuses from builtin responses
|
||||
#define RESP_HELP 1
|
||||
#define RESP_DATE 2
|
||||
#define RESP_ECHO 3
|
||||
#define RESP_SHELL 4
|
||||
|
||||
void echo(const char*);
|
||||
void remove_newline(char*);
|
||||
void exit_branch(const char*);
|
||||
int builtin_response(char*);
|
||||
|
||||
|
||||
int main(void) {
|
||||
char buffer[MAX_BUF];
|
||||
while(1) {
|
||||
printf("%s", PROMPT);
|
||||
fgets(buffer, MAX_BUF, stdin);
|
||||
// process the input
|
||||
remove_newline(buffer);
|
||||
// Deal with EOF input
|
||||
if(!strlen(buffer)) {
|
||||
exit(0);
|
||||
}
|
||||
exit_branch(buffer);
|
||||
echo(buffer);
|
||||
|
||||
remove_newline(buffer);
|
||||
int status = builtin_response(buffer);
|
||||
// the switch in case we need to add more response flags later
|
||||
switch(status) {
|
||||
case RESP_ECHO:
|
||||
echo(buffer);
|
||||
case RESP_SHELL: // shell command handler was used
|
||||
continue;
|
||||
}
|
||||
memset(buffer, 0x00, MAX_BUF); // reset buffer after each usage
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -35,9 +58,51 @@ void remove_newline(char* buf) {
|
||||
}
|
||||
|
||||
void exit_branch(const char* buf) {
|
||||
if(strcmp(buf, EXIT_CMD)) {
|
||||
return;
|
||||
if(!strcmp(buf, EXIT_CMD)) {
|
||||
exit(0);
|
||||
}
|
||||
exit(0);
|
||||
return; // just for gcc but exit shouldn't fail
|
||||
}
|
||||
|
||||
int builtin_response(char* buffer) {
|
||||
#define STRING_DELIMITER " "
|
||||
#define TIME_FMT "%D"
|
||||
exit_branch(buffer); // exit is builtin so here we are
|
||||
if(!strcmp(buffer, "help")) {
|
||||
echo("enter Linux commands, or ‘exit’ to exit");
|
||||
return RESP_HELP;
|
||||
}
|
||||
|
||||
// Printout the date today
|
||||
if(!strcmp(buffer, "date")) {
|
||||
static char date[64];
|
||||
time_t now = time(0);
|
||||
strftime(date, sizeof(date), TIME_FMT, localtime(&now));
|
||||
echo(date);
|
||||
return RESP_DATE;
|
||||
}
|
||||
|
||||
char *tokens[64];
|
||||
int idx = 0;
|
||||
for(char* cur = strtok(buffer, STRING_DELIMITER); cur != NULL; cur=strtok(NULL, STRING_DELIMITER)) {
|
||||
#ifdef DBG_LINE
|
||||
printf("%s ", cur);
|
||||
#endif
|
||||
tokens[idx] = cur;
|
||||
idx++;
|
||||
}
|
||||
|
||||
// Execute child process
|
||||
pid_t child = fork();
|
||||
if(child) {
|
||||
int status_child = wait(NULL);
|
||||
if(status_child == 127) {
|
||||
echo("command not found: oh btw remember to not print this string kek");
|
||||
}
|
||||
}
|
||||
execvp(tokens[0], tokens);
|
||||
// Cleanup pointers
|
||||
for(int i = 0; i< 64; i++) {
|
||||
tokens[i] = NULL;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user