File: xcal_edit.c

package info (click to toggle)
xcal 4.1-19
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, lenny, sid, squeeze, stretch, wheezy
  • size: 780 kB
  • ctags: 3,851
  • sloc: ansic: 5,576; makefile: 4,265; sh: 573
file content (1241 lines) | stat: -rw-r--r-- 27,637 bytes parent folder | download | duplicates (3)
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
#ifndef lint
static char    *sccsid = "@(#)xcal_edit.c	3.40 (Hillside Systems) 9/15/95";
static char    *copyright = "@(#)Copyright 1989,1990,1993 Peter Collinson, Hillside Systems";
#endif				/* lint */
/***

* module name:
	xcal_edit.c
* function:
	Deal with editable days
	This is derived from xcalendar's view of how to store things
* history:
	Written November 1989
	Peter Collinson
	Hillside Systems
* (C) Copyright: 1989 Hillside Systems/Peter Collinson

	For full permissions and copyright notice - see xcal.c
***/
#include <stdio.h>
#include <ctype.h>
#include <pwd.h>
#include <X11/Intrinsic.h>
#include <X11/Xos.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <X11/Xaw/AsciiText.h>
#include <X11/Xaw/Text.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/Label.h>
#include <X11/Xaw/Paned.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Dialog.h>
#include "xcal.h"
#include <sys/stat.h>
#include <dirent.h>

#define argLD(N,V) { XtSetArg(args[nargs], N, V); nargs++; }

typedef struct editline {
	struct editline *ed_next;	/* Pointer to next */
	struct meWrap  *ed_meWrap;	/* Pointer to head of the chain */
	Cardinal        ed_day;		/* What day we are */
	Widget          ed_popup;	/* widget of editor popup */
	Widget          ed_quit;	/* widget of quit button */
	Widget          ed_save;	/* widget of save button */
	Widget          ed_text;	/* the text area */
	Cardinal        ed_size;	/* size of the buffer */
	char           *ed_data;	/* pointer to malloc'ed data buffer */
} EditLine;

typedef struct meWrap {
	struct meWrap  *mw_next;
	String          mw_dir;		/* name of the directory */
	Boolean         mw_useTopDir;	/* have found some data in the top dir */
	MonthEntry      mw_me;		/* what the external world sees */
	Instance       *mw_list;	/* list of toplevel widget for the */
					/* current set of displayed strips */
	EditLine       *mw_ed;		/* data being edited */
} MeWrap;

#define	mw_year	mw_me.me_year
#define mw_month mw_me.me_month
#define mw_have	mw_me.me_have

#define	StripType(mw)	(mw->mw_me.me_type)

static MeWrap  *WrapBase;		/* base of the list */
static MeWrap  *WrapEnd;		/* the last one in the list */

char           *MapStem;		/* pointer to the string which is */
					/* where the map data is stored */

Boolean         FoundCalendarDir;	/* whether the Calendar directory */
					/* exists */

static XtCallbackRec callbacks[] = {
	{NULL, NULL},
	{NULL, NULL}
};
#define ClearCallbacks() memset((caddr_t)callbacks, '\0', sizeof (callbacks))

/*
 * Routine specs
 */	
static void	MakeMapStem();
static MeWrap  *NewMeWrap();
static MeWrap  *MeWrapSearch();
static void     DeRegisterMonth();
static Boolean  WriteCalendarFile();
static Boolean  WriteWeeklyFile();
static void     DeleteCalendarFile();
static int	EditCheck();
static void     SetAllButtons();
static void     TextChanged();
static void	UpdateDayDetails();
static void     FinishEditing();
static void     CleanEditPanel();
static void     CheckExit();
static void     CheckDia();
static void     YesCheck();
static void     NoCheck();
static int      DayMatch();
static void	SaveEdits();
void            StartDayEditor();
void            Fatal();



/*
 * Fire up the month entry environment called once
 */
void
InitMonthEntries()
{
	if (MapStem == NULL)
		MakeMapStem();

	if (access(MapStem, F_OK) < 0)
		FoundCalendarDir = False;
	else {
		FoundCalendarDir = True;
		/*
		 * If we can see the directory, then lurch into it
		 */
		if (chdir(MapStem) < 0)
			Fatal("Cannot change into %s", MapStem);
	}
}

/*
 *	Find the base of the Calendar structure
 */
