Skip to content

Commit c468551

Browse files
committed
Now supports LUACON_ADDITIONS macro to cut out using additions completely (with minor inbeutifications). Code format. Keeping version updated correctly now in help message. Fixed error message in include macro cluster.
1 parent a609886 commit c468551

File tree

3 files changed

+100
-48
lines changed

3 files changed

+100
-48
lines changed

src/additions.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,27 @@
55
*
66
*/
77

8-
9-
#if defined(__linux__) || defined(__unix__)
10-
#include <unistd.h>
8+
#if defined(linux) || defined(__linux__) || defined(__linux)
9+
# include <unistd.h>
10+
# include <stdio.h>
11+
# include <stdlib.h>
12+
#elif defined(unix) || defined(__unix__) || defined(__unix)
13+
# include <unistd.h>
14+
# include <stdio.h>
15+
# include <stdlib.h>
16+
#elif defined(__APPLE__) || defined(__MACH__)
17+
# include <unistd.h>
18+
# include <stdio.h>
19+
# include <stdlib.h>
20+
#elif defined(_WIN32) || defined(_WIN64)
21+
# include <windows.h>
22+
# include <stdio.h>
23+
# include <stdlib.h>
24+
# include <dirent.h>
1125
#else
12-
#include <dirent.h>
26+
# error "Not familiar. Set up headers accordingly, or -D__linux__ or -D__APPLE__ or -D_WIN32"
1327
#endif
1428

15-
#include <limits.h>
16-
#include <stdio.h>
17-
#include <stdlib.h>
18-
1929
#include "additions.h"
2030

2131
#include "lua.h"
@@ -24,9 +34,9 @@
2434

2535

2636
#ifdef _WIN32
27-
#define CLEAR_CONSOLE "cls"
37+
# define CLEAR_CONSOLE "cls"
2838
#else
29-
#define CLEAR_CONSOLE "clear"
39+
# define CLEAR_CONSOLE "clear"
3040
#endif
3141

3242

src/console.c

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
*
1414
*/
1515

16-
// unsupported feature, defined to prepare for future
17-
#define USE_ADDITIONS
18-
1916
#define PRIMARY_BUFFER_SIZE (1024 + 1)
2017
#define SECONDARY_BUFFER_SIZE (1032 + 1)
2118

@@ -40,7 +37,7 @@
4037
# include <stdlib.h>
4138
# include <dirent.h>
4239
#else
43-
# error "Not familiar. Set up headers accordingly, or -D__linux__ or -D__APPLE__ or -D_WIN32"
40+
# error "Not familiar. Set up headers accordingly, or -D__linux__ of -Dunix or -D__APPLE__ or -D_WIN32"
4441
#endif
4542

4643
#include <string.h>
@@ -49,7 +46,7 @@
4946
#include "lualib.h"
5047
#include "lauxlib.h"
5148

52-
#if defined(USE_ADDITIONS)
49+
#if defined(LUACON_ADDITIONS)
5350
# include "additions.h"
5451
#endif
5552

@@ -67,7 +64,7 @@ typedef enum LuaConsoleError {
6764

6865
// usage message
6966
const char HELP_MESSAGE[] =
70-
"Lua Console | Version: 6/13/2017\n"
67+
"Lua Console | Version: 9/26/2017\n"
7168
LUA_COPYRIGHT
7269
LUA_CONSOLE_COPYRIGHT
7370
"\n"
@@ -76,14 +73,18 @@ const char HELP_MESSAGE[] =
7673
"\n"
7774
"\t- Files executed by passing\n"
7875
"\t- Working directory support\n"
79-
"\t- Built in stack-dump\n"
76+
#if defined(LUACON_ADDITIONS)
77+
"\t- Built in stack-dump\n"
78+
#endif
8079
"\n"
8180
"Usage: lua.exe [FILE_PATH] [-v] [-e] [-s START_PATH] [-a] [-c] [-?] [-n]{parameter1 ...} \n"
8281
"\n"
8382
"-v \t Prints the Lua version in use\n"
8483
"-e \t Prevents lua core libraries from loading\n"
8584
"-s \t Issues a new root path\n"
86-
"-a \t Removes the additions\n"
85+
#if defined(LUACON_ADDITIONS)
86+
"-a \t Removes the additions\n"
87+
#endif
8788
"-c \t No copyright on init\n"
8889
"-d \t Defines a global variable as value after '='\n"
8990
"-n \t Start of parameter section\n"
@@ -116,9 +117,11 @@ static void print_error(LuaConsoleError error) {
116117
break;
117118
}
118119
size_t top = lua_gettop(L);
119-
fprintf(stderr, " | Stack Top: %d | %s\n", lua_gettop(L), msg);
120-
if(top > 1) // other than error message
121-
stack_dump(L);
120+
fprintf(stderr, " | Stack Top: %Iu | %s\n", top, msg);
121+
#if defined(LUACON_ADDITIONS)
122+
if(top > 1) // other than error message
123+
stack_dump(L);
124+
#endif
122125
lua_pop(L, 1);
123126
}
124127

