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