static void
MakeMapStem()
{	
	char            buf[BUFSIZ];
	struct passwd	*pw;

	if (appResources.otheruser) {
		/* someone else */
		pw = getpwnam(appResources.otheruser);
		if (pw == NULL)
			Fatal("Cannot get password details for %s\n", appResources.otheruser);
	} else {
		pw = getpwuid(getuid());
		if (pw == NULL)
			Fatal("Cannot get password details for YOU!\n");
	}
	(void) sprintf(buf, "%s/%s", pw->pw_dir, appResources.directory);
	MapStem = XtNewString(buf);
}

/*
 *	Get the entry for a specific month
 *
 *	xcalendar files are all
 *	xc<d><Mon><Year>
 *	or
 *	xc<dd><Mon><Year>
 *
 *	where d or dd is the day (%02d would have been better)
 *	<Mon> is a capitalised first three letters of the
 *		English month name. If you need compatibility
 *		don't redefine the short names in the resources
 *		for this program.
 *	<Year> is the full numeric year.
 *
 *	We will follow this BUT we will also make this program
 *	create subdirectories for new years
 *		xy<Year>
 *	to speed up file access
 */
MonthEntry *
GetMonthEntry(yr, mo)
	Cardinal        yr;
	Cardinal        mo;
{
	MeWrap         *mw;
	char           *dir;
	DIR            *dirp;
	struct dirent  *dp;
	int             da;
	Boolean         inSubDir;
	char            yearbuf[5];
	char            monthbuf[4];

	if ((mw = MeWrapSearch(yr, mo)) == NULL)
		mw = NewMeWrap(yr, mo);
	mw->mw_me.me_type = ME_MONTHLY;
	if (!FoundCalendarDir)
		return (&mw->mw_me);

	/*
	 * need this for string match
	 */
	(void) sprintf(yearbuf, "%d", yr);
	(void) sprintf(monthbuf, "%s", appResources.smon[mo]);

	/* we are in the directory */
	/* so let's lookee here for the any files of interest */

	dir = ".";
	inSubDir = False;
	if (mw->mw_dir) {
		dir = mw->mw_dir;
		inSubDir = True;
	}
	if ((dirp = opendir(dir)) == NULL)
		Fatal("Cannot open directory: %s", MapStem);

	for (da = 1; da < 32; da++)
		if (mw->mw_have[da]) {
			XtFree(mw->mw_have[da]);
			mw->mw_have[da] = NULL;
		}
	while ((dp = readdir(dirp)) != NULL) {

		switch (strlen(dp->d_name)) {

		case 6:	/* xy<Year> ?? */
			if (dp->d_name[0] == 'x' &&
			    dp->d_name[1] == 'y' &&
			    dp->d_name[2] == yearbuf[0] &&
			    dp->d_name[3] == yearbuf[1] &&
			    dp->d_name[4] == yearbuf[2] &&
			    dp->d_name[5] == yearbuf[3] &&
			    appResources.calCompat == False) {
				/*
				 * well - we're wasting
				 * our time at the top
				 * level - rejig things
			         * to work in the
				 * subdirectory
				 */
				inSubDir = True;
				mw->mw_useTopDir = False;
				mw->mw_dir = XtNewString(dp->d_name);
				closedir(dirp);
				if ((dirp = opendir(mw->mw_dir)) == NULL)
					Fatal("Cannot open directory %s/%s", MapStem, mw->mw_dir);

			}
			break;
		case 10:	/* xc<d><Mon><Year> ?? */
			if (dp->d_name[0] == 'x' &&
			    dp->d_name[1] == 'c' &&
			    isdigit(dp->d_name[2]) &&
			    dp->d_name[3] == monthbuf[0] &&
			    dp->d_name[4] == monthbuf[1] &&
			    dp->d_name[5] == monthbuf[2] &&
			    dp->d_name[6] == yearbuf[0] &&
			    dp->d_name[7] == yearbuf[1] &&
			    dp->d_name[8] == yearbuf[2] &&
			    dp->d_name[9] == yearbuf[3]) {
				da = dp->d_name[2] - '0';
				mw->mw_have[da] = ReadCalendarFile(mw->mw_dir, dp->d_name);
				if (inSubDir == False)
					mw->mw_useTopDir = True;
			}
			break;
		case 11:	/* xc<dd><Mon><Year> ?? */
			if (dp->d_name[0] == 'x' &&
			    dp->d_name[1] == 'c' &&
			    isdigit(dp->d_name[2]) &&
			    isdigit(dp->d_name[3]) &&
			    dp->d_name[4] == monthbuf[0] &&
			    dp->d_name[5] == monthbuf[1] &&
			    dp->d_name[6] == monthbuf[2] &&
			    dp->d_name[7] == yearbuf[0] &&
			    dp->d_name[8] == yearbuf[1] &&
			    dp->d_name[9] == yearbuf[2] &&
			    dp->d_name[10] == yearbuf[3]) {
				da = (dp->d_name[2] - '0') * 10 + (dp->d_name[3] - '0');
				mw->mw_have[da] = ReadCalendarFile(mw->mw_dir, dp->d_name);
				if (inSubDir == False)
					mw->mw_useTopDir = True;
			}
			break;
		}
	}
	closedir(dirp);
	return (&mw->mw_me);
}

