Fun
0.41.5
The programming language that makes You have fun
Main Page
Data Structures
Files
File List
Globals
Loading...
Searching...
No Matches
src
vm
os
serial_recv.c
Go to the documentation of this file.
1
/*
2
* This file is part of the Fun programming language.
3
* https://fun-lang.xyz/
4
*
5
* Copyright 2025 Johannes Findeisen <you@hanez.org>
6
* Licensed under the terms of the Apache-2.0 license.
7
* https://opensource.org/license/apache-2-0
8
*/
9
21
22
#ifdef __unix__
23
#include <unistd.h>
24
#endif
25
26
case
OP_SERIAL_RECV
: {
27
/* Pops maxlen (int), fd (int); returns data (string) */
28
Value
maxv = pop_value(vm);
29
Value
fdv
= pop_value(vm);
30
char
*
out
= NULL;
31
#ifdef __unix__
32
if
(
fdv
.type !=
VAL_INT
|| maxv.
type
!=
VAL_INT
) {
33
fprintf
(stderr,
"Runtime type error: serial_recv expects (int fd, int maxlen)\n"
);
34
}
else
{
35
int
fd
= (int)
fdv
.i;
36
int
maxlen = (int)maxv.
i
;
37
if
(maxlen <= 0) maxlen = 4096;
38
if
(maxlen > 1 << 20) maxlen = 1 << 20;
/* cap at 1MB */
39
out
= (
char
*)malloc((
size_t
)maxlen + 1);
40
if
(
out
) {
41
ssize_t
n
= read(
fd
,
out
, (
size_t
)maxlen);
42
if
(
n
<= 0) {
43
free
(
out
);
44
out
= NULL;
45
}
else
{
46
out
[
n
] =
'\0'
;
47
}
48
}
49
}
50
#endif
51
free_value
(maxv);
52
free_value
(
fdv
);
53
Value
s
=
make_string
(
out
?
out
:
""
);
54
if
(
out
)
free
(
out
);
55
push_value(vm,
s
);
56
break
;
57
}
out
Value out
Definition
apop.c:38
OP_SERIAL_RECV
@ OP_SERIAL_RECV
Definition
bytecode.h:244
fdv
Value fdv
Definition
fd_poll_read.c:25
n
int n
Definition
insert.c:41
free
free(vals)
s
uint32_t s
Definition
rol.c:31
fd
int fd
Definition
serial_open.c:92
free_value
free_value(maxv)
Value
Tagged union representing a Fun value.
Definition
value.h:68
Value::i
int64_t i
Definition
value.h:71
Value::type
ValueType type
Definition
value.h:69
make_string
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
Definition
value.c:95
VAL_INT
@ VAL_INT
Definition
value.h:51
fprintf
#define fprintf
Definition
vm.c:200
Generated on
for Fun by
1.16.1