comparison bdengine.c @ 2:9ec037775c38

removed code for &include in input files
author meillo@marmaro.de
date Mon, 17 Dec 2007 08:02:15 +0100
parents 22b6e71de68e
children dc2f94280b01
comparison
equal deleted inserted replaced
1:8534f0e3a0db 2:9ec037775c38
110 110
111 int skptok(int j, char *ptr); 111 int skptok(int j, char *ptr);
112 int evcmp(const void *e1, const void *e2); 112 int evcmp(const void *e1, const void *e2);
113 char *deffname(void); 113 char *deffname(void);
114 114
115 struct event *dir_include(char *dir, char *parm); 115 /*struct event *dir_include(char *dir, char *parm);*/
116 116
117 /* ========== Global variables */ 117 /* ========== Global variables */
118 118
119 struct date today; 119 struct date today;
120 int iDWarn = DEF_WARN; 120 int iDWarn = DEF_WARN;
134 {"for", F_FORDAYS}, 134 {"for", F_FORDAYS},
135 {NULL, 0} 135 {NULL, 0}
136 }; 136 };
137 137
138 138
139 /* list of directives. These are entered in the file prefixed by '&'. The function should be declared as
140 struct event *dir_func(char *directive, char *parameters);
141 and should return a pointer to a NULL-terminated list of extra records, or NULL if there are none.
142 This structure will allow us to dynamically add directives, if we wish to do so in the future.
143 */
144
145 struct {
146 const char *text;
147 int len;
148 struct event *(*func)(char*,char*);
149 } directlist[] = {
150 { "include", 0, dir_include },
151 { NULL, 0, NULL }
152 };
153 139
154 /* ========== */ 140 /* ========== */
155 141
156 142
157 143
354 340
355 341
356 char* deffname(void) { 342 char* deffname(void) {
357 char buf[256]; 343 char buf[256];
358 344
359 strcpy(buf,getpwuid(getuid())->pw_dir); 345 strcpy(buf, getpwuid(getuid())->pw_dir);
360 strcat(buf, "/" DEFAULT_FILE); 346 strcat(buf, "/" DEFAULT_FILE);
361 347
362 return strdup(buf); 348 return strdup(buf);
363 } 349 }
364 350
503 int i,j,k,l,d; 489 int i,j,k,l,d;
504 struct event *evl; 490 struct event *evl;
505 char buf[1024], buf2[1024]; 491 char buf[1024], buf2[1024];
506 char *ptr; 492 char *ptr;
507 unsigned flags; 493 unsigned flags;
508 struct event *nevl; 494
509 /* initialise */ 495 /* initialise */
510 if (fname==NULL) { 496 if (fname==NULL) {
511 fname=deffname(); 497 fname=deffname();
512 } 498 }
513 499
526 512
527 513
528 for (i = 0, evl=NULL; fgets(buf, sizeof(buf), file) != NULL; i++) { 514 for (i = 0, evl=NULL; fgets(buf, sizeof(buf), file) != NULL; i++) {
529 evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1)); 515 evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1));
530 516
531 if (*buf == '#' || *buf == '\n') 517 /* ignore comments and empty lines */
532 { 518 if (*buf == '#' || *buf == '\n') {
533 i--; 519 i--;
534 continue;
535 }
536 if (*buf == '&') {
537 buf[strlen(buf)-1] = 0;
538 if ((ptr=strchr(buf+1,' ')) == NULL && (ptr=strchr((buf+1),'\t')) == NULL) {
539 ptr=(buf+1)+strlen((buf+1));
540 }
541
542 *ptr++=0;
543 nevl=NULL;
544 k=0;
545 for (j=0; directlist[j].text != NULL; j++) {
546 if (directlist[j].len == 0) directlist[j].len = strlen(directlist[j].text);
547 /*
548 fprintf(stderr, "Checking against directive #%d, %s, which has length %d, against \"%s\" with length %d\n",
549 j, directlist[j].text, directlist[j].len, buf+1, ptr-(buf+1)-1);
550 */
551 if (directlist[j].len == ptr-(buf+1)-1 && !strncmp(directlist[j].text,(buf+1),directlist[j].len)) {
552 nevl = directlist[j].func((buf+1),ptr);
553 k=1;
554 break;
555 }
556 }
557
558 if (nevl != NULL) {
559 for (j=0; nevl[j].text != NULL; j++);
560 i+=j-1;
561 evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1));
562 /* in fact, this loop reverses the order of the elements, but we're going to sort them later anyway. */
563 for (j=0; nevl[j].text != NULL; j++)
564 evl[i-j]=nevl[j];
565 }
566 if (!k) {
567 fprintf(stderr, "Warning: unrecognised directive \"%s\" ignored.\n", (buf+1));
568 i--;
569 }
570 continue; 520 continue;
571 } 521 }
572 522
573 /* parse string in buf */ 523 /* parse string in buf */
574 ptr = strrchr(buf, '='); /* allow '=' in text */ 524 ptr = strrchr(buf, '='); /* allow '=' in text */
575 if(ptr==NULL) /* not a valid line, so ignore it! Cool, huh? */ 525
576 { 526 /* not a valid line, so ignore it! Cool, huh? */
577 fprintf(stderr, "WARNING: Invalid line in input file:\n%s", buf); 527 if (ptr == NULL) {
578 i--; 528 fprintf(stderr, "WARNING: Invalid line in input file:\n%s", buf);
579 continue; 529 i--;
580 } 530 continue;
531 }
581 532
582 *(ptr++) = 0; 533 *(ptr++) = 0;
583 534
584 j = sscanf(ptr, "%u-%u-%u", &(evl[i].date.year), &(evl[i].date.month), &(evl[i].date.day)); 535 j = sscanf(ptr, "%u-%u-%u", &(evl[i].date.year), &(evl[i].date.month), &(evl[i].date.day));
585 /* if our year is only two digits, add 1900 to it ... */ 536 /* if our year is only two digits, add 1900 to it ... */
591 542
592 evl[i].warn=iDWarn; 543 evl[i].warn=iDWarn;
593 evl[i].enddate.day=evl[i].enddate.month=evl[i].enddate.year=0; 544 evl[i].enddate.day=evl[i].enddate.month=evl[i].enddate.year=0;
594 545
595 flags=j=0; 546 flags=j=0;
596 while(j = skptok(j, ptr),ptr[j]!=0) 547 while(j = skptok(j, ptr),ptr[j]!=0) {
597 { 548 for (k = 0; FTABLE[k].txt != NULL && strncmp(FTABLE[k].txt, ptr + j, strlen(FTABLE[k].txt)); k++);
598 for (k = 0; FTABLE[k].txt != NULL && strncmp(FTABLE[k].txt, ptr + j, strlen(FTABLE[k].txt)); k++); 549 switch (FTABLE[k].flag) {
599 switch (FTABLE[k].flag) {
600 case F_WTIME_P: /* w<n> -- sets warning time */ 550 case F_WTIME_P: /* w<n> -- sets warning time */
601 sscanf(ptr + j, "w %u", &(evl[i].warn)); 551 sscanf(ptr + j, "w %u", &(evl[i].warn));
602 break; 552 break;
603 case F_FORDAYS: /* for<days> -- sets the duration of the event */ 553 case F_FORDAYS: /* for<days> -- sets the duration of the event */
604 sscanf(ptr + j, "for %d", &d); 554 sscanf(ptr + j, "for %d", &d);
616 } 566 }
617 } 567 }
618 break; 568 break;
619 case F_TODATE: 569 case F_TODATE:
620 l = sscanf(ptr + j, "to %u-%u-%u", &(evl[i].enddate.year), &(evl[i].enddate.month), &(evl[i].enddate.day)); 570 l = sscanf(ptr + j, "to %u-%u-%u", &(evl[i].enddate.year), &(evl[i].enddate.month), &(evl[i].enddate.day));
621 if (evl[i].enddate.year < 100) evl[i].enddate.year+=1900; 571 if (evl[i].enddate.year < 100) {
622 if (l == 2) evl[i].enddate.year=0; 572 evl[i].enddate.year+=1900;
573 }
574 if (l == 2) {
575 evl[i].enddate.year=0;
576 }
623 break; 577 break;
624 case 0: 578 case 0:
625 break; 579 break;
626 default: 580 default:
627 flags|=FTABLE[k].flag; 581 flags|=FTABLE[k].flag;
628 break; 582 break;
583 }
584 }
585
586
587 /* construct event text */
588
589 switch(flags & F_MTYPE) {
590 case F_TBIRTHDAY:
591 default: /* assume it's a birthday */
592 if (evl[i].date.year != 0) {
593 int tmp_age=ydelta(evl[i].date, today);
594 if (tmp_age!=1) {
595 sprintf(buf2, "%s is %d years old", buf, tmp_age);
596 } else {
597 sprintf(buf2, "%s is %d year old", buf, tmp_age);
598 }
599 } else {
600 sprintf(buf2, "%s has a birthday", buf);
629 } 601 }
630 } 602 break;
631 603 case F_TANNIVERSARY:
632 /* construct event text */ 604 if (evl[i].date.year != 0) {
633 605 sprintf(buf2, "%s %d years ago", buf, ydelta(evl[i].date, today));
634 switch(flags & F_MTYPE) {
635 case F_TBIRTHDAY:
636 default: /* assume it's a birthday */
637 if (evl[i].date.year != 0) {
638 int tmp_age=ydelta(evl[i].date, today);
639 if (tmp_age!=1) {
640 sprintf(buf2, "%s is %d years old", buf, tmp_age);
641 } else { 606 } else {
642 sprintf(buf2, "%s is %d year old", buf, tmp_age); 607 strcpy(buf2, buf);
643 } 608 }
644 } else { 609 break;
645 sprintf(buf2, "%s has a birthday", buf); 610 case F_TEVENT:
646 } 611 /* if a year was specified, and this warning isn't for it, ignore! */
647 break; 612 if ((evl[i].date.year != 0 && ydelta(evl[i].date, today) != 0) &&
648 case F_TANNIVERSARY: 613 (evl[i].enddate.year == 0 || ydelta(evl[i].enddate, today) != 0)) {
649 if (evl[i].date.year != 0) { 614 i--;
650 sprintf(buf2, "%s %d years ago", buf, ydelta(evl[i].date, today)); 615 continue;
651 } else { 616 }
652 strcpy(buf2, buf); 617 strcpy(buf2, buf);
653 } 618 break;
654 break; 619 case F_TMESSAGE:
655 case F_TEVENT: 620 /* Like an event, except that it only comes up on the given date, and no text at all is appended */
656 /* if a year was specified, and this warning isn't for it, ignore! */ 621 if ((evl[i].date.year != 0 && ydelta(evl[i].date, today) != 0) &&
657 if ((evl[i].date.year != 0 && ydelta(evl[i].date, today) != 0) && 622 (evl[i].enddate.year == 0 || ydelta(evl[i].enddate, today) != 0)) {
658 (evl[i].enddate.year == 0 || ydelta(evl[i].enddate, today) != 0)) { 623 i--;
659 i--; 624 continue;
660 continue; 625 }
661 } 626 strcpy(buf2, buf);
662 strcpy(buf2, buf); 627 evl[i].warn=-1; /* special code! */
663 break; 628 break;
664 case F_TMESSAGE:
665 /* Like an event, except that it only comes up on the given date, and no text at all is appended */
666 if ((evl[i].date.year != 0 && ydelta(evl[i].date, today) != 0) &&
667 (evl[i].enddate.year == 0 || ydelta(evl[i].enddate, today) != 0)) {
668 i--;
669 continue;
670 }
671 strcpy(buf2, buf);
672 evl[i].warn=-1; /* special code! */
673 break;
674 } 629 }
675 evl[i].text = strdup(buf2); 630 evl[i].text = strdup(buf2);
676 } 631 }
677 632
678 evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1)); 633 evl = (struct event *) xrealloc(evl, sizeof(struct event) * (i + 1));
700 655
701 return j; 656 return j;
702 } 657 }
703 658
704 659
705
706
707
708
709 /* ---------------------------------------------------------------------- */
710 /* Functions to parse input file directives */
711 struct event *dir_include(char *dir, char *parm) {
712 struct event *evl;
713
714 evl=readlist(strdup(parm));
715
716 return evl;
717 }