/*
 *	Get the entry for the weekly strip
 *	Files are xw<Day> in the Calendar directory
 */
MonthEntry *
GetWeeklyEntry()
{
	MeWrap         *mw;
	int             da;
	DIR            *dirp;
	struct dirent  *dp;

	if ((mw = MeWrapSearch(0, 0)) == NULL)
		mw = NewMeWrap(0, 0);
	mw->mw_me.me_type = ME_WEEKLY;

	if (!FoundCalendarDir)
		return (&mw->mw_me);

	if ((dirp = opendir(".")) == NULL)
		Fatal("Cannot open directory: %s", MapStem);

	for (da = 0; da < 7; da++)
		if (mw->mw_have[da]) {
			XtFree(mw->mw_have[da]);
			mw->mw_have[da] = NULL;
		}
	while ((dp = readdir(dirp)) != NULL) {
		if (dp->d_name[0] == 'x' &&
		    dp->d_name[1] == 'w' &&
		    ((da = DayMatch(&dp->d_name[2])) != -1)) {
			mw->mw_have[da] = ReadCalendarFile(NULL, dp->d_name);
		}
	}
	closedir(dirp);
	return (&mw->mw_me);
}

/*
 * Look for a short name match with a day
 */
static int
DayMatch(day)
	String          day;
{
	register        i;

	for (i = 0; i < 7; i++)
		if (strcmp(day, appResources.sday[i]) == 0)
			return (i);
	return (-1);
}

/*
 * create a new MapWrap area
 */
static MeWrap  *
NewMeWrap(yr, mo)
	Cardinal        yr;
	Cardinal        mo;
{
	register MeWrap *mw;
	static MeWrap	zerow;
	
	mw = (MeWrap *) XtMalloc(sizeof(MeWrap));
	*mw = zerow;
	if (WrapEnd)
		WrapEnd->mw_next = mw;
	WrapEnd = mw;
	if (WrapBase == NULL)
		WrapBase = mw;
	mw->mw_year = yr;
	mw->mw_month = mo;
	mw->mw_useTopDir = False;
	return (mw);
}

/*
 * Search the MapWrap list for a year
 */
static MeWrap  *
MeWrapSearch(yr, mo)
	Cardinal        yr;
	Cardinal        mo;
{
	register MeWrap *mw;

	if (WrapBase)
		for (mw = WrapBase; mw; mw = mw->mw_next)
			if (yr == mw->mw_year && mo == mw->mw_month)
				return (mw);
	return (NULL);
}

/*
 * Register an instance of a month Return a pointer to an instance structure
 * so it can be filled in by the caller
 */
Instance       *
RegisterMonth(yr, mo, w)
	Cardinal        yr;
	Cardinal        mo;
	Widget          w;
{
	register MeWrap *mw;
	register Instance *ins;

	if ((mw = MeWrapSearch(yr, mo)) == NULL)
		mw = NewMeWrap(yr, mo);

	ins = (Instance *) XtMalloc(sizeof(Instance));
	ins->i_next = mw->mw_list;
	mw->mw_list = ins;
	ins->i_w = w;

	callbacks[0].callback = DeRegisterMonth;
#ifdef LONG_IS_32_BITS
	callbacks[0].closure = (caddr_t) DatePack(0, 0, mo, yr);
#else
	callbacks[0].closure = (caddr_t) DatePack(mo, yr);
#endif

	XtAddCallbacks(w, XtNdestroyCallback, callbacks);
	return (ins);
}

/*
 * Return the head of an instance list - given a date
 */
Instance       *
FindInstanceList(da)
	Date           *da;
{
	register MeWrap *mw;

	if ((mw = MeWrapSearch(da->year, da->month)) == NULL)
		return (NULL);
	return (mw->mw_list);
}

/*
 * Delete an instance
 */
