xref: /freebsd/usr.bin/calendar/events.c (revision 4e1bc9a039df516be13abb902ab76677fef81b1d)
1 /*-
2  * Copyright (c) 1992-2009 Edwin Groothuis <edwin@FreeBSD.org>.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/time.h>
32 #include <err.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 #include "pathnames.h"
38 #include "calendar.h"
39 
40 struct event *
41 event_add(int year, int month, int day, char *date, int var, char *txt,
42     char *extra)
43 {
44 	struct event *e;
45 
46 	/*
47 	 * Creating a new event:
48 	 * - Create a new event
49 	 * - Copy the machine readable day and month
50 	 * - Copy the human readable and language specific date
51 	 * - Copy the text of the event
52 	 */
53 	e = (struct event *)calloc(1, sizeof(struct event));
54 	if (e == NULL)
55 		errx(1, "event_add: cannot allocate memory");
56 	e->month = month;
57 	e->day = day;
58 	e->var = var;
59 	e->date = strdup(date);
60 	if (e->date == NULL)
61 		errx(1, "event_add: cannot allocate memory");
62 	e->text = strdup(txt);
63 	if (e->text == NULL)
64 		errx(1, "event_add: cannot allocate memory");
65 	e->extra = NULL;
66 	if (extra != NULL && extra[0] != '\0')
67 		e->extra = strdup(extra);
68 	addtodate(e, year, month, day);
69 	return (e);
70 }
71 
72 void
73 event_continue(struct event *e, char *txt)
74 {
75 	char *text;
76 
77 	/*
78 	 * Adding text to the event:
79 	 * - Save a copy of the old text (unknown length, so strdup())
80 	 * - Allocate enough space for old text + \n + new text + 0
81 	 * - Store the old text + \n + new text
82 	 * - Destroy the saved copy.
83 	 */
84 	text = strdup(e->text);
85 	if (text == NULL)
86 		errx(1, "event_continue: cannot allocate memory");
87 
88 	free(e->text);
89 	e->text = (char *)malloc(strlen(text) + strlen(txt) + 3);
90 	if (e->text == NULL)
91 		errx(1, "event_continue: cannot allocate memory");
92 	strcpy(e->text, text);
93 	strcat(e->text, "\n");
94 	strcat(e->text, txt);
95 	free(text);
96 
97 	return;
98 }
99 
100 void
101 event_print_all(FILE *fp)
102 {
103 	struct event *e;
104 
105 	while (walkthrough_dates(&e) != 0) {
106 #ifdef DEBUG
107 		fprintf(stderr, "event_print_allmonth: %d, day: %d\n",
108 		    month, day);
109 #endif
110 
111 		/*
112 		 * Go through all events and print the text of the matching
113 		 * dates
114 		 */
115 		while (e != NULL) {
116 			(void)fprintf(fp, "%s%c%s%s%s%s\n", e->date,
117 			    e->var ? '*' : ' ', e->text,
118 			    e->extra != NULL ? " (" : "",
119 			    e->extra != NULL ? e->extra : "",
120 			    e->extra != NULL ? ")" : ""
121 			);
122 
123 			e = e->next;
124 		}
125 	}
126 }
127