diff --git a/CMakeLists.txt b/CMakeLists.txt index a0af996..3b382a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ endif() add_subdirectory(src) add_subdirectory(boot) +add_subdirectory(kernel) add_custom_target( run diff --git a/boot/CMakeLists.txt b/boot/CMakeLists.txt index b0cbd4d..4efd6c1 100644 --- a/boot/CMakeLists.txt +++ b/boot/CMakeLists.txt @@ -1,15 +1,3 @@ -add_custom_command( - OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/boot.bin - DEPENDS asm ${CMAKE_CURRENT_SOURCE_DIR}/boot.vc6asm - COMMAND asm ${CMAKE_CURRENT_SOURCE_DIR}/boot.vc6asm -o ${CMAKE_CURRENT_BINARY_DIR}/boot.bin -M - DEPFILE ${CMAKE_CURRENT_BINARY_DIR}/boot.bin.d - VERBATIM -) - -add_custom_target( - boot.bin.target - ALL - DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/boot.bin -) +add_vc6asm_bin_target(boot.bin boot.vc6asm) diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt new file mode 100644 index 0000000..a96a409 --- /dev/null +++ b/kernel/CMakeLists.txt @@ -0,0 +1,3 @@ + +add_vc6asm_bin_target(kernel.bin kernel.vc6asm) + diff --git a/kernel/kernel.vc6asm b/kernel/kernel.vc6asm new file mode 100644 index 0000000..bf47511 --- /dev/null +++ b/kernel/kernel.vc6asm @@ -0,0 +1,84 @@ +@include "../boot/arch.vc6asm" +@include "../boot/macros.vc6asm" + +@org 0x0100 + +main: + mov r1, 'V' + store [VCD_BEGIN], r1 + mov r1, 'C' + store [VCD_BEGIN + 1], r1 + mov r1, '6' + store [VCD_BEGIN + 2], r1 + + mov bp, 0x1000 + mov sp, bp + + mov r1, text + mov r2, VCD_BEGIN + 80 + call println + + mov r1, prompt + mov r2, VCD_BEGIN + 120 + mov r0, ip + call println + + mov r0, keydown_handler + store [KBD_KEYDOWN_HANDLER], r0 + mov r1, 0 + sti +.spin: + hlt + jr .spin + +keydown_handler: + load r0, [KBD_SCANCODE] + add r0, 93 + mov r2, VCD_BEGIN + 40 * 3 + 2 + add r2, r1 + store [r2], r0 + add r1, 1 + sti + ret + +println: + push bp + mov bp, sp + sub sp, 2 + + store [sp], r1 + stea r0, r2, sp, 1 + + mov r1, 0 + jr .l0_cond +.l0_body: + ldea r3, sp, 1 + add r3, r1 + load r2, [sp] + add r2, r1 + load r2, [r2] + store [r3], r2 + + add r1, 1 +.l0_cond: + load r2, [sp] + add r2, r1 + load r2, [r2] + clf + lr r3, .l0_body + cmp r2, '\0' + not fl + and fl, FL_EQ + cmov ip, r3 + +.leave: + mov sp, bp + pop bp + ret + +text: + dw "hello world\0" +prompt: + dw "> \0" + + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e3c2d5a..233b25f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -45,5 +45,25 @@ target_link_libraries(fs PRIVATE opts) add_executable(fscli fscli.c) target_link_libraries(fscli PRIVATE opts fs) +add_executable(fstool fstool.c) +target_link_libraries(fstool PRIVATE opts fs) + +function(add_vc6asm_bin_target TARGET_NAME SOURCE_FILE) + add_custom_command( + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME} + COMMAND asm ${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE_FILE} -o ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME} -M + DEPENDS asm ${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE_FILE} + DEPFILE ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}.d + VERBATIM + ) + + add_custom_target( + ${TARGET_NAME}.target + ALL + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME} + ) +endfunction() + + diff --git a/src/asm.c b/src/asm.c index 3ed836f..c414377 100644 --- a/src/asm.c +++ b/src/asm.c @@ -275,98 +275,65 @@ char *read_text_file(const char *filename) return text; } +#define TOK_LIST \ + X(Eof, ) \ + X(Eol, ) \ + X(Ident, ) \ + X(Int, ) \ + X(Hex, ) \ + X(Bin, ) \ + X(Char, ) \ + X(Str, ) \ + X(LtLt, ) \ + X(GtGt, ) \ + X(R0, ) \ + X(R1, ) \ + X(R2, ) \ + X(R3, ) \ + X(Sp, ) \ + X(Bp, ) \ + X(Fl, ) \ + X(Ip, ) \ + X(Const, ) \ + X(Macro, ) \ + X(End, ) \ + X(Include, ) \ + X(Guard, ) \ + X(Org, ) \ + X(MacroArg, ) \ + X(Exclam, = '!') \ + X(Dollar, = '$') \ + X(Percent, = '%') \ + X(Ampersand, = '&') \ + X(LParen, = '(') \ + X(RParen, = ')') \ + X(Asterisk, = '*') \ + X(Plus, = '+') \ + X(Comma, = ',') \ + X(Minus, = '-') \ + X(Dot, = '.') \ + X(Slash, = '/') \ + X(Colon, = ':') \ + X(At, = '@') \ + X(LBracket, = '[') \ + X(RBracket, = ']') \ + X(Caret, = '^') \ + X(Pipe, = '|') + typedef enum { - Tok_Eof, - Tok_Eol, - Tok_Ident, - Tok_Int, - Tok_Hex, - Tok_Bin, - Tok_Char, - Tok_Str, - Tok_LtLt, - Tok_GtGt, - Tok_R0, - Tok_R1, - Tok_R2, - Tok_R3, - Tok_Sp, - Tok_Bp, - Tok_Fl, - Tok_Ip, - Tok_Const, - Tok_Macro, - Tok_End, - Tok_Include, - Tok_Guard, - Tok_MacroArg, - Tok_Exclam = '!', - Tok_Dollar = '$', - Tok_Percent = '%', - Tok_Ampersand = '&', - Tok_LParen = '(', - Tok_RParen = ')', - Tok_Asterisk = '*', - Tok_Plus = '+', - Tok_Comma = ',', - Tok_Minus = '-', - Tok_Dot = '.', - Tok_Slash = '/', - Tok_Colon = ':', - Tok_At = '@', - Tok_LBracket = '[', - Tok_RBracket = ']', - Tok_Caret = '^', - Tok_Pipe = '|', +#define X(NAME, VAL) Tok_##NAME VAL, + TOK_LIST +#undef X } TokTy; const char *tokty_str(TokTy ty) { switch (ty) { - /* clang-format off */ - case Tok_Eof: return "Eof"; - case Tok_Eol: return "Eol"; - case Tok_Ident: return "Ident"; - case Tok_Int: return "Int"; - case Tok_Hex: return "Hex"; - case Tok_Bin: return "Bin"; - case Tok_Char: return "Char"; - case Tok_Str: return "Str"; - case Tok_LtLt: return "LtLt"; - case Tok_GtGt: return "GtGt"; - case Tok_R0: return "R0"; - case Tok_R1: return "R1"; - case Tok_R2: return "R2"; - case Tok_R3: return "R3"; - case Tok_Sp: return "Sp"; - case Tok_Bp: return "Bp"; - case Tok_Fl: return "Fl"; - case Tok_Ip: return "Ip"; - case Tok_Const: return "Const"; - case Tok_Macro: return "Macro"; - case Tok_End: return "End"; - case Tok_Include: return "Include"; - case Tok_Guard: return "Guard"; - case Tok_MacroArg: return "MacroArg"; - case Tok_Exclam: return "Exclam"; - case Tok_Dollar: return "Dollar"; - case Tok_Percent: return "Percent"; - case Tok_Ampersand: return "Ampersand"; - case Tok_LParen: return "LParen"; - case Tok_RParen: return "RParen"; - case Tok_Asterisk: return "Asterisk"; - case Tok_Plus: return "Plus"; - case Tok_Comma: return "Comma"; - case Tok_Minus: return "Minus"; - case Tok_Dot: return "Dot"; - case Tok_Slash: return "Slash"; - case Tok_Colon: return "Colon"; - case Tok_At: return "At"; - case Tok_LBracket: return "LBracket"; - case Tok_RBracket: return "RBracket"; - case Tok_Caret: return "Caret"; - case Tok_Pipe: return "Pipe"; - /* clang-format on */ +#define X(NAME, VAL) \ + case Tok_##NAME: \ + return #NAME; + TOK_LIST +#undef X } assert(false); } @@ -419,6 +386,7 @@ static TokTy l_ident_ty(const char *text, size_t len) "end", "include", "guard", + "org", }; TokTy tys[] = { Tok_R0, @@ -434,6 +402,7 @@ static TokTy l_ident_ty(const char *text, size_t len) Tok_End, Tok_Include, Tok_Guard, + Tok_Org, }; constexpr size_t kws_size = sizeof(kws) / sizeof(kws[0]); @@ -674,6 +643,7 @@ typedef struct { Map dep_str_map; FILE *dep_file; int directive_depth; + uint16_t org_offset; bool second_pass; bool print; } Assembler; @@ -993,7 +963,7 @@ static int64_t a_parse_operand(Assembler *a) } if (a->tok.ty == '$') { a_step(a); - return a->ip; + return a->ip + a->org_offset; } if (a->tok.ty == '(') { a_step(a); @@ -1236,6 +1206,16 @@ static void a_parse_directive_guard(Assembler *a) free(ident); } +static void a_parse_directive_org(Assembler *a) +{ + a_step(a); + int64_t offset = a_parse_expr(a); + a_assert_eol(a); + + assert(offset >= 0 && offset <= UINT16_MAX); + a->org_offset = (uint16_t)offset; +} + static void a_parse_directive(Assembler *a) { assert(a->tok.ty == '@'); @@ -1254,6 +1234,8 @@ static void a_parse_directive(Assembler *a) a_assert_eol(a); } else if (a->tok.ty == Tok_Guard) { a_parse_directive_guard(a); + } else if (a->tok.ty == Tok_Org) { + a_parse_directive_org(a); } else { print_error_exit(a->tok.loc, "malformed directive"); } @@ -1553,7 +1535,7 @@ static void a_parse_label_or_ins(Assembler *a) a_step(a); char *ident = strndup(ident_tok.text, ident_tok.len); - a_sym_define(a, ident, ident_tok.loc, a->ip); + a_sym_define(a, ident, ident_tok.loc, a->ip + a->org_offset); free(ident); a->labels_counter += 1; @@ -1582,7 +1564,7 @@ static void a_parse_local_label(Assembler *a) char *ident = strndup(ident_tok.text, ident_tok.len); char *unique_ident = alloc_sprintf(".%s_%d", ident, a->current_label); - a_sym_define(a, unique_ident, ident_tok.loc, a->ip); + a_sym_define(a, unique_ident, ident_tok.loc, a->ip + a->org_offset); free(ident); free(unique_ident); diff --git a/src/fs.c b/src/fs.c index 31db264..e377f0f 100644 --- a/src/fs.c +++ b/src/fs.c @@ -24,11 +24,6 @@ constexpr size_t direentry_max_size = 64; constexpr size_t inode_ndir_blocks_count = inode_blocks_cap - 1; constexpr size_t inode_ind_block_idx = inode_ndir_blocks_count; -typedef enum : uint16_t { - INode_File, - INode_Dir, -} INodeTy; - typedef struct { uint16_t inode; uint16_t next; @@ -234,6 +229,15 @@ static void make_dir_with_inode_ids( .blocks = { }, }; + DirEntry self_entry = { + .inode = inode_id, + .next = 0, + .name_len = 1, + .name = { '.' }, + }; + size_t self_entry_size = direntry_size(&self_entry); + self_entry.next = self_entry_size; + DirEntry parent_entry = { .inode = parent_inode_id, .next = 0, @@ -243,8 +247,10 @@ static void make_dir_with_inode_ids( uint16_t entry_block_id = inode_allocate_block(fs, out_inode); Block block = { }; - memcpy( - block, &parent_entry, direntry_size(&parent_entry) * sizeof(uint16_t)); + memcpy(block, &self_entry, self_entry_size * sizeof(uint16_t)); + memcpy(&block[self_entry_size], + &parent_entry, + direntry_size(&parent_entry) * sizeof(uint16_t)); write_block(fs, block, entry_block_id); out_inode->last_block_size = direntry_size(&parent_entry); @@ -339,6 +345,7 @@ void fs_init(Fs *fs, Disk *disk, uint16_t block_offset, uint16_t blocks_count) }; fs->sb = (Superblock *)fs->superblock; + fs->sb->magic = superblock_magic; bitmap_set(*fs->block_bitmap, 0, true); bitmap_set(*fs->block_bitmap, 1, true); @@ -350,6 +357,27 @@ void fs_init(Fs *fs, Disk *disk, uint16_t block_offset, uint16_t blocks_count) make_dir_with_inode_ids(fs, &root_inode, root_inode_id, root_inode_id); } +void fs_load(Fs *fs, Disk *disk, uint16_t block_offset, uint16_t blocks_count) +{ + assert(blocks_count <= sizeof(Block) * 8); + *fs = (Fs) { + .disk = disk, + .block_offset = block_offset, + .blocks_count = blocks_count, + .superblock = calloc(1, sizeof(Block)), + .sb = nullptr, + .block_bitmap = calloc(1, sizeof(Block)), + .out_of_sync = false, + }; + + fs->sb = (Superblock *)fs->superblock; + + read_block(fs, *fs->superblock, 0); + read_block(fs, *fs->block_bitmap, 1); + + assert(fs->sb->magic == superblock_magic); +} + void fs_destroy(Fs *fs) { free(fs->superblock); @@ -483,7 +511,7 @@ void fs_remove(Fs *fs, INode *inode, INode *parent) struct list_block_entries_fn_args { Fs *fs; - ListEntry *entries; + FsListEntry *entries; size_t entries_cap; size_t *entries_size; }; @@ -507,7 +535,7 @@ static void list_block_entries_fn(uint16_t block_id, void *arg) &block[entry.next], MIN(direentry_max_size, block_size - entry.next)); - ListEntry list_entry = { + FsListEntry list_entry = { .inode = entry.inode, .name = { }, }; @@ -519,7 +547,7 @@ static void list_block_entries_fn(uint16_t block_id, void *arg) } size_t fs_list( - Fs *fs, ListEntry *entries, size_t entries_cap, const INode *inode) + Fs *fs, FsListEntry *entries, size_t entries_cap, const INode *inode) { size_t entries_size = 0; struct list_block_entries_fn_args args @@ -564,9 +592,14 @@ uint16_t fs_read(Fs *fs, for (; block_idx < inode->blocks_count && i < buffer_size; ++block_idx) { Block block; read_block(fs, block, *inode_block_at(inode, block_idx, ind_block)); - uint16_t chunk_size - = MIN((size_t)buffer_size - i, block_size - block_offset); - memcpy(&buffer[i], &block[block_offset], chunk_size); + + uint16_t chunk_size = buffer_size - i; + chunk_size = MIN((size_t)chunk_size, block_size - block_offset); + if (block_idx == inode->blocks_count - 1) + chunk_size = MIN(chunk_size, inode->last_block_size); + + memcpy(&buffer[i], &block[block_offset], chunk_size * sizeof(uint16_t)); + block_offset = 0; i += chunk_size; } @@ -583,6 +616,7 @@ void fs_dump_info(Fs *fs) void fs_dump_superblock(Fs *fs) { Superblock *sb = fs->sb; + printf("magic = 0x%4x\n", sb->magic); printf("blocks_count = %u\n", sb->blocks_count); printf("inode_blocks_count = %u\n", sb->inodeblocks_count); printf("inode_blocks = ["); diff --git a/src/fs.h b/src/fs.h index 80e56bf..8946b67 100644 --- a/src/fs.h +++ b/src/fs.h @@ -6,7 +6,7 @@ constexpr size_t block_size = 512; typedef uint16_t Block[block_size]; -constexpr size_t size_of_block = sizeof(Block) / sizeof(uint16_t); +constexpr size_t size_of_block = sizeof(Block); typedef struct { Block *blocks; @@ -22,6 +22,11 @@ constexpr size_t inodes_cap = inodeblocks_cap * inodes_per_block; constexpr size_t inodebitmap_size = inodes_cap / 16; constexpr size_t inode_name_cap = 61; +typedef enum : uint16_t { + INode_File, + INode_Dir, +} INodeTy; + typedef struct { uint16_t id; uint16_t mode; @@ -32,7 +37,10 @@ typedef struct { static_assert(sizeof(INode) / sizeof(uint16_t) == inode_size); +constexpr uint16_t superblock_magic = 0x5a5a; + typedef struct { + uint16_t magic; uint16_t blocks_count; uint16_t inodeblocks_count; uint16_t inodeblocks[inodeblocks_cap]; @@ -52,6 +60,7 @@ typedef struct { } Fs; void fs_init(Fs *fs, Disk *disk, uint16_t block_offset, uint16_t blocks_count); +void fs_load(Fs *fs, Disk *disk, uint16_t block_offset, uint16_t blocks_count); void fs_destroy(Fs *fs); void fs_sync(Fs *fs); void fs_root(Fs *fs, INode *out_inode); @@ -63,10 +72,10 @@ void fs_remove(Fs *fs, INode *inode, INode *parent); typedef struct { uint16_t inode; char name[inode_name_cap + 1]; -} ListEntry; +} FsListEntry; size_t fs_list( - Fs *fs, ListEntry *entries, size_t entries_cap, const INode *inode); + Fs *fs, FsListEntry *entries, size_t entries_cap, const INode *inode); uint16_t fs_write( Fs *fs, INode *inode, const uint16_t *buffer, uint16_t buffer_size); diff --git a/src/fscli.c b/src/fscli.c index 2ddf9dc..83043ac 100644 --- a/src/fscli.c +++ b/src/fscli.c @@ -1,4 +1,5 @@ #include "fs.h" +#include #include #include #include @@ -6,8 +7,9 @@ int main(int argc, char *argv[]) { (void)argc, (void)argv; - // assert(argc >= 2); - // const char *output_filename = argv[1]; + const char *filename = nullptr; + if (argc >= 2) + filename = argv[1]; constexpr size_t amount_of_blocks = 128; @@ -19,8 +21,19 @@ int main(int argc, char *argv[]) Fs fs_val; Fs *fs = &fs_val; - fs_init(fs, &disk, 0, amount_of_blocks); - fs_sync(fs); + if (filename) { + FILE *file = fopen(filename, "r"); + assert(file); + + fread( + disk.blocks, sizeof(uint16_t), block_size * amount_of_blocks, file); + fclose(file); + + fs_load(fs, &disk, 0, amount_of_blocks); + } else { + fs_init(fs, &disk, 0, amount_of_blocks); + fs_sync(fs); + } while (true) { char command[32]; @@ -47,6 +60,7 @@ int main(int argc, char *argv[]) "ls i list entries in inode[i]\n" "w i write to inode[i]\n" "r i read from inode[i]\n" + "save save to disk file\n" ""); } else if (strcmp(command, "fs") == 0) { fs_dump_info(fs); @@ -122,12 +136,12 @@ int main(int argc, char *argv[]) goto malformed_command; INode inode; fs_inode(fs, &inode, inode_id); - ListEntry entries[32]; + FsListEntry entries[32]; size_t entries_size = fs_list( fs, entries, sizeof(entries) / sizeof(entries[0]), &inode); fs_sync(fs); for (size_t i = 0; i < entries_size; ++i) - printf("%s\n", entries[i].name); + printf("%2u %s\n", entries[i].inode, entries[i].name); } else if (strcmp(command, "w") == 0) { uint16_t inode_id; if (scanf(" %hu", &inode_id) != 1) @@ -149,10 +163,22 @@ int main(int argc, char *argv[]) goto malformed_command; INode inode; fs_inode(fs, &inode, inode_id); - uint16_t buffer[64] = { 0 }; - size_t buffer_size = fs_read(fs, &inode, buffer, 64, 0); + uint16_t *buffer = calloc(65535, sizeof(uint16_t)); + size_t buffer_size = fs_read(fs, &inode, buffer, 65535, 0); + printf("buffer_size = %zu\n", buffer_size); for (size_t i = 0; i < buffer_size; ++i) fputc(buffer[i], stdout); + free(buffer); + } else if (strcmp(command, "save") == 0) { + fs_sync(fs); + FILE *file = fopen(filename, "w"); + assert(file); + size_t words_written = fwrite(disk.blocks, + sizeof(uint16_t), + block_size * amount_of_blocks, + file); + fclose(file); + printf("wrote %zu words to %s\n", words_written, filename); } else { printf("error: unknown command '%s'\n", command); } diff --git a/src/fstool.c b/src/fstool.c new file mode 100644 index 0000000..95d299c --- /dev/null +++ b/src/fstool.c @@ -0,0 +1,251 @@ +#include "fs.h" +#include +#include +#include +#include +#include + +constexpr size_t amount_of_blocks = 128; + +static void load(Fs *fs, Disk *disk, const char *filename) +{ + *disk = (Disk) { + .blocks = calloc(amount_of_blocks, sizeof(Block)), + .blocks_count = amount_of_blocks, + }; + + FILE *file = fopen(filename, "r"); + if (file) { + fread(disk->blocks, + sizeof(uint16_t), + block_size * amount_of_blocks, + file); + fclose(file); + + fs_load(fs, disk, 0, amount_of_blocks); + } else { + printf( + "warning: could not find '%s'. one will be created.\n", filename); + fs_init(fs, disk, 0, amount_of_blocks); + fs_sync(fs); + } +} + +static void save(Fs *fs, Disk *disk, const char *filename) +{ + fs_sync(fs); + + FILE *file = fopen(filename, "w"); + assert(file); + size_t words_written = fwrite( + disk->blocks, sizeof(uint16_t), block_size * amount_of_blocks, file); + fclose(file); + printf("wrote %zu words to %s\n", words_written, filename); + + fs_destroy(fs); + free(disk->blocks); +} + +static void close_without_saving(Fs *fs, Disk *disk) +{ + fs_destroy(fs); + free(disk->blocks); +} + +static uint16_t search_entries( + FsListEntry entries[], size_t entries_size, const char *name) +{ + for (size_t i = 0; i < entries_size; ++i) + if (strcmp(entries[i].name, name) == 0) + return entries[i].inode; + return 0; +} + +static void resolve_path(Fs *fs, INode *inode, const char *path) +{ + assert(path[0] == '/'); + char *path_copy = strdup(path); + fs_root(fs, inode); + + char *tok = strtok(path_copy, "/"); + while (tok) { + constexpr size_t entries_cap = 16; + FsListEntry entries[entries_cap]; + + size_t entries_size = fs_list(fs, entries, entries_cap, inode); + assert(entries_size < entries_cap); + + uint16_t inode_id = search_entries(entries, entries_size, tok); + assert(inode_id && "not found"); + + fs_inode(fs, inode, inode_id); + + tok = strtok(nullptr, "/"); + } + free(path_copy); +} + +char *help = "" + "h help\n" + "mkdir / create dir with name at path\n" + "mkfile / create file with name at path\n" + "ls print contents of dir\n" + "import copy host file to fs\n" + "importb copy host file to fs, u8 -> u16\n" + ""; + +int main(int argc, char *argv[]) +{ + assert(argc >= 3); + const char *disk_filename = argv[1]; + const char *cmd = argv[2]; + + Disk disk; + Fs fs_val, *fs = &fs_val; + + if (strcmp(cmd, "h") == 0) { + printf("%s", help); + } else if (strcmp(cmd, "mkdir") == 0) { + if (argc < 4) + goto malformed_command; + const char *path = argv[3]; + + load(fs, &disk, disk_filename); + + char *path_copy = strdup(path); + char *name = strrchr(path_copy, '/'); + *name = '\0'; + name += 1; + + INode parent; + resolve_path(fs, &parent, path_copy); + + printf("creating directory '%s' in %d.\n", name, parent.id); + INode inode; + fs_make_dir(fs, &inode, &parent, name); + + free(path_copy); + save(fs, &disk, disk_filename); + } else if (strcmp(cmd, "mkfile") == 0) { + if (argc < 4) + goto malformed_command; + const char *path = argv[3]; + + load(fs, &disk, disk_filename); + + char *path_copy = strdup(path); + char *name = strrchr(path_copy, '/'); + *name = '\0'; + name += 1; + + INode parent; + resolve_path(fs, &parent, path_copy); + + printf("creating file %s in %s.\n", name, path_copy); + INode inode; + fs_make_file(fs, &inode, &parent, name); + + free(path_copy); + save(fs, &disk, disk_filename); + } else if (strcmp(cmd, "ls") == 0) { + if (argc < 4) + goto malformed_command; + const char *path = argv[3]; + + load(fs, &disk, disk_filename); + + INode parent; + resolve_path(fs, &parent, path); + + constexpr size_t entries_cap = 16; + FsListEntry entries[entries_cap]; + + size_t entries_size = fs_list(fs, entries, entries_cap, &parent); + for (size_t i = 0; i < entries_size; ++i) { + INode inode; + fs_inode(fs, &inode, entries[i].inode); + bool is_dir = i >= 2 && inode.mode == INode_Dir; + printf("%s%s\n", entries[i].name, is_dir ? "/" : ""); + } + + close_without_saving(fs, &disk); + } else if (strcmp(cmd, "import") == 0) { + if (argc < 5) + goto malformed_command; + const char *rpath = argv[3]; + const char *vpath = argv[4]; + + load(fs, &disk, disk_filename); + + INode inode; + resolve_path(fs, &inode, vpath); + + FILE *file = fopen(rpath, "r"); + assert(file); + fseek(file, 0, SEEK_END); + long file_size = ftell(file); + assert(file_size > 0); + fseek(file, 0, SEEK_SET); + + size_t buffer_size = (file_size + 1) / 2; + uint16_t *buffer = calloc(buffer_size, sizeof(uint16_t)); + size_t words_read = fread(buffer, sizeof(uint16_t), buffer_size, file); + printf("file_size = %zu, words_read = %zu, buffer_size = %zu\n", + file_size, + words_read, + buffer_size); + assert(words_read == buffer_size); + fclose(file); + + fs_write(fs, &inode, buffer, buffer_size); + + printf("wrote %zu words to file %s.\n", words_read, vpath); + free(buffer); + save(fs, &disk, disk_filename); + } else if (strcmp(cmd, "importb") == 0) { + if (argc < 5) + goto malformed_command; + const char *rpath = argv[3]; + const char *vpath = argv[4]; + + load(fs, &disk, disk_filename); + + INode inode; + resolve_path(fs, &inode, vpath); + + FILE *file = fopen(rpath, "r"); + assert(file); + fseek(file, 0, SEEK_END); + long file_size = ftell(file); + assert(file_size > 0); + fseek(file, 0, SEEK_SET); + + size_t buffer_size = file_size; + uint16_t *buffer = calloc(buffer_size, sizeof(uint16_t)); + size_t words_read = 0; + + int ch; + while ((ch = fgetc(file)) != EOF) { + buffer[words_read] = ch; + words_read += 1; + } + + assert(words_read == buffer_size); + fclose(file); + + fs_write(fs, &inode, buffer, buffer_size); + + printf("wrote %zu words to file %s.\n", words_read, vpath); + free(buffer); + save(fs, &disk, disk_filename); + } else { + printf("error: uknown command '%s'\n", cmd); + return 1; + } + + return 0; + +malformed_command: + printf("error: malformed %s command\n", cmd); + return 1; +} diff --git a/vim/syntax/vc6asm.vim b/vim/syntax/vc6asm.vim index 13d77e7..6e62104 100644 --- a/vim/syntax/vc6asm.vim +++ b/vim/syntax/vc6asm.vim @@ -32,6 +32,7 @@ syn match vcPreProc '@if' syn match vcPreProc '@elif' syn match vcPreProc '@else' syn match vcPreProc '@guard' +syn match vcPreProc '@org' syn match vcNumber '0' syn match vcNumber '[1-9][0-9]*'