weird problem in command line parsing!

Pages: 12
this is some parts of my main which has problems:
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
int main(int argc, char** argv)
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_JoystickEventState(SDL_ENABLE);
dhInitialize(true);
dhToggleExceptions(true);
l=lua_open();
if(l==nullptr)
{
lua_pushstring(l, "unable to initialize Advanced Audio Game Engine");
lua_error(l);
}
luaL_openlibs(l);
luaL_OpenVector(l);
luaL_OpenAI(l);
luaL_OpenJoystick(l);
luaL_OpenTimer(l);
luaL_OpenWindow(l);
luaL_OpenConsole(l);

char* scriptfile;
char* outfile;
fstream in("exec.vm", ios::in|ios::binary);
int cmd=0;
bool dbg=false;
bool compile=false;
while( ++cmd < argc)
{
    if(strcmp(argv[cmd], "-c")==0)
{
compile=true;
}
if (strcmp(argv[cmd], "-d")==0)
{
dbg = TRUE;
}
if(strcmp(argv[cmd], "-o")==0)
{
strcpy(outfile, argv[cmd++]);
}
if (strcmp(argv[cmd], "-h") ==0)
{
printhlp(argv[0]);
exit(0);
}
if (strcmp(argv[cmd], "--help") ==0)
{
printhlp(argv[0]);
exit(0);
}
if( strcmp(argv[cmd], "-?")==0)
{
printhlp(argv[0]);
exit(0);
}
else
{
printf( (const char*) stderr, "unknown option: ", argv[cmd]);
printhlp(argv[0]);
exit(1);
}
strcpy(scriptfile, argv[cmd++]);
}
if(scriptfile="")
{
const char* openfilters[]= {
"*.aage", "*.lua"
};
scriptfile=tinyfd_openFileDialog("select which script to load", "", 1, openfilters, false);
if(scriptfile==nullptr)
{
exit(0);
}
}
if(compile==true&&outfile=="")
{
const char* savefilters[] = {
"*.exe"
};
outfile=tinyfd_saveFileDialog("select executable path", "", 1, savefilters);
if(outfile==nullptr)
{
exit(0);
}
}
}

what should be the problem here!
i've used tclap to parse command line, but that error happened again!
this is all of my main.cpp:
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262

//includes
#include "exevm.hpp"
    #include "resource.hpp"
#include <fstream>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <tclap/CmdLine.h>
#include <luajit-2.0/lua.hpp>
#include <windows.h>
#include <tinyfiledialogs.h>
#define SDL_MAIN_HANDLED
#include <sdl2/sdl.h>
#include "disphelper.h"
#include "AI.hpp"
#include "audio.hpp"
#include "vector.hpp"
#include "joystick.hpp"
#include "timer.hpp"
#include "window.hpp"
#include "console.hpp"

//uses
using namespace std;
using namespace TCLAP;

//variables that we need
lua_State *l;

//compression function for compress the lua scripts
const char* compress (const char* in, int inlen, int* outlen)
{
char* buffer = (char*) malloc(inlen+8);
if (buffer==nullptr)
{
return nullptr;
}
z_stream z;
memset(&z, 0, sizeof(z));
z.next_in = in;
z.avail_in = inlen;
z.next_out = buffer;
z.avail_out = inlen+8;
if (deflateInit(&z,9)!=Z_OK)
{
return nullptr;
}
if (deflate(&z,Z_FINISH)!=Z_STREAM_END)
{
return nullptr;
}
if (outlen)
{
*outlen = inlen +8 -z.avail_out;
}
return buffer;
}

//encryption function in order to prevent the executable from accessed and cracked by others
char* encrypt(char* in, int len)
{
BLOWFISH_CTX c;
Blowfish_Init(&c, encryption_code, len);
char* out;
Blowfish_Encrypt(&c, (int*)in, (int*)out);
return out;
}

//this function writes the executable VM and the script into executable file
int writechunk (lua_State* l, chunkinfo* i, fstream& out, const char* chunkname, bool dbg)
{
int n=0, t=0, length=0, clength=0;
lua_pushboolean(l, !dbg);
lua_call(l, 2, 1);
const char* data = lua_tolstring(l, -1, &length);
const char*cdata = compress(data, length, &clength);
if (cdata==nullptr)
{
return 0;
}
char* encdata=encrypt(cdata, length);
if(encdata==nullptr)
{
return 0;
}
i->offset =out.tellg();
i->length = clength;
while (t<clength && (n=out.write(encdata+t, clength-t))>0)
{
t+=n;
}
free(encdata);
free(cdata);
if (t<clength)
{
return 0;
}
lua_pop(l,1);
return 0;
}

//this function prepares the executable name, in order for it to run
void preparename (char* s)
{
char* c = strrchr(s,'.');
if (c)
{
*c=0;
}
while (*(++s))
{
if (*s=='/' || *s=='\\')
{
*s='.';
}
}
}

//the program main entrypoint
int main(int argc, char** argv)
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_JoystickEventState(SDL_ENABLE);
dhInitialize(true);
dhToggleExceptions(true);
l=lua_open();
if(l==nullptr)
{
lua_pushstring(l, "unable to initialize Advanced Audio Game Engine");
lua_error(l);
}
luaL_openlibs(l);
luaL_OpenVector(l);
luaL_OpenAI(l);
luaL_OpenJoystick(l);
luaL_OpenTimer(l);
luaL_OpenWindow(l);
luaL_OpenConsole(l);

