-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitcmdline.c
414 lines (358 loc) · 11.6 KB
/
initcmdline.c
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
* initcmdline.c - Initial command line options.
*
* Written by
* Andreas Boose <viceteam@t-online.de>
* Ettore Perazzoli <ettore@comm2000.it>
*
* This file is part of VICE, the Versatile Commodore Emulator.
* See README for copyright notice.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA.
*
*/
#include "vice.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "archdep.h"
#include "attach.h"
#include "autostart.h"
#include "charset.h"
#include "cmdline.h"
#include "initcmdline.h"
#include "lib.h"
#include "log.h"
#include "machine.h"
#include "resources.h"
#include "tape.h"
#include "translate.h"
#include "util.h"
static char *autostart_string = NULL;
static char *startup_disk_images[4];
static char *startup_tape_image;
static unsigned int autostart_mode;
static void cmdline_free_autostart_string(void)
{
lib_free(autostart_string);
autostart_string = NULL;
}
static int cmdline_help(const char *param, void *extra_param)
{
cmdline_show_help(NULL);
exit(0);
return 0; /* OSF1 cc complains */
}
static int cmdline_config(const char *param, void *extra_param)
{
/* "-config" needs to be handled before this gets called
but it also needs to be registered as a cmdline option,
hence this kludge. */
return 0;
}
static int cmdline_default(const char *param, void *extra_param)
{
return resources_set_defaults();
}
static int cmdline_autostart(const char *param, void *extra_param)
{
cmdline_free_autostart_string();
autostart_string = lib_stralloc(param);
autostart_mode = AUTOSTART_MODE_RUN;
return 0;
}
static int cmdline_autoload(const char *param, void *extra_param)
{
cmdline_free_autostart_string();
autostart_string = lib_stralloc(param);
autostart_mode = AUTOSTART_MODE_LOAD;
return 0;
}
#if !defined(__OS2__) && !defined(__BEOS__)
static int cmdline_console(const char *param, void *extra_param)
{
console_mode = 1;
return 0;
}
#endif
static int cmdline_attach(const char *param, void *extra_param)
{
int unit = vice_ptr_to_int(extra_param);
switch (unit) {
case 1:
lib_free(startup_tape_image);
startup_tape_image = lib_stralloc(param);
break;
case 8:
case 9:
case 10:
case 11:
lib_free(startup_disk_images[unit - 8]);
startup_disk_images[unit - 8] = lib_stralloc(param);
break;
default:
archdep_startup_log_error("cmdline_attach(): unexpected unit number %d?!\n",
unit);
}
return 0;
}
static const cmdline_option_t common_cmdline_options[] = {
{ "-help", CALL_FUNCTION, 0,
cmdline_help, NULL, NULL, NULL,
USE_PARAM_STRING, USE_DESCRIPTION_ID,
IDCLS_UNUSED, IDCLS_SHOW_COMMAND_LINE_OPTIONS,
NULL, NULL },
{ "-?", CALL_FUNCTION, 0,
cmdline_help, NULL, NULL, NULL,
USE_PARAM_STRING, USE_DESCRIPTION_ID,
IDCLS_UNUSED, IDCLS_SHOW_COMMAND_LINE_OPTIONS,
NULL, NULL },
{ "-h", CALL_FUNCTION, 0,
cmdline_help, NULL, NULL, NULL,
USE_PARAM_STRING, USE_DESCRIPTION_ID,
IDCLS_UNUSED, IDCLS_SHOW_COMMAND_LINE_OPTIONS,
NULL, NULL },
#if (!defined __OS2__ && !defined __BEOS__)
{ "-console", CALL_FUNCTION, 0,
cmdline_console, NULL, NULL, NULL,
USE_PARAM_STRING, USE_DESCRIPTION_ID,
IDCLS_UNUSED, IDCLS_CONSOLE_MODE,
NULL, NULL },
{ "-core", SET_RESOURCE, 0,
NULL, NULL, "DoCoreDump", (resource_value_t)1,
USE_PARAM_STRING, USE_DESCRIPTION_ID,
IDCLS_UNUSED, IDCLS_ALLOW_CORE_DUMPS,
NULL, NULL },
{ "+core", SET_RESOURCE, 0,
NULL, NULL, "DoCoreDump", (resource_value_t)0,
USE_PARAM_STRING, USE_DESCRIPTION_ID,
IDCLS_UNUSED, IDCLS_DONT_ALLOW_CORE_DUMPS,
NULL, NULL },
#else
{ "-debug", SET_RESOURCE, 0,
NULL, NULL, "DoCoreDump", (resource_value_t)1,
USE_PARAM_STRING, USE_DESCRIPTION_ID,
IDCLS_UNUSED, IDCLS_DONT_CALL_EXCEPTION_HANDLER,
NULL, NULL },
{ "+debug", SET_RESOURCE, 0,
NULL, NULL, "DoCoreDump", (resource_value_t)0,
USE_PARAM_STRING, USE_DESCRIPTION_ID,
IDCLS_UNUSED, IDCLS_CALL_EXCEPTION_HANDLER,
NULL, NULL },
#endif
{ NULL }
};
static const cmdline_option_t vsid_cmdline_options[] = {
{ NULL }
};
/* These are the command-line options for the initialization sequence. */
static const cmdline_option_t cmdline_options[] = {
{ "-default", CALL_FUNCTION, 0,
cmdline_default, NULL, NULL, NULL,
USE_PARAM_STRING, USE_DESCRIPTION_ID,
IDCLS_UNUSED, IDCLS_RESTORE_DEFAULT_SETTINGS,
NULL, NULL },
{ "-config", CALL_FUNCTION, 1,
cmdline_config, NULL, NULL, NULL,
USE_PARAM_ID, USE_DESCRIPTION_ID,
IDCLS_P_FILE, IDCLS_SPECIFY_CONFIG_FILE,
NULL, NULL },
{ "-autostart", CALL_FUNCTION, 1,
cmdline_autostart, NULL, NULL, NULL,
USE_PARAM_ID, USE_DESCRIPTION_ID,
IDCLS_P_NAME, IDCLS_ATTACH_AND_AUTOSTART,
NULL, NULL },
{ "-autoload", CALL_FUNCTION, 1,
cmdline_autoload, NULL, NULL, NULL,
USE_PARAM_ID, USE_DESCRIPTION_ID,
IDCLS_P_NAME, IDCLS_ATTACH_AND_AUTOLOAD,
NULL, NULL },
{ "-1", CALL_FUNCTION, 1,
cmdline_attach, (void *)1, NULL, NULL,
USE_PARAM_ID, USE_DESCRIPTION_ID,
IDCLS_P_NAME, IDCLS_ATTACH_AS_TAPE,
NULL, NULL },
{ "-8", CALL_FUNCTION, 1,
cmdline_attach, (void *)8, NULL, NULL,
USE_PARAM_ID, USE_DESCRIPTION_ID,
IDCLS_P_NAME, IDCLS_ATTACH_AS_DISK_8,
NULL, NULL },
{ "-9", CALL_FUNCTION, 1,
cmdline_attach, (void *)9, NULL, NULL,
USE_PARAM_ID, USE_DESCRIPTION_ID,
IDCLS_P_NAME, IDCLS_ATTACH_AS_DISK_9,
NULL, NULL },
{ "-10", CALL_FUNCTION, 1,
cmdline_attach, (void *)10, NULL, NULL,
USE_PARAM_ID, USE_DESCRIPTION_ID,
IDCLS_P_NAME, IDCLS_ATTACH_AS_DISK_10,
NULL, NULL },
{ "-11", CALL_FUNCTION, 1,
cmdline_attach, (void *)11, NULL, NULL,
USE_PARAM_ID, USE_DESCRIPTION_ID,
IDCLS_P_NAME, IDCLS_ATTACH_AS_DISK_11,
NULL, NULL },
{ NULL }
};
int initcmdline_init(void)
{
const cmdline_option_t *main_cmdline_options =
vsid_mode ? vsid_cmdline_options : cmdline_options;
if (cmdline_register_options(common_cmdline_options) < 0)
return -1;
if (cmdline_register_options(main_cmdline_options) < 0)
return -1;
return 0;
}
int initcmdline_check_psid(void)
{
/* Check for PSID here since we don't want to allow autodetection
in autostart.c. */
if (vsid_mode) {
if (autostart_string != NULL
&& machine_autodetect_psid(autostart_string) == -1) {
log_error(LOG_DEFAULT, "`%s' is not a valid PSID file.",
autostart_string);
return -1;
}
}
return 0;
}
int initcmdline_check_args(int argc, char **argv)
{
if (cmdline_parse(&argc, argv) < 0) {
archdep_startup_log_error("Error parsing command-line options, bailing out. For help use '-help'\n");
return -1;
}
/* The last orphan option is the same as `-autostart'. */
if (argc >= 1 && autostart_string == NULL) {
autostart_string = lib_stralloc(argv[1]);
argc--, argv++;
}
if (argc > 1) {
int len = 0, j;
for (j = 1; j < argc; j++)
len += (int)strlen(argv[j]);
{
char *txt = lib_calloc(1, len + argc + 1);
for (j = 1; j < argc; j++)
strcat(strcat(txt, " "), argv[j]);
archdep_startup_log_error("Extra arguments on command-line: %s\n",
txt);
lib_free(txt);
}
return -1;
}
return 0;
}
/* These are a helper function for the `-autostart' command-line option. It
replaces all the $[0-9A-Z][0-9A-Z] patterns in `string' and returns it. */
static char * hexstring_to_byte( char * source, char * destination )
{
char * next = source + 1;
char c;
BYTE value = 0;
int digit = 0;
while ( *next && digit++ < 2) {
value <<= 4;
c = toupper( *next++ );
if (c >= 'A' && c <= 'F' ) {
value += c - 'A';
}
else if ( isdigit(c) ) {
value += c - '0';
}
else {
break;
}
}
if (digit < 2) {
value = *source;
next = source + 1;
}
*destination = value;
return next;
}
static char *replace_hexcodes(char * source)
{
char * destination = lib_stralloc(source ? source : "");
if ( destination ) {
char * pread = destination;
char * pwrite = destination;
while ( *pread != 0 ) {
if ( *pread == '$' ) {
pread = hexstring_to_byte( pread, pwrite++ );
}
else {
*pwrite ++ = *pread ++;
}
}
*pwrite = 0;
}
return destination;
}
void initcmdline_check_attach(void)
{
if (!vsid_mode) {
/* Handle general-purpose command-line options. */
/* `-autostart' */
if (autostart_string != NULL) {
char *tmp;
/* Check for image:prg -format. */
tmp = strrchr(autostart_string, ':');
if (tmp) {
char *autostart_prg_name;
char *autostart_file;
autostart_file = lib_stralloc(autostart_string);
autostart_prg_name = strrchr(autostart_file, ':');
*autostart_prg_name++ = '\0';
/* Does the image exist? */
if (util_file_exists(autostart_file)) {
char *name;
charset_petconvstring((BYTE *)autostart_prg_name, 0);
name = replace_hexcodes(autostart_prg_name);
autostart_autodetect(autostart_file, name, 0,
autostart_mode);
lib_free(name);
} else
autostart_autodetect(autostart_string, NULL, 0,
autostart_mode);
lib_free(autostart_file);
} else {
autostart_autodetect(autostart_string, NULL, 0,
autostart_mode);
}
}
/* `-8', `-9', `-10' and `-11': Attach specified disk image. */
{
int i;
for (i = 0; i < 4; i++) {
if (startup_disk_images[i] != NULL
&& file_system_attach_disk(i + 8, startup_disk_images[i])
< 0)
log_error(LOG_DEFAULT,
"Cannot attach disk image `%s' to unit %d.",
startup_disk_images[i], i + 8);
}
}
/* `-1': Attach specified tape image. */
if (startup_tape_image && tape_image_attach(1, startup_tape_image) < 0)
log_error(LOG_DEFAULT, "Cannot attach tape image `%s'.",
startup_tape_image);
}
cmdline_free_autostart_string();
}