PatchworkOS
3984a1d
A non-POSIX operating system.
Theme:
Default
Round
Robot
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1
#include "
interactive.h
"
2
#include "
pipeline.h
"
3
4
#include <
errno.h
>
5
#include <
stdbool.h
>
6
#include <
stdio.h
>
7
#include <
stdlib.h
>
8
#include <
string.h
>
9
#include <
sys/io.h
>
10
#include <
sys/proc.h
>
11
12
static
uint64_t
cmdline_read
(
char
*
buffer
,
uint64_t
size)
13
{
14
uint64_t
index = 0;
15
buffer
[0] =
'\0'
;
16
while
(1)
17
{
18
char
chr;
19
if
(
read
(
STDIN_FILENO
, &chr, 1) == 0)
20
{
21
return
ERR
;
22
}
23
else
if
(chr ==
'\n'
)
24
{
25
buffer
[index] =
'\0'
;
26
return
0;
27
}
28
if
(index + 1 < size)
29
{
30
buffer
[index++] = chr;
31
}
32
}
33
}
34
35
static
void
join_args
(
char
*
buffer
,
uint64_t
size,
int
argc,
char
* argv[])
36
{
37
buffer
[0] =
'\0'
;
38
uint64_t
pos
= 0;
39
40
for
(
int
i
= 1;
i
< argc &&
pos
< size - 1;
i
++)
41
{
42
if
(
i
> 1 &&
pos
< size - 1)
43
{
44
buffer
[
pos
++] =
' '
;
45
}
46
47
char
* arg = argv[
i
];
48
while
(*arg &&
pos
< size - 1)
49
{
50
buffer
[
pos
++] = *arg++;
51
}
52
}
53
54
buffer
[
pos
] =
'\0'
;
55
}
56
57
void
execute_command
(
const
char
* cmdline)
58
{
59
pipeline_t
pipeline
;
60
if
(
pipeline_init
(&
pipeline
, cmdline,
STDIN_FILENO
,
STDOUT_FILENO
,
STDERR_FILENO
) ==
ERR
)
61
{
62
_exit
(
F
(
"shell: failed to initialize pipeline (%s)\n"
,
strerror
(
errno
)));
63
}
64
65
pipeline_execute
(&
pipeline
);
66
pipeline_wait
(&
pipeline
);
67
68
_exit
(
pipeline
.status);
69
}
70
71
int
main
(
int
argc,
char
* argv[])
72
{
73
if
(argc > 1)
74
{
75
char
cmdline[
MAX_PATH
];
76
join_args
(cmdline,
MAX_PATH
, argc, argv);
77
execute_command
(cmdline);
78
}
79
80
interactive_shell
();
81
82
return
EXIT_FAILURE
;
// Should never reach here
83
}
MAX_PATH
#define MAX_PATH
Maximum length of filepaths.
Definition
MAX_PATH.h:11
main
int main(void)
Definition
main.c:5
y
int64_t y
Definition
main.c:153
errno.h
errno
#define errno
Error number variable.
Definition
errno.h:27
F
#define F(format,...)
Allocates a formatted string on the stack.
Definition
io.h:66
read
size_t read(fd_t fd, void *buffer, size_t count)
System call for reading from files.
Definition
read.c:8
STDOUT_FILENO
#define STDOUT_FILENO
Standard output file descriptor.
Definition
io.h:35
STDERR_FILENO
#define STDERR_FILENO
Standard error file descriptor.
Definition
io.h:36
STDIN_FILENO
#define STDIN_FILENO
Standard input file descriptor.
Definition
io.h:34
_exit
_NORETURN void _exit(const char *status)
System call that handles pending notes for the current thread.
Definition
_exit.c:7
ERR
#define ERR
Integer error value.
Definition
ERR.h:17
pos
static uint64_t pos
Definition
interactive.c:21
interactive_shell
void interactive_shell(void)
Definition
interactive.c:230
interactive.h
io.h
buffer
EFI_PHYSICAL_ADDRESS buffer
Definition
mem.c:15
pipeline_wait
void pipeline_wait(pipeline_t *pipeline)
Definition
pipeline.c:435
pipeline_init
uint64_t pipeline_init(pipeline_t *pipeline, const char *cmdline, fd_t stdin, fd_t stdout, fd_t stderr)
Definition
pipeline.c:13
pipeline_execute
void pipeline_execute(pipeline_t *pipeline)
Definition
pipeline.c:427
pipeline.h
proc.h
join_args
static void join_args(char *buffer, uint64_t size, int argc, char *argv[])
Definition
main.c:35
cmdline_read
static uint64_t cmdline_read(char *buffer, uint64_t size)
Definition
main.c:12
execute_command
void execute_command(const char *cmdline)
Definition
main.c:57
stdbool.h
uint64_t
__UINT64_TYPE__ uint64_t
Definition
stdint.h:17
stdio.h
stdlib.h
EXIT_FAILURE
#define EXIT_FAILURE
Definition
stdlib.h:47
string.h
strerror
_PUBLIC char * strerror(int errnum)
Definition
strerror.c:6
pipeline_t
Definition
pipeline.h:21
src
programs
core
shell
main.c
Generated on Sat Jan 10 2026 00:03:15 for PatchworkOS by
1.9.8