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
|
/*
DiskImageTool - Tool for manipulating disk images
Copyright (C) 2019 Ian Cowburn (ianc@noddybox.co.uk)
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 3 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, see <http://www.gnu.org/licenses/>.
-------------------------------------------------------------------------
Main
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "util.h"
#include "handler.h"
#include "cpc.h"
/* ---------------------------------------- MACROS
*/
/* ---------------------------------------- VERSION INFO
*/
static const char *ditool_version =
"Disk Image Tool Version 1.0 development\n"
"\n"
"This program is distributed in the hope that it will be useful,\n"
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
"GNU General Public License (Version 3) for more details.\n"
"\n";
/* ---------------------------------------- TYPES
*/
/* ---------------------------------------- GLOBALS
*/
static int g_no_handlers;
static Handler *g_handler;
static Handler *g_current;
static int g_open = 0;
/* ---------------------------------------- DISK HANDLER REGISTER
*/
static void DITOOL_Initialise(void)
{
CPC_Initialise();
}
void DITOOL_Register(const Handler *handler)
{
g_no_handlers++;
g_handler = Realloc(g_handler, sizeof *g_handler * g_no_handlers);
g_handler[g_no_handlers - 1] = *handler;
}
/* ---------------------------------------- COMMAND ROUTINES
*/
static void Help(void)
{
printf(
"handlers - List available disk image handlers.\n"
"current <handler> - Set current handler.\n"
"open <file> - Try and open disk image. Changes current handler.\n"
"close [<file>] - Close the current disk image. If the optional name\n"
" name is provided the image is saved to that file.\n"
"create <file> - Create new disk image using current handler.\n"
"ls - List files in disk image.\n"
"info - Get info on the disk image.\n"
"get <name> <file> - Get named file from disk image and store it in file.\n"
"put <name> <file> - Put file into disk image calling it name.\n"
"delete <name> - Delete named file from disk image.\n"
"quit - Quit.\n"
);
}
static void Current(char * const tok[])
{
int f;
int found = 0;
if (tok[1])
{
for(f = 0; f < g_no_handlers; f++)
{
if (strcmp(g_handler[f].id, tok[1]) == 0)
{
if (g_open && g_current)
{
g_current->close_image(NULL);
g_open = 0;
}
g_current = g_handler + f;
found = 1;
printf("Set current handler to %s\n",
g_current->name);
}
}
if (!found)
{
printf("unknown handler %s\n", tok[1]);
}
}
else
{
printf("Missing argument\n");
}
}
static void Handlers(char * const tok[])
{
int f;
for(f = 0; f < g_no_handlers; f++)
{
printf("%s - %s\n", g_handler[f].id, g_handler[f].name);
}
}
static void Open(char * const tok[])
{
if (tok[1])
{
void *mem;
size_t size;
mem = Load(tok[1], &size);
if (mem)
{
int f;
if (g_open && g_current)
{
g_current->close_image(NULL);
g_open = 0;
}
g_current = NULL;
for(f = 0; f < g_no_handlers && !g_current; f++)
{
if (g_handler[f].is_my_file(mem, size))
{
g_current = g_handler + f;
}
}
if (g_current)
{
if (g_current->open_image(mem, size))
{
g_open = 1;
printf("Set current handler to %s\n",
g_current->name);
}
else
{
g_open = 0;
printf("%s\n", g_current->last_error());
}
}
else
{
free(mem);
printf("No handler for file %s\n", tok[1]);
}
}
else
{
printf("Failed to open %s\n", tok[1]);
}
}
else
{
printf("Missing argument\n");
}
}
static void Close(char * const tok[])
{
if (g_open && g_current)
{
g_current->close_image(tok[1]);
g_open = 0;
}
else
{
printf("No open image\n");
}
}
static void Create(char * const tok[])
{
if (tok[1])
{
if (g_current)
{
if (g_open)
{
g_current->close_image(NULL);
g_open = 0;
}
if (!g_current->create(tok[1]))
{
printf("%s\n", g_current->last_error());
}
}
else
{
printf("No current handler\n");
}
}
else
{
printf("Missing argument\n");
}
}
static void Ls(char * const tok[])
{
if (g_current && g_open)
{
g_current->list_files();
}
else
{
printf("No open image\n");
}
}
static void Info(char * const tok[])
{
if (g_current && g_open)
{
g_current->list_info();
}
else
{
printf("No open image\n");
}
}
static void Get(char * const tok[])
{
if (tok[2])
{
if (g_current && g_open)
{
if (!g_current->get_file(tok[1], tok[2]))
{
printf("%s\n", g_current->last_error());
}
}
else
{
printf("No open image\n");
}
}
else
{
printf("Missing arguments\n");
}
}
static void Put(char * const tok[])
{
if (tok[2])
{
if (g_current && g_open)
{
if (!g_current->put_file(tok[1], tok[2]))
{
printf("%s\n", g_current->last_error());
}
}
else
{
printf("No open image\n");
}
}
else
{
printf("Missing arguments\n");
}
}
static void Delete(char * const tok[])
{
if (tok[1])
{
if (g_current && g_open)
{
if (!g_current->delete_file(tok[1]))
{
printf("%s\n", g_current->last_error());
}
}
else
{
printf("No open image\n");
}
}
else
{
printf("Missing argument\n");
}
}
/* ---------------------------------------- MAIN
*/
int main(int argc, char *argv[])
{
char buff[1024];
int quit = 0;
printf("%s", ditool_version);
printf("Type help for help\n");
DITOOL_Initialise();
while(!quit && GetLine(g_current ? g_current->id : "DITOOL",
buff, sizeof buff))
{
char *tok[3] = {0};
tok[0] = strtok(buff, " \t");
if (tok[0])
{
tok[1] = strtok(NULL, " \t");
if (tok[1])
{
tok[2] = strtok(NULL, " \t");
}
if (strcmp(tok[0], "quit") == 0)
{
quit = 1;
}
else if (strcmp(tok[0], "help") == 0 || strcmp(tok[0], "?") == 0)
{
Help();
}
else if (strcmp(tok[0], "current") == 0)
{
Current(tok);
}
else if (strcmp(tok[0], "handlers") == 0)
{
Handlers(tok);
}
else if (strcmp(tok[0], "open") == 0)
{
Open(tok);
}
else if (strcmp(tok[0], "close") == 0)
{
Close(tok);
}
else if (strcmp(tok[0], "create") == 0)
{
Create(tok);
}
else if (strcmp(tok[0], "ls") == 0)
{
Ls(tok);
}
else if (strcmp(tok[0], "info") == 0)
{
Info(tok);
}
else if (strcmp(tok[0], "get") == 0)
{
Get(tok);
}
else if (strcmp(tok[0], "put") == 0)
{
Put(tok);
}
else if (strcmp(tok[0], "delete") == 0)
{
Delete(tok);
}
else
{
printf("unknown command %s - type help for help\n", tok[0]);
}
}
}
return EXIT_SUCCESS;
}
/*
vim: ai sw=4 ts=8 expandtab
*/
|