PatchworkOS  966e257
A non-POSIX operating system.
Loading...
Searching...
No Matches
ctl.c
Go to the documentation of this file.
1#include <kernel/fs/ctl.h>
2
3#include <kernel/mem/pmm.h>
5
6#include <errno.h>
7#include <sys/argsplit.h>
8
9uint64_t ctl_dispatch_one(ctl_t* ctls, file_t* file, uint64_t argc, const char** argv)
10{
11 if (ctls == NULL || file == NULL || argv == NULL || argc == 0)
12 {
13 errno = EINVAL;
14 return ERR;
15 }
16
17 ctl_t* ctl = &ctls[0];
18 while (ctl->name != NULL)
19 {
20 if (strcmp(ctl->name, argv[0]) == 0)
21 {
22 if (argc < ctl->argcMin || argc > ctl->argcMax)
23 {
24 errno = EINVAL;
25 return ERR;
26 }
27
28 if (ctl->func(file, argc, argv) == ERR)
29 {
30 return ERR;
31 }
32 return 0;
33 }
34
35 ctl++;
36 }
37
38 errno = ENOENT;
39 return ERR;
40}
41
43{
44 if (ctls == NULL || file == NULL || buffer == NULL || count == 0)
45 {
46 errno = EINVAL;
47 return ERR;
48 }
49
50 if (count > MAX_PATH)
51 {
52 errno = E2BIG;
53 return ERR;
54 }
55
56 uint8_t argBuffer[MAX_PATH];
57
58 uint64_t argc;
59 const char** argv = argsplit_buf(argBuffer, MAX_PATH, buffer, count, &argc);
60 if (argv == NULL)
61 {
62 return ERR;
63 }
64 if (argc == 0)
65 {
66 errno = ENOENT;
67 return ERR;
68 }
69
70 for (uint64_t i = 0; i < argc; i++)
71 {
72 if (strcmp(argv[i], "&&") == 0)
73 {
74 uint64_t res = ctl_dispatch_one(ctls, file, i, argv);
75 if (res == ERR)
76 {
77 return ERR;
78 }
79
80 argc -= (i + 1);
81 argv += (i + 1);
82 i = -1;
83 }
84 }
85
86 if (ctl_dispatch_one(ctls, file, argc, argv) == ERR)
87 {
88 return ERR;
89 }
90 return count;
91}
#define MAX_PATH
Maximum length of filepaths.
Definition MAX_PATH.h:11
uint64_t ctl_dispatch(ctl_t *ctls, file_t *file, const void *buffer, uint64_t count)
Definition ctl.c:42
uint64_t ctl_dispatch_one(ctl_t *ctls, file_t *file, uint64_t argc, const char **argv)
Definition ctl.c:9
#define ENOENT
No such file or directory.
Definition errno.h:42
#define EINVAL
Invalid argument.
Definition errno.h:142
#define errno
Error number variable.
Definition errno.h:27
#define E2BIG
Argument list too long.
Definition errno.h:67
const char ** argsplit_buf(void *buf, uint64_t size, const char *str, uint64_t maxLen, uint64_t *count)
Standardized argument parsing function using a provided buffer.
Definition argsplit_buf.c:3
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
static dentry_t * file
Definition log_file.c:22
EFI_PHYSICAL_ADDRESS buffer
Definition mem.c:15
static atomic_long count
Definition main.c:10
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
_PUBLIC int strcmp(const char *s1, const char *s2)
Definition strcmp.c:3
Structure defining a ctl command.
Definition ctl.h:65
ctl_func_t func
The function to call for the command.
Definition ctl.h:67
uint64_t argcMax
The maximum number of arguments accepted by func.
Definition ctl.h:69
const char * name
The name of the command.
Definition ctl.h:66
File structure.
Definition file.h:39