@@ -167,6 +170,8 @@ char* strsplit(const char* str1, const char lookout, size_t len, size_t max) {
167170
char* cpy = malloc(len);
168171
memcpy(cpy, str1, len);
169172

173+
size_t temp_max = max;
174+
170175
for (size_t i=0; i<len-1; i++) {
171176
if(str1[i] == lookout) {
172177
cpy[i] = '\0';
@@ -175,6 +180,10 @@ char* strsplit(const char* str1, const char lookout, size_t len, size_t max) {
175180
if(max == 0)
176181
break;
177182
}
183+
if(temp_max == max) {
184+
free(cpy);
185+
return 0;
186+
}
178187
return cpy;
179188
}
180189

@@ -185,7 +194,9 @@ int main(int argc, char* argv[])
185194
static int change_start = 0;
186195
static int no_file = 0;
187196
static char* start = 0;
188-
static int no_additions = 0;
197+
#if defined(LUACON_ADDITIONS)
198+
static int no_additions = 0;
199+
#endif
189200
static int copyright_squelch = 0;
190201

191202
static size_t parameters = 0;
@@ -227,9 +238,11 @@ int main(int argc, char* argv[])
227238
change_start = 1;
228239
start = argv[i+1];
229240
break;
230-
case 'a': case 'A':
231-
no_additions = 1;
232-
break;
241+
#if defined(LUACON_ADDITIONS)
242+
case 'a': case 'A':
243+
no_additions = 1;
244+
break;
245+
#endif
233246
case 'c': case 'C':
234247
copyright_squelch = 1;
235248
break;
@@ -286,7 +299,13 @@ int main(int argc, char* argv[])
286299
for (size_t i=0; i<globals; i++) {
287300
char* globals_D_offset = globals_argv[i] + 2;
288301

289-
char* arg1 = strsplit(globals_D_offset, '=', strlen(globals_D_offset) + 1, 2);
302+
char* m_args = strsplit(globals_D_offset, '=', strlen(globals_D_offset) + 1, 2);
303+
if(m_args == 0) {
304+
fprintf(stderr, "Error: Incorrect -D specified. Use format 'name=value'.\n");
305+
return EXIT_FAILURE;
306+
}
307+
308+
char* arg1 = m_args;
290309
char* arg2 = arg1 + (strlen(arg1) + 1);
291310

292311
lua_pushlstring(L, arg2, strlen(arg2));
@@ -335,9 +354,11 @@ int main(int argc, char* argv[])
335354
}
336355

337356

338-
// add additions
339-
if(no_additions == 0)
340-
additions_add(L);
357+
#if defined(LUACON_ADDITIONS)
358+
// add additions
359+
if(no_additions == 0)
360+
additions_add(L);
361+
#endif
341362

342363

343364
// load function into protected mode (pcall)

src/consolew.c

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
*
1414
*/
1515

16-
// unsupported feature, defined to prepare for future
17-
#define USE_ADDITIONS
18-
1916
#define PRIMARY_BUFFER_SIZE (1024 + 1)
2017
#define SECONDARY_BUFFER_SIZE (1032 + 1)
2118

@@ -40,7 +37,7 @@
4037
# include <stdlib.h>
4138
# include <dirent.h>
4239
#else
43-
# error "Not familiar. Set up headers accordingly, or -D__linux__ or -D__APPLE__ or -D_WIN32"
40+
# error "Not familiar. Set up headers accordingly, or -D__linux__ of -Dunix or -D__APPLE__ or -D_WIN32"
4441
#endif
4542

4643
#include <string.h>
@@ -49,7 +46,7 @@
4946
#include "lualib.h"
5047
#include "lauxlib.h"
5148

52-
#if defined(USE_ADDITIONS)
49+
#if defined(LUACON_ADDITIONS)
5350
# include "additions.h"
5451
#endif
5552

@@ -67,7 +64,7 @@ typedef enum LuaConsoleError {
6764

6865
// usage message
6966
const char HELP_MESSAGE[] =
70-
"Lua Console | Version: 6/13/2017\n"
67+
"Lua Console | Version: 9/26/2017\n"
7168
LUA_COPYRIGHT
7269
LUA_CONSOLE_COPYRIGHT
7370
"\n"
@@ -77,15 +74,19 @@ const char HELP_MESSAGE[] =
7774
"\t- Line by Line interpretation\n"
7875
"\t- Files executed by passing\n"
7976
"\t- Working directory support\n"
80-
"\t- Built in stack-dump\n"
77+
#if defined(LUACON_ADDITIONS)
78+
"\t- Built in stack-dump\n"
79+
#endif
8180
"\n"
8281
"Usage: lua.exe [FILE_PATH] [-v] [-e] [-s START_PATH] [-p] [-a] [-c] [-?] [-n]{parameter1 ...} \n"
8382
"\n"
8483
"-v \t Prints the Lua version in use\n"
8584
"-e \t Prevents lua core libraries from loading\n"
8685
"-s \t Issues a new root path\n"
8786
"-p \t Has console post exist after script in line by line mode\n"
88-
"-a \t Removes the additions\n"
87+
#if defined(LUACON_ADDITIONS)
88+
"-a \t Removes the additions\n"
89+
#endif
8990
"-c \t No copyright on init\n"
9091
"-d \t Defines a global variable as value after '='\n"
9192
"-n \t Start of parameter section\n"
@@ -121,9 +122,11 @@ static void print_error(LuaConsoleError error) {
121122
break;
122123
}
123124
size_t top = lua_gettop(L);
124-
fprintf(stderr, " | Stack Top: %d | %s\n", lua_gettop(L), msg);
125-
if(top > 1) // other than error message
126-
stack_dump(L);
125+
fprintf(stderr, " | Stack Top: %Iu | %s\n", top, msg);
126+
#if defined(LUACON_ADDITIONS)
127+
if(top > 1) // other than error message
128+
stack_dump(L);
129+
#endif
127130
lua_pop(L, 1);
128131
}
129132

@@ -247,6 +250,8 @@ char* strsplit(const char* str1, const char lookout, size_t len, size_t max) {
247250
char* cpy = malloc(len);
248251
memcpy(cpy, str1, len);
249252

253+
size_t temp_max = max;
254+
250255
for (size_t i=0; i<len-1; i++) {
251256
if(str1[i] == lookout) {
252257
cpy[i] = '\0';
@@ -255,6 +260,10 @@ char* strsplit(const char* str1, const char lookout, size_t len, size_t max) {
255260
if(max == 0)
256261
break;
257262
}
263+
if(temp_max == max) {
264+
free(cpy);
265+
return 0;
266+
}
258267
return cpy;
259268
}
260269

@@ -266,7 +275,9 @@ int main(int argc, char* argv[])
266275
static int post_exist = 0;
267276
static int no_file = 0;
268277
static char* start = 0;
269-
static int no_additions = 0;
278+
#if defined(LUACON_ADDITIONS)
279+
static int no_additions = 0;
280+
#endif
270281
static int copyright_squelch = 0;
271282

272283
static size_t parameters = 0;
@@ -312,9 +323,11 @@ int main(int argc, char* argv[])
312323
case 'p': case 'P':
313324
post_exist = 1;
314325
break;
315-
case 'a': case 'A':
316-
no_additions = 1;
317-
break;
326+
#if defined(LUACON_ADDITIONS)
327+
case 'a': case 'A':
328+
no_additions = 1;
329+
break;
330+
#endif
318331
case 'c': case 'C':
319332
copyright_squelch = 1;
320333
break;
@@ -371,7 +384,13 @@ int main(int argc, char* argv[])
371384
for (size_t i=0; i<globals; i++) {
372385
char* globals_D_offset = globals_argv[i] + 2;
373386

374-
char* arg1 = strsplit(globals_D_offset, '=', strlen(globals_D_offset) + 1, 2);
387+
char* m_args = strsplit(globals_D_offset, '=', strlen(globals_D_offset) + 1, 2);
388+
if(m_args == 0) {
389+
fprintf(stderr, "Error: Incorrect -D specified. Use format 'name=value'.\n");
390+
return EXIT_FAILURE;
391+
}
392+
393+
char* arg1 = m_args;
375394
char* arg2 = arg1 + (strlen(arg1) + 1);
376395

377396
lua_pushlstring(L, arg2, strlen(arg2));
@@ -420,9 +439,11 @@ int main(int argc, char* argv[])
420439
}
421440

422441

423-
// add additions
424-
if(no_additions == 0)
425-
additions_add(L);
442+
#if defined(LUACON_ADDITIONS)
443+
// add additions
444+
if(no_additions == 0)
445+
additions_add(L);
446+
#endif
426447

427448

428449
// load function into protected mode (pcall)

0 commit comments

Comments
 (0)