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