PatchworkOS
dbbdc99
A non-POSIX operating system.
Theme:
Default
Round
Robot
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
>
4
#include <
kernel/sched/thread.h
>
5
6
#include <
errno.h
>
7
#include <
sys/argsplit.h
>
8
9
static
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
42
uint64_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
}
buffer
EFI_PHYSICAL_ADDRESS buffer
Definition
main.c:237
ctl_dispatch_one
static uint64_t ctl_dispatch_one(ctl_t *ctls, file_t *file, uint64_t argc, const char **argv)
Definition
ctl.c:9
ctl_dispatch
uint64_t ctl_dispatch(ctl_t *ctls, file_t *file, const void *buffer, size_t count)
Definition
ctl.c:42
ctl.h
errno.h
CTL_MAX_BUFFER
#define CTL_MAX_BUFFER
Maximum size of the buffer used for argument parsing.
Definition
ctl.h:28
ENOENT
#define ENOENT
No such file or directory.
Definition
errno.h:42
EINVAL
#define EINVAL
Invalid argument.
Definition
errno.h:142
errno
#define errno
Error number variable.
Definition
errno.h:27
E2BIG
#define E2BIG
Argument list too long.
Definition
errno.h:67
argsplit_buf
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
NULL
#define NULL
Pointer error value.
Definition
NULL.h:25
ERR
#define ERR
Integer error value.
Definition
ERR.h:17
argsplit.h
pmm.h
count
static atomic_long count
Definition
main.c:11
uint64_t
__UINT64_TYPE__ uint64_t
Definition
stdint.h:17
uint8_t
__UINT8_TYPE__ uint8_t
Definition
stdint.h:11
strcmp
_PUBLIC int strcmp(const char *s1, const char *s2)
Definition
strcmp.c:3
ctl_t
Structure defining a ctl command.
Definition
ctl.h:70
ctl_t::func
ctl_func_t func
The function to call for the command.
Definition
ctl.h:72
ctl_t::argcMax
uint64_t argcMax
The maximum number of arguments accepted by func.
Definition
ctl.h:74
ctl_t::name
const char * name
The name of the command.
Definition
ctl.h:71
file_t
File structure.
Definition
file.h:39
thread.h
src
kernel
fs
ctl.c
Generated on Thu Mar 5 2026 15:27:54 for PatchworkOS by
1.9.8