Skip to content

Commit 5111bc0

Browse files
committed
Added custom function for seperating arguments, which specializes for this instead of modifying arrays members unwillingly. Also cleaned the code for registering the variable in lua to match.
1 parent 827436f commit 5111bc0

File tree

3 files changed

+49
-32
lines changed

3 files changed

+49
-32
lines changed

src/console.c

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,22 @@ int start_protective_mode(lua_CFunction func, const char* file, char** parameter
162162
return EXIT_SUCCESS;
163163
}
164164

165+
// returns a malloc'd string with each split item being seperated by \0
166+
char* strsplit(const char* str1, const char lookout, size_t len, size_t max) {
167+
char* cpy = malloc(len);
168+
memcpy(cpy, str1, len);
169+
170+
for (size_t i=0; i<len-1; i++) {
171+
if(str1[i] == lookout) {
172+
cpy[i] = '\0';
173+
max--;
174+
}
175+
if(max == 0)
176+
break;
177+
}
178+
return cpy;
179+
}
180+
165181
// handles arguments, cwd, loads necessary data, executes lua
166182
int main(int argc, char* argv[])
167183
{
@@ -264,27 +280,18 @@ int main(int argc, char* argv[])
264280
return EXIT_FAILURE;
265281
}
266282

267-
// initiate global variables set up, needs solidifed
283+
// initiate global variables set up
268284
if(globals != 0) {
269-
// this is a hack, need to switch to strtok that doesn't modify original string
270285
for (size_t i=0; i<globals; i++) {
271-
size_t len = strlen(globals_argv[i] + 2);
272-
char* cpy = calloc(len + 1, 1);
273-
memcpy(cpy, globals_argv[i] + 2, len + 1);
274-
275-
char* temp;
276-
char* left;
277-
char* right;
278-
279-
left = strtok_r(globals_argv[i] + 2, "=", &temp);
280-
right = strtok_r(NULL, "\0", &temp);
286+
char* globals_D_offset = globals_argv[i] + 2;
281287

282-
// TODO: check if argument has no =, therefore invalid and error
288+
char* arg1 = strsplit(globals_D_offset, '=', strlen(globals_D_offset) + 1, 2);
289+
char* arg2 = arg1 + (strlen(arg1) + 1);
283290

284-
lua_pushlstring(L, right, strlen(right));
285-
lua_setglobal(L, left);
291+
lua_pushlstring(L, arg2, strlen(arg2));
292+
lua_setglobal(L, arg1);
286293

287-
free(cpy);
294+
free(arg1);
288295
}
289296
free(globals_argv);
290297
}

src/consolew.c

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,22 @@ int start_protective_mode(lua_CFunction func, const char* file, char** parameter
242242
return EXIT_SUCCESS;
243243
}
244244

245+
// returns a malloc'd string with each split item being seperated by \0
246+
char* strsplit(const char* str1, const char lookout, size_t len, size_t max) {
247+
char* cpy = malloc(len);
248+
memcpy(cpy, str1, len);
249+
250+
for (size_t i=0; i<len-1; i++) {
251+
if(str1[i] == lookout) {
252+
cpy[i] = '\0';
253+
max--;
254+
}
255+
if(max == 0)
256+
break;
257+
}
258+
return cpy;
259+
}
260+
245261
// handles arguments, cwd, loads necessary data, executes lua
246262
int main(int argc, char* argv[])
247263
{
@@ -349,27 +365,18 @@ int main(int argc, char* argv[])
349365
return EXIT_FAILURE;
350366
}
351367

352-
// initiate global variables set up, needs solidifed
368+
// initiate global variables set up
353369
if(globals != 0) {
354-
// this is a hack, need to switch to strtok that doesn't modify original string
355370
for (size_t i=0; i<globals; i++) {
356-
size_t len = strlen(globals_argv[i] + 2);
357-
char* cpy = calloc(len + 1, 1);
358-
memcpy(cpy, globals_argv[i] + 2, len + 1);
359-
360-
char* temp;
361-
char* left;
362-
char* right;
363-
364-
left = strtok_r(globals_argv[i] + 2, "=", &temp);
365-
right = strtok_r(NULL, "\0", &temp);
371+
char* globals_D_offset = globals_argv[i] + 2;
366372

367-
// TODO: check if argument has no =, therefore invalid and error
373+
char* arg1 = strsplit(globals_D_offset, '=', strlen(globals_D_offset) + 1, 2);
374+
char* arg2 = arg1 + (strlen(arg1) + 1);
368375

369-
lua_pushlstring(L, right, strlen(right));
370-
lua_setglobal(L, left);
376+
lua_pushlstring(L, arg2, strlen(arg2));
377+
lua_setglobal(L, arg1);
371378

372-
free(cpy);
379+
free(arg1);
373380
}
374381
free(globals_argv);
375382
}

test/testing.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
for i, v in pairs(args) do
44
print(i, v, type(v))
55
end
6+
7+
-- use -Dtest=5 to display use
8+
print(test)

0 commit comments

Comments
 (0)