events.c revision 205872
150276Speter/*- 262449Speter * Copyright (c) 1992-2009 Edwin Groothuis <edwin@FreeBSD.org>. 350276Speter * All rights reserved. 450276Speter * 550276Speter * Redistribution and use in source and binary forms, with or without 650276Speter * modification, are permitted provided that the following conditions 750276Speter * are met: 850276Speter * 1. Redistributions of source code must retain the above copyright 950276Speter * notice, this list of conditions and the following disclaimer. 1050276Speter * 2. Redistributions in binary form must reproduce the above copyright 1150276Speter * notice, this list of conditions and the following disclaimer in the 1250276Speter * documentation and/or other materials provided with the distribution. 1350276Speter * 1450276Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1550276Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1650276Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1750276Speter * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1850276Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1950276Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2050276Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2150276Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2250276Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2350276Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2450276Speter * SUCH DAMAGE. 2550276Speter * 2650276Speter */ 2750276Speter 2850276Speter#include <sys/cdefs.h> 2950276Speter__FBSDID("$FreeBSD: head/usr.bin/calendar/events.c 205872 2010-03-30 06:42:01Z edwin $"); 3050276Speter 3150276Speter#include <sys/time.h> 3250276Speter#include <err.h> 3350276Speter#include <stdio.h> 3450276Speter#include <stdlib.h> 3550276Speter#include <string.h> 3650276Speter 3750276Speter#include "pathnames.h" 3850276Speter#include "calendar.h" 3950276Speter 4050276Speterstruct event * 4150276Speterevent_add(int year, int month, int day, char *date, int var, char *txt, 4250276Speter char *extra) 4362449Speter{ 4462449Speter struct event *e; 4550276Speter 4676726Speter /* 4750276Speter * Creating a new event: 4876726Speter * - Create a new event 4962449Speter * - Copy the machine readable day and month 5050276Speter * - Copy the human readable and language specific date 5162449Speter * - Copy the text of the event 5250276Speter */ 5362449Speter e = (struct event *)calloc(1, sizeof(struct event)); 5450276Speter if (e == NULL) 5562449Speter errx(1, "event_add: cannot allocate memory"); 5662449Speter e->month = month; 5750276Speter e->day = day; 5862449Speter e->var = var; 5962449Speter e->date = strdup(date); 6062449Speter if (e->date == NULL) 6162449Speter errx(1, "event_add: cannot allocate memory"); 6262449Speter e->text = strdup(txt); 6362449Speter if (e->text == NULL) 6462449Speter errx(1, "event_add: cannot allocate memory"); 6550276Speter e->extra = NULL; 6662449Speter if (extra != NULL && extra[0] != '\0') 6762449Speter e->extra = strdup(extra); 6850276Speter addtodate(e, year, month, day); 6950276Speter return (e); 7062449Speter} 7162449Speter 7250276Spetervoid 7362449Speterevent_continue(struct event *e, char *txt) 7462449Speter{ 7562449Speter char *text; 7662449Speter 7762449Speter /* 7850276Speter * Adding text to the event: 7950276Speter * - Save a copy of the old text (unknown length, so strdup()) 8050276Speter * - Allocate enough space for old text + \n + new text + 0 8150276Speter * - Store the old text + \n + new text 8250276Speter * - Destroy the saved copy. 8376726Speter */ 8462449Speter text = strdup(e->text); 8550276Speter if (text == NULL) 8662449Speter errx(1, "event_continue: cannot allocate memory"); 8750276Speter 8862449Speter free(e->text); 8950276Speter e->text = (char *)malloc(strlen(text) + strlen(txt) + 3); 9062449Speter if (e->text == NULL) 9162449Speter errx(1, "event_continue: cannot allocate memory"); 9262449Speter strcpy(e->text, text); 9362449Speter strcat(e->text, "\n"); 9450276Speter strcat(e->text, txt); 9562449Speter free(text); 9662449Speter 9750276Speter return; 9876726Speter} 9976726Speter 10076726Spetervoid 10162449Speterevent_print_all(FILE *fp) 10262449Speter{ 10350276Speter struct event *e; 10462449Speter 10562449Speter while (walkthrough_dates(&e) != 0) { 10650276Speter#ifdef DEBUG 10762449Speter fprintf(stderr, "event_print_allmonth: %d, day: %d\n", 10862449Speter month, day); 10950276Speter#endif 11062449Speter 11150276Speter /* 11262449Speter * Go through all events and print the text of the matching 11362449Speter * dates 11462449Speter */ 11562449Speter while (e != NULL) { 11662449Speter (void)fprintf(fp, "%s%c%s%s%s%s\n", e->date, 11762449Speter e->var ? '*' : ' ', e->text, 11862449Speter e->extra != NULL ? " (" : "", 11962449Speter e->extra != NULL ? e->extra : "", 12062449Speter e->extra != NULL ? ")" : "" 12162449Speter ); 12262449Speter 12350276Speter e = e->next; 12462449Speter } 12562449Speter } 12662449Speter} 12762449Speter