diff --git a/src/apps/CMakeLists.txt b/src/apps/CMakeLists.txt index 68ddbee1..afbd1eb3 100644 --- a/src/apps/CMakeLists.txt +++ b/src/apps/CMakeLists.txt @@ -33,12 +33,15 @@ add_subdirectory(cinit) # Programs loaded from disk add_subdirectory(foo) add_subdirectory(bar) -add_subdirectory(init) +add_subdirectory(cat) add_subdirectory(demo) +add_subdirectory(echo) +add_subdirectory(init) add_subdirectory(ping) add_subdirectory(pong) add_subdirectory(shell) -add_subdirectory(getpid) +add_subdirectory(sleep) +add_subdirectory(time) message("App targets are ${APPS_TARGETS}") diff --git a/src/apps/getpid/CMakeLists.txt b/src/apps/cat/CMakeLists.txt similarity index 51% rename from src/apps/getpid/CMakeLists.txt rename to src/apps/cat/CMakeLists.txt index 444d0d09..16594cfa 100644 --- a/src/apps/getpid/CMakeLists.txt +++ b/src/apps/cat/CMakeLists.txt @@ -1,3 +1,3 @@ -set(TARGET getpid) +set(TARGET cat) add_app(${TARGET}) diff --git a/src/apps/getpid/link.ld b/src/apps/cat/link.ld similarity index 100% rename from src/apps/getpid/link.ld rename to src/apps/cat/link.ld diff --git a/src/apps/cat/src/cat.c b/src/apps/cat/src/cat.c new file mode 100644 index 00000000..6dcd2507 --- /dev/null +++ b/src/apps/cat/src/cat.c @@ -0,0 +1,21 @@ +#include + +#include "libc/stdio.h" + +int main(size_t argc, char ** argv) { + if (argc < 2) { + printf("Usage: %s \n", argv[0]); + return -1; + } + + file_t * file = file_open(argv[1], "r"); + + char c; + while (file_read(file, 1, 1, &c)) { + putc(c); + } + + file_close(file); + + return 0; +} diff --git a/src/apps/cinit/src/cinit.c b/src/apps/cinit/src/cinit.c index 2c601456..74de0dbb 100644 --- a/src/apps/cinit/src/cinit.c +++ b/src/apps/cinit/src/cinit.c @@ -4,11 +4,11 @@ extern int main(size_t argc, char ** argv); void __cinit(size_t argc, char ** argv) { - printf("c init\n"); + // printf("c init\n"); // TODO init malloc // TODO is there anything in signals or system calls to setup? // TODO do stdio handles setup here? int res = main(argc, argv); - printf("Main returned %d\n", res); + // printf("Main returned %d\n", res); proc_exit(res); } diff --git a/src/apps/echo/CMakeLists.txt b/src/apps/echo/CMakeLists.txt new file mode 100644 index 00000000..bd1f5099 --- /dev/null +++ b/src/apps/echo/CMakeLists.txt @@ -0,0 +1,3 @@ +set(TARGET echo) + +add_app(${TARGET}) diff --git a/src/apps/echo/link.ld b/src/apps/echo/link.ld new file mode 100644 index 00000000..9ac34e30 --- /dev/null +++ b/src/apps/echo/link.ld @@ -0,0 +1,29 @@ +ENTRY(__start) + +SECTIONS { + . = 0x400000; + + .text : + { + *(.text) + } + + /* Read-only data. */ + .rodata BLOCK(4K) : ALIGN(4K) + { + *(.rodata) + } + + /* Read-write data (initialized) */ + .data BLOCK(4K) : ALIGN(4K) + { + *(.data) + } + + /* Read-write data (uninitialized) and stack */ + .bss BLOCK(4K) : ALIGN(4K) + { + *(COMMON) + *(.bss) + } +} \ No newline at end of file diff --git a/src/apps/echo/src/echo.c b/src/apps/echo/src/echo.c new file mode 100644 index 00000000..b119017f --- /dev/null +++ b/src/apps/echo/src/echo.c @@ -0,0 +1,28 @@ +#include + +#include "libc/stdio.h" +#include "libc/string.h" + +int main(size_t argc, char ** argv) { + bool next_line = true; + if (argc > 1 && kmemcmp(argv[1], "-n", 2) == 0) { + next_line = false; + } + + size_t i = 1; + if (!next_line) { + i++; + } + for (; i < argc; i++) { + puts(argv[i]); + if (i < argc) { + putc(' '); + } + } + + if (next_line) { + putc('\n'); + } + + return 0; +} diff --git a/src/apps/getpid/src/getpid.c b/src/apps/getpid/src/getpid.c deleted file mode 100644 index e02eadc8..00000000 --- a/src/apps/getpid/src/getpid.c +++ /dev/null @@ -1,7 +0,0 @@ -#include "libc/proc.h" -#include "libc/stdio.h" - -void main() { - int pid = getpid(); - printf("PID is %u\n"); -} diff --git a/src/apps/shell/src/commands.c b/src/apps/shell/src/commands.c index 71d63f34..4e42a8e5 100644 --- a/src/apps/shell/src/commands.c +++ b/src/apps/shell/src/commands.c @@ -8,30 +8,6 @@ #include "libc/time.h" #include "shell.h" -static int echo_cmd(size_t argc, char ** argv) { - bool next_line = true; - if (argc > 1 && kmemcmp(argv[1], "-n", 2) == 0) { - next_line = false; - } - - size_t i = 1; - if (!next_line) { - i++; - } - for (; i < argc; i++) { - puts(argv[i]); - if (i < argc) { - putc(' '); - } - } - - if (next_line) { - putc('\n'); - } - - return 0; -} - static int ls_cmd(size_t argc, char ** argv) { // dir_t dir = dir_open("/"); // if (!dir) { @@ -64,74 +40,12 @@ static int ls_cmd(size_t argc, char ** argv) { return 0; } -static int cat_cmd(size_t argc, char ** argv) { - if (argc < 2) { - printf("Usage: %s \n", argv[0]); - return -1; - } - - file_t * file = file_open(argv[1], "r"); - - char c; - while (file_read(file, 1, 1, &c)) { - putc(c); - } - - file_close(file); - - return 0; -} - static int pid_cmd(size_t argc, char ** argv) { printf("PID is %d\n", getpid()); return 0; } -static int sleep_cmd(size_t argc, char ** argv) { - if (argc < 2 || (!kstrcmp(argv[1], "-u") && argc < 3)) { - printf("Usage: %s [-u] \n", argv[0]); - puts("\nseconds must be at while number that is least 1\n"); - puts("-u use microseconds instead of seconds\n"); - return -1; - } - - if (!kstrcmp(argv[1], "-u")) { - int us = katoi(argv[2]); - if (us < 1) { - printf("Seconds must be a numer that is at least 1\n"); - return -1; - } - - printf("Sleeping for %d microseconds\n", us); - usleep(us); - } - else { - int seconds = katoi(argv[1]); - if (seconds < 1) { - printf("Seconds must be a numer that is at least 1\n"); - return -1; - } - - printf("Sleeping for %d sseconds\n", seconds); - sleep(seconds * 1000); - } - - puts("Finished sleep\n"); - - return 0; -} - -static int time_cmd(size_t argc, char ** argv) { - printf("The time is %u seconds\n", time()); - - return 0; -} - void init_commands() { - term_command_add("echo", echo_cmd); term_command_add("ls", ls_cmd); - term_command_add("cat", cat_cmd); term_command_add("pid", pid_cmd); - term_command_add("sleep", sleep_cmd); - term_command_add("time", time_cmd); } diff --git a/src/apps/shell/src/shell.c b/src/apps/shell/src/shell.c index 8fb26547..96cb61e3 100644 --- a/src/apps/shell/src/shell.c +++ b/src/apps/shell/src/shell.c @@ -329,14 +329,12 @@ static void exec_buff() { // No match was found else { - int pid = proc_open(argv[0], argc, argv); + // kernel adds filename at argv[0], so here it's a duplicate + int pid = proc_open(argv[0], argc - 1, &argv[1]); if (pid < 0) { printf("Unknown command '%s'\n", argv[0]); term_last_ret = 1; } - else { - printf("Running command %u\n", pid); - } } // Free parsed args diff --git a/src/apps/sleep/CMakeLists.txt b/src/apps/sleep/CMakeLists.txt new file mode 100644 index 00000000..f4d40785 --- /dev/null +++ b/src/apps/sleep/CMakeLists.txt @@ -0,0 +1,3 @@ +set(TARGET sleep) + +add_app(${TARGET}) diff --git a/src/apps/sleep/link.ld b/src/apps/sleep/link.ld new file mode 100644 index 00000000..9ac34e30 --- /dev/null +++ b/src/apps/sleep/link.ld @@ -0,0 +1,29 @@ +ENTRY(__start) + +SECTIONS { + . = 0x400000; + + .text : + { + *(.text) + } + + /* Read-only data. */ + .rodata BLOCK(4K) : ALIGN(4K) + { + *(.rodata) + } + + /* Read-write data (initialized) */ + .data BLOCK(4K) : ALIGN(4K) + { + *(.data) + } + + /* Read-write data (uninitialized) and stack */ + .bss BLOCK(4K) : ALIGN(4K) + { + *(COMMON) + *(.bss) + } +} \ No newline at end of file diff --git a/src/apps/sleep/src/sleep.c b/src/apps/sleep/src/sleep.c new file mode 100644 index 00000000..c87e15ee --- /dev/null +++ b/src/apps/sleep/src/sleep.c @@ -0,0 +1,39 @@ +#include + +#include "libc/stdio.h" +#include "libc/string.h" +#include "libc/time.h" + +int main(size_t argc, char ** argv) { + if (argc < 2 || (!kstrcmp(argv[1], "-u") && argc < 3)) { + printf("Usage: %s [-u] \n", argv[0]); + puts("\nseconds must be at while number that is least 1\n"); + puts("-u use microseconds instead of seconds\n"); + return -1; + } + + if (!kstrcmp(argv[1], "-u")) { + int us = katoi(argv[2]); + if (us < 1) { + printf("Seconds must be a numer that is at least 1\n"); + return -1; + } + + printf("Sleeping for %d microseconds\n", us); + usleep(us); + } + else { + int seconds = katoi(argv[1]); + if (seconds < 1) { + printf("Seconds must be a numer that is at least 1\n"); + return -1; + } + + printf("Sleeping for %d sseconds\n", seconds); + sleep(seconds * 1000); + } + + puts("Finished sleep\n"); + + return 0; +} diff --git a/src/apps/time/CMakeLists.txt b/src/apps/time/CMakeLists.txt new file mode 100644 index 00000000..44700299 --- /dev/null +++ b/src/apps/time/CMakeLists.txt @@ -0,0 +1,3 @@ +set(TARGET time) + +add_app(${TARGET}) diff --git a/src/apps/time/link.ld b/src/apps/time/link.ld new file mode 100644 index 00000000..9ac34e30 --- /dev/null +++ b/src/apps/time/link.ld @@ -0,0 +1,29 @@ +ENTRY(__start) + +SECTIONS { + . = 0x400000; + + .text : + { + *(.text) + } + + /* Read-only data. */ + .rodata BLOCK(4K) : ALIGN(4K) + { + *(.rodata) + } + + /* Read-write data (initialized) */ + .data BLOCK(4K) : ALIGN(4K) + { + *(.data) + } + + /* Read-write data (uninitialized) and stack */ + .bss BLOCK(4K) : ALIGN(4K) + { + *(COMMON) + *(.bss) + } +} \ No newline at end of file diff --git a/src/apps/time/src/time.c b/src/apps/time/src/time.c new file mode 100644 index 00000000..494c91a4 --- /dev/null +++ b/src/apps/time/src/time.c @@ -0,0 +1,11 @@ +#include "libc/time.h" + +#include + +#include "libc/stdio.h" + +int main(size_t argc, char ** argv) { + printf("The time is %u seconds\n", time()); + + return 0; +}