SayoriOS 0.3.3

elk_libs.c
1 #include "portability.h"
3 #include <io/ports.h>
4
5
6 #include "io/tty.h"
7 #include "lib/libstring/string.h"
8 #include "../libvector/include/vector.h"
9 #include "lib/stdio.h"
10
11
12 #include "elk_config.h"
13 #include "elk.h"
14
15
16
17 void jse_file_config(const char *cfg, const char *file){
18 size_t minStack = JSE_MIN_STACK;
19 size_t bufferSize = 0;
20 size_t stack = JSE_MIN_STACK;
21 bool jse_p_no_warn_stack = false;
22 size_t param_size = -1;
23 char* out = kcalloc(1,sizeof(char) * minStack + bufferSize);
24 string_t* str = string_from_charptr(cfg);
25 vector_t* vec = string_split(str, "\n");
26
27 for(int i = 0; i < vec->size; i++) {
28 param_size++;
29 string_t* str_param = (string_t*)(vec->data[i]);
30 vector_t* param = string_split(str_param, " ");
31 //qemu_log("[VEC] [%d/%d] [C:%d] Str: %s", i, vec->size, param->size, str_param->data);
32
33 if (param->size < 2) continue;
34
35 jse_trim(((string_t*)(param->data[0]))->data);
36 jse_trim(((string_t*)(param->data[1]))->data);
37 //qemu_note("Key: '%s' | Value: '%s'", ((string_t*)(param->data[0]))->data, ((string_t*)(param->data[1]))->data);
38 if (strcmpn(((string_t*)(param->data[0]))->data,"//#include")){
39 char* fn_buf = kcalloc(1,sizeof(char) * (strlen(JSE_LIBS_PATH) + strlen(((string_t*)(param->data[1]))->data) + 1 ) );
40
41 memcpy(fn_buf, JSE_LIBS_PATH, strlen(JSE_LIBS_PATH));
42 memcpy(fn_buf + strlen(JSE_LIBS_PATH), (((string_t*)(param->data[1]))->data), strlen(((string_t*)(param->data[1]))->data));
43
44 size_t lastInx = strlen(JSE_LIBS_PATH) + strlen(((string_t*)(param->data[1]))->data);
45
46 fn_buf[lastInx] = 0;
47
48 FILE* file = fopen(fn_buf, "r");
49 if (!file){
50 qemu_err("[JSE] [Include] File '%s' no found!", fn_buf);
51 kfree(fn_buf);
52 fclose(file);
53 } else {
54
55 size_t filesize = fsize(file);
56 bufferSize += filesize;
57
58 char* buf = kcalloc(1,sizeof(char) * (filesize+1));
59 fread(file, 1, filesize, buf);
60
61 out = jse_mergeBuffers(out, buf, strlen(out), filesize);
62
63 fclose(file);
64 kfree(buf);
65 kfree(fn_buf);
66
67 qemu_ok(" [JSE] [Include] File '%s' is loaded!", fn_buf);
68
69
70 }
71 } else if (param->size <= 3 && strcmpn(((string_t*)(param->data[0]))->data,"//#pragma")) {
72 jse_trim(((string_t*)(param->data[2]))->data);
73 qemu_note(" [JSE] [Pragma] key: '%s' value: '%s'", ((string_t*)(param->data[1]))->data, ((string_t*)(param->data[2]))->data);
74 if (strcmpn(((string_t*)(param->data[1]))->data,"stack")){
75 int stackValue = jse_func_atoi(((string_t*)(param->data[2]))->data);
76 if (stackValue < stack){
77 if (!jse_p_no_warn_stack) qemu_warn(" [JSE] [WARN] The stack size has been reset to the minimum size by the installed JSE.");
78 continue;
79 } else if (stackValue > (strlen(file) + bufferSize) * 5) {
80 if (!jse_p_no_warn_stack) qemu_warn(" [JSE] [WARN] You have set the stack value to less than the recommended value (%d), errors may appear during script execution.", (strlen(file) + bufferSize) * 5);
81 }
82 stack = stackValue;
83 continue;
84 } else if (strcmpn(((string_t*)(param->data[1]))->data,"no-warn-stack")){
85 int value = jse_func_atoi(((string_t*)(param->data[2]))->data);
86 jse_p_no_warn_stack = (value == 1?true:false);
87 continue;
88 }
89 } else {
90 qemu_err("[JSE] Unknown argv (%s)", ((string_t*)(param->data[0]))->data);
91 continue;
92 }
93 //printf("%s\n", ((string_t*)(vec->data[i]))->data);
94 }
95
96
97 string_split_free(vec);
98 string_destroy(str);
99
100 out = jse_mergeBuffers(out, file, strlen(out), strlen(file));
101 qemu_note(" [JSE] Stack size: %d", stack);
102 stack += sizeof(struct js) + 1;
103
105
106
107 char* js_mem = kcalloc(1,stack);
108 struct js *js = js_create(js_mem, (stack));
109
110 elk_setup(js);
111
112 js->incSize = bufferSize;
113 js->paramSize = param_size;
114
115 //qemu_note("[Buffer PRE] [%d] \n=============\n%s\n=============\n", strlen(out), out);
116
117 //jse_checkBuffer(out);
118
119 //qemu_note("[Buffer POST] [%d] \n=============\n%s\n=============\n", strlen(out), out);
120
121
122 jsval_t result = js_eval(js, out, strlen(out));
123
124 elk_destroy(js);
125
126 if (js->isFatal){
127 qemu_err("\n[JSE] Runtime Fatal Error!\n\r Message: %s\n", js_str(js, result));
128 } else {
129 printf("[JSE] Result: %s\n",js_str(js, result));
130 }
131
132 js_statsInfo(js);
133
134
135 kfree(out);
136 kfree(js_mem);
137 //kfree(file);
138 //return (js->isFatal == 1?0:1);
139
140 //qemu_note("Buffer: \n%s", out);
141 }
142
143 void jse_file_preconfig(const char *buffer, const char *first_search, const char *second_search) {
144 const char *start = buffer;
145 const char *first_occurrence = jse_strstr(start, first_search);
146
147 if (first_occurrence != NULL) {
148 start = first_occurrence + strlen(first_search);
149 const char *second_occurrence = jse_strstr(start, second_search);
150
151 if (second_occurrence != NULL) {
152 int content_length = second_occurrence - start;
153
154 int fileoff = content_length + strlen(first_search) + strlen(second_search);
155
156 void* config = calloc(sizeof(char) * (content_length + 1), 1);
157 void* file = calloc(sizeof(char) * (strlen(buffer) - content_length), 1);
158
159 jse_ncpy((char*) config, (char*) start, content_length);
160 jse_ncpy((char*) file, (char*) buffer + fileoff, (strlen(buffer) - content_length));
161
162 jse_file_config((char*) config, (char*) file);
163
164 return;
165 }
166 }
167
168 qemu_note("[JSE] The JSE configuration block was not found in the file, so JSE executes in normal mode and with standard settings.");
169 elk_eval(buffer);
170 }
171
172 void jse_file_getBuff(char* buf){
173 const char *sc1 = "//<#JSE#";
174 const char *sc2 = "//#JSE#>";
175
176 jse_file_preconfig(buf, sc1, sc2);
177 }
config
uint32_t config
Корректировка
Definition: beeper.c:14
filesize
size_t filesize(const char *Path)
[FileIO] Возвращает размер указанного файла
Definition: fileio.c:67
strlen
size_t strlen(const char *str)
Возращает длину строки
Definition: string.c:88
strcmpn
bool strcmpn(const char *str1, const char *str2)
Сравнение строк
Definition: string.c:270
memcpy
void * memcpy(void *restrict destination, const void *restrict source, size_t n)
Копирование непересекающихся массивов используя SSE.
Definition: string.c:173
fclose
void fclose(FILE *stream)
Закончить работу с файлом
Definition: stdio.c:213
fopen
FILE * fopen(const char *filename, const char *_mode)
Открывает файл
Definition: stdio.c:166
fsize
int fsize(FILE *stream)
Получение размера файла в байтах
Definition: stdio.c:227
fread
int fread(FILE *stream, size_t count, size_t size, void *buffer)
Чтение файла
Definition: stdio.c:250
FILE
Структура файла. Требуется для работы с VFS.
Definition: stdio.h:21
js
Definition: elk.h:48
string
Definition: string.h:10
vector
Definition: vector.h:7