Modify: bf: add file optional argument
This commit is contained in:
@@ -69,9 +69,9 @@ Clears the screen by scrolling (screen height) times.
|
||||
|
||||
A math lexer & parser that can calculate simple arithmetic operations. Adding, subtracting, multiplying, dividing, and factoring are supported. (I plan to get support for trigonometric functions maybe)
|
||||
|
||||
#### `bf`
|
||||
#### `bf <optional: file>`
|
||||
|
||||
A brainfuck interpreter with every instruction and default tape size (30k cells).
|
||||
A brainfuck interpreter with every instruction and default tape size (30k cells). Takes an optional argument, the filename for a Brainfuck source file.
|
||||
|
||||
#### `uptime`
|
||||
|
||||
|
||||
1
src/initrd/hello.bf
Normal file
1
src/initrd/hello.bf
Normal file
@@ -0,0 +1 @@
|
||||
-[------->+<]>-.-[->+++++<]>++.+++++++..+++.[--->+<]>-----.---[->+++<]>.-[--->+<]>---.+++.------.--------.
|
||||
@@ -102,3 +102,33 @@ void cat_initrd(uint8_t* initrd, const char* filename)
|
||||
|
||||
printf("File '%s' not found\n", filename);
|
||||
}
|
||||
|
||||
int tar_file_to_buffer(uint8_t* initrd, const char* filename, char* buffer)
|
||||
{
|
||||
uint8_t* current_block = initrd;
|
||||
|
||||
while (1)
|
||||
{
|
||||
if (current_block[0] == '\0')
|
||||
{
|
||||
//puts("[tar] EOF\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char* file_name = (const char*)current_block;
|
||||
uint32_t file_size = tar_parse_size((const char*)(current_block+124));
|
||||
|
||||
if (strcmp(file_name, filename) == 0)
|
||||
{
|
||||
uint8_t* file_data = current_block + TAR_BLOCK_SIZE;
|
||||
memcpy(buffer, file_data, file_size);
|
||||
buffer[file_size] = '\0';
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t total_size = ((file_size + TAR_BLOCK_SIZE - 1) / TAR_BLOCK_SIZE) * TAR_BLOCK_SIZE;
|
||||
current_block += TAR_BLOCK_SIZE + total_size;
|
||||
}
|
||||
printf("[tar] file '%s' not found\n", filename);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -33,5 +33,6 @@ typedef struct
|
||||
void tar_find_file(uint8_t *tar_start, const char* filename);
|
||||
void ls_initrd(uint8_t* initrd);
|
||||
void cat_initrd(uint8_t* initrd, const char* filename);
|
||||
int tar_file_to_buffer(uint8_t* initrd, const char* filename, char* buffer);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -34,6 +34,18 @@ void *memmove(void* dest, const void* src, size_t n)
|
||||
return dest;
|
||||
}
|
||||
|
||||
void *memcpy(void* dest, const void* src, uint32_t n)
|
||||
{
|
||||
uint8_t* d = (uint8_t*)dest;
|
||||
const uint8_t* s = (const uint8_t*)src;
|
||||
|
||||
for (uint32_t i=0; i<n; i++)
|
||||
{
|
||||
d[i] = s[i];
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
void panic()
|
||||
{
|
||||
for (;;);
|
||||
|
||||
@@ -13,6 +13,7 @@ typedef int size_t;
|
||||
|
||||
void *memset(void *dest, char val, size_t count);
|
||||
void *memmove(void* dest, const void* src, size_t n);
|
||||
void *memcpy(void* dest, const void* src, uint32_t n);
|
||||
|
||||
struct regs
|
||||
{
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
|
||||
#include "../kernel/system.h"
|
||||
#include "../libc/stdio.h"
|
||||
#include "../kernel/kmain.h"
|
||||
#include "../kernel/initrd.h"
|
||||
|
||||
#define BUF_SIZE 256
|
||||
|
||||
@@ -48,12 +50,25 @@ void brainfuck(char* input)
|
||||
}
|
||||
}
|
||||
|
||||
void program_bf()
|
||||
void program_bf(int argc, char* argv[])
|
||||
{
|
||||
if (argc == 1)
|
||||
{
|
||||
char input_buffer[BUF_SIZE];
|
||||
puts("Brainfuck code? ");
|
||||
get_input(input_buffer, BUF_SIZE);
|
||||
brainfuck(input_buffer);
|
||||
//brainfuck(",[.[-],]");
|
||||
puts("\n");
|
||||
} else if (argc == 2) {
|
||||
// Read file content into buffer, then interpret it
|
||||
char input_buffer[BUF_SIZE];
|
||||
int read = tar_file_to_buffer((uint8_t*)initrd_addr, argv[1], input_buffer);
|
||||
if (read == 0)
|
||||
{
|
||||
brainfuck(input_buffer);
|
||||
puts("\n");
|
||||
} else {
|
||||
printf("Could not find file '%s'\n", argv[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user