/* ARGSUSED */
static void
DeRegisterMonth(w, closure, call_data)
	Widget          w;
	caddr_t         closure;
	caddr_t         call_data;
{
	Cardinal        yr, mo;
	register Instance *ins, *inlast;
	register MeWrap *mw;

	yr = YrUnpack((Cardinal) closure);
	mo = MoUnpack((Cardinal) closure);

	if ((mw = MeWrapSearch(yr, mo)) == NULL)
		return;
	for (ins = mw->mw_list, inlast = NULL;
	     ins;
	     inlast = ins, ins = ins->i_next) {
		if (ins->i_w == w) {
			if (inlast)
				inlast->i_next = ins->i_next;
			else
				mw->mw_list = ins->i_next;
			XtFree((char *) ins);
			return;
		}
		inlast = ins;
	}
}


/*
 * Read a calendar file into memory into a string
 * if the file is zero length then unlink and return NULL
 */
String
ReadCalendarFile(dir, file)
	String          dir;
	String          file;
{
	char            fname[256];
	int             fd;
	String          destb;
	struct stat     fsb;

	if (dir) {
		(void) sprintf(fname, "%s/%s", dir, file);
		file = fname;
	}
	if ((fd = open(file, 0)) < 0) {
		if (MyCalendar)
			Fatal("Cannot open: %s for reading", file);
		else
			return(XtNewString(appResources.private));
	}	
	if (fstat(fd, &fsb) < 0)
		Fatal("Cannot fstat %s", file);

	if (fsb.st_size == 0) {
		if (MyCalendar)
			(void) unlink(file);
		close(fd);
		return (NULL);
	}
	destb = (String) XtMalloc(fsb.st_size + 1);

	if (read(fd, (String) destb, fsb.st_size) != fsb.st_size)
		Fatal("Read error on %s", file);

	close(fd);

	destb[fsb.st_size] = '\0';

	return (destb);
}

/*
 * Check to see if we should create the top directory
 */
Boolean
NeedTop()
{
	if (!FoundCalendarDir) {
		if (mkdir(MapStem, 0777) == -1) {
			XBell(XtDisplay(toplevel), 0);
			fprintf(stderr, "xcal: Could not create: %s directory.\n", MapStem);
			perror("xcal: mkdir");
			fflush(stderr);
			return (False);
		}
		if (chdir(MapStem) < 0) {
			XBell(XtDisplay(toplevel), 0);
			fprintf(stderr, "xcal: Could not chdir into %s.\n", MapStem);
			perror("xcal: chdir");
			fflush(stderr);
			return (False);
		}
		FoundCalendarDir = True;
	}
	return (True);
}

/*
 * Write a calendar file creating any directories which are needed
 * Return True if OK
 */
static Boolean
WriteCalendarFile(mw, day, contents)
	register MeWrap *mw;
	Cardinal        day;
	char           *contents;
{
	int             fd;
	Cardinal        len;
	char            fname[256];
	char            cname[16];

	len = strlen(contents);
	if (len == 0) {
		DeleteCalendarFile(mw, day);
		return (True);
	}
	if (!NeedTop())
		return (False);

	/*
	 * So that looks OK 
         * We can now create the output file. However, we
	 * would like to put any new data into subdirectories named for the
	 * year unless we are compatible with xcalendar
	 */
	fname[0] = '\0';
	if (appResources.calCompat == False && mw->mw_useTopDir == False) {
		/*
		 * we have no data in the top directory
		 * so let's create the directory name
		 */
		(void) sprintf(fname, "xy%d", mw->mw_year);

		if (access(fname, F_OK) < 0) {
			if (mkdir(fname, 0777) < 0) {
				XBell(XtDisplay(toplevel), 0);
				fprintf(stderr, "xcal: Could not create: %s/%s directory.\n", MapStem, fname);
				perror("xcal: mkdir ");
				fflush(stderr);
				return (False);
			}
		}
		strcat(fname, "/");
	}
	/*
	 * Whew - it looks as if we can now write the file
	 */
	(void) sprintf(cname, "xc%d%s%d", day,
		       appResources.smon[mw->mw_month],
		       mw->mw_year);

	strcat(fname, cname);

	if ((fd = open(fname, O_WRONLY | O_TRUNC | O_CREAT, 0666)) < 0) {
		XBell(XtDisplay(toplevel), 0);
		fprintf(stderr, "xcal: Could not open %s/%s for writing.\n", MapStem, fname);
		perror("xcal: open");
		fflush(stderr);
		return (False);
	}
	if (write(fd, contents, len) != len) {
		XBell(XtDisplay(toplevel), 0);
		fprintf(stderr, "xcal: Write error %s/%s file.\n", MapStem, fname);
		perror("xcal: write");
		fflush(stderr);
		close(fd);
		return (False);
	}
	close(fd);
	/*
	 * tickle the alarm system if we have altered `today'
	 */
	if (today.day == day && today.month == mw->mw_month &&
	    today.year == mw->mw_year)
		AlarmFilePoll(NULL);
	return (True);
}

