-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathprocess.c
11845 lines (10019 loc) · 378 KB
/
process.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
/*----------------------------------------------------------------------------+
| |
| Enhanced HEYU Functionality for Uploaded Timers and Macros |
| Copyright 2002,2003,2004,2005,2006 Charles W. Sullivan |
| |
| |
| As used herein, HEYU is a trademark of Daniel B. Suthers. |
| X10, CM11A, and ActiveHome are trademarks of X-10 (USA) Inc. |
| The author is not affiliated with either entity. |
| |
| Charles W. Sullivan |
| Co-author and Maintainer |
| Greensboro, North Carolina |
| Email ID: cwsulliv01 |
| Email domain: -at- heyu -dot- org |
| |
+----------------------------------------------------------------------------*/
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <ctype.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <stdio.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include <time.h>
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "x10.h"
#include "sun.h"
#include "process.h"
#define NDSTINTV 6
struct dststruct {
long elapsed; /* Elapsed minutes from 00:00 hours legal time Jan 1st */
int offset; /* Minutes to add to ST to get Legal Time during each */
/* interval. */
} lgls[NDSTINTV], dsts[NDSTINTV], stds[NDSTINTV], stdr[NDSTINTV];
#if 0
#define NDSTINTV (int)(sizeof(lgls)/sizeof(struct dststruct))
#endif
/* Contains info written to or read from the x10record file */
struct record_info x10record = {0, 0, 0, 0, 0, 0, 0, 0};
struct record_info *x10recordp = &x10record;
#define PROMSIZE 1024
/* Global variables */
int line_no;
int timer_size = 0;
int timer_maxsize = 0;
int timer_savesize = 0;
int tevent_size = 0;
int tevent_savesize = 0;
int tevent_maxsize = 0;
int current_timer_generation = 0;
int save_timer_generation = 0;
int timer_generation_delta = 0;
int current_tevent_generation = 0;
int save_tevent_generation = 0;
int tevent_generation_delta = 0;
char default_housecode = 'A';
long int std_tzone; /* Timezone in seconds West of Greenwich */
char *heyu_tzname[2];
/* Directory (terminated with /) containing the critical */
/* heyu files x10config, x10record, and x10macroxref */
char heyu_path[PATH_LEN + 1];
char schedfile[PATH_LEN + 1];
char heyu_script[PATH_LEN + 1];
/* State file */
extern char statefile[];
/* Alternate optional directory (specified in x10config) */
/* for report and non-critical files */
char alt_path[PATH_LEN + 1];
char heyu_config[PATH_LEN + 1]; /* Filename of Heyu configuration file */
int is_writable; /* Flag: Heyu directory is writable */
/* External variables */
extern int verbose, i_am_relay;
extern CONFIG config;
extern CONFIG *configp;
extern struct opt_st *optptr;
char *wday_name[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
char *month_name[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
/* Extended error management functions - these allow stringing together */
/* error messages from a tree of function calls into a (hopefully) */
/* meaningful message for the user. */
/*----------------------------------------------------------------------------+
| Pass back pointer and size of error message buffer - for internal use by |
| functions below. |
+----------------------------------------------------------------------------*/
void error_area ( char **pointer, int *space )
{
static char error_buffer[1024];
*pointer = error_buffer;
*space = (sizeof(error_buffer)/sizeof(char));
return;
}
/*----------------------------------------------------------------------------+
| Save an error message in error message buffer |
+----------------------------------------------------------------------------*/
void store_error_message ( char *message )
{
char *buffp = NULL;
int space;
error_area(&buffp, &space);
if ( space < (int)strlen(message) + 1 ) {
fprintf(stderr, "Internal error: no space - store_error_message()\n");
exit(1);
}
strncpy2(buffp, message, space - 1);
return;
}
/*----------------------------------------------------------------------------+
| Prepend a message to an existing error message in buffer |
+----------------------------------------------------------------------------*/
void add_error_prefix ( char *prefix )
{
char *buffp = NULL;
char *oldmsg = NULL;
int space;
error_area(&buffp, &space);
if ( space < ((int)strlen(prefix) + (int)strlen(buffp) + 1) ) {
fprintf(stderr, "Internal error: no space - add_error_prefix()\n");
exit(1);
}
if ( (oldmsg = strdup(buffp)) == NULL ) {
fprintf(stderr, "Unable to allocate memory for error message\n");
exit(1);
}
strncpy2(buffp, prefix, space - 1);
strncat(buffp, oldmsg, space - 1 - strlen(buffp));
free(oldmsg);
return;
}
/*----------------------------------------------------------------------------+
| Append a message to an existing error message in buffer |
+----------------------------------------------------------------------------*/
void add_error_suffix ( char *suffix )
{
char *buffp = NULL;
int space;
error_area(&buffp, &space);
if ( space < ((int)strlen(buffp) + (int)strlen(suffix) + 1) ) {
fprintf(stderr, "Internal error: no space - add_error_suffix()\n");
exit(1);
}
strncat(buffp, suffix, space - 1 - strlen(buffp));
return;
}
/*----------------------------------------------------------------------------+
| Get pointer to error message buffer |
+----------------------------------------------------------------------------*/
char *error_message ( void )
{
char *buffp = NULL;
int space;
error_area(&buffp, &space);
return buffp;
}
/*----------------------------------------------------------------------------+
| Clear the error message buffer |
+----------------------------------------------------------------------------*/
void clear_error_message( void )
{
char *buffp = NULL;
int space;
error_area(&buffp, &space);
*buffp = '\0';
return;
}
/* Debugging functions */
void tp ( int point )
{
printf("testpoint %d\n", point);
fflush(stdout);
return ;
}
int show_timer_links( TIMER *timerp )
{
int j;
for ( j = 0; j < timer_maxsize; j++ )
printf("%3d %3d\n", j, timerp[j].link);
return 0;
}
/*----------------------------------------------------------------------------+
| Display tokens |
+----------------------------------------------------------------------------*/
void display_tokens ( int tokc, char *tokv[] )
{
int j;
for ( j = 0; j < tokc; j++ ) {
printf(" \"%s\"", tokv[j]);
}
printf("\n");
return;
}
/*----------------------------------------------------------------------------+
| Verify that the start and stop times in any timer are not the same. |
| (If they are, CM11A will not launch the stop macro.) |
| Return the number of timers failing the test. |
+----------------------------------------------------------------------------*/
int check_timer_start_stop ( TIMER *timerp )
{
int j, count;
if ( !timerp )
return 0;
j = 0;
count = 0;
while ( timerp[j].line_no > 0 ) {
if ( timerp[j].generation != current_timer_generation ||
timerp[j].flag_start == NO_EVENT ||
timerp[j].flag_stop == NO_EVENT ) {
j++;
continue;
}
if ( timerp[j].offset_stop == timerp[j].offset_start )
count++;
j++;
}
return count;
}
/*----------------------------------------------------------------------------+
| Verify an unbroken chain of TEVENT links. |
+----------------------------------------------------------------------------*/
int verify_tevent_links ( TEVENT *teventp )
{
int *linker, *linkee;
int j, k, start, size, retcode;
static int sizeint = sizeof(int);
if ( !teventp )
return 0;
size = 0;
while ( teventp[size].line_no > 0 )
size++;
if (!size)
return 0;
linker = calloc( size, sizeint );
if ( linker == NULL ) {
(void)fprintf(stderr, "verify_tevent_links() - Unable to allocate memory 1\n");
return -1;
}
linkee = calloc( size, sizeint );
if ( linkee == NULL ) {
(void)fprintf(stderr, "verify_tevent_links() - Unable to allocate memory 2\n");
return -1;
}
/* Find the start of the chain, i.e., the tevent not linked to by any other */
for ( j = 0; j < size; j++ ) {
linker[j] = 0;
}
for ( j = 0; j < size; j++ ) {
k = teventp[j].link;
if ( k > (size - 1) ) {
(void)fprintf(stderr,
"verify_tevent_links() - link %d out of bound %d at index %d\n", k, size - 1, j);
return -1;
}
if ( k >= 0 )
linker[k] = 1;
}
for ( start = 0; start < size; start++ ) {
if ( linker[start] == 0 )
break;
}
for ( j = 0; j < size; j++ ) {
linkee[j] = 0;
linker[j] = -1;
}
k = start;
for ( j = 0; j < size; j++ ) {
k = teventp[k].link;
if ( k < 0 && j < (size - 1) ) {
(void)fprintf(stderr,
"verify_tevent_links() - premature end of %d length chain at index %d\n", size, j);
return -1;
}
if ( k > (size - 1) ) {
(void)fprintf(stderr,
"verify_tevent_links() - link %d out of bound %d at index %d\n", k, size - 1, j);
return -1;
}
if ( k >= 0 ) {
linkee[k] += 1;
linker[k] = j;
}
}
retcode = 0;
for ( k = 1; k < size; k++ ) {
if ( linkee[k] == 0 ) {
(void)fprintf(stderr, "teventp[linkee[%d]] is not in chain.\n", k);
retcode = 1;
}
if ( linkee[k] > 1 ) {
(void)fprintf(stderr, "teventp[linkee[%d]] is multiply linked.\n", k);
retcode = 1;
}
if ( linkee[k] == -1 ) {
(void)fprintf(stderr, "End of chain at index %d\n", linker[k]);
retcode = 1;
}
}
free(linker);
free(linkee);
return retcode;
}
/*----------------------------------------------------------------------------+
| Verify an unbroken chain of TIMER links. |
+----------------------------------------------------------------------------*/
int verify_timer_links ( TIMER *timerp )
{
int *linker, *linkee;
int j, k, start, size, retcode;
static int sizeint = sizeof(int);
if ( !timerp )
return 0;
size = 0;
while ( timerp[size].line_no > 0 )
size++;
if (!size)
return 0;
linker = calloc( size, sizeint );
if ( linker == NULL ) {
(void)fprintf(stderr, "verify_timer_links() - Unable to allocate memory 1\n");
return -1;
}
linkee = calloc( size, sizeint );
if ( linkee == NULL ) {
(void)fprintf(stderr, "verify_timer_links() - Unable to allocate memory 2\n");
return -1;
}
/* Find the start of the chain, i.e., the timer not linked to by any other */
for ( j = 0; j < size; j++ ) {
linker[j] = 0;
}
for ( j = 0; j < size; j++ ) {
k = timerp[j].link;
if ( k > (size - 1) ) {
(void)fprintf(stderr,
"verify_timer_links() - link %d out of bound %d at index %d\n", k, size - 1, j);
return -1;
}
if ( k >= 0 )
linker[k] = 1;
}
for ( start = 0; start < size; start++ ) {
if ( linker[start] == 0 )
break;
}
for ( j = 0; j < size; j++ ) {
linkee[j] = 0;
linker[j] = -1;
}
for ( j = 0; j < size; j++ ) {
linkee[j] = 0;
linker[j] = -1;
}
k = start;
for ( j = 0; j < size; j++ ) {
k = timerp[k].link;
if ( k < 0 && j < (size - 1) ) {
(void)fprintf(stderr,
"verify_timerp_links() - premature end of %d length chain at index %d\n", size, j);
return -1;
}
if ( k > (size - 1) ) {
(void)fprintf(stderr,
"verify_timerp_links() - link %d out of bound %d at index %d\n", k, size, j);
return -1;
}
if ( k >= 0 ) {
linkee[k] += 1;
linker[k] = j;
}
}
retcode = 0;
for ( k = 1; k < size; k++ ) {
if ( linkee[k] == 0 ) {
(void)fprintf(stderr, "timerp[linkee[%d]] is not in chain.\n", k);
retcode = 1;
}
if ( linkee[k] > 1 ) {
(void)fprintf(stderr, "timerp[linkee[%d]] is multiply linked.\n", k);
retcode = 1;
}
if ( linkee[k] == -1 ) {
(void)fprintf(stderr, "End of chain at index %d\n", linker[k]);
retcode = 1;
}
}
free( linker );
free( linkee );
return retcode;
}
/* String/array manipulation functions */
/*----------------------------------------------------------------------------+
| Trim leading and trailing whitespace from argument string and return |
| pointer to string. Argument string itself is modified. |
+----------------------------------------------------------------------------*/
char *strtrim( char *string )
{
char *ss ;
char *sd ;
ss = sd = string ;
/* Move pointer to first non-whitespace character */
while ( *ss == ' ' || *ss == '\t' || *ss == '\n' || *ss == '\r')
ss++ ;
/* Close up leading whitespace */
while ( (*sd++ = *ss++) != '\0' )
;
/* Back up pointer to character before terminating NULL */
sd -= 2 ;
/* Replace trailing whitespace with NULLs */
while( sd >= string
&& ( *sd == ' ' || *sd == '\t' || *sd == '\n' || *sd == '\r' ) )
*sd-- = '\0' ;
return string ;
}
/*----------------------------------------------------------------------------+
| Convert a string to lower case. Return pointer to string. |
+----------------------------------------------------------------------------*/
char *strlower ( char *string )
{
char *sp = string ;
while ( *sp ) {
*sp = tolower((int)(*sp));
sp++ ;
}
return string;
}
/*----------------------------------------------------------------------------+
| Convert a string to Upper case. Return pointer to string. |
+----------------------------------------------------------------------------*/
char *strupper ( char *string )
{
char *sp = string ;
while ( *sp ) {
*sp = toupper((int)(*sp));
sp++ ;
}
return string;
}
/*----------------------------------------------------------------------------+
| Copy n characters from source to target string and append a trailing null. |
| Return a pointer to the target. (Length of target string must be n+1). |
+----------------------------------------------------------------------------*/
char *strncpy2 ( char *target, char *source, int n )
{
char *sp, *tp;
int count;
sp = source; tp = target; count = n;
while ( count-- > 0 && *sp ) {
*tp++ = *sp++ ;
}
*tp = '\0';
return target;
}
/*----------------------------------------------------------------------------+
| Convert high and low ulongs of a ulonglong to a double. |
+----------------------------------------------------------------------------*/
double hilo2dbl ( unsigned long high, unsigned long low )
{
#ifdef HAVE_UNSIGNED_LONG_LONG_INT
unsigned long long ull;
ull = (unsigned long long)high << 32 | (unsigned long long)low;
return (double)ull;
#else
return (double)high * 4294967296.0 + (double)low;
#endif
}
/*--------------------------------------------------------------------+
| Break up the string 'str' into tokens delimited by characters in |
| 'delim' and create the list 'tokv' of pointers to these tokens. |
| The original string is modified. Free 'tokv' after use. |
+--------------------------------------------------------------------*/
int tokenize ( char *str, char *delim, int *tokc, char ***tokv )
{
char *sp, *tp;
static int sizchptr = sizeof(char *);
*tokc = 0;
*tokv = NULL;
sp = str;
while ( *sp != '\0' ) {
/* Bypass leading delimiters */
while ( *sp != '\0' && strchr(delim, *sp) != NULL )
sp++;
if ( *sp == '\0' )
return 0;
tp = sp;
/* Advance to the next delimiter */
while ( *sp != '\0' && strchr(delim, *sp) == NULL )
sp++;
/* Terminate the token if not already at end of string */
if ( *sp != '\0' )
*sp++ = '\0';
/* Allocate space for the pointer */
if ( *tokc == 0 )
*tokv = calloc(1, sizchptr);
else
*tokv = realloc(*tokv, (*tokc + 1) * sizchptr);
if ( *tokv == NULL ) {
fprintf(stderr, "Unable to allocate memory in tokenize()\n");
exit(1);
}
(*tokv)[(*tokc)++] = tp;
}
/* Add a terminating NULL */
if ( *tokc == 0 )
*tokv = calloc(1, sizchptr);
else
*tokv = realloc(*tokv, (*tokc + 1) * sizchptr);
if ( *tokv == NULL ) {
fprintf(stderr, "Unable to allocate memory in tokenize()\n");
exit(1);
}
(*tokv)[(*tokc)] = (char *)NULL;
return 0;
}
/*--------------------------------------------------------------------+
| Copy maxlen-1 characters to target of the token delimited by delim |
| from the char string *nextpp. On return, *nextpp points to the |
| character following the token (or the part copied thereof). |
| The original string is unchanged. |
+--------------------------------------------------------------------*/
char *get_token( char *target, char **nextpp, char *delim, int maxlen )
{
char *sp, *tp;
int count = 0;
sp = *nextpp;
tp = target;
/* Bypass leading delimiters */
while ( *sp && strchr(delim, *sp) ) {
sp++ ;
}
/* Transfer up to maxlen-1 characters */
while ( *sp && !strchr(delim, *sp) && count < maxlen ) {
*tp++ = *sp++ ;
count++ ;
}
*nextpp = sp;
/* Terminate with a null character */
*tp = '\0';
return target;
}
/*---------------------------------------------------------------------+
| Left rotate the contents of an array. |
+---------------------------------------------------------------------*/
void lrotarray ( unsigned char *array, int length )
{
unsigned char hold;
int j;
hold = array[0];
for ( j = 0; j < length - 1; j++ )
array[j] = array[j + 1];
array[length - 1] = hold;
return;
}
/*---------------------------------------------------------------------+
| Right rotate the contents of an array. |
+---------------------------------------------------------------------*/
void rrotarray ( unsigned char *array, int length )
{
unsigned char hold;
int j;
hold = array[length - 1];
for ( j = length - 1; j > 0; j-- )
array[j] = array[j - 1];
array[0] = hold;
return;
}
/* X10 Encoding/Decoding functions */
/*---------------------------------------------------------------------+
| Convert housecode letter to x10 code. |
+---------------------------------------------------------------------*/
unsigned char hc2code ( char hc )
{
/* X10 codes for housecode letters A through P */
static unsigned char code[] =
{6,14,2,10,1,9,5,13,7,15,3,11,0,8,4,12};
return code[(toupper((int)hc) - 'A') & 0x0f];
}
/*---------------------------------------------------------------------+
| Convert x10 code to housecode letter. |
+---------------------------------------------------------------------*/
char code2hc ( unsigned char code )
{
char *hcode = "MECKOGAINFDLPHBJ";
return hcode[code & 0x0fu];
}
/*---------------------------------------------------------------------+
| Convert X10 unit number (1-16) to x10 code. |
+---------------------------------------------------------------------*/
unsigned char unit2code ( int unit )
{
/* X10 codes for unit 1 through 16 */
static unsigned char code[] =
{6,14,2,10,1,9,5,13,7,15,3,11,0,8,4,12};
return (unit > 0 && unit < 17) ? code[unit - 1] : 0xff;
}
/*---------------------------------------------------------------------+
| Convert X10 code to X10 unit number (1-16). |
+---------------------------------------------------------------------*/
int code2unit ( unsigned char code )
{
static int units[] = {13,5,3,11,15,7,1,9,14,6,4,12,16,8,2,10};
return units[code & 0x0fu];
}
/*---------------------------------------------------------------------+
| Convert bit position (0-15) in X10 bitmap to unit number. |
+---------------------------------------------------------------------*/
int bitpos2unit ( int bitpos )
{
static int units[] = {13,5,3,11,15,7,1,9,14,6,4,12,16,8,2,10};
return units[bitpos & 0x0f];
}
/*---------------------------------------------------------------------+
| Return X10 bitmap for Days of Week string. |
+---------------------------------------------------------------------*/
unsigned char dow2bmap ( char *dow )
{
char *pattern = "smtwtfs";
unsigned char bmap = 0;
unsigned char mask = 0x01;
char buffer[16];
int j;
if ( strlen(dow) != 7 )
return 0xff;
strncpy2(buffer, dow, sizeof(buffer) - 1);
strlower(buffer);
for ( j = 0; j < 7; j++ ) {
if ( buffer[j] == pattern[j] ) {
bmap |= mask;
mask = mask << 1 ;
}
else if ( buffer[j] == '.' )
mask = mask << 1 ;
else
return 0xff ;
}
return bmap;
}
/*---------------------------------------------------------+
| Return Days of Week string for X10 bitmap argument |
+---------------------------------------------------------*/
char *bmap2dow( unsigned char bmap )
{
static char buff[10];
int j;
char *days = "smtwtfs";
char *err = "-error-";
unsigned char mask = 0x01;
if ( bmap > 127 )
return err;
for ( j = 0; j < 7; j++ ) {
buff[j] = (mask & bmap) ? days[j] : '.' ;
mask = mask << 1;
}
buff[7] = '\0';
return buff;
}
/*---------------------------------------------------------+
| Return Linux tm_wday (Sun = 0) for X10 bitmap. |
| (Assumes only one wday represented in the bitmap.) |
+---------------------------------------------------------*/
int bmap2wday ( unsigned char bmap )
{
int j;
j = 0;
while ( (bmap = (bmap >> 1)) != 0 )
j++;
return j;
}
/*---------------------------------------------------------+
| Return ASCII weekday name for X10 DOW bitmap. |
| (Assumes only one wday represented in the bitmap.) |
+---------------------------------------------------------*/
char *bmap2ascdow ( unsigned char bmap )
{
return wday_name[bmap2wday(bmap)];
}
/*---------------------------------------------------------------------+
| Reverse the order of the lower 4 bits, e.g., 1 -> 8 and 8 -> 1 |
| while leaving the upper bits unchanged. |
+---------------------------------------------------------------------*/
unsigned char rev_low_nybble ( unsigned char input )
{
static unsigned char rev_table[] =
{0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15};
int j;
j = (int)(input & 0x0fu);
return (input & 0xf0u) | rev_table[j];
}
/*---------------------------------------------------------------------+
| Reverse the order of the bits in a byte. |
+---------------------------------------------------------------------*/
unsigned char rev_byte_bits ( unsigned char input )
{
int j;
unsigned char output = 0;
for ( j = 0; j < 8; j++ ) {
if ( input & (1 << j) )
output |= (0x80 >> j);
}
return output;
}
/*---------------------------------------------------------------------+
| Rotate the x10 Day of Week bitmap one bit to the left. This has |
| the effect of moving the day of an event into the following day, |
| e.g., bmap(".mt..fs") ==> bmap("s.tw..s") |
+---------------------------------------------------------------------*/
unsigned char lrotbmap ( unsigned char bmap )
{
return ( ((bmap & 0x40u) >> 6) | ( (bmap & 0x3fu) << 1 ) );
}
/*---------------------------------------------------------------------+
| Rotate the x10 Day of Week bitmap one bit to the right. This has |
| the effect of moving the day of an event into the preceding day, |
| e.g., bmap(".mt..fs") ==> bmap("sm..tf.") |
+---------------------------------------------------------------------*/
unsigned char rrotbmap ( unsigned char bmap )
{
return ( ((bmap & 0x01u) << 6) | ( (bmap & 0x7fu) >> 1 ) );
}
/*---------------------------------------------------------------------+
| Return the code for a unit in a bitmap which is presumed to contain |
| only a single unit or 0. If the bitmap contains more than a single |
| unit, an error value of 0xff is returned. |
+---------------------------------------------------------------------*/
unsigned char single_bmap_unit( unsigned int bitmap )
{
unsigned int invmap[] = {6,14,2,10,1,9,5,13,7,15,3,11,0,8,4,12};
unsigned int mask = 1;
unsigned char units[17], nunits;
int j;
nunits = 0; units[0] = 0;
for ( j = 0; j < 16; j++ ) {
mask = 0x01 << invmap[j] ;
if ( bitmap & mask ) {
units[nunits++] = invmap[j];
}
}
return (nunits <= 1) ? units[0] : 0xff;
}
/*---------------------------------------------------------------------+
| Parse the units list and return an X10 unit bitmap containing only |
| the first unit in the units list, with unit 0 acceptable. |
+---------------------------------------------------------------------*/
unsigned int units2single ( char *str )
{
static int umap[] = {6,14,2,10,1,9,5,13,7,15,3,11,0,8,4,12};
int unit;
char buffer[256];
char errmsg[64];
char *tail, *sp;
tail = str;
if ( *get_token(buffer, &tail, "-,", 255) == '\0' )
return 0;
unit = (int)strtol(buffer, &sp, 10);
if ( *sp ) {
sprintf(errmsg,
"Warning: Invalid character '%c' in units list (ignored).\n", *sp);
store_error_message(errmsg);
return 0;
}
if ( unit < 0 || unit > 16 ) {
sprintf(errmsg, "Warning: Unit number %d outside range 0-16 (ignored).\n", unit);
store_error_message(errmsg);
return 0;
}
if ( unit == 0 )
return 0;
return ( 1 << umap[unit - 1] );
}
/*---------------------------------------------------------------------+
| Parse the flags list and return a long bitmap, with bit 0 = flag 1, |
| bit 1 = flag 2, etc. |
+---------------------------------------------------------------------*/
unsigned long flags2longmap ( char *str )
{
char buffer[256];
char errmsg[80];
char *tail, *sp;
int flagmax = 32;
int flist[32];
int j, ustart, flag;
unsigned long longmap;
if ( strchr(str, '*') ) {
longmap = 0xffffffff;
return longmap;
}
for ( j = 0; j < 32; j++ )
flist[j] = 0;
ustart = 0; tail = str;
while ( *(get_token(buffer, &tail, "-,", 255)) ) {
flag = (int)strtol(buffer, &sp, 10);
if ( *sp ) {
sprintf(errmsg, "Invalid char '%c' in flags list.", *sp);
store_error_message(errmsg);
return 0;
}
if ( flag < 1 || flag > flagmax ) {
sprintf(errmsg, "Flag number %d outside range 1-%d.", flag, flagmax);
store_error_message(errmsg);
return 0;
}
if ( *tail == ',' || *tail == '\0' ) {