-
Notifications
You must be signed in to change notification settings - Fork 0
/
ckRecorder.c
751 lines (702 loc) · 19.9 KB
/
ckRecorder.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
/*
* ckRecorder.c --
*
* This file provides a simple event recorder.
*
* Copyright (c) 1996-1999 Christian Werner
* Copyright (c) 2022 vzvca
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "ckPort.h"
#include "ck.h"
/*
* There is one structure of the following type for the global data
* of the recorder.
*/
typedef struct {
CkWindow *mainPtr;
Tcl_Interp *interp;
int timerRunning;
Tk_TimerToken timer;
Tcl_Time lastEvent;
Tcl_Channel record;
Tcl_Channel replay;
int withDelay;
CkEvent event;
} Recorder;
static Recorder *ckRecorder = NULL;
/*
* Internal procedures.
*/
static int RecorderInput _ANSI_ARGS_((ClientData clientData,
CkEvent *eventPtr));
static int DStringGets _ANSI_ARGS_((Tcl_Channel chan,
Tcl_DString *dsPtr));
static void DeliverEvent _ANSI_ARGS_((ClientData clientData));
static void RecorderReplay _ANSI_ARGS_((ClientData clientData));
/*
*----------------------------------------------------------------------
*
* RecorderInput --
*
* This procedure is installed as generic event handler.
* For certain events it adds lines to the recorder file.
*
*----------------------------------------------------------------------
*/
static int
RecorderInput(clientData, eventPtr)
ClientData clientData;
CkEvent *eventPtr;
{
Recorder *recPtr = (Recorder *) clientData;
int hadEvent = 0, type = eventPtr->any.type;
Tcl_Time now;
extern void TclpGetTime _ANSI_ARGS_((Tcl_Time *timePtr));
char buffer[64];
char *keySym, *barCode, *result;
char *argv[16];
if (recPtr->record == NULL) {
Ck_DeleteGenericHandler(RecorderInput, clientData);
return 0;
}
if (type != CK_EV_KEYPRESS && type != CK_EV_BARCODE &&
type != CK_EV_MOUSE_UP && type != CK_EV_MOUSE_DOWN)
return 0;
TclpGetTime(&now);
if (recPtr->withDelay && recPtr->lastEvent.sec != 0 &&
recPtr->lastEvent.usec != 0) {
double diff;
char string[100];
diff = now.sec * 1000 + now.usec / 1000;
diff -= recPtr->lastEvent.sec * 1000 +
recPtr->lastEvent.usec / 1000;
if (diff > 50) {
if (diff > 3600000)
diff = 3600000;
sprintf(string, "<Delay> %d\n", (int) diff);
Tcl_Write(recPtr->record, string, strlen(string));
hadEvent++;
}
}
switch (type) {
case CK_EV_KEYPRESS:
argv[2] = NULL;
keySym = CkKeysymToString(eventPtr->key.keycode, 1);
if (strcmp(keySym, "NoSymbol") != 0)
argv[2] = keySym;
else if (eventPtr->key.keycode > 0 &&
eventPtr->key.keycode < 256) {
/* Unsafe, ie not portable */
sprintf(buffer, "0x%2x", eventPtr->key.keycode);
argv[2] = buffer;
}
if (argv[2] != NULL) {
argv[0] = "<Key>";
argv[1] = eventPtr->key.winPtr == NULL ? "" :
eventPtr->key.winPtr->pathName;
result = Tcl_Merge(3, argv);
printPctSNL:
Tcl_Write(recPtr->record, result, strlen(result));
Tcl_Write(recPtr->record, "\n", 1);
ckfree(result);
hadEvent++;
}
break;
case CK_EV_BARCODE:
barCode = CkGetBarcodeData(recPtr->mainPtr->mainPtr);
if (barCode != NULL) {
argv[0] = "<BarCode>";
argv[1] = eventPtr->key.winPtr == NULL ? "" :
eventPtr->key.winPtr->pathName;
argv[2] = barCode;
result = Tcl_Merge(3, argv);
goto printPctSNL;
}
break;
case CK_EV_MOUSE_UP:
case CK_EV_MOUSE_DOWN:
case CK_EV_MOUSE_MOVE:
{
char bbuf[16], xbuf[16], ybuf[16], rxbuf[16], rybuf[16];
switch(type) {
case CK_EV_MOUSE_UP:
argv[0] = "<ButtonPress>";
break;
case CK_EV_MOUSE_DOWN:
argv[0] = "<ButtonRelease>";
break;
case CK_EV_MOUSE_MOVE:
argv[0] = "<Motion>";
break;
}
argv[1] = eventPtr->mouse.winPtr == NULL ? "" :
eventPtr->mouse.winPtr->pathName;
/* the button encodes the modifiers */
sprintf(bbuf, "%d", eventPtr->mouse.button);
argv[2] = bbuf;
sprintf(xbuf, "%d", eventPtr->mouse.x);
argv[3] = xbuf;
sprintf(ybuf, "%d", eventPtr->mouse.y);
argv[4] = ybuf;
sprintf(rxbuf, "%d", eventPtr->mouse.rootx);
argv[5] = rxbuf;
sprintf(rybuf, "%d", eventPtr->mouse.rooty);
argv[6] = rybuf;
result = Tcl_Merge(7, argv);
goto printPctSNL;
}
break;
}
if (hadEvent) {
Tcl_Flush(recPtr->record);
recPtr->lastEvent = now;
}
return 0;
}
/*
*----------------------------------------------------------------------
*
* DStringGets --
*
* Similar to the fgets library routine, a dynamic string is
* read from a file. Can deal with backslash-newline continuation.
* lines.
*
* Results:
* A standard Tcl result.
*
*----------------------------------------------------------------------
*/
static int
DStringGets(chan, dsPtr)
Tcl_Channel chan;
Tcl_DString *dsPtr;
{
char *p;
int length, code;
for (;;) {
code = Tcl_Gets(chan, dsPtr);
length = Tcl_DStringLength(dsPtr);
if (code == -1)
return length == 0 ? TCL_ERROR : TCL_OK;
if (length > 0) {
p = Tcl_DStringValue(dsPtr) + length - 1;
if (*p != '\\')
return TCL_OK;
*p = ' ';
} else {
return TCL_OK;
}
}
/* Not reached. */
}
/*
*----------------------------------------------------------------------
*
* DeliverEvent --
*
* Call by do-when-idle mechanism, dispatched by replay handler.
* Deliver event, but first reschedule replay handler. This order
* is essential !
*
*----------------------------------------------------------------------
*/
static void
DeliverEvent(clientData)
ClientData clientData;
{
Recorder *recPtr = (Recorder *) clientData;
Tk_DoWhenIdle(RecorderReplay, (ClientData) recPtr);
Ck_HandleEvent(recPtr->mainPtr->mainPtr, &recPtr->event);
}
/*
*----------------------------------------------------------------------
*
* RecorderReplay --
*
* Replay handler, called by the do-when-idle mechanism or by a
* timer's expiration.
*
* Results:
* None.
*
*----------------------------------------------------------------------
*/
static void
RecorderReplay(clientData)
ClientData clientData;
{
Recorder *recPtr = (Recorder *) clientData;
Tcl_DString input;
char *p;
int getsResult, delayValue = 0, doidle = 1;
recPtr->timerRunning = 0;
if (recPtr->replay == NULL)
return;
Tcl_DStringInit(&input);
while ((getsResult = DStringGets(recPtr->replay, &input)) == TCL_OK) {
p = Tcl_DStringValue(&input);
while (*p == ' ' || *p == '\t')
++p;
if (*p == '#') {
Tcl_DStringTrunc(&input, 0);
continue;
}
if (*p == '<') {
CkEvent event;
int cmdError = TCL_OK, deliver = 0;
int argc;
char **argv;
if (Tcl_SplitList(recPtr->interp, p, &argc, &argv) != TCL_OK) {
Tk_BackgroundError(recPtr->interp);
getsResult = TCL_ERROR;
break;
}
if (strcmp(argv[0], "<Delay>") == 0) {
if (argc != 2) {
badNumArgs:
Tcl_AppendResult(recPtr->interp,
"wrong # args for ", argv[0], (char *) NULL);
cmdError = TCL_ERROR;
} else
cmdError = Tcl_GetInt(recPtr->interp, argv[1],
&delayValue);
} else if (strcmp(argv[0], "<Key>") == 0) {
int keySym;
if (argc != 3)
goto badNumArgs;
event.any.type = CK_EV_KEYPRESS;
if (argv[1][0] == '\0')
event.any.winPtr = NULL;
else if ((event.any.winPtr = Ck_NameToWindow(recPtr->interp,
argv[1], recPtr->mainPtr)) == NULL)
cmdError = TCL_ERROR;
else if (strncmp(argv[2], "Control-", 8) == 0 &&
strlen(argv[2]) == 9) {
event.key.keycode = argv[2][8] - 0x40;
if (event.key.keycode > 0x20)
event.key.keycode -= 0x20;
deliver++;
} else if (strncmp(argv[2], "0x", 2) == 0 &&
strlen(argv[2]) == 4) {
sscanf(&argv[2][2], "%x", &event.key.keycode);
deliver++;
} else if ((keySym = CkStringToKeysym(argv[2])) != NoSymbol) {
event.key.keycode = keySym;
deliver++;
}
} else if (strcmp(argv[0], "<BarCode>") == 0) {
if (argc != 3)
goto badNumArgs;
} else if (strcmp(argv[0], "<ButtonPress>") == 0) {
if (argc != 7)
goto badNumArgs;
event.any.type = CK_EV_MOUSE_DOWN;
doMouse:
if (argv[1][0] == '\0')
event.any.winPtr = NULL;
else if ((event.any.winPtr = Ck_NameToWindow(recPtr->interp,
argv[1], recPtr->mainPtr)) == NULL)
cmdError = TCL_ERROR;
else {
cmdError |= Tcl_GetInt(recPtr->interp, argv[2],
&event.mouse.button);
cmdError |= Tcl_GetInt(recPtr->interp, argv[3],
&event.mouse.x);
cmdError |= Tcl_GetInt(recPtr->interp, argv[4],
&event.mouse.y);
cmdError |= Tcl_GetInt(recPtr->interp, argv[5],
&event.mouse.rootx);
cmdError |= Tcl_GetInt(recPtr->interp, argv[6],
&event.mouse.rooty);
if (cmdError == TCL_OK)
deliver++;
}
} else if (strcmp(argv[0], "<ButtonRelease>") == 0) {
if (argc != 7)
goto badNumArgs;
event.any.type = CK_EV_MOUSE_UP;
goto doMouse;
} else if (strcmp(argv[0], "<Motion>") == 0) {
if (argc != 7)
goto badNumArgs;
event.any.type = CK_EV_MOUSE_MOVE;
goto doMouse;
}
ckfree((char *) argv);
if (cmdError != TCL_OK) {
Tk_BackgroundError(recPtr->interp);
getsResult = cmdError;
} else if (deliver) {
doidle = delayValue = 0;
recPtr->event = event;
Tk_DoWhenIdle(DeliverEvent, (ClientData) recPtr);
}
break;
} else if (Tcl_GlobalEval(recPtr->interp, p) != TCL_OK) {
Tk_BackgroundError(recPtr->interp);
getsResult = TCL_ERROR;
break;
}
Tcl_DStringTrunc(&input, 0);
}
if (getsResult != TCL_OK) {
Tcl_Close(NULL, recPtr->replay);
recPtr->replay = NULL;
} else if (delayValue != 0) {
recPtr->timerRunning = 1;
recPtr->timer = Tk_CreateTimerHandler(delayValue, RecorderReplay,
(ClientData) recPtr);
} else if (doidle != 0) {
Tk_DoWhenIdle(RecorderReplay, (ClientData) recPtr);
}
Tcl_DStringFree(&input);
}
/*
*----------------------------------------------------------------------
*
* Ck_RecorderCmd --
*
* This procedure is invoked to process the "recorder" Tcl command.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*----------------------------------------------------------------------
*/
int
Ck_RecorderCmd(clientData, interp, argc, argv)
ClientData clientData; /* Main window associated with
* interpreter. */
Tcl_Interp *interp; /* Current interpreter. */
int argc; /* Number of arguments. */
char **argv; /* Argument strings. */
{
Recorder *recPtr = ckRecorder;
CkWindow *mainPtr = (CkWindow *) clientData;
int length;
char c;
if (recPtr == NULL) {
recPtr = (Recorder *) ckalloc(sizeof (Recorder));
recPtr->mainPtr = mainPtr;
recPtr->interp = NULL;
recPtr->timerRunning = 0;
recPtr->lastEvent.sec = recPtr->lastEvent.usec = 0;
recPtr->record = NULL;
recPtr->replay = NULL;
recPtr->withDelay = 0;
ckRecorder = recPtr;
}
if (argc < 2) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " option ?arg?\"", (char *) NULL);
return TCL_ERROR;
}
c = argv[1][0];
length = strlen(argv[1]);
if ((c == 'r') && (strncmp(argv[1], "replay", length) == 0)) {
char *fileName;
Tcl_DString buffer;
Tcl_Channel newReplay;
if (argc != 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " replay fileName\"", (char *) NULL);
return TCL_ERROR;
}
fileName = Tcl_TranslateFileName(interp, argv[2], &buffer);
if (fileName == NULL) {
replayError:
Tcl_DStringFree(&buffer);
return TCL_ERROR;
}
newReplay = Tcl_OpenFileChannel(interp, fileName, "r", 0);
if (newReplay == NULL)
goto replayError;
Tcl_DStringFree(&buffer);
Tcl_Gets(newReplay, &buffer);
if (strncmp("# CK-RECORDER", Tcl_DStringValue(&buffer), 13) != 0) {
Tcl_Close(NULL, newReplay);
Tcl_AppendResult(interp, "invalid file for replay", (char *) NULL);
goto replayError;
}
if (recPtr->replay != NULL) {
if (recPtr->timerRunning)
Tk_DeleteTimerHandler(recPtr->timer);
Tcl_Close(NULL, recPtr->replay);
recPtr->timerRunning = 0;
}
recPtr->replay = newReplay;
recPtr->interp = interp;
Tk_DoWhenIdle(RecorderReplay, (ClientData) recPtr);
} else if ((c == 's') && (strncmp(argv[1], "start", length) == 0) &&
(length > 1)) {
char *fileName;
int withDelay = 0, fileArg = 2;
Tcl_DString buffer;
Tcl_Channel newRecord;
char *string;
if (argc < 3 || argc > 4) {
badStartArgs:
Tcl_AppendResult(interp, "wrong # or bad args: should be \"",
argv[0], " start ?-withdelay? fileName\"", (char *) NULL);
return TCL_ERROR;
}
if (argc == 4) {
if (strcmp(argv[2], "-withdelay") != 0)
goto badStartArgs;
withDelay++;
fileArg++;
}
fileName = Tcl_TranslateFileName(interp, argv[fileArg], &buffer);
if (fileName == NULL) {
startError:
Tcl_DStringFree(&buffer);
return TCL_ERROR;
}
newRecord = Tcl_OpenFileChannel(interp, fileName, "w", 0666);
if (newRecord == NULL)
goto startError;
if (recPtr->record != NULL)
Tcl_Close(NULL, recPtr->record);
else {
recPtr->lastEvent.sec = recPtr->lastEvent.usec = 0;
Ck_CreateGenericHandler(RecorderInput, (ClientData) recPtr);
}
recPtr->record = newRecord;
recPtr->withDelay = withDelay;
string = "# CK-RECORDER\n# ";
Tcl_Write(recPtr->record, string, strlen(string));
Tcl_Eval(interp, "clock format [clock seconds]");
Tcl_Write(recPtr->record, Tcl_GetString(Tcl_GetObjResult(interp)), -1);
Tcl_ResetResult(interp);
Tcl_Write(recPtr->record, "\n# ", 3);
string = Tcl_GetVar(interp, "argv0", TCL_GLOBAL_ONLY);
Tcl_Write(recPtr->record, string, strlen(string));
Tcl_Write(recPtr->record, " ", 1);
string = Tcl_GetVar(interp, "argv", TCL_GLOBAL_ONLY);
Tcl_Write(recPtr->record, string, strlen(string));
Tcl_Write(recPtr->record, "\n", 1);
Tcl_DStringFree(&buffer);
} else if ((c == 's') && (strncmp(argv[1], "stop", length) == 0) &&
(length > 1)) {
if (argc > 3) {
badStopArgs:
Tcl_AppendResult(interp, "wrong # or bad args: should be \"",
argv[0], " stop ?replay?\"", (char *) NULL);
return TCL_ERROR;
}
if (argc == 3) {
if (strcmp(argv[2], "replay") != 0)
goto badStopArgs;
if (recPtr->replay != NULL) {
if (recPtr->timerRunning)
Tk_DeleteTimerHandler(recPtr->timer);
Tcl_Close(NULL, recPtr->replay);
recPtr->replay = NULL;
recPtr->timerRunning = 0;
}
} else if (recPtr->record != NULL) {
Tcl_Close(NULL, recPtr->record);
Ck_DeleteGenericHandler(RecorderInput, (ClientData) recPtr);
recPtr->record = NULL;
}
} else {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " replay, start, or stop\"", (char *) NULL);
return TCL_ERROR;
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* Ck_RecorderCmdObj --
*
* This procedure is invoked to process the "recorder" Tcl command.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*----------------------------------------------------------------------
*/
int
Ck_RecorderCmdObj(clientData, interp, objc, objv)
ClientData clientData; /* Main window associated with
* interpreter. */
Tcl_Interp *interp; /* Current interpreter. */
int objc; /* Number of arguments. */
Tcl_Obj* CONST objv[]; /* Tcl_Obj* array of arguments. */
{
static char *commands[] =
{
"replay",
"start",
"stop",
NULL
};
enum
{
CMD_REPLAY,
CMD_START,
CMD_STOP
};
Recorder *recPtr = ckRecorder;
CkWindow *mainPtr = (CkWindow *) clientData;
int index;
if (recPtr == NULL) {
recPtr = (Recorder *) ckalloc(sizeof (Recorder));
recPtr->mainPtr = mainPtr;
recPtr->interp = NULL;
recPtr->timerRunning = 0;
recPtr->lastEvent.sec = recPtr->lastEvent.usec = 0;
recPtr->record = NULL;
recPtr->replay = NULL;
recPtr->withDelay = 0;
ckRecorder = recPtr;
}
if (objc < 2) {
Tcl_WrongNumArgs(interp, 1, objv, "option ?arg?");
return TCL_ERROR;
}
if (Tcl_GetIndexFromObj( interp, objv[1], commands, "option", TCL_EXACT, &index) != TCL_OK) {
return TCL_ERROR;
}
switch (index) {
case CMD_REPLAY:
{
char *fileName;
Tcl_DString buffer;
Tcl_Channel newReplay;
if (objc != 3) {
Tcl_WrongNumArgs( interp, 2, objv, "fileName");
return TCL_ERROR;
}
fileName = Tcl_TranslateFileName(interp, Tcl_GetString(objv[2]), &buffer);
if (fileName == NULL) {
replayError:
Tcl_DStringFree(&buffer);
return TCL_ERROR;
}
newReplay = Tcl_OpenFileChannel(interp, fileName, "r", 0);
if (newReplay == NULL)
goto replayError;
Tcl_DStringFree(&buffer);
Tcl_Gets(newReplay, &buffer);
if (strncmp("# CK-RECORDER", Tcl_DStringValue(&buffer), 13) != 0) {
Tcl_Close(NULL, newReplay);
Tcl_AppendResult(interp, "invalid file for replay", (char *) NULL);
goto replayError;
}
if (recPtr->replay != NULL) {
if (recPtr->timerRunning)
Tk_DeleteTimerHandler(recPtr->timer);
Tcl_Close(NULL, recPtr->replay);
recPtr->timerRunning = 0;
}
recPtr->replay = newReplay;
recPtr->interp = interp;
Tk_DoWhenIdle(RecorderReplay, (ClientData) recPtr);
}
break;
case CMD_START:
{
char *fileName;
int withDelay = 0, fileArg = 2;
Tcl_DString buffer;
Tcl_Channel newRecord;
char *string, *argv2;
if (objc < 3 || objc > 4) {
Tcl_WrongNumArgs(interp, 2, objv, "?-withdelay? fileName");
return TCL_ERROR;
}
argv2 = Tcl_GetString(objv[2]);
if (objc == 4) {
if (strcmp(argv2, "-withdelay") != 0) {
Tcl_AppendResult(interp, "expecting \"-withdelay\" but got \"",
argv2, "\"", (char *) NULL);
return TCL_ERROR;
}
withDelay++;
fileArg++;
}
fileName = Tcl_GetString(objv[fileArg]);
fileName = Tcl_TranslateFileName(interp, fileName, &buffer);
if (fileName == NULL) {
startError:
Tcl_DStringFree(&buffer);
return TCL_ERROR;
}
newRecord = Tcl_OpenFileChannel(interp, fileName, "w", 0666);
if (newRecord == NULL)
goto startError;
if (recPtr->record != NULL)
Tcl_Close(NULL, recPtr->record);
else {
recPtr->lastEvent.sec = recPtr->lastEvent.usec = 0;
Ck_CreateGenericHandler(RecorderInput, (ClientData) recPtr);
}
recPtr->record = newRecord;
recPtr->withDelay = withDelay;
string = "# CK-RECORDER\n# ";
Tcl_Write(recPtr->record, string, strlen(string));
Tcl_Eval(interp, "clock format [clock seconds]");
Tcl_Write(recPtr->record, Tcl_GetString(Tcl_GetObjResult(interp)), -1);
Tcl_ResetResult(interp);
Tcl_Write(recPtr->record, "\n# ", 3);
string = Tcl_GetVar(interp, "argv0", TCL_GLOBAL_ONLY);
Tcl_Write(recPtr->record, string, strlen(string));
Tcl_Write(recPtr->record, " ", 1);
string = Tcl_GetVar(interp, "argv", TCL_GLOBAL_ONLY);
Tcl_Write(recPtr->record, string, strlen(string));
Tcl_Write(recPtr->record, "\n", 1);
Tcl_DStringFree(&buffer);
}
break;
case CMD_STOP:
{
if (objc > 3) {
Tcl_WrongNumArgs(interp, 2, objv, "?replay?");
return TCL_ERROR;
}
if (objc == 3) {
if (strcmp(Tcl_GetString(objv[2]), "replay") != 0) {
Tcl_AppendResult(interp, "expecting \"replay\" but got \"",
Tcl_GetString(objv[2]), "\"", (char *) NULL);
return TCL_ERROR;
}
if (recPtr->replay != NULL) {
if (recPtr->timerRunning)
Tk_DeleteTimerHandler(recPtr->timer);
Tcl_Close(NULL, recPtr->replay);
recPtr->replay = NULL;
recPtr->timerRunning = 0;
}
}
else if (recPtr->record != NULL) {
Tcl_Close(NULL, recPtr->record);
Ck_DeleteGenericHandler(RecorderInput, (ClientData) recPtr);
recPtr->record = NULL;
}
}
break;
default:
{
/* -- should never be reached -- */
Tcl_AppendResult(interp, "wrong # args: should be \"",
Tcl_GetString(objv[0]), " replay, start, or stop\"", (char *) NULL);
return TCL_ERROR;
}
}
return TCL_OK;
}