char* scriptfile;
char* outfile;
fstream in("exec.vm", ios::in|ios::binary);
bool dbg=false;
bool compile=false;
try
{
CmdLine cmd("Advanced Audio Game Engine", " ", "0.1.0", true);
UnlabeledValueArg<string> scrfile("scriptfile", "script file to load", false, "", "the script file to be loaded", false, nullptr);
cmd.add(scrfile);
ValueArg<string> out("o", "output", "set the executable output file", false, "", "the path to script file to load", nullptr);
cmd.add(out);
SwitchArg compscript("c", "compile", "compile's the script into executable", false, nullptr);
cmd.add(compscript);
SwitchArg debugscript("d", "debug", "add's debugging information to compiled executable", false, nullptr);
cmd.add(debugscript);
cmd.parse(argc, argv);
scriptfile=scrfile.getValue().c_str();
outfile=out.getValue().c_str();
compile=compscript.getValue();
dbg=debugscript.getValue();
}
catch(ArgException &e)
{
cerr << "error: " << e.what();
}
if(argc<2)
{
if(scriptfile="")
{
const char* openfilters[]= {
"*.aage"
};
scriptfile=tinyfd_openFileDialog("select which script to load", "", 1, openfilters, false);
if(scriptfile==nullptr)
{
exit(0);
}
}
if(compile==true&&outfile=="")
{
const char* savefilters[] = {
"*.exe"
};
outfile=tinyfd_saveFileDialog("select executable path", "", 1, savefilters);
if(outfile==nullptr)
{
exit(0);
}
}
}
if(compile)
{
int n = 0, t=0;
GlobalChunkInfo pkginfo = { 0, 0, 0, MAGIC };
chunkinfo e;
int bufferlen = 4096, bufferpos = 0;
char* buffer=malloc(bufferlen);
if(buffer==nullptr)
{
lua_pushstring(l, "unable to compile the script into executable!");
lua_error(l);
return 1;
}
fstream out(outfile, ios::binary|ios::out);
if(!out.is_open()||!in.is_open())
{
lua_pushstring(l, "unable to initialize executable compiler!");
lua_error(l);
return 1;
}
while(n=in.read(buffer, bufferlen).gcount()>0)
{
out.write(buffer, n);
}
in.close();
lua_getglobal(l, "string");
lua_getfield(l, -1, "dump");
unsigned long length=0;
const char* data=nullptr;
lua_pushvalue(l, -1);
luaL_loadfile(l, scriptfile);
pkginfo.count++;
preparename(scriptfile);
e.namelength=strlen(scriptfile)+1;
if(writechunk(l, &e, out, scriptfile, dbg))
{
lua_pushstring(l, "unable to write the executable to the disk");
lua_error(l);
return 1;
}
if (bufferpos+e.namelength+sizeof(e)+8 >= bufferlen)
{
buffer=realloc(buffer, bufferlen=max<int>(bufferlen*3/2 +1, bufferpos+e.namelength+sizeof(e)+16));
}
memcpy(buffer+bufferpos, &e, sizeof(e));
memcpy(buffer + bufferpos + sizeof(e), scriptfile, e.namelength);
bufferpos += e.namelength + sizeof(e);
pkginfo.offset=out.tellg();
pkginfo.length = bufferpos;
n=0; t=0;
while(t<bufferpos&&(n=out.write(buffer+t, bufferpos))>0)
{
t+=n;
}
if(t<bufferpos)
{
lua_pushstring(l, "unable to write into executable file!");
lua_error(l);
return 1;
}
out.flush();
out.close();
}
else
{
luaL_loadfile(l, scriptfile);
lua_pcall(l, 0, 0, 0);
}
return 0;
}

what is the problem!
i've fully mixed up
the GlobalChunkInfo is a struct:
1
2
3
4
5
6
typedef struct GlobalChunkInfo {
unsigned long offset;
unsigned long length;
unsigned long count;
unsigned long long magic;
} GlobalChunkInfo;

and chunkinfo is like this:
1
2
3
4
5
typedef struct chunkinfo {
unsigned long offset;
unsigned long length:22;
unsigned long namelength:10;
}chunkinfo;

and varnum is declared like this:
1
2
3
4
5
6
typedef union varnum {
struct {
unsigned long n1,n2;
};
double d;
}varnum;
Last edited on
I am thinking that problemmight be in your printhlp function. Your main is fine, but this is invalid use of printf:printf( (const char*) stderr, "unknown option: ", argv[cmd]);. It would not print anything.
i've deleted that, and it doesnt worked
and after i've used tclap, nothing happened and no output
i've deleted that, and it doesnt worked
I assume that the same problem is within printhlp function. Are you sure that it is correct? Try to call it on top of main. Will it show or not?
i've used cout in my printhlp, and it doesnt worked!
no crash, nothing!
instead of printf, i've used cout two, used cerr and nothing worked
after changing the code, nothing happened again
Does printhlp function itself work? Did you try to call it _first_ line in main? And remove SDL initialization from main for now.
i've written a minimal example, and it have worked
and i've tryed your advice and it have worked two!, it doesnt have problem now!
but the printhlp() has deleted from my main, and i'm using tclap, and i dont get anything!
this is the minimal code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cstdio>

using namespace std;

void printhlp(char* text)
{
cout << "my game engine\n" << "usage: " << text << "-c, compiles the script into executable\n-d adds debugging information\n-o sets the executable output file\n";
}

void printhlp_printf(char* text)
{
printf("using printf: ", text);
}

int main(int argc, char** argv)
{
printhlp(argv[0]);
printhlp_printf(argv[0]);
}

thanks
Last edited on
Check if there are files called stdout.txt and stdin.txt in executable working folder and what their content is.
I just remembered one funny thing about SDL.
no, stdin.txt and stdout.txt files are not exists on the executable path
Anyway, try to do stuff recommended here: http://sdl.beuc.net/sdl.wiki/FAQ_Console
thanks, it worked,
i've removed -mwindows and followed those instructions and fixed
thanks in advance
Topic archived. No new replies allowed.
Pages: 12