aboutsummaryrefslogtreecommitdiff
path: root/usr.sbin/xntpd/parse/util/dcfd.c
blob: 23934a8e104ec99ae7fd12d52205279bb6c1f363 (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
/*
 * /src/NTP/REPOSITORY/v3/parse/util/dcfd.c,v 3.15 1994/01/25 19:05:42 kardel Exp
 *  
 * dcfd.c,v 3.15 1994/01/25 19:05:42 kardel Exp
 *
 * DCF77 100/200ms pulse synchronisation daemon program (via 50Baud serial line)
 *
 * Features:
 *  DCF77 decoding
 *  NTP loopfilter logic for local clock
 *  interactive display for debugging
 *
 * Lacks:
 *  Leap second handling (at that level you should switch to xntp3 - really!)
 *
 * Copyright (c) 1993,1994
 * Frank Kardel, Friedrich-Alexander Universitaet Erlangen-Nuernberg
 *                                    
 * 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.
 *
 * This program may not be sold or used for profit without prior
 * written consent of the author.
 */

#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/signal.h>
#include <sys/errno.h>
#include <syslog.h>

/*
 * NTP compilation environment
 */
#ifdef USE_PROTOTYPES
#include "ntp_stdlib.h"
extern int sigvec P((int, struct sigvec *, struct sigvec *));
extern int fscanf P((FILE *, char *, ...));
#endif

#ifdef SYS_LINUX
#include "ntp_timex.h"
#endif

/*
 * select which terminal handling to use (currently only SysV variants)
 */
#if defined(HAVE_TERMIOS) || defined(STREAM)
#include <termios.h>
#define TTY_GETATTR(_FD_, _ARG_) tcgetattr((_FD_), (_ARG_))
#define TTY_SETATTR(_FD_, _ARG_) tcsetattr((_FD_), TCSANOW, (_ARG_))
#endif

#if defined(HAVE_TERMIO) || defined(HAVE_SYSV_TTYS)
#include <termio.h>
#define TTY_GETATTR(_FD_, _ARG_) ioctl((_FD_), TCGETA, (_ARG_))
#define TTY_SETATTR(_FD_, _ARG_) ioctl((_FD_), TCSETAW, (_ARG_))
#endif

#ifndef TTY_GETATTR
MUST DEFINE ONE OF "HAVE_TERMIOS" or "HAVE_TERMIO"
#endif

#ifndef dysize
#define dysize(_x_) (((_x_) % 4) ? 365 : (((_x_) % 400) ? 365 : 366))
#endif

#define timernormalize(_a_) \
		if ((_a_)->tv_usec >= 1000000) \
			{ \
				(_a_)->tv_sec  += (_a_)->tv_usec / 1000000; \
				(_a_)->tv_usec  = (_a_)->tv_usec % 1000000; \
			} \
		if ((_a_)->tv_usec < 0) \
			{ \
				(_a_)->tv_sec  -= 1 + -(_a_)->tv_usec / 1000000; \
				(_a_)->tv_usec = 1000000 - (-(_a_)->tv_usec % 1000000); \
			}

#define timeradd(_a_, _b_) \
		(_a_)->tv_sec  += (_b_)->tv_sec; \
		(_a_)->tv_usec += (_b_)->tv_usec; \
		timernormalize((_a_))

#define timersub(_a_, _b_) \
		(_a_)->tv_sec  -= (_b_)->tv_sec; \
		(_a_)->tv_usec -= (_b_)->tv_usec; \
		timernormalize((_a_))

/*
 * debug macros
 */
#define PRINTF if (interactive) printf
#define LPRINTF if (interactive && loop_filter_debug) printf

#ifdef DEBUG
#define dprintf(_x_) PRINTF _x_
#else
#define dprintf(_x_)
#endif

extern int errno;

/*
 * display received data (avoids also detaching from tty)
 */
static int interactive = 0;

/*
 * display loopfilter (clock control) variables
 */
static int loop_filter_debug = 0;

/*
 * do not set/adjust system time
 */
static int no_set = 0;

/*
 * time that passes between start of DCF impulse and time stamping (fine
 * adjustment) in microseconds (receiver/OS dependent)
 */
#define DEFAULT_DELAY	230000	/* rough estimate */

/*
 * The two states we can be in - eithe we receive nothing
 * usable or we have the correct time
 */
#define NO_SYNC		0x01
#define SYNC		0x02

static int    sync_state = NO_SYNC;
static time_t last_sync;

static unsigned long ticks = 0;

static char pat[] = "-\\|/";

#define LINES		(24-2)	/* error lines after which the two headlines are repeated */

#define MAX_UNSYNC	(10*60)	/* allow synchronisation loss for 10 minutes */
#define NOTICE_INTERVAL (20*60)	/* mention missing synchronisation every 20 minutes */

/*
 * clock adjustment PLL - see NTP protocol spec (RFC1305) for details
 */

#define USECSCALE	10
#define TIMECONSTANT	2
#define ADJINTERVAL	0
#define FREQ_WEIGHT	18
#define PHASE_WEIGHT	7
#define MAX_DRIFT	0x3FFFFFFF

#define R_SHIFT(_X_, _Y_) (((_X_) < 0) ? -(-(_X_) >> (_Y_)) : ((_X_) >> (_Y_)))

static struct timeval max_adj_offset = { 0, 128000 };

static long clock_adjust = 0;	/* current adjustment value (usec * 2^USECSCALE) */
static long drift_comp   = 0;	/* accumulated drift value  (usec / ADJINTERVAL) */
static long adjustments  = 0;
static char skip_adjust  = 1;	/* discard first adjustment (bad samples) */

/*
 * DCF77 state flags
 */
#define DCFB_ANNOUNCE           0x0001 /* switch time zone warning (DST switch) */
#define DCFB_DST                0x0002 /* DST in effect */
#define DCFB_LEAP		0x0004 /* LEAP warning (1 hour prior to occurence) */
#define DCFB_ALTERNATE		0x0008 /* alternate antenna used */

struct clocktime		/* clock time broken up from time code */
{
  long wday;		/* Day of week: 1: Monday - 7: Sunday */
  long day;
  long month;
  long year;
  long hour;
  long minute;
  long second;
  long usecond;
  long utcoffset;	/* in minutes */
  long flags;		/* current clock status  (DCF77 state flags) */
};

typedef struct clocktime clocktime_t;

/*
 * (usually) quick constant multiplications
 */
#define TIMES10(_X_) (((_X_) << 3) + ((_X_) << 1))	/* *8 + *2 */
#define TIMES24(_X_) (((_X_) << 4) + ((_X_) << 3))      /* *16 + *8 */
#define TIMES60(_X_) ((((_X_) << 4)  - (_X_)) << 2)     /* *(16 - 1) *4 */
/*
 * generic abs() function
 */
#define abs(_x_)     (((_x_) < 0) ? -(_x_) : (_x_))

/*
 * conversion related return/error codes
 */
#define CVT_MASK	0x0000000F /* conversion exit code */
#define   CVT_NONE	0x00000001 /* format not applicable */
#define   CVT_FAIL	0x00000002 /* conversion failed - error code returned */
#define   CVT_OK	0x00000004 /* conversion succeeded */
#define CVT_BADFMT	0x00000010 /* general format error - (unparsable) */
#define CVT_BADDATE	0x00000020 /* invalid date */
#define CVT_BADTIME	0x00000040 /* invalid time */

/*
 * DCF77 raw time code
 *
 * From "Zur Zeit", Physikalisch-Technische Bundesanstalt (PTB), Braunschweig
 * und Berlin, Maerz 1989
 *
 * Timecode transmission:
 * AM:
 *	time marks are send every second except for the second before the
 *	next minute mark
 *	time marks consist of a reduction of transmitter power to 25%
 *	of the nominal level
 *	the falling edge is the time indication (on time)
 *	time marks of a 100ms duration constitute a logical 0
 *	time marks of a 200ms duration constitute a logical 1
 * FM:
 *	see the spec. (basically a (non-)inverted psuedo random phase shift)
 *
 * Encoding:
 * Second	Contents
 * 0  - 10	AM: free, FM: 0
 * 11 - 14	free
 * 15		R     - alternate antenna
 * 16		A1    - expect zone change (1 hour before)
 * 17 - 18	Z1,Z2 - time zone
 *		 0  0 illegal
 *		 0  1 MEZ  (MET)
 *		 1  0 MESZ (MED, MET DST)
 *		 1  1 illegal
 * 19		A2    - expect leap insertion/deletion (1 hour before)
 * 20		S     - start of time code (1)
 * 21 - 24	M1    - BCD (lsb first) Minutes
 * 25 - 27	M10   - BCD (lsb first) 10 Minutes
 * 28		P1    - Minute Parity (even)
 * 29 - 32	H1    - BCD (lsb first) Hours
 * 33 - 34      H10   - BCD (lsb first) 10 Hours
 * 35		P2    - Hour Parity (even)
 * 36 - 39	D1    - BCD (lsb first) Days
 * 40 - 41	D10   - BCD (lsb first) 10 Days
 * 42 - 44	DW    - BCD (lsb first) day of week (1: Monday -> 7: Sunday)
 * 45 - 49	MO    - BCD (lsb first) Month
 * 50           MO0   - 10 Months
 * 51 - 53	Y1    - BCD (lsb first) Years
 * 54 - 57	Y10   - BCD (lsb first) 10 Years
 * 58 		P3    - Date Parity (even)
 * 59		      - usually missing (minute indication), except for leap insertion
 */

/*-----------------------------------------------------------------------
 * conversion table to map DCF77 bit stream into data fields.
 * Encoding:
 *   Each field of the DCF77 code is described with two adjacent entries in
 *   this table. The first entry specifies the offset into the DCF77 data stream
 *   while the length is given as the difference between the start index and
 *   the start index of the following field.
 */
static struct rawdcfcode 
{
  char offset;			/* start bit */
} rawdcfcode[] =
{
  {  0 }, { 15 }, { 16 }, { 17 }, { 19 }, { 20 }, { 21 }, { 25 }, { 28 }, { 29 },
  { 33 }, { 35 }, { 36 }, { 40 }, { 42 }, { 45 }, { 49 }, { 50 }, { 54 }, { 58 }, { 59 }
};

/*-----------------------------------------------------------------------
 * symbolic names for the fields of DCF77 describes in "rawdcfcode".
 * see comment above for the structure of the DCF77 data
 */
#define DCF_M	0
#define DCF_R	1
#define DCF_A1	2
#define DCF_Z	3
#define DCF_A2	4
#define DCF_S	5
#define DCF_M1	6
#define DCF_M10	7
#define DCF_P1	8
#define DCF_H1	9
#define DCF_H10	10
#define DCF_P2	11
#define DCF_D1	12
#define DCF_D10	13
#define DCF_DW	14
#define DCF_MO	15
#define DCF_MO0	16
#define DCF_Y1	17
#define DCF_Y10	18
#define DCF_P3	19

/*-----------------------------------------------------------------------
 * parity field table (same encoding as rawdcfcode)
 * This table describes the sections of the DCF77 code that are
 * parity protected
 */
static struct partab
{
  char offset;			/* start bit of parity field */
} partab[] =
{
  { 21 }, { 29 }, { 36 }, { 59 }
};

/*-----------------------------------------------------------------------
 * offsets for parity field descriptions
 */
#define DCF_P_P1	0
#define DCF_P_P2	1
#define DCF_P_P3	2

/*-----------------------------------------------------------------------
 * legal values for time zone information
 */
#define DCF_Z_MET 0x2
#define DCF_Z_MED 0x1

/*-----------------------------------------------------------------------
 * symbolic representation if the DCF77 data stream
 */
static struct dcfparam
{
  unsigned char onebits[60];
  unsigned char zerobits[60];
} dcfparam = 
{
  "###############RADMLS1248124P124812P1248121241248112481248P", /* 'ONE' representation */
  "--------------------s-------p------p----------------------p"  /* 'ZERO' representation */
};

/*-----------------------------------------------------------------------
 * extract a bitfield from DCF77 datastream
 * All numeric field are LSB first.
 * buf holds a pointer to a DCF77 data buffer in symbolic
 *     representation
 * idx holds the index to the field description in rawdcfcode
 */
static unsigned long ext_bf(buf, idx)
  register unsigned char *buf;
  register int   idx;
{
  register unsigned long sum = 0;
  register int i, first;

  first = rawdcfcode[idx].offset;
  
  for (i = rawdcfcode[idx+1].offset - 1; i >= first; i--)
    {
      sum <<= 1;
      sum |= (buf[i] != dcfparam.zerobits[i]);
    }
  return sum;
}

/*-----------------------------------------------------------------------
 * check even parity integrity for a bitfield
 *
 * buf holds a pointer to a DCF77 data buffer in symbolic
 *     representation
 * idx holds the index to the field description in partab
 */
static unsigned pcheck(buf, idx)
  register unsigned char *buf;
  register int   idx;
{
  register int i,last;
  register unsigned psum = 1;

  last = partab[idx+1].offset;

  for (i = partab[idx].offset; i < last; i++)
    psum ^= (buf[i] != dcfparam.zerobits[i]);

  return psum;
}

/*-----------------------------------------------------------------------
 * convert a DCF77 data buffer into wall clock time + flags
 *
 * buffer holds a pointer to a DCF77 data buffer in symbolic
 *        representation
 * size   describes the length of DCF77 information in bits (represented
 *        as chars in symbolic notation
 * clock  points to a wall clock time description of the DCF77 data (result)
 */
static unsigned long convert_rawdcf(buffer, size, clock)
  register unsigned char   *buffer;
  register int              size;
  register clocktime_t     *clock;
{
  if (size < 57)
    {
      PRINTF("%-30s", "*** INCOMPLETE");
      return CVT_NONE;
    }
  
  /*
   * check Start and Parity bits
   */
  if ((ext_bf(buffer, DCF_S) == 1) &&
      pcheck(buffer, DCF_P_P1) &&
      pcheck(buffer, DCF_P_P2) &&
      pcheck(buffer, DCF_P_P3))
    {
      /*
       * buffer OK - extract all fields and build wall clock time from them
       */

      clock->flags  = 0;
      clock->usecond= 0;
      clock->second = 0;
      clock->minute = ext_bf(buffer, DCF_M10);
      clock->minute = TIMES10(clock->minute) + ext_bf(buffer, DCF_M1);
      clock->hour   = ext_bf(buffer, DCF_H10);
      clock->hour   = TIMES10(clock->hour)   + ext_bf(buffer, DCF_H1);
      clock->day    = ext_bf(buffer, DCF_D10);
      clock->day    = TIMES10(clock->day)    + ext_bf(buffer, DCF_D1);
      clock->month  = ext_bf(buffer, DCF_MO0);
      clock->month  = TIMES10(clock->month)  + ext_bf(buffer, DCF_MO);
      clock->year   = ext_bf(buffer, DCF_Y10);
      clock->year   = TIMES10(clock->year)   + ext_bf(buffer, DCF_Y1);
      clock->wday   = ext_bf(buffer, DCF_DW);

      /*
       * determine offset to UTC by examining the time zone
       */
      switch (ext_bf(buffer, DCF_Z))
	{
	case DCF_Z_MET:
	  clock->utcoffset = -60;
	  break;

	case DCF_Z_MED:
	  clock->flags     |= DCFB_DST;
	  clock->utcoffset  = -120;
	  break;

	default:
	  PRINTF("%-30s", "*** BAD TIME ZONE");
	  return CVT_FAIL|CVT_BADFMT;
	}

      /*
       * extract various warnings from DCF77
       */
      if (ext_bf(buffer, DCF_A1))
	clock->flags |= DCFB_ANNOUNCE;

      if (ext_bf(buffer, DCF_A2))
	clock->flags |= DCFB_LEAP;

      if (ext_bf(buffer, DCF_R))
	clock->flags |= DCFB_ALTERNATE;

      return CVT_OK;
    }
  else
    {
      /*
       * bad format - not for us
       */
      PRINTF("%-30s", "*** BAD FORMAT (invalid/parity)");
      return CVT_FAIL|CVT_BADFMT;
    }
}

/*-----------------------------------------------------------------------
 * raw dcf input routine - fix up 50 baud
 * characters for 1/0 decision
 */
static unsigned long cvt_rawdcf(buffer, size, clock)
  register unsigned char   *buffer;
  register int              size;
  register clocktime_t     *clock;
{
  register unsigned char *s = buffer;
  register unsigned char *e = buffer + size;
  register unsigned char *b = dcfparam.onebits;
  register unsigned char *c = dcfparam.zerobits;
  register unsigned rtc = CVT_NONE;
  register unsigned int i, lowmax, highmax, cutoff, span;
#define BITS 9
  unsigned char     histbuf[BITS];
  /*
   * the input buffer contains characters with runs of consecutive
   * bits set. These set bits are an indication of the DCF77 pulse
   * length. We assume that we receive the pulse at 50 Baud. Thus
   * a 100ms pulse would generate a 4 bit train (20ms per bit and
   * start bit)
   * a 200ms pulse would create all zeroes (and probably a frame error)
   *
   * The basic idea is that on corret reception we must have two
   * maxima in the pulse length distribution histogram. (one for
   * the zero representing pulses and one for the one representing
   * pulses)
   * There will always be ones in the datastream, thus we have to see
   * two maxima.
   * The best point to cut for a 1/0 decision is the minimum between those
   * between the maxima. The following code tries to find this cutoff point.
   */

  /*
   * clear histogram buffer
   */
  for (i = 0; i < BITS; i++)
    {
      histbuf[i] = 0;
    }

  cutoff = 0;
  lowmax = 0;

  /*
   * convert sequences of set bits into bits counts updating
   * the histogram alongway
   */
  while (s < e)
    {
      register unsigned int ch = *s ^ 0xFF;
      /*
       * check integrity and update histogramm
       */
      if (!((ch+1) & ch) || !*s)
	{
	  /*
	   * character ok
	   */
	  for (i = 0; ch; i++)
	    {
	      ch >>= 1;
	    }

	  *s = i;
	  histbuf[i]++;
	  cutoff += i;
	  lowmax++;
	}
      else
	{
	  /*
	   * invalid character (no consecutive bit sequence)
	   */
	  dprintf(("parse: cvt_rawdcf: character check for 0x%x@%d FAILED\n", *s, s - buffer));
	  *s = ~0;
	  rtc = CVT_FAIL|CVT_BADFMT;
	}
      s++;
    }

  /*
   * first cutoff estimate (average bit count - must be between both
   * maxima)
   */
  if (lowmax)
    {
      cutoff /= lowmax;
    }
  else
    {
      cutoff = 4;	/* doesn't really matter - it'll fail anyway, but gives error output */
    }

  dprintf(("parse: cvt_rawdcf: average bit count: %d\n", cutoff));

  lowmax = 0;  /* weighted sum */
  highmax = 0; /* bitcount */

  /*
   * collect weighted sum of lower bits (left of initial guess)
   */
  dprintf(("parse: cvt_rawdcf: histogram:"));
  for (i = 0; i <= cutoff; i++)
    {
      lowmax  += histbuf[i] * i;
      highmax += histbuf[i];
      dprintf((" %d", histbuf[i]));
    }
  dprintf((" <M>"));

  /*
   * round up
   */
  lowmax += highmax / 2;

  /*
   * calculate lower bit maximum (weighted sum / bit count)
   *
   * avoid divide by zero
   */
  if (highmax)
    {
      lowmax /= highmax;
    }
  else
    {
      lowmax = 0;
    }

  highmax = 0; /* weighted sum of upper bits counts */
  cutoff = 0;  /* bitcount */

  /*
   * collect weighted sum of lower bits (right of initial guess)
   */
  for (; i < BITS; i++)
    {
      highmax+=histbuf[i] * i;
      cutoff +=histbuf[i];
      dprintf((" %d", histbuf[i]));
    }
  dprintf(("\n"));

  /*
   * determine upper maximum (weighted sum / bit count)
   */
  if (cutoff)
    {
      highmax /= cutoff;
    }
  else
    {
      highmax = BITS-1;
    }

  /*
   * following now holds:
   * lowmax <= cutoff(initial guess) <= highmax
   * best cutoff is the minimum nearest to higher bits
   */

  /*
   * find the minimum between lowmax and highmax (detecting
   * possibly a minimum span)
   */
  span = cutoff = lowmax;
  for (i = lowmax; i <= highmax; i++)
    {
      if (histbuf[cutoff] > histbuf[i])
	{
	  /*
	   * got a new minimum move beginning of minimum (cutoff) and
	   * end of minimum (span) there
	   */
	  cutoff = span = i;
	}
      else
        if (histbuf[cutoff] == histbuf[i])
	  {
	    /*
	     * minimum not better yet - but it spans more than
	     * one bit value - follow it
	     */
	    span = i;
	  }
    }

  /*
   * cutoff point for 1/0 decision is the middle of the minimum section
   * in the histogram
   */
  cutoff = (cutoff + span) / 2;

  dprintf(("parse: cvt_rawdcf: lower maximum %d, higher maximum %d, cutoff %d\n", lowmax, highmax, cutoff));

  /*
   * convert the bit counts to symbolic 1/0 information for data conversion
   */
  s = buffer;
  while ((s < e) && *c && *b)
    {
      if (*s == (unsigned char)~0)
	{
	  /*
	   * invalid character
	   */
	  *s = '?';
	}
      else
	{
	  /*
	   * symbolic 1/0 representation
	   */
	  *s = (*s >= cutoff) ? *b : *c;
	}
      s++;
      b++;
      c++;
    }

  /*
   * if everything went well so far return the result of the symbolic
   * conversion routine else just the accumulated errors
   */
  return (rtc == CVT_NONE) ? convert_rawdcf(buffer, size, clock) : rtc;
}

/*-----------------------------------------------------------------------
 * convert a wall clock time description of DCF77 to a Unix time (seconds
 * since 1.1. 1970 UTC)
 */
time_t
dcf_to_unixtime(clock, cvtrtc)
  register clocktime_t   *clock;
  register unsigned long *cvtrtc;
{
#define SETRTC(_X_)	{ if (cvtrtc) *cvtrtc = (_X_); }
  static int days_of_month[] = 
    {
      0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
    };
  register int i;
  time_t t;
  
  /*
   * map 2 digit years to 19xx (DCF77 is a 20th century item)
   */
  if (clock->year < 100)
    clock->year += 1900;

  /*
   * assume that we convert timecode within the unix/UTC epoch -
   * prolonges validity of 2 digit years
   */
  if (clock->year < 1994)
    clock->year += 100;		/* XXX this will do it till <2094 */

  /*
   * must have been a really negative year code - drop it
   */
  if (clock->year < 0)
    {
      SETRTC(CVT_FAIL|CVT_BADDATE);
      return -1;
    }
  
  /*
   * sorry, slow section here - but it's not time critical anyway
   */

  /*
   * calculate days since 1970 (watching leap years)
   */
  t =  (clock->year - 1970) * 365;
  t += (clock->year >> 2) - (1970 >> 2);
  t -= clock->year / 400 - 1970 / 400;

  				/* month */
  if (clock->month <= 0 || clock->month > 12)
    {
      SETRTC(CVT_FAIL|CVT_BADDATE);
      return -1;		/* bad month */
    }
				/* adjust current leap year */
  if (clock->month >= 3 && dysize(clock->year) == 366)
      t++;

  /*
   * collect days from months excluding the current one
   */
  for (i = 1; i < clock->month; i++)
    {
      t += days_of_month[i];
    }
				/* day */
  if (clock->day < 1 || ((clock->month == 2 && dysize(clock->year) == 366) ?
		  clock->day > 29 : clock->day > days_of_month[clock->month]))
    {
      SETRTC(CVT_FAIL|CVT_BADDATE);
      return -1;		/* bad day */
    }

  /*
   * collect days from date excluding the current one
   */
  t += clock->day - 1;

				/* hour */
  if (clock->hour < 0 || clock->hour >= 24)
    {
      SETRTC(CVT_FAIL|CVT_BADTIME);
      return -1;		/* bad hour */
    }

  /*
   * calculate hours from 1. 1. 1970
   */
  t = TIMES24(t) + clock->hour;

  				/* min */
  if (clock->minute < 0 || clock->minute > 59)
    {
      SETRTC(CVT_FAIL|CVT_BADTIME);
      return -1;		/* bad min */
    }

  /*
   * calculate minutes from 1. 1. 1970
   */
  t = TIMES60(t) + clock->minute;
				/* sec */
  
  /*
   * calculate UTC in minutes
   */
  t += clock->utcoffset;

  if (clock->second < 0 || clock->second > 60)	/* allow for LEAPs */
    {
      SETRTC(CVT_FAIL|CVT_BADTIME);
      return -1;		/* bad sec */
    }

  /*
   * calculate UTC in seconds - phew !
   */
  t  = TIMES60(t) + clock->second;
				/* done */
  return t;
}

/*-----------------------------------------------------------------------
 * cheap half baked 1/0 decision - for interactive operation only
 */
static char type(c)
unsigned char c;
{
  c ^= 0xFF;
  return (c > 0xF);
}

/*-----------------------------------------------------------------------
 * week day representation
 */
static char *wday[8] =
{
  "??",
  "Mo",
  "Tu",
  "We",
  "Th",
  "Fr",
  "Sa",
  "Su"
};

/*-----------------------------------------------------------------------
 * generate a string representation for a timeval
 */
static char * pr_timeval(val)
  struct timeval *val;
{
  static char buf[20];

  if (val->tv_sec == 0)
    sprintf(buf, "%c0.%06d", (val->tv_usec < 0) ? '-' : '+', abs(val->tv_usec));
  else
    sprintf(buf, "%d.%06d", val->tv_sec, abs(val->tv_usec));
  return buf;
}

/*-----------------------------------------------------------------------
 * correct the current time by an offset by setting the time rigorously
 */
static void set_time(offset)
  struct timeval *offset;
{
  struct timeval the_time;

  if (no_set)
    return;

  LPRINTF("set_time: %s ", pr_timeval(offset));
  syslog(LOG_NOTICE, "setting time (offset %s)", pr_timeval(offset));

  if (gettimeofday(&the_time, 0L) == -1)
    {
      perror("gettimeofday()");
    }
  else
    {
      timeradd(&the_time, offset);
      if (settimeofday(&the_time, 0L) == -1)
	{
	  perror("settimeofday()");
	}
    }
}

/*-----------------------------------------------------------------------
 * slew the time by a given offset
 */
static void adj_time(offset)
  register long offset;
{
  struct timeval time_offset;

  if (no_set)
    return;

  time_offset.tv_sec  = offset / 1000000;
  time_offset.tv_usec = offset % 1000000;

  LPRINTF("adj_time: %d us ", offset);
  if (adjtime(&time_offset, 0L) == -1)
    perror("adjtime()");
}

/*-----------------------------------------------------------------------
 * read in a possibly previously written drift value
 */
static void read_drift(drift_file)
  char *drift_file;
{
  FILE *df;

  df = fopen(drift_file, "r");
  if (df != NULL)
    {
      int idrift, fdrift;

      fscanf(df, "%4d.%03d", &idrift, &fdrift);
      fclose(df);
      LPRINTF("read_drift: %d.%03d ppm ", idrift, fdrift);

      drift_comp = idrift << USECSCALE;
      fdrift     = (fdrift << USECSCALE) / 1000;
      drift_comp += fdrift & (1<<USECSCALE);
      LPRINTF("read_drift: drift_comp %d ", drift_comp);
    }
}

/*-----------------------------------------------------------------------
 * write out the current drift value
 */
static void update_drift(drift_file, offset, reftime)
  char *drift_file;
  long offset;
  time_t reftime;
{
  FILE *df;

  df = fopen(drift_file, "w");
  if (df != NULL)
    {
      int idrift = R_SHIFT(drift_comp, USECSCALE);
      int fdrift = drift_comp & ((1<<USECSCALE)-1);

      LPRINTF("update_drift: drift_comp %d ", drift_comp);
      fdrift = (fdrift * 1000) / (1<<USECSCALE);
      fprintf(df, "%4d.%03d %c%d.%06d %.24s\n", idrift, fdrift,
	      (offset < 0) ? '-' : '+', abs(offset) / 1000000, abs(offset) % 1000000,
	      asctime(localtime(&reftime)));
      fclose(df);
      LPRINTF("update_drift: %d.%03d ppm ", idrift, fdrift);
    }
}

/*-----------------------------------------------------------------------
 * process adjustments derived from the DCF77 observation
 * (controls clock PLL)
 */
static void adjust_clock(offset, drift_file, reftime)
  struct timeval *offset;
  char *drift_file;
  time_t reftime;
{
  struct timeval toffset;
  register long usecoffset;
  int tmp;

  if (no_set)
    return;

  if (skip_adjust)
    {
      skip_adjust = 0;
      return;
    }

  toffset = *offset;
  toffset.tv_sec  = abs(toffset.tv_sec);
  toffset.tv_usec = abs(toffset.tv_usec);
  if (timercmp(&toffset, &max_adj_offset, >))
    {
      /*
       * hopeless - set the clock - and clear the timing
       */
      set_time(offset);
      clock_adjust = 0;
      skip_adjust  = 1;
      return;
    }

  usecoffset   = offset->tv_sec * 1000000 + offset->tv_usec;

  clock_adjust = R_SHIFT(usecoffset, TIMECONSTANT);	/* adjustment to make for next period */

  tmp = 0;
  while (adjustments > (1 << tmp))
    tmp++;
  adjustments = 0;
  if (tmp > FREQ_WEIGHT)
    tmp = FREQ_WEIGHT;

  drift_comp  += R_SHIFT(usecoffset << USECSCALE, TIMECONSTANT+TIMECONSTANT+FREQ_WEIGHT-tmp);

  if (drift_comp > MAX_DRIFT)		/* clamp into interval */
    drift_comp = MAX_DRIFT;
  else
    if (drift_comp < -MAX_DRIFT)
      drift_comp = -MAX_DRIFT;

  update_drift(drift_file, usecoffset, reftime);
  LPRINTF("clock_adjust: %s, clock_adjust %d, drift_comp %d(%d) ",
		  pr_timeval(offset), R_SHIFT(clock_adjust, USECSCALE) , R_SHIFT(drift_comp, USECSCALE), drift_comp);
}

/*-----------------------------------------------------------------------
 * adjust the clock by a small mount to simulate frequency correction
 */
static void periodic_adjust()
{
  register long adjustment;

  adjustments++;

  adjustment = R_SHIFT(clock_adjust, PHASE_WEIGHT);

  clock_adjust -= adjustment;

  adjustment += R_SHIFT(drift_comp, USECSCALE+ADJINTERVAL);

  adj_time(adjustment);
}

/*-----------------------------------------------------------------------
 * control synchronisation status (warnings) and do periodic adjusts
 * (frequency control simulation)
 */
static void tick()
{
  static unsigned long last_notice = 0;

#ifndef SV_ONSTACK
  (void)signal(SIGALRM, tick);
#endif

  periodic_adjust();

  ticks += 1<<ADJINTERVAL;

  if ((ticks - last_sync) > MAX_UNSYNC)
    {
      /*
       * not getting time for a while
       */
      if (sync_state == SYNC)
	{
	  /*
	   * completely lost information
	   */
	  sync_state = NO_SYNC;
	  syslog(LOG_INFO, "DCF77 reception lost (timeout)");
	  last_notice = ticks;
	}
      else
	/*
	 * in NO_SYNC state - look whether its time to speak up again
	 */
	if ((ticks - last_notice) > NOTICE_INTERVAL)
	  {
	    syslog(LOG_NOTICE, "still not synchronized to DCF77 - check receiver/signal");
	    last_notice = ticks;
	  }
    }

#ifndef ITIMER_REAL
  (void) alarm(1<<ADJINTERVAL);
#endif
}

/*-----------------------------------------------------------------------
 * break association from terminal to avaoid catching terminal
 * or process group related signals (-> daemon operation)
 */
static void detach()
{
  int s;

  if (fork())
    exit(0);

  for (s = 0; s < 3; s++)
    (void) close(s);
  (void) open("/", 0);
  (void) dup2(0, 1);
  (void) dup2(0, 2);

#if defined(NTP_POSIX_SOURCE) || defined(_POSIX_)
  (void) setsid();
#else  /* _POSIX_ */
#ifndef BSD
  (void) setpgrp();
#else  /* BSD */
  (void) setpgrp(0, getpid());
#endif /* BSD */
#endif /* _POSIX_ */
#if defined(hpux)
  if (fork())
    exit(0);
#endif /* hpux */
}

/*-----------------------------------------------------------------------
 * list possible arguments and options
 */
static void usage(program)
  char *program;
{
  fprintf(stderr, "usage: %s [-f] [-l] [-t] [-i] [-o] [-d <drift_file>] <device>\n", program);
  fprintf(stderr, "\t-n              do not change time\n");
  fprintf(stderr, "\t-i              interactive\n");
  fprintf(stderr, "\t-t              trace (print all datagrams)\n");
  fprintf(stderr, "\t-f              print all databits (includes PTB private data)\n");
  fprintf(stderr, "\t-l              print loop filter debug information\n");
  fprintf(stderr, "\t-o              print offet average for current minute\n");
  fprintf(stderr, "\t-d <drift_file> specify alternate drift file\n");
  fprintf(stderr, "\t-D <input delay>specify delay from input edge to processing in micro seconds\n");
}

/*-----------------------------------------------------------------------
 * main loop - argument interpreter / setup / main loop
 */
int
main(argc, argv)
  int argc;
  char **argv;
{
  unsigned char c;
  char **a = argv;
  int  ac = argc;
  char *file = NULL;
  char *drift_file = "/etc/dcfd.drift";
  int fd;
  int offset = 15;
  int offsets = 0;
  int delay = DEFAULT_DELAY;	/* average delay from input edge to time stamping */
  int trace = 0;
  int errs = 0;

  /*
   * process arguments
   */
  while (--ac)
    {
      char *arg = *++a;
      if (*arg == '-')
	while ((c = *++arg))
	  switch (c)
	    {
	      case 't':
		    trace = 1;
		    interactive = 1;
		    break;

	      case 'f':
		    offset = 0;
		    interactive = 1;
		    break;

	      case 'l':
		    loop_filter_debug = 1;
		    offsets = 1;
		    interactive = 1;
		    break;

              case 'n':
		    no_set = 1;
		    break;

	      case 'o':
		    offsets = 1;
		    interactive = 1;
		    break;

	      case 'i':
		    interactive = 1;
		    break;

	      case 'D':
		    if (ac > 1)
		      {
			delay = atoi(*++a);
			ac--;
		      }
		    else
		      {
			fprintf(stderr, "%s: -D requires integer argument\n", argv[0]);
			errs=1;
		      }
		    break;
	      
	      case 'd':
		    if (ac > 1)
		      {
			drift_file = *++a;
			ac--;
		      }
		    else
		      {
			fprintf(stderr, "%s: -d requires file name argument\n", argv[0]);
			errs=1;
		      }
		    break;
	      
	      default:
		    fprintf(stderr, "%s: unknown option -%c\n", argv[0], c);
		    errs=1;
		    break;
	    }
      else
	if (file == NULL)
	  file = arg;
	else
	  {
	    fprintf(stderr, "%s: device specified twice\n", argv[0]);
	    errs=1;
	  }
    }

  if (errs)
    {
      usage(argv[0]);
      exit(1);
    }
  else
    if (file == NULL)
      {
	fprintf(stderr, "%s: device not specified\n", argv[0]);
	usage(argv[0]);
	exit(1);
      }

  errs = LINES+1;

  /*
   * get access to DCF77 tty port
   */
  fd = open(file, O_RDONLY);
  if (fd == -1)
    {
      perror(file);
      exit(1);
    }
  else
    {
      int i, rrc;
      struct timeval t, tt, tlast;
      struct timeval timeout;
      struct timeval phase;
      struct timeval time_offset;
      char pbuf[61];		/* printable version */
      char buf[61];		/* raw data */
      clocktime_t clock;	/* wall clock time */
      time_t utc_time = 0;
      time_t last_utc_time = 0;
      long usecerror = 0;
      long lasterror = 0;
#if defined(HAVE_TERMIOS) || defined(STREAM)
      struct termios term;
#endif
#if defined(HAVE_TERMIO) || defined(HAVE_SYSV_TTYS)
      struct termio term;
#endif
      int rtc = CVT_NONE;

      timeout.tv_sec  = 1;
      timeout.tv_usec = 500000;

      phase.tv_sec    = 0;
      phase.tv_usec   = delay;

      /*
       * setup TTY (50 Baud, Read, 8Bit, No Hangup, 1 character IO)
       */
      if (TTY_GETATTR(fd,  &term) == -1)
	{
	  perror("tcgetattr");
	  exit(1);
	}

      memset(term.c_cc, 0, sizeof(term.c_cc));
      term.c_cc[VMIN] = 1;
      term.c_cflag = B50|CS8|CREAD|CLOCAL;
      term.c_iflag = 0;
      term.c_oflag = 0;
      term.c_lflag = 0;

      if (TTY_SETATTR(fd, &term) == -1)
	{
	  perror("tcsetattr");
	  exit(1);
	}

      /*
       * loose terminal if in daemon operation
       */
      if (!interactive)
	detach();
      
      /*
       * get syslog() initialized
       */
#ifdef LOG_DAEMON
      openlog("dcfd", LOG_PID, LOG_DAEMON);
#else
      openlog("dcfd", LOG_PID);
#endif

      /*
       * setup periodic operations (state control / frequency control)
       */
#ifdef SV_ONSTACK
      {
	struct sigvec vec;

	vec.sv_handler = tick;
	vec.sv_mask    = 0;
	vec.sv_flags   = 0;

	if (sigvec(SIGALRM, &vec, (struct sigvec *)0) == -1)
	  {
	    syslog(LOG_ERR, "sigvec(SIGALRM): %m");
	    exit(1);
	  }
      }
#else
      (void) signal(SIGALRM, tick);
#endif

#ifdef ITIMER_REAL
      {
	struct itimerval it;

	it.it_interval.tv_sec  = 1<<ADJINTERVAL;
	it.it_interval.tv_usec = 0;
	it.it_value.tv_sec     = 1<<ADJINTERVAL;
	it.it_value.tv_usec    = 0;
	
      if (setitimer(ITIMER_REAL, &it, (struct itimerval *)0) == -1)
	{
	  syslog(LOG_ERR, "setitimer: %m");
	  exit(1);
	}
      }
#else
      (void) alarm(1<<ADJINTERVAL);
#endif

      PRINTF("  DCF77 monitor - Copyright 1993, Frank Kardel\n\n");

      pbuf[60] = '\0';
      for ( i = 0; i < 60; i++)
	pbuf[i] = '.';

      read_drift(drift_file);

      /*
       * what time is it now (for interval measurement)
       */
      gettimeofday(&tlast, 0L);
      i = 0;
      /*
       * loop until input trouble ...
       */
      do
	{
	  /*
	   * get an impulse
	   */
	  while ((rrc = read(fd, &c, 1)) == 1)
	    {
	      gettimeofday(&t, 0L);
	      tt = t;
	      timersub(&t, &tlast);

	      if (errs > LINES)
		{
		  PRINTF("  %s", &"PTB private....RADMLSMin....PHour..PMDay..DayMonthYear....P\n"[offset]);
		  PRINTF("  %s", &"---------------RADMLS1248124P124812P1248121241248112481248P\n"[offset]);
		  errs = 0;
		}

	      /*
	       * timeout -> possible minute mark -> interpretation
	       */
	      if (timercmp(&t, &timeout, >))
		{
		  PRINTF("%c %.*s ", pat[i % (sizeof(pat)-1)], 59 - offset, &pbuf[offset]);

		  if ((rtc = cvt_rawdcf(buf, i, &clock)) != CVT_OK)
		    {
		      /*
		       * this data was bad - well - forget synchronisation for now
		       */
		      PRINTF("\n");
		      if (sync_state == SYNC)
			{
			  sync_state = NO_SYNC;
			  syslog(LOG_INFO, "DCF77 reception lost (bad data)");
			}
		      errs++;
		    }

		  buf[0] = c;

		  /*
		   * collect first character
		   */
		  if (((c^0xFF)+1) & (c^0xFF))
		    pbuf[0] = '?';
		  else
		    pbuf[0] = type(c) ? '#' : '-';

		  for ( i = 1; i < 60; i++)
		    pbuf[i] = '.';

		  i = 0;
		}
	      else
		{
		  /*
		   * collect character
		   */
		  buf[i] = c;

		  /*
		   * initial guess (usually correct)
		   */
		  if (((c^0xFF)+1) & (c^0xFF))
		    pbuf[i] = '?';
		  else
		    pbuf[i] = type(c) ? '#' : '-';

		  PRINTF("%c %.*s ", pat[i % (sizeof(pat)-1)], 59 - offset, &pbuf[offset]);
		}

	      if (i == 0 && rtc == CVT_OK)
		{
		  /*
		   * we got a good time code here - try to convert it to
		   * UTC
		   */
		  if ((utc_time = dcf_to_unixtime(&clock, &rtc)) == -1)
		    {
		      PRINTF("*** BAD CONVERSION\n");
		    }

		  if (utc_time != (last_utc_time + 60))
		    {
		      /*
		       * well, two successive sucessful telegrams are not 60 seconds
		       * apart
		       */
		      PRINTF("*** NO MINUTE INC\n");
		      if (sync_state == SYNC)
			{
			  sync_state = NO_SYNC;
			  syslog(LOG_INFO, "DCF77 reception lost (data mismatch)");
			}
		      errs++;
		      rtc = CVT_FAIL|CVT_BADTIME|CVT_BADDATE;
		    }
		  else
		    usecerror = 0;

		  last_utc_time = utc_time;
		}

	      if (rtc == CVT_OK)
		{
		  if (trace && (i == 0))
		    {
		      PRINTF("\r  %.*s ", 59 - offset, &buf[offset]);
		    }

		  if (i == 0)
		    {
		      /*
		       * valid time code - determine offset and
		       * note regained reception
		       */
		      last_sync = ticks;
		      if (sync_state == NO_SYNC)
			{
			  syslog(LOG_INFO, "receiving DCF77");
			}
		      else
			{
			  /*
			   * we had at least one minute SYNC - thus
			   * last error is valid
			   */
			  time_offset.tv_sec  = lasterror / 1000000;
			  time_offset.tv_usec = lasterror % 1000000;
			  adjust_clock(&time_offset, drift_file, utc_time);
			}
		      sync_state = SYNC;
		    }

		  time_offset.tv_sec  = utc_time + i;
		  time_offset.tv_usec = 0;

		  timeradd(&time_offset, &phase);

		  usecerror += (time_offset.tv_sec - tt.tv_sec) * 1000000 + time_offset.tv_usec
		    -tt.tv_usec;

		  /*
		   * output interpreted DCF77 data
		   */
		  PRINTF(offsets ? "%s, %2d:%02d:%02d, %d.%02d.%02d, <%s%s%s%s> (%c%d.%06ds)" :
			 "%s, %2d:%02d:%02d, %d.%02d.%02d, <%s%s%s%s>",
			 wday[clock.wday],
			 clock.hour, clock.minute, i, clock.day, clock.month,
			 clock.year,
			 (clock.flags & DCFB_ALTERNATE) ? "R" : "_",
			 (clock.flags & DCFB_ANNOUNCE) ? "A" : "_",
			 (clock.flags & DCFB_DST) ? "D" : "_",
			 (clock.flags & DCFB_LEAP) ? "L" : "_",
			 (lasterror < 0) ? '-' : '+', abs(lasterror) / 1000000, abs(lasterror) % 1000000
			 );

		  if (trace && (i == 0))
		    {
		      PRINTF("\n");
		      errs++;
		    }
		  lasterror = usecerror / (i+1);
		}
	      else
		{
                  lasterror = 0; /* we cannot calculate phase errors on bad reception */
		}

	      PRINTF("\r");

	      if (i < 60)
		{
		  i++;
		}

	      tlast = tt;

	      if (interactive)
		fflush(stdout);
	    }
	} while ((rrc == -1) && (errno == EINTR));
      
      /*
       * lost IO - sorry guys
       */
      syslog(LOG_ERR, "TERMINATING - cannot read from device %s (%m)", file);

      (void)close(fd);
    }

  closelog();
  
  return 0;
}