1205821Sedwin/*-
2330449Seadler * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3330449Seadler *
4205872Sedwin * Copyright (c) 1992-2009 Edwin Groothuis <edwin@FreeBSD.org>.
5205872Sedwin * All rights reserved.
6205821Sedwin *
7205821Sedwin * Redistribution and use in source and binary forms, with or without
8205821Sedwin * modification, are permitted provided that the following conditions
9205821Sedwin * are met:
10205821Sedwin * 1. Redistributions of source code must retain the above copyright
11205821Sedwin *    notice, this list of conditions and the following disclaimer.
12205821Sedwin * 2. Redistributions in binary form must reproduce the above copyright
13205821Sedwin *    notice, this list of conditions and the following disclaimer in the
14205821Sedwin *    documentation and/or other materials provided with the distribution.
15205821Sedwin *
16205821Sedwin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17205821Sedwin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18205821Sedwin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19205821Sedwin * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20205821Sedwin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21205821Sedwin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22205821Sedwin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23205821Sedwin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24205821Sedwin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25205821Sedwin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26205821Sedwin * SUCH DAMAGE.
27205821Sedwin *
28205821Sedwin */
29205821Sedwin
30205821Sedwin#include <sys/cdefs.h>
31205821Sedwin__FBSDID("$FreeBSD: stable/11/usr.bin/calendar/events.c 330449 2018-03-05 07:26:05Z eadler $");
32205821Sedwin
33205821Sedwin#include <sys/time.h>
34205821Sedwin#include <err.h>
35205821Sedwin#include <stdio.h>
36205821Sedwin#include <stdlib.h>
37205821Sedwin#include <string.h>
38205821Sedwin
39205821Sedwin#include "pathnames.h"
40205821Sedwin#include "calendar.h"
41205821Sedwin
42205821Sedwinstruct event *
43205821Sedwinevent_add(int year, int month, int day, char *date, int var, char *txt,
44205821Sedwin    char *extra)
45205821Sedwin{
46205821Sedwin	struct event *e;
47205821Sedwin
48205821Sedwin	/*
49205821Sedwin	 * Creating a new event:
50205821Sedwin	 * - Create a new event
51205821Sedwin	 * - Copy the machine readable day and month
52205821Sedwin	 * - Copy the human readable and language specific date
53205821Sedwin	 * - Copy the text of the event
54205821Sedwin	 */
55205821Sedwin	e = (struct event *)calloc(1, sizeof(struct event));
56205821Sedwin	if (e == NULL)
57205821Sedwin		errx(1, "event_add: cannot allocate memory");
58205821Sedwin	e->month = month;
59205821Sedwin	e->day = day;
60205821Sedwin	e->var = var;
61205821Sedwin	e->date = strdup(date);
62205821Sedwin	if (e->date == NULL)
63205821Sedwin		errx(1, "event_add: cannot allocate memory");
64205821Sedwin	e->text = strdup(txt);
65205821Sedwin	if (e->text == NULL)
66205821Sedwin		errx(1, "event_add: cannot allocate memory");
67205821Sedwin	e->extra = NULL;
68205821Sedwin	if (extra != NULL && extra[0] != '\0')
69205821Sedwin		e->extra = strdup(extra);
70205821Sedwin	addtodate(e, year, month, day);
71205821Sedwin	return (e);
72205821Sedwin}
73205821Sedwin
74205821Sedwinvoid
75205821Sedwinevent_continue(struct event *e, char *txt)
76205821Sedwin{
77205821Sedwin	char *text;
78205821Sedwin
79205821Sedwin	/*
80205821Sedwin	 * Adding text to the event:
81205821Sedwin	 * - Save a copy of the old text (unknown length, so strdup())
82205821Sedwin	 * - Allocate enough space for old text + \n + new text + 0
83205821Sedwin	 * - Store the old text + \n + new text
84205821Sedwin	 * - Destroy the saved copy.
85205821Sedwin	 */
86205821Sedwin	text = strdup(e->text);
87205821Sedwin	if (text == NULL)
88205821Sedwin		errx(1, "event_continue: cannot allocate memory");
89205821Sedwin
90205821Sedwin	free(e->text);
91205821Sedwin	e->text = (char *)malloc(strlen(text) + strlen(txt) + 3);
92205821Sedwin	if (e->text == NULL)
93205821Sedwin		errx(1, "event_continue: cannot allocate memory");
94205821Sedwin	strcpy(e->text, text);
95205821Sedwin	strcat(e->text, "\n");
96205821Sedwin	strcat(e->text, txt);
97205821Sedwin	free(text);
98205821Sedwin
99205821Sedwin	return;
100205821Sedwin}
101205821Sedwin
102205821Sedwinvoid
103205821Sedwinevent_print_all(FILE *fp)
104205821Sedwin{
105205821Sedwin	struct event *e;
106205821Sedwin
107205821Sedwin	while (walkthrough_dates(&e) != 0) {
108205821Sedwin#ifdef DEBUG
109205821Sedwin		fprintf(stderr, "event_print_allmonth: %d, day: %d\n",
110205821Sedwin		    month, day);
111205821Sedwin#endif
112205821Sedwin
113205821Sedwin		/*
114205821Sedwin		 * Go through all events and print the text of the matching
115205821Sedwin		 * dates
116205821Sedwin		 */
117205821Sedwin		while (e != NULL) {
118205821Sedwin			(void)fprintf(fp, "%s%c%s%s%s%s\n", e->date,
119205821Sedwin			    e->var ? '*' : ' ', e->text,
120205821Sedwin			    e->extra != NULL ? " (" : "",
121205821Sedwin			    e->extra != NULL ? e->extra : "",
122205821Sedwin			    e->extra != NULL ? ")" : ""
123205821Sedwin			);
124205821Sedwin
125205821Sedwin			e = e->next;
126205821Sedwin		}
127205821Sedwin	}
128205821Sedwin}
129