-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcr3447.c
1517 lines (1334 loc) · 42.5 KB
/
cr3447.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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*--------------------------------------------------------------------------
**
** Copyright (c) 2003-2011, Paul Koning, Tom Hunter
** (c) 2017-2022 Steven Zoppi 23-Jun-2022
** Rewrite Job Submission and support for
** dedicated input/output directories.
**
** Name: cr3447.c
**
** Description:
** Perform emulation of CDC 3447 card reader controller.
**
** 20171110: SZoppi - Added Filesystem Watcher Support
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License version 3 as
** published by the Free Software Foundation.
**
** 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 version 3 for more details.
**
** You should have received a copy of the GNU General Public License
** version 3 along with this program in file "license-gpl-3.0.txt".
** If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
**
**--------------------------------------------------------------------------
*/
#define DEBUG 0
/*
** -------------
** Include Files
** -------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#if defined(_WIN32)
// Filesystem Watcher Machinery
#include <windows.h>
#include "dirent_win.h"
#else
#include <dirent.h>
#include <unistd.h>
#include <ctype.h>
#endif
#include "const.h"
#include "types.h"
#include "proto.h"
#include "dcc6681.h"
/*
** -----------------
** Private Constants
** -----------------
*/
/*
** CDC 3447 card reader function and status codes.
*/
#define FcCr3447Deselect 00000
#define FcCr3447Binary 00001
#define FcCr3447BCD 00002
#define FcCr3447SetGateCard 00004
#define FcCr3447Clear 00005
#define FcCr3447IntReady 00020
#define FcCr3447NoIntReady 00021
#define FcCr3447IntEoi 00022
#define FcCr3447NoIntEoi 00023
#define FcCr3447IntError 00024
#define FcCr3447NoIntError 00025
/*
** Status reply flags
**
** 0001 = Ready
** 0002 = Busy
** 0004 = Binary card (7/9 punch)
** 0010 = File card (7/8 punch)
** 0020 = Jam
** 0040 = Input tray empty
** 0100 = End of file
** 0200 = Ready interrupt
** 0400 = EOI interrupt
** 1000 = Error interrupt
** 2000 = Read compare error
** 4000 = Reserved by other controller (3649 only)
**
*/
#define StCr3447Ready 00201 // includes ReadyInt
#define StCr3447Busy 00002
#define StCr3447Binary 00004
#define StCr3447File 00010
#define StCr3447Empty 00040
#define StCr3447Eof 01540 // includes Empty, EoiInt, ErrorInt
#define StCr3447ReadyInt 00200
#define StCr3447EoiInt 00400
#define StCr3447ErrorInt 01000
#define StCr3447CompareErr 02000
#define StCr3447NonIntStatus 02177
#define Cr3447MaxDecks 128
/*
** -----------------------
** Private Macro Functions
** -----------------------
*/
/*
** -----------------------------------------
** Private Typedef and Structure Definitions
** -----------------------------------------
*/
typedef struct crContext
{
/*
** Info for show_unitrecord operator command.
*/
struct crContext *nextUnit;
u8 channelNo;
u8 eqNo;
u8 unitNo;
bool binary;
bool rawCard;
int intMask;
int status;
int col;
const u16 *table;
u32 getCardCycle;
PpWord card[80];
int inDeck;
int outDeck;
char *decks[Cr3447MaxDecks];
char *curFileName;
char *dirInput;
char *dirOutput;
int seqNum;
bool isWatched;
} CrContext;
/*
** ---------------------------
** Private Function Prototypes
** ---------------------------
*/
static FcStatus cr3447Func(PpWord funcCode);
static void cr3447Io(void);
static void cr3447Activate(void);
static void cr3447Disconnect(void);
static void cr3447NextCard(DevSlot *up, CrContext *cc);
static char *cr3447Func2String(PpWord funcCode);
static bool cr3447StartNextDeck(DevSlot *up, CrContext *cc);
static void cr3447SwapInOut(CrContext *cc, char *fname);
/*
** ----------------
** Public Variables
** ----------------
*/
/*
** -----------------
** Private Variables
** -----------------
*/
static CrContext *firstCr3447 = NULL;
static CrContext *lastCr3447 = NULL;
#if DEBUG
static FILE *cr3447Log = NULL;
#endif
/*--------------------------------------------------------------------------
**
** Public Functions
**
**------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------
** Purpose: Initialise card reader.
**
** Parameters: Name Description.
** eqNo equipment number
** unitNo unit number
** channelNo channel number the device is attached to
** deviceName optional card source file name, "026" (default)
** or "029" to select translation mode
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void cr3447Init(u8 eqNo, u8 unitNo, u8 channelNo, char *deviceName)
{
CrContext *cc;
char *crInput;
char *crOutput;
int len;
struct stat s;
fswContext *threadParms;
char *tokenAuto;
DevSlot *up;
bool watchRequested;
char *xlateTable;
/*
** For FileSystem Watcher
**
** We pass the fswContext structure to be used by the thread.
**
** deviceName is the "space delimited" remainder of the .ini
** file line.
**
** It can have up to three additional parameters of which NONE
** may contain a space. Specifying Parameters 2 or 3 REQUIRES
** specification of the Previous parameter. You may not specify
** Parameter 2 without Parameter 1.
**
** Parameter Description
** --------- -------------------------------------------------------
**
** 1 <Optional:> "*"=NULL Placeholder|"026"|"029" Default="026"
** "026" or "029" <Translate Table Specification>
**
** 2 <optional> "*"=NULL Placeholder|CRInputFolder
**
** The directory of the card reader's virtual "hopper"
** although the user can still load cards directly through
** the operator interface, a directory can be specified
** into which card decks can be submitted for sequential
** processing based on create date.
**
** NO Thread will be created if this parameter doesn't exist.
**
** 3 <optional> "*"=NULL Placeholder|CROutputFolder
**
** If specified, indicates the directory into which the
** processed cards will be deposited. Naming conflicts
** will be serialized with a suffix.
**
** If not specified, all input files will simply be deleted
** upon closure.
**
** 4 <optional> "*"=NULL Placeholder|"AUTO"|"NOAUTO" Default = "Auto"
** If a Virtual Card Hopper is defined, then this
** parameter indicates whether or not to initiate a Filewatcher
** thread to automatically submit jobs in file creation order
** from the CRInputFolder
**
** The context can be declared locally because it's just
** a structure used to marshal the parameters into the
** thread's context.
*/
#if DEBUG
if (cr3447Log == NULL)
{
cr3447Log = fopen("cr3447log.txt", "wt");
}
#endif
up = dcc6681Attach(channelNo, eqNo, 0, DtCr3447);
up->activate = cr3447Activate;
up->disconnect = cr3447Disconnect;
up->func = cr3447Func;
up->io = cr3447Io;
/*
** Only one card reader unit is possible per equipment.
*/
if (up->context[0] != NULL)
{
fputs("(cr3447 ) Only one CR3447 unit is possible per equipment\n", stderr);
exit(1);
}
cc = calloc(1, sizeof(CrContext));
if (cc == NULL)
{
fputs("(cr3447 ) Failed to allocate CR3447 context block\n", stderr);
exit(1);
}
up->context[0] = (void *)cc;
threadParms = calloc(1, sizeof(fswContext));
if (threadParms == NULL)
{
fputs("(cr3447 ) Failed to allocate CR3447 FileWatcher Context block\n", stderr);
exit(1);
}
if (deviceName == NULL)
{
deviceName = "";
}
xlateTable = strtok(deviceName, ", ");
crInput = strtok(NULL, ", ");
crOutput = strtok(NULL, ", ");
tokenAuto = strtok(NULL, ", ");
/*
** Process the request for file system watcher
*/
watchRequested = TRUE; // Default = Run filewatcher thread
if (tokenAuto != NULL)
{
if (strcasecmp(tokenAuto, "noauto") == 0)
{
watchRequested = FALSE;
}
else if ((strcasecmp(tokenAuto, "auto") != 0) && (strcasecmp(tokenAuto, "*") != 0))
{
fprintf(stderr, "(cr3447 ) Unrecognized Automation Type '%s'\n", tokenAuto);
exit(1);
}
}
fprintf(stdout, "(cr3447 ) File watcher %s requested\n", watchRequested ? "was" : "was not");
cc->channelNo = channelNo;
cc->eqNo = eqNo;
cc->unitNo = unitNo;
cc->table = asciiTo026; // default translation table
cc->isWatched = FALSE;
cc->dirInput = NULL;
cc->dirOutput = NULL;
if (xlateTable != NULL)
{
if (strcasecmp(xlateTable, "029") == 0)
{
cc->table = asciiTo029;
}
else if ((strcasecmp(xlateTable, "026") != 0)
&& (strcmp(xlateTable, "*") != 0)
&& (strcmp(xlateTable, "") != 0))
{
fprintf(stderr, "(cr3447 ) Unrecognized card code name %s\n", xlateTable);
exit(1);
}
}
fprintf(stdout, "(cr3447 ) Card code selected '%s'\n", (cc->table == asciiTo029) ? "029" : "026");
/*
** Incorrect specifications for input / output directories
** are fatal. Even though files can still be submitted
** through the operator interface, we want the parameters
** supplied through the ini file to be correct from the start.
*/
if ((crOutput != NULL)
&& (strcmp(crOutput, "*") != 0)
&& (strcmp(crOutput, "") != 0))
{
if (stat(crOutput, &s) != 0)
{
fprintf(stderr, "(cr3447 ) The output location specified '%s' does not exist.\n", crOutput);
exit(1);
}
if ((s.st_mode & S_IFDIR) == 0)
{
fprintf(stderr, "(cr3447 ) The output location specified '%s' is not a directory.\n", crOutput);
exit(1);
}
len = strlen(crOutput);
threadParms->outDoneDir = (char *)calloc(len + 1, 1);
cc->dirOutput = (char *)calloc(len + 1, 1);
if (threadParms->outDoneDir == NULL || cc->dirOutput == NULL)
{
fputs("(cr3447 ) Failed to allocate storage for output directory path\n", stderr);
exit(1);
}
memcpy(threadParms->outDoneDir, crOutput, len);
memcpy(cc->dirOutput, crOutput, len);
fprintf(stdout, "(cr3447 ) Submissions will be preserved in '%s'.\n", crOutput);
}
else
{
fputs("(cr3447 ) Submissions will be purged after processing.\n", stdout);
}
if ((crInput != NULL)
&& (strcmp(crInput, "*") != 0)
&& (strcmp(crInput, "") != 0))
{
if (stat(crInput, &s) != 0)
{
fprintf(stderr, "(cr3447 ) The Input location specified '%s' does not exist.\n", crInput);
exit(1);
}
if ((s.st_mode & S_IFDIR) == 0)
{
fprintf(stderr, "(cr3447 ) The Input location specified '%s' is not a directory.\n", crInput);
exit(1);
}
// We only care about the "Auto" "NoAuto" flag if there is a good input location
/*
** The thread needs to know what directory to watch.
**
** The Card Reader Context needs to remember what directory
** will supply the input files so more can be found at EOD.
*/
len = strlen(crInput);
threadParms->inWatchDir = (char *)calloc(len + 1, 1);
cc->dirInput = (char *)calloc(len + 1, 1);
if (threadParms->inWatchDir == NULL || cc->dirInput == NULL)
{
fputs("(cr3447 ) Failed to allocate storage for input directory path\n", stderr);
exit(1);
}
memcpy(threadParms->inWatchDir, crInput, len);
memcpy(cc->dirInput, crInput, len);
threadParms->eqNo = eqNo;
threadParms->unitNo = unitNo;
threadParms->channelNo = channelNo;
// threadParms->LoadCards = cr3447LoadCards;
threadParms->devType = DtCr3447;
/*
** At this point, we should have a completed context
** and should be ready to launch the thread and pass
** the context along. We don't free the block, the
** thread will do that if it is launched correctly.
*/
/*
** Now establish the filesystem watcher thread.
**
** It is non-fatal if the filesystem watcher thread
** cannot be started.
*/
cc->isWatched = FALSE;
if (watchRequested)
{
cc->isWatched = fsCreateThread(threadParms);
if (!cc->isWatched)
{
printf("(cr3447 ) Unable to create filesystem watch thread for '%s'.\n", crInput);
printf(" Card Loading is still possible via Operator Console.\n");
}
else
{
printf("(cr3447 ) Filesystem watch thread for '%s' created successfully.\n", crInput);
}
}
else
{
printf("(cr3447 ) Filesystem watch thread not requested for '%s'.\n", crInput);
printf(" Card Loading is required via Operator Console.\n");
}
}
/*
** Print a friendly message.
*/
printf("(cr3447 ) Initialised on channel %o equipment %o type '%s'\n",
channelNo,
eqNo,
cc->table == asciiTo026 ? "026" : "029");
if (!cc->isWatched)
{
free(threadParms);
}
/*
** Link into list of 3447 Card Reader units.
*/
if (lastCr3447 == NULL)
{
firstCr3447 = cc;
}
else
{
lastCr3447->nextUnit = cc;
}
lastCr3447 = cc;
}
/*--------------------------------------------------------------------------
** Purpose: Load cards on 3447 card reader.
**
** Parameters: Name Description.
** fname Pathname of file containing card deck
** channelNo Channel number of card reader
** equipmentNo Equipment number of card reader
** params Parameter list supplied on command line
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void cr3447LoadCards(char *fname, int channelNo, int equipmentNo, char *params)
{
CrContext *cc;
DevSlot *dp;
int len;
char outBuf[MaxFSPath+128];
struct stat s;
char *sp;
/*
** Locate the device control block.
*/
dp = dcc6681FindDevice((u8)channelNo, (u8)equipmentNo, DtCr3447);
if (dp == NULL)
{
return;
}
cc = (CrContext *)(dp->context[0]);
/*
** Ensure the tray is not full.
*/
if (((cc->inDeck + 1) % Cr3447MaxDecks) == cc->outDeck)
{
opDisplay("(cr3447 ) Input tray full\n");
return;
}
// At this point we should have a valid(ish) input file
// Make sure before enqueueing it.
if (stat(fname, &s) != 0)
{
sprintf(outBuf, "(cr3447 ) Requested file '%s' not found. (%s).\n", fname, strerror(errno));
opDisplay(outBuf);
return;
}
// Enqueue the file in the chain of pending files
len = strlen(fname);
sp = (char *)calloc(len + 1, 1);
memcpy(sp, fname, len);
cc->decks[cc->inDeck] = sp;
cc->inDeck = (cc->inDeck + 1) % Cr3447MaxDecks;
if (dp->fcb[0] == NULL)
{
if (cr3447StartNextDeck(dp, cc))
{
return;
}
}
// Starting the next deck was not possible
dp->fcb[0] = NULL;
cc->status = StCr3447Eof;
}
/*--------------------------------------------------------------------------
** Purpose: Get the next available deck named in the
** 3447 card reader's input directory.
**
** Parameters: Name Description.
** fname (in/out) string buffer for next file name
** we don't change anything input
** unless there's an existing file.
** channelNo Channel number of card reader
** equipmentNo Equipment number of card reader
** params Parameter list supplied on command line
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void cr3447GetNextDeck(char *fname, int channelNo, int equipmentNo, char *params)
{
CrContext *cc;
DIR *curDir;
struct dirent *curDirEntry;
DevSlot *dp;
char fOldest[MaxFSPath*2+2] = "";
char outBuf[MaxFSPath*2+64];
struct stat s;
char strWork[MaxFSPath*2+2] = "";
time_t tOldest = 0;
/*
** Safety check, we only respond if the first
** character of the filename is an asterisk '*'
*/
if (*fname != '*')
{
sprintf(outBuf, "(cr3447 ) cr3447GetNextDeck called with improper parameter '%s'.\n", fname);
opDisplay(outBuf);
return;
}
/*
** Locate the device control block.
*/
dp = dcc6681FindDevice((u8)channelNo, (u8)equipmentNo, DtCr3447);
if (dp == NULL)
{
return;
}
cc = (CrContext *)(dp->context[0]);
/*
** Ensure the tray is not full.
*/
if (((cc->inDeck + 1) % Cr3447MaxDecks) == cc->outDeck)
{
opDisplay("(cr3447 ) Input tray full\n");
return;
}
/*
** The special filename, asterisk(*)
** means "Load the next deck" from the dirInput
** directory (if it's defined).
**
** This routine is called from operator.c during
** preparation of the input card deck when it detects
** the filename specified as "*".
**
** In that case, we don't link the deck into the chain.
** This flow in date order.
**
** The asterisk convention works even if the
** filewatcher thread cannot be started. It
** simply means: "Pick the next oldest file
** found in the input directory.
*/
if (cc->dirInput == NULL)
{
opDisplay("(cr3447 ) No card reader directory has been specified on the device declaration.\n");
opDisplay("(cr3447 ) The 'Load Next Deck' request is ignored.\n");
return;
}
curDir = opendir(cc->dirInput);
if (curDir == NULL)
{
sprintf(outBuf, "(cr3447 ) Failed to open card reader dirctory '%s'.\n", cc->dirInput);
opDisplay(outBuf);
return;
}
/*
** Scan the input directory (if specified)
** for the oldest file queued.
** If one is found, we replace the
** asterisk with the name of the found file.
** and continue processing.
*/
do
{
curDirEntry = readdir(curDir);
if (curDirEntry == NULL) break;
// Pop over the dot (.) directories
if (*curDirEntry->d_name == '.') continue;
sprintf(strWork, "%s/%s", cc->dirInput, curDirEntry->d_name);
stat(strWork, &s);
if (fOldest[0] == '\0')
{
strcpy(fOldest, strWork);
tOldest = s.st_ctime;
}
else
{
if (s.st_ctime > tOldest)
{
strcpy(fOldest, strWork);
tOldest = s.st_ctime;
}
}
} while (curDirEntry != NULL);
if (fOldest[0] != '\0')
{
sprintf(outBuf, "(cr3447 ) Dequeueing unprocessed file '%s' from '%s'.\n", fOldest, cc->dirInput);
opDisplay(outBuf);
/*
** To complement the functionality of the operator.c process,
** there is an expectation that the file provided must be
** preprocessed.
**
** When the input file is dequeued, and if there exists a
** crOutDir, then we MOVE the file to the output directory
** before dequeueing it.
**
** If there is no output directory, we don't bother changing
** the name.
*/
strcpy(fname, fOldest);
cr3447SwapInOut(cc, fname);
}
else
{
sprintf(outBuf, "(cr3447 ) No files found in '%s'.\n", cc->dirInput);
opDisplay(outBuf);
}
}
/*--------------------------------------------------------------------------
** Purpose: Cleanup files located in the dirInput
** virtual card reader hopper.
**
** Parameters: Name Description.
** fname (in) string buffer for file name
** to be tested for removal.
** channelNo Channel number of card reader
** equipmentNo Equipment number of card reader
** params Parameter list supplied on command line
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void cr3447PostProcess(char *fname, int channelNo, int equipmentNo, char *params)
{
CrContext *cc;
DevSlot *dp;
char outBuf[MaxFSPath+64];
/*
** Locate the device control block.
*/
dp = dcc6681FindDevice((u8)channelNo, (u8)equipmentNo, DtCr3447);
if (dp == NULL)
{
return;
}
cc = (CrContext *)(dp->context[0]);
if (cc->dirInput == NULL)
{
sprintf(outBuf, "(cr3447 ) Submitted deck '%s' processing complete.\n", fname);
opDisplay(outBuf);
return;
}
//
// If the file is from the configured input directory, it can be deleted.
//
if (strncmp(fname, cc->dirInput, strlen(cc->dirInput)) == 0)
{
sprintf(outBuf, "(cr3447 ) Purging submitted deck '%s'.\n", fname);
opDisplay(outBuf);
unlink(fname);
}
}
/*--------------------------------------------------------------------------
** Purpose: Moves Input Directory File to Output Directory.
**
** Parameters: Name Description.
** cc Pointer to card reader context
** fName (in/out) Input file pathname. This string is
** replaced by a generated pathname, so it should
** be a character array at least MaxFSPath long.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void cr3447SwapInOut(CrContext *cc, char *fName)
{
char fnwork[MaxFSPath*2+32] = "";
char outBuf[MaxFSPath+2+64];
// If either directory isn't specified, just ignore the rename.
if (cc->dirOutput == NULL || cc->dirInput == NULL)
{
return;
}
/*
** We have both Input and Output directories
** then we move it to the "output" directory
** and we return the changed name in fname
**
** Don't touch any files that aren't from the input directory
*/
if (strncmp(fName, cc->dirInput, strlen(cc->dirInput)) != 0)
{
return;
}
/*
** Perform the rename of the current file to the "Processed"
** directory. This rename will ALSO trigger the filechange
** watcher. Otherwise the operator will need to use the load
** command from the console.
*/
int fnindex = 0;
while (TRUE)
{
// Create the new filename
sprintf(fnwork, "%s/%s_%04i",
cc->dirOutput,
fName + strlen(cc->dirInput) + 1,
fnindex);
if (rename(fName, fnwork) == 0)
{
sprintf(outBuf, "(cr3447 ) Deck '%s' moved to '%s'. (Input preserved)\n",
fName + strlen(cc->dirInput) + 1,
fnwork);
opDisplay(outBuf);
strcpy(fName, fnwork);
break;
}
else
{
sprintf(outBuf, "(cr3447 ) Rename failure on '%s' - (%s). Retrying (%d)...\n",
fName + strlen(cc->dirInput) + 1,
strerror(errno),
fnindex);
opDisplay(outBuf);
}
fnindex += 1;
if (fnindex > 999)
{
sprintf(outBuf, "(cr3447 ) Rename failure on '%s' to '%s' (retries > 999)\n", fName, fnwork);
opDisplay(outBuf);
break;
}
}
}
/*--------------------------------------------------------------------------
** Purpose: Show card reader status (operator interface).
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void cr3447ShowStatus()
{
CrContext *cp;
char outBuf[MaxFSPath*2+64];
for (cp = firstCr3447; cp != NULL; cp = cp->nextUnit)
{
sprintf(outBuf, " > %-8s C%02o E%02o U%02o", "3447", cp->channelNo, cp->eqNo, cp->unitNo);
opDisplay(outBuf);
sprintf(outBuf, " %-20s (", cp->curFileName != NULL ? cp->curFileName : "");
opDisplay(outBuf);
opDisplay(cp->binary ? "bin" : "char");
if (cp->rawCard) opDisplay(", raw");
sprintf(outBuf, ", seq %d", cp->seqNum);
if (cp->isWatched)
{
sprintf(outBuf, ", in %s/", cp->dirInput);
opDisplay(outBuf);
if (cp->dirOutput != NULL)
{
sprintf(outBuf, ", out %s/", cp->dirOutput);
opDisplay(outBuf);
}
}
opDisplay(")\n");
}
}
/*
**--------------------------------------------------------------------------
**
** Private Functions
**
**--------------------------------------------------------------------------
*/
/*--------------------------------------------------------------------------
** Purpose: Execute function code on 3447 card reader.
**
** Parameters: Name Description.
** funcCode function code
**
** Returns: FcStatus
**
**------------------------------------------------------------------------*/
static FcStatus cr3447Func(PpWord funcCode)
{
CrContext *cc;
FcStatus st;
#if DEBUG
fprintf(cr3447Log, "\n(cr3447 ) %06d PP:%02o CH:%02o f:%04o T:%-25s > ",
traceSequenceNo,
activePpu->id,
activeDevice->channel->id,
funcCode,
cr3447Func2String(funcCode));
#endif
cc = (CrContext *)active3000Device->context[0];
switch (funcCode)
{
default: // all unrecognized codes are NOPs
#if DEBUG
fprintf(cr3447Log, "(cr3447 ) FUNC not implemented & silently ignored!");
#endif
st = FcProcessed;
break;
case FcCr3447SetGateCard:
st = FcProcessed;
break;
case Fc6681InputToEor:
case Fc6681Input:
st = FcAccepted;
cc->getCardCycle = cycles;
cc->status = StCr3447Ready;
active3000Device->fcode = funcCode;
break;
case Fc6681DevStatusReq:
st = FcAccepted;
active3000Device->fcode = funcCode;
break;
case FcCr3447Binary:
cc->binary = TRUE;
st = FcProcessed;
break;
case FcCr3447Deselect:
case FcCr3447Clear:
cc->intMask = 0;
cc->binary = FALSE;
st = FcProcessed;
break;
case FcCr3447BCD:
cc->binary = FALSE;
st = FcProcessed;
break;
case FcCr3447IntReady:
cc->intMask |= StCr3447ReadyInt;
cc->status &= ~StCr3447ReadyInt;
st = FcProcessed;
break;
case FcCr3447NoIntReady:
cc->intMask &= ~StCr3447ReadyInt;
cc->status &= ~StCr3447ReadyInt;
st = FcProcessed;
break;
case FcCr3447IntEoi:
cc->intMask |= StCr3447EoiInt;
cc->status &= ~StCr3447EoiInt;
st = FcProcessed;
break;
case FcCr3447NoIntEoi:
cc->intMask &= ~StCr3447EoiInt;
cc->status &= ~StCr3447EoiInt;
st = FcProcessed;
break;
case FcCr3447IntError:
cc->intMask |= StCr3447ErrorInt;
cc->status &= ~StCr3447ErrorInt;
st = FcProcessed;
break;
case FcCr3447NoIntError:
cc->intMask &= ~StCr3447ErrorInt;
cc->status &= ~StCr3447ErrorInt;
st = FcProcessed;
break;
}