This repository has been archived by the owner on Nov 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCOMMAND.CPP
executable file
·340 lines (266 loc) · 7.76 KB
/
COMMAND.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <dir.h>
#include "dd.h"
#include "config.h"
#include "command.h"
#include "message.h"
//----------------------------------------------------
//defs for system options
//forces a res on startup
int option_res(char *p)
{
int xw,yw;
if (sscanf(p,"%d %d",&xw,&yw)<2) return 0;
SCREENX=xw;
SCREENY=yw;
return 1;
}
int option_help(char *p);
int option_sb(char *p);
int option_sb16(char *p);
static char *inputtypes[]={"NONE","GRAVIS","KEY1","KEY2","JOY1","JOY2",0};
int getinputtype(char *s)
{
for (int i=0; inputtypes[i]; i++)
if (!stricmp(s,inputtypes[i])) return i;
msg.error("Invalid input type %s. Valid types are: ",s);
for (i=0; inputtypes[i]; i++) msg.error(inputtypes[i]);
return 0;
}
int option_setinput(char *p)
{
int num;
char type[32]; type[0]=0;
if (!sscanf(p,"%d %s",&num,type)) return 0;
num--;
if (num<0 || num>3) {msg.error("Input num out of range"); return 1;}
cfg->pinput[num]=getinputtype(type); //set input
return 1;
}
char waitvsync;
int option_waitvsync(char *p)
{
waitvsync=1; return 1;
}
extern int SOUNDRATE;
int option_sndrate(char *p)
{
int num;
if (!sscanf(p,"%d",&num)) return 0;
if (num<8000) {msg.error("Sample rate too low, min=8000"); return 1;}
if (num>44100) {msg.error("Sample rate too high, max=44100"); return 1;}
SOUNDRATE=num;
return 1;
}
optiondef optiondefs[]=
{
#ifdef DOS
{"?",option_help,0, 0,"Display this help"},
{"h",option_help,0, 0, 0},
#endif
{"res", option_res,0, "<X-res> <Y-res>", "Change video resolution to (X-res,Y-res)"},
{"nosound", 0,0, 0,"Disable sound"},
{"waitvsync",option_waitvsync,0, 0, "Wait till vertical sync to draw frames"},
{"sndrate", option_sndrate,0, "<samples/sec>", "Change sound rate [default=22010]"},
#ifdef DOS
{"sb" , option_sb,0, "<port> <irq> <dma>", "Manually set SB hardware settings"},
{"sb16" , option_sb16,0, "<port> <irq> <dma>", "Manually set SB16 hardware settings"},
{"novesa" , 0,0,0, "Do not use VESA 2.0 extensions"},
{"banked" , 0,0,0, "Force banked video mode"},
{"linear" , 0,0,0, "Force linear video mode"},
#endif
{"setinput",option_setinput,0, "<num> <type>","Set input device <num> to <type>"},
// {"definekey",option_definekey,0, "<num> <scancodes...>","Redefine keys for keyboard <num> "},
{0,0,0,0,0}
};
//------------------------------------------------------------
//defs for commands to be executed after system initialization
void disablegui();
void m_showmessages();
void m_showfps();
int cmd_message(char *p) {m_showmessages(); return 1;}
int cmd_showfps(char *p) {m_showfps(); return 1;}
int cmd_hidegui(char *p) {disablegui(); return 1;}
int cmd_maximize(char *p);
int cmd_loadrom(char *p);
int cmd_runrom(char *p);
int cmd_restorerom(char *p);
int cmd_chdir(char *p)
{
chdir(p);
return 1;
}
optiondef commanddefs[]=
{
{"message", cmd_message, 0, 0, "Display message box"},
{"showfps", cmd_showfps, 0, 0, "Show frames/sec"},
{"hidegui", cmd_hidegui, 0, 0, "Hide GUI on startup"},
{"chdir", cmd_chdir, 0, "<dirname>", "Changes dir on startup"},
{"load", cmd_loadrom, 0, "<filename>", "Load ROM <filename>"},
{"run", cmd_runrom, 0, "<filename>", "Load & run ROM <filename>"},
{"restore", cmd_restorerom, 0, "<filename>", "Load/run/restore ROM <filename>"},
{0,0,0,0,0}
};
//----------------
//find optiondef with specific name, 0 if not found
optiondef *findoptiondef(char *name,optiondef *od)
{
optiondef *c=od;
for ( ; c->name; c++)
if (!stricmp(c->name,name)) return c;
return 0;
}
int getoption(char *name)
{
optiondef *od=findoptiondef(name,optiondefs);
return od ? od->flag : 0;
}
int getcommand(char *name)
{
optiondef *od=findoptiondef(name,commanddefs);
return od ? od->flag : 0;
}
void optiondef::printhelp()
{
if (!desc) return;
char s[128];
sprintf(s," -%s %s",name,syntax ? syntax : "");
memset(s+strlen(s),' ',32);
s[26]=0;
strcat(s,desc);
printf("%s\n",s);
}
//displays help
int option_help(char *p)
{
optiondef *c;
#ifdef DOS
printf("System options: \n");
for (c=optiondefs; c->name; c++) c->printhelp();
printf("\n");
printf("Commands: \n");
for (c=commanddefs; c->name; c++) c->printhelp();
exit(1);
#endif
return 1;
}
//----------------------------------------
//individual option on option line
void option::free()
{
if (name) ::free(name);
if (parm) ::free(parm);
}
void option::print()
{
if (!name) return;
if (parm) msg.printf(1,"%s: %s",name,parm);
else msg.printf(1,name);
}
//execute a particular commandline option by finding it's
//option def, setting the flag and calling the func
void option::execute(int type)
{
if (!name) return;
optiondef *od=findoptiondef(name,type ? commanddefs : optiondefs);
if (!od)
{
if (!type && !findoptiondef(name,commanddefs))
msg.error("unrecognized arg: %s",name);
return; //no option
}
od->flag=1;
if (od->func && !od->func(parm ? parm : "")) //error executing option....
msg.error("syntax error: %s %s",od->name,od->syntax);
}
//----------------------------------------
//class for parsing/executing commandline
//see if something is a option
inline int isoption(char *s) {return s[0]=='-';}
inline int isfile(char *s) {return s[0]=='@';}
inline int isparm(char *s) {return s[0]!='-' && s[0]!='@';}
//add option to end of array
option *commandline::addoption()
{
options=(option *)realloc(options,(numoptions+1)*sizeof(option));
options[numoptions].clear();
return &options[numoptions++];
}
//print all commands in commandline
void commandline::print()
{
msg.printf(2,"%d options parsed:",numoptions);
for (int i=0; i<numoptions; i++)
options[i].print();
}
//execute all commands on commandline
void commandline::execute(int type)
{
for (int i=0; i<numoptions; i++) options[i].execute(type);
}
//parse commands from array of strings
void commandline::parse(int argc,char **argv)
{
int i=0;
while (i<argc)
if (isoption(argv[i])) //see if it's a option
{
option *C=addoption(); //add new option...
C->name=strdup(argv[i]+1); //add option itself
static char p[128];
p[0]=0; //string containing parms for option
for (i++; i<argc && isparm(argv[i]); i++)
{if (p[0]) strcat(p," "); strcat(p,argv[i]);}
C->parm=strdup(p); //add parms
} else
if (isfile(argv[i])) //see if it's a file redirection
{
char fname[64];
strcpy(fname,argv[i++]+1);
if (!strchr(fname,'.')) strcat(fname,".cfg");
msg.printf(1,"Parsing file %s",fname);
FILE *f=fopen(fname,"rt"); //open file
if (f) parse(f); //parse filename
fclose(f);
} else
{
option *C=addoption(); //add new option...
C->name=strdup("run"); //'run' by default
C->parm=argv[i++];
// msg.error("misplaced argument: %s",argv[i++]); //it's misplaced
}
}
//parse commands from a string
void commandline::parse(char *c)
{
int argc=0;
static char *argv[32];
while (*c) //until end of commandline...
{
while (*c && isspace(*c)) c++; //scan through all spaces
if (!*c) break;
argv[argc++]=c; //store string start
while(*c && !isspace(*c)) c++; //scan through all non-spaces
if (!*c) break;
*c=0; //put end of string
c++;
}
parse(argc,argv);
}
//parse commands from a file
void commandline::parse(FILE *f)
{
static char line[256];
while (fgets(line,256,f)) parse(line);
}
//constructors....
commandline::commandline() {numoptions=0; options=0;}
//destructor
commandline::~commandline()
{
for (int i=0; i<numoptions; i++) options[i].free();
free(options);
}