PatchworkOS  3984a1d
A non-POSIX operating system.
Loading...
Searching...
No Matches
builtin.c
Go to the documentation of this file.
1#include "builtin.h"
2
3#include <errno.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <sys/defs.h>
8#include <sys/io.h>
9#include <sys/proc.h>
10
11static uint64_t builtin_cd(uint64_t argc, const char** argv);
12static uint64_t builtin_help(uint64_t argc, const char** argv);
13static uint64_t builtin_clear(uint64_t argc, const char** argv);
14
15static builtin_t builtins[] = {
16 {
17 .name = "cd",
18 .callback = builtin_cd,
19 .description = "Change the current working directory.",
20 .usage = "cd [directory]",
21 },
22 {
23 .name = "help",
24 .callback = builtin_help,
25 .description = "Show this help message.",
26 .usage = "help",
27 },
28 {
29 .name = "clear",
30 .callback = builtin_clear,
31 .description = "Clear the terminal screen.",
32 .usage = "clear",
33 },
34};
35
36static uint64_t builtin_cd(uint64_t argc, const char** argv)
37{
38 if (argc < 2)
39 {
40 chdir("/home");
41 return 0;
42 }
43
44 if (chdir(argv[1]) == ERR)
45 {
46 fprintf(stderr, "cd: %s\n", strerror(errno));
47 return ERR;
48 }
49
50 return 0;
51}
52
53static uint64_t builtin_help(uint64_t argc, const char** argv)
54{
55 UNUSED(argc);
56 UNUSED(argv);
57
58 printf("\033[33mBUILTINS:\033[0m\n");
59 for (uint64_t i = 0; i < ARRAY_SIZE(builtins); i++)
60 {
61 printf(" %-20s\033[0m \t\033[90m%s\033[0m\n", builtins[i].usage, builtins[i].description);
62 }
63
64 printf("\n\033[33mFEATURES:\033[0m\n");
65 printf(" command1 | command2\t\033[90mPipe output\033[0m\n");
66 printf(" command > file \t\033[90mRedirect standard output\033[0m\n");
67 printf(" command < file \t\033[90mRedirect standard input\033[0m\n");
68 printf(" command 2> file \t\033[90mRedirect standard error\033[0m\n");
69 printf("\n\033[33mKEYBINDINGS:\033[0m\n");
70 printf(" Back/Left/Right \t\033[90mEdit input\033[0m\n");
71 printf(" Up/Down \t\033[90mNavigate history\033[0m\n");
72 printf(" Ctrl+C \t\033[90mTerminate process\033[0m\n");
73
74 printf("\n\033[90mExternal commands are executed from paths in the PATH environment variable.\033[0m\n");
75
76 return 0;
77}
78
79static uint64_t builtin_clear(uint64_t argc, const char** argv)
80{
81 UNUSED(argc);
82 UNUSED(argv);
83
84 printf("\033[2J\033[H");
85 return 0;
86}
87
88bool builtin_exists(const char* name)
89{
90 for (uint64_t i = 0; i < ARRAY_SIZE(builtins); i++)
91 {
92 if (strcmp(name, builtins[i].name) == 0)
93 {
94 return true;
95 }
96 }
97
98 return false;
99}
100
101uint64_t builtin_execute(uint64_t argc, const char** argv)
102{
103 if (argc == 0)
104 {
105 return 0;
106 }
107
108 for (uint64_t i = 0; i < ARRAY_SIZE(builtins); i++)
109 {
110 if (strcmp(argv[0], builtins[i].name) == 0)
111 {
112 builtins[i].callback(argc, argv);
113 return 0;
114 }
115 }
116
117 return ERR;
118}
int64_t y
Definition main.c:153
static uint64_t builtin_help(uint64_t argc, const char **argv)
Definition builtin.c:53
uint64_t builtin_execute(uint64_t argc, const char **argv)
Definition builtin.c:101
static builtin_t builtins[]
Definition builtin.c:15
static uint64_t builtin_clear(uint64_t argc, const char **argv)
Definition builtin.c:79
static uint64_t builtin_cd(uint64_t argc, const char **argv)
Definition builtin.c:36
bool builtin_exists(const char *name)
Definition builtin.c:88
#define errno
Error number variable.
Definition errno.h:27
#define ARRAY_SIZE(x)
Get the number of elements in a static array.
Definition defs.h:108
#define UNUSED(x)
Mark a variable as unused.
Definition defs.h:100
uint64_t chdir(const char *path)
System call for changing the cwd.
Definition chdir.c:8
#define ERR
Integer error value.
Definition ERR.h:17
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
_PUBLIC int printf(const char *_RESTRICT format,...)
Definition printf.c:3
FILE * stderr
Definition std_streams.c:19
_PUBLIC int fprintf(FILE *_RESTRICT stream, const char *_RESTRICT format,...)
Definition fprintf.c:3
_PUBLIC char * strerror(int errnum)
Definition strerror.c:6
_PUBLIC int strcmp(const char *s1, const char *s2)
Definition strcmp.c:3
const char * name
Definition builtin.h:8
uint64_t(* callback)(uint64_t argc, const char **argv)
Definition builtin.h:11