static void
DeleteCalendarFile(mw, day)
	register MeWrap *mw;
	Cardinal        day;
{
	char            fname[256];
	char            cname[16];

	fname[0] = '\0';

	if (mw->mw_useTopDir == False) {
		/*
		 * we have no data in the top directory 
		 * so let's create the directory name
		 */
		(void) sprintf(fname, "xy%d", mw->mw_year);

		if (access(fname, F_OK) < 0)
			return;
		strcat(fname, "/");
	}
	(void) sprintf(cname, "xc%d%s%d", day,
		       appResources.smon[mw->mw_month],
		       mw->mw_year);

	strcat(fname, cname);

	unlink(fname);

	/*
	 * tickle the alarm system if we have altered `today'
	 */
	if (today.day == day && today.month == mw->mw_month &&
	    today.year == mw->mw_year)
		AlarmFilePoll(NULL);
}

/*
 * Write daily file out
 */
static          Boolean
WriteWeeklyFile(mw, day, contents)
	register MeWrap *mw;
	Cardinal        day;
	char           *contents;
{
	int             fd;
	Cardinal        len;
	char           *fname;

	fname = MakeWeeklyName(day);

	len = strlen(contents);
	if (len == 0)
		(void) unlink(fname);
	else {
		if ((fd = open(fname, O_WRONLY | O_TRUNC | O_CREAT, 0666)) < 0) {
			XBell(XtDisplay(toplevel), 0);
			fprintf(stderr, "xcal: Could not open %s/%s for writing.\n", MapStem, fname);
			perror("xcal: open");
			fflush(stderr);
			return (False);
		}
		if (write(fd, contents, len) != len) {
			XBell(XtDisplay(toplevel), 0);
			fprintf(stderr, "xcal: Write error %s/%s file.\n", MapStem, fname);
			perror("xcal: write");
			fflush(stderr);
			close(fd);
			return (False);
		}
		close(fd);
	}
	/*
	 * tickle the alarm system if we have altered `today'
	 */
	if (today.wday == day)
		AlarmFilePoll(NULL);

	return (True);
}

/*
 * Create a standard weekly file name
 */
String
MakeWeeklyName(day)
	Cardinal        day;
{
	static char     fname[16];

	(void) sprintf(fname, "xw%s", appResources.sday[day]);
	return (fname);
}

/*
 * Get the contents of the current Weekly file if any
 */
String
GetWeeklyFile(day)
	Cardinal        day;
{
	char           *fname;

	if (FoundCalendarDir == False)
		return (NULL);

	fname = MakeWeeklyName(day);

	if (access(fname, F_OK) < 0)
		return (NULL);

	return (ReadCalendarFile(NULL, fname));
}

/*
 * Start up an editor window from the callback
 * Pass the calling widget so we can change its contents after the edit
 */
/* ARGSUSED */
void
StartEditing(w, da, but)
	Widget          w;
	Date           *da;
	Widget		but;
{
	register MeWrap *mw;
	register EditLine *ed;
	static EditLine	zeroe;
	
	if ((mw = MeWrapSearch(da->year, da->month)) == NULL)
		mw = NewMeWrap(da->year, da->month);	/* shouldn`t happen */
	/*
	 * see if we are editing this day
	 */
	if (EditCheck(w, mw, da))
		return;
	/*
	 * Things are looking OK
	 * Create a new editing record
	 */
	ed = (EditLine *) XtMalloc(sizeof(EditLine));
	*ed = zeroe;
	ed->ed_day = da->day;
	ed->ed_meWrap = mw;	/* help for unlinking */
	/*
	 * Do we have a string now
	 */
	if (mw->mw_have[da->day]) {
		ed->ed_size = appResources.textbufsz + strlen(mw->mw_have[da->day]) + 1;
		ed->ed_data = XtMalloc(ed->ed_size);
		strcpy(ed->ed_data, mw->mw_have[da->day]);
	} else {
		ed->ed_data = XtMalloc(ed->ed_size = appResources.textbufsz);
		*ed->ed_data = '\0';
	}
	/*
	 * put the record into the list
	 */
	ed->ed_next = mw->mw_ed;
	mw->mw_ed = ed;
	/*
	 * We fiddle with the source widget too 
	 * Desensitise visible source widgets
	 * If the user starts up another strip for this month then
	 * the NoEditIsPossible() code above copes
	 */
	SetAllButtons(mw->mw_list, da->day, False);
	/*
	 * Now we should start up the edit window for this month
	 */
	StartDayEditor(mw, da, but);
}

