-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsystem_native.c
More file actions
150 lines (127 loc) · 3.68 KB
/
system_native.c
File metadata and controls
150 lines (127 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// --------------------------------------------------
// Project: ProX Programming Language (ProXPL)
// Author: ProgrammerKR
// Created: 2025-12-16
// Copyright © 2025. ProXentix India Pvt. Ltd. All rights reserved.
/*
* ProXPL Standard Library - System Module
* Native C implementation of system functions
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h> // Required for time()
#include "common.h"
#include "vm.h"
#include "value.h"
#include "object.h"
#ifdef _WIN32
#include <windows.h>
#define popen _popen
#define pclose _pclose
#else
#include <unistd.h>
#endif
// exit(code) - Exit program
static Value native_exit(int argCount, Value* args) {
int code = 0;
if (argCount > 0 && IS_NUMBER(args[0])) {
code = (int)AS_NUMBER(args[0]);
}
exit(code);
return NIL_VAL; // Never reached, but satisfies compiler
}
// env(name) - Get environment variable
static Value native_env(int argCount, Value* args) {
if (argCount < 1 || !IS_STRING(args[0])) {
return NIL_VAL;
}
const char* name = AS_CSTRING(args[0]);
const char* value = getenv(name);
if (value) {
return OBJ_VAL(copyString(value, (int)strlen(value)));
}
return NIL_VAL;
}
// platform() - Get platform name
static Value native_platform(int argCount, Value* args) {
(void)argCount; (void)args;
#ifdef _WIN32
return OBJ_VAL(copyString("Windows", 7));
#elif __APPLE__
return OBJ_VAL(copyString("macOS", 5));
#elif __linux__
return OBJ_VAL(copyString("Linux", 5));
#else
return OBJ_VAL(copyString("Unknown", 7));
#endif
}
// version() - Get ProXPL version
static Value native_version(int argCount, Value* args) {
(void)argCount; (void)args;
return OBJ_VAL(copyString("ProXPL 1.0.0", 12));
}
// exec(command) - Execute shell command
static Value native_exec(int argCount, Value* args) {
if (argCount < 1 || !IS_STRING(args[0])) {
return NIL_VAL;
}
const char* command = AS_CSTRING(args[0]);
FILE* pipe = popen(command, "r");
if (!pipe) {
return NIL_VAL;
}
char buffer[4096];
char* result = (char*)malloc(1);
if (!result) {
pclose(pipe);
return NIL_VAL;
}
result[0] = '\0';
size_t totalLen = 0;
while (fgets(buffer, sizeof(buffer), pipe) != NULL) {
size_t len = strlen(buffer);
char* temp = (char*)realloc(result, totalLen + len + 1);
if (!temp) {
free(result);
pclose(pipe);
return NIL_VAL;
}
result = temp;
strcpy(result + totalLen, buffer);
totalLen += len;
}
pclose(pipe);
Value output = OBJ_VAL(copyString(result, (int)totalLen));
free(result);
return output;
}
// time() - Get current timestamp in seconds since epoch
static Value native_time(int argCount, Value* args) {
(void)argCount; (void)args;
time_t now = time(NULL);
return NUMBER_VAL((double)now);
}
// sleep(seconds) - Sleep for specified seconds
static Value native_sleep(int argCount, Value* args) {
if (argCount < 1 || !IS_NUMBER(args[0])) {
return NIL_VAL;
}
int seconds = (int)AS_NUMBER(args[0]);
#ifdef _WIN32
Sleep(seconds * 1000);
#else
sleep(seconds);
#endif
return NIL_VAL;
}
// Register all system functions with the VM
void register_system_natives(VM* pVM) {
defineNative(pVM, "exit", native_exit);
defineNative(pVM, "env", native_env);
defineNative(pVM, "platform", native_platform);
defineNative(pVM, "version", native_version);
defineNative(pVM, "exec", native_exec);
defineNative(pVM, "time", native_time);
defineNative(pVM, "sleep", native_sleep);
}