Add: argument handling & PoC: echo command
This commit is contained in:
Binary file not shown.
@@ -2,11 +2,13 @@
|
|||||||
#include "../libc/stdio.h"
|
#include "../libc/stdio.h"
|
||||||
#include "../libc/string.h"
|
#include "../libc/string.h"
|
||||||
#include "../programs/programs.h"
|
#include "../programs/programs.h"
|
||||||
|
#include "../libc/stdint.h"
|
||||||
|
|
||||||
#define BUFFER_SIZE 256
|
#define BUFFER_SIZE 256
|
||||||
#define MAX_COMMANDS 16
|
#define MAX_COMMANDS 16
|
||||||
|
#define MAX_ARGS 64
|
||||||
|
|
||||||
typedef void (*command_func_t)();
|
typedef void (*command_func_t)(int argc, char *argv[]);
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
@@ -37,6 +39,19 @@ command_func_t find_command(const char* name)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int parse_input(char* input, char* argv[], int max_args)
|
||||||
|
{
|
||||||
|
int argc = 0;
|
||||||
|
char* token = strtok(input, " ");
|
||||||
|
while (token != NULL && argc < max_args - 1)
|
||||||
|
{
|
||||||
|
argv[argc++] = token;
|
||||||
|
token = strtok(NULL, " ");
|
||||||
|
}
|
||||||
|
argv[argc] = NULL;
|
||||||
|
return argc;
|
||||||
|
}
|
||||||
|
|
||||||
void shell_install()
|
void shell_install()
|
||||||
{
|
{
|
||||||
register_command("help", program_help);
|
register_command("help", program_help);
|
||||||
@@ -48,24 +63,26 @@ void shell_install()
|
|||||||
register_command("math", program_math);
|
register_command("math", program_math);
|
||||||
register_command("bf", program_bf);
|
register_command("bf", program_bf);
|
||||||
register_command("uptime", program_uptime);
|
register_command("uptime", program_uptime);
|
||||||
|
register_command("echo", program_echo);
|
||||||
|
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
char input_buffer[BUFFER_SIZE];
|
char input_buffer[BUFFER_SIZE];
|
||||||
|
char* argv[MAX_ARGS];
|
||||||
colorputs("blankos> ", 9);
|
colorputs("blankos> ", 9);
|
||||||
get_input(input_buffer, BUFFER_SIZE);
|
get_input(input_buffer, BUFFER_SIZE);
|
||||||
puts("\n");
|
puts("\n");
|
||||||
|
|
||||||
if (strcmp(input_buffer, "") == 0) {
|
int argc = parse_input(input_buffer, argv, MAX_ARGS);
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
command_func_t command = find_command(input_buffer);
|
if (argc == 0) continue;
|
||||||
|
|
||||||
|
command_func_t command = find_command(argv[0]);
|
||||||
if (command)
|
if (command)
|
||||||
{
|
{
|
||||||
command();
|
command(argc, argv);
|
||||||
} else {
|
} else {
|
||||||
printf("Unknown command %s\n", input_buffer);
|
printf("Unknown command %s\n", argv[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,4 +17,6 @@ typedef uint8_t bool;
|
|||||||
#define true 1
|
#define true 1
|
||||||
#define false 0
|
#define false 0
|
||||||
|
|
||||||
|
#define NULL ((void*)0)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
#include "stdint.h"
|
||||||
|
|
||||||
int strlen(const char* str)
|
int strlen(const char* str)
|
||||||
{
|
{
|
||||||
int len = 0;
|
int len = 0;
|
||||||
@@ -17,3 +19,52 @@ int strcmp(const char* str1, const char* str2)
|
|||||||
}
|
}
|
||||||
return *(const unsigned char*)str1 - *(const unsigned char*)str2;
|
return *(const unsigned char*)str1 - *(const unsigned char*)str2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char* strchr(const char* str, int c)
|
||||||
|
{
|
||||||
|
while (*str)
|
||||||
|
{
|
||||||
|
if (*str == (char)c)
|
||||||
|
{
|
||||||
|
return (char*)str;
|
||||||
|
}
|
||||||
|
str++;
|
||||||
|
}
|
||||||
|
if (c == '\0')
|
||||||
|
{
|
||||||
|
return (char*)str;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* strtok(char* str, const char* delimiter)
|
||||||
|
{
|
||||||
|
static char* last;
|
||||||
|
if (str)
|
||||||
|
{
|
||||||
|
last = str;
|
||||||
|
} else {
|
||||||
|
str = last;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!str || *str == '\0')
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* token_start = str;
|
||||||
|
while (*str && !strchr(delimiter, *str))
|
||||||
|
{
|
||||||
|
str++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*str)
|
||||||
|
{
|
||||||
|
*str = '\0';
|
||||||
|
last = str + 1;
|
||||||
|
} else {
|
||||||
|
last = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return token_start;
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,5 +3,6 @@
|
|||||||
|
|
||||||
int strlen(const char* str);
|
int strlen(const char* str);
|
||||||
int strcmp(const char* str1, const char* str2);
|
int strcmp(const char* str1, const char* str2);
|
||||||
|
char* strtok(char* str, const char* delimiter);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include "../libc/stdio.h"
|
#include "../libc/stdio.h"
|
||||||
#include "../kernel/system.h"
|
#include "../kernel/system.h"
|
||||||
|
#include "../libc/string.h"
|
||||||
|
|
||||||
// Print a rainbow colorful text for testing
|
// Print a rainbow colorful text for testing
|
||||||
|
|
||||||
@@ -44,10 +45,26 @@ void program_uptime()
|
|||||||
|
|
||||||
void program_help()
|
void program_help()
|
||||||
{
|
{
|
||||||
printf("help\tpanic\twords\tprimes\trainbow\tclear\nmath\tbf\tuptime\n");
|
printf("help\tpanic\twords\tprimes\trainbow\tclear\nmath\tbf\tuptime\techo\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Panic
|
||||||
|
|
||||||
void program_panic()
|
void program_panic()
|
||||||
{
|
{
|
||||||
asm volatile("int $0x13");
|
asm volatile("int $0x13");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Output input
|
||||||
|
|
||||||
|
void program_echo(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
for (int i=1; i<argc; i++)
|
||||||
|
{
|
||||||
|
puts(argv[i]);
|
||||||
|
if (i < argc-1) {
|
||||||
|
putc(' ');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
puts("\n");
|
||||||
|
}
|
||||||
|
|||||||
@@ -12,4 +12,5 @@ void program_clear();
|
|||||||
void program_uptime();
|
void program_uptime();
|
||||||
void program_panic();
|
void program_panic();
|
||||||
void program_help();
|
void program_help();
|
||||||
|
void program_echo();
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user