/*
 *	See if we are editing a day already
 *	complain and return 1 if we are
 *	return 0 if not
 */
static int
EditCheck(w, mw, da)
	Widget		w;
	MeWrap		*mw;
	Date		*da;
{
	register EditLine *ed;

	/*
	 * see if we are already editing this day
	 */
	for (ed = mw->mw_ed; ed; ed = ed->ed_next) {
		if (ed->ed_day == da->day) {	/* we are! */
			/* Complain via a popup */
			switch (StripType(mw)) {
			case ME_MONTHLY:
				NoEditIsPossible(w, da);
				break;
			case ME_WEEKLY:
				NoDayEditIsPossible(w, da);
				break;
			}
			return 1;
		}
	}
	return 0;
}

/*
 * Set all the relevant buttons in a widget list to off or on
 */
static void
SetAllButtons(ins, day, val)
	Instance       *ins;
	Cardinal        day;
	Boolean         val;
{
	for (; ins; ins = ins->i_next)
		XtSetSensitive(ins->i_day_info[day], val);
}

/*
 * Start up a day editor Modelled on xcalendar.c
 */
void
StartDayEditor(mw, da, but)
	register MeWrap *mw;
	register Date  *da;
	Widget but;
{
	register EditLine *ed = mw->mw_ed;	/* top of the list is ours */
	Widget          lw, et;
	Widget          frame;
	Arg             args[10];
	Cardinal        nargs;
	char            buf[BUFSIZ];
	void            EditHelp();
	
	ed->ed_popup = XtCreatePopupShell("edit", topLevelShellWidgetClass, toplevel, NULL, 0);

	if (but && XtIsSubclass(but, commandWidgetClass))
		ButtonOff(but, ed->ed_popup);

	/*
	 * Create the title line - which is a form containing buttons and a
	 * date label
	 */
	et = XtCreateManagedWidget("panel", panedWidgetClass, ed->ed_popup, NULL, 0);

	nargs = 0;
	argLD(XtNshowGrip, False);
	argLD(XtNskipAdjust, True);
	argLD(XtNdefaultDistance, 1);
	frame = XtCreateManagedWidget("title", formWidgetClass, et, args, nargs);
	/*
	 * Take label "quit" from resources
	 */
	callbacks[0].callback = FinishEditing;
	callbacks[0].closure = (caddr_t) ed;
	nargs = 0;
	argLD(XtNcallback, callbacks);
	argLD(XtNfromHoriz, NULL);
	argLD(XtNleft, XtChainLeft);
	argLD(XtNright, XtChainLeft);
	lw = ed->ed_quit = XtCreateManagedWidget("quit", commandWidgetClass, frame, args, nargs);

	/*
	 * Take label "save" from resources
	 */
	if (MyCalendar) {
		callbacks[0].callback = SaveEdits;
		callbacks[0].closure = (caddr_t) ed;
		nargs = 0;
		argLD(XtNcallback, callbacks);
		argLD(XtNfromHoriz, ed->ed_quit);
		argLD(XtNleft, XtChainLeft);
		argLD(XtNright, XtChainLeft);
		argLD(XtNsensitive, False);
		lw = ed->ed_save = XtCreateManagedWidget("save", commandWidgetClass, frame, args, nargs);
		
	}	
	if (appResources.giveHelp) {
		/*
		 * Take label "help" from resources
		 */
		callbacks[0].callback = EditHelp;
		callbacks[0].closure = (caddr_t) 0;
		nargs = 0;
		argLD(XtNcallback, callbacks);
		argLD(XtNfromHoriz, lw);
		argLD(XtNleft, XtChainLeft);
		argLD(XtNright, XtChainLeft);
		lw = XtCreateManagedWidget("help", commandWidgetClass, frame, args, nargs);
	}
	switch (StripType(mw)) {
	case ME_MONTHLY:
		FmtDate(da, buf, sizeof buf, appResources.editfmt);
		break;
	case ME_WEEKLY:
		(void) strcpy(buf, appResources.day[da->day]);
		break;
	}
	nargs = 0;
	argLD(XtNlabel, buf);
	argLD(XtNborderWidth, 0);
	argLD(XtNfromHoriz, lw);
	argLD(XtNfromVert, NULL);
	argLD(XtNvertDistance, 2);
	argLD(XtNleft, XtChainLeft);
	argLD(XtNright, XtChainRight);
	lw = XtCreateManagedWidget("date", labelWidgetClass, frame, args, nargs);

	/*
	 * The text widget is in the pane below
	 * The Scroll Attributes are controlled from the application
	 * defaults file
	 */
	callbacks[0].callback = TextChanged;
	callbacks[0].closure = (caddr_t) ed;
	nargs = 0;
	argLD(XtNshowGrip, False);
	argLD(XtNstring, ed->ed_data);
	argLD(XtNeditType, XawtextEdit);
	argLD(XtNlength, ed->ed_size);
	argLD(XtNuseStringInPlace, True);
	argLD(XtNcallback, callbacks);
	ed->ed_text = XtCreateManagedWidget("text", asciiTextWidgetClass, et, args, nargs);

	XtPopup(ed->ed_popup, XtGrabNone);

}

