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