Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
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
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);
54 if (out) free(out);
55 push_value(vm, s);
56 break;
57}
Value out
Definition apop.c:38
@ OP_SERIAL_RECV
Definition bytecode.h:244
Value fdv
int n
Definition insert.c:41
free(vals)
uint32_t s
Definition rol.c:31
int fd
Definition serial_open.c:92
free_value(maxv)
Tagged union representing a Fun value.
Definition value.h:68
int64_t i
Definition value.h:71
ValueType type
Definition value.h:69
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
Definition value.c:95
@ VAL_INT
Definition value.h:51
#define fprintf
Definition vm.c:200