aboutsummaryrefslogtreecommitdiffstats
path: root/src/recur.c
blob: e9e5ac380b4e5c234177531e3be656ee8e2aff90 (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
/*
 * Calcurse - text-based organizer
 *
 * Copyright (c) 2004-2010 Frederic Culot <frederic@culot.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the
 *        following disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the
 *        following disclaimer in the documentation and/or other
 *        materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Send your feedback or comments to : misc@calcurse.org
 * Calcurse home page : http://calcurse.org
 *
 */

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <time.h>

#include "calcurse.h"

struct recur_apoint_list         *recur_alist_p;
struct recur_event               *recur_elist;
static struct recur_event         bkp_cut_recur_event;
static struct recur_apoint        bkp_cut_recur_apoint;


static void
free_exc (struct days *exc)
{
  struct days *o, **i;

  i = &exc;
  while (*i)
    {
      o = *i;
      *i = o->next;
      mem_free (o);
    }
}

static void
recur_add_exc (struct days **exc, long day)
{
  struct days **i, *o;

  o = mem_malloc (sizeof (struct days));
  o->st = day;
  i = exc;
  for (;;)
    {
      if (*i == 0 || (*i)->st > day)
        {
          o->next = *i;
          *i = o;
          break;
        }
      i = &(*i)->next;
    }
}

static void
exc_dup (struct days **in, struct days *exc)
{
  struct days *p;

  for (p = exc; p; p = p->next)
    recur_add_exc (in, p->st);
}

void
recur_event_free_bkp (enum eraseflg flag)
{
  if (bkp_cut_recur_event.mesg)
    {
      mem_free (bkp_cut_recur_event.mesg);
      bkp_cut_recur_event.mesg = 0;
    }
  if (bkp_cut_recur_event.rpt)
    {
      mem_free (bkp_cut_recur_event.rpt);
      bkp_cut_recur_event.rpt = 0;
    }
  if (bkp_cut_recur_event.exc)
    {
      free_exc (bkp_cut_recur_event.exc);
      bkp_cut_recur_event.exc = 0;
    }
  erase_note (&bkp_cut_recur_event.note, flag);
}

void
recur_apoint_free_bkp (enum eraseflg flag)
{
  if (bkp_cut_recur_apoint.mesg)
    {
      mem_free (bkp_cut_recur_apoint.mesg);
      bkp_cut_recur_apoint.mesg = 0;
    }
  if (bkp_cut_recur_apoint.rpt)
    {
      mem_free (bkp_cut_recur_apoint.rpt);
      bkp_cut_recur_apoint.rpt = 0;
    }
  if (bkp_cut_recur_apoint.exc)
    {
      free_exc (bkp_cut_recur_apoint.exc);
      bkp_cut_recur_apoint.exc = 0;
    }
  erase_note (&bkp_cut_recur_apoint.note, flag);
}

static void
recur_event_dup (struct recur_event *in, struct recur_event *bkp)
{
  EXIT_IF (!in || !bkp, _("null pointer"));

  bkp->id = in->id;
  bkp->day = in->day;
  bkp->mesg = mem_strdup (in->mesg);

  bkp->rpt = mem_malloc (sizeof (struct rpt));
  bkp->rpt->type = in->rpt->type;
  bkp->rpt->freq = in->rpt->freq;
  bkp->rpt->until = in->rpt->until;

  if (in->exc)
    exc_dup (&bkp->exc, in->exc);

  if (in->note)
    bkp->note = mem_strdup (in->note);
}

static void
recur_apoint_dup (struct recur_apoint *in, struct recur_apoint *bkp)
{
  EXIT_IF (!in || !bkp, _("null pointer"));

  bkp->start = in->start;
  bkp->dur = in->dur;
  bkp->state = in->state;
  bkp->mesg = mem_strdup (in->mesg);

  bkp->rpt = mem_malloc (sizeof (struct rpt));
  bkp->rpt->type = in->rpt->type;
  bkp->rpt->freq = in->rpt->freq;
  bkp->rpt->until = in->rpt->until;

  if (in->exc)
    exc_dup (&bkp->exc, in->exc);

  if (in->note)
    bkp->note = mem_strdup (in->note);
}

void
recur_apoint_llist_init (void)
{
  recur_alist_p = mem_malloc (sizeof (struct recur_apoint_list));
  recur_alist_p->root = NULL;
  pthread_mutex_init (&(recur_alist_p->mutex), NULL);
}

void
recur_apoint_llist_free (void)
{
  struct recur_apoint *o, **i;

  i = &recur_alist_p->root;
  while (*i)
    {
      o = *i;
      *i = o->next;
      mem_free (o->mesg);
      if (o->note)
        mem_free (o->note);
      if (o->rpt)
        mem_free (o->rpt);
      if (o->exc)
        {
          free_exc (o->exc);
          o->exc = 0;
        }
      mem_free (o);
    }
  mem_free (recur_alist_p);
}

void
recur_event_llist_free (void)
{
  struct recur_event *o, **i;

  i = &recur_elist;
  while (*i)
    {
      o = *i;
      *i = o->next;
      mem_free (o->mesg);
      if (o->note)
        mem_free (o->note);
      if (o->rpt)
        mem_free (o->rpt);
      if (o->exc)
        {
          free_exc (o->exc);
          o->exc = 0;
        }
      mem_free (o);
    }
}

/* Insert a new recursive appointment in the general linked list */
struct recur_apoint *
recur_apoint_new (char *mesg, char *note, long start, long dur, char state,
                  int type, int freq, long until, struct days **except)
{
  struct recur_apoint *o, **i;

  o = mem_malloc (sizeof (struct recur_apoint));
  o->rpt = mem_malloc (sizeof (struct rpt));
  o->mesg = mem_strdup (mesg);
  o->note = (note != 0) ? mem_strdup (note) : 0;
  o->start = start;
  o->state = state;
  o->dur = dur;
  o->rpt->type = type;
  o->rpt->freq = freq;
  o->rpt->until = until;
  o->exc = 0;
  if (except && *except)
    {
      exc_dup (&o->exc, *except);
      free_exc (*except);
      *except = 0;
    }

  pthread_mutex_lock (&(recur_alist_p->mutex));
  i = &recur_alist_p->root;
  for (;;)
    {
      if (*i == 0 || (*i)->start > start)
        {
          o->next = *i;
          *i = o;
          break;
        }
      i = &(*i)->next;
    }
  pthread_mutex_unlock (&(recur_alist_p->mutex));

  return (o);
}

/* Insert a new recursive event in the general linked list */
struct recur_event *
recur_event_new (char *mesg, char *note, long day, int id, int type, int freq,
                 long until, struct days **except)
{
  struct recur_event *o, **i;

  o = mem_malloc (sizeof (struct recur_event));
  o->rpt = mem_malloc (sizeof (struct rpt));
  o->mesg = mem_strdup (mesg);
  o->note = (note != 0) ? mem_strdup (note) : 0;
  o->day = day;
  o->id = id;
  o->rpt->type = type;
  o->rpt->freq = freq;
  o->rpt->until = until;
  o->exc = 0;
  if (except && *except)
    {
      exc_dup (&o->exc, *except);
      free_exc (*except);
      *except = 0;
    }

  i = &recur_elist;
  for (;;)
    {
      if (*i == 0 || (*i)->day > day)
        {
          o->next = *i;
          *i = o;
          break;
        }
      i = &(*i)->next;
    }
  return (o);
}

/*
 * Correspondance between the defines on recursive type,
 * and the letter to be written in file.
 */
char
recur_def2char (enum recur_type define)
{
  char recur_char;

  switch (define)
    {
    case RECUR_DAILY:
      recur_char = 'D';
      break;
    case RECUR_WEEKLY:
      recur_char = 'W';
      break;
    case RECUR_MONTHLY:
      recur_char = 'M';
      break;
    case RECUR_YEARLY:
      recur_char = 'Y';
      break;
    default:
      EXIT (_("unknown repetition type"));
      return 0;
    }

  return (recur_char);
}

/*
 * Correspondance between the letters written in file and the defines
 * concerning the recursive type.
 */
int
recur_char2def (char type)
{
  int recur_def;

  switch (type)
    {
    case 'D':
      recur_def = RECUR_DAILY;
      break;
    case 'W':
      recur_def = RECUR_WEEKLY;
      break;
    case 'M':
      recur_def = RECUR_MONTHLY;
      break;
    case 'Y':
      recur_def = RECUR_YEARLY;
      break;
    default:
      EXIT (_("unknown character"));
      return 0;
    }
  return (recur_def);
}

/* Write days for which recurrent items should not be repeated. */
static void
recur_write_exc (struct days *exc, FILE *f)
{
  struct tm *lt;
  time_t t;
  int st_mon, st_day, st_year;

  while (exc)
    {
      t = exc->st;
      lt = localtime (&t);
      st_mon = lt->tm_mon + 1;
      st_day = lt->tm_mday;
      st_year = lt->tm_year + 1900;
      (void)fprintf (f, " !%02u/%02u/%04u", st_mon, st_day, st_year);
      exc = exc->next;
    }
}

/* Load the recursive appointment description */
struct recur_apoint *
recur_apoint_scan (FILE *f, struct tm start, struct tm end, char type,
                   int freq, struct tm until, char *note, struct days **exc,
                   char state)
{
  struct tm *lt;
  char buf[BUFSIZ], *nl;
  time_t tstart, tend, t, tuntil;

  t = time (NULL);
  lt = localtime (&t);

  /* Read the appointment description */
  (void)fgets (buf, sizeof buf, f);
  nl = strchr (buf, '\n');
  if (nl)
    {
      *nl = '\0';
    }
  start.tm_sec = end.tm_sec = 0;
  start.tm_isdst = end.tm_isdst = -1;
  start.tm_year -= 1900;
  start.tm_mon--;
  end.tm_year -= 1900;
  end.tm_mon--;
  tstart = mktime (&start);
  tend = mktime (&end);

  if (until.tm_year != 0)
    {
      until.tm_hour = 23;
      until.tm_min = 59;
      until.tm_sec = 0;
      until.tm_isdst = -1;
      until.tm_year -= 1900;
      until.tm_mon--;
      tuntil = mktime (&until);
    }
  else
    {
      tuntil = 0;
    }
  EXIT_IF (tstart == -1 || tend == -1 || tstart > tend || tuntil == -1,
           _("date error in appointment"));

  return (recur_apoint_new (buf, note, tstart, tend - tstart, state,
                            recur_char2def (type), freq, tuntil, exc));
}

/* Load the recursive events from file */
struct recur_event *
recur_event_scan (FILE *f, struct tm start, int id, char type, int freq,
                  struct tm until, char *note, struct days **exc)
{
  char buf[BUFSIZ], *nl;
  time_t tstart, tuntil;

  /* Read the event description */
  (void)fgets (buf, sizeof buf, f);
  nl = strchr (buf, '\n');
  if (nl)
    {
      *nl = '\0';
    }
  start.tm_hour = until.tm_hour = 12;
  start.tm_min = until.tm_min = 0;
  start.tm_sec = until.tm_sec = 0;
  start.tm_isdst = until.tm_isdst = -1;
  start.tm_year -= 1900;
  start.tm_mon--;
  if (until.tm_year != 0)
    {
      until.tm_year -= 1900;
      until.tm_mon--;
      tuntil = mktime (&until);
    }
  else
    {
      tuntil = 0;
    }
  tstart = mktime (&start);
  EXIT_IF (tstart == -1 || tuntil == -1,
           _("date error in event"));

  return recur_event_new (buf, note, tstart, id, recur_char2def (type),
                          freq, tuntil, exc);
}

/* Writting of a recursive appointment into file. */
static void
recur_apoint_write (struct recur_apoint *o, FILE *f)
{
  struct tm *lt;
  time_t t;

  t = o->start;
  lt = localtime (&t);
  (void)fprintf (f, "%02u/%02u/%04u @ %02u:%02u",
                 lt->tm_mon + 1, lt->tm_mday, 1900 + lt->tm_year,
                 lt->tm_hour, lt->tm_min);

  t = o->start + o->dur;
  lt = localtime (&t);
  (void)fprintf (f, " -> %02u/%02u/%04u @ %02u:%02u",
                 lt->tm_mon + 1, lt->tm_mday, 1900 + lt->tm_year,
                 lt->tm_hour, lt->tm_min);

  t = o->rpt->until;
  if (t == 0)
    {				/* We have an endless recurrent appointment. */
      (void)fprintf (f, " {%d%c", o->rpt->freq, recur_def2char (o->rpt->type));
    }
  else
    {
      lt = localtime (&t);
      (void)fprintf (f, " {%d%c -> %02u/%02u/%04u",
                     o->rpt->freq, recur_def2char (o->rpt->type),
                     lt->tm_mon + 1, lt->tm_mday, 1900 + lt->tm_year);
    }
  if (o->exc != 0)
    recur_write_exc (o->exc, f);
  (void)fprintf (f, "} ");
  if (o->note != NULL)
    (void)fprintf (f, ">%s ", o->note);
  if (o->state & APOINT_NOTIFY)
    (void)fprintf (f, "!");
  else
    (void)fprintf (f, "|");
  (void)fprintf (f, "%s\n", o->mesg);
}

/* Writting of a recursive event into file. */
static void
recur_event_write (struct recur_event *o, FILE *f)
{
  struct tm *lt;
  time_t t;
  int st_mon, st_day, st_year;
  int end_mon, end_day, end_year;

  t = o->day;
  lt = localtime (&t);
  st_mon = lt->tm_mon + 1;
  st_day = lt->tm_mday;
  st_year = lt->tm_year + 1900;
  t = o->rpt->until;
  if (t == 0)
    {				/* We have an endless recurrent event. */
      (void)fprintf (f, "%02u/%02u/%04u [%d] {%d%c",
                     st_mon, st_day, st_year, o->id, o->rpt->freq,
                     recur_def2char (o->rpt->type));
    }
  else
    {
      lt = localtime (&t);
      end_mon = lt->tm_mon + 1;
      end_day = lt->tm_mday;
      end_year = lt->tm_year + 1900;
      (void)fprintf (f, "%02u/%02u/%04u [%d] {%d%c -> %02u/%02u/%04u",
                     st_mon, st_day, st_year, o->id,
                     o->rpt->freq, recur_def2char (o->rpt->type),
                     end_mon, end_day, end_year);
    }
  if (o->exc != 0)
    recur_write_exc (o->exc, f);
  (void)fprintf (f, "} ");
  if (o->note != NULL)
    (void)fprintf (f, ">%s ", o->note);
  (void)fprintf (f, "%s\n", o->mesg);
}

/* Write recursive items to file. */
void
recur_save_data (FILE *f)
{
  struct recur_event *re;
  struct recur_apoint *ra;

  for (re = recur_elist; re != 0; re = re->next)
    recur_event_write (re, f);

  pthread_mutex_lock (&(recur_alist_p->mutex));
  for (ra = recur_alist_p->root; ra != 0; ra = ra->next)
    recur_apoint_write (ra, f);
  pthread_mutex_unlock (&(recur_alist_p->mutex));
}


/*
 * The two following defines together with the diff_days, diff_weeks,
 * diff_months and diff_years functions were provided by Lukas Fleischer to
 * correct the wrong calculation of recurrent dates after a turn of year.
 */
#define BC(start, end, bs)                                              \
  (((end) - (start) + ((start) % bs) - ((end) % bs)) / bs               \
   + ((((start) % bs) == 0) ? 1 : 0))

#define LEAPCOUNT(start, end)                                           \
  (BC(start, end, 4) - BC(start, end, 100) + BC(start, end, 400))


/* Calculate the difference in days between two dates. */
static long
diff_days (struct tm lt_start, struct tm lt_end)
{
  long diff;

  if (lt_end.tm_year < lt_start.tm_year)
    return 0;

  diff = lt_end.tm_yday - lt_start.tm_yday;

  if (lt_end.tm_year > lt_start.tm_year)
    {
      diff += (lt_end.tm_year - lt_start.tm_year) * YEARINDAYS;
      diff += LEAPCOUNT (lt_start.tm_year + TM_YEAR_BASE,
                         lt_end.tm_year + TM_YEAR_BASE - 1);
    }

  return diff;
}

/* Calculate the difference in weeks between two dates. */
static long
diff_weeks (struct tm lt_start, struct tm lt_end)
{
  return diff_days (lt_start, lt_end) / WEEKINDAYS;
}

/* Calculate the difference in months between two dates. */
static long
diff_months (struct tm lt_start, struct tm lt_end)
{
  long diff;

  if (lt_end.tm_year < lt_start.tm_year)
    return 0;

  diff = lt_end.tm_mon - lt_start.tm_mon;
  diff += (lt_end.tm_year - lt_start.tm_year) * YEARINMONTHS;

  return diff;
}

/* Calculate the difference in years between two dates. */
static long
diff_years (struct tm lt_start, struct tm lt_end)
{
  return lt_end.tm_year - lt_start.tm_year;
}

/*
 * Check if the recurrent item belongs to the selected day,
 * and if yes, return the real start time.
 *
 * This function was improved thanks to Tony's patch.
 * Thanks also to youshe for reporting daylight saving time related problems.
 * And finally thanks to Lukas for providing a patch to correct the wrong
 * calculation of recurrent dates after a turn of years.
 */
unsigned
recur_item_inday (long item_start, struct days *item_exc, int rpt_type,
                  int rpt_freq, long rpt_until, long day_start)
{
  struct date start_date;
  long day_end, diff;
  struct tm lt_item, lt_day;
  struct days *exc;
  time_t t;

  day_end = day_start + DAYINSEC;
  t = day_start;
  lt_day = *localtime (&t);

  for (exc = item_exc; exc != 0; exc = exc->next)
    if (exc->st < day_end && exc->st >= day_start)
      return (0);

  if (rpt_until == 0)		/* we have an endless recurrent item */
    rpt_until = day_end;

  if (item_start > day_end || rpt_until < day_start)
    return (0);

  t = item_start;
  lt_item = *localtime (&t);

  switch (rpt_type)
    {
    case RECUR_DAILY:
      diff = diff_days (lt_item, lt_day);
      if (diff % rpt_freq != 0)
        return (0);
      lt_item.tm_mday = lt_day.tm_mday;
      lt_item.tm_mon = lt_day.tm_mon;
      lt_item.tm_year = lt_day.tm_year;
      break;
    case RECUR_WEEKLY:
      if (lt_item.tm_wday != lt_day.tm_wday)
        return (0);
      else
        {
          diff = diff_weeks (lt_item, lt_day);
          if (diff % rpt_freq != 0)
            return (0);
        }
      lt_item.tm_mday = lt_day.tm_mday;
      lt_item.tm_mon = lt_day.tm_mon;
      lt_item.tm_year = lt_day.tm_year;
      break;
    case RECUR_MONTHLY:
      diff = diff_months (lt_item, lt_day);
      if (diff % rpt_freq != 0)
        return (0);
      lt_item.tm_mon = lt_day.tm_mon;
      lt_item.tm_year = lt_day.tm_year;
      break;
    case RECUR_YEARLY:
      diff = diff_years (lt_item, lt_day);
      if (diff % rpt_freq != 0)
        return (0);
      lt_item.tm_year = lt_day.tm_year;
      break;
    default:
      EXIT (_("unknown item type"));
    }
  start_date.dd = lt_item.tm_mday;
  start_date.mm = lt_item.tm_mon + 1;
  start_date.yyyy = lt_item.tm_year + 1900;
  item_start = date2sec (start_date, lt_item.tm_hour, lt_item.tm_min);

  if (item_start < day_end && item_start >= day_start)
    return (item_start);
  else
    return (0);
}

/*
 * Delete a recurrent event from the list (if delete_whole is not null),
 * or delete only one occurence of the recurrent event.
 */
void
recur_event_erase (long start, unsigned num, unsigned delete_whole,
                   enum eraseflg flag)
{
  unsigned n = 0;
  struct recur_event *i, **iptr;

  iptr = &recur_elist;
  for (i = recur_elist; i != 0; i = i->next)
    {
      if (recur_item_inday (i->day, i->exc, i->rpt->type,
                            i->rpt->freq, i->rpt->until, start))
        {
          if (n == num)
            {
              if (delete_whole)
                {
                  switch (flag)
                    {
                    case ERASE_FORCE_ONLY_NOTE:
                      erase_note (&i->note, flag);
                      break;
                    case ERASE_CUT:
                      recur_event_free_bkp (ERASE_FORCE);
                      recur_event_dup (i, &bkp_cut_recur_event);
                      erase_note (&i->note, ERASE_FORCE_KEEP_NOTE);
                      /* FALLTHROUGH */
                    default:
                      *iptr = i->next;
                      mem_free (i->mesg);
                      if (i->rpt)
                        {
                          mem_free (i->rpt);
                          i->rpt = 0;
                        }
                      if (i->exc)
                        {
                          free_exc (i->exc);
                          i->exc = 0;
                        }
                      if (flag != ERASE_FORCE_KEEP_NOTE && flag != ERASE_CUT)
                        erase_note (&i->note, flag);
                      mem_free (i);
                      break;
                    }
                  return;
                }
              else
                {
                  recur_add_exc (&i->exc, start);
                  return;
                }
            }
          n++;
        }
      iptr = &i->next;
    }
  EXIT (_("event not found"));
  /* NOTREACHED */
}

/*
 * Delete a recurrent appointment from the list (if delete_whole is not null),
 * or delete only one occurence of the recurrent appointment.
 */
void
recur_apoint_erase (long start, unsigned num, unsigned delete_whole,
                    enum eraseflg flag)
{
  unsigned n = 0;
  struct recur_apoint *i, **iptr;
  int need_check_notify = 0;

  pthread_mutex_lock (&(recur_alist_p->mutex));
  iptr = &recur_alist_p->root;
  for (i = recur_alist_p->root; i != 0; i = i->next)
    {
      if (recur_item_inday (i->start, i->exc, i->rpt->type,
                            i->rpt->freq, i->rpt->until, start))
        {
          if (n == num)
            {
              if (notify_bar () && flag != ERASE_FORCE_ONLY_NOTE)
                need_check_notify = notify_same_recur_item (i);
              if (delete_whole)
                {
                  switch (flag)
                    {
                    case ERASE_FORCE_ONLY_NOTE:
                      erase_note (&i->note, flag);
                      break;
                    case ERASE_CUT:
                      recur_apoint_free_bkp (ERASE_FORCE);
                      recur_apoint_dup (i, &bkp_cut_recur_apoint);
                      erase_note (&i->note, ERASE_FORCE_KEEP_NOTE);
                      /* FALLTHROUGH */
                    default:
                      *iptr = i->next;
                      mem_free (i->mesg);
                      if (i->rpt)
                        {
                          mem_free (i->rpt);
                          i->rpt = 0;
                        }
                      if (i->exc)
                        {
                          free_exc (i->exc);
                          i->exc = 0;
                        }
                      if (flag != ERASE_FORCE_KEEP_NOTE && flag != ERASE_CUT)
                        erase_note (&i->note, flag);
                      mem_free (i);
                      pthread_mutex_unlock (&(recur_alist_p->mutex));
                      if (need_check_notify)
                        notify_check_next_app ();
                      break;
                    }
                  return;
                }
              else
                {
                  recur_add_exc (&i->exc, start);
                  pthread_mutex_unlock (&(recur_alist_p->mutex));
                  if (need_check_notify)
                    notify_check_next_app ();
                  return;
                }
            }
          n++;
        }
      iptr = &i->next;
    }
  EXIT (_("appointment not found"));
  /* NOTREACHED */
}

/*
 * Ask user for repetition characteristics:
 * 	o repetition type: daily, weekly, monthly, yearly
 *	o repetition frequence: every X days, weeks, ...
 *	o repetition end date
 * and then delete the selected item to recreate it as a recurrent one
 */
void
recur_repeat_item (struct conf *conf)
{
  struct tm *lt;
  time_t t;
  int ch = 0;
  int date_entered = 0;
  int year = 0, month = 0, day = 0;
  struct date until_date;
  char outstr[BUFSIZ];
  char user_input[BUFSIZ] = "";
  char *mesg_type_1 =
    _("Enter the repetition type: (D)aily, (W)eekly, (M)onthly, (Y)early");
  char *mesg_type_2 = _("[D/W/M/Y] ");
  char *mesg_freq_1 = _("Enter the repetition frequence:");
  char *mesg_wrong_freq = _("The frequence you entered is not valid.");
  char *mesg_until_1 =
    _("Enter the ending date: [%s] or '0' for an endless repetition");
  char *mesg_wrong_1 = _("The entered date is not valid.");
  char *mesg_wrong_2 =
    _("Possible formats are [%s] or '0' for an endless repetition");
  char *wrong_type_1 = _("This item is already a repeated one.");
  char *wrong_type_2 = _("Press [ENTER] to continue.");
  char *mesg_older =
    _("Sorry, the date you entered is older than the item start time.");
  int type = 0, freq = 0;
  int item_nb;
  struct day_item *p;
  struct recur_apoint *ra;
  struct recur_event *re;
  long until, date;

  item_nb = apoint_hilt ();
  p = day_get_item (item_nb);
  if (p->type != APPT && p->type != EVNT)
    {
      status_mesg (wrong_type_1, wrong_type_2);
      ch = wgetch (win[STA].p);
      return;
    }

  while ((ch != 'D') && (ch != 'W') && (ch != 'M')
         && (ch != 'Y') && (ch != ESCAPE))
    {
      status_mesg (mesg_type_1, mesg_type_2);
      ch = wgetch (win[STA].p);
      ch = toupper (ch);
    }
  if (ch == ESCAPE)
    {
      return;
    }
  else
    {
      type = recur_char2def (ch);
      ch = 0;
    }

  while (freq == 0)
    {
      status_mesg (mesg_freq_1, "");
      if (getstring (win[STA].p, user_input, BUFSIZ, 0, 1) == GETSTRING_VALID)
        {
          freq = atoi (user_input);
          if (freq == 0)
            {
              status_mesg (mesg_wrong_freq, wrong_type_2);
              (void)wgetch (win[STA].p);
            }
          user_input[0] = '\0';
        }
      else
        return;
    }

  while (!date_entered)
    {
      (void)snprintf (outstr, BUFSIZ, mesg_until_1,
                      DATEFMT_DESC (conf->input_datefmt));
      status_mesg (_(outstr), "");
      if (getstring (win[STA].p, user_input, BUFSIZ, 0, 1) == GETSTRING_VALID)
        {
          if (strlen (user_input) == 1 && strncmp (user_input, "0", 1) == 0)
            {
              until = 0;
              date_entered = 1;
            }
          else
            {
              if (parse_date (user_input, conf->input_datefmt,
                              &year, &month, &day, calendar_get_slctd_day ()))
                {
                  t = p->start;
                  lt = localtime (&t);
                  until_date.dd = day;
                  until_date.mm = month;
                  until_date.yyyy = year;
                  until = date2sec (until_date, lt->tm_hour, lt->tm_min);
                  if (until < p->start)
                    {
                      status_mesg (mesg_older, wrong_type_2);
                      (void)wgetch (win[STA].p);
                      date_entered = 0;
                    }
                  else
                    {
                      date_entered = 1;
                    }
                }
              else
                {
                  (void)snprintf (outstr, BUFSIZ, mesg_wrong_2,
                                  DATEFMT_DESC (conf->input_datefmt));
                  status_mesg (mesg_wrong_1, _(outstr));
                  (void)wgetch (win[STA].p);
                  date_entered = 0;
                }
            }
        }
      else
        return;
    }

  date = calendar_get_slctd_day_sec ();
  if (p->type == EVNT)
    {
      re = recur_event_new (p->mesg, p->note, p->start, p->evnt_id,
                            type, freq, until, NULL);
    }
  else if (p->type == APPT)
    {
      ra = recur_apoint_new (p->mesg, p->note, p->start, p->appt_dur,
                             p->state, type, freq, until, NULL);
      if (notify_bar ())
        notify_check_repeated (ra);
    }
  else
    {
      EXIT (_("wrong item type"));
      /* NOTREACHED */
    }
  day_erase_item (date, item_nb, ERASE_FORCE_KEEP_NOTE);
}

/*
 * Read days for which recurrent items must not be repeated
 * (such days are called exceptions).
 */
struct days *
recur_exc_scan (FILE *data_file)
{
  int c = 0;
  struct tm day;
  struct days *exc_head, *exc;

  exc_head = 0;
  while ((c = getc (data_file)) == '!')
    {
      (void)ungetc (c, data_file);
      if (fscanf (data_file, "!%u / %u / %u ",
                  &day.tm_mon, &day.tm_mday, &day.tm_year) != 3)
        {
          EXIT (_("syntax error in item date"));
        }
      day.tm_hour = 12;
      day.tm_min = day.tm_sec = 0;
      day.tm_isdst = -1;
      day.tm_year -= 1900;
      day.tm_mon--;
      exc = mem_malloc (sizeof (struct days));
      exc->st = mktime (&day);
      exc->next = exc_head;
      exc_head = exc;
    }
  return exc_head;
}

/*
 * Look in the appointment list if we have an item which starts before the item
 * stored in the notify_app structure (which is the next item to be notified).
 */
struct notify_app *
recur_apoint_check_next (struct notify_app *app, long start, long day)
{
  struct recur_apoint *i;
  long real_recur_start_time;

  pthread_mutex_lock (&(recur_alist_p->mutex));
  for (i = recur_alist_p->root; i != 0; i = i->next)
    {
      if (i->start > app->time)
        {
          pthread_mutex_unlock (&(recur_alist_p->mutex));
          return (app);
        }
      else
        {
          real_recur_start_time =
            recur_item_inday (i->start, i->exc, i->rpt->type, i->rpt->freq,
                              i->rpt->until, day);
          if (real_recur_start_time > start)
            {
              app->time = real_recur_start_time;
              app->txt = mem_strdup (i->mesg);
              app->state = i->state;
              app->got_app = 1;
            }
        }
    }
  pthread_mutex_unlock (&(recur_alist_p->mutex));

  return (app);
}

/* Returns a structure containing the selected recurrent appointment. */
struct recur_apoint *
recur_get_apoint (long date, int num)
{
  struct recur_apoint *o;
  int n = 0;

  pthread_mutex_lock (&(recur_alist_p->mutex));
  for (o = recur_alist_p->root; o != 0; o = o->next)
    {
      if (recur_item_inday (o->start, o->exc, o->rpt->type,
                            o->rpt->freq, o->rpt->until, date))
        {
          if (n == num)
            {
              pthread_mutex_unlock (&(recur_alist_p->mutex));
              return (o);
            }
          n++;
        }
    }
  EXIT (_("item not found"));
  return 0;
  /* NOTREACHED */
}

/* Returns a structure containing the selected recurrent event. */
struct recur_event *
recur_get_event (long date, int num)
{
  struct recur_event *o;
  int n = 0;

  for (o = recur_elist; o != 0; o = o->next)
    {
      if (recur_item_inday (o->day, o->exc, o->rpt->type,
                            o->rpt->freq, o->rpt->until, date))
        {
          if (n == num)
            {
              return (o);
            }
          n++;
        }
    }
  EXIT (_("item not found"));
  return 0;
  /* NOTREACHED */
}

/* Switch recurrent item notification state. */
void
recur_apoint_switch_notify (long date, int recur_nb)
{
  int n, need_chk_notify;
  struct recur_apoint *o;

  n = 0;
  need_chk_notify = 0;

  pthread_mutex_lock (&(recur_alist_p->mutex));
  for (o = recur_alist_p->root; o != 0; o = o->next)
    {
      if (recur_item_inday (o->start, o->exc, o->rpt->type,
                            o->rpt->freq, o->rpt->until, date))
        {
          if (n == recur_nb)
            {
              o->state ^= APOINT_NOTIFY;

              if (notify_bar ())
                notify_check_repeated (o);

              pthread_mutex_unlock (&(recur_alist_p->mutex));
              if (need_chk_notify)
                notify_check_next_app ();
              return;
            }
          n++;
        }
    }
  EXIT (_("item not found"));
  /* NOTREACHED */
}

void
recur_event_paste_item (void)
{
  long new_start, time_shift;

  new_start = date2sec (*calendar_get_slctd_day (), 12, 0);
  time_shift = new_start - bkp_cut_recur_event.day;

  bkp_cut_recur_event.day += time_shift;
  if (bkp_cut_recur_event.rpt->until != 0)
    bkp_cut_recur_event.rpt->until += time_shift;
  if (bkp_cut_recur_event.exc)
    {
      struct days *exc;

      for (exc = bkp_cut_recur_event.exc; exc != 0; exc = exc->next)
        exc->st += time_shift;
    }

  (void)recur_event_new (bkp_cut_recur_event.mesg, bkp_cut_recur_event.note,
                         bkp_cut_recur_event.day, bkp_cut_recur_event.id,
                         bkp_cut_recur_event.rpt->type,
                         bkp_cut_recur_event.rpt->freq,
                         bkp_cut_recur_event.rpt->until,
                         &bkp_cut_recur_event.exc);
  recur_event_free_bkp (ERASE_FORCE_KEEP_NOTE);
}

void
recur_apoint_paste_item (void)
{
  long new_start, time_shift;

  new_start = date2sec (*calendar_get_slctd_day (),
                        get_item_hour (bkp_cut_recur_apoint.start),
                        get_item_min (bkp_cut_recur_apoint.start));
  time_shift = new_start - bkp_cut_recur_apoint.start;

  bkp_cut_recur_apoint.start += time_shift;
  if (bkp_cut_recur_apoint.rpt->until != 0)
    bkp_cut_recur_apoint.rpt->until += time_shift;
  if (bkp_cut_recur_apoint.exc)
    {
      struct days *exc;

      for (exc = bkp_cut_recur_apoint.exc; exc != 0; exc = exc->next)
        exc->st += time_shift;
    }

  (void)recur_apoint_new (bkp_cut_recur_apoint.mesg, bkp_cut_recur_apoint.note,
                          bkp_cut_recur_apoint.start, bkp_cut_recur_apoint.dur,
                          bkp_cut_recur_apoint.state,
                          bkp_cut_recur_apoint.rpt->type,
                          bkp_cut_recur_apoint.rpt->freq,
                          bkp_cut_recur_apoint.rpt->until,
                          &bkp_cut_recur_apoint.exc);
  recur_apoint_free_bkp (ERASE_FORCE_KEEP_NOTE);
}