PatchworkOS  dbbdc99
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
9static uint64_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
42uint64_t ctl_dispatch(ctl_t* ctls, file_t* file, const void* buffer, size_t count)
43{
44 if (ctls == NULL || file == NULL || buffer == NULL || count == 0)
45 {
46 errno = EINVAL;
47 return ERR;
48 }
49
50 uint8_t argBuffer[CTL_MAX_BUFFER];
51
52 uint64_t argc;
53 const char** argv = argsplit_buf(argBuffer, CTL_MAX_BUFFER, buffer, count, &argc);
54 if (argv == NULL)
55 {
56 errno = E2BIG;
57 return ERR;
58 }
59 if (argc == 0)
60 {
61 errno = ENOENT;
62 return ERR;
63 }
64
65 for (uint64_t i = 0; i < argc; i++)
66 {
67 if (strcmp(argv[i], "&&") == 0)
68 {
69 uint64_t res = ctl_dispatch_one(ctls, file, i, argv);
70 if (res == ERR)
71 {
72 return ERR;
73 }
74
75 argc -= (i + 1);
76 argv += (i + 1);
77 i = -1;
78 }
79 }
80
81 if (ctl_dispatch_one(ctls, file, argc, argv) == ERR)
82 {
83 return ERR;
84 }
85 return count;
86}
EFI_PHYSICAL_ADDRESS buffer
Definition main.c:237
static uint64_t ctl_dispatch_one(ctl_t *ctls, file_t *file, uint64_t argc, const char **argv)
Definition ctl.c:9
uint64_t ctl_dispatch(ctl_t *ctls, file_t *file, const void *buffer, size_t count)
Definition ctl.c:42
#define CTL_MAX_BUFFER
Maximum size of the buffer used for argument parsing.
Definition ctl.h:28
#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:25
#define ERR
Integer error value.
Definition ERR.h:17
static atomic_long count
Definition main.c:11
__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:70
ctl_func_t func
The function to call for the command.
Definition ctl.h:72
uint64_t argcMax
The maximum number of arguments accepted by func.
Definition ctl.h:74
const char * name
The name of the command.
Definition ctl.h:71
File structure.
Definition file.h:39