-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfliplist.c
460 lines (375 loc) · 11 KB
/
fliplist.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
* fliplist.c
*
* Written by
* pottendo <pottendo@gmx.net>
*
* 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 "cmdline.h"
#include "fliplist.h"
#include "lib.h"
#include "log.h"
#include "resources.h"
#include "translate.h"
#include "util.h"
#define NUM_DRIVES 4
struct fliplist_s {
fliplist_t next, prev;
char *image;
unsigned int unit;
};
static fliplist_t fliplist[NUM_DRIVES] = {
(fliplist_t)NULL,
(fliplist_t)NULL
};
static char *current_image = (char *)NULL;
static unsigned int current_drive;
static fliplist_t iterator;
static const char flip_file_header[] = "# Vice fliplist file";
#define buffer_size 1024
static void show_fliplist(unsigned int unit);
static char *fliplist_file_name = NULL;
static int set_fliplist_file_name(const char *val, void *param)
{
if (util_string_set(&fliplist_file_name, val))
return 0;
fliplist_load_list((unsigned int)-1, fliplist_file_name, 0);
return 0;
}
static resource_string_t resources_string[] = {
{ "FliplistName", NULL, RES_EVENT_NO, NULL,
&fliplist_file_name, set_fliplist_file_name, NULL },
{ NULL }
};
int fliplist_resources_init(void)
{
resources_string[0].factory_value = archdep_default_fliplist_file_name();
if (resources_register_string(resources_string) < 0)
return -1;
return 0;
}
void fliplist_resources_shutdown(void)
{
int i;
for (i = 0; i < NUM_DRIVES; i++)
fliplist_clear_list(8 + i);
lib_free(fliplist_file_name);
lib_free((char *)(resources_string[0].factory_value));
}
static const cmdline_option_t cmdline_options[] =
{
{ "-flipname", SET_RESOURCE, 1,
NULL, NULL, "FliplistName", NULL,
USE_PARAM_ID, USE_DESCRIPTION_ID,
IDCLS_P_NAME, IDCLS_SPECIFY_FLIP_LIST_NAME,
NULL, NULL },
{ NULL }
};
int fliplist_cmdline_options_init(void)
{
return cmdline_register_options(cmdline_options);
}
/* ------------------------------------------------------------------------- */
/* interface functions */
void fliplist_shutdown(void)
{
lib_free(current_image);
current_image = NULL;
}
void fliplist_set_current(unsigned int unit, const char *filename)
{
lib_free(current_image);
current_image = lib_stralloc(filename);
current_drive = unit;
}
#if 0
char *fliplist_get_head(unsigned int unit)
{
if (fliplist[unit - 8])
return fliplist[unit - 8]->image;
return (char *) NULL;
}
#endif
const char *fliplist_get_next(unsigned int unit)
{
if (fliplist[unit - 8])
return fliplist[unit - 8]->next->image;
return (const char *) NULL;
}
const char *fliplist_get_prev(unsigned int unit)
{
if (fliplist[unit - 8])
return fliplist[unit - 8]->prev->image;
return (const char *) NULL;
}
const char *fliplist_get_image(fliplist_t fl)
{
return fl->image;
}
unsigned int fliplist_get_unit(fliplist_t fl)
{
return fl->unit;
}
void fliplist_add_image(unsigned int unit)
{
fliplist_t n;
if (current_image == NULL)
return;
if (strcmp(current_image, "") == 0)
return;
n = lib_malloc(sizeof(struct fliplist_s));
n->image = lib_stralloc(current_image);
unit = n->unit = current_drive;
log_message(LOG_DEFAULT, "Adding `%s' to fliplist[%d]", n->image, unit);
if (fliplist[unit - 8]) {
n->next = fliplist[unit - 8];
n->prev = fliplist[unit - 8]->prev;
n->next->prev = n;
n->prev->next = n;
fliplist[unit - 8] = n;
} else {
fliplist[unit - 8] = n;
n->next = n;
n->prev = n;
}
show_fliplist(unit);
}
void fliplist_remove(unsigned int unit, const char *image)
{
fliplist_t tmp;
if (fliplist[unit - 8] == NULL)
return;
if (image == (char *) NULL) {
/* no image given, so remove the head */
if ((fliplist[unit - 8] == fliplist[unit - 8]->next) &&
(fliplist[unit - 8] == fliplist[unit - 8]->prev)) {
/* this is the last entry */
tmp = fliplist[unit - 8];
fliplist[unit - 8] = (fliplist_t) NULL;
} else {
fliplist[unit - 8]->next->prev = fliplist[unit - 8]->prev;
fliplist[unit - 8]->prev->next = fliplist[unit - 8]->next;
tmp = fliplist[unit - 8];
fliplist[unit - 8] = fliplist[unit - 8]->next;
}
log_message(LOG_DEFAULT, "Removing `%s' from fliplist[%d]",
tmp->image, unit);
lib_free(tmp->image);
lib_free(tmp);
show_fliplist(unit);
return;
} else {
/* do a lookup and remove it */
fliplist_t it = fliplist[unit - 8];
if (strcmp(it->image, image) == 0) {
/* it's the head */
fliplist_remove(unit, NULL);
return;
}
it = it->next;
while ((strcmp(it->image, image) != 0) &&
(it != fliplist[unit - 8]))
it = it->next;
if (it == fliplist[unit - 8]) {
log_message(LOG_DEFAULT,
"Cannot remove `%s'; not found in fliplist[%d]",
it->image, unit);
return;
}
it->next->prev = it->prev;
it->prev->next = it->next;
lib_free(it->image);
lib_free(it);
show_fliplist(unit);
}
}
void fliplist_attach_head (unsigned int unit, int direction)
{
if (fliplist[unit - 8] == NULL)
return;
if (direction)
fliplist[unit - 8] = fliplist[unit - 8]->next;
else
fliplist[unit - 8] = fliplist[unit - 8]->prev;
if (file_system_attach_disk(fliplist[unit - 8]->unit,
fliplist[unit - 8]->image) < 0) {
/* shouldn't happen, so ignore it */
;
}
}
fliplist_t fliplist_init_iterate(unsigned int unit)
{
fliplist_t ret = NULL;
iterator = fliplist[unit - 8];
if (iterator) {
ret = iterator;
iterator = iterator->next;
}
return ret;
}
fliplist_t fliplist_next_iterate(unsigned int unit)
{
fliplist_t ret = NULL;
if (iterator) {
if (iterator != fliplist[unit - 8]) {
ret = iterator;
iterator=iterator->next;
}
}
return ret;
}
void fliplist_clear_list(unsigned int unit)
{
fliplist_t flip = fliplist[unit - 8];
if (flip != NULL) {
do {
fliplist_t tmp = flip->next;
lib_free(flip->image);
lib_free(flip);
flip = tmp;
}
while (flip != fliplist[unit - 8]);
fliplist[unit - 8] = NULL;
}
}
int fliplist_save_list(unsigned int unit, const char *filename)
{
int all_units = 0;
fliplist_t flip;
FILE *fp = NULL;
if (unit == (unsigned int)-1) {
all_units = 1;
unit = 8;
}
do {
flip = fliplist[unit - 8];
if (flip != NULL) {
if (!fp) {
if ((fp = fopen(filename, MODE_WRITE)) == NULL)
return -1;
fprintf(fp, "%s\n\n", flip_file_header);
}
fprintf(fp, "UNIT %d\n", unit);
do {
fprintf(fp, "%s\n", flip->image);
flip = flip->next;
}
while (flip != fliplist[unit - 8]);
}
unit++;
} while (all_units && ((unit - 8) < NUM_DRIVES));
if (fp)
fclose(fp);
return 0;
}
int fliplist_load_list(unsigned int unit, const char *filename, int autoattach)
{
FILE *fp;
char buffer[buffer_size];
int all_units = 0, i;
if (filename == NULL || *filename == 0 || (fp = fopen(filename, MODE_READ)) == NULL)
return -1;
buffer[0] = '\0';
fgets(buffer, buffer_size, fp);
if (strncmp(buffer, flip_file_header, strlen(flip_file_header)) != 0) {
log_message(LOG_DEFAULT, "File %s is not a fliplist file", filename);
fclose(fp);
return -1;
}
if (unit == (unsigned int)-1) {
all_units = 1;
for (i = 0; i < NUM_DRIVES; i++)
fliplist_clear_list(i+8);
}
else
fliplist_clear_list(unit);
while (!feof(fp)) {
char *b;
buffer[0] = '\0';
fgets(buffer, buffer_size, fp);
if (strncmp("UNIT ", buffer, 5) == 0) {
if (all_units != 0) {
long unit_long;
util_string_to_long(buffer + 5, NULL, 10, &unit_long);
unit = (unsigned int)unit_long;
}
continue;
}
/* remove trailing whitespace (linefeeds etc) */
b = buffer + strlen(buffer);
while ((b > buffer) && (isspace((unsigned int)(b[-1]))))
b--;
if (b > buffer) {
fliplist_t tmp;
*b = '\0';
if (unit == (unsigned int)-1) {
log_message(LOG_DEFAULT, "Fliplist has inconsistent view for unit, assuming 8.\n");
unit = 8;
}
tmp = lib_malloc(sizeof(struct fliplist_s));
tmp->image = lib_stralloc(buffer);
tmp->unit = unit;
if (fliplist[unit - 8] == NULL) {
fliplist[unit - 8] = tmp;
tmp->prev = tmp;
tmp->next = tmp;
} else {
tmp->next = fliplist[unit - 8];
tmp->prev = fliplist[unit - 8]->prev;
tmp->next->prev = tmp;
tmp->prev->next = tmp;
fliplist[unit - 8] = tmp;
}
}
}
current_drive = unit;
fclose(fp);
if (all_units) {
for (i = 0; i < NUM_DRIVES; i++)
show_fliplist(i + 8);
} else
show_fliplist(unit);
if (autoattach)
fliplist_attach_head(unit, 1);
return 0;
}
/* ------------------------------------------------------------------------- */
static void show_fliplist(unsigned int unit)
{
fliplist_t it = fliplist[unit - 8];
log_message(LOG_DEFAULT, "Fliplist[%d] contains:", unit);
if (it) {
do {
log_message(LOG_DEFAULT,
"\tUnit %d %s (n: %s, p:%s)", it->unit, it->image,
it->next->image, it->prev->image);
it = it->next;
} while (it != fliplist[unit - 8]);
} else
log_message(LOG_DEFAULT, "\tnothing");
}