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