/*
 * Callback for text widget
 * This gets called before the string is updated
 */
/* ARGSUSED */
static void
TextChanged(w, closure, call_data)
	Widget          w;
	caddr_t         closure;
	caddr_t         call_data;
{
	register EditLine *ed = (EditLine *) closure;

	if (MyCalendar)
		XtSetSensitive(ed->ed_save, True);
}


/* ARGSUSED */
static void
SaveEdits(w, closure, call_data)
	Widget          w;
	caddr_t         closure;
	caddr_t         call_data;
{
	register EditLine *ed = (EditLine *) closure;
	register MeWrap *mw;
	register Cardinal day;

	mw = ed->ed_meWrap;
	day = ed->ed_day;

	switch (StripType(mw)) {
	case ME_MONTHLY:
		if (WriteCalendarFile(mw, day, ed->ed_data) == False)
			return;
		break;
	case ME_WEEKLY:
		if (WriteWeeklyFile(mw, day, ed->ed_data) == False)
			return;
		break;
	}
	/*
	 * Otherwise change the displayed string
	 */
	if (mw->mw_have[day])
		XtFree(mw->mw_have[day]);
	mw->mw_have[day] = XtMalloc(strlen(ed->ed_data) + 1);
	strcpy(mw->mw_have[day], ed->ed_data);
	/*
	 * Update the visual image
	 */
	UpdateDayDetails(mw, day);
	XtSetSensitive(ed->ed_save, False);
}

static void
UpdateDayDetails(mw, day)
	MeWrap		*mw;
	Cardinal	day;
{
	Instance	*ins;
	int		nargs;
	Arg             args[3];

	XtSetArg(args[0], XtNlabel, mw->mw_have[day]);
	for (ins = mw->mw_list; ins; ins = ins->i_next) {
		nargs = 1;
		if (*mw->mw_have[day] == '\0') {
			argLD(XtNforeground, ins->i_col.fg);
			argLD(XtNbackground, ins->i_col.bg);
		}
		XtSetValues(ins->i_day_info[day], args, nargs);
	}

	/*
	 * worry about updating the memo system
	 */

	switch (StripType(mw)) {
	case ME_MONTHLY:
		if (today.day == day && today.month == mw->mw_month &&
		    today.year == mw->mw_year)
			UpdateMemo();
		break;
	case ME_WEEKLY:
		if (today.wday == day)
			UpdateMemo();
		break;
	}

}

static void
FinishEditing(w, closure, call_data)
	Widget          w;
	caddr_t         closure;
	caddr_t         call_data;
{
	register EditLine *ed = (EditLine *) closure;
	register MeWrap *mw;
	Cardinal        day;

	XtSetSensitive(ed->ed_quit, False);
	if (MyCalendar)
		XtSetSensitive(ed->ed_save, False);
	mw = ed->ed_meWrap;
	day = ed->ed_day;

	if (MyCalendar) {
		if (mw->mw_have[day] == NULL) {
			if (*ed->ed_data) {
				CheckExit(ed);
				return;
			}
		} else if (strcmp(mw->mw_have[day], ed->ed_data)) {
			CheckExit(ed);
			return;
		}
	}
	CleanEditPanel(w, ed, call_data);
}

