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 <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
#define PROMPT "msh> "
|
#define PROMPT "msh> "
|
||||||
#define EXIT_CMD "exit"
|
#define EXIT_CMD "exit"
|
||||||
#define MAX_BUF 121
|
#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 echo(const char*);
|
||||||
void remove_newline(char*);
|
void remove_newline(char*);
|
||||||
void exit_branch(const char*);
|
void exit_branch(const char*);
|
||||||
|
int builtin_response(char*);
|
||||||
|
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
char buffer[MAX_BUF];
|
char buffer[MAX_BUF];
|
||||||
while(1) {
|
while(1) {
|
||||||
printf("%s", PROMPT);
|
printf("%s", PROMPT);
|
||||||
fgets(buffer, MAX_BUF, stdin);
|
fgets(buffer, MAX_BUF, stdin);
|
||||||
// process the input
|
// Deal with EOF input
|
||||||
remove_newline(buffer);
|
if(!strlen(buffer)) {
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
exit_branch(buffer);
|
exit_branch(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);
|
echo(buffer);
|
||||||
|
case RESP_SHELL: // shell command handler was used
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
memset(buffer, 0x00, MAX_BUF); // reset buffer after each usage
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -35,9 +58,51 @@ void remove_newline(char* buf) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void exit_branch(const char* buf) {
|
void exit_branch(const char* buf) {
|
||||||
if(strcmp(buf, EXIT_CMD)) {
|
if(!strcmp(buf, EXIT_CMD)) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
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