Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
proc_system.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
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
10/**
11 * @file proc_system.c
12 * @brief Implements OP_PROC_SYSTEM to execute a shell command and return exit code.
13 *
14 * Behavior:
15 * - Pops command (string); executes it using system(3); pushes the process exit code (int) or -1 on failure.
16 *
17 * Errors:
18 * - If command is not a string or cannot be executed, returns -1.
19 */
20
22 /* Pops command string; pushes exit code number */
23 Value cmdv = pop_value(vm);
24 char *cmd = value_to_string_alloc(&cmdv);
26 if (!cmd) {
27 push_value(vm, make_int(-1));
28 break;
29 }
30 int status = system(cmd);
31 int code = -1;
32#ifdef __unix__
33 if (status == -1)
34 code = -1;
35 else if (WIFEXITED(status))
36 code = WEXITSTATUS(status);
37 else
38 code = -1;
39#else
40 code = status;
41#endif
44 break;
45}
@ OP_PROC_SYSTEM
Definition bytecode.h:143
char * cmd
Definition proc_run.c:33
free_value(cmdv)
free(cmd)
int status
Definition proc_system.c:30
push_value(vm, make_int(code))
int code
Definition proc_system.c:31
Tagged union representing a Fun value.
Definition value.h:68
char * value_to_string_alloc(const Value *v)
Allocate a printable C string for a Value.
Definition value.c:641
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.
Definition vm.c:580