static void
CleanEditPanel(w, closure, call_data)
	Widget          w;
	caddr_t         closure;
	caddr_t         call_data;
{
	register EditLine *ed = (EditLine *) closure;
	register EditLine *edl, *eds;
	register MeWrap *mw;
	Cardinal        day;
	Widget          popup;

	mw = ed->ed_meWrap;
	day = ed->ed_day;
	popup = ed->ed_popup;

	XtFree(ed->ed_data);

	for (edl = NULL, eds = mw->mw_ed;
	     eds;
	     edl = eds, eds = eds->ed_next) {
		if (eds == ed) {
			if (edl)
				edl->ed_next = ed->ed_next;
			else
				mw->mw_ed = ed->ed_next;
			break;
		}
	}
	XtFree((char *) ed);
	XtPopdown(popup);
	XtDestroyWidget(popup);

	SetAllButtons(mw->mw_list, day, True);
}

/*
 * We are trying to leave with saving the data
 * let us see if the user really wants to
 */
static void
CheckExit(ed)
	register EditLine *ed;
{

	DialogPopup(ed->ed_quit, CheckDia, ed, NULL);
}

/*
 * Here we do the work
 */
static void
CheckDia(pop, ed)
	Widget          pop;
	EditLine       *ed;
{
	Widget          dia;

	XtSetSensitive(ed->ed_quit, False);
	/* Take "Save file?" from resources */
	dia = XtCreateManagedWidget("check", dialogWidgetClass, pop, NULL, 0);
	XawDialogAddButton(dia, "yes", YesCheck, ed);
	XawDialogAddButton(dia, "no", NoCheck, ed);
}

/* ARGSUSED */
static void
YesCheck(w, closure, call_data)
	Widget          w;
	caddr_t         closure;
	caddr_t         call_data;
{
	SaveEdits(w, closure, call_data);
	CleanEditPanel(w, closure, call_data);
	XtDestroyWidget(XtParent(XtParent(w)));

}

/* ARGSUSED */
static void
NoCheck(w, closure, call_data)
	Widget          w;
	caddr_t         closure;
	caddr_t         call_data;
{
	CleanEditPanel(w, closure, call_data);
	XtDestroyWidget(XtParent(XtParent(w)));
}


/*
 * Slighty formatted XtError
 */
/* VARARGS1 */
void
Fatal(fmt, a, b)
	char           *fmt;
	char           *a;
	char           *b;
{
	char            buf[BUFSIZ];

	(void) sprintf(buf, fmt, a, b);
	XtAppError(appContext, buf);
	/* NOTREACHED */
}

/*
 * These routines used to insert text into a file
 * from the middle button press.
 * Code called from xcal_strip.c
 * The routine is responsible for freeing the text string
 */
void
AppendText(w, da, txt)
	Widget		w;
	Date		*da;
	String		txt;
{
	MeWrap		*mw;
	Cardinal	len;
	Cardinal	srclen;
	String		newstr;

	if ((mw = MeWrapSearch(da->year, da->month)) == NULL)
		mw = NewMeWrap();	/* shouldn`t happen */

	if (EditCheck(w, mw, da))
		return;

	/*
	 * if we have some text already, we must append the new text to it
	 */
	if (mw->mw_have[da->day]) {
		/*
		 * size of new string is sum of previous two
		 * plus two for the nulls at the end of the string
		 * plus one because we made need to insert a newline
		 * after the old text
		 */
		srclen = strlen(mw->mw_have[da->day]);
		if (srclen) {
			len = srclen + strlen(txt) +3;
			newstr = XtMalloc(len);
			strcpy(newstr, mw->mw_have[da->day]);
			if (newstr[srclen] == '\0') {
				newstr[srclen++] = '\n';
				newstr[srclen] = '\0';
			}
			strcat(newstr, txt);
			XtFree(txt);
			txt = newstr;
		}
	}

	switch (StripType(mw)) {
	case ME_MONTHLY:
		if (WriteCalendarFile(mw, da->day, txt) == False)
			return;
		break;
	case ME_WEEKLY:
		if (WriteWeeklyFile(mw, da->day, txt) == False)
			return;
		break;
	}
	/*
	 * Otherwise change the displayed string
	 */
	if (mw->mw_have[da->day])
		XtFree(mw->mw_have[da->day]);
	mw->mw_have[da->day] = txt;

	UpdateDayDetails(mw, da->day);
}