-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgwhist.c
402 lines (325 loc) · 8.52 KB
/
gwhist.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
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <getopt.h>
#include <ctype.h>
#include "gw.h"
#include "gwx.h"
#include "gwhisto.h"
#include "msg_levels.h"
#include "msg.h"
#include "gwfddrv.h"
#include "gwdetect.h"
static const struct option cmd_long_args[] = {
{ "drive", required_argument, NULL, 'd' },
{ "revs", required_argument, NULL, 'r' },
{ "side", required_argument, NULL, 's' },
{ "track", required_argument, NULL, 't' },
{ "logfile", required_argument, NULL, 'u' },
{ "verbosity", required_argument, NULL, 'v' },
{ "device", required_argument, NULL, 'G' },
{ "stepdelay", required_argument, NULL, 'T' },
{ "gwlogfile", required_argument, NULL, 'U' },
/* Start of binary long options without single letter counterparts. */
{ "hd", no_argument, NULL, 0 },
{ "dd", no_argument, NULL, 0 },
{ "reset", no_argument, NULL, 0 },
{ "noreset", no_argument, NULL, 0 },
{ 0, 0, 0, 0 }
};
#if defined(WIN64) || defined(WIN32)
static const char gwsd_def[] = "\\\\.\\COM3";
#else
static const char gwsd_def[] = "/dev/ttyACM0";
#endif
static struct cmd_settings {
struct gw_fddrv fdd;
int track;
int side;
int revs;
bool reset_on_init;
int scrn_verbosity;
int file_verbosity;
const char *logfile;
const char *devlogfile;
} cmd_settings = {
.fdd.device = gwsd_def,
.fdd.bus = BUS_IBMPC,
.fdd.drive = -1,
.fdd.kind = -1,
.fdd.tracks = -1,
.fdd.sides = -1,
.fdd.steps = -1,
.fdd.densel = DS_NOTSET,
.fdd.step_ms = -1,
.fdd.settle_ms = -1,
.track = 0,
.side = 0,
.revs = 1,
.reset_on_init = true,
.scrn_verbosity = MSG_NORMAL,
.file_verbosity = MSG_QUIET,
.logfile = "gw.log",
.devlogfile = NULL
};
static void
fatal_bad_number(const char *name)
{
msg_fatal("%s requires a numeric argument.\n", name);
}
/*
* Like strtol, but exit with a fatal error message if there are any
* invalid characters or the string is empty.
*/
static long int
strtol_strict(const char *nptr, int base, const char *name)
{
char *endptr;
long int res = strtol(nptr, &endptr, base);
if (*nptr == '\0' || *endptr != '\0')
fatal_bad_number(name);
return res;
}
static void
usage(const char *pgm_name)
{
msg_fatal("Usage: %s [-G device] [-d drive] "
"[-t track] [-s side] [-r revs] "
"[--dd|--hd] [-T stp[,stl]] --[no]reset "
"[-v verbosity] [-u logfile] [-U gwlogfile]\n",
pgm_name);
}
static void
parse_args(int argc, char **argv, struct cmd_settings *cmd_set)
{
int opt;
int lindex = 0;
while ((opt = getopt_long(argc, argv, "d:r:s:t:u:v:G:T:U:",
cmd_long_args, &lindex)) != -1) {
switch(opt) {
case 0:;
const char *name = cmd_long_args[lindex].name;
if (!strcmp(name, "hd")) {
cmd_set->fdd.densel = DS_HD;
} else if (!strcmp(name, "dd")) {
cmd_set->fdd.densel = DS_DD;
} else if (!strcmp(name, "reset")) {
cmd_set->reset_on_init = true;
} else if (!strcmp(name, "noreset")) {
cmd_set->reset_on_init = false;
} else {
goto err_usage;
}
break;
case 'd':
if (optarg[1]) goto d_err;
const int loarg = tolower(optarg[0]);
switch(loarg) {
case '0':
case '1':
case '2':
cmd_set->fdd.bus = BUS_SHUGART;
cmd_set->fdd.drive = loarg - '0';
break;
case 'a':
case 'b':
cmd_set->fdd.bus = BUS_IBMPC;
cmd_set->fdd.drive = loarg - 'a';
break;
default: d_err:
msg_error("Option-argument to '%c' must "
"be 0, 1, 2, a, or b.\n", opt);
goto err_usage;
}
break;
case 'r':;
const int revs = strtol_strict(optarg, 10, "'r'");
if (revs >= 0) {
cmd_set->revs = revs;
} else {
msg_error("Option-argument to '%c' must "
"be greater than 0.\n", opt);
goto err_usage;
}
break;
case 's':;
const int side = strtol_strict(optarg, 10, "'s'");
if (side >= 0 && side <= 1) {
cmd_set->side = side;
} else {
msg_error("Option-argument to '%c' must "
"be 0 or 1.\n", opt);
goto err_usage;
}
break;
case 't':;
const int track = strtol_strict(optarg, 10, "'t'");
if (track >= 0 && track <= GW_MAX_TRACKS) {
cmd_set->track = track;
} else {
msg_error("Option-argument to '%c' must "
"be between 0 and %d.\n",
opt, GW_MAX_TRACKS);
goto err_usage;
}
break;
case 'u':
cmd_set->logfile = optarg;
break;
case 'v':;
const char vstr[] = "'v'";
char *p, *endptr;
int optav;
if ((p = strchr(optarg, ':'))) {
optav = strtol(optarg, &endptr, 10);
if ((optarg == endptr) || (endptr != p))
fatal_bad_number(vstr);
cmd_set->scrn_verbosity =
strtol_strict(p+1, 10, vstr);
cmd_set->file_verbosity = optav;
} else {
optav = strtol_strict(optarg, 10, vstr);
if (optav >= 10) {
cmd_set->scrn_verbosity = optav % 10;
cmd_set->file_verbosity = optav / 10;
} else {
cmd_set->scrn_verbosity = optav;
cmd_set->file_verbosity = optav;
}
}
break;
case 'G':;
#if defined(WIN32) || defined(WIN64)
/* If the user specified a 2 digit COM device
* without the '\\\\.\\' prefix, prefix their string
* with it. Always malloc the string on MSW so we
* always know we can free() if it needed on this OS.
*/
char *ds;
if (strncasecmp("COM", optarg, 3) == 0 &&
isdigit(optarg[3]) &&
isdigit(optarg[4]) &&
!optarg[5]) {
ds = malloc(4 + 5 + 1);
if (ds) {
strcpy(ds, "\\\\.\\");
strcpy(ds + 4, optarg);
}
} else {
ds = strdup(optarg);
}
if (!ds)
msg_fatal("Cannot allocate device name.\n");
cmd_set->fdd.device = ds;
#else
cmd_set->fdd.device = optarg;
#endif
break;
case 'T':;
unsigned int step_ms, settle_ms;
int sfn = sscanf(optarg, "%u,%u", &step_ms, &settle_ms);
switch (sfn) {
case 2:
if (settle_ms > 65000) goto err_usage;
cmd_set->fdd.settle_ms = settle_ms;
/* FALLTHRU */
case 1:
if (step_ms > 65) goto err_usage;
cmd_set->fdd.step_ms = step_ms;
break;
default:
goto err_usage;
break;
}
break;
case 'U':
cmd_set->devlogfile = optarg;
break;
default: /* '?' */
goto err_usage;
break;
}
}
if (optind != argc)
goto err_usage;
msg_scrn_set_level(cmd_set->scrn_verbosity);
msg_file_set_level(cmd_set->file_verbosity);
if ((cmd_set->file_verbosity > MSG_QUIET) && cmd_set->logfile) {
if (!msg_fopen(cmd_set->logfile)) {
msg_error("Failed to open log file '%s': %s\n",
cmd_set->logfile, strerror(errno));
goto err_usage;
}
}
if (cmd_set->devlogfile) {
FILE *dlfp = fopen(cmd_set->devlogfile, "w");
if (!dlfp) {
msg_error("Failed to open device log file '%s': %s\n",
cmd_set->devlogfile, strerror(errno));
goto err_usage;
}
if (!gw_set_logfp(dlfp)) {
msg_error("Failed to set device log file '%s': %s\n",
cmd_set->devlogfile, strerror(errno));
goto err_usage;
}
}
return;
err_usage:
usage(argv[0]);
}
int
main(int argc, char **argv)
{
const char *slash = strrchr(argv[0], '/');
const char *pgm = slash ? slash + 1 : argv[0];
if (!msg_error_prefix(pgm)) {
msg_error("Failure to allocate memory for message prefix.\n");
return EXIT_FAILURE;
}
parse_args(argc, argv, &cmd_settings);
const char *gwsd = cmd_settings.fdd.device;
gw_devt gwfd = gw_open(gwsd);
if (gwfd == GW_DEVT_INVALID)
msg_fatal("Failed to open Greaseweazle's serial device "
"'%s': %s.\n", gwsd, strerror(errno));
gw_init(gwfd);
if (cmd_settings.reset_on_init)
gw_reset(gwfd);
if (cmd_settings.fdd.drive == -1)
gw_detect_drive(&cmd_settings.fdd);
struct gw_info gw_info;
int cmd_ret = gw_get_info(gwfd, &gw_info);
if (cmd_ret != ACK_OKAY) {
// error handling
return EXIT_FAILURE;
}
gw_set_bus_type(gwfd, cmd_settings.fdd.bus);
gw_setdrive(gwfd, cmd_settings.fdd.drive,
cmd_settings.fdd.densel == DS_HD ? DS_HD : DS_DD);
msg(MSG_NORMAL, "Reading track %d, side %d...\n",
cmd_settings.track, cmd_settings.side);
struct histogram histo;
histo_init(cmd_settings.track, cmd_settings.side, cmd_settings.revs,
gw_info.sample_freq, TICKS_PER_BUCKET, &histo);
int chft_ret = collect_histo_from_track(gwfd, &histo);
if (chft_ret > 0) {
msg_fatal("%s (%d)%s\n", gw_cmd_ack(chft_ret), chft_ret,
chft_ret == ACK_NO_INDEX ?
" [Is diskette in drive?]" : "");
} else if (chft_ret < 0) {
msg_fatal("Couldn't collect histogram. Internal error.\n");
}
struct histo_analysis ha;
histo_analysis_init(&ha);
histo_analyze(&histo, &ha);
histo_show(MSG_NORMAL, &histo, &ha);
gw_unsetdrive(gwfd, cmd_settings.fdd.drive);
gw_close(gwfd);
// error checking
msg_fclose();
return EXIT_SUCCESS;
}